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

Scalar subquery can fail to reference unqualified field from outer query #2415

Closed
Tracked by #474
andygrove opened this issue May 2, 2022 · 0 comments · Fixed by #2416
Closed
Tracked by #474

Scalar subquery can fail to reference unqualified field from outer query #2415

andygrove opened this issue May 2, 2022 · 0 comments · Fixed by #2416
Assignees
Labels
bug Something isn't working

Comments

@andygrove
Copy link
Member

Describe the bug

The following query fails with SchemaError(FieldNotFound { qualifier: None, name: "j2_id", valid_fields: Some(["j1.j1_id", "j1.j1_string", "j3.j3_id", "j3.j3_string", "j1.j1_id", "j1.j1_string", "j3.j3_id", "j3.j3_string"]) }

SELECT j1_string, j2_string 
        FROM j1, j2 
        WHERE j1_id = j2_id - 1 
        AND j2_id < (SELECT count(*) 
            FROM j1, j3 
            WHERE j2_id = j1_id 
            AND j1_id = j3_id)

To Reproduce

Add this test to sql/planner.rs:

#[test]
fn scalar_subquery_reference_outer_field() {
    let sql = "SELECT j1_string, j2_string \
    FROM j1, j2 \
    WHERE j1_id = j2_id - 1 \
    AND j2_id < (SELECT count(*) \
        FROM j1, j3 \
        WHERE j2_id = j1_id \
        AND j1_id = j3_id)";

    let expected = "TBD";

    quick_test(sql, &expected);
}

Also, add these tables to MockContextProvider in the same file.

"j1" => Some(Schema::new(vec![
    Field::new("j1_id", DataType::Int32, false),
    Field::new("j1_string", DataType::Utf8, false),
])),
"j2" => Some(Schema::new(vec![
    Field::new("j2_id", DataType::Int32, false),
    Field::new("j2_string", DataType::Utf8, false),
])),
"j3" => Some(Schema::new(vec![
    Field::new("j3_id", DataType::Int32, false),
    Field::new("j3_string", DataType::Utf8, false),
])),

Expected behavior
Query should produce a valid plan.

Additional context
None

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.

1 participant