Skip to content

Commit

Permalink
Add PG15 support
Browse files Browse the repository at this point in the history
This applies changes to pg_ruleutils_15.c that are identical to the ones
that we already made to the PG16 and PG17 versions of these files.

It also notably removes tests involving the psql \bind meta-command,
because those meta-commands are unsupported by PG15 its psql. This is
acceptable because we

Other than that this mainly adds a bunch of #if PG_VERSION_NUM >= 160000
preprocessor statements in places where APIs diverge between versions.
  • Loading branch information
Leo-XM-Zeng authored and JelteF committed Sep 30, 2024
1 parent 61fa688 commit e889d9a
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_and_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
version: [REL_16_STABLE, REL_17_STABLE]
version: [REL_15_STABLE, REL_16_STABLE, REL_17_STABLE]
runs-on: ${{ matrix.os }}

steps:
Expand Down
2 changes: 2 additions & 0 deletions src/pgduckdb_detoast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
extern "C" {
#include "postgres.h"
#include "pg_config.h"
#if PG_VERSION_NUM >= 160000
#include "varatt.h"
#endif

#ifdef USE_LZ4
#include <lz4.h>
Expand Down
12 changes: 12 additions & 0 deletions src/pgduckdb_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ ContainsDuckdbFunctions(Node *node, void *context) {

if (IsA(node, Query)) {
Query *query = (Query *)node;
#if PG_VERSION_NUM >= 160000
return query_tree_walker(query, ContainsDuckdbFunctions, context, 0);
#else
return query_tree_walker(query, (bool (*)()) ((void *) ContainsDuckdbFunctions), context, 0);
#endif
}

if (IsA(node, FuncExpr)) {
Expand All @@ -62,12 +66,20 @@ ContainsDuckdbFunctions(Node *node, void *context) {
}
}

#if PG_VERSION_NUM >= 160000
return expression_tree_walker(node, ContainsDuckdbFunctions, context);
#else
return expression_tree_walker(node, (bool (*)()) ((void *) ContainsDuckdbFunctions), context);
#endif
}

static bool
NeedsDuckdbExecution(Query *query) {
#if PG_VERSION_NUM >= 160000
return query_tree_walker(query, ContainsDuckdbFunctions, NULL, 0);
#else
return query_tree_walker(query, (bool (*)()) ((void *) ContainsDuckdbFunctions), NULL, 0);
#endif
}

static bool
Expand Down
4 changes: 4 additions & 0 deletions src/pgduckdb_planner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ PlanQuery(Query *parse, ParamListInfo bound_params) {
glob->subroots = NIL;
glob->rewindPlanIDs = NULL;
glob->finalrtable = NIL;
#if PG_VERSION_NUM >= 160000
glob->finalrteperminfos = NIL;
#endif
glob->finalrowmarks = NIL;
glob->resultRelations = NIL;
glob->appendRelations = NIL;
Expand Down Expand Up @@ -171,7 +173,9 @@ DuckdbPlanNode(Query *parse, int cursor_options, ParamListInfo bound_params) {
result->parallelModeNeeded = false;
result->planTree = duckdb_plan;
result->rtable = NULL;
#if PG_VERSION_NUM >= 160000
result->permInfos = NULL;
#endif
result->resultRelations = NULL;
result->appendRelations = NULL;
result->subplans = NIL;
Expand Down
25 changes: 24 additions & 1 deletion src/utility/copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ static constexpr char r2_filename_prefix[] = "r2://";
static bool
CreateRelationCopyParseState(ParseState *pstate, const CopyStmt *stmt, List **vars, int stmt_location, int stmt_len) {
ParseNamespaceItem *nsitem;
#if PG_VERSION_NUM >= 160000
RTEPermissionInfo *perminfo;
#else
RangeTblEntry *rte;
#endif
TupleDesc tuple_desc;
List *attnums;
Relation rel;
Expand All @@ -43,12 +47,18 @@ CreateRelationCopyParseState(ParseState *pstate, const CopyStmt *stmt, List **va

nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, false);

#if PG_VERSION_NUM >= 160000
perminfo = nsitem->p_perminfo;
perminfo->requiredPerms = ACL_SELECT;
#else
rte = nsitem->p_rte;
rte->requiredPerms = ACL_SELECT;
#endif

tuple_desc = RelationGetDescr(rel);
attnums = CopyGetAttnums(tuple_desc, rel, stmt->attlist);

#if PG_VERSION_NUM >= 160000
foreach_int(cur, attnums) {
int attno;
Bitmapset **bms;
Expand All @@ -59,12 +69,25 @@ CreateRelationCopyParseState(ParseState *pstate, const CopyStmt *stmt, List **va
lappend(*vars, makeVar(1, cur, tuple_desc->attrs[cur - 1].atttypid, tuple_desc->attrs[cur - 1].atttypmod,
tuple_desc->attrs[cur - 1].attcollation, 0));
}
#else
foreach_int(cur, attnums)
{
int attno = cur - FirstLowInvalidHeapAttributeNumber;
rte->selectedCols = bms_add_member(rte->selectedCols, attno);
}
#endif

#if PG_VERSION_NUM >= 160000
if (!ExecCheckPermissions(pstate->p_rtable, list_make1(perminfo), false)) {
ereport(WARNING, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("(Duckdb) Failed Permission \"%s\"", RelationGetRelationName(rel))));
}

#else
if (!ExecCheckRTPerms(pstate->p_rtable, true)) {
ereport(WARNING, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("(Duckdb) Failed Permission \"%s\"", RelationGetRelationName(rel))));
}
#endif
table_close(rel, AccessShareLock);

/*
Expand Down
23 changes: 16 additions & 7 deletions src/vendor/pg_ruleutils_15.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
/*-------------------------------------------------------------------------
*
* ruleutils.c
* pg_ruleutils_15.c
* Functions to convert stored expressions/querytrees back to
* source text
*
* Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* src/backend/utils/adt/ruleutils.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"

#if PG_VERSION_NUM >= 150000 && PG_VERSION_NUM < 160000

#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
Expand Down Expand Up @@ -67,13 +65,18 @@
#include "utils/lsyscache.h"
#include "utils/partcache.h"
#include "utils/rel.h"
#include "utils/ruleutils.h"
#include "pgduckdb/vendor/pg_ruleutils.h"
#include "utils/snapmgr.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
#include "utils/varlena.h"
#include "utils/xml.h"

#include "pgduckdb/pgduckdb_ruleutils.h"

#include "pgduckdb/utility/rename_ruleutils.h"


/* ----------
* Pretty formatting constants
* ----------
Expand All @@ -94,7 +97,7 @@
/* Standard conversion of a "bool pretty" option to detailed flags */
#define GET_PRETTY_FLAGS(pretty) \
((pretty) ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) \
: PRETTYFLAG_INDENT)
: 0)

/* Default line length for pretty-print wrapping: 0 means wrap always */
#define WRAP_COLUMN_DEFAULT 0
Expand Down Expand Up @@ -11994,6 +11997,10 @@ generate_function_name(Oid funcid, int nargs, List *argnames, Oid *argtypes,
Oid *p_true_typeids;
bool force_qualify = false;

result = pgduckdb_function_name(funcid);
if (result)
return result;

proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
if (!HeapTupleIsValid(proctup))
elog(ERROR, "cache lookup failed for function %u", funcid);
Expand Down Expand Up @@ -12429,3 +12436,5 @@ get_range_partbound_string(List *bound_datums)

return buf->data;
}

#endif
22 changes: 0 additions & 22 deletions test/regression/expected/basic.out
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,6 @@ SELECT a, COUNT(*) FROM t WHERE a > 5 GROUP BY a ORDER BY a;
9 | 100
(4 rows)

select COUNT(*) from t \bind \g
count
-------
1000
(1 row)

select a, COUNT(*) from t WHERE a > $1 GROUP BY a ORDER BY a \bind 5 \g
a | count
---+-------
6 | 100
7 | 100
8 | 100
9 | 100
(4 rows)

\bind 7 \g
a | count
---+-------
8 | 100
9 | 100
(2 rows)

SET duckdb.max_threads_per_query to 4;
SELECT COUNT(*) FROM t;
count
Expand Down
2 changes: 1 addition & 1 deletion test/regression/expected/type_support.out
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ SELECT * FROM json_tbl;

-- REGCLASSOID
CREATE TABLE regclass_tbl (a REGCLASS);
INSERT INTO regclass_tbl VALUES (42), (3_000_000_000);
INSERT INTO regclass_tbl VALUES (42), (3000000000);
SELECT * FROM regclass_tbl;
a
------------
Expand Down
3 changes: 0 additions & 3 deletions test/regression/sql/basic.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ SET client_min_messages to 'DEBUG1';

SELECT COUNT(*) FROM t;
SELECT a, COUNT(*) FROM t WHERE a > 5 GROUP BY a ORDER BY a;
select COUNT(*) from t \bind \g
select a, COUNT(*) from t WHERE a > $1 GROUP BY a ORDER BY a \bind 5 \g
\bind 7 \g

SET duckdb.max_threads_per_query to 4;

Expand Down
2 changes: 1 addition & 1 deletion test/regression/sql/type_support.sql
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ SELECT * FROM json_tbl;

-- REGCLASSOID
CREATE TABLE regclass_tbl (a REGCLASS);
INSERT INTO regclass_tbl VALUES (42), (3_000_000_000);
INSERT INTO regclass_tbl VALUES (42), (3000000000);
SELECT * FROM regclass_tbl;

DROP TABLE chr;
Expand Down

0 comments on commit e889d9a

Please sign in to comment.