-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Allow field name declaration in ROW literal #25261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -574,7 +574,7 @@ primaryExpression | |
| QUESTION_MARK #parameter | ||
| POSITION '(' valueExpression IN valueExpression ')' #position | ||
| '(' expression (',' expression)+ ')' #rowConstructor | ||
| ROW '(' expression (',' expression)* ')' #rowConstructor | ||
| ROW '(' fieldConstructor (',' fieldConstructor)* ')' #rowConstructor | ||
| name=LISTAGG '(' setQuantifier? expression (',' string)? | ||
(ON OVERFLOW listAggOverflowBehavior)? ')' | ||
(WITHIN GROUP '(' ORDER BY sortItem (',' sortItem)* ')') | ||
|
@@ -646,6 +646,10 @@ primaryExpression | |
')' #jsonArray | ||
; | ||
|
||
fieldConstructor | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe name this |
||
: expression (AS? identifier)? | ||
; | ||
|
||
jsonPathInvocation | ||
: jsonValueExpression ',' path=string | ||
(AS pathName=identifier)? | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -679,11 +679,11 @@ public Type process(Node node, @Nullable Context context) | |
@Override | ||
protected Type visitRow(Row node, Context context) | ||
{ | ||
List<Type> types = node.getItems().stream() | ||
.map(child -> process(child, context)) | ||
List<RowType.Field> fields = node.getFields().stream() | ||
.map(field -> new RowType.Field(field.getName().map(Identifier::getCanonicalValue), process(field.getExpression(), context))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wrap arguments? |
||
.collect(toImmutableList()); | ||
|
||
Type type = RowType.anonymous(types); | ||
Type type = RowType.from(fields); | ||
return setExpressionType(node, type); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
import com.google.common.base.CharMatcher; | ||
import com.google.common.base.Joiner; | ||
import com.google.common.collect.ImmutableList; | ||
import io.trino.spi.type.RowType; | ||
import io.trino.sql.planner.Symbol; | ||
|
||
import java.util.List; | ||
|
@@ -67,9 +68,19 @@ protected String visitArray(Array node, Void context) | |
@Override | ||
protected String visitRow(Row node, Void context) | ||
{ | ||
return node.items().stream() | ||
.map(child -> process(child, context)) | ||
.collect(joining(", ", "ROW (", ")")); | ||
List<RowType.Field> fieldTypes = ((RowType) node.type()).getFields(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like the IR |
||
|
||
StringBuilder builder = new StringBuilder(); | ||
builder.append("ROW ("); | ||
for (int i = 0; i < fieldTypes.size(); i++) { | ||
if (i > 0) { | ||
builder.append(", "); | ||
} | ||
builder.append(node.items().get(i).accept(this, context)); | ||
fieldTypes.get(i).getName().ifPresent(name -> builder.append(" AS ").append(name)); | ||
} | ||
builder.append(")"); | ||
return builder.toString(); | ||
} | ||
|
||
@Override | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ | |
import static java.util.Objects.requireNonNull; | ||
|
||
@JsonSerialize | ||
public record Row(List<Expression> items) | ||
public record Row(List<Expression> items, Type type) | ||
implements Expression | ||
{ | ||
public Row | ||
|
@@ -33,10 +33,9 @@ public record Row(List<Expression> items) | |
items = ImmutableList.copyOf(items); | ||
} | ||
|
||
@Override | ||
public Type type() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep this and change signature to |
||
public Row(List<Expression> items) | ||
{ | ||
return RowType.anonymous(items.stream().map(Expression::type).collect(Collectors.toList())); | ||
this(items, RowType.anonymous(items.stream().map(Expression::type).toList())); | ||
} | ||
|
||
@Override | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix alignment of the label