KurtYoung commented on a change in pull request #10123:
[FLINK-14665][table-planner-blink] Support computed column for create…
URL: https://github.com/apache/flink/pull/10123#discussion_r344422330
##########
File path:
flink-table/flink-table-planner-blink/src/main/java/org/apache/flink/table/planner/operations/SqlToOperationConverter.java
##########
@@ -201,30 +201,44 @@ private Operation convertSqlQuery(SqlNode node) {
*
* <p>The returned table schema contains columns (a:int, b:varchar,
c:timestamp).
*
- * @param sqlCreateTable sql create table node.
+ * @param sqlCreateTable sql create table node
* @return TableSchema
*/
private TableSchema createTableSchema(SqlCreateTable sqlCreateTable) {
- if (sqlCreateTable.containsComputedColumn()) {
- throw new SqlConversionException("Computed columns for
DDL is not supported yet!");
- }
- TableSchema.Builder builder = new TableSchema.Builder();
- SqlValidator validator = flinkPlanner.getOrCreateSqlValidator();
- // setup table columns
+ // Setup table columns.
SqlNodeList columnList = sqlCreateTable.getColumnList();
- Map<String, RelDataType> nameToTypeMap = new HashMap<>();
+ // Collect the physical fields info first.
+ Map<String, RelDataType> nameToType = new HashMap<>();
+ final SqlValidator validator =
flinkPlanner.getOrCreateSqlValidator();
for (SqlNode node : columnList.getList()) {
if (node instanceof SqlTableColumn) {
SqlTableColumn column = (SqlTableColumn) node;
RelDataType relType = column.getType()
.deriveType(validator,
column.getType().getNullable());
String name = column.getName().getSimple();
- nameToTypeMap.put(name, relType);
- DataType dataType =
TypeConversions.fromLogicalToDataType(
-
FlinkTypeFactory.toLogicalType(relType));
- builder.field(name, dataType);
- } else if (node instanceof SqlBasicCall) {
- // TODO: computed column ...
+ nameToType.put(name, relType);
+ }
+ }
+ final TableSchema.Builder builder = new TableSchema.Builder();
+ // Build the table schema.
+ for (SqlNode node : columnList) {
+ if (node instanceof SqlTableColumn) {
+ SqlTableColumn column = (SqlTableColumn) node;
+ final String fieldName =
column.getName().getSimple();
+ assert nameToType.containsKey(fieldName);
+ builder.field(fieldName,
+ TypeConversions.fromLogicalToDataType(
+
FlinkTypeFactory.toLogicalType(nameToType.get(fieldName))));
+ } else {
+ assert node.getKind() == SqlKind.AS;
Review comment:
don't assert here, take the type you wanted using `else if (....) {`, and if
you are sure about no third situation would happen, through an exception in
another `else { }`
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services