From 84ad2142aeccbcb89da89ff0d4934328b975a59e Mon Sep 17 00:00:00 2001 From: Minsoo Jee Date: Tue, 16 Jan 2024 17:46:41 -0500 Subject: [PATCH] Backport test cases only from #15280 --- .../crate/lucene/CommonQueryBuilderTest.java | 29 +++++++++++++++++++ .../java/io/crate/testing/QueryTester.java | 18 ++++++++++++ 2 files changed, 47 insertions(+) diff --git a/server/src/test/java/io/crate/lucene/CommonQueryBuilderTest.java b/server/src/test/java/io/crate/lucene/CommonQueryBuilderTest.java index e339cb48ab92..8bf53ee4bb73 100644 --- a/server/src/test/java/io/crate/lucene/CommonQueryBuilderTest.java +++ b/server/src/test/java/io/crate/lucene/CommonQueryBuilderTest.java @@ -706,4 +706,33 @@ public void test_any_neq_operator_maps_column_names_to_oids() throws Exception { assertThat(tester.runQuery("a", "a != any(['s'])")).containsExactly("t"); } } + + @Test + public void test_neq_operator_on_nullable_and_not_nullable_args_does_not_filter_nulls_from_non_nullable_arg() throws Exception { + long[] oid = new long[] {123, 124}; + int[] oidIdx = new int[]{0}; + try (QueryTester tester = new QueryTester.Builder( + createTempDir(), + THREAD_POOL, + clusterService, + Version.CURRENT, + "create table t (a int, b int)", + () -> oid[oidIdx[0]++]) // oid mapping: a: 123, b: 124 + .indexValues(List.of("a", "b"), null, null) + .indexValues(List.of("a", "b"), null, 2) + .indexValues(List.of("a", "b"), 2, null) + .indexValues(List.of("a", "b"), 2, 2) + .build()) { + assertThat(oidIdx[0]).isEqualTo(2); + Query query = tester.toQuery("a != b||1"); // where a is nullable and b||1 is not null + assertThat(query).hasToString("+(+*:* -(a = concat(b, '1'))) #(NOT (a = concat(b, '1')))"); + assertThat(tester.runQuery("b", "a != b||1")).containsExactlyInAnyOrder(2, null); + } + } + + @Test + public void test_nested_not_operators() { + Query query = convert("not (name is not null)"); + assertThat(query).hasToString("+(+*:* -FieldExistsQuery [field=name]) #(NOT (NOT (name IS NULL)))"); + } } diff --git a/server/src/testFixtures/java/io/crate/testing/QueryTester.java b/server/src/testFixtures/java/io/crate/testing/QueryTester.java index 400afd274daf..dde5c73b1a78 100644 --- a/server/src/testFixtures/java/io/crate/testing/QueryTester.java +++ b/server/src/testFixtures/java/io/crate/testing/QueryTester.java @@ -45,6 +45,7 @@ import io.crate.analyze.relations.DocTableRelation; import io.crate.common.collections.Iterables; +import io.crate.common.collections.Lists2; import io.crate.data.Input; import io.crate.execution.dml.IndexItem; import io.crate.execution.dml.Indexer; @@ -154,6 +155,23 @@ public Builder indexValue(String column, Object value) throws IOException { return this; } + public Builder indexValues(List columns, Object ... values) throws IOException { + MapperService mapperService = indexEnv.mapperService(); + Indexer indexer = new Indexer( + table.concreteIndices()[0], + table, + plannerContext.transactionContext(), + plannerContext.nodeContext(), + mapperService::getLuceneFieldType, + Lists2.map(columns, c -> table.getReference(ColumnIdent.fromPath(c))), + null + ); + var item = new IndexItem.StaticItem("dummy-id", List.of(), values, -1L, -1L); + ParsedDocument parsedDocument = indexer.index(item); + indexEnv.writer().addDocument(parsedDocument.doc()); + return this; + } + private LuceneBatchIterator getIterator(ColumnIdent column, Query query) { InputFactory inputFactory = new InputFactory(plannerContext.nodeContext()); InputFactory.Context> ctx = inputFactory.ctxForRefs(