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


##########
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:
   > As far as I see, before this pr, there is no test will need method 
addExpression
   
   That is due to it compare `TableSchema` with `TableSchema` before. So for 
the watermark spec or computed columns, it was compared in the string format. 
But, now it should compared in the `ResolvedSchema` format (for unified 
expression and resolved dataType). So, we need a resolved expression for 
comparing. 



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