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

Parameter $1 for postgres is not supported #2931

Closed
eric-zhu opened this issue Feb 8, 2024 · 0 comments · Fixed by #2935
Closed

Parameter $1 for postgres is not supported #2931

eric-zhu opened this issue Feb 8, 2024 · 0 comments · Fixed by #2935
Assignees

Comments

@eric-zhu
Copy link

eric-zhu commented Feb 8, 2024

In version v18.8.0, heredoc strings support was added, and "$": TokenType.PARAMETER was replaced with "$": TokenType.HEREDOC_STRING. Since then, sql query with a parameter like $1 cannot be parsed.

Here is the original PR Feat!: add support for heredoc strings (Postgres, ClickHouse) #2328

Fully reproducible code snippet

import sqlglot
from sqlglot.optimizer.annotate_types import annotate_types
from sqlglot.optimizer.qualify import qualify

schema = {"t": {"x": "text"}}
sql = "select x from t where $1::text = 'ok'"
expression = sqlglot.parse_one(sql, dialect="postgres")
print(expression.sql(dialect="postgres"))
qualified_expr = qualify(expression, schema=schema, dialect="postgres")
annotated_expr = annotate_types(qualified_expr, schema=schema)
print(annotated_expr.selects[0].type)

Exception was raised. Expected output is TEXT.

Traceback (most recent call last):
  File "/Users/eric/venv/lib/python3.10/site-packages/sqlglot/tokens.py", line 836, in tokenize
    self._scan()
  File "/Users/eric/venv/lib/python3.10/site-packages/sqlglot/tokens.py", line 859, in _scan
    self._scan_keywords()
  File "/Users/eric/venv/lib/python3.10/site-packages/sqlglot/tokens.py", line 994, in _scan_keywords
    if self._scan_string(word):
  File "/Users/eric/venv/lib/python3.10/site-packages/sqlglot/tokens.py", line 1136, in _scan_string
    tag = "" if self._char == end else self._extract_string(end)
  File "/Users/eric/venv/lib/python3.10/site-packages/sqlglot/tokens.py", line 1203, in _extract_string
    raise TokenError(f"Missing {delimiter} from {self._line}:{self._start}")
sqlglot.errors.TokenError: Missing $ from 1:22

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/eric/tmp/test.py", line 20, in <module>
    expression = sqlglot.parse_one(sql, dialect="postgres")
  File "/Users/eric/venv/lib/python3.10/site-packages/sqlglot/__init__.py", line 125, in parse_one
    result = dialect.parse(sql, **opts)
  File "/Users/eric/venv/lib/python3.10/site-packages/sqlglot/dialects/dialect.py", line 304, in parse
    return self.parser(**opts).parse(self.tokenize(sql), sql)
  File "/Users/eric/venv/lib/python3.10/site-packages/sqlglot/dialects/dialect.py", line 318, in tokenize
    return self.tokenizer.tokenize(sql)
  File "/Users/eric/venv/lib/python3.10/site-packages/sqlglot/tokens.py", line 841, in tokenize
    raise TokenError(f"Error tokenizing '{context}'") from e
sqlglot.errors.TokenError: Error tokenizing 'select x from t where $1::text = 'ok'
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.

2 participants