Search in sources :

Example 1 with BitStringType

use of io.crate.types.BitStringType in project crate by crate.

the class DocIndexMetadata method getColumnDataType.

/**
 * extract dataType from given columnProperties
 *
 * @param columnProperties map of String to Object containing column properties
 * @return dataType of the column with columnProperties
 */
public static DataType<?> getColumnDataType(Map<String, Object> columnProperties) {
    DataType<?> type;
    String typeName = (String) columnProperties.get("type");
    if (typeName == null || ObjectType.NAME.equals(typeName)) {
        Map<String, Object> innerProperties = (Map<String, Object>) columnProperties.get("properties");
        if (innerProperties != null) {
            ObjectType.Builder builder = ObjectType.builder();
            for (Map.Entry<String, Object> entry : innerProperties.entrySet()) {
                builder.setInnerType(entry.getKey(), getColumnDataType((Map<String, Object>) entry.getValue()));
            }
            type = builder.build();
        } else {
            type = Objects.requireNonNullElse(DataTypes.ofMappingName(typeName), DataTypes.NOT_SUPPORTED);
        }
    } else if (typeName.equalsIgnoreCase("array")) {
        Map<String, Object> innerProperties = Maps.get(columnProperties, "inner");
        DataType<?> innerType = getColumnDataType(innerProperties);
        type = new ArrayType<>(innerType);
    } else {
        typeName = typeName.toLowerCase(Locale.ENGLISH);
        switch(typeName) {
            case DateFieldMapper.CONTENT_TYPE:
                Boolean ignoreTimezone = (Boolean) columnProperties.get("ignore_timezone");
                if (ignoreTimezone != null && ignoreTimezone) {
                    return DataTypes.TIMESTAMP;
                } else {
                    return DataTypes.TIMESTAMPZ;
                }
            case KeywordFieldMapper.CONTENT_TYPE:
                Integer lengthLimit = (Integer) columnProperties.get("length_limit");
                return lengthLimit != null ? StringType.of(lengthLimit) : DataTypes.STRING;
            case BitStringFieldMapper.CONTENT_TYPE:
                Integer length = (Integer) columnProperties.get("length");
                assert length != null : "Length is required for bit string type";
                return new BitStringType(length);
            default:
                type = Objects.requireNonNullElse(DataTypes.ofMappingName(typeName), DataTypes.NOT_SUPPORTED);
        }
    }
    return type;
}
Also used : BitStringType(io.crate.types.BitStringType) ArrayType(io.crate.types.ArrayType) ObjectType(io.crate.types.ObjectType) DataType(io.crate.types.DataType) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with BitStringType

use of io.crate.types.BitStringType in project crate by crate.

the class PGTypesTest method test_bit_binary_round_trip_streaming.

@Test
public void test_bit_binary_round_trip_streaming() {
    int bitLength = randomIntBetween(1, 40);
    BitStringType type = new BitStringType(bitLength);
    Supplier<BitString> dataGenerator = DataTypeTesting.getDataGenerator(type);
    PGType<?> bitType = PGTypes.get(type);
    Entry entry = new Entry(type, dataGenerator.get());
    assertThat(writeAndReadBinary(entry, bitType), is(entry.value));
}
Also used : BitString(io.crate.sql.tree.BitString) BitStringType(io.crate.types.BitStringType) Test(org.junit.Test)

Example 3 with BitStringType

use of io.crate.types.BitStringType in project crate by crate.

the class BitStringQueryTest method test_eq_on_bits_uses_term_query.

@Test
public void test_eq_on_bits_uses_term_query() throws Exception {
    var builder = builder("create table tbl (bits bit(8))");
    BitStringType bitStringType = new BitStringType(8);
    Supplier<BitString> dataGenerator = DataTypeTesting.getDataGenerator(bitStringType);
    BitString bits1 = dataGenerator.get();
    BitString bitsNoMatch = dataGenerator.get();
    while (bits1.equals(bitsNoMatch)) {
        bitsNoMatch = dataGenerator.get();
    }
    builder.indexValues("bits", bits1);
    try (var tester = builder.build()) {
        Query query = tester.toQuery("bits = ?", bits1);
        assertThat(query, instanceOf(TermQuery.class));
        assertThat(tester.runQuery("bits", "bits = ?", bits1), Matchers.contains(bits1));
        assertThat(tester.runQuery("bits", "bits = ?", bitsNoMatch), Matchers.empty());
    }
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) BitString(io.crate.sql.tree.BitString) Query(org.apache.lucene.search.Query) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) TermQuery(org.apache.lucene.search.TermQuery) BitStringType(io.crate.types.BitStringType) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 4 with BitStringType

use of io.crate.types.BitStringType in project crate by crate.

the class CountAggregationTest method test_count_with_bitstring_argument.

@Test
public void test_count_with_bitstring_argument() throws Exception {
    BitStringType bitStringType = new BitStringType(4);
    Object[][] rows = new Object[][] { { BitString.ofRawBits("0100") }, { null }, { BitString.ofRawBits("0110") } };
    assertThat(executeAggregation(bitStringType, rows), is(2L));
    assertHasDocValueAggregator("count", List.of(bitStringType));
}
Also used : BitStringType(io.crate.types.BitStringType) Test(org.junit.Test)

Aggregations

BitStringType (io.crate.types.BitStringType)4 Test (org.junit.Test)3 BitString (io.crate.sql.tree.BitString)2 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)1 ArrayType (io.crate.types.ArrayType)1 DataType (io.crate.types.DataType)1 ObjectType (io.crate.types.ObjectType)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Query (org.apache.lucene.search.Query)1 TermInSetQuery (org.apache.lucene.search.TermInSetQuery)1 TermQuery (org.apache.lucene.search.TermQuery)1