-- ut-T: tests for table hints
-- This test is focusing on hint retrieval from table
LOAD 'pg_hint_plan';
SET pg_hint_plan.enable_hint TO on;
SET pg_hint_plan.debug_print TO on;
SET client_min_messages TO LOG;
SET search_path TO public;
-- test for get_query_string
INSERT INTO hint_plan.hints VALUES(DEFAULT,'EXPLAIN SELECT * FROM t1 WHERE id = ?;', '', 'SeqScan(t1)');
INSERT INTO hint_plan.hints VALUES(DEFAULT,'PREPARE p1 AS SELECT * FROM t1 WHERE id = ?;', '', 'SeqScan(t1)');
INSERT INTO hint_plan.hints VALUES(DEFAULT,'EXPLAIN DECLARE c1 CURSOR FOR SELECT * FROM t1 WHERE id = ?;', '', 'SeqScan(t1)');
INSERT INTO hint_plan.hints VALUES(DEFAULT,'EXPLAIN CREATE TABLE ct1 AS SELECT * FROM t1 WHERE id = ?;', '', 'SeqScan(t1)');
PREPARE p1 AS SELECT * FROM t1 WHERE id = 100;
-- These queries uses IndexScan without hints
SET pg_hint_plan.enable_hint_table to off;
EXPLAIN SELECT * FROM t1 WHERE id = 100;
                            QUERY PLAN                            
------------------------------------------------------------------
 Index Scan using t1_pkey on t1  (cost=0.29..8.30 rows=1 width=8)
   Index Cond: (id = 100)
(2 rows)

EXPLAIN DECLARE c1 CURSOR FOR SELECT * FROM t1 WHERE id = 100;
                            QUERY PLAN                            
------------------------------------------------------------------
 Index Scan using t1_pkey on t1  (cost=0.29..8.30 rows=1 width=8)
   Index Cond: (id = 100)
(2 rows)

EXPLAIN CREATE TABLE ct1 AS SELECT * FROM t1 WHERE id = 100;
                            QUERY PLAN                            
------------------------------------------------------------------
 Index Scan using t1_pkey on t1  (cost=0.29..8.30 rows=1 width=8)
   Index Cond: (id = 100)
(2 rows)

EXPLAIN EXECUTE p1;
                            QUERY PLAN                            
------------------------------------------------------------------
 Index Scan using t1_pkey on t1  (cost=0.29..8.30 rows=1 width=8)
   Index Cond: (id = 100)
(2 rows)

DEALLOCATE p1;
PREPARE p1 AS SELECT * FROM t1 WHERE id = 100;
EXPLAIN CREATE TABLE ct1 AS EXECUTE p1;
                            QUERY PLAN                            
------------------------------------------------------------------
 Index Scan using t1_pkey on t1  (cost=0.29..8.30 rows=1 width=8)
   Index Cond: (id = 100)
(2 rows)

DEALLOCATE p1;
PREPARE p1 AS SELECT * FROM t1 WHERE id = 100;
-- Forced to use SeqScan by table hints
SET pg_hint_plan.enable_hint_table to on;
EXPLAIN SELECT * FROM t1 WHERE id = 100;
LOG:  pg_hint_plan:
used hint:
SeqScan(t1)
not used hint:
duplication hint:
error hint:

                     QUERY PLAN                     
----------------------------------------------------
 Seq Scan on t1  (cost=0.00..170.00 rows=1 width=8)
   Filter: (id = 100)
(2 rows)

EXPLAIN DECLARE c1 CURSOR FOR SELECT * FROM t1 WHERE id = 100;
LOG:  pg_hint_plan:
used hint:
SeqScan(t1)
not used hint:
duplication hint:
error hint:

                     QUERY PLAN                     
----------------------------------------------------
 Seq Scan on t1  (cost=0.00..170.00 rows=1 width=8)
   Filter: (id = 100)
(2 rows)

EXPLAIN CREATE TABLE ct1 AS SELECT * FROM t1 WHERE id = 100;
LOG:  pg_hint_plan:
used hint:
SeqScan(t1)
not used hint:
duplication hint:
error hint:

                     QUERY PLAN                     
----------------------------------------------------
 Seq Scan on t1  (cost=0.00..170.00 rows=1 width=8)
   Filter: (id = 100)
(2 rows)

EXPLAIN EXECUTE p1;
LOG:  pg_hint_plan:
used hint:
SeqScan(t1)
not used hint:
duplication hint:
error hint:

                     QUERY PLAN                     
----------------------------------------------------
 Seq Scan on t1  (cost=0.00..170.00 rows=1 width=8)
   Filter: (id = 100)
(2 rows)

DEALLOCATE p1;
PREPARE p1 AS SELECT * FROM t1 WHERE id = 100;
EXPLAIN CREATE TABLE ct1 AS EXECUTE p1;
LOG:  pg_hint_plan:
used hint:
SeqScan(t1)
not used hint:
duplication hint:
error hint:

                     QUERY PLAN                     
----------------------------------------------------
 Seq Scan on t1  (cost=0.00..170.00 rows=1 width=8)
   Filter: (id = 100)
(2 rows)

DEALLOCATE p1;
-- Check proper calling to generate_normalized_query
\;\;SELECT 1,2;
 ?column? | ?column? 
----------+----------
        1 |        2
(1 row)

SET pg_hint_plan.enable_hint_table to off;
DELETE FROM hint_plan.hints;
