You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@impala.apache.org by mi...@apache.org on 2026/10/06 20:25:09 UTC
(impala) branch master updated (346901c75 -> b2dc597ef)
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/impala.git
from 346901c75 IMPALA-11917: Update LLVM CPU attribute whitelists for LLVM 22
new 91f13aabd IMPALA-14946: Catch "trunc" error within Calcite planner
new b2dc597ef IMPALA-15484: Calcite planner: Support temporal TRUNC overloads
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../operators/ImpalaAdjustScaleFunction.java | 48 ++++++-
.../impala/calcite/service/CalciteTruncTest.java | 139 +++++++++++++++++++++
.../functional-query/queries/QueryTest/exprs.test | 10 ++
3 files changed, 193 insertions(+), 4 deletions(-)
create mode 100644 java/calcite-planner/src/test/java/org/apache/impala/calcite/service/CalciteTruncTest.java
(impala) 02/02: IMPALA-15484: Calcite planner: Support temporal TRUNC overloads
Posted by mi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/impala.git
commit b2dc597ef32172ea29ee587556c565eb55308919
Author: Aleksandr Efimov <dz...@gmail.com>
AuthorDate: Mon Oct 5 17:57:17 2026 +0000
IMPALA-15484: Calcite planner: Support temporal TRUNC overloads
Resolve temporal TRUNC calls through the native function resolver,
including DATE, TIMESTAMP and legacy string-date inputs. Keep numeric
scale inference unchanged and reject numeric temporal unit arguments.
Cover nested temporal views with full result metadata parity.
Tests: 19 focused Maven methods standalone and 50 with the six pilot fixes.
The unchanged production delta passed 84 strict HS2 executions on 2f022a5,
also with Gerrit 24260 PS6. Native 2f022a5, codegen disabled.
Change-Id: I9e92f3c9de42ade5630673ee9f062ae675458ad7
Assisted-by: GPT-6 (Codex)
Reviewed-on: http://gerrit.cloudera.org:8080/25019
Reviewed-by: Steve Carlin <sc...@cloudera.com>
Tested-by: Impala Public Jenkins <im...@cloudera.com>
Reviewed-by: Michael Smith <mi...@cloudera.com>
---
.../operators/ImpalaAdjustScaleFunction.java | 33 ++++-
.../impala/calcite/service/CalciteTruncTest.java | 139 +++++++++++++++++++++
2 files changed, 171 insertions(+), 1 deletion(-)
diff --git a/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaAdjustScaleFunction.java b/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaAdjustScaleFunction.java
index 9a11fcb56..6015ca13e 100644
--- a/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaAdjustScaleFunction.java
+++ b/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaAdjustScaleFunction.java
@@ -19,11 +19,13 @@ package org.apache.impala.calcite.operators;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.sql.SqlFunction;
+import org.apache.calcite.sql.SqlCallBinding;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlOperatorBinding;
import org.apache.calcite.sql.type.OperandTypes;
import org.apache.calcite.sql.SqlFunctionCategory;
import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeFamily;
import org.apache.impala.catalog.ScalarType;
import org.apache.impala.catalog.Type;
import org.apache.impala.calcite.type.ImpalaTypeConverter;
@@ -36,12 +38,41 @@ public class ImpalaAdjustScaleFunction extends SqlFunction {
public ImpalaAdjustScaleFunction(String name) {
super(name, null, SqlKind.OTHER_FUNCTION,
- null, null, OperandTypes.NUMERIC_OPTIONAL_INTEGER,
+ null, null, name.equals("TRUNC")
+ ? OperandTypes.or(OperandTypes.NUMERIC_OPTIONAL_INTEGER,
+ OperandTypes.family(SqlTypeFamily.DATETIME, SqlTypeFamily.CHARACTER),
+ OperandTypes.family(SqlTypeFamily.CHARACTER, SqlTypeFamily.CHARACTER))
+ : OperandTypes.NUMERIC_OPTIONAL_INTEGER,
SqlFunctionCategory.NUMERIC);
}
+ @Override
+ public boolean checkOperandTypes(SqlCallBinding binding, boolean throwOnFailure) {
+ if (getName().equals("TRUNC") && binding.getOperandCount() == 2
+ && SqlTypeFamily.DATETIME.contains(binding.getOperandType(0))
+ && !SqlTypeFamily.CHARACTER.contains(binding.getOperandType(1))
+ && binding.getOperandType(1).getSqlTypeName() != SqlTypeName.NULL) {
+ if (throwOnFailure) throw binding.newValidationSignatureError();
+ return false;
+ }
+ return super.checkOperandTypes(binding, throwOnFailure);
+ }
+
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
+ // Only TRUNC has temporal overloads. Preserve the numeric scale rules for
+ // TRUNC and its numeric aliases, and use native resolution for temporal calls.
+ SqlTypeName inputType = opBinding.getOperandType(0).getSqlTypeName();
+ if (getName().equals("TRUNC") && opBinding.getOperandCount() == 2
+ && (SqlTypeFamily.DATETIME.contains(opBinding.getOperandType(0))
+ || SqlTypeFamily.CHARACTER.contains(opBinding.getOperandType(0))
+ || (inputType == SqlTypeName.NULL
+ && (SqlTypeFamily.CHARACTER.contains(opBinding.getOperandType(1))
+ || opBinding.getOperandType(1).getSqlTypeName()
+ == SqlTypeName.NULL)))) {
+ return CommonOperatorFunctions.inferReturnType(opBinding, getName());
+ }
+
if (opBinding.getOperandType(0).getSqlTypeName().equals(SqlTypeName.DOUBLE) ||
opBinding.getOperandType(0).getSqlTypeName().equals(SqlTypeName.FLOAT)) {
return ImpalaTypeConverter.getRelDataType(Type.DOUBLE);
diff --git a/java/calcite-planner/src/test/java/org/apache/impala/calcite/service/CalciteTruncTest.java b/java/calcite-planner/src/test/java/org/apache/impala/calcite/service/CalciteTruncTest.java
new file mode 100644
index 000000000..33633a00c
--- /dev/null
+++ b/java/calcite-planner/src/test/java/org/apache/impala/calcite/service/CalciteTruncTest.java
@@ -0,0 +1,139 @@
+// 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.service;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import org.apache.impala.analysis.AnalysisContext;
+import org.apache.impala.analysis.AnalysisContext.AnalysisResult;
+import org.apache.impala.catalog.Column;
+import org.apache.impala.catalog.Type;
+import org.apache.impala.common.AnalysisException;
+import org.apache.impala.common.FrontendTestBase;
+import org.apache.impala.planner.PlannerContext;
+import org.apache.impala.planner.SingleNodePlannerIntf;
+import org.apache.impala.service.CompilerFactory;
+import org.apache.impala.service.CompilerFactoryImpl;
+import org.apache.impala.thrift.TResultSetMetadata;
+import org.apache.impala.util.EventSequence;
+import org.junit.Test;
+
+/** Temporal TRUNC must resolve native overloads without changing numeric calls. */
+public class CalciteTruncTest extends FrontendTestBase {
+ private TResultSetMetadata metadata(String sql, boolean calcite) throws Exception {
+ CompilerFactory factory = calcite
+ ? new CalciteCompilerFactory() : new CompilerFactoryImpl();
+ AnalysisContext ctx = feFixture_.createAnalysisCtx();
+ ctx.getQueryCtx().client_request.setStmt(sql);
+ AnalysisResult result = parseAndAnalyze(sql, ctx, factory);
+ SingleNodePlannerIntf planner = factory.createSingleNodePlanner(new PlannerContext(
+ result, ctx.getQueryCtx(), new EventSequence("TRUNC parity")));
+ planner.createSingleNodePlan();
+ return planner.getTResultSetMetadata(result.getParsedStmt());
+ }
+
+ private void assertParity(String expression) throws Exception {
+ String sql = "select " + expression + " as value";
+ assertEquals(sql, metadata(sql, false), metadata(sql, true));
+ }
+
+ @Test
+ public void testTimestampUnits() throws Exception {
+ for (String unit : new String[] {"DD", "MM", "YEAR", "Q", "DAY", "HH", "MI"}) {
+ assertParity("trunc(cast('2026-01-15 12:34:56' as timestamp),'" + unit + "')");
+ }
+ }
+
+ @Test
+ public void testDateUnits() throws Exception {
+ for (String unit : new String[] {"DD", "MM", "YEAR", "Q", "DAY"}) {
+ assertParity("trunc(cast('2026-01-15' as date),'" + unit + "')");
+ }
+ }
+
+ @Test
+ public void testNullAndStringOperands() throws Exception {
+ assertParity("trunc(cast(null as timestamp),'DD')");
+ assertParity("trunc(cast(null as date),'MM')");
+ assertParity("trunc(null,'DD')");
+ assertParity("trunc(null,null)");
+ assertParity("trunc(cast('2026-01-15' as date),null)");
+ assertParity("trunc('2014-11-11','DD')");
+ assertEquals(metadata("select trunc('2014-11-11','DD')", false),
+ metadata("select trunc('2014-11-11','DD')", true));
+ }
+
+ @Test
+ public void testNumericAliases() throws Exception {
+ for (String name : new String[] {"trunc", "truncate", "dtrunc", "round", "dround"}) {
+ String decimalType = name.equals("round") || name.equals("dround")
+ ? "decimal(38,3)" : "decimal(15,3)";
+ assertParity(name + "(cast(1.234 as " + decimalType + "),3)");
+ assertParity(name + "(cast(1.234 as double))");
+ }
+ }
+
+ @Test
+ public void testColumnsWithViewAndDynamicUnit() throws Exception {
+ addTestDb("trunc_test", "Synthetic TRUNC metadata");
+ addTestTable("create table trunc_test.t (ts timestamp, d date, unit string) "
+ + "stored as parquet location '/trunc-test/t'");
+ addTestView("create view trunc_test.v as select trunc(ts,'DD') value "
+ + "from trunc_test.t").addColumn(new Column("value", Type.TIMESTAMP, 0));
+ for (String sql : new String[] {
+ "select trunc(ts,'DD') value, trunc(d,'MM') d from trunc_test.t",
+ "with t as (select * from trunc_test.t) select trunc(ts,'DAY') value from t",
+ "select value from trunc_test.v",
+ "select trunc(ts,unit) value from trunc_test.t",
+ "select dayofyear(trunc('2014-11-11',unit)) value from trunc_test.t limit 1"}) {
+ assertEquals(sql, metadata(sql, false), metadata(sql, true));
+ }
+ }
+
+ @Test
+ public void testNestedTimestampView() throws Exception {
+ addTestDb("trunc_nested_test", "Synthetic nested TRUNC view");
+ addTestTable("create table trunc_nested_test.t (ts timestamp) "
+ + "stored as parquet location '/trunc-nested-test/t'");
+ addTestView("create view trunc_nested_test.v as select q.value from "
+ + "(select trunc(ts,'DD') value from trunc_nested_test.t) q")
+ .addColumn(new Column("value", Type.TIMESTAMP, 0));
+ String sql = "select value from trunc_nested_test.v";
+ assertEquals(sql, metadata(sql, false), metadata(sql, true));
+ }
+
+ @Test
+ public void testWrongArityAndUnitType() throws Exception {
+ for (String sql : new String[] {
+ "select trunc(cast('2026-01-15' as timestamp))",
+ "select trunc(cast('2026-01-15' as timestamp),1)",
+ "select trunc(cast('2026-01-15' as date),1)",
+ "select truncate(cast('2026-01-15' as timestamp),'DD')",
+ "select dtrunc(cast('2026-01-15' as date),'DD')"}) {
+ for (boolean calcite : new boolean[] {false, true}) {
+ try {
+ metadata(sql, calcite);
+ fail("invalid TRUNC overload must fail analysis: " + sql);
+ } catch (AnalysisException expected) {
+ // Temporal aliases and numeric unit arguments are not native overloads.
+ }
+ }
+ }
+ }
+}
(impala) 01/02: IMPALA-14946: Catch "trunc" error within Calcite planner
Posted by mi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/impala.git
commit 91f13aabdb1e18ab44ac07ef2efc57323afb468f
Author: Steve Carlin <sc...@cloudera.com>
AuthorDate: Thu Apr 9 07:14:55 2026 -0700
IMPALA-14946: Catch "trunc" error within Calcite planner
For the following query:
select dayofyear(trunc('2014-11-11', string_col))
from functional.alltypestiny limit 1;
...the Calcite planner now throws the same exception as the original
planner does. Previous to this commit, a java exception was being
thrown. This allows the test framework to match error messages when
there is no fallback planner.
Change-Id: I766c138fae027ba0d919daa5cce38dc93d7144d1
Reviewed-on: http://gerrit.cloudera.org:8080/24260
Reviewed-by: Impala Public Jenkins <im...@cloudera.com>
Tested-by: Impala Public Jenkins <im...@cloudera.com>
---
.../calcite/operators/ImpalaAdjustScaleFunction.java | 15 ++++++++++++---
.../functional-query/queries/QueryTest/exprs.test | 10 ++++++++++
2 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaAdjustScaleFunction.java b/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaAdjustScaleFunction.java
index b9ae48e60..9a11fcb56 100644
--- a/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaAdjustScaleFunction.java
+++ b/java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaAdjustScaleFunction.java
@@ -56,9 +56,18 @@ public class ImpalaAdjustScaleFunction extends SqlFunction {
ScalarType.MAX_PRECISION)
: decimalType.decimalPrecision();
- Integer scale = opBinding.getOperandCount() > 1
- ? opBinding.getOperandLiteralValue(1, Integer.class)
- : 0;
+ int scale = 0;
+ if (opBinding.getOperandCount() > 1) {
+ if (!opBinding.isOperandLiteral(1, true)) {
+ throw new IllegalArgumentException(getName().toLowerCase() +
+ "() must be called with a constant second argument.");
+ }
+ if (opBinding.isOperandNull(1, true)) {
+ throw new IllegalArgumentException(getName().toLowerCase() +
+ "() cannot be called with a NULL second argument.");
+ }
+ scale = opBinding.getOperandLiteralValue(1, Integer.class);
+ }
Type newDecimalType = ScalarType.createDecimalType(precision, scale);
diff --git a/testdata/workloads/functional-query/queries/QueryTest/exprs.test b/testdata/workloads/functional-query/queries/QueryTest/exprs.test
index eb3620ad5..bc92fcacc 100644
--- a/testdata/workloads/functional-query/queries/QueryTest/exprs.test
+++ b/testdata/workloads/functional-query/queries/QueryTest/exprs.test
@@ -2426,6 +2426,16 @@ select dayofyear(trunc('2014-11-11', string_col)) from functional.alltypestiny l
Invalid Truncate Unit: 0
====
---- QUERY
+select trunc(2.1, int_col) from functional.alltypestiny limit 1;
+---- CATCH
+trunc() must be called with a constant second argument.
+====
+---- QUERY
+select trunc(2.1, null) from functional.alltypestiny limit 1;
+---- CATCH
+trunc() cannot be called with a NULL second argument.
+====
+---- QUERY
select regexp_match_count(tmp.str, tmp.`pattern`) from (values
('aaa' as str, 'a' as `pattern`),
('aaa', 'aa'),