snuyanzin opened a new pull request, #4899:
URL: https://github.com/apache/calcite/pull/4899

   ## Jira Link
   
   [CALCITE-7465](https://issues.apache.org/jira/browse/CALCITE-7465)
   
   Current issue is that some of `MEASURES` 
   1. might be without `FINAL` or `RUNNING` keyword
   2. might be with a keyword however the computed one is different.
   In both cases unparsed sql is wrong.
   
   Since calculation is happenning during validation it impacts unparse logic,
   The proposal is movement calculation to `SqlToRelConverter` then unparse 
will work as expected
   
   query for the first case
   ```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
   ```
   if not all rows then the default operation will be `FINAL` and it will be 
unparsed (non parsable anymore) to
   ```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`
   
   ```
   
   for the second case
   ```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
   ```
   here for one of the measures it is `RUNNING` however it is not `ALL ROWS` 
then will be replaced with `FINAL`
   the problem is  that it is unparsed as
   ```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))
   ```
   i.e. there is `FINAL` and `RUNNING` together for the same measure which 
makes the query unparsable.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to