Skip to content

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ primaryExpression
| QUESTION_MARK #parameter
| POSITION '(' valueExpression IN valueExpression ')' #position
| '(' expression (',' expression)+ ')' #rowConstructor
| ROW '(' expression (',' expression)* ')' #rowConstructor
| ROW '(' fieldConstructor (',' fieldConstructor)* ')' #rowConstructor
Copy link
Member

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

| name=LISTAGG '(' setQuantifier? expression (',' string)?
(ON OVERFLOW listAggOverflowBehavior)? ')'
(WITHIN GROUP '(' ORDER BY sortItem (',' sortItem)* ')')
Expand Down Expand Up @@ -646,6 +646,10 @@ primaryExpression
')' #jsonArray
;

fieldConstructor
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe name this rowField

: expression (AS? identifier)?
;

jsonPathInvocation
: jsonValueExpression ',' path=string
(AS pathName=identifier)?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -701,10 +701,16 @@ protected Boolean visitTryExpression(TryExpression node, Void context)
@Override
protected Boolean visitRow(Row node, Void context)
{
return node.getItems().stream()
return node.getFields().stream()
.allMatch(item -> process(item, context));
}

@Override
protected Boolean visitRowField(Row.Field node, Void context)
{
return process(node.getExpression(), context);
}

@Override
protected Boolean visitParameter(Parameter node, Void context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Copy link
Member

Choose a reason for hiding this comment

The 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3929,7 +3929,7 @@ protected Scope visitValues(Values node, Optional<Scope> scope)
// TODO coerce the whole Row and add an Optimizer rule that converts CAST(ROW(...) AS ...) into ROW(CAST(...), CAST(...), ...).
// The rule would also handle Row-type expressions that were specified as CAST(ROW). It should support multiple casts over a ROW.
for (int i = 0; i < actualType.getTypeParameters().size(); i++) {
Expression item = ((Row) row).getItems().get(i);
Expression item = ((Row) row).getFields().get(i).getExpression();
Type actualItemType = actualType.getTypeParameters().get(i);
Type expectedItemType = commonSuperType.getTypeParameters().get(i);
if (!actualItemType.equals(expectedItemType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the IR Row type should override type() to return RowType so that callers don't need to cast


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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected Expression visitRow(Row node, Context<C> context)
List<Expression> items = rewrite(node.items(), context);

if (!sameElements(node.items(), items)) {
return new Row(items);
return new Row(items, node.type());
}

return node;
Expand Down
7 changes: 3 additions & 4 deletions core/trino-main/src/main/java/io/trino/sql/ir/Row.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,10 +33,9 @@ public record Row(List<Expression> items)
items = ImmutableList.copyOf(items);
}

@Override
public Type type()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep this and change signature to RowType

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private Optional<Expression> processChildren(Expression expression, Session sess
case Logical logical -> process(logical.terms(), session, bindings).map(arguments -> new Logical(logical.operator(), arguments));
case Call call -> process(call.arguments(), session, bindings).map(arguments -> new Call(call.function(), arguments));
case Array array -> process(array.elements(), session, bindings).map(elements -> new Array(array.elementType(), elements));
case Row row -> process(row.items(), session, bindings).map(fields -> new Row(fields));
case Row row -> process(row.items(), session, bindings).map(fields -> new Row(fields, row.type()));
case Between between -> {
Optional<Expression> value = process(between.value(), session, bindings);
Optional<Expression> min = process(between.min(), session, bindings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class EvaluateRow
@Override
public Optional<Expression> apply(Expression expression, Session session, Map<Symbol, Expression> bindings)
{
if (!(expression instanceof Row(List<Expression> fields)) || !fields.stream().allMatch(Constant.class::isInstance)) {
if (!(expression instanceof Row(List<Expression> fields, RowType _)) || !fields.stream().allMatch(Constant.class::isInstance)) {
return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1764,10 +1764,16 @@ protected RelationPlan visitValues(Values node, Void context)

ImmutableList.Builder<Expression> rows = ImmutableList.builder();
for (io.trino.sql.tree.Expression row : node.getRows()) {
if (row instanceof io.trino.sql.tree.Row) {
rows.add(new Row(((io.trino.sql.tree.Row) row).getItems().stream()
.map(item -> coerceIfNecessary(analysis, item, translationMap.rewrite(item)))
.collect(toImmutableList())));
if (row instanceof io.trino.sql.tree.Row astRow) {
ImmutableList.Builder<Expression> fields = ImmutableList.builder();
ImmutableList.Builder<RowType.Field> typeFields = ImmutableList.builder();
for (int i = 0; i < astRow.getFields().size(); i++) {
io.trino.sql.tree.Row.Field astField = astRow.getFields().get(i);
Expression expression = coerceIfNecessary(analysis, astField.getExpression(), translationMap.rewrite(astField.getExpression()));
fields.add(expression);
typeFields.add(new RowType.Field(astField.getName().map(Identifier::getCanonicalValue), expression.type()));
}
rows.add(new Row(fields.build(), RowType.from(typeFields.build())));
}
else if (analysis.getType(row) instanceof RowType) {
rows.add(coerceIfNecessary(analysis, row, translationMap.rewrite(row)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
import static io.trino.sql.ir.Comparison.Operator.NOT_EQUAL;
import static io.trino.sql.ir.IrExpressions.ifExpression;
import static io.trino.sql.ir.IrExpressions.not;
import static io.trino.sql.planner.QueryPlanner.coerceIfNecessary;
import static io.trino.sql.planner.ScopeAware.scopeAwareKey;
import static io.trino.sql.tree.JsonQuery.EmptyOrErrorBehavior.ERROR;
import static io.trino.sql.tree.JsonQuery.QuotesBehavior.KEEP;
Expand Down Expand Up @@ -545,11 +546,17 @@ private io.trino.sql.ir.Expression translate(NotExpression expression)
return not(plannerContext.getMetadata(), translateExpression(expression.getValue()));
}

private io.trino.sql.ir.Expression translate(Row expression)
private io.trino.sql.ir.Expression translate(Row row)
{
return new io.trino.sql.ir.Row(expression.getItems().stream()
.map(this::translateExpression)
.collect(toImmutableList()));
ImmutableList.Builder<io.trino.sql.ir.Expression> fields = ImmutableList.builder();
ImmutableList.Builder<RowType.Field> typeFields = ImmutableList.builder();
for (int i = 0; i < row.getFields().size(); i++) {
io.trino.sql.tree.Row.Field field = row.getFields().get(i);
io.trino.sql.ir.Expression expression = coerceIfNecessary(analysis, field.getExpression(), translateExpression(field.getExpression()));
fields.add(expression);
typeFields.add(new RowType.Field(field.getName().map(Identifier::getCanonicalValue), expression.type()));
}
return new io.trino.sql.ir.Row(fields.build(), RowType.from(typeFields.build()));
}

private io.trino.sql.ir.Expression translate(ComparisonExpression expression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,18 +300,20 @@ public Result apply(ValuesNode valuesNode, Captures captures, Context context)

boolean anyRewritten = false;
ImmutableList.Builder<Expression> rows = ImmutableList.builder();
for (Expression row : valuesNode.getRows().get()) {
for (Expression original : valuesNode.getRows().get()) {
Expression rewritten;
if (row instanceof Row) {
if (original instanceof Row row) {
// preserve the structure of row
rewritten = new Row(((Row) row).items().stream()
.map(item -> rewriter.rewrite(item, context))
.collect(toImmutableList()));
rewritten = new Row(
row.items().stream()
.map(item -> rewriter.rewrite(item, context))
.collect(toImmutableList()),
row.type());
}
else {
rewritten = rewriter.rewrite(row, context);
rewritten = rewriter.rewrite(original, context);
}
if (!row.equals(rewritten)) {
if (!original.equals(rewritten)) {
anyRewritten = true;
}
rows.add(rewritten);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,73 +20,57 @@
import io.trino.sql.ir.Expression;
import io.trino.sql.ir.ExpressionTreeRewriter;
import io.trino.sql.ir.Row;
import io.trino.type.UnknownType;

/**
* Transforms expressions of the form
*
* <pre>
* CAST(
* CAST(
* ROW(x, y)
* AS row(f1 type1, f2 type2))
* AS row(g1 type3, g2 type4))
* ROW(x, y)
* AS row(f1 type1, f2 type2))
* </pre>
*
* to
*
* <pre>
* CAST(
* ROW(
* CAST(x AS type1),
* CAST(y AS type2))
* AS row(g1 type3, g2 type4))
* ROW(
* CAST(x AS type1) as f1,
* CAST(y AS type2) as f2)
* </pre>
*
* Note: it preserves the top-level CAST if the row type has field names because the names are needed by the ROW to JSON cast
* TODO: ideally, the types involved in ROW to JSON cast should be captured at analysis time and
* remain fixed for the duration of the optimization process so as to have flexibility in terms
* of removing field names, which are irrelevant in the IR
*/
public class PushCastIntoRow
extends ExpressionRewriteRuleSet
{
public PushCastIntoRow()
{
super((expression, context) -> ExpressionTreeRewriter.rewriteWith(new Rewriter(), expression, false));
super((expression, context) -> ExpressionTreeRewriter.rewriteWith(new Rewriter(), expression, null));
}

private static class Rewriter
extends io.trino.sql.ir.ExpressionRewriter<Boolean>
extends io.trino.sql.ir.ExpressionRewriter<Void>
{
@Override
public Expression rewriteCast(Cast node, Boolean inRowCast, ExpressionTreeRewriter<Boolean> treeRewriter)
public Expression rewriteCast(Cast node, Void context, ExpressionTreeRewriter<Void> treeRewriter)
{
if (!(node.type() instanceof RowType type)) {
return treeRewriter.defaultRewrite(node, false);
if (!(node.type() instanceof RowType castToType)) {
return treeRewriter.defaultRewrite(node, null);
}

// if inRowCast == true or row is anonymous, we're free to push Cast into Row. An enclosing CAST(... AS ROW) will take care of preserving field names
// otherwise, apply recursively with inRowCast == true and don't push this one

if (inRowCast || type.getFields().stream().allMatch(field -> field.getName().isEmpty())) {
Expression value = treeRewriter.rewrite(node.expression(), true);

if (value instanceof Row row) {
ImmutableList.Builder<Expression> items = ImmutableList.builder();
for (int i = 0; i < row.items().size(); i++) {
Expression item = row.items().get(i);
Type itemType = type.getFields().get(i).getType();
if (!(itemType instanceof UnknownType)) {
item = new Cast(item, itemType);
}
items.add(item);
Expression value = treeRewriter.rewrite(node.expression(), null);
if (value instanceof Row(java.util.List<Expression> expressions, RowType type)) {
ImmutableList.Builder<Expression> items = ImmutableList.builder();
for (int i = 0; i < expressions.size(); i++) {
Expression fieldValue = expressions.get(i);
Type fieldType = castToType.getFields().get(i).getType();
if (!fieldValue.type().equals(fieldType)) {
fieldValue = new Cast(fieldValue, fieldType);
}
return new Row(items.build());
items.add(fieldValue);
}
return new Row(items.build(), castToType);
}

return treeRewriter.defaultRewrite(node, true);
return treeRewriter.defaultRewrite(node, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public Result apply(CorrelatedJoinNode parent, Captures captures, Context contex
.putIdentities(parent.getInput().getOutputSymbols());
forEachPair(
values.getOutputSymbols().stream(),
row.items().stream(),
row.children().stream(),
assignments::put);
return Result.ofPlanNode(projectNode(parent.getInput(), assignments.build(), context));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import io.trino.sql.tree.NullLiteral;
import io.trino.sql.tree.Parameter;
import io.trino.sql.tree.Query;
import io.trino.sql.tree.Row;
import io.trino.sql.tree.SelectItem;
import io.trino.sql.tree.ShowStats;
import io.trino.sql.tree.Statement;
Expand All @@ -74,6 +73,7 @@
import static io.trino.spi.type.TimeZoneKey.UTC_KEY;
import static io.trino.spi.type.VarcharType.VARCHAR;
import static io.trino.sql.QueryUtil.aliased;
import static io.trino.sql.QueryUtil.row;
import static io.trino.sql.QueryUtil.selectAll;
import static io.trino.sql.QueryUtil.selectList;
import static io.trino.sql.QueryUtil.simpleQuery;
Expand Down Expand Up @@ -176,26 +176,24 @@ private Node rewriteShowStats(Plan plan, PlanNodeStatsEstimate planNodeStatsEsti
String columnName = root.getColumnNames().get(columnIndex);
Type columnType = outputSymbol.type();
SymbolStatsEstimate symbolStatistics = planNodeStatsEstimate.getSymbolStatistics(outputSymbol);
ImmutableList.Builder<Expression> rowValues = ImmutableList.builder();
rowValues.add(new StringLiteral(columnName));
rowValues.add(toDoubleLiteral(symbolStatistics.getAverageRowSize() * planNodeStatsEstimate.getOutputRowCount() * (1 - symbolStatistics.getNullsFraction())));
rowValues.add(toDoubleLiteral(symbolStatistics.getDistinctValuesCount()));
rowValues.add(toDoubleLiteral(symbolStatistics.getNullsFraction()));
rowValues.add(NULL_DOUBLE);
rowValues.add(toStringLiteral(columnType, symbolStatistics.getLowValue()));
rowValues.add(toStringLiteral(columnType, symbolStatistics.getHighValue()));
rowsBuilder.add(new Row(rowValues.build()));
rowsBuilder.add(row(
new StringLiteral(columnName),
toDoubleLiteral(symbolStatistics.getAverageRowSize() * planNodeStatsEstimate.getOutputRowCount() * (1 - symbolStatistics.getNullsFraction())),
toDoubleLiteral(symbolStatistics.getDistinctValuesCount()),
toDoubleLiteral(symbolStatistics.getNullsFraction()),
NULL_DOUBLE,
toStringLiteral(columnType, symbolStatistics.getLowValue()),
toStringLiteral(columnType, symbolStatistics.getHighValue())));
}
// Stats for whole table
ImmutableList.Builder<Expression> rowValues = ImmutableList.builder();
rowValues.add(NULL_VARCHAR);
rowValues.add(NULL_DOUBLE);
rowValues.add(NULL_DOUBLE);
rowValues.add(NULL_DOUBLE);
rowValues.add(toDoubleLiteral(planNodeStatsEstimate.getOutputRowCount()));
rowValues.add(NULL_VARCHAR);
rowValues.add(NULL_VARCHAR);
rowsBuilder.add(new Row(rowValues.build()));
rowsBuilder.add(row(
NULL_VARCHAR,
NULL_DOUBLE,
NULL_DOUBLE,
NULL_DOUBLE,
toDoubleLiteral(planNodeStatsEstimate.getOutputRowCount()),
NULL_VARCHAR,
NULL_VARCHAR));
List<Expression> resultRows = rowsBuilder.build();

return simpleQuery(selectAll(selectItems), aliased(new Values(resultRows), "table_stats", statsColumnNames));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected Expression rewriteExpression(Expression node, Void context, Expression
public Expression rewriteRow(Row node, Void context, ExpressionTreeRewriter<Void> treeRewriter)
{
// rewrite Row items to preserve Row structure of ValuesNode
return new Row(node.items().stream().map(item -> new Constant(INTEGER, 0L)).collect(toImmutableList()));
return new Row(node.items().stream().map(item -> new Constant(INTEGER, 0L)).collect(toImmutableList()), node.type());
}
}, expression));

Expand Down
Loading