This is an automated email from the ASF dual-hosted git repository. michaelsmith pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/impala.git
commit 453c9519bcadb64d8ad5a131c8d031a457dd4d22 Author: Steve Carlin <[email protected]> AuthorDate: Mon Oct 21 12:42:51 2024 -0700 IMPALA-13482: Bug fixes for lag/coalesce in analytic function. The following SQL query in analytics.test ... select lag(coalesce(505, 1 + NULL), 1) over (order by int_col desc) from alltypestiny ... had a couple of issues 1) The coalesce function needed a special operator. This function derives its return type from a common type that works for all parameters. 2) The function was not being saved when being reset. This is needed for when resetAnalysisState is called. 3) createNullLiteral needed to be overriden for similar reasons. The null literal type needs to be saved for when resetAnalysisState is called. Change-Id: Ic54d955a73cec4b5f421099a74df4172a1b7dd8b Reviewed-on: http://gerrit.cloudera.org:8080/22024 Reviewed-by: Impala Public Jenkins <[email protected]> Reviewed-by: Michael Smith <[email protected]> Tested-by: Impala Public Jenkins <[email protected]> --- .../calcite/functions/AnalyzedAnalyticExpr.java | 10 +++++ .../functions/AnalyzedFunctionCallExpr.java | 4 +- .../calcite/operators/ImpalaCoalesceFunction.java | 48 ++++++++++++++++++++++ .../operators/ImpalaCustomOperatorTable.java | 3 ++ .../calcite/operators/ImpalaOperatorTable.java | 2 + 5 files changed, 65 insertions(+), 2 deletions(-) diff --git a/java/calcite-planner/src/main/java/org/apache/impala/calcite/functions/AnalyzedAnalyticExpr.java b/java/calcite-planner/src/main/java/org/apache/impala/calcite/functions/AnalyzedAnalyticExpr.java index b9b452eb0..225f32ce7 100644 --- a/java/calcite-planner/src/main/java/org/apache/impala/calcite/functions/AnalyzedAnalyticExpr.java +++ b/java/calcite-planner/src/main/java/org/apache/impala/calcite/functions/AnalyzedAnalyticExpr.java @@ -72,4 +72,14 @@ public class AnalyzedAnalyticExpr extends AnalyticExpr { } } } + + // Overriding this method because a null literal may be created during the + // standardize() call and we want it to use the AnalyzedNullLiteral instead of + // base NullLiteral. The reason is that the base NullLiteral's + // resetAnalysisState() resets the type to NULL whereas we want any type that + // was previously assigned to the null literal to be preserved. + @Override + protected Expr createNullLiteral() { + return new AnalyzedNullLiteral(getFnCall().getParams().exprs().get(0).getType()); + } } diff --git a/java/calcite-planner/src/main/java/org/apache/impala/calcite/functions/AnalyzedFunctionCallExpr.java b/java/calcite-planner/src/main/java/org/apache/impala/calcite/functions/AnalyzedFunctionCallExpr.java index 0dd0e0157..83307f35f 100644 --- a/java/calcite-planner/src/main/java/org/apache/impala/calcite/functions/AnalyzedFunctionCallExpr.java +++ b/java/calcite-planner/src/main/java/org/apache/impala/calcite/functions/AnalyzedFunctionCallExpr.java @@ -47,7 +47,7 @@ public class AnalyzedFunctionCallExpr extends FunctionCallExpr { // resetAnalyzeState() method can be called at various points which could // set the fn_ member to null. So we save the function in the savedFunction_ // variable so it can be properly set in analyzeImpl() - private final Function savedFunction_; + private Function savedFunction_; // c'tor that takes a list of Exprs that eventually get converted to FunctionParams public AnalyzedFunctionCallExpr(Function fn, List<Expr> params, Type retType) { @@ -132,11 +132,11 @@ public class AnalyzedFunctionCallExpr extends FunctionCallExpr { } fn_ = FunctionResolver.getExactFunction(getFnName().getFunction(), ImpalaTypeConverter.createRelDataTypes(operandTypes)); + this.savedFunction_ = fn_; break; default: throw new AnalysisException("Unsupported aggregate function."); } } } - } diff --git a/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaCoalesceFunction.java b/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaCoalesceFunction.java new file mode 100644 index 000000000..e9bc58eac --- /dev/null +++ b/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaCoalesceFunction.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.impala.calcite.operators; + +import org.apache.calcite.jdbc.JavaTypeFactoryImpl; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rel.type.RelDataTypeFactory; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.sql.SqlOperatorBinding; +import org.apache.impala.calcite.type.ImpalaTypeConverter; +import org.apache.impala.calcite.type.ImpalaTypeSystemImpl; + +import java.util.List; + +/** + * Implementation of coalesce which generates a return type + * that is common to all parameters. + */ +public class ImpalaCoalesceFunction extends ImpalaOperator { + + public ImpalaCoalesceFunction() { + super("COALESCE"); + } + + @Override + public RelDataType inferReturnType(SqlOperatorBinding opBinding) { + RexBuilder rexBuilder = + new RexBuilder(new JavaTypeFactoryImpl(new ImpalaTypeSystemImpl())); + RelDataTypeFactory factory = rexBuilder.getTypeFactory(); + + List<RelDataType> operands = CommonOperatorFunctions.getOperandTypes(opBinding); + return ImpalaTypeConverter.getCompatibleType(operands, factory); + } +} diff --git a/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaCustomOperatorTable.java b/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaCustomOperatorTable.java index fc8741fc5..22006f7fd 100644 --- a/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaCustomOperatorTable.java +++ b/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaCustomOperatorTable.java @@ -192,6 +192,9 @@ public class ImpalaCustomOperatorTable extends ReflectiveSqlOperatorTable { public static final ImpalaAdjustScaleFunction DTRUNC = new ImpalaAdjustScaleFunction("DTRUNC"); + public static final ImpalaCoalesceFunction COALESCE = + new ImpalaCoalesceFunction(); + public static final ImpalaGroupingFunction GROUPING = new ImpalaGroupingFunction(); diff --git a/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaOperatorTable.java b/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaOperatorTable.java index f9dbab1ee..f7da2c637 100644 --- a/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaOperatorTable.java +++ b/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaOperatorTable.java @@ -74,6 +74,8 @@ public class ImpalaOperatorTable extends ReflectiveSqlOperatorTable { .add("corr") .add("covar_pop") .add("covar_samp") + .add("coalesce") + .add("lag") .build(); private static ImpalaOperatorTable INSTANCE;
