trinodb / trino

Official repository of Trino, the distributed SQL query engine for big data, formerly known as PrestoSQL (https://trino.io)
https://trino.io
Apache License 2.0
10.27k stars 2.95k forks source link

Invalid argument in `reduce_agg` throws null pointer exception #15768

Open conor-mcavoy opened 1 year ago

conor-mcavoy commented 1 year ago

If you attempt to reference a column in one of the lambda arguments to reduce_agg and that column isn't the input to reduce_agg (i.e. the first argument), Trino will throw a null pointer exception.

> create table lambda_cap_test (col1 int, col2 int);
> select reduce_agg(col1, 0, (a, b) -> col2, (a, b) -> 0) from lambda_cap_test;
Query 20230118_205314_00004_dsgu5 failed: Internal error
java.lang.NullPointerException
    at java.base/java.util.Objects.requireNonNull(Objects.java:208)
    at java.base/java.util.Optional.of(Optional.java:113)
    at io.trino.sql.planner.TranslationMap.getSymbolForColumn(TranslationMap.java:1116)
    at io.trino.sql.planner.TranslationMap$1.rewriteIdentifier(TranslationMap.java:267)
    at io.trino.sql.planner.TranslationMap$1.rewriteIdentifier(TranslationMap.java:227)
    at io.trino.sql.tree.ExpressionTreeRewriter$RewritingVisitor.visitIdentifier(ExpressionTreeRewriter.java:809)
    at io.trino.sql.tree.ExpressionTreeRewriter$RewritingVisitor.visitIdentifier(ExpressionTreeRewriter.java:70)
    at io.trino.sql.tree.Identifier.accept(Identifier.java:91)
    at io.trino.sql.tree.AstVisitor.process(AstVisitor.java:27)
...

There should be a more informative error message.

Note that if you group over this column, you will get an IllegalArgumentException instead, which is more helpful.

select reduce_agg(col1, 0, (a, b) -> a + col2, (a, b) -> 0) from lambda_cap_test group by col2;
Query 20230118_204201_00000_dsgu5 failed: argument must be symbol or lambda expression: BindExpression
conor-mcavoy commented 1 year ago

Discussed here https://trinodb.slack.com/archives/CP1MUNEUX/p1674075157507339