[ 
https://issues.apache.org/jira/browse/FLINK-7062?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16654806#comment-16654806
 ] 

ASF GitHub Bot commented on FLINK-7062:
---------------------------------------

twalthr commented on a change in pull request #6815:  [FLINK-7062][cep][table] 
Added basic support for MATCH_RECOGNIZE
URL: https://github.com/apache/flink/pull/6815#discussion_r226190984
 
 

 ##########
 File path: 
flink-libraries/flink-table/src/test/scala/org/apache/flink/table/match/PatternTranslatorTestBase.scala
 ##########
 @@ -0,0 +1,164 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.`match`
+
+import com.google.common.collect.ImmutableList
+import org.apache.calcite.plan.hep.{HepMatchOrder, HepPlanner, 
HepProgramBuilder}
+import org.apache.calcite.sql2rel.RelDecorrelator
+import org.apache.calcite.tools.{Programs, RelBuilder}
+import org.apache.flink.api.common.typeinfo.{BasicTypeInfo, TypeInformation}
+import org.apache.flink.api.java.typeutils.RowTypeInfo
+import org.apache.flink.cep.pattern.Pattern
+import org.apache.flink.streaming.api.datastream.{DataStream => JDataStream}
+import org.apache.flink.streaming.api.scala.{DataStream, 
StreamExecutionEnvironment}
+import org.apache.flink.table.api.scala._
+import org.apache.flink.table.api.{TableConfig, TableEnvironment}
+import org.apache.flink.table.calcite.FlinkPlannerImpl
+import org.apache.flink.table.plan.nodes.FlinkConventions
+import org.apache.flink.table.plan.nodes.datastream.{DataStreamMatch, 
DataStreamScan, PatternVisitor}
+import org.apache.flink.table.plan.rules.FlinkRuleSets
+import org.apache.flink.types.Row
+import org.apache.flink.util.TestLogger
+import org.junit.Assert._
+import org.junit.rules.ExpectedException
+import org.junit.{ComparisonFailure, Rule}
+import org.mockito.Mockito.{mock, when}
+
+class PatternTranslatorTestBase extends TestLogger{
+
+  private val typeInfo = new RowTypeInfo(BasicTypeInfo.INT_TYPE_INFO)
+
+  private val expectedException = ExpectedException.none()
+
+  @Rule
+  def thrown: ExpectedException = expectedException
+
+  // setup test utils
+  private val tableName = "testTable"
+  private val context = prepareContext(typeInfo)
+  private val planner = new FlinkPlannerImpl(
+    context._2.getFrameworkConfig,
+    context._2.getPlanner,
+    context._2.getTypeFactory)
+  private val logicalOptProgram = 
Programs.ofRules(FlinkRuleSets.LOGICAL_OPT_RULES)
+  private val dataStreamOptProgram = 
Programs.ofRules(FlinkRuleSets.DATASTREAM_OPT_RULES)
+
+  private def hepPlanner = {
+    val builder = new HepProgramBuilder
+    builder.addMatchOrder(HepMatchOrder.BOTTOM_UP)
+    val it = FlinkRuleSets.DATASTREAM_NORM_RULES.iterator()
+    while (it.hasNext) {
+      builder.addRuleInstance(it.next())
+    }
+    new HepPlanner(builder.build, context._2.getFrameworkConfig.getContext)
+  }
+
+  private def prepareContext(typeInfo: TypeInformation[Row])
+  : (RelBuilder, TableEnvironment, StreamExecutionEnvironment) = {
+    // create DataSetTable
+    val dataStreamMock = mock(classOf[DataStream[Row]])
+    val jDataStreamMock = mock(classOf[JDataStream[Row]])
+    when(dataStreamMock.javaStream).thenReturn(jDataStreamMock)
+    when(jDataStreamMock.getType).thenReturn(typeInfo)
+
+    val env = StreamExecutionEnvironment.getExecutionEnvironment
+    val tEnv = TableEnvironment.getTableEnvironment(env)
+    tEnv.registerDataStream(tableName, dataStreamMock, 'f0, 'proctime.proctime)
+
+    // prepare RelBuilder
+    val relBuilder = tEnv.getRelBuilder
+    relBuilder.scan(tableName)
+
+    (relBuilder, tEnv, env)
+  }
+
+  def verifyPattern(matchRecognize: String, expected: Pattern[Row, _ <: Row]): 
Unit = {
+    // create RelNode from SQL expression
+    val parsed = planner.parse(
+      s"""
+         |SELECT *
+         |FROM $tableName
+         |$matchRecognize
+         |""".stripMargin)
+    val validated = planner.validate(parsed)
+    val converted = planner.rel(validated).rel
+
+    val decorPlan = RelDecorrelator.decorrelateQuery(converted)
+
+    // normalize
+    val normalizedPlan = if 
(FlinkRuleSets.DATASET_NORM_RULES.iterator().hasNext) {
+      val planner = hepPlanner
+      planner.setRoot(decorPlan)
+      planner.findBestExp
+    } else {
+      decorPlan
+    }
+
+    // convert to logical plan
+    val logicalProps = 
converted.getTraitSet.replace(FlinkConventions.LOGICAL).simplify()
+    val logicalCalc = logicalOptProgram.run(context._2.getPlanner, 
normalizedPlan, logicalProps,
+      ImmutableList.of(), ImmutableList.of())
+
+    // convert to dataset plan
+    val physicalProps = 
converted.getTraitSet.replace(FlinkConventions.DATASTREAM).simplify()
+    val dataStreamMatch = dataStreamOptProgram
+      .run(context._2.getPlanner, logicalCalc, physicalProps,
+        ImmutableList.of(), ImmutableList.of())
+
+    // throw exception if plan contains more than a calc
 
 Review comment:
   This line and the `fail` need an update.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> Support the basic functionality of MATCH_RECOGNIZE
> --------------------------------------------------
>
>                 Key: FLINK-7062
>                 URL: https://issues.apache.org/jira/browse/FLINK-7062
>             Project: Flink
>          Issue Type: Sub-task
>          Components: CEP, Table API &amp; SQL
>            Reporter: Dian Fu
>            Assignee: Dian Fu
>            Priority: Major
>              Labels: pull-request-available
>
> In this JIRA, we will support the basic functionality of {{MATCH_RECOGNIZE}} 
> in Flink SQL API which includes the support of syntax {{MEASURES}}, 
> {{PATTERN}} and {{DEFINE}}. This would allow users write basic cep use cases 
> with SQL like the following example:
> {code}
> SELECT T.aid, T.bid, T.cid
> FROM MyTable
> MATCH_RECOGNIZE (
>   MEASURES
>     A.id AS aid,
>     B.id AS bid,
>     C.id AS cid
>   PATTERN (A B C)
>   DEFINE
>     A AS A.name = 'a',
>     B AS B.name = 'b',
>     C AS C.name = 'c'
> ) AS T
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to