diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index 98eb40cc47e..d3ff5aed629 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -89,9 +89,10 @@ create_upper_paths_hook_type create_upper_paths_hook = NULL; #define EXPRKIND_APPINFO 7 #define EXPRKIND_PHV 8 #define EXPRKIND_TABLESAMPLE 9 -#define EXPRKIND_ARBITER_ELEM 10 -#define EXPRKIND_TABLEFUNC 11 -#define EXPRKIND_TABLEFUNC_LATERAL 12 +#define EXPRKIND_TABLESAMPLE_LATERAL 10 +#define EXPRKIND_ARBITER_ELEM 11 +#define EXPRKIND_TABLEFUNC 12 +#define EXPRKIND_TABLEFUNC_LATERAL 13 /* Passthrough data for standard_qp_callback */ typedef struct @@ -915,10 +916,15 @@ subquery_planner(PlannerGlobal *glob, Query *parse, if (rte->rtekind == RTE_RELATION) { if (rte->tablesample) + { + /* Preprocess the tablesample expression(s) fully */ + kind = rte->lateral ? EXPRKIND_TABLESAMPLE_LATERAL : + EXPRKIND_TABLESAMPLE; rte->tablesample = (TableSampleClause *) preprocess_expression(root, (Node *) rte->tablesample, - EXPRKIND_TABLESAMPLE); + kind); + } } else if (rte->rtekind == RTE_SUBQUERY) { diff --git a/src/test/regress/expected/tablesample.out b/src/test/regress/expected/tablesample.out index 60bb4e8e3e6..0534b530793 100644 --- a/src/test/regress/expected/tablesample.out +++ b/src/test/regress/expected/tablesample.out @@ -279,6 +279,38 @@ select pct, count(unique1) from 100 | 10000 (1 row) +-- check that a join alias Var in a pulled-up LATERAL subquery's TABLESAMPLE +-- clause gets flattened +explain (costs off) +select pct, count(unique1) from + ((values (0)) v(pct) full join (values (100)) w(pct) using (pct)), + lateral (select * from tenk1 tablesample bernoulli (pct)) ss + group by pct order by pct; + QUERY PLAN +---------------------------------------------------------------- + GroupAggregate + Group Key: (COALESCE((0), (100))) + -> Sort + Sort Key: (COALESCE((0), (100))) + -> Nested Loop + -> Hash Full Join + Hash Cond: ((0) = (100)) + -> Result + -> Hash + -> Result + -> Sample Scan on tenk1 + Sampling: bernoulli (COALESCE((0), (100))) +(12 rows) + +select pct, count(unique1) from + ((values (0)) v(pct) full join (values (100)) w(pct) using (pct)), + lateral (select * from tenk1 tablesample bernoulli (pct)) ss + group by pct order by pct; + pct | count +-----+------- + 100 | 10000 +(1 row) + -- errors SELECT id FROM test_tablesample TABLESAMPLE FOOBAR (1); ERROR: tablesample method foobar does not exist diff --git a/src/test/regress/sql/tablesample.sql b/src/test/regress/sql/tablesample.sql index aa17994277c..7dd7738e530 100644 --- a/src/test/regress/sql/tablesample.sql +++ b/src/test/regress/sql/tablesample.sql @@ -82,6 +82,18 @@ select pct, count(unique1) from lateral (select * from tenk1 tablesample system (pct)) ss group by pct; +-- check that a join alias Var in a pulled-up LATERAL subquery's TABLESAMPLE +-- clause gets flattened +explain (costs off) +select pct, count(unique1) from + ((values (0)) v(pct) full join (values (100)) w(pct) using (pct)), + lateral (select * from tenk1 tablesample bernoulli (pct)) ss + group by pct order by pct; +select pct, count(unique1) from + ((values (0)) v(pct) full join (values (100)) w(pct) using (pct)), + lateral (select * from tenk1 tablesample bernoulli (pct)) ss + group by pct order by pct; + -- errors SELECT id FROM test_tablesample TABLESAMPLE FOOBAR (1);