FrankChen021 commented on code in PR #19370: URL: https://github.com/apache/druid/pull/19370#discussion_r3173373899
########## 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: Thanks, that makes sense. Given this is restoring the prior table-level UNION ALL behavior and the native union path already only checks column-name compatibility, I agree this should not block the PR. The original type-coercion risk is still worth keeping in mind for direct UNION ALL results without a downstream expression, aggregation, or result coercion, but I’m okay treating it as existing behavior rather than a new regression here. -- 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]
