Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/backend/distributed/planner/recursive_planning.c
Original file line number Diff line number Diff line change
Expand Up @@ -1843,14 +1843,23 @@ ReplaceRTERelationWithRteSubquery(RangeTblEntry *rangeTableEntry,
RecursivePlanningContext *context,
RTEPermissionInfo *perminfo)
{
List *restrictionList =
GetRestrictInfoListForRelation(rangeTableEntry,
context->plannerRestrictionContext);

List *restrictionVars = pull_var_clause_default((Node *) restrictionList);
Var *restrictionVar = NULL;
foreach_declared_ptr(restrictionVar, restrictionVars)
{
requiredAttrNumbers = list_append_unique_int(requiredAttrNumbers,
restrictionVar->varattno);
}

Query *subquery = WrapRteRelationIntoSubquery(rangeTableEntry, requiredAttrNumbers,
perminfo);
List *outerQueryTargetList = CreateAllTargetListForRelation(rangeTableEntry->relid,
requiredAttrNumbers);

List *restrictionList =
GetRestrictInfoListForRelation(rangeTableEntry,
context->plannerRestrictionContext);
List *copyRestrictionList = copyObject(restrictionList);
Expr *andedBoundExpressions = make_ands_explicit(copyRestrictionList);
subquery->jointree->quals = (Node *) andedBoundExpressions;
Expand Down
119 changes: 119 additions & 0 deletions src/test/regress/expected/issue_8468_num_nulls.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
--
-- ISSUE_8468_NUM_NULLS
--
-- Test for GitHub issue #8468: TLP test fail on sqlancer due to
-- num_nulls interpreted differently on workers.
-- Root cause: when wrapping local tables into subqueries during
-- recursive planning, restriction-referenced columns were projected
-- as NULL in the outer subquery, causing wrong WHERE clause results.
--
CREATE SCHEMA issue_8468;
SET search_path TO issue_8468;
SET citus.shard_count TO 4;
SET citus.next_shard_id TO 9468000;
CREATE TABLE t5(c0 FLOAT);
SELECT create_distributed_table('t5', 'c0');
create_distributed_table
---------------------------------------------------------------------

(1 row)

INSERT INTO t5(c0) VALUES(0.009452163), (1.4691802E9), (0.005109378), (0.6941109),
(0.7013781), (0.8670044), (-1.6739732E9), (-4.5730365E8);
CREATE TABLE t1_parent(c0 FLOAT);
CREATE TABLE t2_child(c0 FLOAT, c1 CHAR(20), c2 DECIMAL) INHERITS(t1_parent);
NOTICE: merging column "c0" with inherited definition
INSERT INTO t1_parent(c0) VALUES(-1.6739732E9), (0), (0.32921866), ('-Infinity');
INSERT INTO t2_child(c1, c2, c0) VALUES('', 0.19, 0), ('test', 0.33, 0.89),
('abc', 0.58, 0.68), ('', 0.18, 0.74), ('', 0.22, 0.71);
CREATE TEMP TABLE t0(c0 FLOAT);
INSERT INTO t0(c0) VALUES(-1.9619044E9), (0.18373421), (6.175733E8), (0.58579546);
-- t1_parent* = 9 rows (4 from parent + 5 from child)
SELECT count(*) AS parent_star_count FROM t1_parent;
parent_star_count
---------------------------------------------------------------------
9
(1 row)

-- Original: 9 * (4*8) = 288
SELECT count(*) AS original FROM (
SELECT t5.c0 FROM t1_parent, t0 LEFT OUTER JOIN t5 ON (True)
) sub;
original
---------------------------------------------------------------------
288
(1 row)

-- TLP branches should sum to original
SELECT count(*) AS branch_true FROM (
SELECT t5.c0 FROM t1_parent, t0 LEFT OUTER JOIN t5 ON (True)
WHERE (0::double precision IS DISTINCT FROM t1_parent.c0)
) sub;
branch_true
---------------------------------------------------------------------
224
(1 row)

SELECT count(*) AS branch_false FROM (
SELECT t5.c0 FROM t1_parent, t0 LEFT OUTER JOIN t5 ON (True)
WHERE NOT (0::double precision IS DISTINCT FROM t1_parent.c0)
) sub;
branch_false
---------------------------------------------------------------------
64
(1 row)

SELECT count(*) AS branch_null FROM (
SELECT t5.c0 FROM t1_parent, t0 LEFT OUTER JOIN t5 ON (True)
WHERE (0::double precision IS DISTINCT FROM t1_parent.c0) IS NULL
) sub;
branch_null
---------------------------------------------------------------------
0
(1 row)

-- TLP combined must equal original
SELECT count(*) AS tlp_total FROM (
SELECT t5.c0 FROM t1_parent, t0 LEFT OUTER JOIN t5 ON (True)
WHERE (0::double precision IS DISTINCT FROM t1_parent.c0)
UNION ALL
SELECT t5.c0 FROM t1_parent, t0 LEFT OUTER JOIN t5 ON (True)
WHERE NOT (0::double precision IS DISTINCT FROM t1_parent.c0)
UNION ALL
SELECT t5.c0 FROM t1_parent, t0 LEFT OUTER JOIN t5 ON (True)
WHERE (0::double precision IS DISTINCT FROM t1_parent.c0) IS NULL
) sub;
tlp_total
---------------------------------------------------------------------
288
(1 row)

-- Verify with num_nulls (the original condition from the bug report)
SELECT count(*) AS num_nulls_tlp FROM (
SELECT t5.c0 FROM t1_parent, t0 LEFT OUTER JOIN t5 ON (True)
WHERE (num_nulls(t1_parent.c0) IS DISTINCT FROM t1_parent.c0)
UNION ALL
SELECT t5.c0 FROM t1_parent, t0 LEFT OUTER JOIN t5 ON (True)
WHERE NOT (num_nulls(t1_parent.c0) IS DISTINCT FROM t1_parent.c0)
UNION ALL
SELECT t5.c0 FROM t1_parent, t0 LEFT OUTER JOIN t5 ON (True)
WHERE (num_nulls(t1_parent.c0) IS DISTINCT FROM t1_parent.c0) IS NULL
) sub;
num_nulls_tlp
---------------------------------------------------------------------
288
(1 row)

-- Also test the plain equality condition
SELECT count(*) AS equals_zero FROM (
SELECT t5.c0 FROM t1_parent, t0 LEFT OUTER JOIN t5 ON (True)
WHERE t1_parent.c0 = 0
) sub;
equals_zero
---------------------------------------------------------------------
64
(1 row)

-- Clean up
SET client_min_messages TO ERROR;
DROP SCHEMA issue_8468 CASCADE;
1 change: 1 addition & 0 deletions src/test/regress/multi_schedule
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ test: multi_deparse_shard_query multi_distributed_transaction_id intermediate_re
test: multi_explain
test: hyperscale_tutorial partitioned_intermediate_results distributed_intermediate_results multi_real_time_transaction
test: multi_basic_queries cross_join multi_complex_expressions multi_subquery multi_subquery_complex_queries multi_subquery_behavioral_analytics
test: issue_8468_num_nulls
test: multi_subquery_complex_reference_clause multi_subquery_window_functions multi_view multi_sql_function multi_prepare_sql
test: sql_procedure multi_function_in_join row_types materialized_view
test: multi_subquery_in_where_reference_clause adaptive_executor propagate_set_commands geqo
Expand Down
88 changes: 88 additions & 0 deletions src/test/regress/sql/issue_8468_num_nulls.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
--
-- ISSUE_8468_NUM_NULLS
--
-- Test for GitHub issue #8468: TLP test fail on sqlancer due to
-- num_nulls interpreted differently on workers.
-- Root cause: when wrapping local tables into subqueries during
-- recursive planning, restriction-referenced columns were projected
-- as NULL in the outer subquery, causing wrong WHERE clause results.
--

CREATE SCHEMA issue_8468;
SET search_path TO issue_8468;
SET citus.shard_count TO 4;
SET citus.next_shard_id TO 9468000;

CREATE TABLE t5(c0 FLOAT);
SELECT create_distributed_table('t5', 'c0');

INSERT INTO t5(c0) VALUES(0.009452163), (1.4691802E9), (0.005109378), (0.6941109),
(0.7013781), (0.8670044), (-1.6739732E9), (-4.5730365E8);

CREATE TABLE t1_parent(c0 FLOAT);
CREATE TABLE t2_child(c0 FLOAT, c1 CHAR(20), c2 DECIMAL) INHERITS(t1_parent);

INSERT INTO t1_parent(c0) VALUES(-1.6739732E9), (0), (0.32921866), ('-Infinity');
INSERT INTO t2_child(c1, c2, c0) VALUES('', 0.19, 0), ('test', 0.33, 0.89),
('abc', 0.58, 0.68), ('', 0.18, 0.74), ('', 0.22, 0.71);

CREATE TEMP TABLE t0(c0 FLOAT);
INSERT INTO t0(c0) VALUES(-1.9619044E9), (0.18373421), (6.175733E8), (0.58579546);

-- t1_parent* = 9 rows (4 from parent + 5 from child)
SELECT count(*) AS parent_star_count FROM t1_parent;

-- Original: 9 * (4*8) = 288
SELECT count(*) AS original FROM (
SELECT t5.c0 FROM t1_parent, t0 LEFT OUTER JOIN t5 ON (True)
) sub;

-- TLP branches should sum to original
SELECT count(*) AS branch_true FROM (
SELECT t5.c0 FROM t1_parent, t0 LEFT OUTER JOIN t5 ON (True)
WHERE (0::double precision IS DISTINCT FROM t1_parent.c0)
) sub;

SELECT count(*) AS branch_false FROM (
SELECT t5.c0 FROM t1_parent, t0 LEFT OUTER JOIN t5 ON (True)
WHERE NOT (0::double precision IS DISTINCT FROM t1_parent.c0)
) sub;

SELECT count(*) AS branch_null FROM (
SELECT t5.c0 FROM t1_parent, t0 LEFT OUTER JOIN t5 ON (True)
WHERE (0::double precision IS DISTINCT FROM t1_parent.c0) IS NULL
) sub;

-- TLP combined must equal original
SELECT count(*) AS tlp_total FROM (
SELECT t5.c0 FROM t1_parent, t0 LEFT OUTER JOIN t5 ON (True)
WHERE (0::double precision IS DISTINCT FROM t1_parent.c0)
UNION ALL
SELECT t5.c0 FROM t1_parent, t0 LEFT OUTER JOIN t5 ON (True)
WHERE NOT (0::double precision IS DISTINCT FROM t1_parent.c0)
UNION ALL
SELECT t5.c0 FROM t1_parent, t0 LEFT OUTER JOIN t5 ON (True)
WHERE (0::double precision IS DISTINCT FROM t1_parent.c0) IS NULL
) sub;

-- Verify with num_nulls (the original condition from the bug report)
SELECT count(*) AS num_nulls_tlp FROM (
SELECT t5.c0 FROM t1_parent, t0 LEFT OUTER JOIN t5 ON (True)
WHERE (num_nulls(t1_parent.c0) IS DISTINCT FROM t1_parent.c0)
UNION ALL
SELECT t5.c0 FROM t1_parent, t0 LEFT OUTER JOIN t5 ON (True)
WHERE NOT (num_nulls(t1_parent.c0) IS DISTINCT FROM t1_parent.c0)
UNION ALL
SELECT t5.c0 FROM t1_parent, t0 LEFT OUTER JOIN t5 ON (True)
WHERE (num_nulls(t1_parent.c0) IS DISTINCT FROM t1_parent.c0) IS NULL
) sub;

-- Also test the plain equality condition
SELECT count(*) AS equals_zero FROM (
SELECT t5.c0 FROM t1_parent, t0 LEFT OUTER JOIN t5 ON (True)
WHERE t1_parent.c0 = 0
) sub;

-- Clean up
SET client_min_messages TO ERROR;
DROP SCHEMA issue_8468 CASCADE;
Loading