Skip to content

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

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

[WIP] HIVE-29641: Upgrade Calcite to 1.42.0#6523
rubenada wants to merge 50 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 30 commits June 12, 2026 14:20
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)
…alues in RexLiteral:

[CALCITE-2067] RexLiteral cannot represent accurately floating point values, including NaN, Infinity (1.38)
[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)
… 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)
…alues in RexLiteral:

[CALCITE-2067] RexLiteral cannot represent accurately floating point values, including NaN, Infinity (1.38)
[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)
… due to

[CALCITE-4590] Incorrect query result with fixed-length string (1.39)
[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)
[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-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)
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