Yibo Dong created SPARK-62284:
---------------------------------

             Summary: LATERAL VIEW between a metric view and its aggregate 
causes an internal planning error
                 Key: SPARK-62284
                 URL: https://issues.apache.org/jira/browse/SPARK-62284
             Project: Spark
          Issue Type: Bug
          Components: SQL
    Affects Versions: 5.0.0
         Environment: Stock Apache Spark 5.0.0-SNAPSHOT at commit 
082cf160890a0a0a748f70ffb5a993144b0de079, Java 25.0.1, macOS 15.7.4 aarch64, 
local[2], in-memory session catalog, built-in Parquet data source (V1), default 
SQL configuration.
            Reporter: Yibo Dong


h2. What happened

A query using MEASURE() over a metric view fails with an internal planning 
error when a LATERAL VIEW generator appears between the metric view and its 
aggregate.

The equivalent aggregation over the source table works, as does a query that 
aggregates the metric view before applying the generator. However, placing the 
generator before the metric aggregation leaves the unevaluable MEASURE function 
in the plan and causes physical planning to fail.

h2. How to reproduce

Run the following SQL in a fresh Spark SQL session:

{code:sql}
DROP VIEW IF EXISTS metric_generator_view;
DROP TABLE IF EXISTS metric_generator_base;

CREATE TABLE metric_generator_base (g INT, v INT) USING parquet;
INSERT INTO metric_generator_base VALUES (1, 10), (1, 20), (2, 5);

CREATE VIEW metric_generator_view
WITH METRICS
LANGUAGE YAML
AS $$
version: "0.1"
source: metric_generator_base
dimensions:

name: g
expr: g
measures:

name: sum_v
expr: sum(v)
$$;

-- Control 1: equivalent aggregation over the source table.
SELECT x, sum(v) AS s
FROM metric_generator_base
LATERAL VIEW explode(array(g, g + 10)) e AS x
GROUP BY x
ORDER BY x;

-- Control 2: aggregate the metric view before the generator.
WITH aggregated AS (
SELECT g, measure(sum_v) AS s
FROM metric_generator_view
GROUP BY g
)
SELECT x, s
FROM aggregated
LATERAL VIEW explode(array(g, g + 10)) e AS x
ORDER BY x;

-- Failing query: generator before metric aggregation.
SELECT x, measure(sum_v) AS s
FROM metric_generator_view
LATERAL VIEW explode(array(g, g + 10)) e AS x
GROUP BY x
ORDER BY x;
{code}

Save as tmp.sql and execute:

{code:bash}
spark-sql --master 'local[2]' --conf spark.sql.catalogImplementation=in-memory 
-f tmp.sql
{code}

h2. Expected result

For the given data, all three queries should return:

{noformat}
x  | s
---+---
1  | 30
2  | 5
11 | 30
12 | 5
{noformat}

Alternatively, if a generator between a metric view and its aggregation is 
unsupported, Spark should reject the query with an appropriate analysis error 
rather than fail internally during physical planning.

h2. Actual result

Both control queries return the expected rows.

The final query fails during physical planning:

{noformat}
org.apache.spark.SparkException:
[INTERNAL_ERROR] UnevaluableAggregateFunc.aggBufferAttributes
should not be called. SQLSTATE: XX000

at 
org.apache.spark.sql.catalyst.expressions.UnevaluableAggregateFunc.aggBufferAttributes(PythonUDF.scala:498)
at 
org.apache.spark.sql.execution.aggregate.AggUtils$.$anonfun$planAggregateWithoutDistinct$6(AggUtils.scala:173)
at 
org.apache.spark.sql.execution.aggregate.AggUtils$.planAggregateWithoutDistinct(AggUtils.scala:173)
at 
org.apache.spark.sql.execution.SparkStrategies$Aggregation$.apply(SparkStrategies.scala:658)
{noformat}



--
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