Skip to content

Parsing SQL strings to Exprs wtih the qualified schema #11551

@goldmedal

Description

@goldmedal

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

Metadata

Metadata

Assignees

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