From 5280263232c962a61905ccaed76fedf89486268b Mon Sep 17 00:00:00 2001 From: Matthias Wahl Date: Tue, 10 Mar 2015 14:22:22 +0100 Subject: [PATCH] fix ordering with a scalar on a column containing NULL --- CHANGES.txt | 3 +++ .../action/sql/query/CrateSearchService.java | 4 ++-- .../ArithmeticIntegrationTest.java | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 7e30fa6fc291..a7191151f7b8 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -5,6 +5,9 @@ Changes for Crate Unreleased ========== + - Fix: Sorting on a scalar with a column containing NULL values as + argument no longer throws an NPE + - Fix: group by on clustered by column on a partitioned table now produces correct results for groups spanning different partitions diff --git a/sql/src/main/java/io/crate/action/sql/query/CrateSearchService.java b/sql/src/main/java/io/crate/action/sql/query/CrateSearchService.java index c8162352ee08..9c05cb656c12 100644 --- a/sql/src/main/java/io/crate/action/sql/query/CrateSearchService.java +++ b/sql/src/main/java/io/crate/action/sql/query/CrateSearchService.java @@ -499,7 +499,7 @@ public int compareBottom(int doc) throws IOException { LuceneCollectorExpression collectorExpression = collectorExpressions.get(i); collectorExpression.setNextDocId(doc); } - return valueType.compareValueTo(bottom, input.value()); + return valueType.compareValueTo(bottom, MoreObjects.firstNonNull(input.value(), missingValue)); } @SuppressWarnings("unchecked") @@ -509,7 +509,7 @@ public int compareTop(int doc) throws IOException { LuceneCollectorExpression collectorExpression = collectorExpressions.get(i); collectorExpression.setNextDocId(doc); } - return valueType.compareValueTo(top, input.value()); + return valueType.compareValueTo(top, MoreObjects.firstNonNull(input.value(), missingValue)); } @Override diff --git a/sql/src/test/java/io/crate/integrationtests/ArithmeticIntegrationTest.java b/sql/src/test/java/io/crate/integrationtests/ArithmeticIntegrationTest.java index e6e9530193cb..710e2b3e190e 100644 --- a/sql/src/test/java/io/crate/integrationtests/ArithmeticIntegrationTest.java +++ b/sql/src/test/java/io/crate/integrationtests/ArithmeticIntegrationTest.java @@ -383,4 +383,20 @@ public void testArithmeticScalarFunctionsOnAllTypes() throws Exception { execute(String.format(Locale.ENGLISH, "select %s, d from t where %s < 2", doubleCall, doubleCall)); } } + + @Test + public void testSelectOrderByScalarOnNullValue() throws Exception { + execute("create table t (d long, name string) clustered into 1 shards with (number_of_replicas=0)"); + ensureGreen(); + execute("insert into t (d, name) values (?, ?)", new Object[][] { + new Object[] {-7L, "Marvin" }, + new Object[] {null, "Arthur" }, + new Object[] {5L, "Trillian" }, + new Object[] {null, "Trillian" } + + } ); + execute("refresh table t"); + execute("select (d - 10) from t order by (d - 10) nulls first limit 2"); + assertThat(response.rows()[0][0], is(nullValue())); + } }