Skip to content

[WIP] HIVE-29641: Upgrade Calcite to 1.42.0#6523

Draft
rubenada wants to merge 36 commits into
apache:masterfrom
rubenada:HIVE-29641
Draft

[WIP] HIVE-29641: Upgrade Calcite to 1.42.0#6523
rubenada wants to merge 36 commits into
apache:masterfrom
rubenada:HIVE-29641

Conversation

@rubenada

@rubenada rubenada commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Upgrade Calcite to 1.42.0.

Why are the changes needed?

Upgrade to latest Calcite version.

Does this PR introduce any user-facing change?

tbd

How was this patch tested?

tbd

Details

pom changes

  • Change calcite version in parent pom (from 1.33.0 to 1.42.0)

Code adjustments due to Calcite breaking changes

  • [CALCITE-5557] Add SAFE_CAST function (enabled in BigQuery library) (1.35)
    Use non-deprecated version of RexBuilder#makeAbstractCast in HiveSqlSumAggFunction, RexNodeConverter, ASTConverter
  • ddb4200 Refactor: Add fields AggregateCall.rexList and RelBuilder.AggCall.preOperands (1.35)
    AggCall construction adjustments in HiveAggregate and HiveSqlSumAggFunction
  • [CALCITE-3679] Allow lambda expressions in SQL queries (1.37)
    Add visitLambda and visitLambdaRef in RexVisitor implementation (HiveCalciteUtil#ConstantFinder)
  • [CALCITE-6961] Support LogicalRepeatUnion in RelShuttle + [CALCITE-6959] Support LogicalAsofJoin in RelShuttle (1.40)
    Add visit(LogicalAsofJoin) and visit(LogicalRepeatUnion) to HiveRelShuttleImpl
  • [CALCITE-5387] Type-mismatch on nullability in JoinPushTransitivePredicatesRule RelRule (1.40)
    Adjust RexPermuteInputsShuttle constructor in HiveJoinConstraintsRule
  • [CALCITE-7029] Support DPhyp to handle various join types (1.41)
    Add visitNodeAndFieldIndex in RexVisitor implementation (HiveCalciteUtil#ConstantFinder)
  • [CALCITE-7351] Make getMaxNumericScale() and getMaxNumericPrecision() final (1.42)
    Adjust HiveTypeSystemImpl

Other code changes

  • Hive.java: update Update Calcite default charset system property (use UTF16 instead of the default ISO-8859-1), otherwise we get "charset pollution" on char literals in explained plans (e.g. _UTF-16LE'1' instead of simply '1') due to the modification in SqlImplemento#toSql(RexLiteral) via "[CALCITE-6006] RelToSqlConverter loses charset information" (introduced in 1.36)
    As a consequence of defining Calcite default charset system property, now RexLiteral#appendAsJava (which already contained the literal charset vs default charset verification, added to SqlImplementor in CALCITE-6006 1.36), literals printed via this method (i.e. used to print the charset) don't do it any more, e.g. _UTF-16LE'ten' => 'ten', which aligns them with the ones printed via SqlImplemento#toSql(RexLiteral)
  • Due to "7fc3e1b Refactor: Add RelNode.stripped" (included in Calcite 1.35), which added a new check in HepRelVertex constructor (inner rel cannot be another HepRelVertex), we basically cannot apply a HepProgram on a tree containing already HepVertex. This was violated by this is violated by HiveHepExtractRelNodeRule, so all its caller now use an existing auxiliary method (based on a shuttle) for this unwrapping (HiveCalciteUtil.stripHepVertices).
    This was also violated by materialization rules in HiveMaterializedViewRule, which used its own private static class HiveHepExtractRelNodeRule in a HepProgram as unionRewritingPullProgram. As a workaround, it is proposed to extend the MaterializedView rules (as HiveMaterializedViewRule), override the rewriteQuery
    (where the UnionRewritingPullProgram is called), and unwrap the HepRelVertex tree ourselves (using (HiveCalciteUtil.stripHepVertices) before calling the super.rewriteQuery.
  • The upgrade unveiled a bug in HiveRelFieldTrimmer#rewriteGBConstantKeys: the condition on its javadoc "none of the relnode above aggregate refers to these group keys" should not be checked via fieldsUsed.contains(aggregate.getGroupSet()) but rather fieldsUsed.intersects(aggregate.getGroupSet()).
    Also took the opportunity here to get rid of the unnecessary check aggregate.getIndicatorCount() > 0 which is always false (this method is deprecated and always returns zero)
  • Disable simplifyValues in HiveRelBuilder config. Due to "[CALCITE-5717] RelBuilder.project of literals on a single-row Aggregate should create a Values" (introduced 1.35), cases like TestMiniLlapLocalCliDriver with file explainuser_1.q led to: java.lang.UnsupportedOperationException: Values with non-empty tuples are not supported. at org.apache.hadoop.hive.ql.optimizer.calcite.translator.ASTConverter.convert(ASTConverter.java:264). The reason is that RelBuilder#project_ contains the simplification (only applicable if config.simplifyValues is true): "If the expressions are all literals, and the input is a Values with N rows [...], replace with a Values with same tuple N times"; and with CALCITE-5717 that simplification was extended to not only "Values with N rows" but also "Aggregates with 1 row", and this case would lead to creating a non-empty HiveValues, which is not supported in our case. The easiest solution to prevent that from happening is disabling simplifyValues in HiveRelBuilder config.
  • Due to a new assertion introduced in 1.41 via "[CALCITE-7070] FILTER_REDUCE_EXPRESSIONS crashes on expression BETWEEN ( NULL) AND X", now Calcite can throw an AssertionError in case of a wrong binary comparison predicate with null value java.lang.AssertionError: Comparison with NULL in pulledUpPredicates, so in order to avoid that, a few classes needed to be adjusted. Firstly HiveFunctionHelper#getExpression must not create such expressions (seen NULL = NULL in TestMiniLlapLocalCliDriver with subquery_null_agg.q); secondly avoid incorrect comparison ($snapshotIdInputRef <= NULL seen in TestIcebergCliDriver with mv_iceberg_orc.q) in HiveAugmentSnapshotMaterializationRule, use snapshotId -1 instead, and then switch it back in HivePushdownSnapshotFilterRule.

rubenada added 28 commits June 14, 2026 18:46
…n on Jenkins:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:3.6.0:shade (default) on project hive-druid-handler:
Error creating shaded jar: Problem shading JAR /home/jenkins/agent/workspace/hive-precommit_PR-6523/druid-handler/target/hive-druid-handler-4.3.0-SNAPSHOT.jar
entry org/apache/hive/druid/org/apache/calcite/runtime/SqlFunctions.class: org.apache.maven.plugin.MojoExecutionException:
Error in ASM processing class org/apache/hive/druid/org/apache/calcite/runtime/SqlFunctions.class: Index 65536 out of bounds for length 334
As a consequence of defining Calcite default charset system property in a previous commit, now in RexLiteral#appendAsJava
(which already contained the literal charset vs default charset verification, added to SqlImplementor in CALCITE-6006 1.36)
literals that used to print the charset don't do it anymore, e.g. _UTF-16LE'ten' => 'ten'
Due to "[CALCITE-5717] RelBuilder.project of literals on a single-row Aggregate should create a Values" (introduced 1.35), cases like
TestMiniLlapLocalCliDriver with file explainuser_1.q led to: "java.lang.UnsupportedOperationException: Values with non-empty tuples are not supported.
at org.apache.hadoop.hive.ql.optimizer.calcite.translator.ASTConverter.convert(ASTConverter.java:264)".
The reason is that RelBuilder#project_ contains the simplification (only applicable if config.simplifyValues is true):
 "If the expressions are all literals, and the input is a Values with N rows [...], replace with a Values with same tuple N times";
 and with CALCITE-5717 that simplification was extended to not only "Values with N rows" but also "Aggregates with 1 row", and this case
 would lead to creating a non-empty HiveValues, which is not supported in our case.
 The easiest solution to prevent that from happening is disabling simplifyValues in HiveRelBuilder config.
Another attempt to fix druid-handler asm javadoc error seen on Jenkins:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:3.6.0:shade (default) on project hive-druid-handler:
Error creating shaded jar: Problem shading JAR /home/jenkins/agent/workspace/hive-precommit_PR-6523/druid-handler/target/hive-druid-handler-4.3.0-SNAPSHOT.jar
entry org/apache/hive/druid/org/apache/calcite/runtime/SqlFunctions.class: org.apache.maven.plugin.MojoExecutionException:
Error in ASM processing class org/apache/hive/druid/org/apache/calcite/runtime/SqlFunctions.class: Index 65536 out of bounds for length 334
[CALCITE-5798] Improve simplification of '(x < y) IS NOT TRUE' when x and y are not nullable (1.35)
[CALCITE-6317] Incorrect constant replacement when group keys are NULL (1.37)

Adjust test plans due to change in representation of floating point values in RexLiteral:
[CALCITE-2067] RexLiteral cannot represent accurately floating point values, including NaN, Infinity (1.38)

Adjust test plans simplifications around IS_NOT_NULL:
[CALCITE-5639] RexSimplify should remove IS NOT NULL check when LIKE comparison is present (1.35) (actually more generic than just LIKE)
[CALCITE-5769] Optimizing 'CAST(e AS t) IS NOT NULL' to 'e IS NOT NULL' (1.35) (actually more generic than just CAST)
1.40
[CALCITE-6961] Support LogicalRepeatUnion in RelShuttle + [CALCITE-6959] Support LogicalAsofJoin in RelShuttle
Add visit(LogicalAsofJoin) and visit(LogicalRepeatUnion) to HiveRelShuttleImpl

[CALCITE-5387] Type-mismatch on nullability in JoinPushTransitivePredicatesRule RelRule
Adjust RexPermuteInputsShuttle constructor in HiveJoinConstraintsRule

1.41
[CALCITE-7029] Support DPhyp to handle various join types
Add visitNodeAndFieldIndex in RexVisitor implementation (HiveCalciteUtil#ConstantFinder)

1.42
[CALCITE-7351] Make getMaxNumericScale() and getMaxNumericPrecision() final
Adjust HiveTypeSystemImpl
…alues in RexLiteral:

[CALCITE-2067] RexLiteral cannot represent accurately floating point values, including NaN, Infinity (1.38)
Adjust test plans due to change in representation of floating point values in RexLiteral:
[CALCITE-2067] RexLiteral cannot represent accurately floating point values, including NaN, Infinity (1.38)

Adjust test plans simplifications around IS_NOT_NULL:
[CALCITE-5639] RexSimplify should remove IS NOT NULL check when LIKE comparison is present (1.35) (actually more generic than just LIKE)
[CALCITE-5769] Optimizing 'CAST(e AS t) IS NOT NULL' to 'e IS NOT NULL' (1.35) (actually more generic than just CAST)

Adjust test plans: add missing padding on fixed-length char literals, due to
[CALCITE-4590] Incorrect query result with fixed-length string (1.39)
Note: this Calcite fix only impacts predicates with OR/IN/SEARCH, and aligns these plans with single value condition ones
(which already contained the padding, e.g. ql/src/test/results/clientpositive/perf/tpcds30tb/tez/cbo_query33.q.out)
[CALCITE-6417] Map value constructor and Array value constructor unparsed incorrectly for HiveSqlDialect (1.38)
… due to

[CALCITE-4590] Incorrect query result with fixed-length string (1.39)

Adjust tests (condition predicates in different order)

Adjust test plans due to change in representation of floating point values in RexLiteral:
[CALCITE-2067] RexLiteral cannot represent accurately floating point values, including NaN, Infinity (1.38)

Adjust test plans simplifications around IS_NOT_NULL:
[CALCITE-5639] RexSimplify should remove IS NOT NULL check when LIKE comparison is present (1.35) (actually more generic than just LIKE)
[CALCITE-5769] Optimizing 'CAST(e AS t) IS NOT NULL' to 'e IS NOT NULL' (1.35) (actually more generic than just CAST)

Adjust test plans: add missing padding on fixed-length char literals, due to
[CALCITE-4590] Incorrect query result with fixed-length string (1.39)

Adjust test files: MINUS type added to Json due to
[CALCITE-5607] Serialize return type during RelJson.toJson(RexNode node) for SqlKind.MINUS (1.37)
[CALCITE-7160] Simplify AND/OR with DISTINCT predicates to SEARCH (1.41)

Adjust test plans simplifications around IS_NOT_NULL:
[CALCITE-5639] RexSimplify should remove IS NOT NULL check when LIKE comparison is present (1.35) (actually more generic than just LIKE)
[CALCITE-5769] Optimizing 'CAST(e AS t) IS NOT NULL' to 'e IS NOT NULL' (1.35)

Adjust test plans on array construction due to:
[CALCITE-6417] Map value constructor and Array value constructor unparsed incorrectly for HiveSqlDialect (1.38)
…creating the filter RelNode.

This is required to avoid this exception (new assertion introduced in 1.41 via
[CALCITE-7070] FILTER_REDUCE_EXPRESSIONS crashes on expression BETWEEN ( NULL) AND X )
seen in TestMiniLlapLocalCliDriver subquery_null_agg when trying to create a Filter
with NULL = NULL condition:
java.lang.AssertionError: Comparison with NULL in pulledUpPredicates
	at org.apache.calcite.util.Litmus.lambda$static$0(Litmus.java:31)
	at org.apache.calcite.util.Litmus.check(Litmus.java:76)
	at org.apache.calcite.plan.RelOptPredicateList.<init>(RelOptPredicateList.java:128)
	...
…y comparison RexCall create in HiveFunctionHelper),

and avoid RexSimplify solution on CalcitePlanner (too many side effects)
…ntSnapshotMaterializationRule, use snapshotId -1 instead,

and then switch it back in HivePushdownSnapshotFilterRule; this is required because an incorrect NULL comparison predicate would
trigger a new assertion introduced in 1.41 (via [CALCITE-7070] FILTER_REDUCE_EXPRESSIONS crashes on expression BETWEEN ( NULL) AND X ):
java.lang.AssertionError: Comparison with NULL in pulledUpPredicates
	at org.apache.calcite.util.Litmus.lambda$static$0(Litmus.java:31)
	at org.apache.calcite.util.Litmus.check(Litmus.java:76)
	at org.apache.calcite.plan.RelOptPredicateList.<init>(RelOptPredicateList.java:128)
…ge on HiveAugmentSnapshotMaterializationRule:

generate $snapshotId <= -1 instead of $snapshotId <= null (which can lead to a Calcite error)
…doc generation:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:3.6.0:shade (default) on project hive-druid-handler:
Error creating shaded jar: Problem shading JAR /../hive/druid-handler/target/hive-druid-handler-4.3.0-SNAPSHOT.jar entry
org/apache/hive/druid/org/apache/calcite/runtime/SqlFunctions.class: org.apache.maven.plugin.MojoExecutionException:
Error in ASM processing class org/apache/hive/druid/org/apache/calcite/runtime/SqlFunctions.class: Index 65536 out of bounds for length 334
…d from RexSimplify for "non-safe" operations,

e.g. division (if it could "hide" a runtime division by zero) or operations involving cast (only "lossless" casts can be simplified), via:
[CALCITE-7145] RexSimplify should not simplify IS NULL(10/0) (1.42)
[CALCITE-7296] RexSimplify should not simplify IS (NOT) NULL(CAST(10/0 AS BIGINT)) (1.42)
[CALCITE-7032] Simplify 'NULL>ALL (ARRAY[1,2,NULL])' to 'NULL' (1.41)
[CALCITE-5639] RexSimplify should remove IS NOT NULL check when LIKE comparison is present (1.35) (actually more generic than just LIKE)
[CALCITE-5769] Optimizing 'CAST(e AS t) IS NOT NULL' to 'e IS NOT NULL' (1.35)
[CALCITE-7005] Invalid unparse for IS TRUE,IS FALSE,IS NOT TRUE and IS NOT FALSE in Hive/Presto Dialect (1.40)
…ange in udf_between.q

The plan difference comes from:
1.33
BETWEEN -> RexSimplify: 100 <= x AND x <= 200 -> HivePointLookupOptimizerRule: x BETWEEN 100, 200 -> SearchTransformer: no-op
NOT BETWEEN -> RexSimplify: x < 100 OR x > 200 -> HivePointLookupOptimizerRule: x NOT BETWEEN 100, 200 -> SearchTransformer: no-op
vs
1.42
BETWEEN -> RexSimpliy: Sarg x [100..200] -> HivePointLookupOptimizerRule: no-op -> SearchTransformer: x BETWEEN 100, 200
NOT BETWEEN -> RexSimpliy: Sarg x (-inf..100, 200..+inf) -> HivePointLookupOptimizerRule: no-op -> SearchTransformer: x < 100 OR x > 200

Thus, we need to adjust SearchTransformer to detect the last case and generate: x NOT BETWEEN 100, 200
@sonarqubecloud

Copy link
Copy Markdown

… to avoid plan change in udf_between.q) solved 1 plan change

but "broke" 8 other tests (see below *); so instead, simply revert the change in SearchTransformer and adjust udf_between.q.out.
The "identify NOT BETWEEN expression in SearchTransformer" (to deal with "regression" caused by ""[CALCITE-7194] Simplify comparisons
between function calls and literals to SEARCH (1.41)") can be handled in a separate, dedicated PR.

*
testCliDriver[vector_between_columns] – org.apache.hadoop.hive.cli.split28.TestMiniLlapLocalCliDriver4s
testCliDriver[external_jdbc_table_perf] – org.apache.hadoop.hive.cli.split8.TestMiniLlapLocalCliDriver48s
testCliDriver[filter_numeric] – org.apache.hadoop.hive.cli.split8.TestMiniLlapLocalCliDriver3s
testCliDriver[correlationoptimizer8] – org.apache.hadoop.hive.cli.split5.TestMiniLlapLocalCliDriver5s
testCliDriver[vector_between_in] – org.apache.hadoop.hive.cli.split29.TestMiniLlapLocalCliDriver36s
testCliDriver[stats_histogram_null] – org.apache.hadoop.hive.cli.split22.TestMiniLlapLocalCliDriver28s
testCliDriver[join34] – org.apache.hadoop.hive.cli.split11.TestMiniLlapLocalCliDriver2s
testCliDriver[join35] – org.apache.hadoop.hive.cli.split11.TestMiniLlapLocalCliDriver
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants