gianm commented on code in PR #12971:
URL: https://github.com/apache/druid/pull/12971#discussion_r955528160
##########
sql/src/main/java/org/apache/druid/sql/calcite/planner/DruidRexExecutor.java:
##########
@@ -142,40 +142,42 @@ public void reduce(
}
} else if (sqlTypeName == SqlTypeName.ARRAY) {
assert exprResult.isArray();
- if
(SqlTypeName.NUMERIC_TYPES.contains(constExp.getType().getComponentType().getSqlTypeName()))
{
+ final Object[] array = exprResult.asArray();
+ if (array == null) {
+ literal = rexBuilder.makeNullLiteral(constExp.getType());
+ } else if
(SqlTypeName.NUMERIC_TYPES.contains(constExp.getType().getComponentType().getSqlTypeName()))
{
if (exprResult.type().getElementType().is(ExprType.LONG)) {
- List<BigDecimal> resultAsBigDecimalList =
Arrays.stream(exprResult.asArray())
- .map(val -> {
- final Number
longVal = (Number) val;
- if (longVal ==
null) {
- return null;
- }
- return
BigDecimal.valueOf(longVal.longValue());
- })
-
.collect(Collectors.toList());
+ List<BigDecimal> resultAsBigDecimalList = new
ArrayList<>(array.length);
+ for (Object val : array) {
+ final Number longVal = (Number) val;
+ if (longVal == null) {
+ resultAsBigDecimalList.add(null);
+ } else {
+
resultAsBigDecimalList.add(BigDecimal.valueOf(longVal.longValue()));
+ }
+ }
literal = rexBuilder.makeLiteral(resultAsBigDecimalList,
constExp.getType(), true);
} else {
- List<BigDecimal> resultAsBigDecimalList =
Arrays.stream(exprResult.asArray()).map(
- val -> {
- final Number doubleVal = (Number) val;
- if (doubleVal == null) {
- return null;
- }
- if (Double.isNaN(doubleVal.doubleValue()) ||
Double.isInfinite(doubleVal.doubleValue())) {
- String expression = druidExpression.getExpression();
- throw new UnsupportedSQLQueryException(
- "'%s' contains an element that evaluates to '%s'
which is not supported in SQL. You can either cast the element in the array to
bigint or char or change the expression itself",
- expression,
- Double.toString(doubleVal.doubleValue())
- );
- }
- return BigDecimal.valueOf(doubleVal.doubleValue());
- }
- ).collect(Collectors.toList());
+ List<BigDecimal> resultAsBigDecimalList = new
ArrayList<>(array.length);
+ for (Object val : array) {
+ final Number doubleVal = (Number) val;
+ if (doubleVal == null) {
+ resultAsBigDecimalList.add(null);
+ } else if (Double.isNaN(doubleVal.doubleValue()) ||
Double.isInfinite(doubleVal.doubleValue())) {
+ String expression = druidExpression.getExpression();
+ throw new UnsupportedSQLQueryException(
+ "'%s' contains an element that evaluates to '%s' which
is not supported in SQL. You can either cast the element in the array to bigint
or char or change the expression itself",
Review Comment:
I know you didn't change anything here, but you moved it so I get to comment
🙂
"BIGINT or VARCHAR" would be better than "bigint or char".
##########
sql/src/main/java/org/apache/druid/sql/calcite/expression/builtin/NestedDataOperatorConversions.java:
##########
@@ -207,7 +209,16 @@ public static class JsonKeysOperatorConversion implements
SqlOperatorConversion
)
)
.functionCategory(SqlFunctionCategory.USER_DEFINED_FUNCTION)
- .returnTypeNullableArray(SqlTypeName.VARCHAR)
+ .returnTypeInference(
Review Comment:
Hmm. If I read the code right, the difference is that
`returnTypeNullableArray(SqlTypeName.VARCHAR)` yields `ARRAY<VARCHAR NULLABLE>`
(nullable elements, non-nullable array), whereas, what you have here yields
`ARRAY<VARCHAR NULLABLE> NULLABLE` (the array is itself nullable too).
Suggestion: rename `returnTypeNullableArray` to
`returnTypeArrayWithNullableElements` and then introduce
`returnTypeNullableArrayWithNullableElements` that does the thing you have here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]