Skip to content

Commit

Permalink
Support TSQL PIVOT operator (babelfish-for-postgresql#165)
Browse files Browse the repository at this point in the history
Add support for PIVOT in babelfish

Task: BABEL-284
Signed-off-by: Yanjie Xu <[email protected]>
  • Loading branch information
RIC06X authored and priyansx committed Nov 22, 2023
1 parent a0bb316 commit d807167
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/backend/parser/analyze.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -1365,7 +1367,6 @@ count_rowexpr_columns(ParseState *pstate, Node *expr)
return -1;
}


/*
* transformSelectStmt -
* transforms a Select Statement
Expand Down Expand Up @@ -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);

Expand Down
8 changes: 8 additions & 0 deletions src/include/nodes/parsenodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down
4 changes: 4 additions & 0 deletions src/include/parser/analyze.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d807167

Please sign in to comment.