You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by "LakshSingla (via GitHub)" <gi...@apache.org> on 2023/11/02 22:41:48 UTC
[PR] Allow casted literal values in SQL functions accepting literals (Part 2) (druid)
LakshSingla opened a new pull request, #15316:
URL: https://github.com/apache/druid/pull/15316
### Description
This is a follow-up to https://github.com/apache/druid/pull/15282. I missed a few instances of type checkers being used which internally used the `OperandTypes.LITERAL`, causing the same issue as described in the mentioned PR. This PR cleans up those usages as well, and unfortunately duplicates a fair bit of code from Calcite, to change a few calls which define whether or not casted literals can be used or not.
#### Release note
<!-- Give your best effort to summarize your changes in a couple of sentences aimed toward Druid users.
If your change doesn't have end user impact, you can skip this section.
For tips about how to write a good release note, see [Release notes](https://github.com/apache/druid/blob/master/CONTRIBUTING.md#release-notes).
-->
<hr>
##### Key changed/added classes in this PR
* `MyFoo`
* `OurBar`
* `TheirBaz`
<hr>
<!-- Check the items by putting "x" in the brackets for the done things. Not all of these items apply to every PR. Remove the items which are not done or not relevant to the PR. None of the items from the checklist below are strictly necessary, but it would be very helpful if you at least self-review the PR. -->
This PR has:
- [ ] been self-reviewed.
- [ ] using the [concurrency checklist](https://github.com/apache/druid/blob/master/dev/code-review/concurrency.md) (Remove this item if the PR doesn't have any relation to concurrency.)
- [ ] added documentation for new or modified features or behaviors.
- [ ] a release note entry in the PR description.
- [ ] added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
- [ ] added or updated version, license, or notice information in [licenses.yaml](https://github.com/apache/druid/blob/master/dev/license.md)
- [ ] added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
- [ ] added unit tests or modified existing tests to cover new code paths, ensuring the threshold for [code coverage](https://github.com/apache/druid/blob/master/dev/code-review/code-coverage.md) is met.
- [ ] added integration tests.
- [ ] been tested in a test Druid cluster.
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Allow casted literal values in SQL functions accepting literals (Part 2) (druid)
Posted by "LakshSingla (via GitHub)" <gi...@apache.org>.
LakshSingla commented on code in PR #15316:
URL: https://github.com/apache/druid/pull/15316#discussion_r1381473076
##########
sql/src/main/java/org/apache/calcite/sql/type/CastedLiteralOperandTypeCheckers.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * 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
Review Comment:
> Nit: What are the implications of this during the Calcite upgrade?
Since this code is a copy & paste from the Calcite, on upgrading Calcite (major version), it'd be good to check if anything major has changed it in the code.
> Or better yet move them to Apache Calcite?
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.
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Allow casted literal values in SQL functions accepting literals (Part 2) (druid)
Posted by "LakshSingla (via GitHub)" <gi...@apache.org>.
LakshSingla commented on PR #15316:
URL: https://github.com/apache/druid/pull/15316#issuecomment-1792385559
There would be a few code coverage failures, which seem expected, given that I have copied functions from Calcite. Adding a few negative tests might improve it but won't fix it completely, so I am favor of leaving them as is.
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Allow casted literal values in SQL functions accepting literals (Part 2) (druid)
Posted by "LakshSingla (via GitHub)" <gi...@apache.org>.
LakshSingla commented on PR #15316:
URL: https://github.com/apache/druid/pull/15316#issuecomment-1792690162
The following 5 actions are failing, and all of them are related to codecov in the newly added functions.
Merging the PR
https://github.com/apache/druid/actions/runs/6744567321/job/18335247194?pr=15316
https://github.com/apache/druid/actions/runs/6744567321/job/18335247344?pr=15316
https://github.com/apache/druid/actions/runs/6744567321/job/18337279442?pr=15316
https://github.com/apache/druid/actions/runs/6744567321/job/18337279628?pr=15316
https://github.com/apache/druid/actions/runs/6744567321/job/18337279877?pr=15316
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Allow casted literal values in SQL functions accepting literals (Part 2) (druid)
Posted by "LakshSingla (via GitHub)" <gi...@apache.org>.
LakshSingla commented on code in PR #15316:
URL: https://github.com/apache/druid/pull/15316#discussion_r1381217000
##########
sql/src/main/java/org/apache/calcite/sql/type/CastedLiteralOperandTypeCheckers.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * 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;
+ }
+
+ if (SqlUtil.isLiteral(node, false)) {
+ return (SqlLiteral) node;
Review Comment:
I think the cast is incorrect in my code.
The `SqlUtil.isLiteral(node, false)` would be true if the node is `ARRAY['a', 'b']` but the node won't be an instance of `SqlLiteral`.
```
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;
}
}
```
I'll remove this check, since it'd be false anyways.
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Allow casted literal values in SQL functions accepting literals (Part 2) (druid)
Posted by "github-advanced-security[bot] (via GitHub)" <gi...@apache.org>.
github-advanced-security[bot] commented on code in PR #15316:
URL: https://github.com/apache/druid/pull/15316#discussion_r1380881752
##########
sql/src/main/java/org/apache/calcite/sql/type/CastedLiteralOperandTypeCheckers.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * 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;
+ }
+
+ if (SqlUtil.isLiteral(node, false)) {
+ return (SqlLiteral) node;
Review Comment:
## Contradictory type checks
This access of [node](1) cannot be of type [SqlLiteral](2), since [this expression](3) ensures that it is not of type [SqlLiteral](2).
[Show more details](https://github.com/apache/druid/security/code-scanning/5958)
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Allow casted literal values in SQL functions accepting literals (Part 2) (druid)
Posted by "LakshSingla (via GitHub)" <gi...@apache.org>.
LakshSingla commented on code in PR #15316:
URL: https://github.com/apache/druid/pull/15316#discussion_r1381217000
##########
sql/src/main/java/org/apache/calcite/sql/type/CastedLiteralOperandTypeCheckers.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * 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;
+ }
+
+ if (SqlUtil.isLiteral(node, false)) {
+ return (SqlLiteral) node;
Review Comment:
I think the cast is incorrect here.
The `SqlUtil.isLiteral(node, false)` would be true if the node is `ARRAY['a', 'b']` but the node won't be an instance of `SqlLiteral`.
```
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;
}
}
```
I'll remove this check, since it'd be false anyways.
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Allow casted literal values in SQL functions accepting literals (Part 2) (druid)
Posted by "LakshSingla (via GitHub)" <gi...@apache.org>.
LakshSingla commented on code in PR #15316:
URL: https://github.com/apache/druid/pull/15316#discussion_r1381217000
##########
sql/src/main/java/org/apache/calcite/sql/type/CastedLiteralOperandTypeCheckers.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * 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;
+ }
+
+ if (SqlUtil.isLiteral(node, false)) {
+ return (SqlLiteral) node;
Review Comment:
~I think the cast is incorrect in my code.~
~The `SqlUtil.isLiteral(node, false)` would be true if the node is `ARRAY['a', 'b']` but the node won't be an instance of `SqlLiteral`.~
I see that'd it would fail before hand. I'll remove this check.
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Allow casted literal values in SQL functions accepting literals (Part 2) (druid)
Posted by "LakshSingla (via GitHub)" <gi...@apache.org>.
LakshSingla commented on code in PR #15316:
URL: https://github.com/apache/druid/pull/15316#discussion_r1381473076
##########
sql/src/main/java/org/apache/calcite/sql/type/CastedLiteralOperandTypeCheckers.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * 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
Review Comment:
> Nit: What are the implications of this during the Calcite upgrade?
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.
> Or better yet move them to Apache Calcite?
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.
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Allow casted literal values in SQL functions accepting literals (Part 2) (druid)
Posted by "clintropolis (via GitHub)" <gi...@apache.org>.
clintropolis commented on code in PR #15316:
URL: https://github.com/apache/druid/pull/15316#discussion_r1381211784
##########
sql/src/main/java/org/apache/calcite/sql/type/CastedLiteralOperandTypeCheckers.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * 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;
+ }
+
+ if (SqlUtil.isLiteral(node, false)) {
+ return (SqlLiteral) node;
Review Comment:
the bot is right, the previous `if (node instance SqlLiteral)` already covers this since `SqlUtil.isLiteral` will only do the same check if `false` is set.
```
public static boolean isLiteral(SqlNode node, boolean allowCast) {
assert node != null;
if (node instanceof SqlLiteral) {
return true;
}
if (!allowCast) {
return false;
}
```
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Allow casted literal values in SQL functions accepting literals (Part 2) (druid)
Posted by "LakshSingla (via GitHub)" <gi...@apache.org>.
LakshSingla commented on code in PR #15316:
URL: https://github.com/apache/druid/pull/15316#discussion_r1381473076
##########
sql/src/main/java/org/apache/calcite/sql/type/CastedLiteralOperandTypeCheckers.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * 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
Review Comment:
> Nit: What are the implications of this during the Calcite upgrade?
Since this code is a copy & paste from the Calcite, on upgrading Calcite (major version), it'd be good to check if anything major has changed it in the code.
> Or better yet move them to Apache Calcite?
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.
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Allow casted literal values in SQL functions accepting literals (Part 2) (druid)
Posted by "LakshSingla (via GitHub)" <gi...@apache.org>.
LakshSingla merged PR #15316:
URL: https://github.com/apache/druid/pull/15316
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Allow casted literal values in SQL functions accepting literals (Part 2) (druid)
Posted by "cryptoe (via GitHub)" <gi...@apache.org>.
cryptoe commented on code in PR #15316:
URL: https://github.com/apache/druid/pull/15316#discussion_r1381444142
##########
sql/src/main/java/org/apache/calcite/sql/type/CastedLiteralOperandTypeCheckers.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * 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
Review Comment:
Or better yet move them to apache calcite ?
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Allow casted literal values in SQL functions accepting literals (Part 2) (druid)
Posted by "LakshSingla (via GitHub)" <gi...@apache.org>.
LakshSingla commented on code in PR #15316:
URL: https://github.com/apache/druid/pull/15316#discussion_r1381217000
##########
sql/src/main/java/org/apache/calcite/sql/type/CastedLiteralOperandTypeCheckers.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * 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;
+ }
+
+ if (SqlUtil.isLiteral(node, false)) {
+ return (SqlLiteral) node;
Review Comment:
~I think the cast is incorrect in my code.~
~The `SqlUtil.isLiteral(node, false)` would be true if the node is `ARRAY['a', 'b']` but the node won't be an instance of `SqlLiteral`.~
I see that it would fail before the case I was thinking of gets hit. I'll remove this check.
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Allow casted literal values in SQL functions accepting literals (Part 2) (druid)
Posted by "cryptoe (via GitHub)" <gi...@apache.org>.
cryptoe commented on code in PR #15316:
URL: https://github.com/apache/druid/pull/15316#discussion_r1381442515
##########
sql/src/main/java/org/apache/calcite/sql/type/CastedLiteralOperandTypeCheckers.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * 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
Review Comment:
Nit: What are the implications of this during calcite upgrade ?
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org