Skip to content

Commit e32c9b1

Browse files
committed
demo: add test fixtures showing type-name quoting bug (DO NOT MERGE)
This PR demonstrates the bug where user-defined schema-qualified types with keyword names are over-quoted. The snapshots show: - myschema."json" (should be myschema.json) - custom."int" (should be custom.int) - myapp."boolean" (should be myapp.boolean) See PR #248 for the fix.
1 parent 48ea421 commit e32c9b1

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

__fixtures__/generated/generated.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@
6060
"pretty/quoting-5.sql": "CREATE FUNCTION faker.boolean() RETURNS boolean AS $EOFCODE$\nBEGIN\n RETURN random() < 0.5;\nEND;\n$EOFCODE$ LANGUAGE plpgsql",
6161
"pretty/quoting-6.sql": "CREATE FUNCTION faker.\"boolean\"() RETURNS boolean AS $EOFCODE$\nBEGIN\n RETURN random() < 0.5;\nEND;\n$EOFCODE$ LANGUAGE plpgsql",
6262
"pretty/quoting-7.sql": "CREATE DOMAIN origin AS text CHECK (value = pg_catalog.\"substring\"(value, '^(https?://[^/]*)'))",
63+
"pretty/quoting-8.sql": "SELECT '{\"a\":1}'::json",
64+
"pretty/quoting-9.sql": "SELECT '{\"b\":2}'::jsonb",
65+
"pretty/quoting-10.sql": "SELECT true::boolean",
66+
"pretty/quoting-11.sql": "SELECT '1 day'::interval",
67+
"pretty/quoting-12.sql": "SELECT 42::int",
68+
"pretty/quoting-13.sql": "INSERT INTO test_table (data) VALUES ('{\"c\":3}'::json)",
69+
"pretty/quoting-14.sql": "SELECT '{\"d\":4}'::myschema.json",
70+
"pretty/quoting-15.sql": "SELECT 100::custom.int",
71+
"pretty/quoting-16.sql": "SELECT true::myapp.boolean",
6372
"pretty/procedures-1.sql": "SELECT handle_insert('TYPE_A')",
6473
"pretty/procedures-2.sql": "SELECT \"HandleInsert\"('TYPE_A', 'Region-1')",
6574
"pretty/procedures-3.sql": "SELECT compute_score(42, TRUE)",

__fixtures__/kitchen-sink/pretty/quoting.sql

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,37 @@ $EOFCODE$ LANGUAGE plpgsql;
4545
-- Note: SUBSTRING(value FROM 'pattern') SQL syntax gets deparsed to pg_catalog."substring"(value, 'pattern')
4646
-- The SQL syntax form cannot be tested here due to AST round-trip differences (COERCE_SQL_SYNTAX vs COERCE_EXPLICIT_CALL)
4747
CREATE DOMAIN origin AS text CHECK (value = pg_catalog."substring"(value, '^(https?://[^/]*)'));
48+
49+
-- 8-16: Type name quoting test cases
50+
-- These demonstrate the BUG where user-defined schema-qualified types with keyword names
51+
-- are over-quoted (e.g., myschema."json" instead of myschema.json)
52+
53+
-- 8. Type name quoting: json type should NOT be quoted (COL_NAME_KEYWORD in type position)
54+
SELECT '{"a":1}'::json;
55+
56+
-- 9. Type name quoting: jsonb type should NOT be quoted
57+
SELECT '{"b":2}'::jsonb;
58+
59+
-- 10. Type name quoting: boolean type should NOT be quoted (TYPE_FUNC_NAME_KEYWORD in type position)
60+
SELECT true::boolean;
61+
62+
-- 11. Type name quoting: interval type should NOT be quoted (TYPE_FUNC_NAME_KEYWORD in type position)
63+
SELECT '1 day'::interval;
64+
65+
-- 12. Type name quoting: int type should NOT be quoted (COL_NAME_KEYWORD in type position)
66+
SELECT 42::int;
67+
68+
-- 13. Type cast in INSERT VALUES - json type should NOT be quoted
69+
INSERT INTO test_table (data) VALUES ('{"c":3}'::json);
70+
71+
-- 14. User-defined schema-qualified type with keyword name - BUG: currently quotes the type name
72+
-- Expected: myschema.json, Actual (buggy): myschema."json"
73+
SELECT '{"d":4}'::myschema.json;
74+
75+
-- 15. User-defined schema-qualified type with keyword name 'int' - BUG: currently quotes
76+
-- Expected: custom.int, Actual (buggy): custom."int"
77+
SELECT 100::custom.int;
78+
79+
-- 16. User-defined schema-qualified type with keyword name 'boolean' - BUG: currently quotes
80+
-- Expected: myapp.boolean, Actual (buggy): myapp."boolean"
81+
SELECT true::myapp.boolean;

packages/deparser/__tests__/pretty/__snapshots__/quoting-pretty.test.ts.snap

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@ $$ LANGUAGE plpgsql"
5050

5151
exports[`non-pretty: pretty/quoting-7.sql 1`] = `"CREATE DOMAIN origin AS text CHECK (value = pg_catalog.substring(value, '^(https?://[^/]*)'))"`;
5252

53+
exports[`non-pretty: pretty/quoting-8.sql 1`] = `"SELECT '{"a":1}'::json"`;
54+
55+
exports[`non-pretty: pretty/quoting-9.sql 1`] = `"SELECT '{"b":2}'::jsonb"`;
56+
57+
exports[`non-pretty: pretty/quoting-10.sql 1`] = `"SELECT CAST(true AS boolean)"`;
58+
59+
exports[`non-pretty: pretty/quoting-11.sql 1`] = `"SELECT '1 day'::interval"`;
60+
61+
exports[`non-pretty: pretty/quoting-12.sql 1`] = `"SELECT 42::int"`;
62+
63+
exports[`non-pretty: pretty/quoting-13.sql 1`] = `"INSERT INTO test_table (data) VALUES ('{"c":3}'::json)"`;
64+
65+
exports[`non-pretty: pretty/quoting-14.sql 1`] = `"SELECT CAST('{"d":4}' AS myschema."json")"`;
66+
67+
exports[`non-pretty: pretty/quoting-15.sql 1`] = `"SELECT CAST(100 AS custom."int")"`;
68+
69+
exports[`non-pretty: pretty/quoting-16.sql 1`] = `"SELECT CAST(true AS myapp."boolean")"`;
70+
5371
exports[`pretty: pretty/quoting-1.sql 1`] = `
5472
"CREATE FUNCTION faker.float(min double precision DEFAULT 0, max double precision DEFAULT 100) RETURNS double precision AS $$
5573
BEGIN
@@ -102,3 +120,26 @@ exports[`pretty: pretty/quoting-7.sql 1`] = `
102120
"CREATE DOMAIN origin AS text
103121
CHECK (value = pg_catalog.substring(value, '^(https?://[^/]*)'))"
104122
`;
123+
124+
exports[`pretty: pretty/quoting-8.sql 1`] = `"SELECT '{"a":1}'::json"`;
125+
126+
exports[`pretty: pretty/quoting-9.sql 1`] = `"SELECT '{"b":2}'::jsonb"`;
127+
128+
exports[`pretty: pretty/quoting-10.sql 1`] = `"SELECT CAST(true AS boolean)"`;
129+
130+
exports[`pretty: pretty/quoting-11.sql 1`] = `"SELECT '1 day'::interval"`;
131+
132+
exports[`pretty: pretty/quoting-12.sql 1`] = `"SELECT 42::int"`;
133+
134+
exports[`pretty: pretty/quoting-13.sql 1`] = `
135+
"INSERT INTO test_table (
136+
data
137+
) VALUES
138+
('{"c":3}'::json)"
139+
`;
140+
141+
exports[`pretty: pretty/quoting-14.sql 1`] = `"SELECT CAST('{"d":4}' AS myschema."json")"`;
142+
143+
exports[`pretty: pretty/quoting-15.sql 1`] = `"SELECT CAST(100 AS custom."int")"`;
144+
145+
exports[`pretty: pretty/quoting-16.sql 1`] = `"SELECT CAST(true AS myapp."boolean")"`;

packages/deparser/__tests__/pretty/quoting-pretty.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ const prettyTest = new PrettyTest([
77
'pretty/quoting-5.sql',
88
'pretty/quoting-6.sql',
99
'pretty/quoting-7.sql',
10+
'pretty/quoting-8.sql',
11+
'pretty/quoting-9.sql',
12+
'pretty/quoting-10.sql',
13+
'pretty/quoting-11.sql',
14+
'pretty/quoting-12.sql',
15+
'pretty/quoting-13.sql',
16+
'pretty/quoting-14.sql',
17+
'pretty/quoting-15.sql',
18+
'pretty/quoting-16.sql',
1019
]);
1120

1221
prettyTest.generateTests();

0 commit comments

Comments
 (0)