You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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'
The text was updated successfully, but these errors were encountered:
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
Exception was raised. Expected output is
TEXT
.The text was updated successfully, but these errors were encountered: