Yibo Dong created SPARK-62275:
---------------------------------

             Summary: MEASURE silently drops its aggregate FILTER predicate
                 Key: SPARK-62275
                 URL: https://issues.apache.org/jira/browse/SPARK-62275
             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

Applying the standard aggregate {{FILTER}} clause to a metric-view measure 
silently returns the unfiltered measure. The equivalent ordinary aggregate 
returns 3 for {{g = 1}}, but {{measure(sum_v) FILTER (WHERE g = 1)}} returns 
the total 7.

h2. How to reproduce

{code:sql}
DROP VIEW IF EXISTS metric_filter_view;
DROP TABLE IF EXISTS metric_filter_base;

CREATE TABLE metric_filter_base (g INT, v INT) USING parquet;
INSERT INTO metric_filter_base VALUES (1, 1), (1, 2), (2, 4);

CREATE VIEW metric_filter_view
WITH METRICS
LANGUAGE YAML
AS $$
version: "0.1"
source: metric_filter_base
dimensions:
  - name: g
    expr: g
measures:
  - name: sum_v
    expr: sum(v)
$$;

SELECT sum(v) FILTER (WHERE g = 1) AS s
FROM metric_filter_base;

SELECT measure(sum_v) AS s
FROM metric_filter_view;

SELECT measure(sum_v) FILTER (WHERE g = 1) AS s
FROM metric_filter_view;
{code}

Save the SQL as {{tmp.sql}} and run:

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

h2. Expected result

The rows satisfying {{g = 1}} have values 1 and 2, so the first and third 
queries should both return:

{noformat}
3
{noformat}

The unfiltered metric-view control should return 7.

h2. Actual result

The ordinary filtered aggregate returns 3 and the unfiltered metric measure 
returns 7. The filtered metric measure also returns 7:

{noformat}
7
{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