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

'@@' not supported when parsing with postgres dialect #2066

Closed
cmilhaupt opened this issue Aug 16, 2023 · 3 comments
Closed

'@@' not supported when parsing with postgres dialect #2066

cmilhaupt opened this issue Aug 16, 2023 · 3 comments

Comments

@cmilhaupt
Copy link

cmilhaupt commented Aug 16, 2023

I saw in #1290 that transpiling of full-text search queries isn't in the scope of sqlglot, but is parsing queries with postgres-specific full-text search tokens?

Observed bevhaior:

$ python3 --version
Python 3.11.3
$ python3 -c "import sqlglot; print(sqlglot.__version__)"
17.14.2
$ python3 -m sqlglot --read postgres --parse "SELECT title FROM pgweb WHERE to_tsvector('english', body) @@ to_tsquery('english', 'friend');"
...
sqlglot.errors.ParseError: Invalid expression / Unexpected token. Line 1, Col: 60.
  SELECT title FROM pgweb WHERE to_tsvector('english', body) @@ to_tsquery('english', 'friend');

Ideally I'd also expect to be able to

sqlglot.parse_one("SELECT title FROM pgweb WHERE author LIKE '%Smith%';").where("to_tsvector('english', body) @@ to_tsquery('english', 'friend')").sql()

or

sqlglot.parse_one("SELECT title FROM pgweb WHERE to_tsvector('english', body) @@ to_tsquery('english', 'friend')").find(exp.Where).sql(

If this isn't in scope for this project please feel free to close!

@cmilhaupt cmilhaupt changed the title '@@' not supported by postgres dialect '@@' not supported when parsing with postgres dialect Aug 16, 2023
@cmilhaupt
Copy link
Author

cmilhaupt commented Aug 18, 2023

Thanks for the quick fix @tobymao ! Looks like the first examples works but the second example is throwing this error:

>>> sqlglot.parse_one("SELECT title FROM pgweb WHERE author LIKE '%Smith%';").where("to_tsvector('english', body
) @@ to_tsquery('english', 'friend')").sql()
Traceback (most recent call last):
  File "/tmp/env/lib/python3.11/site-packages/sqlglot/parser.py", line 930, in parse_into
    return self._parse(parser, raw_tokens, sql)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/env/lib/python3.11/site-packages/sqlglot/parser.py", line 969, in _parse
    self.raise_error("Invalid expression / Unexpected token")
  File "/tmp/env/lib/python3.11/site-packages/sqlglot/parser.py", line 1010, in raise_error
    raise error
sqlglot.errors.ParseError: Invalid expression / Unexpected token. Line 1, Col: 30.
  to_tsvector('english', body) @@ to_tsquery('english', 'friend')

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

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/env/lib/python3.11/site-packages/sqlglot/expressions.py", line 3034, in where
    return _apply_conjunction_builder(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/env/lib/python3.11/site-packages/sqlglot/expressions.py", line 5055, in _apply_conjunction_builder
    node = and_(*expressions, dialect=dialect, copy=copy, **opts)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/env/lib/python3.11/site-packages/sqlglot/expressions.py", line 5421, in and_
    return t.cast(Condition, _combine(expressions, And, dialect, copy=copy, **opts))
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/env/lib/python3.11/site-packages/sqlglot/expressions.py", line 5092, in _combine
    conditions = [
                 ^
  File "/tmp/env/lib/python3.11/site-packages/sqlglot/expressions.py", line 5093, in <listcomp>
    condition(expression, dialect=dialect, copy=copy, **opts)
  File "/tmp/env/lib/python3.11/site-packages/sqlglot/expressions.py", line 5392, in condition
    return maybe_parse(
           ^^^^^^^^^^^^
  File "/tmp/env/lib/python3.11/site-packages/sqlglot/expressions.py", line 4917, in maybe_parse
    return sqlglot.parse_one(sql, read=dialect, into=into, **opts)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/env/lib/python3.11/site-packages/sqlglot/__init__.py", line 123, in parse_one
    result = dialect.parse_into(into, sql, **opts)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/env/lib/python3.11/site-packages/sqlglot/dialects/dialect.py", line 294, in parse_into
    return self.parser(**opts).parse_into(expression_type, self.tokenize(sql), sql)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/env/lib/python3.11/site-packages/sqlglot/parser.py", line 935, in parse_into
    raise ParseError(
sqlglot.errors.ParseError: Failed to parse 'to_tsvector('english', body) @@ to_tsquery('english', 'friend')' int
o <class 'sqlglot.expressions.Condition'>

and the third example this:

sqlglot.parse_one("SELECT title FROM pgweb WHERE to_tsvector('english', body) @@ to_tsquery('english', 'friend')").find(exp.Where).sql()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/env/lib/python3.11/site-packages/sqlglot/__init__.py", line 125, in parse_one
    result = dialect.parse(sql, **opts)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/env/lib/python3.11/site-packages/sqlglot/dialects/dialect.py", line 289, in parse
    return self.parser(**opts).parse(self.tokenize(sql), sql)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/env/lib/python3.11/site-packages/sqlglot/parser.py", line 900, in parse
    return self._parse(
           ^^^^^^^^^^^^
  File "/tmp/env/lib/python3.11/site-packages/sqlglot/parser.py", line 969, in _parse
    self.raise_error("Invalid expression / Unexpected token")
  File "/tmp/env/lib/python3.11/site-packages/sqlglot/parser.py", line 1010, in raise_error
    raise error
sqlglot.errors.ParseError: Invalid expression / Unexpected token. Line 1, Col: 60.
  SELECT title FROM pgweb WHERE to_tsvector('english', body) @@ to_tsquery('english', 'friend')

@tobymao
Copy link
Owner

tobymao commented Aug 18, 2023

you need to specify the dialect, .where(..., dialect="postgres")

@cmilhaupt
Copy link
Author

Yep -- my mistake. Thanks!!

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

No branches or pull requests

2 participants