Skip to content

[Rust] [DataFusion] Can't alias an aggregate expression #25588

Description

@asfimport

The following test (on execute) fails:

    #[test]
    fn aggregate_with_alias() -> Result<()> {
        let results = execute("SELECT c1, COUNT(c2) AS count FROM test GROUP BY c1", 4)?;

        assert_eq!(field_names(batch), vec!["c1", "count"]);
        let expected = vec!["0,10", "1,10", "2,10", "3,10"];
        let mut rows = test::format_batch(&batch);
        rows.sort();
        assert_eq!(rows, expected);
        Ok(())
    }

The root cause is that, in sql::planner, we interpret COUNT(c2) AS count as An Expr::Alias, which fails the is_aggregate_expr condition, thus being interpreted as grouped expression instead of an aggregated expression. This raises the Error

General("Projection references non-aggregate values")

The planner could interpret the statement above as two steps: an aggregation followed by a projection. Alternatively, we can allow aliases to be valid aggregation expressions.

Reporter: Jorge Leitão / @jorgecarleitao

Related issues:

PRs and other links:

Note: This issue was originally created as ARROW-9520. Please see the migration documentation for further details.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions