Skip to content

Regression in simplifying expressions in subqueries #3760

@andygrove

Description

@andygrove

Describe the bug
We hit a regression in Dask SQL after upgrading to 13.0.0-rc1 (dask-contrib/dask-sql#844).

Example query:

SELECT col_int32 FROM test
    WHERE col_int32 > (
      SELECT AVG(col_int32) FROM test
      WHERE col_utf8 BETWEEN '2002-05-08'
        AND (cast('2002-05-08' as date) + interval '5 days')
    )

Expected Filter

Filter: test.col_utf8 BETWEEN Utf8("2002-05-08") AND Utf8("2002-05-13")

Actual Filter

Filter: test.col_utf8 BETWEEN Utf8("2002-05-08") AND CAST(CAST(Utf8("2002-05-08") AS Date32) + IntervalDayTime("21474836480") AS Utf8)

To Reproduce
Here is a repro for the optimizer integration-test.rs:

#[test]
fn subquery_filter_with_cast() -> Result<()> {
    let sql = "SELECT col_int32 FROM test \
    WHERE col_int32 > (\
      SELECT AVG(col_int32) FROM test \
      WHERE col_utf8 BETWEEN '2002-05-08' \
        AND (cast('2002-05-08' as date) + interval '5 days')\
    )";
    let plan = test_sql(sql)?;
    let expected =
        "Projection: test.col_int32\n  Filter: CAST(test.col_int32 AS Float64) > __sq_1.__value\
        \n    CrossJoin:\n      TableScan: test projection=[col_int32]\
        \n      Projection: AVG(test.col_int32) AS __value, alias=__sq_1\
        \n        Aggregate: groupBy=[[]], aggr=[[AVG(test.col_int32)]]\
        \n          Filter: test.col_utf8 BETWEEN Utf8(\"2002-05-08\") AND Utf8(\"2002-05-13\")\
        \n            TableScan: test projection=[col_int32, col_utf8]";
    assert_eq!(expected, format!("{:?}", plan));
    Ok(())
}

Expected behavior
A clear and concise description of what you expected to happen.

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingoptimizerOptimizer rules

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions