FrankChen021 commented on code in PR #19370: URL: https://github.com/apache/druid/pull/19370#discussion_r3141594313
########## sql/src/main/java/org/apache/druid/sql/calcite/rule/DruidStripUnionArmCastRule.java: ########## @@ -0,0 +1,161 @@ +/* + * 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.druid.sql.calcite.rule; + +import org.apache.calcite.plan.RelOptRule; +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.Project; +import org.apache.calcite.rel.core.TableScan; +import org.apache.calcite.rel.core.Union; +import org.apache.calcite.rel.logical.LogicalProject; +import org.apache.calcite.rel.rules.SubstitutionRule; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rex.RexCall; +import org.apache.calcite.rex.RexInputRef; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.sql.SqlKind; +import org.apache.calcite.sql.type.SqlTypeUtil; + +import java.util.ArrayList; +import java.util.List; + +/** + * Strips numeric-to-numeric {@code CAST} expressions so {@link DruidUnionDataSourceRule} is able + * to generate unions in more plans. + * + * <p>Example: for {@code SELECT dim1, dim2, m1 FROM foo2 UNION ALL SELECT dim1, dim2, m1 FROM foo} + * where {@code foo2.m1} is {@code BIGINT} and {@code foo.m1} is {@code FLOAT}, Calcite produces: + * <pre> + * LogicalUnion(all=[true]) + * LogicalProject(dim1=[$1], dim2=[$2], m1=[CAST($5):FLOAT]) + * LogicalTableScan(table=[[druid, foo2]]) + * LogicalProject(dim1=[$1], dim2=[$2], m1=[$5]) + * LogicalTableScan(table=[[druid, foo]]) + * </pre> + * This rule rewrites it to: + * <pre> + * LogicalUnion(all=[true]) + * LogicalProject(dim1=[$1], dim2=[$2], m1=[$5]) // CAST stripped + * LogicalTableScan(table=[[druid, foo2]]) + * LogicalProject(dim1=[$1], dim2=[$2], m1=[$5]) + * LogicalTableScan(table=[[druid, foo]]) + * </pre> + * + * <p>See {@code union_datasource.iq} for test cases that fail without this rule. + */ +public class DruidStripUnionArmCastRule extends RelOptRule implements SubstitutionRule +{ + private static final DruidStripUnionArmCastRule INSTANCE = new DruidStripUnionArmCastRule(); + + private DruidStripUnionArmCastRule() + { + super(operand(Union.class, any())); + } + + public static DruidStripUnionArmCastRule instance() + { + return INSTANCE; + } + + @Override + public void onMatch(final RelOptRuleCall call) + { + final Union union = call.rel(0); + final List<RelNode> newInputs = new ArrayList<>(union.getInputs().size()); + boolean anyChanged = false; + for (final RelNode input : union.getInputs()) { + final RelNode arm = input.stripped(); Review Comment: P1 Stripping union-arm casts can change native output types stripArmCasts removes numeric casts from UNION ALL arms whenever the resulting union row type still equals the original row type. That planner-level equality does not guarantee the native Druid query still emits the same column type: removing a BIGINT-to-FLOAT or DECIMAL-to-DOUBLE cast lets the scan arm expose its underlying native type, and the union datasource can then mix arm output types or choose a different runtime type than the SQL row type promised. This can change query results or fail during native union execution. Keep the casts in a project above each arm, or prove the native union path preserves the SQL cast semantics before stripping them. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
