-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathquoting.sql
More file actions
81 lines (65 loc) · 3.31 KB
/
quoting.sql
File metadata and controls
81 lines (65 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
-- 1. Unquoted function name "float" (reserved keyword) - INPUT FORM
-- The deparser should quote this reserved keyword
CREATE FUNCTION faker.float(min double precision DEFAULT 0, max double precision DEFAULT 100) RETURNS double precision AS $EOFCODE$
BEGIN
RETURN min + random() * (max - min);
END;
$EOFCODE$ LANGUAGE plpgsql;
-- 2. Quoted function name "float" - CANONICAL FORM (idempotence check)
CREATE FUNCTION faker."float"(min double precision DEFAULT 0, max double precision DEFAULT 100) RETURNS double precision AS $EOFCODE$
BEGIN
RETURN min + random() * (max - min);
END;
$EOFCODE$ LANGUAGE plpgsql;
-- 3. Unquoted function name "interval" (reserved keyword) - INPUT FORM
CREATE FUNCTION faker.interval(min int, max int) RETURNS interval AS $EOFCODE$
BEGIN
RETURN make_interval(secs => (min + floor(random() * (max - min + 1)))::int);
END;
$EOFCODE$ LANGUAGE plpgsql;
-- 4. Quoted function name "interval" - CANONICAL FORM (idempotence check)
CREATE FUNCTION faker."interval"(min int, max int) RETURNS interval AS $EOFCODE$
BEGIN
RETURN make_interval(secs => (min + floor(random() * (max - min + 1)))::int);
END;
$EOFCODE$ LANGUAGE plpgsql;
-- 5. Unquoted function name "boolean" (reserved keyword) - INPUT FORM
CREATE FUNCTION faker.boolean() RETURNS boolean AS $EOFCODE$
BEGIN
RETURN random() < 0.5;
END;
$EOFCODE$ LANGUAGE plpgsql;
-- 6. Quoted function name "boolean" - CANONICAL FORM (idempotence check)
CREATE FUNCTION faker."boolean"() RETURNS boolean AS $EOFCODE$
BEGIN
RETURN random() < 0.5;
END;
$EOFCODE$ LANGUAGE plpgsql;
-- 7. pg_catalog.substring with quoted identifier - CANONICAL FORM
-- Note: SUBSTRING(value FROM 'pattern') SQL syntax gets deparsed to pg_catalog."substring"(value, 'pattern')
-- The SQL syntax form cannot be tested here due to AST round-trip differences (COERCE_SQL_SYNTAX vs COERCE_EXPLICIT_CALL)
CREATE DOMAIN origin AS text CHECK (value = pg_catalog."substring"(value, '^(https?://[^/]*)'));
-- 8-16: Type name quoting test cases
-- These demonstrate the BUG where user-defined schema-qualified types with keyword names
-- are over-quoted (e.g., myschema."json" instead of myschema.json)
-- 8. Type name quoting: json type should NOT be quoted (COL_NAME_KEYWORD in type position)
SELECT '{"a":1}'::json;
-- 9. Type name quoting: jsonb type should NOT be quoted
SELECT '{"b":2}'::jsonb;
-- 10. Type name quoting: boolean type should NOT be quoted (TYPE_FUNC_NAME_KEYWORD in type position)
SELECT true::boolean;
-- 11. Type name quoting: interval type should NOT be quoted (TYPE_FUNC_NAME_KEYWORD in type position)
SELECT '1 day'::interval;
-- 12. Type name quoting: int type should NOT be quoted (COL_NAME_KEYWORD in type position)
SELECT 42::int;
-- 13. Type cast in INSERT VALUES - json type should NOT be quoted
INSERT INTO test_table (data) VALUES ('{"c":3}'::json);
-- 14. User-defined schema-qualified type with keyword name - BUG: currently quotes the type name
-- Expected: myschema.json, Actual (buggy): myschema."json"
SELECT '{"d":4}'::myschema.json;
-- 15. User-defined schema-qualified type with keyword name 'int' - BUG: currently quotes
-- Expected: custom.int, Actual (buggy): custom."int"
SELECT 100::custom.int;
-- 16. User-defined schema-qualified type with keyword name 'boolean' - BUG: currently quotes
-- Expected: myapp.boolean, Actual (buggy): myapp."boolean"
SELECT true::myapp.boolean;