spena commented on code in PR #24987:
URL: https://github.com/apache/flink/pull/24987#discussion_r1664452721


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/operations/SqlConverterUtils.java:
##########
@@ -0,0 +1,116 @@
+/*
+ * 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.operations;
+
+import org.apache.flink.sql.parser.ddl.SqlTableColumn;
+import org.apache.flink.sql.parser.ddl.SqlTableColumn.SqlComputedColumn;
+import org.apache.flink.sql.parser.ddl.SqlTableColumn.SqlMetadataColumn;
+import org.apache.flink.sql.parser.ddl.SqlTableColumn.SqlRegularColumn;
+import org.apache.flink.table.api.Schema.UnresolvedComputedColumn;
+import org.apache.flink.table.api.Schema.UnresolvedMetadataColumn;
+import org.apache.flink.table.api.Schema.UnresolvedPhysicalColumn;
+import org.apache.flink.table.catalog.DataTypeFactory;
+import org.apache.flink.table.expressions.SqlCallExpression;
+import org.apache.flink.table.types.logical.LogicalType;
+
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.sql.SqlDataTypeSpec;
+import org.apache.calcite.sql.SqlLiteral;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.validate.SqlValidator;
+
+import java.util.Map;
+import java.util.Optional;
+import java.util.function.Function;
+
+import static 
org.apache.flink.table.planner.calcite.FlinkTypeFactory.toLogicalType;
+import static 
org.apache.flink.table.types.utils.TypeConversions.fromLogicalToDataType;
+
+/** A utility class for {@link SqlCreateTableConverter} conversions. */
+public final class SqlConverterUtils {

Review Comment:
   Done



##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/operations/SqlDdlToOperationConverterTest.java:
##########
@@ -658,6 +658,119 @@ public void testCreateTableInvalidDistribution() {
                         "Invalid bucket key 'f3'. A bucket key for a 
distribution must reference a physical column in the schema. Available columns 
are: [a]");
     }
 
+    @Test
+    public void testCreateTableAsWithColumns() {
+        CatalogTable catalogTable =
+                CatalogTable.newBuilder()
+                        .schema(
+                                Schema.newBuilder()
+                                        .column("f0", 
DataTypes.INT().notNull())
+                                        .column("f1", DataTypes.TIMESTAMP(3))
+                                        .build())
+                        .build();
+
+        catalogManager.createTable(
+                catalogTable, ObjectIdentifier.of("builtin", "default", 
"src1"), false);
+
+        final String sql =
+                "create table tbl1 (c0 int, c1 double metadata, c2 as c0 * f0) 
"
+                        + "AS SELECT * FROM src1";
+
+        Operation ctas = parseAndConvert(sql);
+        Operation operation = ((CreateTableASOperation) 
ctas).getCreateTableOperation();
+        assertThat(operation)
+                .is(
+                        new HamcrestCondition<>(
+                                isCreateTableOperation(
+                                        withNoDistribution(),
+                                        withSchema(
+                                                Schema.newBuilder()
+                                                        .column("c0", 
DataTypes.INT())
+                                                        
.columnByMetadata("c1", DataTypes.DOUBLE())
+                                                        
.columnByExpression("c2", "`c0` * `f0`")
+                                                        .column("f0", 
DataTypes.INT().notNull())
+                                                        .column("f1", 
DataTypes.TIMESTAMP(3))
+                                                        .build()))));
+    }
+
+    @Test
+    public void testCreateTableAsWithColumnsOverridden() {
+        CatalogTable catalogTable =
+                CatalogTable.newBuilder()
+                        .schema(
+                                Schema.newBuilder()
+                                        .column("f0", 
DataTypes.INT().notNull())
+                                        .column("f1", DataTypes.INT())
+                                        .build())
+                        .build();
+
+        catalogManager.createTable(
+                catalogTable, ObjectIdentifier.of("builtin", "default", 
"src1"), false);
+
+        final String sql =
+                "create table tbl1 (c0 int, f0 bigint not null, f1 double) "
+                        + "AS SELECT * FROM src1";
+
+        Operation ctas = parseAndConvert(sql);
+        Operation operation = ((CreateTableASOperation) 
ctas).getCreateTableOperation();
+        assertThat(operation)
+                .is(
+                        new HamcrestCondition<>(
+                                isCreateTableOperation(
+                                        withNoDistribution(),
+                                        withSchema(
+                                                Schema.newBuilder()
+                                                        .column("c0", 
DataTypes.INT())
+                                                        .column("f0", 
DataTypes.BIGINT().notNull())
+                                                        .column("f1", 
DataTypes.DOUBLE())
+                                                        .build()))));
+    }
+
+    @Test
+    public void testCreateTableAsWithNotNullColumnsAreNotAllowed() {
+        CatalogTable catalogTable =
+                CatalogTable.newBuilder()
+                        .schema(
+                                Schema.newBuilder()
+                                        .column("f0", 
DataTypes.INT().notNull())
+                                        .column("f1", DataTypes.INT())
+                                        .build())
+                        .build();
+
+        catalogManager.createTable(
+                catalogTable, ObjectIdentifier.of("builtin", "default", 
"src1"), false);
+
+        final String sql = "create table tbl1 (c0 int not null) " + "AS SELECT 
* FROM src1";
+
+        assertThatThrownBy(() -> parseAndConvert(sql))
+                .isInstanceOf(ValidationException.class)
+                .hasMessageContaining("Column 'c0' has no default value and 
does not allow NULLs.");
+    }
+
+    @Test
+    public void testCreateTableAsWithIncompatibleImplicitCastTypes() {
+        CatalogTable catalogTable =
+                CatalogTable.newBuilder()
+                        .schema(
+                                Schema.newBuilder()
+                                        .column("f0", 
DataTypes.INT().notNull())
+                                        .column("f1", DataTypes.TIMESTAMP(3))
+                                        .build())
+                        .build();
+
+        catalogManager.createTable(
+                catalogTable, ObjectIdentifier.of("builtin", "default", 
"src1"), false);
+
+        final String sql = "create table tbl1 (f0 boolean) AS SELECT * FROM 
src1";
+
+        assertThatThrownBy(() -> parseAndConvert(sql))
+                .isInstanceOf(ValidationException.class)
+                .hasMessageContaining(
+                        "Incompatible types for sink column 'f0' at position 
0. "
+                                + "The source column has type [INT NOT NULL], 
while the target "

Review Comment:
   Done



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