Skip to content
Merged
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
3 changes: 3 additions & 0 deletions include/pgduckdb/pgduckdb_ruleutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@ int pgduckdb_show_type(Const *constval, int original_showtype);
bool pgduckdb_subscript_has_custom_alias(Plan *plan, List *rtable, Var *subscript_var, char *colname);
SubscriptingRef *pgduckdb_strip_first_subscript(SubscriptingRef *sbsref, StringInfo buf);
char *pgduckdb_write_row_refname(StringInfo buf, char *refname, bool is_top_level);
bool is_system_sampling(const char *tsm_name, int num_args);
bool is_bernoulli_sampling(const char *tsm_name, int num_args);
void pgduckdb_add_tablesample_percent(const char *tsm_name, StringInfo buf, int num_args);

extern bool processed_targetlist;
18 changes: 18 additions & 0 deletions src/pgduckdb_ruleutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1137,4 +1137,22 @@ pgduckdb_is_not_default_expr(Node *node, void *context) {
return expression_tree_walker(node, (bool (*)())((void *)pgduckdb_is_not_default_expr), context);
#endif
}

bool
is_system_sampling(const char *tsm_name, int num_args) {
return (pg_strcasecmp(tsm_name, "system") == 0) && (num_args == 1);
}

bool
is_bernoulli_sampling(const char *tsm_name, int num_args) {
return (pg_strcasecmp(tsm_name, "bernoulli") == 0) && (num_args == 1);
}

void
pgduckdb_add_tablesample_percent(const char *tsm_name, StringInfo buf, int num_args) {
if (!(is_system_sampling(tsm_name, num_args) || is_bernoulli_sampling(tsm_name, num_args))) {
return;
}
appendStringInfoChar(buf, '%');
}
}
4 changes: 4 additions & 0 deletions src/vendor/pg_ruleutils_14.c
Original file line number Diff line number Diff line change
Expand Up @@ -11448,6 +11448,10 @@ get_tablesample_def(TableSampleClause *tablesample, deparse_context *context)
if (nargs++ > 0)
appendStringInfoString(buf, ", ");
get_rule_expr((Node *) lfirst(l), context, false);
const char *tsm_name = generate_function_name(tablesample->tsmhandler, 1,
NIL, argtypes,
false, NULL, EXPR_KIND_NONE);
pgduckdb_add_tablesample_percent(tsm_name, buf, list_length(tablesample->args));
}
appendStringInfoChar(buf, ')');

Expand Down
4 changes: 4 additions & 0 deletions src/vendor/pg_ruleutils_15.c
Original file line number Diff line number Diff line change
Expand Up @@ -11650,6 +11650,10 @@ get_tablesample_def(TableSampleClause *tablesample, deparse_context *context)
if (nargs++ > 0)
appendStringInfoString(buf, ", ");
get_rule_expr((Node *) lfirst(l), context, false);
const char *tsm_name = generate_function_name(tablesample->tsmhandler, 1,
NIL, argtypes,
false, NULL, EXPR_KIND_NONE);
pgduckdb_add_tablesample_percent(tsm_name, buf, list_length(tablesample->args));
}
appendStringInfoChar(buf, ')');

Expand Down
4 changes: 4 additions & 0 deletions src/vendor/pg_ruleutils_16.c
Original file line number Diff line number Diff line change
Expand Up @@ -11859,6 +11859,10 @@ get_tablesample_def(TableSampleClause *tablesample, deparse_context *context)
if (nargs++ > 0)
appendStringInfoString(buf, ", ");
get_rule_expr((Node *) lfirst(l), context, false);
const char *tsm_name = generate_function_name(tablesample->tsmhandler, 1,
NIL, argtypes,
false, NULL, EXPR_KIND_NONE);
pgduckdb_add_tablesample_percent(tsm_name, buf, list_length(tablesample->args));
}
appendStringInfoChar(buf, ')');

Expand Down
4 changes: 4 additions & 0 deletions src/vendor/pg_ruleutils_17.c
Original file line number Diff line number Diff line change
Expand Up @@ -12517,6 +12517,10 @@ get_tablesample_def(TableSampleClause *tablesample, deparse_context *context)
if (nargs++ > 0)
appendStringInfoString(buf, ", ");
get_rule_expr((Node *) lfirst(l), context, false);
const char *tsm_name = generate_function_name(tablesample->tsmhandler, 1,
NIL, argtypes,
false, NULL, EXPR_KIND_NONE);
pgduckdb_add_tablesample_percent(tsm_name, buf, list_length(tablesample->args));
}
appendStringInfoChar(buf, ')');

Expand Down
43 changes: 43 additions & 0 deletions test/regression/expected/tablesample.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
SET duckdb.force_execution = true;
CREATE TABLE t (a INT);
INSERT INTO t SELECT i FROM generate_series(1, 10) AS i;
SELECT * FROM t TABLESAMPLE SYSTEM (100);
Comment thread
nbiscaro marked this conversation as resolved.
a
----
1
2
3
4
5
6
7
8
9
10
(10 rows)

SELECT * FROM t TABLESAMPLE BERNOULLI (100);
a
----
1
2
3
4
5
6
7
8
9
10
(10 rows)

SELECT * FROM t TABLESAMPLE BERNOULLI (50) REPEATABLE(42);
a
----
2
5
7
10
(4 rows)

DROP TABLE t;
1 change: 1 addition & 0 deletions test/regression/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ test: date
test: timestamp_timestamptz
test: union_functions
test: foreign_data_wrapper
test: tablesample
11 changes: 11 additions & 0 deletions test/regression/sql/tablesample.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SET duckdb.force_execution = true;

CREATE TABLE t (a INT);
INSERT INTO t SELECT i FROM generate_series(1, 10) AS i;

SELECT * FROM t TABLESAMPLE SYSTEM (100);

SELECT * FROM t TABLESAMPLE BERNOULLI (100);
SELECT * FROM t TABLESAMPLE BERNOULLI (50) REPEATABLE(42);

DROP TABLE t;