Skip to content

Commit

Permalink
Add testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
mos65o2 committed Dec 27, 2024
1 parent 72cc8b2 commit d0eeb6a
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/backend/gpopt/translate/CTranslatorDXLToScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,8 @@ CTranslatorDXLToScalar::TranslateDXLScalarSubplanToScalar(
// Generate var mapping to handle test expression based on the required
// output context. Test expression may contain vars from external
// (relative to subplan) nodes, so add their context too.
// We create a copy of the external mapping because we don't want to
// add TestExpr params to the original.
CMappingColIdVarPlStmt test_expr_var_mapping = CMappingColIdVarPlStmt(
m_mp, m_base_table_context, outer_child_contexts,
&test_expr_output_ctxt, plstmt->GetDXLToPlStmtContext());
Expand All @@ -762,7 +764,6 @@ CTranslatorDXLToScalar::TranslateDXLScalarSubplanToScalar(
dxlop->GetDxlTestExpr(), slink, &test_expr_var_mapping);

const CDXLColRefArray *outer_refs = dxlop->GetDxlOuterColRefsArray();

const ULONG len = outer_refs->Size();

// Translate a copy of the translate context: the param mappings from the outer scope get copied in the constructor
Expand Down
51 changes: 49 additions & 2 deletions src/test/regress/expected/subselect.out
Original file line number Diff line number Diff line change
Expand Up @@ -1161,9 +1161,9 @@ drop table tl3;
drop table tl4;
-- Check deep tree support in Test expression node when outer params are present with =ANY or IN queries
CREATE TABLE test_tbl_t (text_t text, int_t int) DISTRIBUTED BY (int_t);
INSERT INTO test_tbl_t VALUES ('0', 0), ('0', 1);
INSERT INTO test_tbl_t VALUES ('0', 0);
CREATE TABLE test_tbl_d (text_d text, int_d int) DISTRIBUTED BY (int_d);
INSERT INTO test_tbl_d VALUES ('0', 0), ('0', 1);
INSERT INTO test_tbl_d VALUES ('0', 0);
CREATE TABLE test_tbl_p (text_p text, int_p int) DISTRIBUTED BY (int_p);
INSERT INTO test_tbl_p VALUES ('0', 0), ('0', 1);
EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM test_tbl_t WHERE
Expand Down Expand Up @@ -1202,6 +1202,53 @@ EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM test_tbl_t WHERE
Settings: optimizer=off
(30 rows)

SELECT * FROM test_tbl_t WHERE
(int_t IN (SELECT int_d FROM test_tbl_d)) IN (SELECT int_p = 1 from test_tbl_p) ORDER BY int_t;
text_t | int_t
--------+-------
0 | 0
(1 row)

INSERT INTO test_tbl_t VALUES ('0', 1);
INSERT INTO test_tbl_d VALUES ('0', 1);
INSERT INTO test_tbl_p VALUES ('0', 0);
ANALYZE test_tbl_t;
EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM test_tbl_t WHERE
(int_t IN (SELECT int_d FROM test_tbl_d)) IN (SELECT int_p = 1 from test_tbl_p) ORDER BY int_t;
QUERY PLAN
-------------------------------------------------------------------------------------------
Gather Motion 3:1 (slice4; segments: 3)
Output: test_tbl_t.text_t, test_tbl_t.int_t
Merge Key: test_tbl_t.int_t
-> Sort
Output: test_tbl_t.text_t, test_tbl_t.int_t
Sort Key: test_tbl_t.int_t
-> Hash Semi Join
Output: test_tbl_t.text_t, test_tbl_t.int_t
Hash Cond: (((hashed SubPlan 1)) = (test_tbl_p.int_p = 1))
-> Redistribute Motion 3:3 (slice2; segments: 3)
Output: test_tbl_t.text_t, test_tbl_t.int_t, ((hashed SubPlan 1))
Hash Key: ((hashed SubPlan 1))
-> Seq Scan on public.test_tbl_t
Output: test_tbl_t.text_t, test_tbl_t.int_t, (hashed SubPlan 1)
SubPlan 1 (slice2; segments: 3)
-> Materialize
Output: test_tbl_d.int_d
-> Broadcast Motion 3:3 (slice1; segments: 3)
Output: test_tbl_d.int_d
-> Seq Scan on public.test_tbl_d
Output: test_tbl_d.int_d
-> Hash
Output: test_tbl_p.int_p
-> Redistribute Motion 3:3 (slice3; segments: 3)
Output: test_tbl_p.int_p
Hash Key: (test_tbl_p.int_p = 1)
-> Seq Scan on public.test_tbl_p
Output: test_tbl_p.int_p
Optimizer: Postgres query optimizer
Settings: optimizer=off
(30 rows)

SELECT * FROM test_tbl_t WHERE
(int_t IN (SELECT int_d FROM test_tbl_d)) IN (SELECT int_p = 1 from test_tbl_p) ORDER BY int_t;
text_t | int_t
Expand Down
66 changes: 64 additions & 2 deletions src/test/regress/expected/subselect_optimizer.out
Original file line number Diff line number Diff line change
Expand Up @@ -1237,11 +1237,73 @@ drop table tl3;
drop table tl4;
-- Check deep tree support in Test expression node when outer params are present with =ANY or IN queries
CREATE TABLE test_tbl_t (text_t text, int_t int) DISTRIBUTED BY (int_t);
INSERT INTO test_tbl_t VALUES ('0', 0), ('0', 1);
INSERT INTO test_tbl_t VALUES ('0', 0);
CREATE TABLE test_tbl_d (text_d text, int_d int) DISTRIBUTED BY (int_d);
INSERT INTO test_tbl_d VALUES ('0', 0), ('0', 1);
INSERT INTO test_tbl_d VALUES ('0', 0);
CREATE TABLE test_tbl_p (text_p text, int_p int) DISTRIBUTED BY (int_p);
INSERT INTO test_tbl_p VALUES ('0', 0), ('0', 1);
EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM test_tbl_t WHERE
(int_t IN (SELECT int_d FROM test_tbl_d)) IN (SELECT int_p = 1 from test_tbl_p) ORDER BY int_t;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Result
Output: test_tbl_t.text_t, test_tbl_t.int_t
Filter: (SubPlan 1)
-> Sort
Output: test_tbl_t.text_t, test_tbl_t.int_t
Sort Key: test_tbl_t.int_t
-> Gather Motion 3:1 (slice1; segments: 3)
Output: test_tbl_t.text_t, test_tbl_t.int_t
-> Seq Scan on public.test_tbl_t
Output: test_tbl_t.text_t, test_tbl_t.int_t
SubPlan 1 (slice0)
-> Result
Output: (count("outer".ColRef_0028)), (sum((CASE WHEN ((test_tbl_t.int_t = test_tbl_d.int_d) IS NULL) THEN 1 ELSE 0 END))), ((test_tbl_p.int_p = 1))
-> GroupAggregate
Output: count("outer".ColRef_0028), sum((CASE WHEN ((test_tbl_t.int_t = test_tbl_d.int_d) IS NULL) THEN 1 ELSE 0 END)), ((test_tbl_p.int_p = 1)), test_tbl_p.int_p, test_tbl_p.ctid, test_tbl_p.gp_segment_id
Group Key: test_tbl_p.int_p, test_tbl_p.ctid, test_tbl_p.gp_segment_id, ((test_tbl_p.int_p = 1))
-> Sort
Output: ((test_tbl_p.int_p = 1)), (CASE WHEN ((test_tbl_t.int_t = test_tbl_d.int_d) IS NULL) THEN 1 ELSE 0 END), test_tbl_p.int_p, test_tbl_p.ctid, test_tbl_p.gp_segment_id, "outer".ColRef_0028
Sort Key: test_tbl_p.int_p, test_tbl_p.ctid, test_tbl_p.gp_segment_id, ((test_tbl_p.int_p = 1))
-> Result
Output: (test_tbl_p.int_p = 1), CASE WHEN ((test_tbl_t.int_t = test_tbl_d.int_d) IS NULL) THEN 1 ELSE 0 END, test_tbl_p.int_p, test_tbl_p.ctid, test_tbl_p.gp_segment_id, "outer".ColRef_0028
-> Nested Loop Left Join
Output: test_tbl_p.int_p, test_tbl_p.ctid, test_tbl_p.gp_segment_id, test_tbl_d.int_d, "outer".ColRef_0028
Join Filter: true
-> Materialize
Output: test_tbl_p.int_p, test_tbl_p.ctid, test_tbl_p.gp_segment_id
-> Gather Motion 3:1 (slice3; segments: 3)
Output: test_tbl_p.int_p, test_tbl_p.ctid, test_tbl_p.gp_segment_id
-> Seq Scan on public.test_tbl_p
Output: test_tbl_p.int_p, test_tbl_p.ctid, test_tbl_p.gp_segment_id
-> Materialize
Output: "outer".ColRef_0028, test_tbl_d.int_d
-> Result
Output: true, test_tbl_d.int_d
-> Result
Output: test_tbl_d.int_d
Filter: ((test_tbl_t.int_t = test_tbl_d.int_d) IS NOT FALSE)
-> Materialize
Output: test_tbl_d.int_d
-> Gather Motion 3:1 (slice2; segments: 3)
Output: test_tbl_d.int_d
-> Seq Scan on public.test_tbl_d
Output: test_tbl_d.int_d
Optimizer: Pivotal Optimizer (GPORCA)
Settings: optimizer=on
(45 rows)

SELECT * FROM test_tbl_t WHERE
(int_t IN (SELECT int_d FROM test_tbl_d)) IN (SELECT int_p = 1 from test_tbl_p) ORDER BY int_t;
text_t | int_t
--------+-------
0 | 0
(1 row)

INSERT INTO test_tbl_t VALUES ('0', 1);
INSERT INTO test_tbl_d VALUES ('0', 1);
INSERT INTO test_tbl_p VALUES ('0', 0);
ANALYZE test_tbl_t;
EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM test_tbl_t WHERE
(int_t IN (SELECT int_d FROM test_tbl_d)) IN (SELECT int_p = 1 from test_tbl_p) ORDER BY int_t;
QUERY PLAN
Expand Down
14 changes: 12 additions & 2 deletions src/test/regress/sql/subselect.sql
Original file line number Diff line number Diff line change
Expand Up @@ -617,14 +617,24 @@ drop table tl4;

-- Check deep tree support in Test expression node when outer params are present with =ANY or IN queries
CREATE TABLE test_tbl_t (text_t text, int_t int) DISTRIBUTED BY (int_t);
INSERT INTO test_tbl_t VALUES ('0', 0), ('0', 1);
INSERT INTO test_tbl_t VALUES ('0', 0);

CREATE TABLE test_tbl_d (text_d text, int_d int) DISTRIBUTED BY (int_d);
INSERT INTO test_tbl_d VALUES ('0', 0), ('0', 1);
INSERT INTO test_tbl_d VALUES ('0', 0);

CREATE TABLE test_tbl_p (text_p text, int_p int) DISTRIBUTED BY (int_p);
INSERT INTO test_tbl_p VALUES ('0', 0), ('0', 1);

EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM test_tbl_t WHERE
(int_t IN (SELECT int_d FROM test_tbl_d)) IN (SELECT int_p = 1 from test_tbl_p) ORDER BY int_t;
SELECT * FROM test_tbl_t WHERE
(int_t IN (SELECT int_d FROM test_tbl_d)) IN (SELECT int_p = 1 from test_tbl_p) ORDER BY int_t;

INSERT INTO test_tbl_t VALUES ('0', 1);
INSERT INTO test_tbl_d VALUES ('0', 1);
INSERT INTO test_tbl_p VALUES ('0', 0);
ANALYZE test_tbl_t;

EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM test_tbl_t WHERE
(int_t IN (SELECT int_d FROM test_tbl_d)) IN (SELECT int_p = 1 from test_tbl_p) ORDER BY int_t;
SELECT * FROM test_tbl_t WHERE
Expand Down

0 comments on commit d0eeb6a

Please sign in to comment.