klahnakoski / mo-sql-parsing

Let's make a SQL parser so we can provide a familiar interface to non-sql datastores!
Mozilla Public License 2.0
255 stars 58 forks source link

PostgreSQL POSIX regular expressions raise ParseException #248

Closed a1ea321 closed 1 week ago

a1ea321 commented 1 month ago
from mo_sql_parsing import parse as p
p("select * from my_table where my_col ~ 'regex'")

This raises a ParseException.

pip show mo-sql-parsing says: Version: 10.652.24214

There's also the ~* variant: https://www.postgresql.org/docs/current/functions-matching.html#FUNCTIONS-POSIX-REGEXP

These are operators so the problem is actually more generic, since it is possible to define new operators: https://www.postgresql.org/docs/current/sql-createoperator.html

For instance this works on PostgreSQL 16:

CREATE FUNCTION DELETEME(INT, INT) RETURNS BOOLEAN AS $$ SELECT TRUE $$ LANGUAGE SQL;
CREATE OPERATOR =*///+-= (LEFTARG = int, RIGHTARG = int, FUNCTION = deleteme);
SELECT 42 WHERE 1=*///+-=2;

It returns 42 as expected.

I don't care whether your library supports arbitrary operators because I never define any. I suppose most people will be like me. I just wanted to bring it to your attention.

klahnakoski commented 1 week ago

The regex ops are exposing a bug in the parser generator; it is not jumping to the correct pattern

klahnakoski commented 1 week ago

I was wrong, parser generator is fine. Just-a-bug on my part.

https://github.com/klahnakoski/mo-sql-parsing/pull/251/files

klahnakoski commented 1 week ago

https://pypi.org/project/mo-sql-parsing/10.656.24252/

klahnakoski commented 1 week ago

https://github.com/klahnakoski/mo-sql-parsing/issues/252

klahnakoski commented 1 week ago

@a1ea321 thanks again for this bug report. I got stumped by a dumb bug: Another NOT_REGEXP was hiding in the keyword.py file. I am going to sort that file to better prevent that type of mistake.