Skip to content

RFC: add a field for Expr::Alias #6681

@jackwener

Description

@jackwener

Is your feature request related to a problem or challenge?

In the current optimization of Datafusion, we often use Expr::Alias instances for purposes:

  1. make the parent plan to correctly access the corresponding column.
  2. ensure that the column name remains unchanged.
  3. prevent changes to the schema.

However, there is a vulnerability in this point 3. When an alias contains a qualifier, the schema cannot remain unchanged.

Here's an example:

Original Expr: column: t.c1 (field: qualifier: t, name: c1)
New Expr: c2 as t.c1 (its field will be: field: qualifier: None, name: t.c1)

Describe the solution you'd like

I try to match Expr::Alias and handle it in to_field().

such as

    fn to_field(&self, input_schema: &DFSchema) -> Result<DFField> {
        match self {
            Expr::Alias(e, alias) if alias.contains('.') => {
                let index = alias.rfind('.').unwrap();
                let qualifier = alias[..index].to_string();
                let name = alias[index + 1..].to_string();
                Ok(DFField::new(
                    Some(qualifier),
                    &name,
                    e.get_type(input_schema)?,
                    e.nullable(input_schema)?,
                ))
            }
            Expr::Column(c) => Ok(DFField::new(
                c.relation.clone(),
                &c.name,
                self.get_type(input_schema)?,
                self.nullable(input_schema)?,
            )),

but I failed, because alias can be complex, we hard to parse it and separate qualifier and name.
such as above code will fail in expr like alias is Cast(t1.a as int)

So, I proposal to add a DFField in Expr::Alias, so we will get DFField of Expr::Alias directly

Describe alternatives you've considered

No response

Additional context

I find it in this PR #6595 (comment)

Metadata

Metadata

Assignees

No one assigned

    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