[ 
https://issues.apache.org/jira/browse/CALCITE-7465?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Sergey Nuyanzin updated CALCITE-7465:
-------------------------------------
    Description: 
It adds extra {{FINAL}} or {{RUNNING}} for every field forward at 
{{SqlValidatorImpl#navigationInMeasure}} depending on all or not all rows

for instance this query
{code:sql}
SELECT *
FROM emp
MATCH_RECOGNIZE (
  MEASURES
     FINAL COUNT(A.deptno) AS deptno,
     A.ename AS ename
  PATTERN (A B)
  DEFINE
    A AS A.empno = 123
) AS T
{code}

is being rewritten to (added {{FINAL}})
{code:sql}
SELECT *
FROM `EMP` MATCH_RECOGNIZE(
MEASURES FINAL COUNT(`A`.`DEPTNO`) AS `DEPTNO`, FINAL `A`.`ENAME` AS `ENAME`
PATTERN (`A` `B`)
DEFINE `A` AS (PREV(`A`.`EMPNO`, 0) = 123)) AS `T`
{code}

At the same time an attempt to parse unparsed version will fail because of 
added {{FINAL}}

UPD: after deeper look it might be not only {{FINAL}} being added however it 
might be rewritten {{RUNNING}} with {{FINAL}} or vice versa, depending on 
{{MATCH ALL ROWS}} or {{ONE ROW}}.
for instance
{code:sql}
SELECT *
  from \"product\" match_recognize
  (
   measures STRT.\"net_weight\" as start_nw,
   FINAL COUNT(\"net_weight\") as down_cnt,
   RUNNING COUNT(\"net_weight\") as running_cnt
    pattern (strt down+ up+)
    define
      down as down.\"net_weight\" < PREV(down.\"net_weight\"),
      up as up.\"net_weight\" > prev(up.\"net_weight\")
  ) mr
{code}
currently being rewritten as (also invalid since it has both {{FINAL}} and 
{{RUNNING}} for the same measure)
{code:sql}
SELECT *
FROM (SELECT *
FROM \"foodmart\".\"product\") 
MATCH_RECOGNIZE(
MEASURES 
FINAL \"STRT\".\"net_weight\" AS \"START_NW\",
FINAL COUNT(\"product\".\"net_weight\") AS \"DOWN_CNT\", 
FINAL (RUNNING COUNT(\"product\".\"net_weight\")) AS \"RUNNING_CNT\"
ONE ROW PER MATCH
AFTER MATCH SKIP TO NEXT ROW
PATTERN (\"STRT\" \"DOWN\" + \"UP\" +)
DEFINE 
\"DOWN\" AS PREV(\"DOWN\".\"net_weight\", 0) < 
PREV(\"DOWN\".\"net_weight\", 1), 
\"UP\" AS PREV(\"UP\".\"net_weight\", 0) > 
PREV(\"UP\".\"net_weight\", 1))
{code}
So probably better to move the logic from validation to {{SqlToRelConverter}} 
then unparsing will stop being impacted 

  was:
It adds extra {{FINAL}} or {{RUNNING}} for every field forward at 
{{SqlValidatorImpl#navigationInMeasure}} depending on all or not all rows

for instance this query
{code:sql}
SELECT *
FROM emp
MATCH_RECOGNIZE (
  MEASURES
     FINAL COUNT(A.deptno) AS deptno,
     A.ename AS ename
  PATTERN (A B)
  DEFINE
    A AS A.empno = 123
) AS T
{code}

is being rewritten to (added {{FINAL}})
{code:sql}
SELECT *
FROM `EMP` MATCH_RECOGNIZE(
MEASURES FINAL COUNT(`A`.`DEPTNO`) AS `DEPTNO`, FINAL `A`.`ENAME` AS `ENAME`
PATTERN (`A` `B`)
DEFINE `A` AS (PREV(`A`.`EMPNO`, 0) = 123)) AS `T`
{code}

At the same time an attempt to parse unparsed version will fail because of 
added {{FINAL}}

UPD: after deeper look it might be not only {{FINAL}} being added however it 
might be rewritten {{RUNNING}} with {{FINAL}} or vice versa, depending on 
{{MATCH ALL ROWS}} or {{ONE ROW}}.
So probably better to move the logic from validation to {{SqlToRelConverter}} 
then unparsing will stop being impacted 


> Unparse of MATCH_RECOGNIZE with field forwards produce unparsable sql
> ---------------------------------------------------------------------
>
>                 Key: CALCITE-7465
>                 URL: https://issues.apache.org/jira/browse/CALCITE-7465
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>            Reporter: Sergey Nuyanzin
>            Assignee: Sergey Nuyanzin
>            Priority: Major
>
> It adds extra {{FINAL}} or {{RUNNING}} for every field forward at 
> {{SqlValidatorImpl#navigationInMeasure}} depending on all or not all rows
> for instance this query
> {code:sql}
> SELECT *
> FROM emp
> MATCH_RECOGNIZE (
>   MEASURES
>      FINAL COUNT(A.deptno) AS deptno,
>      A.ename AS ename
>   PATTERN (A B)
>   DEFINE
>     A AS A.empno = 123
> ) AS T
> {code}
> is being rewritten to (added {{FINAL}})
> {code:sql}
> SELECT *
> FROM `EMP` MATCH_RECOGNIZE(
> MEASURES FINAL COUNT(`A`.`DEPTNO`) AS `DEPTNO`, FINAL `A`.`ENAME` AS `ENAME`
> PATTERN (`A` `B`)
> DEFINE `A` AS (PREV(`A`.`EMPNO`, 0) = 123)) AS `T`
> {code}
> At the same time an attempt to parse unparsed version will fail because of 
> added {{FINAL}}
> UPD: after deeper look it might be not only {{FINAL}} being added however it 
> might be rewritten {{RUNNING}} with {{FINAL}} or vice versa, depending on 
> {{MATCH ALL ROWS}} or {{ONE ROW}}.
> for instance
> {code:sql}
> SELECT *
>   from \"product\" match_recognize
>   (
>    measures STRT.\"net_weight\" as start_nw,
>    FINAL COUNT(\"net_weight\") as down_cnt,
>    RUNNING COUNT(\"net_weight\") as running_cnt
>     pattern (strt down+ up+)
>     define
>       down as down.\"net_weight\" < PREV(down.\"net_weight\"),
>       up as up.\"net_weight\" > prev(up.\"net_weight\")
>   ) mr
> {code}
> currently being rewritten as (also invalid since it has both {{FINAL}} and 
> {{RUNNING}} for the same measure)
> {code:sql}
> SELECT *
> FROM (SELECT *
> FROM \"foodmart\".\"product\") 
> MATCH_RECOGNIZE(
> MEASURES 
> FINAL \"STRT\".\"net_weight\" AS \"START_NW\",
> FINAL COUNT(\"product\".\"net_weight\") AS \"DOWN_CNT\", 
> FINAL (RUNNING COUNT(\"product\".\"net_weight\")) AS \"RUNNING_CNT\"
> ONE ROW PER MATCH
> AFTER MATCH SKIP TO NEXT ROW
> PATTERN (\"STRT\" \"DOWN\" + \"UP\" +)
> DEFINE 
> \"DOWN\" AS PREV(\"DOWN\".\"net_weight\", 0) < 
> PREV(\"DOWN\".\"net_weight\", 1), 
> \"UP\" AS PREV(\"UP\".\"net_weight\", 0) > 
> PREV(\"UP\".\"net_weight\", 1))
> {code}
> So probably better to move the logic from validation to {{SqlToRelConverter}} 
> then unparsing will stop being impacted 



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to