Aitozi commented on code in PR #21522:
URL: https://github.com/apache/flink/pull/21522#discussion_r1139876700


##########
flink-table/flink-table-common/src/test/java/org/apache/flink/table/catalog/TestSchemaResolver.java:
##########
@@ -0,0 +1,199 @@
+/*
+ * 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.catalog;
+
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.table.api.Schema;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.expressions.Expression;
+import org.apache.flink.table.expressions.ResolvedExpression;
+import org.apache.flink.table.expressions.SqlCallExpression;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.UnresolvedDataType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.utils.LogicalTypeParser;
+
+import javax.annotation.Nullable;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import static 
org.apache.flink.table.types.utils.TypeConversions.fromLogicalToDataType;
+
+/** Testing implementation of {@link SchemaResolver}. */
+public class TestSchemaResolver implements SchemaResolver {
+
+    private final DataTypeFactory dataTypeFactory = new 
TestingDataTypeFactory();
+    private final Map<String, ResolvedExpression> resolveExpressionTable = new 
HashMap<>();
+
+    @Override
+    public ResolvedSchema resolve(Schema schema) {
+        final List<Column> columns = resolveColumns(schema.getColumns());
+
+        final List<WatermarkSpec> watermarkSpecs =
+                schema.getWatermarkSpecs().stream()
+                        .map(this::resolveWatermarkSpecs)
+                        .collect(Collectors.toList());
+
+        final UniqueConstraint primaryKey = 
resolvePrimaryKey(schema.getPrimaryKey().orElse(null));
+
+        return new ResolvedSchema(columns, watermarkSpecs, primaryKey);
+    }
+
+    private List<Column> resolveColumns(List<Schema.UnresolvedColumn> 
unresolvedColumns) {
+        final Column[] resolvedColumns = new Column[unresolvedColumns.size()];
+
+        int i = 0;
+        for (Schema.UnresolvedColumn unresolvedColumn : unresolvedColumns) {
+            if (unresolvedColumn instanceof Schema.UnresolvedPhysicalColumn) {
+                resolvedColumns[i] =
+                        
resolvePhysicalColumn((Schema.UnresolvedPhysicalColumn) unresolvedColumn);
+            } else if (unresolvedColumn instanceof 
Schema.UnresolvedMetadataColumn) {
+                resolvedColumns[i] =
+                        
resolveMetadataColumn((Schema.UnresolvedMetadataColumn) unresolvedColumn);
+            } else if (unresolvedColumn instanceof 
Schema.UnresolvedComputedColumn) {
+                resolvedColumns[i] =
+                        Column.computed(
+                                        unresolvedColumn.getName(),
+                                        resolveExpression(
+                                                
((Schema.UnresolvedComputedColumn) unresolvedColumn)
+                                                        .getExpression()))
+                                
.withComment(unresolvedColumn.getComment().orElse(null));
+            }
+            i++;
+        }
+        return Arrays.asList(resolvedColumns);
+    }
+
+    private Column.PhysicalColumn resolvePhysicalColumn(
+            Schema.UnresolvedPhysicalColumn unresolvedColumn) {
+        return Column.physical(
+                        unresolvedColumn.getName(),
+                        
dataTypeFactory.createDataType(unresolvedColumn.getDataType()))
+                .withComment(unresolvedColumn.getComment().orElse(null));
+    }
+
+    private Column.MetadataColumn resolveMetadataColumn(
+            Schema.UnresolvedMetadataColumn unresolvedColumn) {
+        return Column.metadata(
+                        unresolvedColumn.getName(),
+                        
dataTypeFactory.createDataType(unresolvedColumn.getDataType()),
+                        unresolvedColumn.getMetadataKey(),
+                        unresolvedColumn.isVirtual())
+                .withComment(unresolvedColumn.getComment().orElse(null));
+    }
+
+    private WatermarkSpec resolveWatermarkSpecs(
+            Schema.UnresolvedWatermarkSpec unresolvedWatermarkSpec) {
+        return WatermarkSpec.of(
+                unresolvedWatermarkSpec.getColumnName(),
+                
resolveExpression(unresolvedWatermarkSpec.getWatermarkExpression()));
+    }
+
+    private @Nullable UniqueConstraint resolvePrimaryKey(
+            @Nullable Schema.UnresolvedPrimaryKey unresolvedPrimaryKey) {
+        if (unresolvedPrimaryKey == null) {
+            return null;
+        }
+
+        final UniqueConstraint primaryKey =
+                UniqueConstraint.primaryKey(
+                        unresolvedPrimaryKey.getConstraintName(),
+                        unresolvedPrimaryKey.getColumnNames());
+
+        return primaryKey;
+    }
+
+    private ResolvedExpression resolveExpression(Expression expression) {
+        if (expression instanceof SqlCallExpression) {
+            String callString = ((SqlCallExpression) 
expression).getSqlExpression();
+            if (resolveExpressionTable.containsKey(callString)) {
+                return resolveExpressionTable.get(callString);
+            }
+        }
+        throw new IllegalArgumentException("Unsupported expression: " + 
expression);
+    }
+
+    public TestSchemaResolver addExpression(

Review Comment:
   IMO, due to it's a migration work. So it can't do the exactly same this as 
before. for example:
   
   Before we compare the `TableSchema` with `TableSchema`. In `TableSchema`, 
the dataTypes are resolved, but the expressions are unresolved.
   Now, we have two choice: compare `Schema` with `Schema` or `ResolvedSchema` 
with `ResolvedSchema`. Both are not exactly same as before. Because the new 
framework split out the resolved and unresolved part. 
   
   So from this point, we must have to adapt the test to the new api. So we 
have two way to do this:
   
   - Compare `ResolvedSchema` with `ResolvedSchema`, it need the ability of 
resolve, so we introduce the `TestSchemaResolver`
   - Or compare `Schema` with `Schema`. Due to the `Schema` is not comparable. 
So we have to write the expected `Schema` manually and `ResolvedSchema` for 
creating table. Then compare the string representation of the `getTable`'s  
result and the expected `Schema`.
   
   Both is acceptable for me, but the first one looks more convenient and less 
boilerplate code, So I use the first choice. 
   
   
   
   



-- 
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: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to