Skip to content

Commit 55eed5e

Browse files
tatsuo-ishiiCommitfest Bot
authored andcommitted
Row pattern recognition patch (rewriter).
1 parent bbc4cd0 commit 55eed5e

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,10 @@ static void get_rule_groupingset(GroupingSet *gset, List *targetlist,
438438
bool omit_parens, deparse_context *context);
439439
static void get_rule_orderby(List *orderList, List *targetList,
440440
bool force_colno, deparse_context *context);
441+
static void get_rule_pattern(List *patternVariable, List *patternRegexp,
442+
bool force_colno, deparse_context *context);
443+
static void get_rule_define(List *defineClause, List *patternVariables,
444+
bool force_colno, deparse_context *context);
441445
static void get_rule_windowclause(Query *query, deparse_context *context);
442446
static void get_rule_windowspec(WindowClause *wc, List *targetList,
443447
deparse_context *context);
@@ -6740,6 +6744,67 @@ get_rule_orderby(List *orderList, List *targetList,
67406744
}
67416745
}
67426746

6747+
/*
6748+
* Display a PATTERN clause.
6749+
*/
6750+
static void
6751+
get_rule_pattern(List *patternVariable, List *patternRegexp,
6752+
bool force_colno, deparse_context *context)
6753+
{
6754+
StringInfo buf = context->buf;
6755+
const char *sep;
6756+
ListCell *lc_var,
6757+
*lc_reg = list_head(patternRegexp);
6758+
6759+
sep = "";
6760+
appendStringInfoChar(buf, '(');
6761+
foreach(lc_var, patternVariable)
6762+
{
6763+
char *variable = strVal((String *) lfirst(lc_var));
6764+
char *regexp = NULL;
6765+
6766+
if (lc_reg != NULL)
6767+
{
6768+
regexp = strVal((String *) lfirst(lc_reg));
6769+
6770+
lc_reg = lnext(patternRegexp, lc_reg);
6771+
}
6772+
6773+
appendStringInfo(buf, "%s%s", sep, variable);
6774+
if (regexp !=NULL)
6775+
appendStringInfoString(buf, regexp);
6776+
6777+
sep = " ";
6778+
}
6779+
appendStringInfoChar(buf, ')');
6780+
}
6781+
6782+
/*
6783+
* Display a DEFINE clause.
6784+
*/
6785+
static void
6786+
get_rule_define(List *defineClause, List *patternVariables,
6787+
bool force_colno, deparse_context *context)
6788+
{
6789+
StringInfo buf = context->buf;
6790+
const char *sep;
6791+
ListCell *lc_var,
6792+
*lc_def;
6793+
6794+
sep = " ";
6795+
Assert(list_length(patternVariables) == list_length(defineClause));
6796+
6797+
forboth(lc_var, patternVariables, lc_def, defineClause)
6798+
{
6799+
char *varName = strVal(lfirst(lc_var));
6800+
TargetEntry *te = (TargetEntry *) lfirst(lc_def);
6801+
6802+
appendStringInfo(buf, "%s%s AS ", sep, varName);
6803+
get_rule_expr((Node *) te->expr, context, false);
6804+
sep = ",\n ";
6805+
}
6806+
}
6807+
67436808
/*
67446809
* Display a WINDOW clause.
67456810
*
@@ -6820,6 +6885,7 @@ get_rule_windowspec(WindowClause *wc, List *targetList,
68206885
get_rule_orderby(wc->orderClause, targetList, false, context);
68216886
needspace = true;
68226887
}
6888+
68236889
/* framing clause is never inherited, so print unless it's default */
68246890
if (wc->frameOptions & FRAMEOPTION_NONDEFAULT)
68256891
{
@@ -6828,7 +6894,53 @@ get_rule_windowspec(WindowClause *wc, List *targetList,
68286894
get_window_frame_options(wc->frameOptions,
68296895
wc->startOffset, wc->endOffset,
68306896
context);
6897+
needspace = true;
68316898
}
6899+
6900+
/* RPR */
6901+
if (wc->rpSkipTo == ST_NEXT_ROW)
6902+
{
6903+
if (needspace)
6904+
appendStringInfoChar(buf, ' ');
6905+
appendStringInfoString(buf,
6906+
"\n AFTER MATCH SKIP TO NEXT ROW ");
6907+
needspace = true;
6908+
}
6909+
else if (wc->rpSkipTo == ST_PAST_LAST_ROW)
6910+
{
6911+
if (needspace)
6912+
appendStringInfoChar(buf, ' ');
6913+
appendStringInfoString(buf,
6914+
"\n AFTER MATCH SKIP PAST LAST ROW ");
6915+
needspace = true;
6916+
}
6917+
if (wc->initial)
6918+
{
6919+
if (needspace)
6920+
appendStringInfoChar(buf, ' ');
6921+
appendStringInfoString(buf, "\n INITIAL");
6922+
needspace = true;
6923+
}
6924+
if (wc->patternVariable)
6925+
{
6926+
if (needspace)
6927+
appendStringInfoChar(buf, ' ');
6928+
appendStringInfoString(buf, "\n PATTERN ");
6929+
get_rule_pattern(wc->patternVariable, wc->patternRegexp,
6930+
false, context);
6931+
needspace = true;
6932+
}
6933+
6934+
if (wc->defineClause)
6935+
{
6936+
if (needspace)
6937+
appendStringInfoChar(buf, ' ');
6938+
appendStringInfoString(buf, "\n DEFINE\n");
6939+
get_rule_define(wc->defineClause, wc->patternVariable,
6940+
false, context);
6941+
appendStringInfoChar(buf, ' ');
6942+
}
6943+
68326944
appendStringInfoChar(buf, ')');
68336945
}
68346946

0 commit comments

Comments
 (0)