aokolnychyi commented on a change in pull request #3763: URL: https://github.com/apache/iceberg/pull/3763#discussion_r773311538
########## File path: spark/v3.2/spark/src/main/java/org/apache/iceberg/spark/source/SparkPositionDeltaOperation.java ########## @@ -0,0 +1,102 @@ +/* + * 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.iceberg.spark.source; + +import org.apache.iceberg.IsolationLevel; +import org.apache.iceberg.MetadataColumns; +import org.apache.iceberg.Table; +import org.apache.spark.sql.SparkSession; +import org.apache.spark.sql.connector.expressions.Expressions; +import org.apache.spark.sql.connector.expressions.NamedReference; +import org.apache.spark.sql.connector.iceberg.write.DeltaWriteBuilder; +import org.apache.spark.sql.connector.iceberg.write.ExtendedLogicalWriteInfo; +import org.apache.spark.sql.connector.iceberg.write.RowLevelOperation; +import org.apache.spark.sql.connector.iceberg.write.RowLevelOperationInfo; +import org.apache.spark.sql.connector.iceberg.write.SupportsDelta; +import org.apache.spark.sql.connector.read.Scan; +import org.apache.spark.sql.connector.read.ScanBuilder; +import org.apache.spark.sql.util.CaseInsensitiveStringMap; + +class SparkPositionDeltaOperation implements RowLevelOperation, SupportsDelta { + + private final SparkSession spark; + private final Table table; + private final Command command; + private final IsolationLevel isolationLevel; + + // lazy vars + private ScanBuilder lazyScanBuilder; + private Scan configuredScan; + private DeltaWriteBuilder lazyWriteBuilder; + + SparkPositionDeltaOperation(SparkSession spark, Table table, RowLevelOperationInfo info, + IsolationLevel isolationLevel) { + this.spark = spark; + this.table = table; + this.command = info.command(); + this.isolationLevel = isolationLevel; + } + + @Override + public Command command() { + return command; + } + + @Override + public ScanBuilder newScanBuilder(CaseInsensitiveStringMap options) { + if (lazyScanBuilder == null) { + this.lazyScanBuilder = new SparkScanBuilder(spark, table, options) { + @Override + public Scan build() { + Scan scan = super.buildMergeOnReadScan(); + SparkPositionDeltaOperation.this.configuredScan = scan; + return scan; + } + }; + } + + return lazyScanBuilder; + } + + @Override + public DeltaWriteBuilder newWriteBuilder(ExtendedLogicalWriteInfo info) { + if (lazyWriteBuilder == null) { + lazyWriteBuilder = new SparkPositionDeltaWriteBuilder( + spark, table, command, configuredScan, Review comment: For merge-on-read, it can be null if the condition evaluates to false and the optimizer replaces the original scan relation with a local relation. ########## File path: spark/v3.2/spark/src/main/java/org/apache/iceberg/spark/source/SparkPositionDeltaWriteBuilder.java ########## @@ -0,0 +1,123 @@ +/* + * 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.iceberg.spark.source; + +import org.apache.iceberg.DistributionMode; +import org.apache.iceberg.IsolationLevel; +import org.apache.iceberg.MetadataColumns; +import org.apache.iceberg.Schema; +import org.apache.iceberg.Table; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.spark.SparkDistributionAndOrderingUtil; +import org.apache.iceberg.spark.SparkSchemaUtil; +import org.apache.iceberg.spark.SparkUtil; +import org.apache.iceberg.spark.SparkWriteConf; +import org.apache.iceberg.types.TypeUtil; +import org.apache.iceberg.types.Types.NestedField; +import org.apache.spark.sql.SparkSession; +import org.apache.spark.sql.connector.distributions.Distribution; +import org.apache.spark.sql.connector.expressions.SortOrder; +import org.apache.spark.sql.connector.iceberg.write.DeltaWrite; +import org.apache.spark.sql.connector.iceberg.write.DeltaWriteBuilder; +import org.apache.spark.sql.connector.iceberg.write.ExtendedLogicalWriteInfo; +import org.apache.spark.sql.connector.iceberg.write.RowLevelOperation.Command; +import org.apache.spark.sql.connector.read.Scan; +import org.apache.spark.sql.types.StructType; + +class SparkPositionDeltaWriteBuilder implements DeltaWriteBuilder { + + private final SparkSession spark; + private final Table table; + private final Command command; + private final SparkBatchQueryScan scan; + private final IsolationLevel isolationLevel; + private final SparkWriteConf writeConf; + private final ExtendedLogicalWriteInfo info; + private final boolean handleTimestampWithoutZone; + private final boolean checkNullability; + private final boolean checkOrdering; + + SparkPositionDeltaWriteBuilder(SparkSession spark, Table table, Command command, Scan scan, + IsolationLevel isolationLevel, ExtendedLogicalWriteInfo info) { + this.spark = spark; + this.table = table; + this.command = command; + this.scan = (SparkBatchQueryScan) scan; + this.isolationLevel = isolationLevel; + this.writeConf = new SparkWriteConf(spark, table, info.options()); + this.info = info; + this.handleTimestampWithoutZone = writeConf.handleTimestampWithoutZone(); + this.checkNullability = writeConf.checkNullability(); + this.checkOrdering = writeConf.checkOrdering(); + } + + @Override + public DeltaWrite build() { + Preconditions.checkArgument(handleTimestampWithoutZone || !SparkUtil.hasTimestampWithoutZone(table.schema()), + SparkUtil.TIMESTAMP_WITHOUT_TIMEZONE_ERROR); + + Schema dataSchema = dataSchema(); + if (dataSchema != null) { + TypeUtil.validateWriteSchema(table.schema(), dataSchema, checkNullability, checkOrdering); + } + + Schema expectedRowIdSchema = expectedRowIdSchema(); + Schema rowIdSchema = SparkSchemaUtil.convert(expectedRowIdSchema, info.rowIdSchema()); + TypeUtil.validateSchema("row ID", expectedRowIdSchema, rowIdSchema, checkNullability, checkOrdering); + + Schema expectedMetadataSchema = expectedMetadataSchema(); + Schema metadataSchema = SparkSchemaUtil.convert(expectedMetadataSchema, info.metadataSchema()); + TypeUtil.validateSchema("metadata", expectedMetadataSchema, metadataSchema, checkNullability, checkOrdering); + + SparkUtil.validatePartitionTransforms(table.spec()); + + Distribution distribution = SparkDistributionAndOrderingUtil.buildPositionDeltaDistribution( + table, command, distributionMode()); + SortOrder[] ordering = SparkDistributionAndOrderingUtil.buildPositionDeltaOrdering( + table, command, distribution); + + return new SparkPositionDeltaWrite( + spark, table, command, scan, isolationLevel, writeConf, + info, dataSchema, distribution, ordering); + } + + private Schema dataSchema() { + StructType dataSparkType = info.schema(); + return dataSparkType != null ? SparkSchemaUtil.convert(table.schema(), dataSparkType) : null; + } + + private Schema expectedRowIdSchema() { + return new Schema(MetadataColumns.FILE_PATH, MetadataColumns.ROW_POSITION); + } + + private Schema expectedMetadataSchema() { + NestedField partition = MetadataColumns.metadataColumn(table, MetadataColumns.PARTITION_COLUMN_NAME); Review comment: Yeah, exactly. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
