Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion sqlglot/dialects/exasol.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
build_date_delta,
)
from sqlglot.generator import unsupported_args
from sqlglot.helper import seq_get
from sqlglot.helper import seq_get, find_new_name
from sqlglot.tokens import TokenType
from sqlglot.optimizer.scope import build_scope

if t.TYPE_CHECKING:
from sqlglot.dialects.dialect import DialectType
Expand Down Expand Up @@ -167,6 +168,55 @@ def _substring_index_sql(self: Exasol.Generator, expression: exp.SubstringIndex)
return self.func("SUBSTR", haystack_sql, direction, length)


def _qualify_unscoped_star(node: exp.Expression) -> exp.Expression:
"""
Exasol doesn't support a bare * alongside other select items, so we rewrite it
Rewrite: SELECT *, <other> FROM <Table>
Into: SELECT T.*, <other> FROM <Table> AS T
"""

if not isinstance(node, exp.Select):
return node
items = list(node.expressions or [])

has_bare_star = any(isinstance(x, exp.Star) and x.this is None for x in items)

if not has_bare_star or len(items) <= 1:
return node

from_ = node.args.get("from_")

if not from_ or not isinstance(from_.this, exp.Table):
return node

table = from_.this
alias_node = table.args.get("alias")
scope = build_scope(node)
if not scope:
return node

if alias_node and alias_node.name:
alias_name = alias_node.name

else:
taken_source_name = set(scope.sources)
alias_name = find_new_name(taken_source_name, "T")
table.set("alias", exp.TableAlias(this=exp.to_identifier(alias_name, quoted=False)))

qualified_items: list[exp.Expression] = []

for it in items:
if isinstance(it, exp.Star) and it.this is None:
qualified_items.append(
exp.Column(this=exp.Star(), table=exp.to_identifier(alias_name, quoted=False))
)
else:
qualified_items.append(it)
node.set("expressions", qualified_items)

return node


DATE_UNITS = {"DAY", "WEEK", "MONTH", "YEAR", "HOUR", "MINUTE", "SECOND"}


Expand Down Expand Up @@ -425,6 +475,7 @@ def datatype_sql(self, expression: exp.DataType) -> str:
exp.CommentColumnConstraint: lambda self, e: f"COMMENT IS {self.sql(e, 'this')}",
exp.Select: transforms.preprocess(
[
_qualify_unscoped_star,
_add_local_prefix_for_aliases,
]
),
Expand Down
4 changes: 4 additions & 0 deletions tests/dialects/test_exasol.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def test_exasol(self):
"SELECT 1 AS [x]",
'SELECT 1 AS "x"',
)
self.validate_identity(
"SELECT *, 1 FROM TEST",
"SELECT T.*, 1 FROM TEST AS T",
)

def test_type_mappings(self):
self.validate_identity("CAST(x AS BLOB)", "CAST(x AS VARCHAR)")
Expand Down