github-advanced-security[bot] commented on code in PR #19370: URL: https://github.com/apache/druid/pull/19370#discussion_r3134522563
########## 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())); Review Comment: ## CodeQL / Deprecated method or constructor invocation Invoking [RelOptRule.operand](1) should be avoided because it has been deprecated. [Show more details](https://github.com/apache/druid/security/code-scanning/11128) ########## sql/src/main/java/org/apache/druid/sql/calcite/table/RowSignatures.java: ########## @@ -75,6 +82,15 @@ return rowSignatureBuilder.build(); } + /** + * Creates a {@link RowSignature} from a Calcite row type, adjusting field names if needed + * such that they are unique. + */ + public static RowSignature fromRelDataTypeWithUniqueFields(final RelDataType rowType) + { + return fromRelDataType(SqlValidatorUtil.uniquify(rowType.getFieldNames()), rowType); Review Comment: ## CodeQL / Deprecated method or constructor invocation Invoking [SqlValidatorUtil.uniquify](1) should be avoided because it has been deprecated. [Show more details](https://github.com/apache/druid/security/code-scanning/11130) ########## 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())); Review Comment: ## CodeQL / Deprecated method or constructor invocation Invoking [RelOptRule.any](1) should be avoided because it has been deprecated. [Show more details](https://github.com/apache/druid/security/code-scanning/11129) -- 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]
