JasonLi-cn commented on PR #12184:
URL: https://github.com/apache/datafusion/pull/12184#issuecomment-2311677644

   > > > The plan looks like aliasing to the same name after 
`optimize_projections`
   > > > ```
   > > >     Projection: test.a AS test.a
   > > >       TableScan: test projection=[a]
   > > > ```
   > > > 
   > > > 
   > > >     
   > > >       
   > > >     
   > > > 
   > > >       
   > > >     
   > > > 
   > > >     
   > > >   
   > > > I wonder could we even remove the alias here, since the name is the 
same
   > > 
   > > 
   > > Removing the Alias will cause a schema change. 🤔
   > 
   > That is why I think the root cause might be the incorrect schema we have.
   > 
   > `SELECT "test.a" FROM (SELECT a AS "test.a" FROM test)`
   > 
   > The expected schema should be something like.
   > 
   > ```
   > DFSchema { inner: Schema { fields: [Field { name: "test.a", data_type: 
Int32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }], 
metadata: {} }, field_qualifiers: [Some(Bare { table: "test" })], 
functional_dependencies: FunctionalDependencies { deps: [] } }.
   > ```
   > 
   > a is aliased to "test.a". The column should be `test."test.a"`
   > 
   > Upd:
   > 
   > ```
   > query I
   > SELECT a FROM (SELECT a FROM test)
   > ----
   > 1
   > ```
   > 
   > This is the schema for the query without alias
   > 
   > ```
   > schema: DFSchema { inner: Schema { fields: [Field { name: "a", data_type: 
Int32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }], 
metadata: {} }, field_qualifiers: [Some(Bare { table: "test" })], 
functional_dependencies: FunctionalDependencies { deps: [] } }
   > ```
   > 
   > ```
   > query I
   > SELECT b FROM (SELECT a as b FROM test)
   > ----
   > 1
   > ```
   > 
   > This is the schema with alias
   > 
   > ```
   > schema: DFSchema { inner: Schema { fields: [Field { name: "b", data_type: 
Int32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }], 
metadata: {} }, field_qualifiers: [None], functional_dependencies: 
FunctionalDependencies { deps: [] } }
   > ```
   > 
   > `field_qualifiers` is empty for alias case. Does that mean we didn't 
preserve `field_qualifiers` for alias. 🤔
   > 
   > ```
   > field_qualifiers: [Some(Bare { table: "test" })]
   > ```
   
   Yes. Possibly because the `b` field itself is not part of the `test` table? 
🤔 
   If we make the following changes to the SQL, the `b` field has 
field_qualifiers: `[Some(Bare { table: "t0" })]`
   
   ```
   query I
   SELECT b FROM (SELECT a as b FROM test) t0
   ----
   1
   ```
   
   ```
   schema: DFSchema { inner: Schema { fields: [Field { name: "b", data_type: 
Int32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }], 
metadata: {} }, field_qualifiers: [Some(Bare { table: "t0" })], 
functional_dependencies: FunctionalDependencies { deps: [] } }
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to