mihaibudiu commented on code in PR #5077:
URL: https://github.com/apache/calcite/pull/5077#discussion_r3573330261
##########
core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java:
##########
@@ -2444,18 +2494,101 @@ public Context qualifiedContext() {
return aliasContext(aliases, true);
}
+ /** Returns a result for use as a derived relation in the FROM clause of an
+ * enclosing query. Field names are made unique according to the dialect so
+ * that the enclosing query can reference them. */
+ private Result forDerivedRelation() {
+ if (neededType == null) {
+ return this;
+ }
+ final List<String> fieldNames =
+ SqlValidatorUtil.uniquify(neededType.getFieldNames(),
+ dialect.isCaseSensitive());
+ if (fieldNames.equals(neededType.getFieldNames())) {
+ return this;
+ }
+ final RelDataType type = renameRowTypeFields(neededType, fieldNames);
+ final SqlNode newNode = withOutputFieldNames(fieldNames);
+ final ImmutableMap.Builder<String, RelDataType> aliasBuilder =
+ ImmutableMap.builder();
+ for (Map.Entry<String, RelDataType> alias : aliases.entrySet()) {
+ aliasBuilder.put(alias.getKey(),
+ alias.getValue() == neededType ? type : alias.getValue());
+ }
+ return new Result(newNode, clauses, neededAlias, type,
aliasBuilder.build(), anon,
+ ignoreClauses, expectedClauses, expectedRel, forceExplicitAlias);
+ }
+
+ /** Returns this result's SQL node with {@code fieldNames} as its output
+ * field names.
+ *
+ * @param fieldNames Output field names
+ */
+ private SqlNode withOutputFieldNames(List<String> fieldNames) {
+ if (node.getKind() == SqlKind.AS) {
+ final SqlCall call = (SqlCall) node;
+ final List<SqlNode> operands = call.getOperandList();
+ final int fieldAliasStart = 2;
+ if (operands.size() == fieldNames.size() + fieldAliasStart) {
Review Comment:
I am not sure exactly what is happening here, maybe a comment would help.
--
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]