Search in sources :

Example 6 with BitString

use of io.crate.sql.tree.BitString in project crate by crate.

the class BitStringQueryTest method test_eq_any_on_bits_uses_termset_query.

@Test
public void test_eq_any_on_bits_uses_termset_query() throws Exception {
    var builder = builder("create table tbl (bits bit(8))");
    BitString bits1 = BitString.ofRawBits("00010110");
    BitString bits2 = BitString.ofRawBits("01110000");
    builder.indexValues("bits", bits1, bits2);
    try (var tester = builder.build()) {
        Query query = tester.toQuery("bits = ANY([B'01001110', B'11111111'])");
        assertThat(query, instanceOf(TermInSetQuery.class));
        assertThat(query.toString(), is("bits:(r [ff])"));
        List<Object> result = tester.runQuery("bits", "bits = ANY([B'01001110', B'11111111'])");
        assertThat(result, Matchers.empty());
        result = tester.runQuery("bits", "bits = ANY([B'00010110', B'11111111'])");
        assertThat(result, Matchers.contains(bits1));
        result = tester.runQuery("bits", "bits = ANY([B'00010110', B'01110000'])");
        assertThat(result, Matchers.contains(bits1, bits2));
    }
}
Also used : BitString(io.crate.sql.tree.BitString) Query(org.apache.lucene.search.Query) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) TermQuery(org.apache.lucene.search.TermQuery) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 7 with BitString

use of io.crate.sql.tree.BitString 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 8 with BitString

use of io.crate.sql.tree.BitString in project crate by crate.

the class BitStringTypeTest method test_explicit_cast_can_trim_bitstring.

@Test
public void test_explicit_cast_can_trim_bitstring() throws Exception {
    BitStringType type = new BitStringType(3);
    BitString result = type.explicitCast(BitString.ofRawBits("1111"));
    assertThat(result, is(BitString.ofRawBits("111")));
}
Also used : BitString(io.crate.sql.tree.BitString) Test(org.junit.Test)

Example 9 with BitString

use of io.crate.sql.tree.BitString in project crate by crate.

the class ServerXContentExtension method getXContentWriters.

@Override
public Map<Class<?>, XContentBuilder.Writer> getXContentWriters() {
    Map<Class<?>, XContentBuilder.Writer> writers = new HashMap<>();
    // Fully-qualified here to reduce ambiguity around our (ES') Version class
    writers.put(org.apache.lucene.util.Version.class, (b, v) -> b.value(Objects.toString(v)));
    writers.put(DateTimeZone.class, (b, v) -> b.value(Objects.toString(v)));
    writers.put(CachedDateTimeZone.class, (b, v) -> b.value(Objects.toString(v)));
    writers.put(FixedDateTimeZone.class, (b, v) -> b.value(Objects.toString(v)));
    writers.put(MutableDateTime.class, XContentBuilder::timeValue);
    writers.put(DateTime.class, XContentBuilder::timeValue);
    writers.put(TimeValue.class, (b, v) -> b.value(v.toString()));
    writers.put(ZonedDateTime.class, XContentBuilder::timeValue);
    writers.put(OffsetDateTime.class, XContentBuilder::timeValue);
    writers.put(OffsetTime.class, XContentBuilder::timeValue);
    writers.put(java.time.Instant.class, XContentBuilder::timeValue);
    writers.put(LocalDateTime.class, XContentBuilder::timeValue);
    writers.put(LocalDate.class, XContentBuilder::timeValue);
    writers.put(LocalTime.class, XContentBuilder::timeValue);
    writers.put(DayOfWeek.class, (b, v) -> b.value(v.toString()));
    writers.put(Month.class, (b, v) -> b.value(v.toString()));
    writers.put(MonthDay.class, (b, v) -> b.value(v.toString()));
    writers.put(Year.class, (b, v) -> b.value(v.toString()));
    writers.put(Duration.class, (b, v) -> b.value(v.toString()));
    writers.put(Period.class, (b, v) -> b.value(v.toString()));
    writers.put(org.joda.time.Period.class, (b, v) -> {
        org.joda.time.Period period = (org.joda.time.Period) v;
        b.value(IntervalType.PERIOD_FORMATTER.print(period));
    });
    writers.put(BytesReference.class, (b, v) -> {
        if (v == null) {
            b.nullValue();
        } else {
            BytesRef bytes = ((BytesReference) v).toBytesRef();
            b.value(bytes.bytes, bytes.offset, bytes.length);
        }
    });
    writers.put(BytesRef.class, (b, v) -> {
        if (v == null) {
            b.nullValue();
        } else {
            BytesRef bytes = (BytesRef) v;
            b.value(bytes.bytes, bytes.offset, bytes.length);
        }
    });
    writers.put(Regproc.class, (b, v) -> b.value(((Regproc) v).name()));
    writers.put(Regclass.class, (b, v) -> b.value(((Regclass) v).oid()));
    writers.put(PointImpl.class, (b, v) -> {
        Point point = (Point) v;
        b.startArray();
        b.value(point.getX());
        b.value(point.getY());
        b.endArray();
    });
    writers.put(JtsPoint.class, (b, v) -> {
        Point point = (Point) v;
        b.startArray();
        b.value(point.getX());
        b.value(point.getY());
        b.endArray();
    });
    writers.put(RowN.class, (b, v) -> {
        RowN row = (RowN) v;
        b.startArray();
        for (int i = 0; i < row.numColumns(); i++) {
            b.value(row.get(i));
        }
        b.endArray();
    });
    writers.put(TimeTZ.class, (b, v) -> {
        TimeTZ timetz = (TimeTZ) v;
        b.startArray();
        b.value(timetz.getMicrosFromMidnight());
        b.value(timetz.getSecondsFromUTC());
        b.endArray();
    });
    writers.put(BitString.class, (b, v) -> {
        BitString bitString = (BitString) v;
        b.value(bitString.asBitString());
    });
    return writers;
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) TimeTZ(io.crate.types.TimeTZ) HashMap(java.util.HashMap) Period(java.time.Period) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) Point(org.locationtech.spatial4j.shape.Point) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) Point(org.locationtech.spatial4j.shape.Point) Regproc(io.crate.types.Regproc) RowN(io.crate.data.RowN) BitString(io.crate.sql.tree.BitString) Regclass(io.crate.types.Regclass) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

BitString (io.crate.sql.tree.BitString)9 Test (org.junit.Test)6 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)2 BitStringType (io.crate.types.BitStringType)2 Query (org.apache.lucene.search.Query)2 TermInSetQuery (org.apache.lucene.search.TermInSetQuery)2 TermQuery (org.apache.lucene.search.TermQuery)2 RowN (io.crate.data.RowN)1 GroupByOnArrayUnsupportedException (io.crate.exceptions.GroupByOnArrayUnsupportedException)1 Regclass (io.crate.types.Regclass)1 Regproc (io.crate.types.Regproc)1 TimeTZ (io.crate.types.TimeTZ)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 Period (java.time.Period)1 BitSet (java.util.BitSet)1 HashMap (java.util.HashMap)1 BytesRef (org.apache.lucene.util.BytesRef)1 BytesReference (org.elasticsearch.common.bytes.BytesReference)1 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)1