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


##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/operations/WindowTableFunctionQueryOperation.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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.operations;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.catalog.Column;
+import org.apache.flink.table.catalog.ResolvedSchema;
+import org.apache.flink.table.expressions.ApiExpressionUtils;
+import org.apache.flink.table.expressions.SqlFactory;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.utils.EncodingUtils;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import static 
org.apache.flink.table.types.logical.utils.LogicalTypeChecks.canBeTimeAttributeType;
+
+/**
+ * Relational operation that assigns rows to windows using a windowing 
table-valued function
+ * (TUMBLE/HOP/CUMULATE/SESSION). Appends {@code window_start}, {@code 
window_end} and {@code
+ * window_time} to the input and returns the enriched relation.
+ */
+@Internal
+public class WindowTableFunctionQueryOperation implements QueryOperation {
+
+    private static final String INPUT_ALIAS = "$$T_WIN";
+
+    /** Window kind; the name maps to the SQL window operator. */
+    @Internal
+    public enum WindowKind {
+        TUMBLE(1),
+        HOP(2),
+        CUMULATE(2),
+        SESSION(1);
+
+        private final int expectedIntervalCount;
+
+        WindowKind(int expectedIntervalCount) {
+            this.expectedIntervalCount = expectedIntervalCount;
+        }
+
+        /** Number of interval operands this window kind requires. */
+        public int expectedIntervalCount() {
+            return expectedIntervalCount;
+        }
+    }
+
+    private final WindowKind windowKind;
+    private final String timeColumn;
+    private final List<Duration> intervals;
+    private final QueryOperation child;
+    private final ResolvedSchema resolvedSchema;
+
+    public WindowTableFunctionQueryOperation(
+            WindowKind windowKind,
+            String timeColumn,
+            List<Duration> intervals,
+            QueryOperation child) {
+        if (intervals.size() != windowKind.expectedIntervalCount()) {
+            throw new ValidationException(
+                    String.format(
+                            "Window kind %s requires %d interval(s), but got 
%d.",
+                            windowKind, windowKind.expectedIntervalCount(), 
intervals.size()));
+        }
+        final ResolvedSchema inputSchema = child.getResolvedSchema();
+        final int timeIndex = inputSchema.getColumnNames().indexOf(timeColumn);
+        if (timeIndex < 0) {
+            throw new ValidationException(
+                    String.format(
+                            "Window time column '%s' does not exist. Available 
columns: %s",
+                            timeColumn, inputSchema.getColumnNames()));
+        }
+        final DataType timeType = 
inputSchema.getColumnDataTypes().get(timeIndex);
+        if (!canBeTimeAttributeType(timeType.getLogicalType())) {

Review Comment:
   should we also check if it is  actual rowtime/proctime time attribute?
   
   looks like the reason of failure for this case
   ```java
   @Test
       void plannerFailureShouldBeCleanValidationError() {
           final QueryOperation op = buildWindow();
           assertThatThrownBy(() -> tEnv().createTable(op).explain())
                   .as("planner failure for a non-time-attribute window column")
                   .isInstanceOf(ValidationException.class)
                   .hasMessageContaining("time attribute");
       }
   
       private QueryOperation buildWindow() {
           return tEnv().getOperationTreeBuilder()
                   .windowTableFunction(
                           TUMBLE,
                           $("ts"),
                           Collections.singletonList(lit(10).minutes()),
                           tEnv().from("src4").getQueryOperation());
       }
   
       private TableEnvironmentImpl tEnv() {
           return (TableEnvironmentImpl) util.tableEnv();
       }
   ...
   @BeforeEach
       void setup() {
           util = streamTestUtil(TableConfig.getDefault());
           util.tableEnv()
                   .executeSql(
                           "CREATE TEMPORARY TABLE src4 (\n"
                                   + "  id INT, ts TIMESTAMP(3)\n" // no 
WATERMARK -> ts is not rowtime
                                   + ") WITH ('connector' = 'datagen')");
       }
   ```



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