Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add implicit % to bridge TABLESAMPLE syntax #559

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions include/pgduckdb/pgduckdb_ruleutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ 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);

extern bool processed_targetlist;
5 changes: 5 additions & 0 deletions src/pgduckdb_ruleutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -770,4 +770,9 @@ 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);
}
}
29 changes: 18 additions & 11 deletions src/vendor/pg_ruleutils_14.c
Original file line number Diff line number Diff line change
Expand Up @@ -11424,26 +11424,33 @@ static void
get_tablesample_def(TableSampleClause *tablesample, deparse_context *context)
{
StringInfo buf = context->buf;
Oid argtypes[1];
Oid argtypes[1] = { INTERNALOID };
char *tsm_name;
int nargs;
ListCell *l;

/*
* We should qualify the handler's function name if it wouldn't be
* resolved by lookup in the current search path.
*/
argtypes[0] = INTERNALOID;
appendStringInfo(buf, " TABLESAMPLE %s (",
generate_function_name(tablesample->tsmhandler, 1,
NIL, argtypes,
false, NULL, EXPR_KIND_NONE));
tsm_name = generate_function_name(tablesample->tsmhandler, 1, NIL, argtypes,
false, NULL, EXPR_KIND_NONE);
appendStringInfo(buf, " TABLESAMPLE %s (", tsm_name);

nargs = 0;
foreach(l, tablesample->args)
if (is_system_sampling(tsm_name, list_length(tablesample->args)))
{
if (nargs++ > 0)
appendStringInfoString(buf, ", ");
get_rule_expr((Node *) lfirst(l), context, false);
get_rule_expr((Node *) linitial(tablesample->args), context, false);
appendStringInfoChar(buf, '%');
}
else
{
nargs = 0;
foreach(l, tablesample->args)
{
if (nargs++ > 0)
appendStringInfoString(buf, ", ");
get_rule_expr((Node *) lfirst(l), context, false);
}
}
appendStringInfoChar(buf, ')');

Expand Down
29 changes: 18 additions & 11 deletions src/vendor/pg_ruleutils_15.c
Original file line number Diff line number Diff line change
Expand Up @@ -11626,26 +11626,33 @@ static void
get_tablesample_def(TableSampleClause *tablesample, deparse_context *context)
{
StringInfo buf = context->buf;
Oid argtypes[1];
Oid argtypes[1] = { INTERNALOID };
char *tsm_name;
int nargs;
ListCell *l;

/*
* We should qualify the handler's function name if it wouldn't be
* resolved by lookup in the current search path.
*/
argtypes[0] = INTERNALOID;
appendStringInfo(buf, " TABLESAMPLE %s (",
generate_function_name(tablesample->tsmhandler, 1,
NIL, argtypes,
false, NULL, EXPR_KIND_NONE));
tsm_name = generate_function_name(tablesample->tsmhandler, 1, NIL, argtypes,
false, NULL, EXPR_KIND_NONE);
appendStringInfo(buf, " TABLESAMPLE %s (", tsm_name);

nargs = 0;
foreach(l, tablesample->args)
if (is_system_sampling(tsm_name, list_length(tablesample->args)))
{
if (nargs++ > 0)
appendStringInfoString(buf, ", ");
get_rule_expr((Node *) lfirst(l), context, false);
get_rule_expr((Node *) linitial(tablesample->args), context, false);
appendStringInfoChar(buf, '%');
}
else
{
nargs = 0;
foreach(l, tablesample->args)
{
if (nargs++ > 0)
appendStringInfoString(buf, ", ");
get_rule_expr((Node *) lfirst(l), context, false);
}
}
appendStringInfoChar(buf, ')');

Expand Down
29 changes: 18 additions & 11 deletions src/vendor/pg_ruleutils_16.c
Original file line number Diff line number Diff line change
Expand Up @@ -11835,26 +11835,33 @@ static void
get_tablesample_def(TableSampleClause *tablesample, deparse_context *context)
{
StringInfo buf = context->buf;
Oid argtypes[1];
Oid argtypes[1] = { INTERNALOID };
char *tsm_name;
int nargs;
ListCell *l;

/*
* We should qualify the handler's function name if it wouldn't be
* resolved by lookup in the current search path.
*/
argtypes[0] = INTERNALOID;
appendStringInfo(buf, " TABLESAMPLE %s (",
generate_function_name(tablesample->tsmhandler, 1,
NIL, argtypes,
false, NULL, EXPR_KIND_NONE));
tsm_name = generate_function_name(tablesample->tsmhandler, 1, NIL, argtypes,
false, NULL, EXPR_KIND_NONE);
appendStringInfo(buf, " TABLESAMPLE %s (", tsm_name);

nargs = 0;
foreach(l, tablesample->args)
if (is_system_sampling(tsm_name, list_length(tablesample->args)))
{
if (nargs++ > 0)
appendStringInfoString(buf, ", ");
get_rule_expr((Node *) lfirst(l), context, false);
get_rule_expr((Node *) linitial(tablesample->args), context, false);
appendStringInfoChar(buf, '%');
}
else
{
nargs = 0;
foreach(l, tablesample->args)
{
if (nargs++ > 0)
appendStringInfoString(buf, ", ");
get_rule_expr((Node *) lfirst(l), context, false);
}
}
appendStringInfoChar(buf, ')');

Expand Down
29 changes: 18 additions & 11 deletions src/vendor/pg_ruleutils_17.c
Original file line number Diff line number Diff line change
Expand Up @@ -12493,26 +12493,33 @@ static void
get_tablesample_def(TableSampleClause *tablesample, deparse_context *context)
{
StringInfo buf = context->buf;
Oid argtypes[1];
Oid argtypes[1] = { INTERNALOID };
char *tsm_name;
int nargs;
ListCell *l;

/*
* We should qualify the handler's function name if it wouldn't be
* resolved by lookup in the current search path.
*/
argtypes[0] = INTERNALOID;
appendStringInfo(buf, " TABLESAMPLE %s (",
generate_function_name(tablesample->tsmhandler, 1,
NIL, argtypes,
false, NULL, EXPR_KIND_NONE));
tsm_name = generate_function_name(tablesample->tsmhandler, 1, NIL, argtypes,
false, NULL, EXPR_KIND_NONE);
appendStringInfo(buf, " TABLESAMPLE %s (", tsm_name);

nargs = 0;
foreach(l, tablesample->args)
if (is_system_sampling(tsm_name, list_length(tablesample->args)))
{
if (nargs++ > 0)
appendStringInfoString(buf, ", ");
get_rule_expr((Node *) lfirst(l), context, false);
get_rule_expr((Node *) linitial(tablesample->args), context, false);
appendStringInfoChar(buf, '%');
}
else
{
nargs = 0;
foreach(l, tablesample->args)
{
if (nargs++ > 0)
appendStringInfoString(buf, ", ");
get_rule_expr((Node *) lfirst(l), context, false);
}
}
appendStringInfoChar(buf, ')');

Expand Down
19 changes: 19 additions & 0 deletions test/regression/expected/tablesample.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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);
a
----
1
2
3
4
5
6
7
8
9
10
(10 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 @@ -29,3 +29,4 @@ test: function
test: timestamp_with_interval
test: approx_count_distinct
test: scan_postgres_tables
test: tablesample
7 changes: 7 additions & 0 deletions test/regression/sql/tablesample.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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);
DROP TABLE t;