Skip to content

Commit 8d28590

Browse files
committed
Add test suite for query parser
This is a follow-up of 24e4da8, providing some tests to cover the problem that has been detected in a bug report related to the query parsing. One of the additions added by this commit is the treatment of hints within a dollar-quoted constant, which should be ignored by the query parser. This test suite can be improved over time depending on the needs. Test with values that include a quote are added, checking that the parser is able to handle that correctly. Per issue #228. Backpatch-through: 17
1 parent 24e4da8 commit 8d28590

File tree

3 files changed

+153
-1
lines changed

3 files changed

+153
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ HINTPLANVER = 1.9.0
1414

1515
REGRESS = init base_plan pg_hint_plan ut-init ut-A ut-S ut-J ut-L ut-G ut-R \
1616
ut-fdw ut-W ut-T ut-fini plpgsql hint_table disable_index \
17-
oldextversions
17+
query_parser oldextversions
1818
REGRESS_OPTS = --encoding=UTF8
1919

2020
EXTENSION = pg_hint_plan

expected/query_parser.out

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
-- Tests for the query parser
2+
LOAD 'pg_hint_plan';
3+
SET pg_hint_plan.debug_print TO on;
4+
SET client_min_messages TO LOG;
5+
SET pg_hint_plan.enable_hint TO on;
6+
-- Dollar-based constants.
7+
SELECT $$fox$$ AS constant;
8+
constant
9+
----------
10+
fox
11+
(1 row)
12+
13+
SELECT $SomeTag$s fox$SomeTag$ AS constant;
14+
constant
15+
----------
16+
s fox
17+
(1 row)
18+
19+
-- Dollar-based constants with quotes embedded.
20+
SELECT $$Mike's fox$$ AS constant;
21+
constant
22+
------------
23+
Mike's fox
24+
(1 row)
25+
26+
SELECT $SomeTag$Mike's fox$SomeTag$ AS constant;
27+
constant
28+
------------
29+
Mike's fox
30+
(1 row)
31+
32+
SELECT $$Mike"s fox$$ AS constant;
33+
constant
34+
------------
35+
Mike"s fox
36+
(1 row)
37+
38+
SELECT $SomeTag$Mike"s fox$SomeTag$ AS constant;
39+
constant
40+
------------
41+
Mike"s fox
42+
(1 row)
43+
44+
CREATE TABLE hint_parser_tab (a int, b int);
45+
CREATE INDEX hint_parser_ind ON hint_parser_tab(a);
46+
INSERT INTO hint_parser_tab VALUES (generate_series(1,200), generate_series(1,200));
47+
EXPLAIN SELECT /*+ IndexScan (hint_parser_tab hint_parser_ind) */ b
48+
FROM hint_parser_tab WHERE a = 108;
49+
LOG: available indexes for IndexScan(hint_parser_tab): hint_parser_ind
50+
LOG: pg_hint_plan:
51+
used hint:
52+
IndexScan(hint_parser_tab hint_parser_ind)
53+
not used hint:
54+
duplication hint:
55+
error hint:
56+
57+
QUERY PLAN
58+
-----------------------------------------------------------------------------------------
59+
Index Scan using hint_parser_ind on hint_parser_tab (cost=0.15..36.35 rows=11 width=4)
60+
Index Cond: (a = 108)
61+
(2 rows)
62+
63+
SELECT /*+ IndexScan (hint_parser_tab hint_parser_ind) */ b
64+
FROM hint_parser_tab WHERE a = 108;
65+
LOG: available indexes for IndexScan(hint_parser_tab): hint_parser_ind
66+
LOG: pg_hint_plan:
67+
used hint:
68+
IndexScan(hint_parser_tab hint_parser_ind)
69+
not used hint:
70+
duplication hint:
71+
error hint:
72+
73+
b
74+
-----
75+
108
76+
(1 row)
77+
78+
-- Dollar-quoted constant with hint embedded.
79+
-- IndexScan used, with SeqScan ignored because it is a query constant.
80+
EXPLAIN SELECT /*+ IndexScan (hint_parser_tab hint_parser_ind) */ b,
81+
$$/*+ SeqScan (hint_parser_tab) */$$ AS hint_ignored
82+
FROM hint_parser_tab WHERE a = 108;
83+
LOG: available indexes for IndexScan(hint_parser_tab): hint_parser_ind
84+
LOG: pg_hint_plan:
85+
used hint:
86+
IndexScan(hint_parser_tab hint_parser_ind)
87+
not used hint:
88+
duplication hint:
89+
error hint:
90+
91+
QUERY PLAN
92+
------------------------------------------------------------------------------------------
93+
Index Scan using hint_parser_ind on hint_parser_tab (cost=0.15..36.35 rows=11 width=36)
94+
Index Cond: (a = 108)
95+
(2 rows)
96+
97+
-- Different dollar-based pattern.
98+
-- SeqScan used, with IndexScan ignored because it is a query constant.
99+
EXPLAIN SELECT /*+ SeqScan (hint_parser_tab) */ b,
100+
$SomeTag$/*+ IndexScan (hint_parser_tab hint_parser_ind */$SomeTag$ AS hint_ignored
101+
FROM hint_parser_tab WHERE a = 108;
102+
LOG: pg_hint_plan:
103+
used hint:
104+
SeqScan(hint_parser_tab)
105+
not used hint:
106+
duplication hint:
107+
error hint:
108+
109+
QUERY PLAN
110+
------------------------------------------------------------------
111+
Seq Scan on hint_parser_tab (cost=0.00..38.25 rows=11 width=36)
112+
Filter: (a = 108)
113+
(2 rows)
114+
115+
DROP TABLE hint_parser_tab;

sql/query_parser.sql

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
-- Tests for the query parser
2+
3+
LOAD 'pg_hint_plan';
4+
SET pg_hint_plan.debug_print TO on;
5+
SET client_min_messages TO LOG;
6+
SET pg_hint_plan.enable_hint TO on;
7+
8+
-- Dollar-based constants.
9+
SELECT $$fox$$ AS constant;
10+
SELECT $SomeTag$s fox$SomeTag$ AS constant;
11+
-- Dollar-based constants with quotes embedded.
12+
SELECT $$Mike's fox$$ AS constant;
13+
SELECT $SomeTag$Mike's fox$SomeTag$ AS constant;
14+
SELECT $$Mike"s fox$$ AS constant;
15+
SELECT $SomeTag$Mike"s fox$SomeTag$ AS constant;
16+
17+
CREATE TABLE hint_parser_tab (a int, b int);
18+
CREATE INDEX hint_parser_ind ON hint_parser_tab(a);
19+
INSERT INTO hint_parser_tab VALUES (generate_series(1,200), generate_series(1,200));
20+
21+
EXPLAIN SELECT /*+ IndexScan (hint_parser_tab hint_parser_ind) */ b
22+
FROM hint_parser_tab WHERE a = 108;
23+
SELECT /*+ IndexScan (hint_parser_tab hint_parser_ind) */ b
24+
FROM hint_parser_tab WHERE a = 108;
25+
26+
-- Dollar-quoted constant with hint embedded.
27+
-- IndexScan used, with SeqScan ignored because it is a query constant.
28+
EXPLAIN SELECT /*+ IndexScan (hint_parser_tab hint_parser_ind) */ b,
29+
$$/*+ SeqScan (hint_parser_tab) */$$ AS hint_ignored
30+
FROM hint_parser_tab WHERE a = 108;
31+
-- Different dollar-based pattern.
32+
-- SeqScan used, with IndexScan ignored because it is a query constant.
33+
EXPLAIN SELECT /*+ SeqScan (hint_parser_tab) */ b,
34+
$SomeTag$/*+ IndexScan (hint_parser_tab hint_parser_ind */$SomeTag$ AS hint_ignored
35+
FROM hint_parser_tab WHERE a = 108;
36+
37+
DROP TABLE hint_parser_tab;

0 commit comments

Comments
 (0)