-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow casted literal values in SQL functions accepting literals (Part 2) #15316
Merged
LakshSingla
merged 4 commits into
apache:master
from
LakshSingla:allow-casted-literals-2
Nov 3, 2023
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
sql/src/main/java/org/apache/calcite/sql/type/CastedLiteralOperandTypeChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
//CHECKSTYLE.OFF: PackageName - Must be in Calcite | ||
|
||
package org.apache.calcite.sql.type; | ||
|
||
import org.apache.calcite.sql.SqlCallBinding; | ||
import org.apache.calcite.sql.SqlNode; | ||
import org.apache.calcite.sql.SqlOperator; | ||
import org.apache.calcite.sql.SqlUtil; | ||
import org.apache.calcite.util.Static; | ||
import org.apache.calcite.util.Util; | ||
|
||
/** | ||
* Like {@link LiteralOperandTypeChecker}, but also allows casted literals. | ||
* | ||
* "Casted literals" are like `CAST(100 AS INTEGER)`. While it doesn't make sense to cast a literal that the user | ||
* themselves enter, it is important to add a broader validation to allow these literals because Calcite's JDBC driver | ||
* doesn't allow the wildcards (?)to work without a cast, and there's no workaround it. | ||
* <p> | ||
* This makes sure that the functions using the literal operand type checker can be workaround the JDBC's restriction, | ||
* without being marked as invalid SQL input | ||
*/ | ||
|
||
public class CastedLiteralOperandTypeChecker implements SqlSingleOperandTypeChecker | ||
{ | ||
public static SqlSingleOperandTypeChecker LITERAL = new CastedLiteralOperandTypeChecker(false); | ||
|
||
private final boolean allowNull; | ||
|
||
CastedLiteralOperandTypeChecker(boolean allowNull) | ||
{ | ||
this.allowNull = allowNull; | ||
} | ||
|
||
@Override | ||
public boolean checkSingleOperandType( | ||
SqlCallBinding callBinding, | ||
SqlNode node, | ||
int iFormalOperand, | ||
boolean throwOnFailure | ||
) | ||
{ | ||
Util.discard(iFormalOperand); | ||
|
||
if (SqlUtil.isNullLiteral(node, true)) { | ||
if (allowNull) { | ||
return true; | ||
} | ||
if (throwOnFailure) { | ||
throw callBinding.newError( | ||
Static.RESOURCE.argumentMustNotBeNull( | ||
callBinding.getOperator().getName())); | ||
} | ||
return false; | ||
} | ||
// The following line of code is the only difference between the OperandTypes.LITERAL and this type checker | ||
if (!SqlUtil.isLiteral(node, true) && !SqlUtil.isLiteralChain(node)) { | ||
if (throwOnFailure) { | ||
throw callBinding.newError( | ||
Static.RESOURCE.argumentMustBeLiteral( | ||
callBinding.getOperator().getName())); | ||
} | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public String getAllowedSignatures(SqlOperator op, String opName) | ||
{ | ||
return "<LITERAL>"; | ||
} | ||
} |
160 changes: 160 additions & 0 deletions
160
sql/src/main/java/org/apache/calcite/sql/type/CastedLiteralOperandTypeCheckers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
//CHECKSTYLE.OFF: PackageName - Must be in Calcite | ||
|
||
package org.apache.calcite.sql.type; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import org.apache.calcite.sql.SqlCall; | ||
import org.apache.calcite.sql.SqlCallBinding; | ||
import org.apache.calcite.sql.SqlLiteral; | ||
import org.apache.calcite.sql.SqlNode; | ||
import org.apache.calcite.sql.SqlUtil; | ||
import org.apache.calcite.sql.parser.SqlParserPos; | ||
import org.apache.calcite.util.Static; | ||
import org.apache.druid.error.DruidException; | ||
|
||
import java.math.BigDecimal; | ||
|
||
public class CastedLiteralOperandTypeCheckers | ||
{ | ||
public static SqlSingleOperandTypeChecker LITERAL = new CastedLiteralOperandTypeChecker(false); | ||
|
||
/** | ||
* Blatantly copied from {@link OperandTypes#POSITIVE_INTEGER_LITERAL}, however the reference to the {@link #LITERAL} | ||
* is the one which accepts casted literals | ||
*/ | ||
public static final SqlSingleOperandTypeChecker POSITIVE_INTEGER_LITERAL = | ||
new FamilyOperandTypeChecker( | ||
ImmutableList.of(SqlTypeFamily.INTEGER), | ||
i -> false | ||
) | ||
{ | ||
@Override | ||
public boolean checkSingleOperandType( | ||
SqlCallBinding callBinding, | ||
SqlNode operand, | ||
int iFormalOperand, | ||
SqlTypeFamily family, | ||
boolean throwOnFailure | ||
) | ||
{ | ||
// This LITERAL refers to the above implementation, the one which allows casted literals | ||
if (!LITERAL.checkSingleOperandType( | ||
callBinding, | ||
operand, | ||
iFormalOperand, | ||
throwOnFailure | ||
)) { | ||
return false; | ||
} | ||
|
||
if (!super.checkSingleOperandType( | ||
callBinding, | ||
operand, | ||
iFormalOperand, | ||
family, | ||
throwOnFailure | ||
)) { | ||
return false; | ||
} | ||
|
||
final SqlLiteral arg = fetchPrimitiveLiteralFromCasts(operand); | ||
final BigDecimal value = arg.getValueAs(BigDecimal.class); | ||
if (value.compareTo(BigDecimal.ZERO) < 0 | ||
|| hasFractionalPart(value)) { | ||
if (throwOnFailure) { | ||
throw callBinding.newError( | ||
Static.RESOURCE.argumentMustBePositiveInteger( | ||
callBinding.getOperator().getName())); | ||
} | ||
return false; | ||
} | ||
if (value.compareTo(BigDecimal.valueOf(Integer.MAX_VALUE)) > 0) { | ||
if (throwOnFailure) { | ||
throw callBinding.newError( | ||
Static.RESOURCE.numberLiteralOutOfRange(value.toString())); | ||
} | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** Returns whether a number has any fractional part. | ||
* | ||
* @see BigDecimal#longValueExact() */ | ||
private boolean hasFractionalPart(BigDecimal bd) | ||
{ | ||
return bd.precision() - bd.scale() <= 0; | ||
} | ||
}; | ||
|
||
public static boolean isLiteral(SqlNode node, boolean allowCast) | ||
{ | ||
assert node != null; | ||
if (node instanceof SqlLiteral) { | ||
return true; | ||
} | ||
if (!allowCast) { | ||
return false; | ||
} | ||
switch (node.getKind()) { | ||
case CAST: | ||
// "CAST(e AS type)" is literal if "e" is literal | ||
return isLiteral(((SqlCall) node).operand(0), true); | ||
case MAP_VALUE_CONSTRUCTOR: | ||
case ARRAY_VALUE_CONSTRUCTOR: | ||
return ((SqlCall) node).getOperandList().stream() | ||
.allMatch(o -> isLiteral(o, true)); | ||
case DEFAULT: | ||
return true; // DEFAULT is always NULL | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Fetches primitive literals from the casts, including NULL literal. | ||
* It throws if the entered node isn't a primitive literal, which can be cast multiple times. | ||
* | ||
* Therefore, it would fail on the following types: | ||
* 1. Nodes that are not of the form CAST(....(CAST LITERAL AS TYPE).....) | ||
* 2. ARRAY and MAP literals. This won't be required since we are only using this method in the type checker for | ||
* primitive types | ||
*/ | ||
private static SqlLiteral fetchPrimitiveLiteralFromCasts(SqlNode node) | ||
{ | ||
if (node == null) { | ||
throw DruidException.defensive("'node' cannot be null"); | ||
} | ||
if (node instanceof SqlLiteral) { | ||
return (SqlLiteral) node; | ||
} | ||
|
||
switch (node.getKind()) { | ||
case CAST: | ||
return fetchPrimitiveLiteralFromCasts(((SqlCall) node).operand(0)); | ||
case DEFAULT: | ||
return SqlLiteral.createNull(SqlParserPos.ZERO); | ||
default: | ||
throw DruidException.defensive("Expected a literal or a cast on the literal. Found [%s] instead", node.getKind()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: What are the implications of this during calcite upgrade ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or better yet move them to apache calcite ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this code is a copy & paste from the Calcite, on upgrading Calcite (minor version), it'd be good to check if anything major has changed it in the code. On a major version upgrade, the interfaces and method signatures are likely to change so we'd probably have to copy & paste the new class with the desired changes.
Casting a literal isn't a very valid operation. Either the type-checking code should allow the dynamic parameters (in Calcite), or we need a different way to replace the parameters. This is kind of a hacky fix, which allows
CAST
on literals using the type checker in the code. The real fix would be to allow the users to not write dynamic parameters with CAST.The only downside with this approach, and Druid 28 is that since we are suggesting users do a CAST(? AS TYPE) while writing JDBC queries, we would need to support this method even if we fix the real reason because users might be writing the queries with an explicit cast. However, that cannot be helped by this fix. This just allows what users have been advised to pass through the Druid's functions.