Skip to content

WIP: Support PostgreSQL 14 #272

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

Open
wants to merge 2 commits into
base: master
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,6 @@ ifeq (${SUPPORTS_IMPORT}, 1)
endif

REGRESS = $(patsubst test-$(PYTHON_TEST_VERSION)/sql/%.sql,%,$(TESTS))
REGRESS_OPTS = --inputdir=test-$(PYTHON_TEST_VERSION) --load-language=plpgsql
REGRESS_OPTS = --inputdir=test-$(PYTHON_TEST_VERSION)

$(info Python version is $(python_version))
14 changes: 13 additions & 1 deletion src/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,17 @@ reportException(PyObject *pErrType, PyObject *pErrValue, PyObject *pErrTraceback
{
severity = ERROR;
}
#if PG_VERSION_NUM >= 130000
if (errstart(severity, TEXTDOMAIN))
#else
if (errstart(severity, __FILE__, __LINE__, PG_FUNCNAME_MACRO, TEXTDOMAIN))
#endif
{
#if PG_VERSION_NUM >= 130000
if (errstart(severity, TEXTDOMAIN))
#else
if (errstart(severity, __FILE__, __LINE__, PG_FUNCNAME_MACRO, TEXTDOMAIN))
#endif
errmsg("Error in python: %s", errName);
errdetail("%s", errValue);
errdetail_log("%s", errTraceback);
Expand All @@ -81,5 +89,9 @@ reportException(PyObject *pErrType, PyObject *pErrValue, PyObject *pErrTraceback
Py_DECREF(tracebackModule);
Py_DECREF(newline);
Py_DECREF(pTemp);
errfinish(0);
#if PG_VERSION_NUM >= 130000
errfinish(__FILE__, __LINE__, PG_FUNCNAME_MACRO);
#else
errfinish(0);
#endif
}
38 changes: 32 additions & 6 deletions src/multicorn.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
#include "utils/rel.h"
#include "parser/parsetree.h"
#include "fmgr.h"
#if PG_VERSION_NUM >= 130000
#include "common/hashfn.h" /* oid_hash */
#endif


PG_MODULE_MAGIC;
Expand Down Expand Up @@ -70,7 +73,13 @@ static void multicornReScanForeignScan(ForeignScanState *node);
static void multicornEndForeignScan(ForeignScanState *node);

#if PG_VERSION_NUM >= 90300
static void multicornAddForeignUpdateTargets(Query *parsetree,
static void multicornAddForeignUpdateTargets(
#if PG_VERSION_NUM >= 140000
PlannerInfo *root,
Index rtindex,
#else
Query *parsetree,
#endif
RangeTblEntry *target_rte,
Relation target_relation);

Expand Down Expand Up @@ -311,7 +320,7 @@ multicornGetForeignRelSize(PlannerInfo *root,
/* Extract the restrictions from the plan. */
foreach(lc, baserel->baserestrictinfo)
{
extractRestrictions(baserel->relids, ((RestrictInfo *) lfirst(lc))->clause,
extractRestrictions(root, baserel->relids, ((RestrictInfo *) lfirst(lc))->clause,
&planstate->qual_list);

}
Expand Down Expand Up @@ -444,7 +453,7 @@ multicornGetForeignPlan(PlannerInfo *root,

foreach(lc, scan_clauses)
{
extractRestrictions(baserel->relids, (Expr *) lfirst(lc),
extractRestrictions(root, baserel->relids, (Expr *) lfirst(lc),
&planstate->qual_list);
}
}
Expand Down Expand Up @@ -503,12 +512,15 @@ multicornBeginForeignScan(ForeignScanState *node, int eflags)
execstate->values = palloc(sizeof(Datum) * tupdesc->natts);
execstate->nulls = palloc(sizeof(bool) * tupdesc->natts);
execstate->qual_list = NULL;
#if PG_VERSION_NUM < 140000
foreach(lc, fscan->fdw_exprs)
{
extractRestrictions(bms_make_singleton(fscan->scan.scanrelid),
extractRestrictions(NULL, /* FIXME: set this properly. Parameter is used on PG14+ only, we can use NULL on older versions */
bms_make_singleton(fscan->scan.scanrelid),
((Expr *) lfirst(lc)),
&execstate->qual_list);
}
#endif
initConversioninfo(execstate->cinfos, TupleDescGetAttInMetadata(tupdesc));
node->fdw_state = execstate;
}
Expand Down Expand Up @@ -598,7 +610,13 @@ multicornEndForeignScan(ForeignScanState *node)
* Add resjunk columns needed for update/delete.
*/
static void
multicornAddForeignUpdateTargets(Query *parsetree,
multicornAddForeignUpdateTargets(
#if PG_VERSION_NUM >= 140000
PlannerInfo *root,
Index rtindex,
#else
Query *parsetree,
#endif
RangeTblEntry *target_rte,
Relation target_relation)
{
Expand All @@ -610,6 +628,9 @@ multicornAddForeignUpdateTargets(Query *parsetree,
TupleDesc desc = target_relation->rd_att;
int i;
ListCell *cell;
#if PG_VERSION_NUM >= 140000
Query *parsetree = root->parse;
#endif

foreach(cell, parsetree->returningList)
{
Expand Down Expand Up @@ -681,7 +702,12 @@ multicornBeginForeignModify(ModifyTableState *mtstate,
MulticornModifyState *modstate = palloc0(sizeof(MulticornModifyState));
Relation rel = resultRelInfo->ri_RelationDesc;
TupleDesc desc = RelationGetDescr(rel);
PlanState *ps = mtstate->mt_plans[subplan_index];
PlanState *ps =
#if PG_VERSION_NUM >= 140000
outerPlanState(mtstate);
#else
mtstate->mt_plans[subplan_index];
#endif
Plan *subplan = ps->plan;
MemoryContext oldcontext;
int i;
Expand Down
3 changes: 2 additions & 1 deletion src/multicorn.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ extern PGDLLIMPORT HTAB *InstancesHash;


/* query.c */
void extractRestrictions(Relids base_relids,
void extractRestrictions(PlannerInfo *root,
Relids base_relids,
Expr *node,
List **quals);
List *extractColumns(List *reltargetlist, List *restrictinfolist);
Expand Down
15 changes: 15 additions & 0 deletions src/python.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,22 +524,37 @@ compareColumns(List *columns1, List *columns2)
{
return false;
}
#if PG_VERSION_NUM >= 130000
cell1 = lnext(coldef1, cell1);
cell2 = lnext(coldef2, cell2);
#else
cell1 = lnext(cell1);
cell2 = lnext(cell2);
#endif
/* Compare typoid */
if (((Const *) (lfirst(cell1)))->constvalue != ((Const *) lfirst(cell2))->constvalue)
{
return false;
}
#if PG_VERSION_NUM >= 130000
cell1 = lnext(coldef1, cell1);
cell2 = lnext(coldef2, cell2);
#else
cell1 = lnext(cell1);
cell2 = lnext(cell2);
#endif
/* Compare typmod */
if (((Const *) (lfirst(cell1)))->constvalue != ((Const *) lfirst(cell2))->constvalue)
{
return false;
}
#if PG_VERSION_NUM >= 130000
cell1 = lnext(coldef1, cell1);
cell2 = lnext(coldef2, cell2);
#else
cell1 = lnext(cell1);
cell2 = lnext(cell2);
#endif
/* Compare column options */
if (!compareOptions(lfirst(cell1), lfirst(cell2)))
{
Expand Down
54 changes: 45 additions & 9 deletions src/query.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@
#define get_attname(x, y) get_attname(x, y, true)
#endif

void extractClauseFromOpExpr(Relids base_relids,
void extractClauseFromOpExpr(PlannerInfo *root,
Relids base_relids,
OpExpr *node,
List **quals);

void extractClauseFromNullTest(Relids base_relids,
NullTest *node,
List **quals);

void extractClauseFromScalarArrayOpExpr(Relids base_relids,
void extractClauseFromScalarArrayOpExpr(PlannerInfo *root,
Relids base_relids,
ScalarArrayOpExpr *node,
List **quals);

Expand Down Expand Up @@ -305,22 +307,23 @@ canonicalScalarArrayOpExpr(ScalarArrayOpExpr *opExpr,
*
*/
void
extractRestrictions(Relids base_relids,
extractRestrictions(PlannerInfo *root,
Relids base_relids,
Expr *node,
List **quals)
{
switch (nodeTag(node))
{
case T_OpExpr:
extractClauseFromOpExpr(base_relids,
extractClauseFromOpExpr(root, base_relids,
(OpExpr *) node, quals);
break;
case T_NullTest:
extractClauseFromNullTest(base_relids,
(NullTest *) node, quals);
break;
case T_ScalarArrayOpExpr:
extractClauseFromScalarArrayOpExpr(base_relids,
extractClauseFromScalarArrayOpExpr(root, base_relids,
(ScalarArrayOpExpr *) node,
quals);
break;
Expand All @@ -346,7 +349,8 @@ extractRestrictions(Relids base_relids,
* - Var or Const value: the value.
*/
void
extractClauseFromOpExpr(Relids base_relids,
extractClauseFromOpExpr(PlannerInfo *root,
Relids base_relids,
OpExpr *op,
List **quals)
{
Expand All @@ -363,7 +367,11 @@ extractClauseFromOpExpr(Relids base_relids,
/* Do not add it if it either contains a mutable function, or makes */
/* self references in the right hand side. */
if (!(contain_volatile_functions((Node *) right) ||
bms_is_subset(base_relids, pull_varnos((Node *) right))))
bms_is_subset(base_relids, pull_varnos(
#if PG_VERSION_NUM >= 140000
root,
#endif
(Node *) right))))
{
*quals = lappend(*quals, makeQual(left->varattno,
getOperatorString(op->opno),
Expand All @@ -373,7 +381,8 @@ extractClauseFromOpExpr(Relids base_relids,
}

void
extractClauseFromScalarArrayOpExpr(Relids base_relids,
extractClauseFromScalarArrayOpExpr(PlannerInfo *root,
Relids base_relids,
ScalarArrayOpExpr *op,
List **quals)
{
Expand All @@ -386,7 +395,11 @@ extractClauseFromScalarArrayOpExpr(Relids base_relids,
left = list_nth(op->args, 0);
right = list_nth(op->args, 1);
if (!(contain_volatile_functions((Node *) right) ||
bms_is_subset(base_relids, pull_varnos((Node *) right))))
bms_is_subset(base_relids, pull_varnos(
#if PG_VERSION_NUM >= 140000
root,
#endif
(Node *) right))))
{
*quals = lappend(*quals, makeQual(left->varattno,
getOperatorString(op->opno),
Expand Down Expand Up @@ -837,26 +850,49 @@ deserializeDeparsedSortGroup(List *items)
ListCell *lc;
MulticornDeparsedSortGroup *key =
palloc0(sizeof(MulticornDeparsedSortGroup));
#if PG_VERSION_NUM >= 130000
List *list = lfirst(k);
#endif

lc = list_head(lfirst(k));
key->attname = (Name) strdup(strVal(lfirst(lc)));

#if PG_VERSION_NUM >= 130000
lc = lnext(list, lc);
#else
lc = lnext(lc);
#endif
key->attnum = (int) intVal(lfirst(lc));

#if PG_VERSION_NUM >= 130000
lc = lnext(list, lc);
#else
lc = lnext(lc);
#endif
key->reversed = (bool) intVal(lfirst(lc));

#if PG_VERSION_NUM >= 130000
lc = lnext(list, lc);
#else
lc = lnext(lc);
#endif
key->nulls_first = (bool) intVal(lfirst(lc));

#if PG_VERSION_NUM >= 130000
lc = lnext(list, lc);
#else
lc = lnext(lc);
#endif
if(lfirst(lc) != NULL)
key->collate = (Name) strdup(strVal(lfirst(lc)));
else
key->collate = NULL;

#if PG_VERSION_NUM >= 130000
lc = lnext(list, lc);
#else
lc = lnext(lc);
#endif
key->key = (PathKey *) lfirst(lc);

result = lappend(result, key);
Expand Down
8 changes: 8 additions & 0 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ log_to_postgres(PyObject *self, PyObject *args, PyObject *kwargs)
}
hint = PyDict_GetItemString(kwargs, "hint");
detail = PyDict_GetItemString(kwargs, "detail");
#if PG_VERSION_NUM >= 130000
if (errstart(severity, TEXTDOMAIN))
#else
if (errstart(severity, __FILE__, __LINE__, PG_FUNCNAME_MACRO, TEXTDOMAIN))
#endif
{
errmsg("%s", message);
if (hint != NULL && hint != Py_None)
Expand All @@ -104,7 +108,11 @@ log_to_postgres(PyObject *self, PyObject *args, PyObject *kwargs)
}
Py_DECREF(args);
Py_DECREF(kwargs);
#if PG_VERSION_NUM >= 130000
errfinish(__FILE__, __LINE__, PG_FUNCNAME_MACRO);
#else
errfinish(0);
#endif
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions test-2.7/expected/write_filesystem.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CREATE EXTENSION multicorn;
CREATE server multicorn_srv foreign data wrapper multicorn options (
wrapper 'multicorn.fsfdw.FilesystemFdw'
);
CREATE language plpythonu;
CREATE EXTENSION plpythonu;
CREATE TABLE temp_dir (dirname varchar);
-- Create a table with the filesystem fdw in a temporary directory,
-- and store the dirname in the temp_dir table.
Expand Down Expand Up @@ -598,4 +598,4 @@ DROP EXTENSION multicorn cascade;
NOTICE: drop cascades to 2 other objects
DETAIL: drop cascades to server multicorn_srv
drop cascades to foreign table testmulticorn
DROP LANGUAGE plpythonu;
DROP EXTENSION plpythonu;
4 changes: 2 additions & 2 deletions test-2.7/sql/write_filesystem.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CREATE server multicorn_srv foreign data wrapper multicorn options (
);


CREATE language plpythonu;
CREATE EXTENSION plpythonu;
CREATE TABLE temp_dir (dirname varchar);

-- Create a table with the filesystem fdw in a temporary directory,
Expand Down Expand Up @@ -63,4 +63,4 @@ DROP FUNCTION cleanup_dir();
DROP TABLE temp_dir;
DROP FUNCTION create_table();
DROP EXTENSION multicorn cascade;
DROP LANGUAGE plpythonu;
DROP EXTENSION plpythonu;
4 changes: 2 additions & 2 deletions test-3.3/expected/write_filesystem.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- Setup the test
CREATE EXTENSION multicorn;
CREATE language plpython3u;
CREATE EXTENSION plpython3u;
\i test-common/disable_jit.include
DO $$
BEGIN
Expand Down Expand Up @@ -606,4 +606,4 @@ DROP EXTENSION multicorn cascade;
NOTICE: drop cascades to 2 other objects
DETAIL: drop cascades to server multicorn_srv
drop cascades to foreign table testmulticorn
DROP LANGUAGE plpython3u;
DROP EXTENSION plpython3u;
Loading