Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aliased Window Expr enters unreachable code #14108

Closed
berkaysynnada opened this issue Jan 13, 2025 · 3 comments · Fixed by #14109
Closed

Aliased Window Expr enters unreachable code #14108

berkaysynnada opened this issue Jan 13, 2025 · 3 comments · Fixed by #14109
Labels
bug Something isn't working

Comments

@berkaysynnada
Copy link
Contributor

berkaysynnada commented Jan 13, 2025

Describe the bug

There is an unreachable!() line in PushDownFilter rule, where LogicalPlan::Window is being handled.

The following query enters there:

    pub(crate) const QUERY2: &str = "
    -- Define the subquery
    WITH SubQuery AS (
        SELECT
            a.column1,
            a.column2 AS ts_column,
            a.column3,
            SUM(a.column3) OVER (
                PARTITION BY a.column1
                ORDER BY a.column2 RANGE BETWEEN INTERVAL '10 minutes' PRECEDING AND CURRENT ROW
            ) AS moving_sum
        FROM source_table a
    )
    SELECT
        column1,
        ts_column,
        moving_sum
    FROM SubQuery
    WHERE moving_sum > 100
    ";

To Reproduce

    #[tokio::test]
    async fn test_window_alias_in_subquery() -> Result<()> {
        let schema = Arc::new(Schema::new(vec![
            Field::new("column1", DataType::Utf8, false),
            Field::new(
                "column2",
                DataType::Timestamp(TimeUnit::Nanosecond, None),
                false,
            ),
            Field::new("column3", DataType::Float64, false),
        ]));

        let column1 = Arc::new(StringArray::from(vec!["item1", "item2", "item1"]));
        let column2 = Arc::new(TimestampNanosecondArray::from(vec![
            1_000_000_000,
            2_000_000_000,
            3_000_000_000,
        ]));
        let column3 = Arc::new(Float64Array::from(vec![50.0, 30.0, 25.0]));
        let source_table =
            RecordBatch::try_new(schema.clone(), vec![column1, column2, column3])?;

        let mut ctx = SessionContext::new();
        ctx.register_batch("source_table", source_table)?;

        let df = ctx.sql(QUERY2).await?;
        let results = df.collect().await?;

        for batch in results {
            println!("{:?}", batch);
        }

        Ok(())
    }

Expected behavior

It should run without error.

Additional context

This query works successfully:

        SELECT
            a.column1,
            a.column2 AS ts_column,
            a.column3,
            SUM(a.column3) OVER (
                PARTITION BY a.column1
                ORDER BY a.column2 RANGE BETWEEN INTERVAL '10 minutes' PRECEDING AND CURRENT ROW
            ) AS moving_sum
        FROM source_table a

However, if it becomes a subquery, then it starts to fail.

Handling the alias expr in the if let part solves the problem, but I am not sure it is the correct way.

@berkaysynnada berkaysynnada added the bug Something isn't working label Jan 13, 2025
@berkaysynnada
Copy link
Contributor Author

BTW, this bug should have been introduced in a commit that merged in the last 10 days, but couldn't find exactly which it is.

@alamb
Copy link
Contributor

alamb commented Jan 14, 2025

BTW, this bug should have been introduced in a commit that merged in the last 10 days, but couldn't find exactly which it is.

Maybe it is filter pushdown through window functions

@berkaysynnada
Copy link
Contributor Author

BTW, this bug should have been introduced in a commit that merged in the last 10 days, but couldn't find exactly which it is.

Maybe it is filter pushdown through window functions

I suspect it too, but not have a strong evidence

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants