[
https://issues.apache.org/jira/browse/CALCITE-7239?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18031982#comment-18031982
]
weihua zhang edited comment on CALCITE-7239 at 10/28/25 11:15 AM:
------------------------------------------------------------------
[~scarlin] FYI
[https://github.com/apache/impala/blob/master/java/calcite-planner/src/main/java/org/apache/impala/calcite/service/CalciteRelNodeConverter.java#L236]
false should change to true
was (Author: JIRAUSER307426):
[~scarlin] FYI
> If no trim() after SqlToRelConverter.convertQuery(), maybe throw Exception in
> some cases
> ----------------------------------------------------------------------------------------
>
> Key: CALCITE-7239
> URL: https://issues.apache.org/jira/browse/CALCITE-7239
> Project: Calcite
> Issue Type: Bug
> Components: core
> Reporter: weihua zhang
> Priority: Major
> Labels: pull-request-available
>
> {code:sql}
> SELECT e1.*, d.deptno
> FROM emp e1
> LEFT JOIN dept d
> ON e1.deptno = d.deptno
> AND d.deptno IN (
> SELECT e3.deptno
> FROM emp e3
> WHERE d.deptno > e3.comm
> )
> ORDER BY e1.empno;
> {code}
> when https://issues.apache.org/jira/browse/CALCITE-7034 fixed, I want to
> check this fix in impala (I use calcite 1.41.0-snapshot locally), but throw a
> RuntimeException:
> {code:java}
> Could not find a matching signature for call =($7, $9) {code}
> https://github.com/apache/impala/blob/1913ab46ed7e4eba47a9932dc6d825661e4245f7/java/calcite-planner/src/main/java/org/apache/impala/calcite/service/CalciteRelNodeConverter.java#L136
>
> Impala's convert() do not have trimmer.trim()
> if I change Calcite
> [prepareSql()|https://github.com/apache/calcite/blob/b6b3a068f5a524a18db7523a9a6db0a325fb390d/core/src/main/java/org/apache/calcite/prepare/Prepare.java#L229]
> as following, we can recurrence the bug as in impala:
> {code:java}
> public PreparedResult prepareSql(
> SqlNode sqlQuery,
> SqlNode sqlNodeOriginal,
> Class<?> runtimeContextClass,
> SqlValidator validator,
> boolean needsValidation) {
> init(runtimeContextClass);
> final SqlToRelConverter.Config config =
> SqlToRelConverter.config()
> .withTrimUnusedFields(false)
> .withExpand(false);
> final Holder<SqlToRelConverter.Config> configHolder = Holder.of(config);
> Hook.SQL2REL_CONVERTER_CONFIG_BUILDER.run(configHolder);
> final SqlToRelConverter sqlToRelConverter =
> getSqlToRelConverter(validator, catalogReader, configHolder.get());
> // Convert the valid AST into a logical plan
> RelRoot root = sqlToRelConverter.convertQuery(sqlQuery, needsValidation,
> true);
> RelNode relNode = root.project();
> RelNode subQueryRemovedPlan =
> runProgram(
> ImmutableList.of(
> CoreRules.JOIN_SUB_QUERY_TO_CORRELATE,
> CoreRules.PROJECT_SUB_QUERY_TO_CORRELATE,
> CoreRules.FILTER_SUB_QUERY_TO_CORRELATE
> ),
> relNode);
> RelBuilder relBuilder =
> RelFactories.LOGICAL_BUILDER.create(root.rel.getCluster(), catalogReader);
> RelNode decorrelatedPlan =
> RelDecorrelator.decorrelateQuery(subQueryRemovedPlan, relBuilder);
> root = root.withRel(decorrelatedPlan);
> return implement(root);
> }
> private RelNode runProgram(List<RelOptRule> rules, RelNode currentNode) {
> HepProgramBuilder builder = new HepProgramBuilder();
> builder.addRuleCollection(rules);
> builder.addMatchOrder(HepMatchOrder.BOTTOM_UP);
> HepPlanner planner = new HepPlanner(builder.build(),
> currentNode.getCluster().getPlanner().getContext(),
> false, null, RelOptCostImpl.FACTORY);
> planner.setRoot(currentNode);
> return planner.findBestExp();
> }
> {code}
> subQueryRemovedPlan is following:
> {code:java}
> LogicalSort(sort0=[$0], dir0=[ASC])
> LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4],
> SAL=[$5], COMM=[$6], DEPTNO=[$7], DEPTNO0=[$8])
> LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4],
> SAL=[$5], COMM=[$6], DEPTNO=[$7], DEPTNO0=[$8], DNAME=[$9], LOC=[$10])
> LogicalJoin(condition=[=($7, $8)], joinType=[left])
> LogicalTableScan(table=[[scott, EMP]])
> LogicalFilter(condition=[=($0, $3)])
> LogicalCorrelate(correlation=[$cor0], joinType=[inner],
> requiredColumns=[{0}])
> LogicalTableScan(table=[[scott, DEPT]])
> LogicalAggregate(group=[{0}])
> LogicalProject(DEPTNO=[$7])
> LogicalFilter(condition=[>(CAST($cor0.DEPTNO):DECIMAL(7, 2),
> $6)])
> LogicalTableScan(table=[[scott, EMP]])
> {code}
> decorrelatedPlan is following:
> {code:java}
> LogicalSort(sort0=[$0], dir0=[ASC])
> LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4],
> SAL=[$5], COMM=[$6], DEPTNO=[$7], DEPTNO0=[$8])
> LogicalJoin(condition=[=($7, $9)], joinType=[left]) ///
> <------------here------------
> LogicalTableScan(table=[[scott, EMP]])
> LogicalFilter(condition=[=($0, $3)])
> LogicalProject(DEPTNO=[$0], DNAME=[$1], LOC=[$2], DEPTNO0=[$4]) ///
> <------------here------------
> LogicalJoin(condition=[=($3, $5)], joinType=[inner])
> LogicalProject(DEPTNO=[$0], DNAME=[$1], LOC=[$2],
> DEPTNO0=[CAST($0):DECIMAL(7, 2)])
> LogicalTableScan(table=[[scott, DEPT]])
> LogicalAggregate(group=[{0, 1}])
> LogicalProject(DEPTNO=[$7], DEPTNO0=[$8])
> LogicalJoin(condition=[>($8, $6)], joinType=[inner])
> LogicalTableScan(table=[[scott, EMP]])
> LogicalAggregate(group=[{0}])
> LogicalProject(DEPTNO0=[CAST($0):DECIMAL(7, 2)])
> LogicalTableScan(table=[[scott, DEPT]])
> {code}
--
This message was sent by Atlassian Jira
(v8.20.10#820010)