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 4 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
9 changes: 9 additions & 0 deletions 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 Down Expand Up @@ -203,6 +204,14 @@ ExecMakeTableFunctionResult(SetExprState *setexpr,
InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
}

/* if current FuncExpr is a bbf_pivot function, we set the fcinfo context to pivot data */
if (sql_dialect == SQL_DIALECT_TSQL && IsA(setexpr->expr, FuncExpr)
&& ((FuncExpr*) setexpr->expr)->pivot_parsetree != NIL
&& ((FuncExpr*) setexpr->expr)->pivot_extrainfo != NIL)
{
fcinfo->context = (Node *) list_make2(((FuncExpr*) setexpr->expr)->pivot_parsetree, ((FuncExpr*) setexpr->expr)->pivot_extrainfo);
}

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

return funcexpr;
}
Expand Down Expand Up @@ -600,6 +602,8 @@ makeFuncCall(List *name, List *args, CoercionForm funcformat, int location)
n->func_variadic = false;
n->funcformat = funcformat;
n->location = location;
n->pivot_parsetree = NIL;
n->pivot_extrainfo = NIL;
return n;
}

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

return evaluate_expr((Expr *) newexpr, result_type, result_typmod,
result_collid);
Expand Down Expand Up @@ -4674,6 +4678,8 @@ inline_function(Oid funcid, Oid result_type, Oid result_collid,
fexpr->inputcollid = input_collid;
fexpr->args = args;
fexpr->location = -1;
fexpr->pivot_parsetree = NIL;
fexpr->pivot_extrainfo = NIL;

/* Fetch the function body */
tmp = SysCacheGetAttrNotNull(PROCOID, func_tuple, Anum_pg_proc_prosrc);
Expand Down
11 changes: 11 additions & 0 deletions src/backend/parser/parse_func.c
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,17 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs,
/* funccollid and inputcollid will be set by parse_collate.c */
funcexpr->args = fargs;
funcexpr->location = location;
if (fn != NULL)
{
funcexpr->pivot_parsetree = copyObject(fn->pivot_parsetree);
funcexpr->pivot_extrainfo = copyObject(fn->pivot_extrainfo);
}
else
{
funcexpr->pivot_parsetree = NIL;
funcexpr->pivot_extrainfo = NIL;
}


retval = (Node *) funcexpr;
}
Expand Down
1 change: 1 addition & 0 deletions src/include/fmgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#ifndef FMGR_H
#define FMGR_H

#include "nodes/pg_list.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fmgr.h is a header file used by execSRF.c. And List hasn't been used in execSRF.c until we added our pivot logic. So we need to include this to fmgr.h

/* We don't want to include primnodes.h here, so make some stub references */
typedef struct Node *fmNodePtr;
typedef struct Aggref *fmAggrefPtr;
Expand Down
2 changes: 2 additions & 0 deletions src/include/nodes/parsenodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,8 @@ 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 */
List *pivot_parsetree; /* bbf_pivot function required rewritted parsetrees */
List *pivot_extrainfo; /* bbf_pivot function required aggregation function and source sql string */
} FuncCall;

/*
Expand Down
4 changes: 4 additions & 0 deletions src/include/nodes/primnodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,10 @@ typedef struct FuncExpr
List *args;
/* token location, or -1 if unknown */
int location;
/* bbf_pivot function required rewritted parsetrees */
List *pivot_parsetree pg_node_attr(query_jumble_ignore, read_write_ignore, read_as(NULL));
/* bbf_pivot function required aggregation function name and source sql string */
List *pivot_extrainfo pg_node_attr(query_jumble_ignore, read_write_ignore, read_as(NULL));
} FuncExpr;

/*
Expand Down