Repository navigation
fix(deparser): support ALTER STATISTICS RENAME and quote/validate operator names #370
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
Open
pyramation
wants to merge
1
commit into
main
Choose a base branch
from
devin/1792901466-deparser-statistics-operators
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| ALTER STATISTICS ab1_a_b_stats RENAME TO ab1_a_b_stats_new; | ||
| ALTER STATISTICS "My Schema"."My Stats" RENAME TO "New Stats"; | ||
| ALTER STATISTICS public.s1 RENAME TO s2; | ||
| ALTER STATISTICS s1 SET SCHEMA s2; | ||
| ALTER STATISTICS s1 OWNER TO r1; | ||
| ALTER STATISTICS ab1_a_b_stats SET STATISTICS 0; | ||
| ALTER STATISTICS IF EXISTS ab1_a_b_stats_new SET STATISTICS -1; | ||
| ALTER STATISTICS s1 SET STATISTICS DEFAULT; | ||
| ALTER STATISTICS "My Schema"."My Stats" SET STATISTICS 5; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| SELECT a OPERATOR(pg_catalog.+) b FROM t; | ||
| SELECT a OPERATOR("My Schema".+) b FROM t; | ||
| SELECT a OPERATOR("S".===) b OPERATOR(public.*) c FROM t; | ||
| SELECT OPERATOR("S".-) a FROM t; | ||
| SELECT a OPERATOR("S".=) ANY (ARRAY[1]) FROM t; | ||
| SELECT a OPERATOR("S".=) ALL (SELECT 1) FROM t; | ||
| SELECT - -1, @ -1, 1 - -1, 2 ^ - 1; | ||
| CREATE OPERATOR "My Schema".=== (LEFTARG = int, RIGHTARG = int, PROCEDURE = f, COMMUTATOR = OPERATOR("My Schema".===), NEGATOR = OPERATOR("My Schema".!==)); | ||
| CREATE OPERATOR === (LEFTARG = int, RIGHTARG = int, PROCEDURE = f, COMMUTATOR = ===, NEGATOR = !==); | ||
| ALTER OPERATOR === (int, int) SET (COMMUTATOR = OPERATOR("My Schema".===)); | ||
| ALTER OPERATOR "S".=== (int, int) SET (NEGATOR = !==); | ||
| DROP OPERATOR "S".< (int, int); | ||
| COMMENT ON OPERATOR "My Schema".=== (int, int) IS 'x'; | ||
| COMMENT ON OPERATOR - (NONE, int) IS 'x'; | ||
| CREATE OPERATOR CLASS c FOR TYPE int USING btree AS OPERATOR 1 "S".< (int, int), OPERATOR 2 <=, FUNCTION 1 f(int, int); | ||
| ALTER OPERATOR FAMILY f USING btree ADD OPERATOR 1 "S".< (int, int); | ||
| ALTER TABLE t ADD EXCLUDE USING gist (c WITH OPERATOR("S".&&)); | ||
| ALTER TABLE t ADD EXCLUDE USING gist (c WITH &&); |
17 changes: 17 additions & 0 deletions
17
packages/deparser/__tests__/kitchen-sink/misc-alter-statistics.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
|
|
||
| import { FixtureTestUtils } from '../../test-utils'; | ||
| const fixtures = new FixtureTestUtils(); | ||
|
|
||
| it('misc-alter-statistics', async () => { | ||
| await fixtures.runFixtureTests([ | ||
| "misc/alter-statistics-1.sql", | ||
| "misc/alter-statistics-2.sql", | ||
| "misc/alter-statistics-3.sql", | ||
| "misc/alter-statistics-4.sql", | ||
| "misc/alter-statistics-5.sql", | ||
| "misc/alter-statistics-6.sql", | ||
| "misc/alter-statistics-7.sql", | ||
| "misc/alter-statistics-8.sql", | ||
| "misc/alter-statistics-9.sql" | ||
| ]); | ||
| }); |
26 changes: 26 additions & 0 deletions
26
packages/deparser/__tests__/kitchen-sink/misc-operator-names.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
|
|
||
| import { FixtureTestUtils } from '../../test-utils'; | ||
| const fixtures = new FixtureTestUtils(); | ||
|
|
||
| it('misc-operator-names', async () => { | ||
| await fixtures.runFixtureTests([ | ||
| "misc/operator-names-1.sql", | ||
| "misc/operator-names-2.sql", | ||
| "misc/operator-names-3.sql", | ||
| "misc/operator-names-4.sql", | ||
| "misc/operator-names-5.sql", | ||
| "misc/operator-names-6.sql", | ||
| "misc/operator-names-7.sql", | ||
| "misc/operator-names-8.sql", | ||
| "misc/operator-names-9.sql", | ||
| "misc/operator-names-10.sql", | ||
| "misc/operator-names-11.sql", | ||
| "misc/operator-names-12.sql", | ||
| "misc/operator-names-13.sql", | ||
| "misc/operator-names-14.sql", | ||
| "misc/operator-names-15.sql", | ||
| "misc/operator-names-16.sql", | ||
| "misc/operator-names-17.sql", | ||
| "misc/operator-names-18.sql" | ||
| ]); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { Deparser } from '../../src'; | ||
|
|
||
| // The parser can only produce operator names that lex back as one operator token, | ||
| // so names that would not round-trip have to be hand-built. | ||
| const opExpr = (...name: string[]) => ({ | ||
| A_Expr: { | ||
| kind: 'AEXPR_OP', | ||
| name: name.map(sval => ({ String: { sval } })), | ||
| lexpr: { ColumnRef: { fields: [{ String: { sval: 'a' } }] } }, | ||
| rexpr: { ColumnRef: { fields: [{ String: { sval: 'b' } }] } } | ||
| } | ||
| }); | ||
|
|
||
| describe('operator names', () => { | ||
| it.each([ | ||
| ['+-'], | ||
| ['*-'], | ||
| ['--'], | ||
| ['/*'], | ||
| ['!='], | ||
| ['=>'], | ||
| ['and'], | ||
| [''], | ||
| ['<'.repeat(64)] | ||
| ])('rejects %j, which would not reparse as the same operator', op => { | ||
| expect(() => Deparser.deparse(opExpr(op) as any)).toThrow(/Invalid operator name/); | ||
| expect(() => Deparser.deparse(opExpr('s', op) as any)).toThrow(/Invalid operator name/); | ||
| }); | ||
|
|
||
| it.each([ | ||
| [['+'], 'a + b'], | ||
| [['@-@'], 'a @-@ b'], | ||
| [['~-'], 'a ~- b'], | ||
| [['My Schema', '<->'], 'a OPERATOR("My Schema".<->) b'] | ||
| ])('keeps %j', (name, expected) => { | ||
| expect(Deparser.deparse(opExpr(...name) as any)).toBe(expected); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 bug · medium
%wrongly accepted in operators ending in+/-isValidOperatorSymbolincludes%in the exception class/[~!@#^&|?%]/that legalizes multi-character operators ending in+or-(packages/deparser/src/deparser.ts:991). PostgreSQL's lexer only allows such operators when they contain at least one of~ ! @ # ^ & | ` ?—%does not qualify. A name like%+` therefore passes validation and is deparsed, but the emitted SQL is rejected by the real PostgreSQL parser, breaking the round-trip guarantee this check was added to enforce.📋 Prompt for AI Agents
In packages/deparser/src/deparser.ts at line 991, change the trailing +/- exception regex in
isValidOperatorSymbolfrom/[~!@#^&|?%]/to/[~!@#^&|?]/. PostgreSQL's lexer only allows a multi-character operator ending in + or - when it contains one of ~ ! @ # ^ & |? — % does not qualify, so the current code accepts invalid operators like%+` that PostgreSQL will reject on reparse.