Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression in 25.1.0 when the write dialect is postgres and the read dialect is omitted #3647

Closed
ronnix opened this issue Jun 13, 2024 · 1 comment · Fixed by #3650
Closed
Assignees

Comments

@ronnix
Copy link

ronnix commented Jun 13, 2024

Hi, we ran into the following change of behaviour in 25.1.0.

>>> from sqlglot import parse_one

>>> parse_one("SELECT unnest(ARRAY[1, 2, 3])").sql()
'SELECT UNNEST(1, 2, 3)'

>>> parse_one("SELECT unnest(ARRAY[1, 2, 3])").sql(dialect="postgres")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ronan/.virtualenvs/insitu/lib/python3.9/site-packages/sqlglot/expressions.py", line 597, in sql
    return Dialect.get_or_raise(dialect).generate(self, **opts)
  File "/home/ronan/.virtualenvs/insitu/lib/python3.9/site-packages/sqlglot/dialects/dialect.py", line 538, in generate
    return self.generator(**opts).generate(expression, copy=copy)
  File "/home/ronan/.virtualenvs/insitu/lib/python3.9/site-packages/sqlglot/generator.py", line 623, in generate
    sql = self.sql(expression).strip()
  File "/home/ronan/.virtualenvs/insitu/lib/python3.9/site-packages/sqlglot/generator.py", line 777, in sql
    sql = transform(self, expression)
  File "/home/ronan/.virtualenvs/insitu/lib/python3.9/site-packages/sqlglot/transforms.py", line 650, in _to_sql
    return _sql_handler(expression)
  File "/home/ronan/.virtualenvs/insitu/lib/python3.9/site-packages/sqlglot/generator.py", line 2302, in select_sql
    expressions = self.expressions(expression)
  File "/home/ronan/.virtualenvs/insitu/lib/python3.9/site-packages/sqlglot/generator.py", line 3371, in expressions
    sql = self.sql(e, comment=False)
  File "/home/ronan/.virtualenvs/insitu/lib/python3.9/site-packages/sqlglot/generator.py", line 782, in sql
    sql = getattr(self, exp_handler_name)(expression)
  File "/home/ronan/.virtualenvs/insitu/lib/python3.9/site-packages/sqlglot/dialects/postgres.py", line 584, in unnest_sql
    if len(expression.expressions) == 1:
TypeError: object of type 'Array' has no len()

>>> parse_one("SELECT unnest(ARRAY[1, 2, 3])", dialect="postgres").sql(dialect="postgres")
'SELECT UNNEST(ARRAY[1, 2, 3])'

I realized that this only occurs when the read dialect is not specified, so the workaround for us was to make sure that we added the dialect parameter to parse_one(), but I thought I would file the issue in case it would be useful.

@VaggelisD VaggelisD self-assigned this Jun 13, 2024
@georgesittas
Copy link
Collaborator

georgesittas commented Jun 13, 2024

Thanks for the report, this does look like a bug.

so the workaround for us was to make sure that we added the dialect parameter to parse_one()

I'd suggest this anyway because it's more stable vs using the base dialect.

@tobymao it's because Unnest now inherits from Func, so when it's used in places other than FROM, JOIN etc we now parse it in _parse_function_call as exp.Unnest, vs exp.Anonymous which was the behavior before 6a607d3.

>>> import sqlglot
>>> sqlglot.parse_one("SELECT unnest(ARRAY[1, 2, 3])")
Select(
  expressions=[
    Unnest(
      expressions=Array(
        expressions=[
          Literal(this=1, is_string=False),
          Literal(this=2, is_string=False),
          Literal(this=3, is_string=False)]))])
>>> sqlglot.parse_one("SELECT unnest(ARRAY[1, 2, 3])", "postgres")
Select(
  expressions=[
    Explode(
      this=Array(
        expressions=[
          Literal(this=1, is_string=False),
          Literal(this=2, is_string=False),
          Literal(this=3, is_string=False)]))])

Parsing / generating with Postgres works because it produces an Explode node, not an Unnest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants