snuyanzin commented on code in PR #29070:
URL: https://github.com/apache/flink/pull/29070#discussion_r3925967505


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/WindowTableFunctionRelNodeTest.java:
##########
@@ -0,0 +1,189 @@
+/*
+ * 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.planner.plan;
+
+import org.apache.flink.table.api.Table;
+import org.apache.flink.table.api.TableConfig;
+import org.apache.flink.table.api.internal.TableEnvironmentImpl;
+import org.apache.flink.table.operations.QueryOperation;
+import org.apache.flink.table.operations.WindowTableFunctionQueryOperation;
+import 
org.apache.flink.table.operations.WindowTableFunctionQueryOperation.WindowKind;
+import org.apache.flink.table.planner.utils.StreamTableTestUtil;
+import org.apache.flink.table.planner.utils.TableTestBase;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.table.api.Expressions.lit;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/**
+ * Verifies that a programmatically built {@link 
WindowTableFunctionQueryOperation} is lowered by
+ * {@code QueryOperationConverter} to the same window-TVF plan as the 
equivalent {@code
+ * TABLE(TUMBLE(...))} / {@code HOP} / {@code CUMULATE} / {@code SESSION} SQL.
+ */
+class WindowTableFunctionRelNodeTest extends TableTestBase {
+
+    private StreamTableTestUtil util;
+
+    @BeforeEach
+    void setup() {
+        util = streamTestUtil(TableConfig.getDefault());
+        util.tableEnv()
+                .executeSql(
+                        "CREATE TEMPORARY TABLE src (\n"
+                                + "  id INT, amount INT, ts TIMESTAMP(3),\n"
+                                + "  WATERMARK FOR ts AS ts - INTERVAL '1' 
SECOND\n"
+                                + ") WITH ('connector' = 'datagen')");
+    }
+
+    @Test
+    void testTumbleMatchesSqlPlan() {
+        assertProgrammaticPlanMatchesSql(
+                "SELECT * FROM TABLE(TUMBLE(TABLE src, DESCRIPTOR(ts), 
INTERVAL '10' MINUTE))",
+                tEnv().getOperationTreeBuilder()
+                        .windowTableFunction(
+                                WindowKind.TUMBLE,
+                                $("ts"),
+                                Collections.singletonList(lit(10).minutes()),
+                                tEnv().from("src").getQueryOperation()));
+    }
+
+    @Test
+    void testHopMatchesSqlPlan() {
+        assertProgrammaticPlanMatchesSql(
+                "SELECT * FROM TABLE(HOP(TABLE src, DESCRIPTOR(ts), "
+                        + "INTERVAL '5' MINUTE, INTERVAL '10' MINUTE))",
+                tEnv().getOperationTreeBuilder()
+                        .windowTableFunction(
+                                WindowKind.HOP,
+                                $("ts"),
+                                Arrays.asList(lit(5).minutes(), 
lit(10).minutes()),
+                                tEnv().from("src").getQueryOperation()));
+    }
+
+    @Test
+    void testCumulateMatchesSqlPlan() {
+        assertProgrammaticPlanMatchesSql(
+                "SELECT * FROM TABLE(CUMULATE(TABLE src, DESCRIPTOR(ts), "
+                        + "INTERVAL '5' MINUTE, INTERVAL '10' MINUTE))",
+                tEnv().getOperationTreeBuilder()
+                        .windowTableFunction(
+                                WindowKind.CUMULATE,
+                                $("ts"),
+                                Arrays.asList(lit(5).minutes(), 
lit(10).minutes()),
+                                tEnv().from("src").getQueryOperation()));
+    }
+
+    @Test
+    void testSessionMatchesSqlPlan() {
+        assertProgrammaticPlanMatchesSql(
+                "SELECT * FROM TABLE(SESSION(TABLE src, DESCRIPTOR(ts), 
INTERVAL '10' MINUTE))",
+                tEnv().getOperationTreeBuilder()
+                        .windowTableFunction(
+                                WindowKind.SESSION,
+                                $("ts"),
+                                Collections.singletonList(lit(10).minutes()),
+                                tEnv().from("src").getQueryOperation()));
+    }
+
+    @Test
+    void testYearMonthIntervalRejected() {
+        assertThatThrownBy(
+                        () ->
+                                tEnv().getOperationTreeBuilder()
+                                        .windowTableFunction(
+                                                WindowKind.TUMBLE,
+                                                $("ts"),
+                                                
Collections.singletonList(lit(1).month()),
+                                                
tEnv().from("src").getQueryOperation()))
+                .hasMessageContaining("day-time");
+    }
+
+    @Test
+    void testZeroIntervalRejected() {
+        assertThatThrownBy(
+                        () ->
+                                tEnv().getOperationTreeBuilder()
+                                        .windowTableFunction(
+                                                WindowKind.TUMBLE,
+                                                $("ts"),
+                                                
Collections.singletonList(lit(0).seconds()),
+                                                
tEnv().from("src").getQueryOperation()))
+                .hasMessageContaining("positive");
+    }
+
+    @Test
+    void testNegativeIntervalRejected() {
+        assertThatThrownBy(
+                        () ->
+                                tEnv().getOperationTreeBuilder()
+                                        .windowTableFunction(
+                                                WindowKind.TUMBLE,
+                                                $("ts"),
+                                                
Collections.singletonList(lit(-5).seconds()),
+                                                
tEnv().from("src").getQueryOperation()))
+                .hasMessageContaining("positive");
+    }
+
+    @Test
+    void testNonLiteralIntervalRejected() {
+        assertThatThrownBy(
+                        () ->
+                                tEnv().getOperationTreeBuilder()
+                                        .windowTableFunction(
+                                                WindowKind.TUMBLE,
+                                                $("ts"),
+                                                
Collections.singletonList($("ts")),
+                                                
tEnv().from("src").getQueryOperation()))
+                .hasMessageContaining("interval literal");
+    }
+
+    private void assertProgrammaticPlanMatchesSql(String sql, QueryOperation 
windowOp) {
+        final Table viaSql = tEnv().sqlQuery(sql);
+        final Table viaApi = tEnv().createTable(windowOp);
+        assertThat(physicalPlan(viaApi)).isEqualTo(physicalPlan(viaSql));
+    }
+
+    private TableEnvironmentImpl tEnv() {
+        return (TableEnvironmentImpl) util.tableEnv();
+    }
+
+    /**
+     * Compares the optimized physical (and execution) plan, which is the true 
measure of plan
+     * equivalence. The abstract syntax tree is intentionally excluded because 
{@code SELECT *} adds
+     * an identity projection over the window-TVF scan that the programmatic 
operation does not, and
+     * which the optimizer removes.
+     */
+    private static String physicalPlan(Table table) {
+        final String explain = table.explain();
+        final int start = explain.indexOf("== Optimized Physical Plan ==");
+        assertThat(start)
+                .as(
+                        "explain() output must contain the optimized physical 
plan header:%n%s",
+                        explain)
+                .isNotNegative();
+        return explain.substring(start).replaceAll("UnnamedTable\\$\\d+", "T");
+    }

Review Comment:
   same here



-- 
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