abhishekagarwal87 commented on code in PR #14510: URL: https://github.com/apache/druid/pull/14510#discussion_r1267819567
########## sql/src/main/java/org/apache/druid/sql/calcite/aggregation/builtin/LiteralSqlAggregator.java: ########## @@ -0,0 +1,78 @@ +/* + * 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.aggregation.builtin; + +import com.google.common.collect.ImmutableList; +import org.apache.calcite.rel.core.AggregateCall; +import org.apache.calcite.rel.core.Project; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.sql.SqlAggFunction; +import org.apache.calcite.sql.fun.SqlInternalOperators; +import org.apache.druid.query.aggregation.post.ExpressionPostAggregator; +import org.apache.druid.segment.column.RowSignature; +import org.apache.druid.sql.calcite.aggregation.Aggregation; +import org.apache.druid.sql.calcite.aggregation.SqlAggregator; +import org.apache.druid.sql.calcite.expression.DruidExpression; +import org.apache.druid.sql.calcite.expression.Expressions; +import org.apache.druid.sql.calcite.planner.PlannerContext; +import org.apache.druid.sql.calcite.rel.VirtualColumnRegistry; + +import javax.annotation.Nullable; +import java.util.List; + +public class LiteralSqlAggregator implements SqlAggregator Review Comment: javadocs please ########## sql/src/main/java/org/apache/druid/sql/calcite/expression/BasicOperandTypeChecker.java: ########## @@ -0,0 +1,229 @@ +/* + * 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.expression; + +import com.google.common.base.Preconditions; +import it.unimi.dsi.fastutil.ints.IntArraySet; +import it.unimi.dsi.fastutil.ints.IntSet; +import it.unimi.dsi.fastutil.ints.IntSets; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.runtime.CalciteException; +import org.apache.calcite.sql.SqlCallBinding; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.SqlOperandCountRange; +import org.apache.calcite.sql.SqlOperator; +import org.apache.calcite.sql.SqlUtil; +import org.apache.calcite.sql.type.SqlOperandCountRanges; +import org.apache.calcite.sql.type.SqlOperandTypeChecker; +import org.apache.calcite.sql.type.SqlTypeFamily; +import org.apache.calcite.sql.type.SqlTypeName; +import org.apache.calcite.util.Static; + +import javax.annotation.Nullable; +import java.util.Arrays; +import java.util.List; +import java.util.function.Function; +import java.util.stream.IntStream; + +/** + * Operand type checker that is used in simple situations: there are a particular number of operands, with + * particular types, some of which may be optional or nullable, and some of which may be required to be literals. + */ +public class BasicOperandTypeChecker implements SqlOperandTypeChecker +{ + private final List<SqlTypeFamily> operandTypes; + private final int requiredOperands; + private final IntSet nullOperands; + private final IntSet literalOperands; + + BasicOperandTypeChecker( + final List<SqlTypeFamily> operandTypes, + final int requiredOperands, + final IntSet nullOperands, Review Comment: ```suggestion final IntSet nullableOperands, ``` ########## sql/src/main/java/org/apache/druid/sql/calcite/expression/BasicOperandTypeChecker.java: ########## @@ -0,0 +1,229 @@ +/* + * 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.expression; + +import com.google.common.base.Preconditions; +import it.unimi.dsi.fastutil.ints.IntArraySet; +import it.unimi.dsi.fastutil.ints.IntSet; +import it.unimi.dsi.fastutil.ints.IntSets; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.runtime.CalciteException; +import org.apache.calcite.sql.SqlCallBinding; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.SqlOperandCountRange; +import org.apache.calcite.sql.SqlOperator; +import org.apache.calcite.sql.SqlUtil; +import org.apache.calcite.sql.type.SqlOperandCountRanges; +import org.apache.calcite.sql.type.SqlOperandTypeChecker; +import org.apache.calcite.sql.type.SqlTypeFamily; +import org.apache.calcite.sql.type.SqlTypeName; +import org.apache.calcite.util.Static; + +import javax.annotation.Nullable; +import java.util.Arrays; +import java.util.List; +import java.util.function.Function; +import java.util.stream.IntStream; + +/** + * Operand type checker that is used in simple situations: there are a particular number of operands, with + * particular types, some of which may be optional or nullable, and some of which may be required to be literals. + */ +public class BasicOperandTypeChecker implements SqlOperandTypeChecker +{ + private final List<SqlTypeFamily> operandTypes; + private final int requiredOperands; + private final IntSet nullOperands; + private final IntSet literalOperands; + + BasicOperandTypeChecker( + final List<SqlTypeFamily> operandTypes, + final int requiredOperands, + final IntSet nullOperands, + @Nullable final int[] literalOperands + ) + { + Preconditions.checkArgument(requiredOperands <= operandTypes.size() && requiredOperands >= 0); + this.operandTypes = Preconditions.checkNotNull(operandTypes, "operandTypes"); + this.requiredOperands = requiredOperands; + this.nullOperands = Preconditions.checkNotNull(nullOperands, "nullOperands"); + + if (literalOperands == null) { + this.literalOperands = IntSets.EMPTY_SET; + } else { + this.literalOperands = new IntArraySet(); + Arrays.stream(literalOperands).forEach(this.literalOperands::add); + } + } + + public static Builder builder() + { + return new Builder(); + } + + public static boolean throwOrReturn( + final boolean throwOnFailure, + final SqlCallBinding callBinding, + final Function<SqlCallBinding, CalciteException> exceptionMapper + ) + { + if (throwOnFailure) { + throw exceptionMapper.apply(callBinding); + } else { + return false; + } + } + + @Override + public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) + { + for (int i = 0; i < callBinding.operands().size(); i++) { + final SqlNode operand = callBinding.operands().get(i); + + if (literalOperands.contains(i)) { + // Verify that 'operand' is a literal. + if (!SqlUtil.isLiteral(operand)) { + return throwOrReturn( + throwOnFailure, + callBinding, + cb -> cb.getValidator() + .newValidationError( + operand, + Static.RESOURCE.argumentMustBeLiteral(callBinding.getOperator().getName()) Review Comment: how does this error appear to an end-user? ########## sql/src/main/java/org/apache/druid/sql/calcite/expression/BasicOperandTypeChecker.java: ########## @@ -0,0 +1,229 @@ +/* + * 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.expression; + +import com.google.common.base.Preconditions; +import it.unimi.dsi.fastutil.ints.IntArraySet; +import it.unimi.dsi.fastutil.ints.IntSet; +import it.unimi.dsi.fastutil.ints.IntSets; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.runtime.CalciteException; +import org.apache.calcite.sql.SqlCallBinding; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.SqlOperandCountRange; +import org.apache.calcite.sql.SqlOperator; +import org.apache.calcite.sql.SqlUtil; +import org.apache.calcite.sql.type.SqlOperandCountRanges; +import org.apache.calcite.sql.type.SqlOperandTypeChecker; +import org.apache.calcite.sql.type.SqlTypeFamily; +import org.apache.calcite.sql.type.SqlTypeName; +import org.apache.calcite.util.Static; + +import javax.annotation.Nullable; +import java.util.Arrays; +import java.util.List; +import java.util.function.Function; +import java.util.stream.IntStream; + +/** + * Operand type checker that is used in simple situations: there are a particular number of operands, with + * particular types, some of which may be optional or nullable, and some of which may be required to be literals. + */ +public class BasicOperandTypeChecker implements SqlOperandTypeChecker +{ + private final List<SqlTypeFamily> operandTypes; + private final int requiredOperands; + private final IntSet nullOperands; + private final IntSet literalOperands; + + BasicOperandTypeChecker( + final List<SqlTypeFamily> operandTypes, + final int requiredOperands, + final IntSet nullOperands, + @Nullable final int[] literalOperands + ) + { + Preconditions.checkArgument(requiredOperands <= operandTypes.size() && requiredOperands >= 0); + this.operandTypes = Preconditions.checkNotNull(operandTypes, "operandTypes"); + this.requiredOperands = requiredOperands; + this.nullOperands = Preconditions.checkNotNull(nullOperands, "nullOperands"); + + if (literalOperands == null) { + this.literalOperands = IntSets.EMPTY_SET; + } else { + this.literalOperands = new IntArraySet(); + Arrays.stream(literalOperands).forEach(this.literalOperands::add); + } + } + + public static Builder builder() + { + return new Builder(); + } + + public static boolean throwOrReturn( + final boolean throwOnFailure, + final SqlCallBinding callBinding, + final Function<SqlCallBinding, CalciteException> exceptionMapper + ) + { + if (throwOnFailure) { + throw exceptionMapper.apply(callBinding); + } else { + return false; + } + } + + @Override + public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) + { + for (int i = 0; i < callBinding.operands().size(); i++) { + final SqlNode operand = callBinding.operands().get(i); + + if (literalOperands.contains(i)) { + // Verify that 'operand' is a literal. + if (!SqlUtil.isLiteral(operand)) { + return throwOrReturn( + throwOnFailure, + callBinding, + cb -> cb.getValidator() + .newValidationError( + operand, + Static.RESOURCE.argumentMustBeLiteral(callBinding.getOperator().getName()) + ) + ); + } + } + + final RelDataType operandType = callBinding.getValidator().deriveType(callBinding.getScope(), operand); + final SqlTypeFamily expectedFamily = operandTypes.get(i); Review Comment: there should be some check that index is bounded correctly. ########## sql/src/main/java/org/apache/druid/sql/calcite/expression/BasicOperandTypeChecker.java: ########## @@ -0,0 +1,229 @@ +/* + * 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.expression; + +import com.google.common.base.Preconditions; +import it.unimi.dsi.fastutil.ints.IntArraySet; +import it.unimi.dsi.fastutil.ints.IntSet; +import it.unimi.dsi.fastutil.ints.IntSets; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.runtime.CalciteException; +import org.apache.calcite.sql.SqlCallBinding; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.SqlOperandCountRange; +import org.apache.calcite.sql.SqlOperator; +import org.apache.calcite.sql.SqlUtil; +import org.apache.calcite.sql.type.SqlOperandCountRanges; +import org.apache.calcite.sql.type.SqlOperandTypeChecker; +import org.apache.calcite.sql.type.SqlTypeFamily; +import org.apache.calcite.sql.type.SqlTypeName; +import org.apache.calcite.util.Static; + +import javax.annotation.Nullable; +import java.util.Arrays; +import java.util.List; +import java.util.function.Function; +import java.util.stream.IntStream; + +/** + * Operand type checker that is used in simple situations: there are a particular number of operands, with + * particular types, some of which may be optional or nullable, and some of which may be required to be literals. + */ +public class BasicOperandTypeChecker implements SqlOperandTypeChecker +{ + private final List<SqlTypeFamily> operandTypes; + private final int requiredOperands; + private final IntSet nullOperands; + private final IntSet literalOperands; + + BasicOperandTypeChecker( + final List<SqlTypeFamily> operandTypes, + final int requiredOperands, + final IntSet nullOperands, + @Nullable final int[] literalOperands + ) + { + Preconditions.checkArgument(requiredOperands <= operandTypes.size() && requiredOperands >= 0); + this.operandTypes = Preconditions.checkNotNull(operandTypes, "operandTypes"); + this.requiredOperands = requiredOperands; + this.nullOperands = Preconditions.checkNotNull(nullOperands, "nullOperands"); + + if (literalOperands == null) { + this.literalOperands = IntSets.EMPTY_SET; + } else { + this.literalOperands = new IntArraySet(); + Arrays.stream(literalOperands).forEach(this.literalOperands::add); + } + } + + public static Builder builder() + { + return new Builder(); + } + + public static boolean throwOrReturn( + final boolean throwOnFailure, + final SqlCallBinding callBinding, + final Function<SqlCallBinding, CalciteException> exceptionMapper + ) + { + if (throwOnFailure) { + throw exceptionMapper.apply(callBinding); + } else { + return false; + } + } + + @Override + public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) + { + for (int i = 0; i < callBinding.operands().size(); i++) { + final SqlNode operand = callBinding.operands().get(i); + + if (literalOperands.contains(i)) { + // Verify that 'operand' is a literal. + if (!SqlUtil.isLiteral(operand)) { + return throwOrReturn( + throwOnFailure, + callBinding, + cb -> cb.getValidator() + .newValidationError( + operand, + Static.RESOURCE.argumentMustBeLiteral(callBinding.getOperator().getName()) + ) + ); + } + } + + final RelDataType operandType = callBinding.getValidator().deriveType(callBinding.getScope(), operand); + final SqlTypeFamily expectedFamily = operandTypes.get(i); + + if (expectedFamily == SqlTypeFamily.ANY) { + // ANY matches anything. This operand is all good; do nothing. + } else if (expectedFamily.getTypeNames().contains(operandType.getSqlTypeName())) { + // Operand came in with one of the expected types. + } else if (operandType.getSqlTypeName() == SqlTypeName.NULL || SqlUtil.isNullLiteral(operand, true)) { + // Null came in, check if operand is a nullable type. + if (!nullOperands.contains(i)) { + return throwOrReturn( + throwOnFailure, + callBinding, + cb -> cb.getValidator().newValidationError(operand, Static.RESOURCE.nullIllegal()) Review Comment: same question here about the error message ########## sql/src/test/java/org/apache/druid/sql/calcite/CalciteReplaceDmlTest.java: ########## @@ -440,9 +440,8 @@ public void testReplaceWithoutCompleteOverwriteClause() { testIngestionQuery() .sql("REPLACE INTO dst OVERWRITE SELECT * FROM foo PARTITIONED BY ALL TIME") - .expectValidationError(invalidSqlIs( - "Missing time chunk information in OVERWRITE clause for REPLACE. " - + "Use OVERWRITE WHERE <__time based condition> or OVERWRITE ALL to overwrite the entire table." + .expectValidationError(invalidSqlContains( + "Incorrect syntax near the keyword 'OVERWRITE' at line 1, column 18." Review Comment: We do get the suggestion too in the error message, right? ########## sql/src/main/java/org/apache/druid/sql/calcite/parser/DruidSqlParserUtils.java: ########## @@ -566,14 +571,28 @@ public static String parseColumnName(SqlNode sqlNode) * @return the timestamp string as milliseconds from epoch * @throws DruidException if the SQL node is not a SqlTimestampLiteral */ - private static String parseTimeStampWithTimeZone(SqlNode sqlNode, DateTimeZone timeZone) + static String parseTimeStampWithTimeZone(SqlNode sqlNode, DateTimeZone timeZone) { + Timestamp sqlTimestamp; + ZonedDateTime zonedTimestamp; + + if (sqlNode instanceof SqlUnknownLiteral) { + try { + SqlTimestampLiteral timestampLiteral = (SqlTimestampLiteral) ((SqlUnknownLiteral) sqlNode).resolve(SqlTypeName.TIMESTAMP); + sqlTimestamp = Timestamp.valueOf(timestampLiteral.toFormattedString()); + } + catch (Exception e) { + throw InvalidSqlInput.exception("Cannot get a timestamp from sql expression [%s]", sqlNode); Review Comment: can this be rephrased a bit since the end-user is going to see this error? I can suggest something if there is an example scenario where this error will pop up. ########## sql/src/main/java/org/apache/druid/sql/calcite/rule/DruidRules.java: ########## @@ -118,8 +118,7 @@ public static List<RelOptRule> rules(PlannerContext plannerContext) if (plannerContext.featureAvailable(EngineFeature.UNNEST)) { retVal.add(new DruidUnnestRule(plannerContext)); retVal.add(new DruidCorrelateUnnestRule(plannerContext)); - retVal.add(ProjectCorrelateTransposeRule.INSTANCE); - retVal.add(CorrelateFilterLTransposeRule.instance()); Review Comment: is CorrelateFilterLTransposeRule not needed? -- 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]
