This is an automated email from the ASF dual-hosted git repository.

rohangarg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/druid.git


The following commit(s) were added to refs/heads/master by this push:
     new 4d8feeb279 Fix planning in CASE expressions with complex WHEN and ELSE 
expressions (#14220)
4d8feeb279 is described below

commit 4d8feeb279cc144998e06bd2c9ab30044a77862b
Author: Rohan Garg <[email protected]>
AuthorDate: Mon May 8 11:35:04 2023 +0530

    Fix planning in CASE expressions with complex WHEN and ELSE expressions 
(#14220)
---
 .../expression/builtin/CaseOperatorConversion.java | 20 ++++----------
 .../apache/druid/sql/calcite/CalciteQueryTest.java | 32 ++++++++++++++++++++++
 2 files changed, 37 insertions(+), 15 deletions(-)

diff --git 
a/sql/src/main/java/org/apache/druid/sql/calcite/expression/builtin/CaseOperatorConversion.java
 
b/sql/src/main/java/org/apache/druid/sql/calcite/expression/builtin/CaseOperatorConversion.java
index 45282e86fa..392a965fa1 100644
--- 
a/sql/src/main/java/org/apache/druid/sql/calcite/expression/builtin/CaseOperatorConversion.java
+++ 
b/sql/src/main/java/org/apache/druid/sql/calcite/expression/builtin/CaseOperatorConversion.java
@@ -67,7 +67,7 @@ public class CaseOperatorConversion implements 
SqlOperatorConversion
     // at the native layer
     // this conversion won't help if the condition expression is only part of 
then expression, like if the input
     // expression to coalesce was an expression itself, but this is better 
than nothing
-    if (druidExpressions.size() == 3) {
+    if (druidExpressions != null && druidExpressions.size() == 3) {
       final DruidExpression condition = druidExpressions.get(0);
       final DruidExpression thenExpression = druidExpressions.get(1);
       final DruidExpression elseExpression = druidExpressions.get(2);
@@ -109,7 +109,7 @@ public class CaseOperatorConversion implements 
SqlOperatorConversion
 
     // rewrite case_searched(notnull(someColumn), then, else) into better 
native filters
     //    or(then, and(else, isNull(someColumn))
-    if (druidExpressions.size() == 3) {
+    if (druidExpressions != null && druidExpressions.size() == 3) {
       final DruidExpression condition = druidExpressions.get(0);
       final DruidExpression thenExpression = druidExpressions.get(1);
       final DruidExpression elseExpression = druidExpressions.get(2);
@@ -124,15 +124,10 @@ public class CaseOperatorConversion implements 
SqlOperatorConversion
 
         if (call.getOperands().get(1) instanceof RexCall) {
           final RexCall thenCall = (RexCall) call.getOperands().get(1);
-          final SqlOperatorConversion thenConversion = 
plannerContext.getPlannerToolbox()
-                                                                     
.operatorTable()
-                                                                     
.lookupOperatorConversion(thenCall.getOperator());
-          if (thenConversion != null) {
-            thenFilter = thenConversion.toDruidFilter(plannerContext, 
rowSignature, virtualColumnRegistry, thenCall);
-          }
+          thenFilter = Expressions.toFilter(plannerContext, rowSignature, 
virtualColumnRegistry, thenCall);
         }
 
-        if (call.getOperands().get(2) instanceof RexLiteral) {
+        if (thenFilter != null && call.getOperands().get(2) instanceof 
RexLiteral) {
           if (call.getOperands().get(2).isAlwaysTrue()) {
             return new OrDimFilter(thenFilter, isNull);
           } else {
@@ -141,12 +136,7 @@ public class CaseOperatorConversion implements 
SqlOperatorConversion
           }
         } else if (call.getOperands().get(2) instanceof RexCall) {
           RexCall elseCall = (RexCall) call.getOperands().get(2);
-          SqlOperatorConversion elseConversion = 
plannerContext.getPlannerToolbox()
-                                                               .operatorTable()
-                                                               
.lookupOperatorConversion(elseCall.getOperator());
-          if (elseConversion != null) {
-            elseFilter = elseConversion.toDruidFilter(plannerContext, 
rowSignature, virtualColumnRegistry, elseCall);
-          }
+          elseFilter = Expressions.toFilter(plannerContext, rowSignature, 
virtualColumnRegistry, elseCall);
         }
 
         // if either then or else filters produced a native filter (that 
wasn't just another expression filter)
diff --git 
a/sql/src/test/java/org/apache/druid/sql/calcite/CalciteQueryTest.java 
b/sql/src/test/java/org/apache/druid/sql/calcite/CalciteQueryTest.java
index 14d8cba123..54036f705a 100644
--- a/sql/src/test/java/org/apache/druid/sql/calcite/CalciteQueryTest.java
+++ b/sql/src/test/java/org/apache/druid/sql/calcite/CalciteQueryTest.java
@@ -14642,4 +14642,36 @@ public class CalciteQueryTest extends 
BaseCalciteQueryTest
         )
     );
   }
+
+  @Test
+  public void testFilterWithNVLAndNotIn()
+  {
+    testQuery(
+        "select __time, dim1 from druid.foo where nvl(dim1, '') NOT IN ('a' , 
'')",
+        ImmutableList.of(
+            newScanQueryBuilder()
+                .intervals(querySegmentSpec(Filtration.eternity()))
+                .dataSource(new TableDataSource(CalciteTests.DATASOURCE1))
+                .context(QUERY_CONTEXT_DEFAULT)
+                .intervals(querySegmentSpec(Intervals.of(
+                    
"-146136543-09-08T08:23:32.096Z/146140482-04-24T15:36:27.903Z")))
+                .columns(ImmutableList.of("__time", "dim1"))
+                .filters(and(
+                    or(
+                        not(selector("dim1", "a", null)),
+                        selector("dim1", null, null)
+                    ),
+                    not(selector("dim1", NullHandling.sqlCompatible() ? "" : 
null, null))
+                ))
+                .build()
+        ),
+        ImmutableList.of(
+            new Object[]{946771200000L, "10.1"},
+            new Object[]{946857600000L, "2"},
+            new Object[]{978307200000L, "1"},
+            new Object[]{978393600000L, "def"},
+            new Object[]{978480000000L, "abc"}
+        )
+    );
+  }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to