Skip to content
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

Support TSQL PIVOT operator #165

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: 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 @@ -1753,6 +1753,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;
Comment on lines +1758 to +1763
Copy link
Contributor

@lejaokri lejaokri Oct 27, 2023

Choose a reason for hiding this comment

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

Since you are adding in new fields to SelectStmt please refer to the following fix by @timchang514 where he fixed a problem with uninitialized value when copying these types of structs. Please check if you need to do the same.

commit 26b4e995d4b69e934c511019ee2175a31227c56a
Author: Tim Chang <[email protected]>
Date:   Mon Sep 25 15:14:17 2023 -0400

    Add new fields to IntoClause funcs to avoid issues with uninitialized memory. (#219)

    Signed-off-by: Tim Chang <[email protected]>

Also, I found your branch based on a really old commit. You may need to rebase.

} 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
Loading