Skip to content
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

Fix the order of parameters listed by DESCRIBE INPUT #15086

Closed
wants to merge 2 commits into from
Closed
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 @@ -65,4 +65,16 @@ public void testLambda()

assertThat(ParameterExtractor.getParameterCount(statement)).isEqualTo(1);
}

@Test
public void testWith()
{
// The parameter from CTE has id=0. The parameter from the query has id=1. In the DESCRIBE statement they will be listed following this order.
Statement statement = sqlParser.createStatement("WITH t(a) AS (VALUES ?) SELECT a + ? FROM t", new ParsingOptions());
assertThat(ParameterExtractor.getParameters(statement))
.containsExactly(
new Parameter(new NodeLocation(1, 22), 0),
new Parameter(new NodeLocation(1, 38), 1));
assertThat(ParameterExtractor.getParameterCount(statement)).isEqualTo(2);
}
}
20 changes: 20 additions & 0 deletions core/trino-parser/src/main/java/io/trino/sql/QueryUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import io.trino.sql.tree.Values;
import io.trino.sql.tree.WhenClause;
import io.trino.sql.tree.WindowDefinition;
import io.trino.sql.tree.With;

import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -296,4 +297,23 @@ public static Query query(QueryBody body)
Optional.empty(),
Optional.empty());
}

public static Query query(With with, Select select, Relation from)
{
return new Query(
Optional.of(with),
new QuerySpecification(
select,
Optional.of(from),
Optional.empty(),
Optional.empty(),
Optional.empty(),
ImmutableList.of(),
Optional.empty(),
Optional.empty(),
Optional.empty()),
Optional.empty(),
Optional.empty(),
Optional.empty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -913,11 +913,12 @@ public Node visitProperty(SqlBaseParser.PropertyContext context)
@Override
public Node visitQuery(SqlBaseParser.QueryContext context)
{
Optional<With> with = visitIfPresent(context.with(), With.class);
Query body = (Query) visit(context.queryNoWith());

return new Query(
getLocation(context),
visitIfPresent(context.with(), With.class),
with,
body.getQueryBody(),
body.getOrderBy(),
body.getOffset(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@
import static io.trino.sql.parser.TreeNodes.rowType;
import static io.trino.sql.parser.TreeNodes.simpleType;
import static io.trino.sql.testing.TreeAssertions.assertFormattedSql;
import static io.trino.sql.tree.ArithmeticBinaryExpression.Operator.ADD;
import static io.trino.sql.tree.ArithmeticBinaryExpression.Operator.DIVIDE;
import static io.trino.sql.tree.ArithmeticBinaryExpression.Operator.MULTIPLY;
import static io.trino.sql.tree.ArithmeticBinaryExpression.Operator.SUBTRACT;
import static io.trino.sql.tree.ArithmeticUnaryExpression.negative;
import static io.trino.sql.tree.ArithmeticUnaryExpression.positive;
import static io.trino.sql.tree.ComparisonExpression.Operator.EQUAL;
Expand Down Expand Up @@ -896,25 +900,32 @@ public void testPrecedenceAndAssociativity()
new NotExpression(new LongLiteral("1")),
new LongLiteral("2")));

assertExpression("-1 + 2", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.ADD,
assertExpression("-1 + 2", new ArithmeticBinaryExpression(
ADD,
new LongLiteral("-1"),
new LongLiteral("2")));

assertExpression("1 - 2 - 3", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.SUBTRACT,
new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.SUBTRACT,
assertExpression("1 - 2 - 3", new ArithmeticBinaryExpression(
SUBTRACT,
new ArithmeticBinaryExpression(
SUBTRACT,
new LongLiteral("1"),
new LongLiteral("2")),
new LongLiteral("3")));

assertExpression("1 / 2 / 3", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.DIVIDE,
new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.DIVIDE,
assertExpression("1 / 2 / 3", new ArithmeticBinaryExpression(
DIVIDE,
new ArithmeticBinaryExpression(
DIVIDE,
new LongLiteral("1"),
new LongLiteral("2")),
new LongLiteral("3")));

assertExpression("1 + 2 * 3", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.ADD,
assertExpression("1 + 2 * 3", new ArithmeticBinaryExpression(
ADD,
new LongLiteral("1"),
new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.MULTIPLY,
new ArithmeticBinaryExpression(
MULTIPLY,
new LongLiteral("2"),
new LongLiteral("3"))));
}
Expand Down Expand Up @@ -1874,7 +1885,7 @@ public void testMerge()
Optional.of(equal(nameReference("c", "action"), new StringLiteral("mod"))),
ImmutableList.of(
new MergeUpdate.Assignment(new Identifier("qty"), new ArithmeticBinaryExpression(
ArithmeticBinaryExpression.Operator.ADD,
ADD,
nameReference("qty"),
nameReference("c", "qty"))),
new MergeUpdate.Assignment(new Identifier("ts"), new CurrentTime(CurrentTime.Function.TIMESTAMP)))),
Expand Down Expand Up @@ -2759,6 +2770,20 @@ public void testPrepareWithParameters()
Optional.empty(),
Optional.of(new Offset(new Parameter(1))),
Optional.of(new FetchFirst(new Parameter(2), true)))));

// The parameter from CTE has id=0. The parameter from the query has id=1. In the DESCRIBE statement they will be listed following this order.
assertStatement("PREPARE myquery FROM WITH t(a) AS (VALUES ROW(?)) SELECT a + ? FROM t",
new Prepare(
identifier("myquery"),
query(
new With(
false,
ImmutableList.of(new WithQuery(
identifier("t"),
query(values(row(new Parameter(0)))),
Optional.of(ImmutableList.of(identifier("a")))))),
selectList(new ArithmeticBinaryExpression(ADD, identifier("a"), new Parameter(1))),
table(QualifiedName.of("t")))));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,17 @@ public void testDescribeInput()
.row(4, "decimal(3,2)")
.build();
assertEqualsIgnoreOrder(actual, expected);

session = Session.builder(getSession())
.addPreparedStatement("my_query", "WITH t(a) AS (VALUES lower(?)) SELECT NOT ? FROM t")
.build();
actual = computeActual(session, "DESCRIBE INPUT my_query");
// The parameter from CTE is listed first with id=0. The parameter from the query is listed next with id=1.
expected = resultBuilder(session, BIGINT, VARCHAR)
.row(0, "varchar(0)")
.row(1, "boolean")
.build();
assertEquals(actual, expected);
}

@Test
Expand Down