Yang Jie created SPARK-62351:
--------------------------------

             Summary: GROUP BY a non-deterministic scalar subquery or aggregate 
function fails with an internal error
                 Key: SPARK-62351
                 URL: https://issues.apache.org/jira/browse/SPARK-62351
             Project: Spark
          Issue Type: Bug
          Components: SQL
    Affects Versions: 5.0.0
            Reporter: Yang Jie


`PullOutNondeterministic` moves a non-deterministic expression out of an 
operator only when `NondeterministicExpressionCollection` collects it, and that 
collects only `Nondeterministic`, `UserDefinedExpression` and 
`ExternalUserDefinedFunction`. Some non-deterministic expressions are left 
where they are, and the queries below fail with an internal error.

1. A non-deterministic scalar subquery, whose `deterministic` includes 
`plan.deterministic`. In GROUP BY, the `Aggregate` case of the rule throws:

{code:sql}
SELECT count(*) FROM VALUES (1), (2) AS a(x) GROUP BY (SELECT rand(1));
-- [INTERNAL_ERROR] Non-deterministic expression ... should not appear in 
grouping expression.
{code}

In ORDER BY, {{SELECT * FROM VALUES (1), (2) AS a(x) ORDER BY (SELECT 
rand(1))}} fails with INVALID_NON_DETERMINISTIC_EXPRESSIONS, while {{ORDER BY 
rand(1)}} works. In tests, it fails earlier with _LEGACY_ERROR_TEMP_2172, 
because the rule adds a Project on each run and the Once batch 
"Nondeterministic" is not idempotent (see also SPARK-28236).

2. An aggregate function in GROUP BY, which is not allowed. For a deterministic 
function, {{SELECT f(id) FROM t GROUP BY f(id)}} fails with GROUP_BY_AGGREGATE. 
For {{udaf(...).asNondeterministic()}}, the rule replaces the UDAF under its 
AggregateExpression with an attribute, and the query fails with 
{{ClassCastException: AttributeReference cannot be cast to AggregateFunction}}.

Both fail the same way on 3.5.5, 4.0.2, 4.1.2 and 4.2.0 (case 2 checked on 
4.2.0 only). Since SPARK-62133, an aggregate function from a FunctionCatalog 
whose bound function is not deterministic takes the same paths: a scalar 
subquery that calls it in GROUP BY fails as in case 1, and the function itself 
in GROUP BY fails with the INTERNAL_ERROR of case 1 instead of 
GROUP_BY_AGGREGATE.




--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to