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

Parsing SQL strings to Exprs wtih the qualified schema #11551

Closed
goldmedal opened this issue Jul 19, 2024 · 1 comment · Fixed by #11562
Closed

Parsing SQL strings to Exprs wtih the qualified schema #11551

goldmedal opened this issue Jul 19, 2024 · 1 comment · Fixed by #11562
Assignees
Labels
enhancement New feature or request

Comments

@goldmedal
Copy link
Contributor

Is your feature request related to a problem or challenge?

After #10995, we have parse_sql_expr() to create a logical expression from the SQL string. This API allows creating the expression with a Schema. I tried to create the expression with a qualified schema:

let sql = "a < 5 OR a = 8";
let expr = col("t.a").lt(lit(5_i64)).or(col("t.a").eq(lit(8_i64)));

// provide type information that `a` is an Int32
let schema = Schema::new(vec![Field::new("a", DataType::Int32, true)]);
let df_schema = DFSchema::try_from_qualified_schema("t", &schema).unwrap();
let ctx = SessionContext::new();
let parsed_expr = ctx.parse_sql_expr(sql, &df_schema)?;
assert_eq!(parsed_expr, expr);

I expected the identifier to be col("t.a"), but it is actually col("a").

I think the behavior can be aligned with the planning for the SQL statement:

ctx.register_batch("t", a_batch());
let df = ctx.sql("SELECT a < 5 OR a = 8 FROM t").await?.into_optimized_plan()?;
dbg!(&df);

The result would be:

[datafusion-examples/examples/parse_sql_expr.rs:89:5] &df = Projection: t.a < Int32(5) OR t.a = Int32(8) AS t.a < Int64(5) OR t.a = Int64(8)
  TableScan: t projection=[a]

All of the identifiers are qualified.

Describe the solution you'd like

I think we can find the field by qualified_field_with_unqualified_name, and pass the qualifier when planning a column at

match schema.field_with_unqualified_name(normalize_ident.as_str()) {
Ok(_) => {
// found a match without a qualified name, this is a inner table column
Ok(Expr::Column(Column {
relation: None,
name: normalize_ident,
}))
}

Describe alternatives you've considered

No response

Additional context

No response

@Lordworms
Copy link
Contributor

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants