-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Check type of filter expression at statement analysis #18460
Conversation
@@ -7043,6 +7043,14 @@ public void testAlterTableAddRowField() | |||
.hasMessage("line 1:1: Adding fields with COMMENT is unsupported"); | |||
} | |||
|
|||
@Test | |||
public void testNNonBooleanFilterExpression() |
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.
I would move to the above testInvalidAggregationFilter
method.
public void testNNonBooleanFilterExpression() | |
public void testNonBooleanFilterExpression() |
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.
Merged this case into testInvalidAggregationFilter
.
490776e
to
7685f58
Compare
if (!type.equals(BOOLEAN)) { | ||
throw semanticException(TYPE_MISMATCH, expression, "Filter expression must evaluate to a boolean: actual type %s", type); | ||
} |
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.
This breaks queries with ... FILTER (WHERE <expr>)
where <expr>
is known to evaluate to NULL
at analysis time. E.g.,
SELECT count(*) FILTER (WHERE NULL) FROM (VALUES 1,2,3) t(x)
Take a look at analyzeWhere
in SemanticAnalyzer
for an example of how to handle this check. Also, please add a test for that case.
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.
@martint NULL literal is not allowed in FILTER clause. FILTER (WHERE NULL)
fails even without this PR.
trino/core/trino-main/src/main/java/io/trino/sql/planner/plan/FilterNode.java
Lines 46 to 48 in 67327de
// The condition doesn't guarantee that predicate is of type boolean, but was found to be a practical way to identify | |
// places where FilterNode was created without appropriate coercions. | |
checkArgument(!(predicate instanceof NullLiteral), "Predicate must be an expression of boolean type: %s", predicate); |
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.
FilterNode is an IR operation to represent the application of a filter, and is unrelated to the FILTER clause in an aggregation.
What’s not allowed is for the IR to contain a NullLiteral without an intervening cast that indicates what type it’s supposed to be, since that information is not included in the NullLiteral class.
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.
It does work, BTW. What query did you try?
trino> SELECT count(*) FILTER (WHERE NULL) FROM (VALUES 1,2,3) t(x);
_col0
-------
0
(1 row)
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.
is unrelated to the FILTER clause in an aggregation
Ah, I see. Thanks. I had only tested SELECT count(*) FILTER (WHERE NULL) FROM t1
.
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.
Updated the PR to allow UNKNOWN
type.
7685f58
to
f843c04
Compare
@@ -4032,6 +4032,12 @@ public void testInValidJoinOnClause() | |||
.hasMessage("line 1:69: JOIN ON clause must evaluate to a boolean: actual type row(boolean, boolean)"); | |||
} | |||
|
|||
@Test | |||
public void testValidAggregationFilter() |
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.
This name is too vague. Change it to testNullAggregationFilter
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.
Fixed.
f843c04
to
1ee04ba
Compare
Description
Avoid INTERNAL ERROR (COMPILER_ERROR) when non boolean expression is given as FILTER predicate. With this fix, the error will be USER ERROR (TYPE_MISMATCH).
Additional context and related issues
Release notes
(x) This is not user-visible or docs only and no release notes are required.