fix(postgres): preserve contained-by operator direction CODEX#7768
fix(postgres): preserve contained-by operator direction CODEX#7768fpatz wants to merge 2 commits into
Conversation
|
Why is this necessary? We already handle |
|
The PR is mostly about parse/render round-trip fidelity, which i stumbled over in my specific use case for SQLGlot. For stock Postgres This is a contrived example: CREATE FUNCTION lt_(int, int) RETURNS bool
AS $$ SELECT $1 < $2 $$ LANGUAGE sql IMMUTABLE;
CREATE OPERATOR <@ (LEFTARG = int, RIGHTARG = int, FUNCTION = lt_);
CREATE OPERATOR @> (LEFTARG = int, RIGHTARG = int, FUNCTION = lt_);
SELECT 2 <@ 5 AS original; -- lt_(2,5) => true
SELECT 5 @> 2 AS rewritten; -- lt_(5,2) => falseI'd admit, though, that operator commutation is therefore potentially incorrec with Pg for all operators where SQLGlot commutes (I didn't check). And making them all order-preserving maybe isn't worth the extra code. I'll close the PR if you deem it not helpful. |
georgesittas
left a comment
There was a problem hiding this comment.
Fair enough, I think the motivation is reasonable, then. It's not like this is an established pattern elsewhere, anyway. My guess is that this was done to cut corners.
Just a nit that I'll clean up myself, but LGTM otherwise. Thank you!
Summary
Preserve
<@as a distinct contained-by expression instead of commuting it into reversed@>.Previously, Postgres and DuckDB parsed
<@successfully, but SQL generation normalized it by reversing the operands and rendering@>. This caused round-trip/render mismatches for operator-sensitive checks. This PR keeps the original operator direction in the AST and generated SQL.Changes
ArrayContainedByexpression for<@TokenType.LT_ATintoArrayContainedByinstead of reversedArrayContainsAllArrayContainedByas<@<@round-trips directlyVerification
python -m unittest tests.dialects.test_postgres.TestPostgres.test_postgres tests.dialects.test_duckdb.TestDuckDB.test_duckdbuv run ruff check sqlglot/expressions/array.py sqlglot/parser.py sqlglot/generator.py sqlglot/generators/mysql.py tests/dialects/test_postgres.py tests/dialects/test_duckdb.pyContributor's Note
I am working through Postgres support gaps in small, focused PRs (the previous one was #7766).