Skip to content

Support for aggregate function with filter #272

Closed

Description

@jimexist

Is your feature request related to a problem or challenge? Please describe what you are trying to do.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
(This section helps Arrow developers understand the context and why for this feature, in addition to the what)

A new challenge, to support aggregate function with filter. See Postgres manual

Describe the solution you'd like
A clear and concise description of what you want to happen.

Exactly like in the manual

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

Activity

  1. Dandandan commented on May 5, 2021

    @Dandandan
    Contributor

    That would be a valuable feature @jimexist

  2. alamb commented on May 6, 2021

    @alamb
    Contributor

    @jimexist if you are proposing supporting this kind of syntax, then I think it is a reasonable idea.

    SELECT
        count(*) AS unfiltered,
        count(*) FILTER (WHERE i < 5) AS filtered
    FROM generate_series(1,10) AS s(i);
     unfiltered | filtered
    ------------+----------
             10 |        4
    (1 row)
    

    For anyone else who might read this ticket, You can express the same type of query using SQL that data fusion already supports via CASE, though it is much less beautiful. Something like:

    $ seq 1 10 > /tmp/foo.csv
    $ cargo run -p datafusion-cli
    > CREATE EXTERNAL TABLE t(i double) STORED AS CSV LOCATION '/tmp/foo.csv';
    
    > select * from t;
    +----+
    | i  |
    +----+
    | 1  |
    | 2  |
    | 3  |
    | 4  |
    | 5  |
    | 6  |
    | 7  |
    | 8  |
    | 9  |
    | 10 |
    +----+
    
    >    select count(*) as unfiltered, sum(case when i < 5 then 1 else 0 end) as filtered from t;
    +------------+----------+
    | unfiltered | filtered |
    +------------+----------+
    | 10         | 4        |
    +------------+----------+
    
  3. Dandandan commented on May 6, 2021

    @Dandandan
    Contributor

    @alamb yes, that's a great one - isn't that only feasible with SUM/MAX/MIN? I think primary use case would be COUNT or AVG

    Edit: yeah or simulating count using SUM, as I am seeing in your example. You could do something in the same way for AVG of course if you are feeling creative :D

  4. kawadakk commented on May 30, 2024

    @kawadakk
    Contributor

    This feature has been implemented by #5868. Note that (afaik) you still need to select a SQL dialect supporting it.

  5. jayzhan211 commented on May 30, 2024

    @jayzhan211
    Contributor

    This feature has been implemented by #5868. Note that (afaik) you still need to select a SQL dialect supporting it.

    It seems we can close this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions