snuyanzin commented on code in PR #29932: URL: https://github.com/apache/flink/pull/29932#discussion_r4586339360
########## flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/operations/converters/PartialInsertUtil.java: ########## @@ -0,0 +1,188 @@ +/* + * 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.converters; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.sql.parser.SqlProperty; +import org.apache.flink.sql.parser.dml.RichSqlInsert; +import org.apache.flink.table.api.ValidationException; +import org.apache.flink.table.catalog.ResolvedSchema; +import org.apache.flink.table.planner.calcite.FlinkCalciteSqlValidator; +import org.apache.flink.table.planner.calcite.FlinkPlannerImpl; +import org.apache.flink.table.planner.calcite.FlinkTypeFactory; +import org.apache.flink.table.planner.operations.PlannerQueryOperation; +import org.apache.flink.table.planner.utils.OperationConverterUtils; +import org.apache.flink.table.types.logical.RowType; + +import org.apache.calcite.plan.RelOptCluster; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.RelFactories; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rel.type.RelDataTypeFactory; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.sql.SqlIdentifier; +import org.apache.calcite.sql.SqlLiteral; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.SqlNodeList; +import org.apache.calcite.sql.SqlUtil; +import org.apache.calcite.tools.RelBuilder; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import static org.apache.calcite.util.Static.RESOURCE; +import static org.apache.flink.util.Preconditions.checkState; + +/** + * Projects the source query of an {@code INSERT INTO} with static partitions or a target column + * list onto the sink's persisted columns, after validation. Static partition columns become their + * value cast to the column type, unlisted columns become {@code NULL}. The target columns are + * validated before that in {@link org.apache.flink.table.planner.calcite.PreValidateReWriter}. + */ +@Internal +public final class PartialInsertUtil { + + private PartialInsertUtil() {} + + /** + * Returns the query projected onto the sink's persisted columns, or the query itself when the + * statement has no static partitions and its target column list neither reorders nor pads it. + */ + public static PlannerQueryOperation alignWithSink( + FlinkPlannerImpl planner, + RichSqlInsert insert, + ResolvedSchema sinkSchema, + PlannerQueryOperation query) { + final SqlNodeList targetColumnList = insert.getTargetColumnList(); + final SqlNodeList staticPartitions = insert.getStaticPartitions(); + if (targetColumnList == null && staticPartitions.isEmpty()) { + return query; + } + + final RelNode queryRel = query.getCalciteTree(); + final int queryColumnCount = queryRel.getRowType().getFieldCount(); + final RelOptCluster cluster = queryRel.getCluster(); + final RexBuilder rexBuilder = cluster.getRexBuilder(); + final FlinkTypeFactory typeFactory = (FlinkTypeFactory) cluster.getTypeFactory(); + final RowType sinkRowType = (RowType) sinkSchema.toSinkRowDataType().getLogicalType(); + + final Map<String, SqlLiteral> partitionValues = new HashMap<>(); + for (SqlNode node : staticPartitions) { + final SqlProperty property = (SqlProperty) node; + partitionValues.put(property.getKey().getSimple(), (SqlLiteral) property.getValue()); + } + + final List<String> targetColumns; + if (targetColumnList == null) { + targetColumns = null; + } else { + targetColumns = + targetColumnList.stream() + .map(column -> ((SqlIdentifier) column).getSimple()) + .collect(Collectors.toList()); + // PreValidateReWriter can only check this before validation for sources whose column + // count is known syntactically, so not for a SELECT *. + if (targetColumns.size() != queryColumnCount) { + throw validationError( + SqlUtil.newContextException( + insert.getSource().getParserPosition(), + RESOURCE.columnCountMismatch())); + } + } + + // PreValidateReWriter resolved these names against the same schema. An unresolved name + // here would pad the sink column with NULL and drop the query column. + final List<String> sinkFieldNames = sinkRowType.getFieldNames(); + checkState( + sinkFieldNames.containsAll(partitionValues.keySet()) + && (targetColumns == null || sinkFieldNames.containsAll(targetColumns)), + "Unresolved target column for sink columns %s", + sinkFieldNames); + + final List<RexNode> projects = new ArrayList<>(); + final List<String> fieldNames = new ArrayList<>(); + boolean projectionNeeded = !partitionValues.isEmpty(); + int nextQueryColumn = 0; + for (RowType.RowField field : sinkRowType.getFields()) { + final RelDataType fieldType = + typeFactory.createFieldTypeFromLogicalType(field.getType()); + final SqlLiteral partitionValue = partitionValues.get(field.getName()); + if (partitionValue != null) { + projects.add(convertStaticPartitionValue(planner, partitionValue, fieldType)); + } else if (targetColumns != null) { + final int queryColumn = targetColumns.indexOf(field.getName()); Review Comment: it will be slow for huge schema better to use mapping `name` -> `index` -- 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]
