somu-imply commented on code in PR #13576:
URL: https://github.com/apache/druid/pull/13576#discussion_r1081630567
##########
sql/src/main/java/org/apache/druid/sql/calcite/expression/Expressions.java:
##########
@@ -214,12 +217,43 @@ public static DruidExpression
toDruidExpressionWithPostAggOperands(
return rexCallToDruidExpression(plannerContext, rowSignature, rexNode,
postAggregatorVisitor);
} else if (kind == SqlKind.LITERAL) {
return literalToDruidExpression(plannerContext, rexNode);
+ } else if (kind == SqlKind.FIELD_ACCESS) {
+ return fieldAccessToDruidExpression(rowSignature, rexNode);
} else {
// Can't translate.
return null;
}
}
+ private static DruidExpression fieldAccessToDruidExpression(
+ final RowSignature rowSignature,
+ final RexNode rexNode
+ )
+ {
+ // Translate field references.
+ final RexFieldAccess ref = (RexFieldAccess) rexNode;
+ // This case arises in the case of a correlation where the rexNode points
to a table from the left subtree
+ // while the underlying datasource is the scan stub created from
LogicalValuesRule
+ // In such a case we throw a CannotBuildQueryException so that Calcite
does not go ahead with this path
+ // This exception is caught while returning false from isValidDruidQuery()
method
+
+ //use this index to return
+ final int index =
rowSignature.getColumnNames().indexOf(ref.getField().getName());
+ if (ref.getField().getIndex() > rowSignature.size()) {
+ throw new CannotBuildQueryException(StringUtils.format(
+ "Cannot build query as column name [%s] does not exist in row [%s]",
ref.getField().getName(), rowSignature)
+ );
+ }
+
+ final String columnName = rowSignature.getColumnName(index);
+ final Optional<ColumnType> columnType = rowSignature.getColumnType(index);
+ if (columnName == null) {
+ throw new ISE("Expression referred to nonexistent index [%d] in row
[%s]", index, rowSignature);
+ }
+
+ return DruidExpression.ofColumn(columnType.orElse(null), columnName);
Review Comment:
With the index in bounds this should be just `columnType.get()`
--
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]