Skip to content

Support CTE/JOIN usage for PIVOT operator #299

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

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
10 changes: 9 additions & 1 deletion src/backend/executor/execSRF.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "funcapi.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "parser/parser.h"
#include "parser/parse_coerce.h"
#include "pgstat.h"
#include "utils/acl.h"
Expand All @@ -33,6 +34,8 @@
#include "utils/memutils.h"
#include "utils/typcache.h"

/* Hook to set TSQL pivot data to fcinfo */
pass_pivot_data_to_fcinfo_hook_type pass_pivot_data_to_fcinfo_hook = NULL;

/* static function decls */
static void init_sexpr(Oid foid, Oid input_collation, Expr *node,
Expand Down Expand Up @@ -202,7 +205,12 @@ ExecMakeTableFunctionResult(SetExprState *setexpr,
/* Treat setexpr as a generic expression */
InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
}


if (sql_dialect == SQL_DIALECT_TSQL && pass_pivot_data_to_fcinfo_hook)
{
(pass_pivot_data_to_fcinfo_hook)(fcinfo, setexpr->expr);
}

/*
* Switch to short-lived context for calling the function or expression.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/backend/nodes/makefuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ makeFuncExpr(Oid funcid, Oid rettype, List *args,
funcexpr->inputcollid = inputcollid;
funcexpr->args = args;
funcexpr->location = -1;
funcexpr->context = NULL;

return funcexpr;
}
Expand Down Expand Up @@ -600,6 +601,7 @@ makeFuncCall(List *name, List *args, CoercionForm funcformat, int location)
n->func_variadic = false;
n->funcformat = funcformat;
n->location = location;
n->context = NULL;
return n;
}

Expand Down
3 changes: 3 additions & 0 deletions src/backend/optimizer/util/clauses.c
Original file line number Diff line number Diff line change
Expand Up @@ -2601,6 +2601,7 @@ eval_const_expressions_mutator(Node *node,
newexpr->inputcollid = expr->inputcollid;
newexpr->args = args;
newexpr->location = expr->location;
newexpr->context = expr->context;
return (Node *) newexpr;
}
case T_OpExpr:
Expand Down Expand Up @@ -4562,6 +4563,7 @@ evaluate_function(Oid funcid, Oid result_type, int32 result_typmod,
newexpr->inputcollid = input_collid;
newexpr->args = args;
newexpr->location = -1;
newexpr->context = NULL;

return evaluate_expr((Expr *) newexpr, result_type, result_typmod,
result_collid);
Expand Down Expand Up @@ -4674,6 +4676,7 @@ inline_function(Oid funcid, Oid result_type, Oid result_collid,
fexpr->inputcollid = input_collid;
fexpr->args = args;
fexpr->location = -1;
fexpr->context = NULL;

/* Fetch the function body */
tmp = SysCacheGetAttrNotNull(PROCOID, func_tuple, Anum_pg_proc_prosrc);
Expand Down
2 changes: 1 addition & 1 deletion src/backend/parser/analyze.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pre_transform_setop_tree_hook_type pre_transform_setop_tree_hook = NULL;
/* Hook to reset a query's targetlist after modification in pre_transfrom_sort_clause */
pre_transform_setop_sort_clause_hook_type pre_transform_setop_sort_clause_hook = NULL;

/* Hooks for transform TSQL pivot clause in select stmt */
/* Hook to transform TSQL pivot clause in select stmt */
transform_pivot_clause_hook_type transform_pivot_clause_hook = NULL;

static Query *transformOptionalSelectInto(ParseState *pstate, Node *parseTree);
Expand Down
4 changes: 4 additions & 0 deletions src/backend/parser/parse_func.c
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,10 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs,
/* funccollid and inputcollid will be set by parse_collate.c */
funcexpr->args = fargs;
funcexpr->location = location;
funcexpr->context = NULL;

if (fn != NULL)
funcexpr->context = copyObject(fn->context);

retval = (Node *) funcexpr;
}
Expand Down
2 changes: 2 additions & 0 deletions src/include/executor/executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,8 @@ extern Datum ExecMakeFunctionResultSet(SetExprState *fcache,
MemoryContext argContext,
bool *isNull,
ExprDoneCond *isDone);
typedef void (*pass_pivot_data_to_fcinfo_hook_type)(FunctionCallInfo fcinfo, Expr *expr);
extern PGDLLEXPORT pass_pivot_data_to_fcinfo_hook_type pass_pivot_data_to_fcinfo_hook;

/*
* prototypes from functions in execScan.c
Expand Down
1 change: 1 addition & 0 deletions src/include/nodes/parsenodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ typedef struct FuncCall
bool func_variadic; /* last argument was labeled VARIADIC */
CoercionForm funcformat; /* how to display this node */
int location; /* token location, or -1 if unknown */
Node *context; /* pass necessary info through planner and executor */
} FuncCall;

/*
Expand Down
2 changes: 2 additions & 0 deletions src/include/nodes/primnodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,8 @@ typedef struct FuncExpr
List *args;
/* token location, or -1 if unknown */
int location;
/* pass necessary info through planner and executor */
Node *context pg_node_attr(query_jumble_ignore, read_write_ignore, read_as(NULL));
} FuncExpr;

/*
Expand Down
Loading