asolimando commented on code in PR #4902:
URL: https://github.com/apache/calcite/pull/4902#discussion_r3122382019
##########
core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java:
##########
@@ -2367,8 +2375,28 @@ public SqlNode asStatement() {
case MERGE:
return maybeStrip(node);
default:
- return maybeStrip(asSelect());
+ return maybeStrip(maybeExpandStar(asSelect()));
+ }
+ }
+
+ /** If the dialect does not support {@code SELECT *} and the select list
+ * is {@link SqlNodeList#SINGLETON_STAR}, replaces it with explicit column
+ * references derived from the result's aliases. */
+ SqlSelect maybeExpandStar(SqlSelect select) {
+ if (expectedRel != null
+ && !expectedRel.getInputs().isEmpty()
+ && select.getSelectList().equals(SqlNodeList.SINGLETON_STAR)
+ && !dialect.supportGenerateSelectStar(expectedRel.getInput(0))) {
+ boolean qualified =
+ !dialect.hasImplicitTableAlias() || aliases.size() > 1;
+ final Context ctx = aliasContext(aliases, qualified);
+ final List<SqlNode> expandedList = new ArrayList<>();
+ for (int i = 0; i < ctx.fieldCount; i++) {
+ expandedList.add(ctx.field(i));
+ }
+ select.setSelectList(new SqlNodeList(expandedList, POS));
Review Comment:
That's a good question and I don't have a definitive answer, in general I am
not a fan of this pattern, as Java doesn't have a good way to document this
type of contracts, and it can easily get confusing/problematic.
In this specific case `maybeExpandStar` is always called as
`maybeExpandStar(asSelect())`, it is technically mutating the argument, but
it's basically a short-hand to mutate the internal state, like what
[asSelect()](https://github.com/apache/calcite/blob/82e5ae70720de17a010d3f39be1119b86610cdd2/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java#L2273)
does already to build the `FROM` clause.
If we are worried this might create an unwanted precedent or that
`maybeExpandStar` could be used in unsafe ways, we could:
1. inline the mutation of the internal state explicitly in `asSelect()` and
avoid the `maybeExpandStar` method and argument mutation altogether, or
2. keep `maybeExpandStar` but use a ~copy-constructor (`return new
SqlSelect(...)`) to avoid the mutation.
WDYT?
--
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]