diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c index 7faeefabf86..5862cc8e86e 100644 --- a/src/backend/parser/analyze.c +++ b/src/backend/parser/analyze.c @@ -88,6 +88,9 @@ 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 */ +transform_pivot_clause_hook_type transform_pivot_clause_hook = NULL; + static Query *transformOptionalSelectInto(ParseState *pstate, Node *parseTree); static Query *transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt); static Query *transformInsertStmt(ParseState *pstate, InsertStmt *stmt); @@ -120,7 +123,6 @@ static void transformLockingClause(ParseState *pstate, Query *qry, static bool test_raw_expression_coverage(Node *node, void *context); #endif - /* * parse_analyze_fixedparams * Analyze a raw parse tree and transform it to Query form. @@ -1365,7 +1367,6 @@ count_rowexpr_columns(ParseState *pstate, Node *expr) return -1; } - /* * transformSelectStmt - * transforms a Select Statement @@ -1404,6 +1405,11 @@ transformSelectStmt(ParseState *pstate, SelectStmt *stmt) /* make WINDOW info available for window functions, too */ pstate->p_windowdefs = stmt->windowClause; + if (stmt->isPivot && transform_pivot_clause_hook) + { + (*transform_pivot_clause_hook)(pstate, stmt); + } + /* process the FROM clause */ transformFromClause(pstate, stmt->fromClause); diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index cb973f63e9d..843ab3fd5cc 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -2047,6 +2047,14 @@ typedef struct SelectStmt struct SelectStmt *larg; /* left child */ struct SelectStmt *rarg; /* right child */ /* Eventually add fields for CORRESPONDING spec here */ + + /* These fields are used only in SelectStmt with PIVOT keyword */ + bool isPivot; + struct SelectStmt *srcSql; + struct SelectStmt *catSql; + List *value_col_strlist; + char *pivotCol; + Node *aggFunc; } SelectStmt; diff --git a/src/include/parser/analyze.h b/src/include/parser/analyze.h index b7fb76eaaaa..c165c929ef0 100644 --- a/src/include/parser/analyze.h +++ b/src/include/parser/analyze.h @@ -64,6 +64,10 @@ extern PGDLLIMPORT pre_transform_setop_tree_hook_type pre_transform_setop_tree_h typedef void (*pre_transform_setop_sort_clause_hook_type) (ParseState *pstate, Query *qry, List *sortClause, Query *leftmostQuery); extern PGDLLIMPORT pre_transform_setop_sort_clause_hook_type pre_transform_setop_sort_clause_hook; +/* Hook for transform pivot clause in tsql select stmt */ +typedef void (*transform_pivot_clause_hook_type)(ParseState *pstate, SelectStmt *stmt); +extern PGDLLIMPORT transform_pivot_clause_hook_type transform_pivot_clause_hook; + extern Query *parse_analyze_fixedparams(RawStmt *parseTree, const char *sourceText, const Oid *paramTypes, int numParams, QueryEnvironment *queryEnv); extern Query *parse_analyze(RawStmt *parseTree, const char *sourceText,