Skip to content

Add support for named window frames #6125

@mustafasrepo

Description

@mustafasrepo

Is your feature request related to a problem or challenge?

Currently we do not support named window frames. The query below can run successfully,

SELECT SUM(amount) OVER(ORDER BY ts) as sum1
                        FROM sales_us
                        ORDER BY ts
                        LIMIT 5;

However, equivalent query below with named window frame cannot be run.

SELECT SUM(amount) OVER running_window
                     FROM sales_us
                     WINDOW running_window AS (ORDER BY ts)
                     ORDER BY ts
                     LIMIT 5;

Postgres supports both versions.

Describe the solution you'd like

No response

Describe alternatives you've considered

No response

Additional context

To reproduce one can run test below

#[tokio::test]
async fn test_named_window() -> Result<()> {
    let config = SessionConfig::new()
        .with_target_partitions(1);
    let ctx = SessionContext::with_config(config);
    ctx.sql("CREATE TABLE sales_us (
          ts TIMESTAMP,
          currency VARCHAR(3),
          amount INT
        ) as VALUES
              ('2022-01-01 10:00:00'::timestamp, 'USD', 100.00),
              ('2022-01-01 11:00:00'::timestamp, 'USD', 200.00),
              ('2022-01-02 09:00:00'::timestamp, 'USD', 300.00),
              ('2022-01-02 10:00:00'::timestamp, 'USD', 150.00)").await?;

    // Below query cn run successfully

    // let sql ="SELECT SUM(amount) OVER(ORDER BY ts) as sum1
    //                 FROM sales_us
    //                 ORDER BY ts
    //                 LIMIT 5";

    // Below query produces error, whereas it runs in Postgre

    let sql ="SELECT SUM(amount) OVER running_window
                 FROM sales_us
                 WINDOW running_window AS (ORDER BY ts)
                 ORDER BY ts
                 LIMIT 5;";

    let msg = format!("Creating logical plan for '{sql}'");
    let dataframe: DataFrame = ctx.sql(sql).await.expect(&msg);
    let physical_plan = dataframe.create_physical_plan().await?;
    let batches = collect(physical_plan, ctx.task_ctx()).await?;
    print_batches(&batches)?;
    Ok(())
}

No response

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