Skip to content

Commit

Permalink
[BABEL-284] Clean code in gram-tsql-rule.y
Browse files Browse the repository at this point in the history
Removed unused pivot rewrite code in gram-tsql-rule.y

Task: BABEL-284
Signed-off-by: Yanjie Xu <[email protected]>
  • Loading branch information
RIC06X committed Oct 13, 2023
1 parent 883163d commit 517b8a2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 114 deletions.
4 changes: 2 additions & 2 deletions contrib/babelfishpg_tsql/runtime/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -3435,7 +3435,8 @@ SPI_execute_raw_parsetree(RawStmt *parsetree, bool read_only, long tcount)
if (parsetree == NULL || tcount < 0)
return SPI_ERROR_ARGUMENT;


sql_dialect = SQL_DIALECT_TSQL;

memset(&plan, 0, sizeof(_SPI_plan));
plan.magic = _SPI_PLAN_MAGIC;
plan.parse_mode = RAW_PARSE_DEFAULT;
Expand Down Expand Up @@ -3527,7 +3528,6 @@ bbf_pivot(PG_FUNCTION_ARGS)
return (Datum) 0;
}


/*
* load up the categories hash table
*/
Expand Down
112 changes: 37 additions & 75 deletions contrib/babelfishpg_tsql/src/backend_parser/gram-tsql-rule.y
Original file line number Diff line number Diff line change
Expand Up @@ -1575,26 +1575,33 @@ simple_select:
into_clause from_clause tsql_pivot_expr alias_clause where_clause
group_clause having_clause window_clause
{

FuncCall *pivotCall;
ColumnDef *rowid_df;
TypeName *t;

ColumnRef *a_star;
ResTarget *a_star_restarget;
RangeFunction *funCallNode;

SelectStmt *s_sql;
SelectStmt *c_sql;
SelectStmt *wrapper_sl;
SelectStmt *pivot_sl;

SortBy *s;
A_Const *g_a1;
A_Const *g_a2;

Node *rowid = (Node *) list_nth($3, 0);
Node *r_col = (Node *) list_nth((List *)$6, 0);
Node *r_func = (Node *) list_nth((List *)$6, 1);
/* TODO: Need to remove r_col after lowercase bug was fixed */
/* Node *pivotCol = (Node *) list_nth((List *)$6, 0); */
ColumnRef *c = makeNode(ColumnRef);
ResTarget *pivot_col = makeNode(ResTarget);
Node *r_col = (Node *) list_nth((List *)$6, 4);
char *pivot_colstr = (char *)list_nth((List *)$6, 0);
Node *aggFunc = (Node *) list_nth((List *)$6, 1);

/* prepare category column for pivot source sql */
c->location = -1;
c->fields = list_make1(makeString(pivot_colstr));
pivot_col->name = NULL;
pivot_col->name_location = -1;
pivot_col->indirection = NIL;
pivot_col->val = (Node *) c;
pivot_col->location = -1;

/* prepare SortBy node for source sql */
s = makeNode(SortBy);
Expand All @@ -1604,10 +1611,6 @@ simple_select:
s->useOp = NIL;
s->location = -1;

/* prepare group by clause for source sql*/
g_a1 = (A_Const *) makeIntConst(1, -1);
g_a2 = (A_Const *) makeIntConst(2, -1);

/* transform to select * from funcCall as newtable(a type1, b type2 ...) */
/* create * node for select target list */
a_star = makeNode(ColumnRef);
Expand All @@ -1622,9 +1625,8 @@ simple_select:

/* prepare source sql for babelfish_pivot function */
s_sql = makeNode(SelectStmt);
s_sql->targetList = list_make3(rowid, r_col, r_func);
s_sql->targetList = list_make1(a_star_restarget);
s_sql->fromClause = $5;
s_sql->groupClause = list_make2(g_a1, g_a2);
s_sql->sortClause = list_make1(s);

/* prepare category sql for babelfish_pivot function */
Expand All @@ -1635,47 +1637,23 @@ simple_select:
c_sql->whereClause = list_nth((List *)$6, 2);

/* create a function call node for the fromClause */
funCallNode = makeNode(RangeFunction);
pivotCall = makeFuncCall(TsqlSystemFuncName2("bbf_pivot"),NIL, COERCE_EXPLICIT_CALL, -1);
funCallNode = makeNode(RangeFunction);
funCallNode->lateral = false;
funCallNode->is_rowsfrom = false;
funCallNode->functions = list_make1(list_make2((Node *) pivotCall, NIL));

/* prepare for alias clause */
/* prepare the column list for alias clause */
/* TODO: add 0 - 2+ non pivot columns support */
rowid_df = makeNode(ColumnDef);
/* TODO: add support for other data type */
t = SystemTypeName("int4");
t->location = -1;
/* rowid_df->colname = ((String *)((List *)((ColumnRef)((ResTarget *)rowid)->val)->field)->elements[0])->sval; */
rowid_df->colname = "venderid";
rowid_df->typeName = t;
rowid_df->inhcount = 0;
rowid_df->is_local = true;
rowid_df->is_not_null = false;
rowid_df->is_from_type = false;
rowid_df->storage = 0;
rowid_df->raw_default = NULL;
rowid_df->cooked_default = NULL;
rowid_df->collOid = InvalidOid;
rowid_df->location = -1;

/* alias_clause */
funCallNode->alias = $7;
/* column definiation for new pivot table */
/* TODO: change the following to list_concat after support 0 - 2+ non pivot columns */
funCallNode->coldeflist = list_insert_nth((List *) list_nth((List *)$6, 3), 0, rowid_df);

wrapper_sl = makeNode(SelectStmt);
wrapper_sl->targetList = list_make1(a_star_restarget);
// wrapper_sl->targetList = $3;
wrapper_sl->fromClause = list_make1(funCallNode);
wrapper_sl->larg = s_sql;
wrapper_sl->rarg = c_sql;
wrapper_sl->isPivot = true;

$$ = (Node *)wrapper_sl;
pivot_sl = makeNode(SelectStmt);
pivot_sl->targetList = $3;
pivot_sl->fromClause = list_make1(funCallNode);
pivot_sl->isPivot = true;
pivot_sl->srcSql = s_sql;
pivot_sl->catSql = c_sql;
pivot_sl->pivotCol = pivot_colstr;
pivot_sl->aggFunc = aggFunc;
pivot_sl->value_col_strlist = (List *) list_nth((List *)$6, 3);;
$$ = (Node *)pivot_sl;
}
| tsql_values_clause { $$ = $1; }
;
Expand All @@ -1685,12 +1663,12 @@ tsql_pivot_expr: TSQL_PIVOT '(' func_application FOR ColId IN_P in_expr ')'
A_Expr *where_clause;
List *ret;
List *col_list = NULL;
List *coldf_list = NULL;
List *value_col_strlist = NULL;

ColumnRef *c = makeNode(ColumnRef);
ResTarget *r_col = makeNode(ResTarget);
ResTarget *r_func = makeNode(ResTarget);

/* prepare category column for pivot source sql */
c->location = -1;
c->fields = list_make1(makeString($5));
r_col->name = NULL;
Expand All @@ -1712,35 +1690,19 @@ tsql_pivot_expr: TSQL_PIVOT '(' func_application FOR ColId IN_P in_expr ')'
String *s = list_nth(tempRef->fields, 0);
Node *n = makeStringConst(s->sval, -1);

//prepare for the alias clause
ColumnDef *coldf = makeNode(ColumnDef);
TypeName *t = SystemTypeName("int4");
t->location = -1;

coldf->colname = s->sval;
coldf->typeName = t;
coldf->inhcount = 0;
coldf->is_local = true;
coldf->is_not_null = false;
coldf->is_from_type = false;
coldf->storage = 0;
coldf->raw_default = NULL;
coldf->cooked_default = NULL;
coldf->collOid = InvalidOid;
coldf->location = -1;

if (col_list == NULL || coldf_list == NULL){
if (col_list == NULL || value_col_strlist == NULL){
col_list = list_make1(n);
coldf_list = list_make1(coldf);
value_col_strlist = list_make1(s->sval);
}else{
col_list = lappend(col_list, n);
coldf_list = lappend(coldf_list, coldf);
value_col_strlist = lappend(value_col_strlist, s->sval);
}
}
}
where_clause = makeSimpleA_Expr(AEXPR_IN, "=",(Node *) c,(Node *) col_list, -1);
ret = list_make4(r_col, r_func, (Node *)where_clause, coldf_list);

ret = list_make4($5, r_func, (Node *)where_clause, value_col_strlist);
/* TODO: Need to remove r_col after lowercase bug was fixed */
ret = lappend(ret, r_col);
$$ = (Node*) ret;
}
;
Expand Down
37 changes: 0 additions & 37 deletions contrib/babelfishpg_tsql/src/pl_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,6 @@ static validate_set_config_function_hook_type prev_validate_set_config_function_
static void pltsql_guc_push_old_value(struct config_generic *gconf, GucAction action);
bool current_query_is_create_tbl_check_constraint = false;

RawStmt *bbf_pivot_sql1;
RawStmt *bbf_pivot_sql2;

/* Configurations */
bool pltsql_trace_tree = false;
bool pltsql_trace_exec_codes = false;
Expand Down Expand Up @@ -593,40 +590,6 @@ pltsql_pre_parse_analyze(ParseState *pstate, RawStmt *parseTree)
}
}

if (parseTree->stmt->type == T_SelectStmt)
{
SelectStmt *selectStmt;

selectStmt = (SelectStmt *) parseTree->stmt;
if (selectStmt->isPivot){
RawStmt *s_sql;
RawStmt *c_sql;
MemoryContext oldContext = CurrentMemoryContext;
MemoryContextSwitchTo(TopMemoryContext);
/* save rewrited sqls to global variable for later retrive */
s_sql = makeNode(RawStmt);
c_sql = makeNode(RawStmt);

s_sql->stmt = (Node *) selectStmt->larg;
s_sql->stmt_location = 0;
s_sql->stmt_len = 0;

c_sql->stmt = (Node *) selectStmt->rarg;
c_sql->stmt_location = 0;
c_sql->stmt_len = 0;

bbf_pivot_sql1 = copyObject(s_sql);
bbf_pivot_sql2 = copyObject(c_sql);

MemoryContextSwitchTo(oldContext);
/*
call analyzer to generate two plan
and save to global variable for later pivot
function use
*/
}
}

if (enable_schema_mapping())
rewrite_object_refs(parseTree->stmt);

Expand Down

0 comments on commit 517b8a2

Please sign in to comment.