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

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


The following commit(s) were added to refs/heads/master by this push:
     new 787b63a4b IMPALA-14819: Calcite planner: Implement sqrt function in 
convertlet
787b63a4b is described below

commit 787b63a4b88b91bf441200e336ca0bac873541b9
Author: Steve Carlin <[email protected]>
AuthorDate: Sun Mar 8 07:48:32 2026 -0700

    IMPALA-14819: Calcite planner: Implement sqrt function in convertlet
    
    When a user uses sqrt as a function in their SQL, Calcite converts this
    to the "power()" function. While this is basically harmless, it did affect
    an e2e test, and it seems a bit more natural to keep things using the
    Impala function.
    
    Change-Id: I5b94421f568dd8d3f01738388277aed35c8993cb
    Reviewed-on: http://gerrit.cloudera.org:8080/24076
    Reviewed-by: Impala Public Jenkins <[email protected]>
    Tested-by: Impala Public Jenkins <[email protected]>
---
 .../impala/calcite/operators/ImpalaConvertletTable.java     | 13 +++++++++++++
 .../functional-query/queries/QueryTest/calcite.test         |  8 ++++++++
 .../queries/QueryTest/geospatial-esri-planner.test          |  2 +-
 3 files changed, 22 insertions(+), 1 deletion(-)

diff --git 
a/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaConvertletTable.java
 
b/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaConvertletTable.java
index a39b569f6..1aa494a13 100644
--- 
a/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaConvertletTable.java
+++ 
b/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaConvertletTable.java
@@ -48,6 +48,7 @@ public class ImpalaConvertletTable extends 
ReflectiveConvertletTable {
   public static Set<String> IMPALA_OVERRIDE_CONVERTLETS =
       ImmutableSet.<String> builder()
       .add("||")
+      .add("SQRT")
       .build();
 
   public static final ImpalaConvertletTable INSTANCE =
@@ -59,6 +60,7 @@ public class ImpalaConvertletTable extends 
ReflectiveConvertletTable {
     registerOp(SqlStdOperatorTable.IS_DISTINCT_FROM, 
this::convertIsDistinctFrom);
     registerOp(SqlStdOperatorTable.IS_NOT_DISTINCT_FROM, 
this::convertIsNotDistinctFrom);
     registerOp(ImpalaConcatOrOperator.INSTANCE, this::convertConcatOr);
+    registerOp(SqlStdOperatorTable.SQRT, this::convertSqrt);
   }
 
   @Override
@@ -146,4 +148,15 @@ public class ImpalaConvertletTable extends 
ReflectiveConvertletTable {
         : SqlStdOperatorTable.CONCAT;
     return rexBuilder.makeCall(returnType, op, operands);
   }
+
+  protected RexNode convertSqrt(
+      SqlRexContext cx, SqlCall call) {
+    final SqlNode expr1 = call.operand(0);
+    final RexBuilder rexBuilder = cx.getRexBuilder();
+    RelDataType returnType =
+        cx.getValidator().getValidatedNodeTypeIfKnown(call);
+    List<RexNode> operands = Lists.newArrayList(cx.convertExpression(expr1));
+    return rexBuilder.makeCall(returnType, SqlStdOperatorTable.SQRT,
+        operands);
+  }
 }
diff --git a/testdata/workloads/functional-query/queries/QueryTest/calcite.test 
b/testdata/workloads/functional-query/queries/QueryTest/calcite.test
index 7ed7c64eb..0d4f03767 100644
--- a/testdata/workloads/functional-query/queries/QueryTest/calcite.test
+++ b/testdata/workloads/functional-query/queries/QueryTest/calcite.test
@@ -1247,3 +1247,11 @@ NULL
 ---- RUNTIME_PROFILE
 row_regex: .*PlannerType: CalcitePlanner.*
 ====
+---- QUERY
+# test single row optimization with limit clause
+explain select id from functional.alltypes where bigint_col > sqrt(id) limit 1;
+---- RESULTS: VERIFY_IS_SUBSET
+'   predicates: functional.alltypes.bigint_col > sqrt(functional.alltypes.id)'
+---- RUNTIME_PROFILE
+row_regex: PlannerType: CalcitePlanner
+====
diff --git 
a/testdata/workloads/functional-query/queries/QueryTest/geospatial-esri-planner.test
 
b/testdata/workloads/functional-query/queries/QueryTest/geospatial-esri-planner.test
index c36dbd8ec..5d6393d10 100644
--- 
a/testdata/workloads/functional-query/queries/QueryTest/geospatial-esri-planner.test
+++ 
b/testdata/workloads/functional-query/queries/QueryTest/geospatial-esri-planner.test
@@ -5,7 +5,7 @@
 select * from functional.binary_tbl
 where st_geomfromwkb(binary_col) is not null and sqrt(id) = 1;
 ---- RUNTIME_PROFILE
-predicates: sqrt(CAST(id AS DOUBLE)) = CAST(1 AS DOUBLE), 
st_geomfromwkb(binary_col) IS NOT NULL
+row_regex: .*predicates: sqrt\(CAST\(.*id AS DOUBLE\)\) = CAST\(1 AS DOUBLE\), 
st_geomfromwkb\(.*binary_col\) IS NOT NULL
 ====
 ---- QUERY
 # Check that NormalizeGeospatialRelationsRule is applied and the const 
argument is moved

Reply via email to