linkedin / coral

Coral is a translation, analysis, and query rewrite engine for SQL and other relational languages.
BSD 2-Clause "Simplified" License
783 stars 184 forks source link

[WIP] POC for renaming table names #443

Closed aastha25 closed 6 months ago

aastha25 commented 1 year ago

What changes are proposed in this pull request, and why are they necessary?

During translations, Coral generates SQL where each table name is appended with an alias and this alias is used to reference columns from the table. For example:

source SQL:
SELECT * FROM foo
Translated Trino SQL:
SELECT * FROM default.foo AS foo

source SQL:
SELECT a FROM foo UNION ALL SELECT a FROM foo
Translated Trino SQL:
SELECT foo.a FROM default.foo AS foo UNION ALL SELECT foo0.a FROM default.foo AS foo0

With this information, altering the table name can be accomplished using a post-processing SqlShuttle. This involves: (1) Identifying the tables accessed in the RelNode via RelOptUtil.findAllTables() (2) Using SqlShuttle RewriteTableNameShuttle to replace a single reference to the original table name.

The new translation are:

source SQL:
SELECT * FROM foo
Translated Trino SQL:
SELECT * FROM default.foo_tmp AS foo

source SQL:
SELECT a FROM foo UNION ALL SELECT a FROM foo
Translated Trino SQL:
SELECT foo.a FROM default.foo_tmp AS foo UNION ALL SELECT foo0.a FROM default.foo_tmp AS foo0

How was this patch tested?

NA