[CALCITE-7465] Make MATCH_RECOGNIZE tolerant to FINAL and RUNNING non function MEASURES#4899
Conversation
| + "FINAL LAST(\"DOWN\".\"net_weight\", 0) AS \"BOTTOM_NW\", " | ||
| + "FINAL LAST(\"UP\".\"net_weight\", 0) AS \"END_NW\"\n" | ||
| + "ONE ROW PER MATCH\n" |
There was a problem hiding this comment.
old test producing unparsable query
91f44e4 to
dd71d5b
Compare
|
|
The PR name is the subject to be changed once we align on the approach in jira |
| { s = span(); } | ||
| func = NamedFunctionCall() { | ||
| return runningOp.createCall(s.end(func), func); | ||
| e = Expression3(ExprContext.ACCEPT_NON_QUERY) { |
There was a problem hiding this comment.
Is there a spec of the grammar for this construct somewhere?
Ideally we would follow that.
I don't know enough about this construct to make a choice myself.
There was a problem hiding this comment.
Didn't get if your question is about Expression3 then https://github.com/snuyanzin/calcite/blob/dd71d5b3aa0bd004f4d5369986e50301b628b0ad/core/src/main/codegen/templates/Parser.jj#L4045-L4049
if your question is about MATCH_RECOGNIZE then I have only links to other vendors
like Snowflake https://docs.snowflake.com/en/sql-reference/constructs/match_recognize#syntax
[ MEASURES <expr> [AS] <alias> [, ... ] ]
and below they have explanation for FINAL, RUNNING (Snowlake doesn't allow FINAL, RUNNING for non function, however Calcite always sets either FINAL or RUNNING and unparses it like that)
expr ::= ... [ { RUNNING | FINAL } ] windowFunction ...
BigQuery seems doesn't have FINAL, RUNNING at all https://docs.cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#match_recognize_clause
Oracle doesn't use these keyword and determines FINAL or RUNNING implicitly https://docs.oracle.com/cd/E29542_01/apirefs.1111/e12048/pattern_recog.htm#BEJBFEGJ
There was a problem hiding this comment.
Ok, so there isn't a universally agreed standard for MATCH RECOGNIZE.
It's fine for the parser to be more permissive and the validator to reject some things later (e.g., if you want to emulate restrictions of a specific dialect).
There was a problem hiding this comment.
But is Expression3 too permissive?
Can you make up some expressions that would be accepted which are not legal?
There was a problem hiding this comment.
Otherwise this approach is nice, because it is minimally invasive.
There was a problem hiding this comment.
I spotted another case where this PR will help #4907
I tried other case where expression under FIRST, LAST, NEXT , PREV might be expanded however parser after that not able to parse it.
After changing to expression parser started to parse such unparsed queries, example in linked PR and related jira
…G` non function `MEASURES`
|
MATCH_RECOGNIZE MEASURES might produce unparsable sqlMATCH_RECOGNIZE tolerant to FINAL and RUNNING non function MEASURES



Jira Link
CALCITE-7465
Current issue is that some of
MEASURESFINALorRUNNINGkeywordIn both cases unparsed sql is wrong.
Since calculation is happenning during validation it impacts unparse logic,
The proposal is movement calculation to
SqlToRelConverterthen unparse will work as expectedquery for the first case
if not all rows then the default operation will be
FINALand it will be unparsed (non parsable anymore) tofor the second case
here for one of the measures it is
RUNNINGhowever it is notALL ROWSthen will be replaced withFINALthe problem is that it is unparsed as
i.e. there is
FINALandRUNNINGtogether for the same measure which makes the query unparsable.