You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2020/11/06 23:20:53 UTC
[GitHub] [iceberg] aokolnychyi opened a new pull request #1738: Spark: Copy-on-write DELETE prototype
aokolnychyi opened a new pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738
This is a WIP PR that shows how we can support copy-on-write DELETEs in Iceberg/Spark.
**Disclaimer**: It is in an early stage and I'll split it into a number of smaller PRs once we agree on the design.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r520950347
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteDelete.scala
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import java.util.UUID
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.{sources, AnalysisException}
+import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeReference, EqualNullSafe, Expression, InputFileName, Literal, Not, PredicateHelper, SubqueryExpression}
+import org.apache.spark.sql.catalyst.expressions.Literal.FalseLiteral
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, DeleteFromTable, DynamicFileFilter, Filter, LocalRelation, LogicalPlan, Project, ReplaceData}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{ExtendedSupportsDelete, Table}
+import org.apache.spark.sql.connector.read.{Scan, ScanBuilder, SupportsFileFilter}
+import org.apache.spark.sql.connector.write.{LogicalWriteInfo, LogicalWriteInfoImpl, MergeBuilder, RequiresScan}
+import org.apache.spark.sql.execution.datasources.DataSourceStrategy
+import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, DataSourceV2ScanRelation, PushDownUtils}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.{BooleanType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+// TODO: should be part of early scan push down after the delete condition is optimized
+object RewriteDelete extends Rule[LogicalPlan] with PredicateHelper with Logging {
+
+ import org.apache.spark.sql.execution.datasources.v2.ExtendedDataSourceV2Implicits._
+
+ private val FILE_NAME_COL = "_file"
+
+ override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+ // no need to execute this delete as the condition is false
+ case DeleteFromTable(_, Some(FalseLiteral)) =>
+ LocalRelation()
+
+ // don't rewrite deletes that can be answered by passing filters to deleteWhere in SupportsDelete
+ case d @ DeleteFromTable(r: DataSourceV2Relation, Some(cond)) if isDeleteWhereCase(r, cond) =>
+ d
+
+ // rewrite all operations that require reading the table to delete records
+ case DeleteFromTable(r: DataSourceV2Relation, Some(cond)) =>
+ // TODO: do a switch based on whether we get BatchWrite or DeltaBatchWrite
+ val writeInfo = newWriteInfo(r.schema)
+ val mergeBuilder = r.table.asMergeable.newMergeBuilder(writeInfo)
+
+ val (scan, scanPlan) = buildScanPlan(r.table, r.output, mergeBuilder, cond)
+
+ val remainingRowFilter = Not(EqualNullSafe(cond, Literal(true, BooleanType)))
+ val remainingRowsPlan = Filter(remainingRowFilter, scanPlan)
+
+ val batchWrite = mergeBuilder.asWriteBuilder.buildForBatch()
+ batchWrite match {
+ case w: RequiresScan => w.withScan(scan)
+ case _ => // do nothing
+ }
+
+ // TODO: group by something so that we can write back
+ // Option 1: repartition/sort by partition values
+ // Option 2: repartition by _file and local sort by partition values (how to deal with 1 GB files?)
+ ReplaceData(r, batchWrite, remainingRowsPlan)
+ }
+
+ private def buildScanPlan(
+ table: Table,
+ output: Seq[AttributeReference],
+ mergeBuilder: MergeBuilder,
+ cond: Expression): (Scan, LogicalPlan) = {
+
+ val scanBuilder = mergeBuilder.asScanBuilder
+
+ val predicates = splitConjunctivePredicates(cond)
+ val normalizedPredicates = DataSourceStrategy.normalizeExprs(predicates, output)
+ PushDownUtils.pushFilters(scanBuilder, normalizedPredicates)
+
+ val scan = scanBuilder.build()
+ val scanRelation = DataSourceV2ScanRelation(table, scan, output)
+
+ scan match {
+ case _: SupportsFileFilter =>
+ val matchingFilePlan = buildFileFilterPlan(cond, scanRelation)
+ val dynamicFileFilter = DynamicFileFilter(scanRelation, matchingFilePlan)
+ (scan, dynamicFileFilter)
+ case _ =>
+ (scan, scanRelation)
+ }
+ }
+
+ private def isDeleteWhereCase(relation: DataSourceV2Relation, cond: Expression): Boolean = {
+ relation.table match {
+ case t: ExtendedSupportsDelete if !SubqueryExpression.hasSubquery(cond) =>
+ val predicates = splitConjunctivePredicates(cond)
+ val normalizedPredicates = DataSourceStrategy.normalizeExprs(predicates, relation.output)
+ val dataSourceFilters = toDataSourceFilters(normalizedPredicates)
+ val allPredicatesTranslated = normalizedPredicates.size == dataSourceFilters.length
+ allPredicatesTranslated && t.canDeleteWhere(dataSourceFilters)
Review comment:
We should open a PR against Spark for this. It may be easy to get it into 3.1.0.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r520056497
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteDelete.scala
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import java.util.UUID
+import org.apache.spark.sql.{sources, AnalysisException}
+import org.apache.spark.sql.catalyst.analysis.Resolver
+import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeReference, EqualNullSafe, Expression, InputFileName, Literal, Not, PredicateHelper, SubqueryExpression}
+import org.apache.spark.sql.catalyst.expressions.Literal.FalseLiteral
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, DeleteFromTable, DynamicFileFilter, Filter, LocalRelation, LogicalPlan, OverwriteFiles, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{SupportsMetadataOnlyDeletes, Table}
+import org.apache.spark.sql.connector.write.{LogicalWriteInfo, LogicalWriteInfoImpl, RowLevelOperationsBuilder, SupportsPushDownRowLevelFilters}
+import org.apache.spark.sql.execution.datasources.DataSourceStrategy
+import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, DataSourceV2ScanRelation}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.{BooleanType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+// TODO: should be part of early scan push down after the delete condition is optimized
+object RewriteDelete extends Rule[LogicalPlan] with PredicateHelper {
+
+ import org.apache.spark.sql.execution.datasources.v2.ExtendedDataSourceV2Implicits._
+
+ private val FILE_NAME_COL = "_file"
+
+ override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+ // no need to execute this delete as the condition is false
+ case DeleteFromTable(_, Some(FalseLiteral)) =>
+ LocalRelation()
+
+ // don't rewrite deletes that can be answered using metadata only operations
+ case d @ DeleteFromTable(r: DataSourceV2Relation, Some(cond)) if isMetadataOnlyOperation(r, cond) =>
+ d
+
+ // rewrite all operations that require reading the table to delete records
+ case DeleteFromTable(r: DataSourceV2Relation, Some(cond)) =>
+ // TODO: do a switch based on whether we get BatchWrite or DeltaBatchWrite
+ val writeInfo = newWriteInfo(r.schema)
+ val builder = r.table.asRowLevelModifiable.newRowLevelOperationsBuilder(writeInfo)
+
+ pushDownFilters(builder, cond, r.output)
+
+ val scan = builder.buildScan()
+ val scanRelation = DataSourceV2ScanRelation(r.table, scan, r.output)
+ val filterPlan = buildFilterPlan(cond, scanRelation)
+
+ val batchWrite = builder.buildBatchWrite()
+ // TODO: group by something so that we can write back
+ // Option 1: repartition/sort by partition values
+ // Option 2: repartition by _file and local sort by partition values (how to deal with 1 GB files?)
Review comment:
Subquery execution will introduce a shuffle so the data will be everywhere. If we decide to write it after that, we will produce a huge number of small files. Also, if we don't add a shuffle, we probably won't be able to write 1-1 files as the input split size will control our target file size.
In our internal implementation, we went for option 1 initially. In certain cases, this poses a problem. For example, let's say you compacted your table and produced 1GB sorted files (it is an extremely expensive operation). If you do a delete and try to resort the data again before writing, this will be way too expensive. Moreover, the file sizes won't be ideal so you will most likely have to compact again.
So, it seems to me that we should try to write 1-1 replacements. That way, if you had 1 GB sorted file, you would delete records and write 1GB file again. If we introduce a global sort by `_file` that will be hard to scale (e.g. extra job for skew estimation, having a single task write that much of data, etc). Instead, we may hash partition it by `_file`.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519553379
##########
File path: spark3/src/main/java/org/apache/iceberg/spark/source/SparkRowLevelOperationsBuilder.java
##########
@@ -0,0 +1,191 @@
+/*
+ * 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 java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.iceberg.DataFile;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.TableScan;
+import org.apache.iceberg.encryption.EncryptionManager;
+import org.apache.iceberg.exceptions.RuntimeIOException;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.expressions.Binder;
+import org.apache.iceberg.expressions.Expression;
+import org.apache.iceberg.expressions.Expressions;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.spark.SparkFilters;
+import org.apache.iceberg.spark.SparkSchemaUtil;
+import org.apache.iceberg.spark.SparkUtil;
+import org.apache.spark.api.java.JavaSparkContext;
+import org.apache.spark.broadcast.Broadcast;
+import org.apache.spark.sql.SparkSession;
+import org.apache.spark.sql.connector.read.Scan;
+import org.apache.spark.sql.connector.write.BatchWrite;
+import org.apache.spark.sql.connector.write.LogicalWriteInfo;
+import org.apache.spark.sql.connector.write.SupportsPushDownRowLevelFilters;
+import org.apache.spark.sql.sources.Filter;
+import org.apache.spark.sql.types.StructType;
+import org.apache.spark.sql.util.CaseInsensitiveStringMap;
+
+import static org.apache.iceberg.TableProperties.ROW_LEVEL_OPS_ISOLATION_LEVEL;
+import static org.apache.iceberg.TableProperties.ROW_LEVEL_OPS_ISOLATION_LEVEL_DEFAULT;
+
+class SparkRowLevelOperationsBuilder implements SupportsPushDownRowLevelFilters {
Review comment:
Oh, yeah, I'll definitely break it into smaller changes once we agree on a design.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r520954934
##########
File path: spark3/src/main/java/org/apache/iceberg/spark/source/SparkTable.java
##########
@@ -155,6 +163,39 @@ public WriteBuilder newWriteBuilder(LogicalWriteInfo info) {
return new SparkWriteBuilder(sparkSession(), icebergTable, info);
}
+ @Override
+ public MergeBuilder newMergeBuilder(LogicalWriteInfo info) {
+ String mode = icebergTable.properties().getOrDefault(WRITE_ROW_LEVEL_MODE, WRITE_ROW_LEVEL_MODE_DEFAULT);
+ ValidationException.check(mode.equals("copy-on-write"), "Unsupported row operations mode: %s", mode);
+ return new SparkMergeBuilder(sparkSession(), icebergTable, info);
+ }
+
+ @Override
+ public boolean canDeleteWhere(Filter[] filters) {
+ // TODO: multiple partition specs
+ Set<Integer> partitionFieldSourceIds = PartitionUtil.sourceFieldIds(table().spec());
+ Schema schema = table().schema();
+
+ for (Filter filter : filters) {
+ // return false if we have a predicate on a non-partition column or if we cannot translate the filter
+ if (isDataFilter(filter, schema, partitionFieldSourceIds) || SparkFilters.convert(filter) == null) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ private boolean isDataFilter(Filter filter, Schema schema, Set<Integer> partitionFieldSourceIds) {
+ // TODO: handle dots correctly via v2references
+ Set<String> filterRefs = Sets.newHashSet(filter.references());
+ return filterRefs.stream().anyMatch(ref -> {
+ Types.NestedField field = schema.findField(ref);
+ ValidationException.check(field != null, "Cannot find field %s in schema", ref);
+ return !partitionFieldSourceIds.contains(field.fieldId());
Review comment:
I think this only works for identity partition columns. A filter like `ts < 2020-11-10T11:15:03.712498` may reference a partition source field, `ts`, but it isn't safe because it doesn't align with the partition boundaries. The only time we know that it aligns with boundaries is when the partition uses `identity`
The check that we use in the `ManifestFilterManager` to delete files is when `inclusiveProjection.eval` and `structProjection.eval` both accept a file. That is, when the file can contain rows that match the filter (inclusive projection) and all rows are guaranteed to match the filter (strict projection). We will also delete files where the filter is guaranteed to match all rows of a file using the file's metrics.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519066221
##########
File path: spark3/src/main/java/org/apache/spark/sql/connector/read/SupportsReadFileFilter.java
##########
@@ -0,0 +1,26 @@
+/*
+ * 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.spark.sql.connector.read;
+
+import java.util.Set;
+
+public interface SupportsReadFileFilter extends Scan {
+ void filterFiles(Set<String> locations);
Review comment:
The MERGE INTO doc is more generic than files and thinks of each records as part of a "group" that is deleted at once. That group could be a file or a partition.
We could use that to make this generic by renaming it to `filterGroups` and passing in an `Iterator` of group identifiers. Then we would need to have a way to project the group identifier using a metadata column.
This probably isn't something we're interested in doing just for Iceberg, but it is a change we would likely make to get this upstream.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r520058626
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteDelete.scala
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import java.util.UUID
+import org.apache.spark.sql.{sources, AnalysisException}
+import org.apache.spark.sql.catalyst.analysis.Resolver
+import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeReference, EqualNullSafe, Expression, InputFileName, Literal, Not, PredicateHelper, SubqueryExpression}
+import org.apache.spark.sql.catalyst.expressions.Literal.FalseLiteral
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, DeleteFromTable, DynamicFileFilter, Filter, LocalRelation, LogicalPlan, OverwriteFiles, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{SupportsMetadataOnlyDeletes, Table}
+import org.apache.spark.sql.connector.write.{LogicalWriteInfo, LogicalWriteInfoImpl, RowLevelOperationsBuilder, SupportsPushDownRowLevelFilters}
+import org.apache.spark.sql.execution.datasources.DataSourceStrategy
+import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, DataSourceV2ScanRelation}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.{BooleanType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+// TODO: should be part of early scan push down after the delete condition is optimized
+object RewriteDelete extends Rule[LogicalPlan] with PredicateHelper {
+
+ import org.apache.spark.sql.execution.datasources.v2.ExtendedDataSourceV2Implicits._
+
+ private val FILE_NAME_COL = "_file"
+
+ override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+ // no need to execute this delete as the condition is false
+ case DeleteFromTable(_, Some(FalseLiteral)) =>
+ LocalRelation()
+
+ // don't rewrite deletes that can be answered using metadata only operations
+ case d @ DeleteFromTable(r: DataSourceV2Relation, Some(cond)) if isMetadataOnlyOperation(r, cond) =>
+ d
+
+ // rewrite all operations that require reading the table to delete records
+ case DeleteFromTable(r: DataSourceV2Relation, Some(cond)) =>
+ // TODO: do a switch based on whether we get BatchWrite or DeltaBatchWrite
+ val writeInfo = newWriteInfo(r.schema)
+ val builder = r.table.asRowLevelModifiable.newRowLevelOperationsBuilder(writeInfo)
+
+ pushDownFilters(builder, cond, r.output)
+
+ val scan = builder.buildScan()
+ val scanRelation = DataSourceV2ScanRelation(r.table, scan, r.output)
+ val filterPlan = buildFilterPlan(cond, scanRelation)
+
+ val batchWrite = builder.buildBatchWrite()
+ // TODO: group by something so that we can write back
+ // Option 1: repartition/sort by partition values
+ // Option 2: repartition by _file and local sort by partition values (how to deal with 1 GB files?)
Review comment:
I agree, if we are going to shuffle because the query gets rewritten then we should group by `_file`.
It would be nice to also order by `_pos` to preserve the underlying ordering. Maybe we could detect a subquery that would be rewritten and avoid adding the shuffle in that case?
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r520015532
##########
File path: spark3/src/main/java/org/apache/spark/sql/connector/catalog/SupportsRowLevelOperations.java
##########
@@ -0,0 +1,27 @@
+/*
+ * 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.spark.sql.connector.catalog;
+
+import org.apache.spark.sql.connector.write.LogicalWriteInfo;
+import org.apache.spark.sql.connector.write.RowLevelOperationsBuilder;
+
+public interface SupportsRowLevelOperations extends Table {
Review comment:
After thinking about this a bit more, I think we do want to use the combined scan/write builder for all cases. Sharing information between the two is much more obvious and clean that way, and we definitely need the combined builder to signal the scan not to filter within files (delete groups).
If we use the same builder for both, then I think we could call it `MergeBuilder` to solve the problem of ambiguity of "replace". That works since we can think of `DELETE FROM` as a special case of `MERGE INTO`.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519993262
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/DeleteCheck.scala
##########
@@ -0,0 +1,49 @@
+/*
+ * 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.spark.sql.catalyst.analysis
+
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.catalyst.expressions.{Expression, InSubquery, Not}
+import org.apache.spark.sql.catalyst.plans.logical.{DeleteFromTable, LogicalPlan}
+
+object DeleteCheck extends (LogicalPlan => Unit) {
Review comment:
Sounds good to me. I like the more specific name and it is good to follow existing conventions.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519991445
##########
File path: core/src/main/java/org/apache/iceberg/TableProperties.java
##########
@@ -134,4 +134,10 @@ private TableProperties() {
public static final String ENGINE_HIVE_ENABLED = "engine.hive.enabled";
public static final boolean ENGINE_HIVE_ENABLED_DEFAULT = false;
+
+ public static final String ROW_LEVEL_OPS_ISOLATION_LEVEL = "row-level-ops.isolation-level";
+ public static final String ROW_LEVEL_OPS_ISOLATION_LEVEL_DEFAULT = "serializable";
Review comment:
I'd probably group this in `write` because it is a setting that affects write behavior. If we don't want it to be specific to Spark, then we can make it `write.isolation.default-level` maybe?
I'm okay not making it specific to Spark, but it may be something that not every engine respects. Are we okay with that?
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519062452
##########
File path: spark3/src/main/java/org/apache/iceberg/spark/source/OverwriteByFilterBatchWrite.java
##########
@@ -0,0 +1,59 @@
+/*
+ * 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.DataFile;
+import org.apache.iceberg.OverwriteFiles;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.encryption.EncryptionManager;
+import org.apache.iceberg.expressions.Expression;
+import org.apache.iceberg.io.FileIO;
+import org.apache.spark.broadcast.Broadcast;
+import org.apache.spark.sql.connector.write.WriterCommitMessage;
+import org.apache.spark.sql.types.StructType;
+import org.apache.spark.sql.util.CaseInsensitiveStringMap;
+
+class OverwriteByFilterBatchWrite extends BaseBatchWrite {
Review comment:
Some of these refactors could go in separate PRs fairly easily.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519065312
##########
File path: spark3/src/main/java/org/apache/spark/sql/connector/read/SupportsReadFileFilter.java
##########
@@ -0,0 +1,26 @@
+/*
+ * 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.spark.sql.connector.read;
+
+import java.util.Set;
+
+public interface SupportsReadFileFilter extends Scan {
+ void filterFiles(Set<String> locations);
+}
Review comment:
I think we have both `SupportsReadFileFilter` and `SupportsWriteFileFilter` so that we can check that we have both or else we won't call `filterFiles`.
That seems reasonable to me, but it would be nice to have a comment in the docs for both interfaces stating that this is a requirement.
Also, there are alternatives to this that may be simpler. Since we are constructing the write and the read at the same time, we could rely on them to coordinate and call `filterFiles` just once. The write could be passed the scan when it's created so that it always uses the scan's latest set of files, for example.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519560678
##########
File path: spark3/src/main/java/org/apache/iceberg/spark/source/SparkFileBatchScan.java
##########
@@ -0,0 +1,161 @@
+/*
+ * 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 java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.iceberg.CombinedScanTask;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.encryption.EncryptionManager;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.util.PropertyUtil;
+import org.apache.iceberg.util.TableScanUtil;
+import org.apache.spark.broadcast.Broadcast;
+import org.apache.spark.sql.connector.read.Statistics;
+import org.apache.spark.sql.connector.read.SupportsReadFileFilter;
+import org.apache.spark.sql.util.CaseInsensitiveStringMap;
+
+import static org.apache.iceberg.TableProperties.SPLIT_LOOKBACK;
+import static org.apache.iceberg.TableProperties.SPLIT_LOOKBACK_DEFAULT;
+import static org.apache.iceberg.TableProperties.SPLIT_OPEN_FILE_COST;
+import static org.apache.iceberg.TableProperties.SPLIT_OPEN_FILE_COST_DEFAULT;
+import static org.apache.iceberg.TableProperties.SPLIT_SIZE;
+import static org.apache.iceberg.TableProperties.SPLIT_SIZE_DEFAULT;
+
+public class SparkFileBatchScan extends BaseBatchScan implements SupportsReadFileFilter {
+ private List<FileScanTask> files;
+
+ // lazy cache of tasks
+ private List<CombinedScanTask> tasks = null;
+
+ public SparkFileBatchScan(Table table, List<FileScanTask> files, Broadcast<FileIO> io,
+ Broadcast<EncryptionManager> encryptionManager, boolean caseSensitive,
+ CaseInsensitiveStringMap options) {
+ super(table, table.schema(), io, encryptionManager, caseSensitive, options);
+ this.files = files;
Review comment:
I like this idea, let me prototype tomorrow.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r520953087
##########
File path: spark3/src/main/java/org/apache/iceberg/spark/source/SparkTable.java
##########
@@ -155,6 +163,39 @@ public WriteBuilder newWriteBuilder(LogicalWriteInfo info) {
return new SparkWriteBuilder(sparkSession(), icebergTable, info);
}
+ @Override
+ public MergeBuilder newMergeBuilder(LogicalWriteInfo info) {
+ String mode = icebergTable.properties().getOrDefault(WRITE_ROW_LEVEL_MODE, WRITE_ROW_LEVEL_MODE_DEFAULT);
+ ValidationException.check(mode.equals("copy-on-write"), "Unsupported row operations mode: %s", mode);
+ return new SparkMergeBuilder(sparkSession(), icebergTable, info);
+ }
+
+ @Override
+ public boolean canDeleteWhere(Filter[] filters) {
+ // TODO: multiple partition specs
Review comment:
If there are multiple, return false?
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r520958311
##########
File path: spark3/src/main/java/org/apache/iceberg/spark/source/SparkMergeBuilder.java
##########
@@ -0,0 +1,100 @@
+/*
+ * 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 java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import org.apache.iceberg.IsolationLevel;
+import org.apache.iceberg.Snapshot;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.TableProperties;
+import org.apache.spark.sql.SparkSession;
+import org.apache.spark.sql.connector.read.ScanBuilder;
+import org.apache.spark.sql.connector.write.LogicalWriteInfo;
+import org.apache.spark.sql.connector.write.MergeBuilder;
+import org.apache.spark.sql.connector.write.WriteBuilder;
+import org.apache.spark.sql.util.CaseInsensitiveStringMap;
+
+class SparkMergeBuilder implements MergeBuilder {
+
+ private final SparkSession spark;
+ private final Table table;
+ private final LogicalWriteInfo writeInfo;
+ private final IsolationLevel isolationLevel;
+
+ // lazy vars
+ private ScanBuilder lazyScanBuilder;
+ private WriteBuilder lazyWriteBuilder;
+
+ SparkMergeBuilder(SparkSession spark, Table table, LogicalWriteInfo writeInfo) {
+ this.spark = spark;
+ this.table = table;
+ this.writeInfo = writeInfo;
+
+ String isolationLevelAsString = table.properties().getOrDefault(
+ TableProperties.WRITE_ISOLATION_LEVEL,
+ TableProperties.WRITE_ISOLATION_LEVEL_DEFAULT
+ ).toUpperCase(Locale.ROOT);
+ this.isolationLevel = IsolationLevel.valueOf(isolationLevelAsString);
+ }
+
+ @Override
+ public ScanBuilder asScanBuilder() {
+ return scanBuilder();
+ }
+
+ private ScanBuilder scanBuilder() {
+ if (lazyScanBuilder == null) {
+ // ignore residuals to ensure we read full files
+ SparkScanBuilder scanBuilder = new SparkScanBuilder(spark, table, scanOptions());
Review comment:
To capture the scan that gets built, you can use an anonymous subclass of `SparkScanBuilder`:
```java
SparkScanBuilder scanBuilder = new SparkScanBuilder(spark, table, scanOptions()) {
public Scan build() {
Scan configuredScan = super.build();
SparkMergeBuilder.this.configuredScan = configuredScan;
return configuredScan;
}
};
```
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519551905
##########
File path: core/src/main/java/org/apache/iceberg/TableProperties.java
##########
@@ -134,4 +134,10 @@ private TableProperties() {
public static final String ENGINE_HIVE_ENABLED = "engine.hive.enabled";
public static final boolean ENGINE_HIVE_ENABLED_DEFAULT = false;
+
+ public static final String ROW_LEVEL_OPS_ISOLATION_LEVEL = "row-level-ops.isolation-level";
+ public static final String ROW_LEVEL_OPS_ISOLATION_LEVEL_DEFAULT = "serializable";
+
+ public static final String ROW_LEVEL_OPS_MODE = "row-level-ops.mode";
+ public static final String ROW_LEVEL_OPS_MODE_DEFAULT = "copy-on-write";
Review comment:
I'll be happy to reconsider the naming but `write.spark.mode` seems to generic to me. If I read it in the docs, it would not be immediately clear to me that it applies only to UPDATE/DELETE/MERGE.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r521688170
##########
File path: spark3/src/main/java/org/apache/iceberg/spark/source/SparkMergeBuilder.java
##########
@@ -0,0 +1,100 @@
+/*
+ * 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 java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import org.apache.iceberg.IsolationLevel;
+import org.apache.iceberg.Snapshot;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.TableProperties;
+import org.apache.spark.sql.SparkSession;
+import org.apache.spark.sql.connector.read.ScanBuilder;
+import org.apache.spark.sql.connector.write.LogicalWriteInfo;
+import org.apache.spark.sql.connector.write.MergeBuilder;
+import org.apache.spark.sql.connector.write.WriteBuilder;
+import org.apache.spark.sql.util.CaseInsensitiveStringMap;
+
+class SparkMergeBuilder implements MergeBuilder {
+
+ private final SparkSession spark;
+ private final Table table;
+ private final LogicalWriteInfo writeInfo;
+ private final IsolationLevel isolationLevel;
+
+ // lazy vars
+ private ScanBuilder lazyScanBuilder;
+ private WriteBuilder lazyWriteBuilder;
+
+ SparkMergeBuilder(SparkSession spark, Table table, LogicalWriteInfo writeInfo) {
+ this.spark = spark;
+ this.table = table;
+ this.writeInfo = writeInfo;
+
+ String isolationLevelAsString = table.properties().getOrDefault(
+ TableProperties.WRITE_ISOLATION_LEVEL,
+ TableProperties.WRITE_ISOLATION_LEVEL_DEFAULT
+ ).toUpperCase(Locale.ROOT);
+ this.isolationLevel = IsolationLevel.valueOf(isolationLevelAsString);
+ }
+
+ @Override
+ public ScanBuilder asScanBuilder() {
+ return scanBuilder();
+ }
+
+ private ScanBuilder scanBuilder() {
+ if (lazyScanBuilder == null) {
+ // ignore residuals to ensure we read full files
+ SparkScanBuilder scanBuilder = new SparkScanBuilder(spark, table, scanOptions());
+ lazyScanBuilder = scanBuilder.ignoreResiduals();
+ }
+
+ return lazyScanBuilder;
+ }
+
+ private CaseInsensitiveStringMap scanOptions() {
Review comment:
Updated.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r520957396
##########
File path: spark3/src/main/java/org/apache/iceberg/spark/source/SparkMergeBuilder.java
##########
@@ -0,0 +1,100 @@
+/*
+ * 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 java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import org.apache.iceberg.IsolationLevel;
+import org.apache.iceberg.Snapshot;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.TableProperties;
+import org.apache.spark.sql.SparkSession;
+import org.apache.spark.sql.connector.read.ScanBuilder;
+import org.apache.spark.sql.connector.write.LogicalWriteInfo;
+import org.apache.spark.sql.connector.write.MergeBuilder;
+import org.apache.spark.sql.connector.write.WriteBuilder;
+import org.apache.spark.sql.util.CaseInsensitiveStringMap;
+
+class SparkMergeBuilder implements MergeBuilder {
+
+ private final SparkSession spark;
+ private final Table table;
+ private final LogicalWriteInfo writeInfo;
+ private final IsolationLevel isolationLevel;
+
+ // lazy vars
+ private ScanBuilder lazyScanBuilder;
+ private WriteBuilder lazyWriteBuilder;
+
+ SparkMergeBuilder(SparkSession spark, Table table, LogicalWriteInfo writeInfo) {
+ this.spark = spark;
+ this.table = table;
+ this.writeInfo = writeInfo;
+
+ String isolationLevelAsString = table.properties().getOrDefault(
+ TableProperties.WRITE_ISOLATION_LEVEL,
+ TableProperties.WRITE_ISOLATION_LEVEL_DEFAULT
+ ).toUpperCase(Locale.ROOT);
+ this.isolationLevel = IsolationLevel.valueOf(isolationLevelAsString);
+ }
+
+ @Override
+ public ScanBuilder asScanBuilder() {
+ return scanBuilder();
+ }
+
+ private ScanBuilder scanBuilder() {
+ if (lazyScanBuilder == null) {
+ // ignore residuals to ensure we read full files
+ SparkScanBuilder scanBuilder = new SparkScanBuilder(spark, table, scanOptions());
+ lazyScanBuilder = scanBuilder.ignoreResiduals();
+ }
+
+ return lazyScanBuilder;
+ }
+
+ private CaseInsensitiveStringMap scanOptions() {
Review comment:
I think this needs to also respect `writeInfo.options()`, right?
We may want to fail if options specifies a snapshot ID, but we shouldn't just ignore the options passed to the write.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519069333
##########
File path: spark3/src/main/java/org/apache/spark/sql/connector/write/SupportsPushDownRowLevelFilters.java
##########
@@ -0,0 +1,27 @@
+/*
+ * 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.spark.sql.connector.write;
+
+import org.apache.spark.sql.sources.Filter;
+
+public interface SupportsPushDownRowLevelFilters extends RowLevelOperationsBuilder {
Review comment:
I think if we use a `ScanBuilder` created by `ReplaceBuilder.asScanBuilder` then we should be able to use the one for scans.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519072639
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteDelete.scala
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import java.util.UUID
+import org.apache.spark.sql.{sources, AnalysisException}
+import org.apache.spark.sql.catalyst.analysis.Resolver
+import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeReference, EqualNullSafe, Expression, InputFileName, Literal, Not, PredicateHelper, SubqueryExpression}
+import org.apache.spark.sql.catalyst.expressions.Literal.FalseLiteral
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, DeleteFromTable, DynamicFileFilter, Filter, LocalRelation, LogicalPlan, OverwriteFiles, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{SupportsMetadataOnlyDeletes, Table}
+import org.apache.spark.sql.connector.write.{LogicalWriteInfo, LogicalWriteInfoImpl, RowLevelOperationsBuilder, SupportsPushDownRowLevelFilters}
+import org.apache.spark.sql.execution.datasources.DataSourceStrategy
+import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, DataSourceV2ScanRelation}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.{BooleanType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+// TODO: should be part of early scan push down after the delete condition is optimized
+object RewriteDelete extends Rule[LogicalPlan] with PredicateHelper {
+
+ import org.apache.spark.sql.execution.datasources.v2.ExtendedDataSourceV2Implicits._
+
+ private val FILE_NAME_COL = "_file"
+
+ override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+ // no need to execute this delete as the condition is false
+ case DeleteFromTable(_, Some(FalseLiteral)) =>
+ LocalRelation()
Review comment:
I think this should be done in `run` instead of replacing the plan with a no-op. That way it is clear that the plan did run, it just didn't have an effect.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519074189
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteDelete.scala
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import java.util.UUID
+import org.apache.spark.sql.{sources, AnalysisException}
+import org.apache.spark.sql.catalyst.analysis.Resolver
+import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeReference, EqualNullSafe, Expression, InputFileName, Literal, Not, PredicateHelper, SubqueryExpression}
+import org.apache.spark.sql.catalyst.expressions.Literal.FalseLiteral
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, DeleteFromTable, DynamicFileFilter, Filter, LocalRelation, LogicalPlan, OverwriteFiles, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{SupportsMetadataOnlyDeletes, Table}
+import org.apache.spark.sql.connector.write.{LogicalWriteInfo, LogicalWriteInfoImpl, RowLevelOperationsBuilder, SupportsPushDownRowLevelFilters}
+import org.apache.spark.sql.execution.datasources.DataSourceStrategy
+import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, DataSourceV2ScanRelation}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.{BooleanType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+// TODO: should be part of early scan push down after the delete condition is optimized
+object RewriteDelete extends Rule[LogicalPlan] with PredicateHelper {
+
+ import org.apache.spark.sql.execution.datasources.v2.ExtendedDataSourceV2Implicits._
+
+ private val FILE_NAME_COL = "_file"
+
+ override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+ // no need to execute this delete as the condition is false
+ case DeleteFromTable(_, Some(FalseLiteral)) =>
+ LocalRelation()
+
+ // don't rewrite deletes that can be answered using metadata only operations
+ case d @ DeleteFromTable(r: DataSourceV2Relation, Some(cond)) if isMetadataOnlyOperation(r, cond) =>
+ d
+
+ // rewrite all operations that require reading the table to delete records
+ case DeleteFromTable(r: DataSourceV2Relation, Some(cond)) =>
+ // TODO: do a switch based on whether we get BatchWrite or DeltaBatchWrite
+ val writeInfo = newWriteInfo(r.schema)
+ val builder = r.table.asRowLevelModifiable.newRowLevelOperationsBuilder(writeInfo)
+
+ pushDownFilters(builder, cond, r.output)
+
+ val scan = builder.buildScan()
+ val scanRelation = DataSourceV2ScanRelation(r.table, scan, r.output)
+ val filterPlan = buildFilterPlan(cond, scanRelation)
+
+ val batchWrite = builder.buildBatchWrite()
+ // TODO: group by something so that we can write back
+ // Option 1: repartition/sort by partition values
+ // Option 2: repartition by _file and local sort by partition values (how to deal with 1 GB files?)
+ OverwriteFiles(r, batchWrite, filterPlan)
+ // OverwriteFiles(r, batchWrite, Project(r.output, filterPlan))
+ }
+
+ private def isMetadataOnlyOperation(relation: DataSourceV2Relation, cond: Expression): Boolean = {
+ relation.table match {
+ case t: SupportsMetadataOnlyDeletes if isMetadataOnlyCondition(t, cond) =>
+ val dataSourceFilters = toDataSourceFilters(cond, relation.output)
+ t.canDeleteUsingMetadataWhere(dataSourceFilters)
+ case _ => false
+ }
+ }
+
+ private def isMetadataOnlyCondition(table: Table, cond: Expression): Boolean = {
+ val partitionColumnRefs = table.partitioning.flatMap(_.references)
+ val partitionColumnNames = partitionColumnRefs.map(_.fieldNames.mkString(".")).toSet
+
+ val resolver = SQLConf.get.resolver
+ val predicates = splitConjunctivePredicates(cond)
+
+ predicates.forall { pred =>
+ val isExecutable = pred.deterministic && !SubqueryExpression.hasSubquery(pred)
+ isExecutable && isMetadataPredicate(pred, partitionColumnNames, resolver)
+ }
+ }
+
+ private def isMetadataPredicate(
+ predicate: Expression,
+ partitionColumnNames: Set[String],
+ resolver: Resolver): Boolean = {
+ predicate.references.forall { reference => partitionColumnNames.exists(resolver(reference.name, _)) }
+ }
+
+ private def newWriteInfo(schema: StructType): LogicalWriteInfo = {
+ val uuid = UUID.randomUUID()
+ LogicalWriteInfoImpl(queryId = uuid.toString, schema, CaseInsensitiveStringMap.empty)
+ }
+
+ private def pushDownFilters(
+ builder: RowLevelOperationsBuilder,
+ cond: Expression,
+ output: Seq[AttributeReference]): Unit = {
+
+ builder match {
+ case s: SupportsPushDownRowLevelFilters =>
+ val dataSourceFilters = toDataSourceFilters(cond, output)
+ s.pushFilters(dataSourceFilters)
+ case _ => // do nothing
+ }
+ }
+
+ private def toDataSourceFilters(
+ cond: Expression,
+ output: Seq[AttributeReference]): Array[sources.Filter] = {
+
+ val predicates = splitConjunctivePredicates(cond)
+ val normalizedPredicates = DataSourceStrategy.normalizeExprs(predicates, output)
+ toDataSourceFilters(normalizedPredicates)
+ }
+
+ private def toDataSourceFilters(
+ predicates: Seq[Expression],
+ skipUntranslated: Boolean = true): Array[sources.Filter] = {
+
+ predicates.flatMap { p =>
+ val translatedFilter = DataSourceStrategy.translateFilter(p, supportNestedPredicatePushdown = true)
+ if (translatedFilter.isEmpty && !skipUntranslated) {
+ throw new AnalysisException(s"Cannot translate expression to source filter: $p")
+ }
+ translatedFilter
+ }.toArray
+ }
+
+ private def buildFilterPlan(cond: Expression, scanRelation: DataSourceV2ScanRelation): LogicalPlan = {
+ val fileNameExpr = Alias(InputFileName(), FILE_NAME_COL)()
+ val fileNameProjection = Project(scanRelation.output :+ fileNameExpr, scanRelation)
+ val matchingFilter = Filter(cond, fileNameProjection)
+ val fileAttr = findOutputAttr(matchingFilter, FILE_NAME_COL)
+ val matchingFileAgg = Aggregate(Seq(fileAttr), Seq(fileAttr), matchingFilter)
+ // TODO: inject DynamicFileFilter only if either Scan or Write support it
+ val dynamicFileFilter = DynamicFileFilter(scanRelation, matchingFileAgg)
Review comment:
You might want to separate this into two clear sections: the delete plan with the `notMatchingRowFilter` and the dynamic filter plan with `matchingFilter` and `matchingFileAgg`. I was expecting the former when I started reading this method.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519559625
##########
File path: spark3/src/main/java/org/apache/spark/sql/connector/catalog/SupportsRowLevelOperations.java
##########
@@ -0,0 +1,27 @@
+/*
+ * 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.spark.sql.connector.catalog;
+
+import org.apache.spark.sql.connector.write.LogicalWriteInfo;
+import org.apache.spark.sql.connector.write.RowLevelOperationsBuilder;
+
+public interface SupportsRowLevelOperations extends Table {
Review comment:
The only worry I have with `replace` is the meaning it has in Iceberg. When I hear `replace`, I usually assume a compaction job where the data does not change. It feels more like an overwrite to me if we are talking about copy-on-write. Also, do you see a separate interface for merge-on-read? My intent was to have one.
I am not entirely sure we don't need any coordination between scans and writes for merge-on-read, though. For example, we still want to propagate the expression that was used to scan for checking concurrently appended files under serializable isolation.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r521670864
##########
File path: spark3/src/main/java/org/apache/iceberg/spark/source/SparkTable.java
##########
@@ -155,6 +163,39 @@ public WriteBuilder newWriteBuilder(LogicalWriteInfo info) {
return new SparkWriteBuilder(sparkSession(), icebergTable, info);
}
+ @Override
+ public MergeBuilder newMergeBuilder(LogicalWriteInfo info) {
+ String mode = icebergTable.properties().getOrDefault(WRITE_ROW_LEVEL_MODE, WRITE_ROW_LEVEL_MODE_DEFAULT);
+ ValidationException.check(mode.equals("copy-on-write"), "Unsupported row operations mode: %s", mode);
+ return new SparkMergeBuilder(sparkSession(), icebergTable, info);
+ }
+
+ @Override
+ public boolean canDeleteWhere(Filter[] filters) {
+ // TODO: multiple partition specs
+ Set<Integer> partitionFieldSourceIds = PartitionUtil.sourceFieldIds(table().spec());
+ Schema schema = table().schema();
+
+ for (Filter filter : filters) {
+ // return false if we have a predicate on a non-partition column or if we cannot translate the filter
+ if (isDataFilter(filter, schema, partitionFieldSourceIds) || SparkFilters.convert(filter) == null) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ private boolean isDataFilter(Filter filter, Schema schema, Set<Integer> partitionFieldSourceIds) {
+ // TODO: handle dots correctly via v2references
+ Set<String> filterRefs = Sets.newHashSet(filter.references());
+ return filterRefs.stream().anyMatch(ref -> {
+ Types.NestedField field = schema.findField(ref);
+ ValidationException.check(field != null, "Cannot find field %s in schema", ref);
+ return !partitionFieldSourceIds.contains(field.fieldId());
Review comment:
You are right, if a table is partitioned by `day(ts)`, it is not safe to execute `ts < timestamp`.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r520056497
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteDelete.scala
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import java.util.UUID
+import org.apache.spark.sql.{sources, AnalysisException}
+import org.apache.spark.sql.catalyst.analysis.Resolver
+import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeReference, EqualNullSafe, Expression, InputFileName, Literal, Not, PredicateHelper, SubqueryExpression}
+import org.apache.spark.sql.catalyst.expressions.Literal.FalseLiteral
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, DeleteFromTable, DynamicFileFilter, Filter, LocalRelation, LogicalPlan, OverwriteFiles, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{SupportsMetadataOnlyDeletes, Table}
+import org.apache.spark.sql.connector.write.{LogicalWriteInfo, LogicalWriteInfoImpl, RowLevelOperationsBuilder, SupportsPushDownRowLevelFilters}
+import org.apache.spark.sql.execution.datasources.DataSourceStrategy
+import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, DataSourceV2ScanRelation}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.{BooleanType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+// TODO: should be part of early scan push down after the delete condition is optimized
+object RewriteDelete extends Rule[LogicalPlan] with PredicateHelper {
+
+ import org.apache.spark.sql.execution.datasources.v2.ExtendedDataSourceV2Implicits._
+
+ private val FILE_NAME_COL = "_file"
+
+ override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+ // no need to execute this delete as the condition is false
+ case DeleteFromTable(_, Some(FalseLiteral)) =>
+ LocalRelation()
+
+ // don't rewrite deletes that can be answered using metadata only operations
+ case d @ DeleteFromTable(r: DataSourceV2Relation, Some(cond)) if isMetadataOnlyOperation(r, cond) =>
+ d
+
+ // rewrite all operations that require reading the table to delete records
+ case DeleteFromTable(r: DataSourceV2Relation, Some(cond)) =>
+ // TODO: do a switch based on whether we get BatchWrite or DeltaBatchWrite
+ val writeInfo = newWriteInfo(r.schema)
+ val builder = r.table.asRowLevelModifiable.newRowLevelOperationsBuilder(writeInfo)
+
+ pushDownFilters(builder, cond, r.output)
+
+ val scan = builder.buildScan()
+ val scanRelation = DataSourceV2ScanRelation(r.table, scan, r.output)
+ val filterPlan = buildFilterPlan(cond, scanRelation)
+
+ val batchWrite = builder.buildBatchWrite()
+ // TODO: group by something so that we can write back
+ // Option 1: repartition/sort by partition values
+ // Option 2: repartition by _file and local sort by partition values (how to deal with 1 GB files?)
Review comment:
Subquery execution will introduce a shuffle so the data will be everywhere. If we decide to write it after that, we will produce a huge number of small files. Also, if we don't add a shuffle, we probably won't be able to write 1-1 files as the input split size will control our target file size.
In our internal implementation, we went for option 1 initially. In certain cases, this poses a problem. For example, let's say you compacted your table and produced 1GB sorted files (it is an extremely expensive operation). If you do a delete and try to resort the data again before writing, this will be way too expensive. Moreover, the file sizes won't be ideal so you will most likely have to compact again.
I think we should try to write 1-1 replacements. That way, if you had 1 GB sorted file, you would delete records and write 1GB file again. If we introduce a global sort by `_file` that will be hard to scale (e.g. extra job for skew estimation, having a single task write that much of data, etc). Instead, we may hash partition it by `_file`.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r521654610
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/DynamicFileFilter.scala
##########
@@ -0,0 +1,33 @@
+/*
+ * 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.spark.sql.catalyst.plans.logical
+
+import org.apache.spark.sql.catalyst.expressions.Attribute
+import org.apache.spark.sql.execution.datasources.v2.DataSourceV2ScanRelation
+
+// TODO: fix stats (ignore the fact it is a binary node and report only scanRelation stats)
+case class DynamicFileFilter(
Review comment:
@rdblue, could you elaborate a bit on this one?
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r520949968
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteDelete.scala
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import java.util.UUID
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.{sources, AnalysisException}
+import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeReference, EqualNullSafe, Expression, InputFileName, Literal, Not, PredicateHelper, SubqueryExpression}
+import org.apache.spark.sql.catalyst.expressions.Literal.FalseLiteral
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, DeleteFromTable, DynamicFileFilter, Filter, LocalRelation, LogicalPlan, Project, ReplaceData}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{ExtendedSupportsDelete, Table}
+import org.apache.spark.sql.connector.read.{Scan, ScanBuilder, SupportsFileFilter}
+import org.apache.spark.sql.connector.write.{LogicalWriteInfo, LogicalWriteInfoImpl, MergeBuilder, RequiresScan}
+import org.apache.spark.sql.execution.datasources.DataSourceStrategy
+import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, DataSourceV2ScanRelation, PushDownUtils}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.{BooleanType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+// TODO: should be part of early scan push down after the delete condition is optimized
+object RewriteDelete extends Rule[LogicalPlan] with PredicateHelper with Logging {
+
+ import org.apache.spark.sql.execution.datasources.v2.ExtendedDataSourceV2Implicits._
+
+ private val FILE_NAME_COL = "_file"
+
+ override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+ // no need to execute this delete as the condition is false
+ case DeleteFromTable(_, Some(FalseLiteral)) =>
+ LocalRelation()
+
+ // don't rewrite deletes that can be answered by passing filters to deleteWhere in SupportsDelete
+ case d @ DeleteFromTable(r: DataSourceV2Relation, Some(cond)) if isDeleteWhereCase(r, cond) =>
+ d
+
+ // rewrite all operations that require reading the table to delete records
+ case DeleteFromTable(r: DataSourceV2Relation, Some(cond)) =>
+ // TODO: do a switch based on whether we get BatchWrite or DeltaBatchWrite
+ val writeInfo = newWriteInfo(r.schema)
+ val mergeBuilder = r.table.asMergeable.newMergeBuilder(writeInfo)
+
+ val (scan, scanPlan) = buildScanPlan(r.table, r.output, mergeBuilder, cond)
+
+ val remainingRowFilter = Not(EqualNullSafe(cond, Literal(true, BooleanType)))
+ val remainingRowsPlan = Filter(remainingRowFilter, scanPlan)
+
+ val batchWrite = mergeBuilder.asWriteBuilder.buildForBatch()
+ batchWrite match {
+ case w: RequiresScan => w.withScan(scan)
+ case _ => // do nothing
+ }
+
+ // TODO: group by something so that we can write back
+ // Option 1: repartition/sort by partition values
+ // Option 2: repartition by _file and local sort by partition values (how to deal with 1 GB files?)
+ ReplaceData(r, batchWrite, remainingRowsPlan)
+ }
+
+ private def buildScanPlan(
+ table: Table,
+ output: Seq[AttributeReference],
+ mergeBuilder: MergeBuilder,
+ cond: Expression): (Scan, LogicalPlan) = {
+
+ val scanBuilder = mergeBuilder.asScanBuilder
+
+ val predicates = splitConjunctivePredicates(cond)
+ val normalizedPredicates = DataSourceStrategy.normalizeExprs(predicates, output)
+ PushDownUtils.pushFilters(scanBuilder, normalizedPredicates)
+
+ val scan = scanBuilder.build()
+ val scanRelation = DataSourceV2ScanRelation(table, scan, output)
+
+ scan match {
Review comment:
I like this better.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r521680849
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/execution/datasources/v2/ReplaceDataExec.scala
##########
@@ -0,0 +1,47 @@
+/*
+ * 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.spark.sql.execution.datasources.v2
+
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.connector.write.BatchWrite
+import org.apache.spark.sql.execution.SparkPlan
+
+case class ReplaceDataExec(batchWrite: BatchWrite, queryExec: SparkPlan) extends V2TableWriteExec {
+
+ // override child so that we can properly plan queryExec without executing the file filter
+ override def child: SparkPlan = queryExec
+
+ // TODO: doPrepare is NOT invoked by V2TableWriteExec
Review comment:
I thought that prepare should automatically be called, but it looks like our fix is to update `result` to call `prepare` and then `run`.
That would work here:
```
def run(): ... {
prepare()
...
}
```
Calling it in `query` should work, too.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519056245
##########
File path: spark3/src/main/java/org/apache/iceberg/spark/source/BaseBatchScan.java
##########
@@ -0,0 +1,263 @@
+/*
+ * 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 java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import org.apache.iceberg.CombinedScanTask;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.SchemaParser;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.TableProperties;
+import org.apache.iceberg.encryption.EncryptionManager;
+import org.apache.iceberg.hadoop.HadoopInputFile;
+import org.apache.iceberg.hadoop.Util;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.mapping.NameMapping;
+import org.apache.iceberg.mapping.NameMappingParser;
+import org.apache.iceberg.spark.Spark3Util;
+import org.apache.iceberg.spark.SparkSchemaUtil;
+import org.apache.iceberg.util.TableScanUtil;
+import org.apache.spark.broadcast.Broadcast;
+import org.apache.spark.sql.catalyst.InternalRow;
+import org.apache.spark.sql.connector.read.Batch;
+import org.apache.spark.sql.connector.read.InputPartition;
+import org.apache.spark.sql.connector.read.PartitionReader;
+import org.apache.spark.sql.connector.read.PartitionReaderFactory;
+import org.apache.spark.sql.connector.read.Scan;
+import org.apache.spark.sql.connector.read.SupportsReportStatistics;
+import org.apache.spark.sql.types.StructType;
+import org.apache.spark.sql.util.CaseInsensitiveStringMap;
+import org.apache.spark.sql.vectorized.ColumnarBatch;
+
+abstract class BaseBatchScan implements Scan, Batch, SupportsReportStatistics {
+ protected final Table table;
Review comment:
Having these vars as protected breaks our style checks but having a getter for each of them sounds pretty bad.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519056767
##########
File path: core/src/main/java/org/apache/iceberg/TableProperties.java
##########
@@ -134,4 +134,10 @@ private TableProperties() {
public static final String ENGINE_HIVE_ENABLED = "engine.hive.enabled";
public static final boolean ENGINE_HIVE_ENABLED_DEFAULT = false;
+
+ public static final String ROW_LEVEL_OPS_ISOLATION_LEVEL = "row-level-ops.isolation-level";
+ public static final String ROW_LEVEL_OPS_ISOLATION_LEVEL_DEFAULT = "serializable";
Review comment:
What about `write.spark.isolation-level`? And if we want to be more specific, we could use `write.spark.delete-from.isolation-level`.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519560393
##########
File path: spark3/src/main/java/org/apache/spark/sql/connector/write/RowLevelOperationsBuilder.java
##########
@@ -0,0 +1,30 @@
+/*
+ * 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.spark.sql.connector.write;
+
+import org.apache.spark.sql.connector.read.Scan;
+
+// TODO: how to represent merge-on-read?
+// Option 1: Introduce DeltaBatchWrite that should be returned for merge-on-read only, use BatchWrite for copy-on-write
+// Option 2: Introduce DeltaBatchWrite with keepUnchanged flag and use that flag to do copy-on-write/merge-on-read
+public interface RowLevelOperationsBuilder {
Review comment:
Let me think about this tomorrow.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r520950174
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteDelete.scala
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import java.util.UUID
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.{sources, AnalysisException}
+import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeReference, EqualNullSafe, Expression, InputFileName, Literal, Not, PredicateHelper, SubqueryExpression}
+import org.apache.spark.sql.catalyst.expressions.Literal.FalseLiteral
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, DeleteFromTable, DynamicFileFilter, Filter, LocalRelation, LogicalPlan, Project, ReplaceData}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{ExtendedSupportsDelete, Table}
+import org.apache.spark.sql.connector.read.{Scan, ScanBuilder, SupportsFileFilter}
+import org.apache.spark.sql.connector.write.{LogicalWriteInfo, LogicalWriteInfoImpl, MergeBuilder, RequiresScan}
+import org.apache.spark.sql.execution.datasources.DataSourceStrategy
+import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, DataSourceV2ScanRelation, PushDownUtils}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.{BooleanType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+// TODO: should be part of early scan push down after the delete condition is optimized
+object RewriteDelete extends Rule[LogicalPlan] with PredicateHelper with Logging {
+
+ import org.apache.spark.sql.execution.datasources.v2.ExtendedDataSourceV2Implicits._
+
+ private val FILE_NAME_COL = "_file"
+
+ override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+ // no need to execute this delete as the condition is false
+ case DeleteFromTable(_, Some(FalseLiteral)) =>
+ LocalRelation()
+
+ // don't rewrite deletes that can be answered by passing filters to deleteWhere in SupportsDelete
+ case d @ DeleteFromTable(r: DataSourceV2Relation, Some(cond)) if isDeleteWhereCase(r, cond) =>
+ d
+
+ // rewrite all operations that require reading the table to delete records
+ case DeleteFromTable(r: DataSourceV2Relation, Some(cond)) =>
+ // TODO: do a switch based on whether we get BatchWrite or DeltaBatchWrite
+ val writeInfo = newWriteInfo(r.schema)
+ val mergeBuilder = r.table.asMergeable.newMergeBuilder(writeInfo)
+
+ val (scan, scanPlan) = buildScanPlan(r.table, r.output, mergeBuilder, cond)
+
+ val remainingRowFilter = Not(EqualNullSafe(cond, Literal(true, BooleanType)))
+ val remainingRowsPlan = Filter(remainingRowFilter, scanPlan)
+
+ val batchWrite = mergeBuilder.asWriteBuilder.buildForBatch()
+ batchWrite match {
+ case w: RequiresScan => w.withScan(scan)
+ case _ => // do nothing
+ }
+
+ // TODO: group by something so that we can write back
+ // Option 1: repartition/sort by partition values
+ // Option 2: repartition by _file and local sort by partition values (how to deal with 1 GB files?)
+ ReplaceData(r, batchWrite, remainingRowsPlan)
+ }
+
+ private def buildScanPlan(
+ table: Table,
+ output: Seq[AttributeReference],
+ mergeBuilder: MergeBuilder,
+ cond: Expression): (Scan, LogicalPlan) = {
+
+ val scanBuilder = mergeBuilder.asScanBuilder
+
+ val predicates = splitConjunctivePredicates(cond)
+ val normalizedPredicates = DataSourceStrategy.normalizeExprs(predicates, output)
+ PushDownUtils.pushFilters(scanBuilder, normalizedPredicates)
+
+ val scan = scanBuilder.build()
+ val scanRelation = DataSourceV2ScanRelation(table, scan, output)
+
+ scan match {
+ case _: SupportsFileFilter =>
+ val matchingFilePlan = buildFileFilterPlan(cond, scanRelation)
+ val dynamicFileFilter = DynamicFileFilter(scanRelation, matchingFilePlan)
+ (scan, dynamicFileFilter)
+ case _ =>
+ (scan, scanRelation)
+ }
+ }
+
+ private def isDeleteWhereCase(relation: DataSourceV2Relation, cond: Expression): Boolean = {
+ relation.table match {
+ case t: ExtendedSupportsDelete if !SubqueryExpression.hasSubquery(cond) =>
Review comment:
I like being able to test this in planning. I think you're right about it.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r520013020
##########
File path: spark3/src/main/java/org/apache/spark/sql/connector/write/RowLevelOperationsBuilder.java
##########
@@ -0,0 +1,30 @@
+/*
+ * 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.spark.sql.connector.write;
+
+import org.apache.spark.sql.connector.read.Scan;
+
+// TODO: how to represent merge-on-read?
+// Option 1: Introduce DeltaBatchWrite that should be returned for merge-on-read only, use BatchWrite for copy-on-write
+// Option 2: Introduce DeltaBatchWrite with keepUnchanged flag and use that flag to do copy-on-write/merge-on-read
+public interface RowLevelOperationsBuilder {
Review comment:
Makes sense to me.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519552018
##########
File path: core/src/main/java/org/apache/iceberg/TableProperties.java
##########
@@ -134,4 +134,10 @@ private TableProperties() {
public static final String ENGINE_HIVE_ENABLED = "engine.hive.enabled";
public static final boolean ENGINE_HIVE_ENABLED_DEFAULT = false;
+
+ public static final String ROW_LEVEL_OPS_ISOLATION_LEVEL = "row-level-ops.isolation-level";
+ public static final String ROW_LEVEL_OPS_ISOLATION_LEVEL_DEFAULT = "serializable";
+
+ public static final String ROW_LEVEL_OPS_MODE = "row-level-ops.mode";
+ public static final String ROW_LEVEL_OPS_MODE_DEFAULT = "copy-on-write";
Review comment:
Any prefix other than `row-level-ops` that can make it more descriptive?
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519067899
##########
File path: spark3/src/main/java/org/apache/spark/sql/connector/catalog/SupportsRowLevelOperations.java
##########
@@ -0,0 +1,27 @@
+/*
+ * 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.spark.sql.connector.catalog;
+
+import org.apache.spark.sql.connector.write.LogicalWriteInfo;
+import org.apache.spark.sql.connector.write.RowLevelOperationsBuilder;
+
+public interface SupportsRowLevelOperations extends Table {
Review comment:
I think I like the name `SupportsReplace` better than `SupportsRowLevelOperations`. The operation isn't really row-level because row-level operations don't require coordination between the scan and the write: each writer is passed a row with a row operation (delete, update, or insert).
Calling this `SupportsReplace` makes sense to me because we are replacing a set of input files (groups) with a matching set of output files. Then we could use `ReplaceBuilder` as well. I don't think those names conflict with anything.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519070975
##########
File path: spark3/src/main/java/org/apache/iceberg/spark/source/SparkFileBatchScan.java
##########
@@ -0,0 +1,161 @@
+/*
+ * 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 java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.iceberg.CombinedScanTask;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.encryption.EncryptionManager;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.util.PropertyUtil;
+import org.apache.iceberg.util.TableScanUtil;
+import org.apache.spark.broadcast.Broadcast;
+import org.apache.spark.sql.connector.read.Statistics;
+import org.apache.spark.sql.connector.read.SupportsReadFileFilter;
+import org.apache.spark.sql.util.CaseInsensitiveStringMap;
+
+import static org.apache.iceberg.TableProperties.SPLIT_LOOKBACK;
+import static org.apache.iceberg.TableProperties.SPLIT_LOOKBACK_DEFAULT;
+import static org.apache.iceberg.TableProperties.SPLIT_OPEN_FILE_COST;
+import static org.apache.iceberg.TableProperties.SPLIT_OPEN_FILE_COST_DEFAULT;
+import static org.apache.iceberg.TableProperties.SPLIT_SIZE;
+import static org.apache.iceberg.TableProperties.SPLIT_SIZE_DEFAULT;
+
+public class SparkFileBatchScan extends BaseBatchScan implements SupportsReadFileFilter {
+ private List<FileScanTask> files;
+
+ // lazy cache of tasks
+ private List<CombinedScanTask> tasks = null;
+
+ public SparkFileBatchScan(Table table, List<FileScanTask> files, Broadcast<FileIO> io,
+ Broadcast<EncryptionManager> encryptionManager, boolean caseSensitive,
+ CaseInsensitiveStringMap options) {
+ super(table, table.schema(), io, encryptionManager, caseSensitive, options);
+ this.files = files;
Review comment:
I think I would pass this Scan into the write and expose `files()` to return the list. That way, we only implement `filterFiles` once and we know the two lists are always in sync.
If we do that and rely on the fact that the scan and the write are built at the same time, then we can remove the write interface to expose `filterFiles`.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519064133
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/execution/datasources/v2/ExtendedDataSourceV2Strategy.scala
##########
@@ -20,14 +20,25 @@
package org.apache.spark.sql.execution.datasources.v2
import org.apache.spark.sql.{AnalysisException, Strategy}
-import org.apache.spark.sql.catalyst.plans.logical.{CallStatement, LogicalPlan}
-import org.apache.spark.sql.execution.SparkPlan
+import org.apache.spark.sql.catalyst.plans.logical.{CallStatement, DynamicFileFilter, LogicalPlan, OverwriteFiles}
+import org.apache.spark.sql.execution.{ProjectExec, SparkPlan}
object ExtendedDataSourceV2Strategy extends Strategy {
override def apply(plan: LogicalPlan): Seq[SparkPlan] = plan match {
case _: CallStatement =>
throw new AnalysisException("CALL statements are not currently supported")
+ case DynamicFileFilter(scanRelation, fileFilterPlan) =>
+ val scanExec = ExtendedBatchScanExec(scanRelation.output, scanRelation.scan)
Review comment:
I think you should mention that this doesn't use `planLater` because we need to use `ExtendedBatchScanExec` instead of `BatchScanExec`.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519054640
##########
File path: spark3/src/main/java/org/apache/spark/sql/connector/write/RowLevelOperationsBuilder.java
##########
@@ -0,0 +1,30 @@
+/*
+ * 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.spark.sql.connector.write;
+
+import org.apache.spark.sql.connector.read.Scan;
+
+// TODO: how to represent merge-on-read?
+// Option 1: Introduce DeltaBatchWrite that should be returned for merge-on-read only, use BatchWrite for copy-on-write
+// Option 2: Introduce DeltaBatchWrite with keepUnchanged flag and use that flag to do copy-on-write/merge-on-read
+public interface RowLevelOperationsBuilder {
Review comment:
@rdblue, I'll need your input based on the merge into design doc you had.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519075194
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/OverwriteFiles.scala
##########
@@ -0,0 +1,28 @@
+/*
+ * 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.spark.sql.catalyst.plans.logical
+
+import org.apache.spark.sql.catalyst.analysis.NamedRelation
+import org.apache.spark.sql.connector.write.BatchWrite
+
+case class OverwriteFiles(
Review comment:
If we rename the builder to `ReplaceBuilder` then we'd probably want to call this `ReplaceData`. Replace because that's the operation we are using for the builder (since overwrite is part of a normal `Write`) and "data" because this isn't specific to files. It could also replace other groups, like whole partitions.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r521654069
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteDelete.scala
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import java.util.UUID
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.{sources, AnalysisException}
+import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeReference, EqualNullSafe, Expression, InputFileName, Literal, Not, PredicateHelper, SubqueryExpression}
+import org.apache.spark.sql.catalyst.expressions.Literal.FalseLiteral
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, DeleteFromTable, DynamicFileFilter, Filter, LocalRelation, LogicalPlan, Project, ReplaceData}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{ExtendedSupportsDelete, Table}
+import org.apache.spark.sql.connector.read.{Scan, ScanBuilder, SupportsFileFilter}
+import org.apache.spark.sql.connector.write.{LogicalWriteInfo, LogicalWriteInfoImpl, MergeBuilder, RequiresScan}
+import org.apache.spark.sql.execution.datasources.DataSourceStrategy
+import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, DataSourceV2ScanRelation, PushDownUtils}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.{BooleanType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+// TODO: should be part of early scan push down after the delete condition is optimized
+object RewriteDelete extends Rule[LogicalPlan] with PredicateHelper with Logging {
+
+ import org.apache.spark.sql.execution.datasources.v2.ExtendedDataSourceV2Implicits._
+
+ private val FILE_NAME_COL = "_file"
+
+ override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+ // no need to execute this delete as the condition is false
+ case DeleteFromTable(_, Some(FalseLiteral)) =>
+ LocalRelation()
+
+ // don't rewrite deletes that can be answered by passing filters to deleteWhere in SupportsDelete
+ case d @ DeleteFromTable(r: DataSourceV2Relation, Some(cond)) if isDeleteWhereCase(r, cond) =>
+ d
+
+ // rewrite all operations that require reading the table to delete records
+ case DeleteFromTable(r: DataSourceV2Relation, Some(cond)) =>
+ // TODO: do a switch based on whether we get BatchWrite or DeltaBatchWrite
+ val writeInfo = newWriteInfo(r.schema)
+ val mergeBuilder = r.table.asMergeable.newMergeBuilder(writeInfo)
+
+ val (scan, scanPlan) = buildScanPlan(r.table, r.output, mergeBuilder, cond)
+
+ val remainingRowFilter = Not(EqualNullSafe(cond, Literal(true, BooleanType)))
+ val remainingRowsPlan = Filter(remainingRowFilter, scanPlan)
+
+ val batchWrite = mergeBuilder.asWriteBuilder.buildForBatch()
+ batchWrite match {
+ case w: RequiresScan => w.withScan(scan)
+ case _ => // do nothing
+ }
+
+ // TODO: group by something so that we can write back
+ // Option 1: repartition/sort by partition values
+ // Option 2: repartition by _file and local sort by partition values (how to deal with 1 GB files?)
+ ReplaceData(r, batchWrite, remainingRowsPlan)
+ }
+
+ private def buildScanPlan(
+ table: Table,
+ output: Seq[AttributeReference],
+ mergeBuilder: MergeBuilder,
+ cond: Expression): (Scan, LogicalPlan) = {
+
+ val scanBuilder = mergeBuilder.asScanBuilder
+
+ val predicates = splitConjunctivePredicates(cond)
+ val normalizedPredicates = DataSourceStrategy.normalizeExprs(predicates, output)
+ PushDownUtils.pushFilters(scanBuilder, normalizedPredicates)
+
+ val scan = scanBuilder.build()
+ val scanRelation = DataSourceV2ScanRelation(table, scan, output)
+
+ scan match {
+ case _: SupportsFileFilter =>
+ val matchingFilePlan = buildFileFilterPlan(cond, scanRelation)
+ val dynamicFileFilter = DynamicFileFilter(scanRelation, matchingFilePlan)
+ (scan, dynamicFileFilter)
+ case _ =>
+ (scan, scanRelation)
+ }
+ }
+
+ private def isDeleteWhereCase(relation: DataSourceV2Relation, cond: Expression): Boolean = {
+ relation.table match {
+ case t: ExtendedSupportsDelete if !SubqueryExpression.hasSubquery(cond) =>
+ val predicates = splitConjunctivePredicates(cond)
+ val normalizedPredicates = DataSourceStrategy.normalizeExprs(predicates, relation.output)
+ val dataSourceFilters = toDataSourceFilters(normalizedPredicates)
+ val allPredicatesTranslated = normalizedPredicates.size == dataSourceFilters.length
+ allPredicatesTranslated && t.canDeleteWhere(dataSourceFilters)
Review comment:
Definetely.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r520949764
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteDelete.scala
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import java.util.UUID
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.{sources, AnalysisException}
+import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeReference, EqualNullSafe, Expression, InputFileName, Literal, Not, PredicateHelper, SubqueryExpression}
+import org.apache.spark.sql.catalyst.expressions.Literal.FalseLiteral
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, DeleteFromTable, DynamicFileFilter, Filter, LocalRelation, LogicalPlan, Project, ReplaceData}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{ExtendedSupportsDelete, Table}
+import org.apache.spark.sql.connector.read.{Scan, ScanBuilder, SupportsFileFilter}
+import org.apache.spark.sql.connector.write.{LogicalWriteInfo, LogicalWriteInfoImpl, MergeBuilder, RequiresScan}
+import org.apache.spark.sql.execution.datasources.DataSourceStrategy
+import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, DataSourceV2ScanRelation, PushDownUtils}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.{BooleanType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+// TODO: should be part of early scan push down after the delete condition is optimized
+object RewriteDelete extends Rule[LogicalPlan] with PredicateHelper with Logging {
+
+ import org.apache.spark.sql.execution.datasources.v2.ExtendedDataSourceV2Implicits._
+
+ private val FILE_NAME_COL = "_file"
+
+ override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+ // no need to execute this delete as the condition is false
+ case DeleteFromTable(_, Some(FalseLiteral)) =>
+ LocalRelation()
+
+ // don't rewrite deletes that can be answered by passing filters to deleteWhere in SupportsDelete
+ case d @ DeleteFromTable(r: DataSourceV2Relation, Some(cond)) if isDeleteWhereCase(r, cond) =>
+ d
+
+ // rewrite all operations that require reading the table to delete records
+ case DeleteFromTable(r: DataSourceV2Relation, Some(cond)) =>
+ // TODO: do a switch based on whether we get BatchWrite or DeltaBatchWrite
+ val writeInfo = newWriteInfo(r.schema)
+ val mergeBuilder = r.table.asMergeable.newMergeBuilder(writeInfo)
+
+ val (scan, scanPlan) = buildScanPlan(r.table, r.output, mergeBuilder, cond)
+
+ val remainingRowFilter = Not(EqualNullSafe(cond, Literal(true, BooleanType)))
+ val remainingRowsPlan = Filter(remainingRowFilter, scanPlan)
+
+ val batchWrite = mergeBuilder.asWriteBuilder.buildForBatch()
+ batchWrite match {
+ case w: RequiresScan => w.withScan(scan)
+ case _ => // do nothing
+ }
Review comment:
I would probably trust the `MergeBuilder` to do this internally. Adding `RequiresScan` isn't ideal because other writers may implement `RequiresScan`, but there is no support for passing the writer a scan in the existing write path.
Initially, I thought that maybe we could avoid this by adding `withScan` to the `MergeBuilder`, but then why not just capture the output of the scan builder inside the builder?
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r520911079
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteDelete.scala
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import java.util.UUID
+import org.apache.spark.sql.{sources, AnalysisException}
+import org.apache.spark.sql.catalyst.analysis.Resolver
+import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeReference, EqualNullSafe, Expression, InputFileName, Literal, Not, PredicateHelper, SubqueryExpression}
+import org.apache.spark.sql.catalyst.expressions.Literal.FalseLiteral
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, DeleteFromTable, DynamicFileFilter, Filter, LocalRelation, LogicalPlan, OverwriteFiles, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{SupportsMetadataOnlyDeletes, Table}
+import org.apache.spark.sql.connector.write.{LogicalWriteInfo, LogicalWriteInfoImpl, RowLevelOperationsBuilder, SupportsPushDownRowLevelFilters}
+import org.apache.spark.sql.execution.datasources.DataSourceStrategy
+import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, DataSourceV2ScanRelation}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.{BooleanType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+// TODO: should be part of early scan push down after the delete condition is optimized
+object RewriteDelete extends Rule[LogicalPlan] with PredicateHelper {
+
+ import org.apache.spark.sql.execution.datasources.v2.ExtendedDataSourceV2Implicits._
+
+ private val FILE_NAME_COL = "_file"
+
+ override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+ // no need to execute this delete as the condition is false
+ case DeleteFromTable(_, Some(FalseLiteral)) =>
+ LocalRelation()
Review comment:
Oh, I see. If this is intended to avoid this getting rewritten to a different plan, then let's add a comment to it?
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519559625
##########
File path: spark3/src/main/java/org/apache/spark/sql/connector/catalog/SupportsRowLevelOperations.java
##########
@@ -0,0 +1,27 @@
+/*
+ * 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.spark.sql.connector.catalog;
+
+import org.apache.spark.sql.connector.write.LogicalWriteInfo;
+import org.apache.spark.sql.connector.write.RowLevelOperationsBuilder;
+
+public interface SupportsRowLevelOperations extends Table {
Review comment:
The only worry I have with `replace` is the meaning it has in Iceberg. When I hear `replace`, I usually assume a compaction job where the data does not change.
Do you see a separate interface for merge-on-read? I am not entirely sure we don't need any coordination between scans and writes for merge-on-read, though. For example, we still want to propagate the expression that was used to scan for checking concurrently appended files under serializable isolation.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi closed pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi closed pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r521681450
##########
File path: core/src/main/java/org/apache/iceberg/TableProperties.java
##########
@@ -134,4 +134,10 @@ private TableProperties() {
public static final String ENGINE_HIVE_ENABLED = "engine.hive.enabled";
public static final boolean ENGINE_HIVE_ENABLED_DEFAULT = false;
+
+ public static final String WRITE_ISOLATION_LEVEL = "write.isolation-level";
Review comment:
Since we are using "merge" in the code, what do you think about `write.merge.isolation` instead of `row-level`?
Then if we need to, we can introduce other properties to be more specific, like `write.delete.isolation`. And I agree with engine-specific properties if we need them.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r521698615
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteDelete.scala
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import java.util.UUID
+import org.apache.spark.sql.{sources, AnalysisException}
+import org.apache.spark.sql.catalyst.analysis.Resolver
+import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeReference, EqualNullSafe, Expression, InputFileName, Literal, Not, PredicateHelper, SubqueryExpression}
+import org.apache.spark.sql.catalyst.expressions.Literal.FalseLiteral
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, DeleteFromTable, DynamicFileFilter, Filter, LocalRelation, LogicalPlan, OverwriteFiles, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{SupportsMetadataOnlyDeletes, Table}
+import org.apache.spark.sql.connector.write.{LogicalWriteInfo, LogicalWriteInfoImpl, RowLevelOperationsBuilder, SupportsPushDownRowLevelFilters}
+import org.apache.spark.sql.execution.datasources.DataSourceStrategy
+import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, DataSourceV2ScanRelation}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.{BooleanType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+// TODO: should be part of early scan push down after the delete condition is optimized
+object RewriteDelete extends Rule[LogicalPlan] with PredicateHelper {
+
+ import org.apache.spark.sql.execution.datasources.v2.ExtendedDataSourceV2Implicits._
+
+ private val FILE_NAME_COL = "_file"
+
+ override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+ // no need to execute this delete as the condition is false
+ case DeleteFromTable(_, Some(FalseLiteral)) =>
+ LocalRelation()
Review comment:
I think we just need to return early instead of trying the delete and produce a new snapshot without changes.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r521697409
##########
File path: spark3/src/main/java/org/apache/iceberg/spark/source/SparkFileBatchScan.java
##########
@@ -0,0 +1,161 @@
+/*
+ * 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 java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.iceberg.CombinedScanTask;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.encryption.EncryptionManager;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.util.PropertyUtil;
+import org.apache.iceberg.util.TableScanUtil;
+import org.apache.spark.broadcast.Broadcast;
+import org.apache.spark.sql.connector.read.Statistics;
+import org.apache.spark.sql.connector.read.SupportsReadFileFilter;
+import org.apache.spark.sql.util.CaseInsensitiveStringMap;
+
+import static org.apache.iceberg.TableProperties.SPLIT_LOOKBACK;
+import static org.apache.iceberg.TableProperties.SPLIT_LOOKBACK_DEFAULT;
+import static org.apache.iceberg.TableProperties.SPLIT_OPEN_FILE_COST;
+import static org.apache.iceberg.TableProperties.SPLIT_OPEN_FILE_COST_DEFAULT;
+import static org.apache.iceberg.TableProperties.SPLIT_SIZE;
+import static org.apache.iceberg.TableProperties.SPLIT_SIZE_DEFAULT;
+
+public class SparkFileBatchScan extends BaseBatchScan implements SupportsReadFileFilter {
+ private List<FileScanTask> files;
+
+ // lazy cache of tasks
+ private List<CombinedScanTask> tasks = null;
+
+ public SparkFileBatchScan(Table table, List<FileScanTask> files, Broadcast<FileIO> io,
+ Broadcast<EncryptionManager> encryptionManager, boolean caseSensitive,
+ CaseInsensitiveStringMap options) {
+ super(table, table.schema(), io, encryptionManager, caseSensitive, options);
+ this.files = files;
+ }
+
+ @Override
+ public void filterFiles(Set<String> locations) {
Review comment:
@rdblue, you mean `file.file().path().toString()` conversion?
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519069223
##########
File path: spark3/src/main/java/org/apache/spark/sql/connector/write/RowLevelOperationsBuilder.java
##########
@@ -0,0 +1,30 @@
+/*
+ * 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.spark.sql.connector.write;
+
+import org.apache.spark.sql.connector.read.Scan;
+
+// TODO: how to represent merge-on-read?
+// Option 1: Introduce DeltaBatchWrite that should be returned for merge-on-read only, use BatchWrite for copy-on-write
+// Option 2: Introduce DeltaBatchWrite with keepUnchanged flag and use that flag to do copy-on-write/merge-on-read
+public interface RowLevelOperationsBuilder {
Review comment:
What about adding `asScanBuilder` and `asWriteBuilder`? Then we could configure the scan and the write using the correct interface independently. The underlying implementation can instantiate both separately, too, and can do whatever it chooses to coordinate between the configuration across both builders.
We would want to guarantee that the scan is configured and built first. The only information the scan needs is that it is part of a replace operation, so it should not push down filters within files (delete groups). For Iceberg, that's building a scan with `ignoreResiduals`. The write/replace operation needs information from a configured scan, so it should be built second.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519061913
##########
File path: spark3/src/main/java/org/apache/iceberg/spark/source/SparkRowLevelOperationsBuilder.java
##########
@@ -0,0 +1,191 @@
+/*
+ * 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 java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.iceberg.DataFile;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.TableScan;
+import org.apache.iceberg.encryption.EncryptionManager;
+import org.apache.iceberg.exceptions.RuntimeIOException;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.expressions.Binder;
+import org.apache.iceberg.expressions.Expression;
+import org.apache.iceberg.expressions.Expressions;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.spark.SparkFilters;
+import org.apache.iceberg.spark.SparkSchemaUtil;
+import org.apache.iceberg.spark.SparkUtil;
+import org.apache.spark.api.java.JavaSparkContext;
+import org.apache.spark.broadcast.Broadcast;
+import org.apache.spark.sql.SparkSession;
+import org.apache.spark.sql.connector.read.Scan;
+import org.apache.spark.sql.connector.write.BatchWrite;
+import org.apache.spark.sql.connector.write.LogicalWriteInfo;
+import org.apache.spark.sql.connector.write.SupportsPushDownRowLevelFilters;
+import org.apache.spark.sql.sources.Filter;
+import org.apache.spark.sql.types.StructType;
+import org.apache.spark.sql.util.CaseInsensitiveStringMap;
+
+import static org.apache.iceberg.TableProperties.ROW_LEVEL_OPS_ISOLATION_LEVEL;
+import static org.apache.iceberg.TableProperties.ROW_LEVEL_OPS_ISOLATION_LEVEL_DEFAULT;
+
+class SparkRowLevelOperationsBuilder implements SupportsPushDownRowLevelFilters {
Review comment:
I like being able to see everything in a single PR, but I would rather not commit both the Spark extensions and the update to Iceberg at the same time. We'll have to figure out how to break this up into better-sized chunks of work to review.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r521708364
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/execution/datasources/v2/ReplaceDataExec.scala
##########
@@ -0,0 +1,47 @@
+/*
+ * 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.spark.sql.execution.datasources.v2
+
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.connector.write.BatchWrite
+import org.apache.spark.sql.execution.SparkPlan
+
+case class ReplaceDataExec(batchWrite: BatchWrite, queryExec: SparkPlan) extends V2TableWriteExec {
+
+ // override child so that we can properly plan queryExec without executing the file filter
+ override def child: SparkPlan = queryExec
+
+ // TODO: doPrepare is NOT invoked by V2TableWriteExec
Review comment:
Simplified.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519070458
##########
File path: spark3/src/main/java/org/apache/iceberg/spark/source/SparkFileBatchScan.java
##########
@@ -0,0 +1,161 @@
+/*
+ * 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 java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.iceberg.CombinedScanTask;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.encryption.EncryptionManager;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.util.PropertyUtil;
+import org.apache.iceberg.util.TableScanUtil;
+import org.apache.spark.broadcast.Broadcast;
+import org.apache.spark.sql.connector.read.Statistics;
+import org.apache.spark.sql.connector.read.SupportsReadFileFilter;
+import org.apache.spark.sql.util.CaseInsensitiveStringMap;
+
+import static org.apache.iceberg.TableProperties.SPLIT_LOOKBACK;
+import static org.apache.iceberg.TableProperties.SPLIT_LOOKBACK_DEFAULT;
+import static org.apache.iceberg.TableProperties.SPLIT_OPEN_FILE_COST;
+import static org.apache.iceberg.TableProperties.SPLIT_OPEN_FILE_COST_DEFAULT;
+import static org.apache.iceberg.TableProperties.SPLIT_SIZE;
+import static org.apache.iceberg.TableProperties.SPLIT_SIZE_DEFAULT;
+
+public class SparkFileBatchScan extends BaseBatchScan implements SupportsReadFileFilter {
+ private List<FileScanTask> files;
+
+ // lazy cache of tasks
+ private List<CombinedScanTask> tasks = null;
+
+ public SparkFileBatchScan(Table table, List<FileScanTask> files, Broadcast<FileIO> io,
+ Broadcast<EncryptionManager> encryptionManager, boolean caseSensitive,
+ CaseInsensitiveStringMap options) {
+ super(table, table.schema(), io, encryptionManager, caseSensitive, options);
+ this.files = files;
+ }
+
+ @Override
+ public void filterFiles(Set<String> locations) {
Review comment:
I think we should build a `CharSequenceSet` out of the locations so that we don't need to convert all of the file paths to strings. That could potentially create a lot of strings and take a long time.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519978729
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/DynamicFileFilter.scala
##########
@@ -0,0 +1,33 @@
+/*
+ * 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.spark.sql.catalyst.plans.logical
+
+import org.apache.spark.sql.catalyst.expressions.Attribute
+import org.apache.spark.sql.execution.datasources.v2.DataSourceV2ScanRelation
+
+// TODO: fix stats (ignore the fact it is a binary node and report only scanRelation stats)
+case class DynamicFileFilter(
Review comment:
We could separate the file filter query into its own execution that is run in `DynamicFileFilter.doPrepare`. That way it isn't part of this plan.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519054819
##########
File path: spark3/src/main/java/org/apache/spark/sql/connector/write/SupportsPushDownRowLevelFilters.java
##########
@@ -0,0 +1,27 @@
+/*
+ * 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.spark.sql.connector.write;
+
+import org.apache.spark.sql.sources.Filter;
+
+public interface SupportsPushDownRowLevelFilters extends RowLevelOperationsBuilder {
Review comment:
Ideally, we should get rid of this if there is a good idea how to reuse similar interfaces for scans.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519053979
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/DynamicFileFilter.scala
##########
@@ -0,0 +1,33 @@
+/*
+ * 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.spark.sql.catalyst.plans.logical
+
+import org.apache.spark.sql.catalyst.expressions.Attribute
+import org.apache.spark.sql.execution.datasources.v2.DataSourceV2ScanRelation
+
+// TODO: fix stats (ignore the fact it is a binary node and report only scanRelation stats)
+case class DynamicFileFilter(
Review comment:
@rdblue, stats is something we will have to think about here.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r521693467
##########
File path: core/src/main/java/org/apache/iceberg/TableProperties.java
##########
@@ -134,4 +134,10 @@ private TableProperties() {
public static final String ENGINE_HIVE_ENABLED = "engine.hive.enabled";
public static final boolean ENGINE_HIVE_ENABLED_DEFAULT = false;
+
+ public static final String WRITE_ISOLATION_LEVEL = "write.isolation-level";
Review comment:
I think having only `write.merge.isolation` may not be descriptive enough since users won't know that we treat delete as a simplified version of merge.
I would keep `write.row-level.isolation` with `write.row-level.mode` or introduce a pair of configs for each operation type like `write.delete.isolation` and `write.delete.mode`. I am ok with both options.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#issuecomment-738908631
I am closing this one as most parts were merged to master.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r521686257
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteDelete.scala
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import java.util.UUID
+import org.apache.spark.sql.{sources, AnalysisException}
+import org.apache.spark.sql.catalyst.analysis.Resolver
+import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeReference, EqualNullSafe, Expression, InputFileName, Literal, Not, PredicateHelper, SubqueryExpression}
+import org.apache.spark.sql.catalyst.expressions.Literal.FalseLiteral
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, DeleteFromTable, DynamicFileFilter, Filter, LocalRelation, LogicalPlan, OverwriteFiles, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{SupportsMetadataOnlyDeletes, Table}
+import org.apache.spark.sql.connector.write.{LogicalWriteInfo, LogicalWriteInfoImpl, RowLevelOperationsBuilder, SupportsPushDownRowLevelFilters}
+import org.apache.spark.sql.execution.datasources.DataSourceStrategy
+import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, DataSourceV2ScanRelation}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.{BooleanType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+// TODO: should be part of early scan push down after the delete condition is optimized
+object RewriteDelete extends Rule[LogicalPlan] with PredicateHelper {
+
+ import org.apache.spark.sql.execution.datasources.v2.ExtendedDataSourceV2Implicits._
+
+ private val FILE_NAME_COL = "_file"
+
+ override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+ // no need to execute this delete as the condition is false
+ case DeleteFromTable(_, Some(FalseLiteral)) =>
+ LocalRelation()
Review comment:
Do we need to change anything? Currently, it should try to generate a commit, but not remove anything and probably not even write new metadata.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519989586
##########
File path: spark3/src/main/java/org/apache/spark/sql/connector/write/RowLevelOperationsBuilder.java
##########
@@ -0,0 +1,30 @@
+/*
+ * 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.spark.sql.connector.write;
+
+import org.apache.spark.sql.connector.read.Scan;
+
+// TODO: how to represent merge-on-read?
+// Option 1: Introduce DeltaBatchWrite that should be returned for merge-on-read only, use BatchWrite for copy-on-write
+// Option 2: Introduce DeltaBatchWrite with keepUnchanged flag and use that flag to do copy-on-write/merge-on-read
+public interface RowLevelOperationsBuilder {
Review comment:
I like option 1. The doc proposed option 2, but the main benefit there was being able to get ID information for rows and pass the rows back with the operation for that row. For example, a deleted row would be passed back using `delete(idValues)`.
Now that we have a better idea how this is going to work, I don't think it is a good idea to mix the row-level delta operation with the file-replace operation. There's no need to add row ID information to the replace operation and no need to add additional write interfaces for it.
Also, the plans for each case are different. Deleted rows can be discarded immediately for replace, and kept rows can be discarded immediately for row-level changes. Trying to unify those two plans just to be able to call `delete` or `keep` when those aren't needed doesn't make much sense any more.
I'll update the proposal doc to option 1. Do you agree?
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r521641516
##########
File path: core/src/main/java/org/apache/iceberg/TableProperties.java
##########
@@ -134,4 +134,10 @@ private TableProperties() {
public static final String ENGINE_HIVE_ENABLED = "engine.hive.enabled";
public static final boolean ENGINE_HIVE_ENABLED_DEFAULT = false;
+
+ public static final String WRITE_ISOLATION_LEVEL = "write.isolation-level";
Review comment:
Dynamic partition overwrite does mess up the situation. I like the `row-level` namespace we introduced for properties. We could make it `write.row-level.isolation` to match `write.row-level.mode`. That way, it is clear it applies only to update/delete/merge.
If we decide to have engine specific defaults later, we can introduce `write.row-level.spark.isolation` or `engine.spark.write.row-level.isolation`. The latter can serve as a basis for providing any engine specific properties..
##########
File path: core/src/main/java/org/apache/iceberg/TableProperties.java
##########
@@ -134,4 +134,10 @@ private TableProperties() {
public static final String ENGINE_HIVE_ENABLED = "engine.hive.enabled";
public static final boolean ENGINE_HIVE_ENABLED_DEFAULT = false;
+
+ public static final String WRITE_ISOLATION_LEVEL = "write.isolation-level";
Review comment:
Dynamic partition overwrite does mess up the situation. I like the `row-level` namespace we introduced for properties. We could make it `write.row-level.isolation` to match `write.row-level.mode`. That way, it is clear it applies only to update/delete/merge.
If we decide to have engine specific defaults later, we can introduce `write.row-level.spark.isolation` or `engine.spark.write.row-level.isolation`. The latter can serve as a basis for providing any engine specific properties.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r521686795
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/DynamicFileFilter.scala
##########
@@ -0,0 +1,33 @@
+/*
+ * 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.spark.sql.catalyst.plans.logical
+
+import org.apache.spark.sql.catalyst.expressions.Attribute
+import org.apache.spark.sql.execution.datasources.v2.DataSourceV2ScanRelation
+
+// TODO: fix stats (ignore the fact it is a binary node and report only scanRelation stats)
+case class DynamicFileFilter(
Review comment:
We could separate the plan out of this one, so that it doesn't show up in this plan, but gets run in `doPrepare`.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r521642866
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/OptimizeConditionsInRowLevelOperations.scala
##########
@@ -0,0 +1,45 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.expressions.{Expression, Literal, SubqueryExpression}
+import org.apache.spark.sql.catalyst.plans.logical.{DeleteFromTable, Filter, LocalRelation, LogicalPlan}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.execution.datasources.v2.DataSourceV2ScanRelation
+
+// a temp solution until we move the rewrite of row level ops after operator optimization rules
+object OptimizeConditionsInRowLevelOperations extends Rule[LogicalPlan] {
+ override def apply(plan: LogicalPlan): LogicalPlan = plan transform {
+ // TODO: fix this?
Review comment:
This must be fixed before committing for sure.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519061728
##########
File path: spark3/src/main/java/org/apache/iceberg/spark/source/SparkStreamingWrite.java
##########
@@ -40,7 +40,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class SparkStreamingWrite extends SparkBatchWrite implements StreamingWrite {
+class SparkStreamingWrite extends BaseBatchWrite implements StreamingWrite {
Review comment:
In my changes for `RequiresDistributionAndOrdering`, this class went away and is replaced by an inner class. I think that pattern worked well. Maybe we could do that before this one to reduce the number of changes here.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519549159
##########
File path: spark3/src/main/java/org/apache/iceberg/spark/source/BaseBatchScan.java
##########
@@ -0,0 +1,263 @@
+/*
+ * 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 java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import org.apache.iceberg.CombinedScanTask;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.SchemaParser;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.TableProperties;
+import org.apache.iceberg.encryption.EncryptionManager;
+import org.apache.iceberg.hadoop.HadoopInputFile;
+import org.apache.iceberg.hadoop.Util;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.mapping.NameMapping;
+import org.apache.iceberg.mapping.NameMappingParser;
+import org.apache.iceberg.spark.Spark3Util;
+import org.apache.iceberg.spark.SparkSchemaUtil;
+import org.apache.iceberg.util.TableScanUtil;
+import org.apache.spark.broadcast.Broadcast;
+import org.apache.spark.sql.catalyst.InternalRow;
+import org.apache.spark.sql.connector.read.Batch;
+import org.apache.spark.sql.connector.read.InputPartition;
+import org.apache.spark.sql.connector.read.PartitionReader;
+import org.apache.spark.sql.connector.read.PartitionReaderFactory;
+import org.apache.spark.sql.connector.read.Scan;
+import org.apache.spark.sql.connector.read.SupportsReportStatistics;
+import org.apache.spark.sql.types.StructType;
+import org.apache.spark.sql.util.CaseInsensitiveStringMap;
+import org.apache.spark.sql.vectorized.ColumnarBatch;
+
+abstract class BaseBatchScan implements Scan, Batch, SupportsReportStatistics {
+ protected final Table table;
Review comment:
Do you have an example at hand?
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519548812
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/DynamicFileFilter.scala
##########
@@ -0,0 +1,33 @@
+/*
+ * 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.spark.sql.catalyst.plans.logical
+
+import org.apache.spark.sql.catalyst.expressions.Attribute
+import org.apache.spark.sql.execution.datasources.v2.DataSourceV2ScanRelation
+
+// TODO: fix stats (ignore the fact it is a binary node and report only scanRelation stats)
+case class DynamicFileFilter(
Review comment:
I don't think so, unfortunately. Nodes themselves don't control this. For example, `LogicalPlanVisitor` and its implementation, `SizeInBytesOnlyStatsPlanVisitor`, has the following logic:
```
override def default(p: LogicalPlan): Statistics = p match {
case p: LeafNode => p.computeStats()
case _: LogicalPlan =>
Statistics(sizeInBytes = p.children.map(_.stats.sizeInBytes).filter(_ > 0L).product)
}
```
Here, we have a binary node so the output will be a product. We could wrap the filter plan into something that reports 1 as its size in bytes. That way, the product will produce the size of the scan relation. Any better ideas?
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519054366
##########
File path: spark3/src/main/java/org/apache/spark/sql/connector/catalog/SupportsMetadataOnlyDeletes.java
##########
@@ -0,0 +1,27 @@
+/*
+ * 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.spark.sql.connector.catalog;
+
+import org.apache.spark.sql.sources.Filter;
+
+// TODO: any better ideas? make it generic as SupportsDeleteUsingFilters and merge it into SupportsDelete upstream?
Review comment:
@rdblue, I feel like `SupportsDelete` is very rudimentary and did not assume we can support deletes by rewriting plans. We will have to think how to approach that.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519064302
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/execution/datasources/v2/ExtendedDataSourceV2Implicits.scala
##########
@@ -0,0 +1,37 @@
+/*
+ * 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.spark.sql.execution.datasources.v2
+
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.connector.catalog.{SupportsRowLevelOperations, Table}
+
+// must be merged with DataSourceV2Implicits in Spark
+object ExtendedDataSourceV2Implicits {
Review comment:
What about `IcebergExtensionImplicits` to keep our implicits in a single place?
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r520950347
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteDelete.scala
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import java.util.UUID
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.{sources, AnalysisException}
+import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeReference, EqualNullSafe, Expression, InputFileName, Literal, Not, PredicateHelper, SubqueryExpression}
+import org.apache.spark.sql.catalyst.expressions.Literal.FalseLiteral
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, DeleteFromTable, DynamicFileFilter, Filter, LocalRelation, LogicalPlan, Project, ReplaceData}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{ExtendedSupportsDelete, Table}
+import org.apache.spark.sql.connector.read.{Scan, ScanBuilder, SupportsFileFilter}
+import org.apache.spark.sql.connector.write.{LogicalWriteInfo, LogicalWriteInfoImpl, MergeBuilder, RequiresScan}
+import org.apache.spark.sql.execution.datasources.DataSourceStrategy
+import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, DataSourceV2ScanRelation, PushDownUtils}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.{BooleanType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+// TODO: should be part of early scan push down after the delete condition is optimized
+object RewriteDelete extends Rule[LogicalPlan] with PredicateHelper with Logging {
+
+ import org.apache.spark.sql.execution.datasources.v2.ExtendedDataSourceV2Implicits._
+
+ private val FILE_NAME_COL = "_file"
+
+ override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+ // no need to execute this delete as the condition is false
+ case DeleteFromTable(_, Some(FalseLiteral)) =>
+ LocalRelation()
+
+ // don't rewrite deletes that can be answered by passing filters to deleteWhere in SupportsDelete
+ case d @ DeleteFromTable(r: DataSourceV2Relation, Some(cond)) if isDeleteWhereCase(r, cond) =>
+ d
+
+ // rewrite all operations that require reading the table to delete records
+ case DeleteFromTable(r: DataSourceV2Relation, Some(cond)) =>
+ // TODO: do a switch based on whether we get BatchWrite or DeltaBatchWrite
+ val writeInfo = newWriteInfo(r.schema)
+ val mergeBuilder = r.table.asMergeable.newMergeBuilder(writeInfo)
+
+ val (scan, scanPlan) = buildScanPlan(r.table, r.output, mergeBuilder, cond)
+
+ val remainingRowFilter = Not(EqualNullSafe(cond, Literal(true, BooleanType)))
+ val remainingRowsPlan = Filter(remainingRowFilter, scanPlan)
+
+ val batchWrite = mergeBuilder.asWriteBuilder.buildForBatch()
+ batchWrite match {
+ case w: RequiresScan => w.withScan(scan)
+ case _ => // do nothing
+ }
+
+ // TODO: group by something so that we can write back
+ // Option 1: repartition/sort by partition values
+ // Option 2: repartition by _file and local sort by partition values (how to deal with 1 GB files?)
+ ReplaceData(r, batchWrite, remainingRowsPlan)
+ }
+
+ private def buildScanPlan(
+ table: Table,
+ output: Seq[AttributeReference],
+ mergeBuilder: MergeBuilder,
+ cond: Expression): (Scan, LogicalPlan) = {
+
+ val scanBuilder = mergeBuilder.asScanBuilder
+
+ val predicates = splitConjunctivePredicates(cond)
+ val normalizedPredicates = DataSourceStrategy.normalizeExprs(predicates, output)
+ PushDownUtils.pushFilters(scanBuilder, normalizedPredicates)
+
+ val scan = scanBuilder.build()
+ val scanRelation = DataSourceV2ScanRelation(table, scan, output)
+
+ scan match {
+ case _: SupportsFileFilter =>
+ val matchingFilePlan = buildFileFilterPlan(cond, scanRelation)
+ val dynamicFileFilter = DynamicFileFilter(scanRelation, matchingFilePlan)
+ (scan, dynamicFileFilter)
+ case _ =>
+ (scan, scanRelation)
+ }
+ }
+
+ private def isDeleteWhereCase(relation: DataSourceV2Relation, cond: Expression): Boolean = {
+ relation.table match {
+ case t: ExtendedSupportsDelete if !SubqueryExpression.hasSubquery(cond) =>
+ val predicates = splitConjunctivePredicates(cond)
+ val normalizedPredicates = DataSourceStrategy.normalizeExprs(predicates, relation.output)
+ val dataSourceFilters = toDataSourceFilters(normalizedPredicates)
+ val allPredicatesTranslated = normalizedPredicates.size == dataSourceFilters.length
+ allPredicatesTranslated && t.canDeleteWhere(dataSourceFilters)
Review comment:
We should open a PR against Spark for this. It may be easy to get it into `SupportsDelete` for 3.1.0.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519058823
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/OptimizeConditionsInRowLevelOperations.scala
##########
@@ -0,0 +1,45 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.expressions.{Expression, Literal, SubqueryExpression}
+import org.apache.spark.sql.catalyst.plans.logical.{DeleteFromTable, Filter, LocalRelation, LogicalPlan}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.execution.datasources.v2.DataSourceV2ScanRelation
+
+// a temp solution until we move the rewrite of row level ops after operator optimization rules
+object OptimizeConditionsInRowLevelOperations extends Rule[LogicalPlan] {
+ override def apply(plan: LogicalPlan): LogicalPlan = plan transform {
+ // TODO: fix this?
Review comment:
What does this TODO refer to?
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r520004493
##########
File path: spark3/src/main/java/org/apache/spark/sql/connector/catalog/SupportsRowLevelOperations.java
##########
@@ -0,0 +1,27 @@
+/*
+ * 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.spark.sql.connector.catalog;
+
+import org.apache.spark.sql.connector.write.LogicalWriteInfo;
+import org.apache.spark.sql.connector.write.RowLevelOperationsBuilder;
+
+public interface SupportsRowLevelOperations extends Table {
Review comment:
I like replace because it is in the context of Spark operations, not Iceberg. For Spark, this is planning an operation to replace the what was read and I think it describes that case well. And in Spark, overwrite is used to configure a normal Write operation in the builder, using a completely different builder seems like it should use a different name.
I agree that it isn't what we use "replace" for in Iceberg. This would be an overwrite in Iceberg.
> We still want to propagate the expression that was used to scan for checking concurrently appended files under serializable isolation.
Okay, interesting point. So you're saying that we would use this to plan all row-level plans and this would be the only way to get a `DeltaWriter`? My main question is whether we think that is going to make it harder to build a `DeltaWriter` that doesn't support copy-on-write.
Take a JDBC source for example. It doesn't have any reason to use copy-on-write because all of the operations are row-level. So would we be over-complicating that source just to avoid passing an extra filter to `DeltaWriter` that is created using a normal `WriteBuilder`?
I think it would be. The simpler case for a JDBC implementation is to add `SupportsDelta` to `WriteBuilder` and not require a separate builder.
But, we would also need an interface for passing the filter to the write for both `WriteBuilder` and `ReplaceBuilder`, and I agree that it is a bit cleaner to rely on this information coming from the scan configuration itself.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r520947880
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/OptimizeConditionsInRowLevelOperations.scala
##########
@@ -0,0 +1,45 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.expressions.{Expression, Literal, SubqueryExpression}
+import org.apache.spark.sql.catalyst.plans.logical.{DeleteFromTable, Filter, LocalRelation, LogicalPlan}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.execution.datasources.v2.DataSourceV2ScanRelation
+
+// a temp solution until we move the rewrite of row level ops after operator optimization rules
+object OptimizeConditionsInRowLevelOperations extends Rule[LogicalPlan] {
+ override def apply(plan: LogicalPlan): LogicalPlan = plan transform {
+ // TODO: fix this?
Review comment:
It's not obvious what is outstanding, so I think we should either do the additional work and remove it, or expand it to explain what needs to be done before committing this.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519552987
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/OptimizeConditionsInRowLevelOperations.scala
##########
@@ -0,0 +1,45 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.expressions.{Expression, Literal, SubqueryExpression}
+import org.apache.spark.sql.catalyst.plans.logical.{DeleteFromTable, Filter, LocalRelation, LogicalPlan}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.execution.datasources.v2.DataSourceV2ScanRelation
+
+// a temp solution until we move the rewrite of row level ops after operator optimization rules
Review comment:
Yeah, absolutely.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519551328
##########
File path: core/src/main/java/org/apache/iceberg/TableProperties.java
##########
@@ -134,4 +134,10 @@ private TableProperties() {
public static final String ENGINE_HIVE_ENABLED = "engine.hive.enabled";
public static final boolean ENGINE_HIVE_ENABLED_DEFAULT = false;
+
+ public static final String ROW_LEVEL_OPS_ISOLATION_LEVEL = "row-level-ops.isolation-level";
+ public static final String ROW_LEVEL_OPS_ISOLATION_LEVEL_DEFAULT = "serializable";
Review comment:
Do we want to make this property specific to Spark? Seems generic enough and I hope it will be respected by other query engines in the future too.
If we want to keep it Spark specific, it is going to be the first Spark table property. The only other query engine specific property is `engine.hive.enabled`. Do we want to share the `engine` prefix in this case?
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r521681450
##########
File path: core/src/main/java/org/apache/iceberg/TableProperties.java
##########
@@ -134,4 +134,10 @@ private TableProperties() {
public static final String ENGINE_HIVE_ENABLED = "engine.hive.enabled";
public static final boolean ENGINE_HIVE_ENABLED_DEFAULT = false;
+
+ public static final String WRITE_ISOLATION_LEVEL = "write.isolation-level";
Review comment:
Since we are using "merge" in the code, what do you think about `write.merge.isolation` instead of `row-level`?
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519993678
##########
File path: core/src/main/java/org/apache/iceberg/TableProperties.java
##########
@@ -134,4 +134,10 @@ private TableProperties() {
public static final String ENGINE_HIVE_ENABLED = "engine.hive.enabled";
public static final boolean ENGINE_HIVE_ENABLED_DEFAULT = false;
+
+ public static final String ROW_LEVEL_OPS_ISOLATION_LEVEL = "row-level-ops.isolation-level";
+ public static final String ROW_LEVEL_OPS_ISOLATION_LEVEL_DEFAULT = "serializable";
+
+ public static final String ROW_LEVEL_OPS_MODE = "row-level-ops.mode";
+ public static final String ROW_LEVEL_OPS_MODE_DEFAULT = "copy-on-write";
Review comment:
I like to use `write` because it will control behavior at write time. If we want this to be a general setting to prefer copy-on-write, then we could use `write.row-level.mode`. I think that's specific. What do you think?
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r521644086
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/OptimizeConditionsInRowLevelOperations.scala
##########
@@ -0,0 +1,45 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.expressions.{Expression, Literal, SubqueryExpression}
+import org.apache.spark.sql.catalyst.plans.logical.{DeleteFromTable, Filter, LocalRelation, LogicalPlan}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.execution.datasources.v2.DataSourceV2ScanRelation
+
+// a temp solution until we move the rewrite of row level ops after operator optimization rules
+object OptimizeConditionsInRowLevelOperations extends Rule[LogicalPlan] {
+ override def apply(plan: LogicalPlan): LogicalPlan = plan transform {
+ // TODO: fix this?
Review comment:
Actually, we may try to extend `SparkSessionExtensions` with a way to inject custom rules at the early scan push down phase. If we can get that in Spark 3.1 and we are ok to wait, then this rule won't be needed.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r521573168
##########
File path: core/src/main/java/org/apache/iceberg/TableProperties.java
##########
@@ -134,4 +134,10 @@ private TableProperties() {
public static final String ENGINE_HIVE_ENABLED = "engine.hive.enabled";
public static final boolean ENGINE_HIVE_ENABLED_DEFAULT = false;
+
+ public static final String WRITE_ISOLATION_LEVEL = "write.isolation-level";
Review comment:
My only reservation about this is that the isolation level varies by operation. Dynamic partition replacement doesn't check whether the partitions that will be replaced have been modified since the query started. Should we go back and update those operations?
I think people also expect those operations to have the semantics that they currently do because that matches Hive. It would be strange for them to either implement a stricter isolation level, or to prevent using a stricter isolation level in merge operations.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519059423
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/DynamicFileFilter.scala
##########
@@ -0,0 +1,33 @@
+/*
+ * 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.spark.sql.catalyst.plans.logical
+
+import org.apache.spark.sql.catalyst.expressions.Attribute
+import org.apache.spark.sql.execution.datasources.v2.DataSourceV2ScanRelation
+
+// TODO: fix stats (ignore the fact it is a binary node and report only scanRelation stats)
+case class DynamicFileFilter(
Review comment:
Agreed. Can we just delegate to the relation?
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519552914
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/DeleteCheck.scala
##########
@@ -0,0 +1,49 @@
+/*
+ * 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.spark.sql.catalyst.analysis
+
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.catalyst.expressions.{Expression, InSubquery, Not}
+import org.apache.spark.sql.catalyst.plans.logical.{DeleteFromTable, LogicalPlan}
+
+object DeleteCheck extends (LogicalPlan => Unit) {
Review comment:
I was actually surprised myself when I saw existing rules in `BaseSessionStateBuilder`:
```
override val extendedCheckRules: Seq[LogicalPlan => Unit] =
PreWriteCheck +:
PreReadCheck +:
HiveOnlyCheck +:
TableCapabilityCheck +:
CommandCheck(conf) +:
customCheckRules
```
What about `DeleteFromTablePredicateCheck`?
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r520951135
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/ReplaceData.scala
##########
@@ -0,0 +1,28 @@
+/*
+ * 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.spark.sql.catalyst.plans.logical
+
+import org.apache.spark.sql.catalyst.analysis.NamedRelation
+import org.apache.spark.sql.connector.write.BatchWrite
+
+case class ReplaceData(
Review comment:
Will need some docs for this, since it holds an operation that replaces what was read.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519073827
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteDelete.scala
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import java.util.UUID
+import org.apache.spark.sql.{sources, AnalysisException}
+import org.apache.spark.sql.catalyst.analysis.Resolver
+import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeReference, EqualNullSafe, Expression, InputFileName, Literal, Not, PredicateHelper, SubqueryExpression}
+import org.apache.spark.sql.catalyst.expressions.Literal.FalseLiteral
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, DeleteFromTable, DynamicFileFilter, Filter, LocalRelation, LogicalPlan, OverwriteFiles, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{SupportsMetadataOnlyDeletes, Table}
+import org.apache.spark.sql.connector.write.{LogicalWriteInfo, LogicalWriteInfoImpl, RowLevelOperationsBuilder, SupportsPushDownRowLevelFilters}
+import org.apache.spark.sql.execution.datasources.DataSourceStrategy
+import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, DataSourceV2ScanRelation}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.{BooleanType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+// TODO: should be part of early scan push down after the delete condition is optimized
+object RewriteDelete extends Rule[LogicalPlan] with PredicateHelper {
+
+ import org.apache.spark.sql.execution.datasources.v2.ExtendedDataSourceV2Implicits._
+
+ private val FILE_NAME_COL = "_file"
+
+ override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+ // no need to execute this delete as the condition is false
+ case DeleteFromTable(_, Some(FalseLiteral)) =>
+ LocalRelation()
+
+ // don't rewrite deletes that can be answered using metadata only operations
+ case d @ DeleteFromTable(r: DataSourceV2Relation, Some(cond)) if isMetadataOnlyOperation(r, cond) =>
+ d
+
+ // rewrite all operations that require reading the table to delete records
+ case DeleteFromTable(r: DataSourceV2Relation, Some(cond)) =>
+ // TODO: do a switch based on whether we get BatchWrite or DeltaBatchWrite
+ val writeInfo = newWriteInfo(r.schema)
+ val builder = r.table.asRowLevelModifiable.newRowLevelOperationsBuilder(writeInfo)
+
+ pushDownFilters(builder, cond, r.output)
+
+ val scan = builder.buildScan()
+ val scanRelation = DataSourceV2ScanRelation(r.table, scan, r.output)
+ val filterPlan = buildFilterPlan(cond, scanRelation)
+
+ val batchWrite = builder.buildBatchWrite()
+ // TODO: group by something so that we can write back
+ // Option 1: repartition/sort by partition values
+ // Option 2: repartition by _file and local sort by partition values (how to deal with 1 GB files?)
Review comment:
If this is currently a one-stage plan (other than dynamic filtering) then I'd say let's not add a shuffle. That way, the source can create a 1-to-1 file replacement. Later, we can use `RequiresDistributionAndOrdering` to make this customizable.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519058752
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/OptimizeConditionsInRowLevelOperations.scala
##########
@@ -0,0 +1,45 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.expressions.{Expression, Literal, SubqueryExpression}
+import org.apache.spark.sql.catalyst.plans.logical.{DeleteFromTable, Filter, LocalRelation, LogicalPlan}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.execution.datasources.v2.DataSourceV2ScanRelation
+
+// a temp solution until we move the rewrite of row level ops after operator optimization rules
Review comment:
Could you add a longer comment explaining what this rule does? I think we should make sure it is easy to read and understand because it looks strange.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r520901652
##########
File path: core/src/main/java/org/apache/iceberg/TableProperties.java
##########
@@ -134,4 +134,10 @@ private TableProperties() {
public static final String ENGINE_HIVE_ENABLED = "engine.hive.enabled";
public static final boolean ENGINE_HIVE_ENABLED_DEFAULT = false;
+
+ public static final String WRITE_ISOLATION_LEVEL = "write.isolation-level";
Review comment:
@rdblue, naming is still TBD.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r520952346
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/execution/datasources/v2/ReplaceDataExec.scala
##########
@@ -0,0 +1,47 @@
+/*
+ * 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.spark.sql.execution.datasources.v2
+
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.connector.write.BatchWrite
+import org.apache.spark.sql.execution.SparkPlan
+
+case class ReplaceDataExec(batchWrite: BatchWrite, queryExec: SparkPlan) extends V2TableWriteExec {
+
+ // override child so that we can properly plan queryExec without executing the file filter
+ override def child: SparkPlan = queryExec
+
+ // TODO: doPrepare is NOT invoked by V2TableWriteExec
Review comment:
I thought that `doPrepare` was called on all child nodes. So you can't add the `doPrepare` here, but it should work for any child. Is that not the case?
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519072423
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/execution/datasources/v2/OverwriteFilesExec.scala
##########
@@ -0,0 +1,63 @@
+/*
+ * 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.spark.sql.execution.datasources.v2
+
+import collection.JavaConverters._
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.connector.read.SupportsReadFileFilter
+import org.apache.spark.sql.connector.write.{BatchWrite, SupportsWriteFileFilter}
+import org.apache.spark.sql.execution.SparkPlan
+
+case class OverwriteFilesExec(batchWrite: BatchWrite, queryExec: SparkPlan) extends V2TableWriteExec {
+
+ // override child so that we can properly plan queryExec without executing the file filter
+ override def child: SparkPlan = queryExec
+
+ // TODO: doPrepare is NOT invoked by V2TableWriteExec
+ // TODO: it should be ok if at least reader or writer supports dynamic file filter
+ override lazy val query: SparkPlan = {
+ batchWrite match {
+ case w: SupportsWriteFileFilter =>
+ val dynamicFileFilters = queryExec.collect { case s: DynamicFileFilterExec => s }
Review comment:
The way this works seems a little strange to me because it finds another exec node further down in the tree, and then uses that node's children. Instead, I would expect the execution here to happen on that node, where `doPrepare` will be called.
I think the only reason to do it this way is to have a reference to `SupportsWriteFileFilter` that is further up the plan tree. If we relied on the write and the scan to coordinate because they are part of the same replace operation, then we could make this a bit cleaner.
What about moving this to `DynamicFileFilter.doPrepare`, where it will check whether the scan is `SupportsReadFileFilter`, if so execute the query, and call `filterFiles`?
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519989586
##########
File path: spark3/src/main/java/org/apache/spark/sql/connector/write/RowLevelOperationsBuilder.java
##########
@@ -0,0 +1,30 @@
+/*
+ * 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.spark.sql.connector.write;
+
+import org.apache.spark.sql.connector.read.Scan;
+
+// TODO: how to represent merge-on-read?
+// Option 1: Introduce DeltaBatchWrite that should be returned for merge-on-read only, use BatchWrite for copy-on-write
+// Option 2: Introduce DeltaBatchWrite with keepUnchanged flag and use that flag to do copy-on-write/merge-on-read
+public interface RowLevelOperationsBuilder {
Review comment:
I like option 1. The doc proposed option 2, but the main benefit there was being able to get ID information for rows and pass the rows back with the operation for that row. For example, a deleted row would be passed back using `delete(idValues)`.
Now that we have a better idea how this is going to work, I don't think it is a good idea to mix the row-level delta operation with the replace operation. There's no need to add row ID information to the replace operation and no need to add additional write interfaces for it.
Also, the plans for each case are different. Deleted rows can be discarded immediately for replace, and kept rows can be discarded immediately for row-level changes. Trying to unify those two plans just to be able to call `delete` or `keep` when those aren't needed doesn't make much sense any more.
I'll update the proposal doc to option 1. Do you agree?
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519072847
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteDelete.scala
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import java.util.UUID
+import org.apache.spark.sql.{sources, AnalysisException}
+import org.apache.spark.sql.catalyst.analysis.Resolver
+import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeReference, EqualNullSafe, Expression, InputFileName, Literal, Not, PredicateHelper, SubqueryExpression}
+import org.apache.spark.sql.catalyst.expressions.Literal.FalseLiteral
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, DeleteFromTable, DynamicFileFilter, Filter, LocalRelation, LogicalPlan, OverwriteFiles, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{SupportsMetadataOnlyDeletes, Table}
+import org.apache.spark.sql.connector.write.{LogicalWriteInfo, LogicalWriteInfoImpl, RowLevelOperationsBuilder, SupportsPushDownRowLevelFilters}
+import org.apache.spark.sql.execution.datasources.DataSourceStrategy
+import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, DataSourceV2ScanRelation}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.{BooleanType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+// TODO: should be part of early scan push down after the delete condition is optimized
+object RewriteDelete extends Rule[LogicalPlan] with PredicateHelper {
+
+ import org.apache.spark.sql.execution.datasources.v2.ExtendedDataSourceV2Implicits._
+
+ private val FILE_NAME_COL = "_file"
+
+ override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+ // no need to execute this delete as the condition is false
+ case DeleteFromTable(_, Some(FalseLiteral)) =>
+ LocalRelation()
+
+ // don't rewrite deletes that can be answered using metadata only operations
+ case d @ DeleteFromTable(r: DataSourceV2Relation, Some(cond)) if isMetadataOnlyOperation(r, cond) =>
+ d
Review comment:
I think I noted elsewhere, but I'd prefer to use a subclass of `OverwriteFiles` that first attempts to run this as a metadata delete, and then runs the replace query if that throws `IllegalArgumentException` to signal that the delete cannot be done in metadata.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519061075
##########
File path: spark3/src/main/java/org/apache/spark/sql/connector/catalog/SupportsMetadataOnlyDeletes.java
##########
@@ -0,0 +1,27 @@
+/*
+ * 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.spark.sql.connector.catalog;
+
+import org.apache.spark.sql.sources.Filter;
+
+// TODO: any better ideas? make it generic as SupportsDeleteUsingFilters and merge it into SupportsDelete upstream?
Review comment:
My intent there was to first run `DeleteFrom` and if that failed, then attempt the non-metadata version.
We could try doing that by extending `OverwriteFiles` for the delete and calling `SupportsDelete.deleteWhere`. If that fails, then call `OverwriteFiles.doExecute`.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r520895579
##########
File path: spark3/src/main/java/org/apache/iceberg/spark/source/SparkBatchScan.java
##########
@@ -20,81 +20,61 @@
package org.apache.iceberg.spark.source;
import java.io.IOException;
-import java.io.Serializable;
-import java.util.Collection;
import java.util.List;
+import java.util.Map;
import java.util.Objects;
+import java.util.Set;
import java.util.stream.Collectors;
import org.apache.iceberg.CombinedScanTask;
-import org.apache.iceberg.FileFormat;
import org.apache.iceberg.FileScanTask;
import org.apache.iceberg.Schema;
-import org.apache.iceberg.SchemaParser;
import org.apache.iceberg.SnapshotSummary;
import org.apache.iceberg.Table;
-import org.apache.iceberg.TableProperties;
import org.apache.iceberg.TableScan;
import org.apache.iceberg.encryption.EncryptionManager;
import org.apache.iceberg.exceptions.RuntimeIOException;
import org.apache.iceberg.expressions.Expression;
-import org.apache.iceberg.hadoop.HadoopInputFile;
-import org.apache.iceberg.hadoop.Util;
import org.apache.iceberg.io.CloseableIterable;
import org.apache.iceberg.io.FileIO;
-import org.apache.iceberg.mapping.NameMapping;
-import org.apache.iceberg.mapping.NameMappingParser;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.spark.Spark3Util;
import org.apache.iceberg.spark.SparkSchemaUtil;
import org.apache.iceberg.util.PropertyUtil;
import org.apache.iceberg.util.TableScanUtil;
import org.apache.spark.broadcast.Broadcast;
-import org.apache.spark.sql.catalyst.InternalRow;
-import org.apache.spark.sql.connector.read.Batch;
-import org.apache.spark.sql.connector.read.InputPartition;
-import org.apache.spark.sql.connector.read.PartitionReader;
-import org.apache.spark.sql.connector.read.PartitionReaderFactory;
-import org.apache.spark.sql.connector.read.Scan;
import org.apache.spark.sql.connector.read.Statistics;
-import org.apache.spark.sql.connector.read.SupportsReportStatistics;
-import org.apache.spark.sql.types.StructType;
+import org.apache.spark.sql.connector.read.SupportsFileFilter;
import org.apache.spark.sql.util.CaseInsensitiveStringMap;
-import org.apache.spark.sql.vectorized.ColumnarBatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-class SparkBatchScan implements Scan, Batch, SupportsReportStatistics {
+import static org.apache.iceberg.TableProperties.SPLIT_LOOKBACK;
+import static org.apache.iceberg.TableProperties.SPLIT_LOOKBACK_DEFAULT;
+import static org.apache.iceberg.TableProperties.SPLIT_OPEN_FILE_COST;
+import static org.apache.iceberg.TableProperties.SPLIT_OPEN_FILE_COST_DEFAULT;
+import static org.apache.iceberg.TableProperties.SPLIT_SIZE;
+import static org.apache.iceberg.TableProperties.SPLIT_SIZE_DEFAULT;
+
+class SparkBatchScan extends BaseBatchScan implements SupportsFileFilter {
Review comment:
I think we no longer need `BaseBatchScan` as I got rid of the other scan.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r521505089
##########
File path: core/src/main/java/org/apache/iceberg/TableProperties.java
##########
@@ -134,4 +134,10 @@ private TableProperties() {
public static final String ENGINE_HIVE_ENABLED = "engine.hive.enabled";
public static final boolean ENGINE_HIVE_ENABLED_DEFAULT = false;
+
+ public static final String WRITE_ISOLATION_LEVEL = "write.isolation-level";
Review comment:
My current thinking on naming here is that all query engines use the same API to commit. Right now, we only have appends that guarantee serializable isolation. I think query engines supporting update/delete/merge should respect this property or throw an exception if they cannot satisfy the required isolation level.
We may add a property specific to query engines if we think we will need different defaults. I'd say those will be optional. If a query engine specific value is not set, use this for all engines.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r521654755
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/ReplaceData.scala
##########
@@ -0,0 +1,28 @@
+/*
+ * 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.spark.sql.catalyst.plans.logical
+
+import org.apache.spark.sql.catalyst.analysis.NamedRelation
+import org.apache.spark.sql.connector.write.BatchWrite
+
+case class ReplaceData(
Review comment:
Will add.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519553444
##########
File path: spark3/src/main/java/org/apache/iceberg/spark/source/SparkStreamingWrite.java
##########
@@ -40,7 +40,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class SparkStreamingWrite extends SparkBatchWrite implements StreamingWrite {
+class SparkStreamingWrite extends BaseBatchWrite implements StreamingWrite {
Review comment:
Let me see that change.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519058611
##########
File path: core/src/main/java/org/apache/iceberg/TableProperties.java
##########
@@ -134,4 +134,10 @@ private TableProperties() {
public static final String ENGINE_HIVE_ENABLED = "engine.hive.enabled";
public static final boolean ENGINE_HIVE_ENABLED_DEFAULT = false;
+
+ public static final String ROW_LEVEL_OPS_ISOLATION_LEVEL = "row-level-ops.isolation-level";
+ public static final String ROW_LEVEL_OPS_ISOLATION_LEVEL_DEFAULT = "serializable";
+
+ public static final String ROW_LEVEL_OPS_MODE = "row-level-ops.mode";
+ public static final String ROW_LEVEL_OPS_MODE_DEFAULT = "copy-on-write";
Review comment:
Similar, how about `write.spark.mode`?
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/DeleteCheck.scala
##########
@@ -0,0 +1,49 @@
+/*
+ * 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.spark.sql.catalyst.analysis
+
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.catalyst.expressions.{Expression, InSubquery, Not}
+import org.apache.spark.sql.catalyst.plans.logical.{DeleteFromTable, LogicalPlan}
+
+object DeleteCheck extends (LogicalPlan => Unit) {
Review comment:
I think analysis check classes are usually named with "check" first, like `CheckDeleteFromPredicate`.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r521668335
##########
File path: spark3/src/main/java/org/apache/iceberg/spark/source/SparkMergeBuilder.java
##########
@@ -0,0 +1,100 @@
+/*
+ * 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 java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import org.apache.iceberg.IsolationLevel;
+import org.apache.iceberg.Snapshot;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.TableProperties;
+import org.apache.spark.sql.SparkSession;
+import org.apache.spark.sql.connector.read.ScanBuilder;
+import org.apache.spark.sql.connector.write.LogicalWriteInfo;
+import org.apache.spark.sql.connector.write.MergeBuilder;
+import org.apache.spark.sql.connector.write.WriteBuilder;
+import org.apache.spark.sql.util.CaseInsensitiveStringMap;
+
+class SparkMergeBuilder implements MergeBuilder {
+
+ private final SparkSession spark;
+ private final Table table;
+ private final LogicalWriteInfo writeInfo;
+ private final IsolationLevel isolationLevel;
+
+ // lazy vars
+ private ScanBuilder lazyScanBuilder;
+ private WriteBuilder lazyWriteBuilder;
+
+ SparkMergeBuilder(SparkSession spark, Table table, LogicalWriteInfo writeInfo) {
+ this.spark = spark;
+ this.table = table;
+ this.writeInfo = writeInfo;
+
+ String isolationLevelAsString = table.properties().getOrDefault(
+ TableProperties.WRITE_ISOLATION_LEVEL,
+ TableProperties.WRITE_ISOLATION_LEVEL_DEFAULT
+ ).toUpperCase(Locale.ROOT);
+ this.isolationLevel = IsolationLevel.valueOf(isolationLevelAsString);
+ }
+
+ @Override
+ public ScanBuilder asScanBuilder() {
+ return scanBuilder();
+ }
+
+ private ScanBuilder scanBuilder() {
+ if (lazyScanBuilder == null) {
+ // ignore residuals to ensure we read full files
+ SparkScanBuilder scanBuilder = new SparkScanBuilder(spark, table, scanOptions());
Review comment:
I like this idea and we don't need `RequiresScan` then.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r521669207
##########
File path: spark3/src/main/java/org/apache/iceberg/spark/source/SparkTable.java
##########
@@ -155,6 +163,39 @@ public WriteBuilder newWriteBuilder(LogicalWriteInfo info) {
return new SparkWriteBuilder(sparkSession(), icebergTable, info);
}
+ @Override
+ public MergeBuilder newMergeBuilder(LogicalWriteInfo info) {
+ String mode = icebergTable.properties().getOrDefault(WRITE_ROW_LEVEL_MODE, WRITE_ROW_LEVEL_MODE_DEFAULT);
+ ValidationException.check(mode.equals("copy-on-write"), "Unsupported row operations mode: %s", mode);
+ return new SparkMergeBuilder(sparkSession(), icebergTable, info);
+ }
+
+ @Override
+ public boolean canDeleteWhere(Filter[] filters) {
+ // TODO: multiple partition specs
Review comment:
That's probably the simplest approach. Let's do that.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r521659562
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/execution/datasources/v2/ReplaceDataExec.scala
##########
@@ -0,0 +1,47 @@
+/*
+ * 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.spark.sql.execution.datasources.v2
+
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.connector.write.BatchWrite
+import org.apache.spark.sql.execution.SparkPlan
+
+case class ReplaceDataExec(batchWrite: BatchWrite, queryExec: SparkPlan) extends V2TableWriteExec {
+
+ // override child so that we can properly plan queryExec without executing the file filter
+ override def child: SparkPlan = queryExec
+
+ // TODO: doPrepare is NOT invoked by V2TableWriteExec
Review comment:
I think the code below can be as simple as this:
```
override lazy val query: SparkPlan = {
queryExec.prepare()
queryExec
}
```
Is this what you mean, @rdblue?
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r521648214
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteDelete.scala
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import java.util.UUID
+import org.apache.spark.sql.{sources, AnalysisException}
+import org.apache.spark.sql.catalyst.analysis.Resolver
+import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeReference, EqualNullSafe, Expression, InputFileName, Literal, Not, PredicateHelper, SubqueryExpression}
+import org.apache.spark.sql.catalyst.expressions.Literal.FalseLiteral
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, DeleteFromTable, DynamicFileFilter, Filter, LocalRelation, LogicalPlan, OverwriteFiles, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{SupportsMetadataOnlyDeletes, Table}
+import org.apache.spark.sql.connector.write.{LogicalWriteInfo, LogicalWriteInfoImpl, RowLevelOperationsBuilder, SupportsPushDownRowLevelFilters}
+import org.apache.spark.sql.execution.datasources.DataSourceStrategy
+import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, DataSourceV2ScanRelation}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.{BooleanType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+// TODO: should be part of early scan push down after the delete condition is optimized
+object RewriteDelete extends Rule[LogicalPlan] with PredicateHelper {
+
+ import org.apache.spark.sql.execution.datasources.v2.ExtendedDataSourceV2Implicits._
+
+ private val FILE_NAME_COL = "_file"
+
+ override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+ // no need to execute this delete as the condition is false
+ case DeleteFromTable(_, Some(FalseLiteral)) =>
+ LocalRelation()
Review comment:
Well, you are right, it is weird. I think we can get rid of this branch at all. It should be covered by `deleteWhere` case. We just need to adapt our delete with filters implementation to handle `false` predicates.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] rdblue commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519062752
##########
File path: spark3/src/main/java/org/apache/iceberg/spark/source/BaseBatchScan.java
##########
@@ -0,0 +1,263 @@
+/*
+ * 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 java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import org.apache.iceberg.CombinedScanTask;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.SchemaParser;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.TableProperties;
+import org.apache.iceberg.encryption.EncryptionManager;
+import org.apache.iceberg.hadoop.HadoopInputFile;
+import org.apache.iceberg.hadoop.Util;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.mapping.NameMapping;
+import org.apache.iceberg.mapping.NameMappingParser;
+import org.apache.iceberg.spark.Spark3Util;
+import org.apache.iceberg.spark.SparkSchemaUtil;
+import org.apache.iceberg.util.TableScanUtil;
+import org.apache.spark.broadcast.Broadcast;
+import org.apache.spark.sql.catalyst.InternalRow;
+import org.apache.spark.sql.connector.read.Batch;
+import org.apache.spark.sql.connector.read.InputPartition;
+import org.apache.spark.sql.connector.read.PartitionReader;
+import org.apache.spark.sql.connector.read.PartitionReaderFactory;
+import org.apache.spark.sql.connector.read.Scan;
+import org.apache.spark.sql.connector.read.SupportsReportStatistics;
+import org.apache.spark.sql.types.StructType;
+import org.apache.spark.sql.util.CaseInsensitiveStringMap;
+import org.apache.spark.sql.vectorized.ColumnarBatch;
+
+abstract class BaseBatchScan implements Scan, Batch, SupportsReportStatistics {
+ protected final Table table;
Review comment:
I think we should take the approach I used for `RequiresDistributionAndOrdering` and add inner classes. Then these can be private variables and the base class can be used for whatever we'd like.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r519553151
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/OptimizeConditionsInRowLevelOperations.scala
##########
@@ -0,0 +1,45 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.expressions.{Expression, Literal, SubqueryExpression}
+import org.apache.spark.sql.catalyst.plans.logical.{DeleteFromTable, Filter, LocalRelation, LogicalPlan}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.execution.datasources.v2.DataSourceV2ScanRelation
+
+// a temp solution until we move the rewrite of row level ops after operator optimization rules
+object OptimizeConditionsInRowLevelOperations extends Rule[LogicalPlan] {
+ override def apply(plan: LogicalPlan): LogicalPlan = plan transform {
+ // TODO: fix this?
Review comment:
Well, we can have conditions that contain predicates with and without subqueries. I'll have to test this a bit more.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
[GitHub] [iceberg] aokolnychyi commented on a change in pull request #1738: Spark: Copy-on-write DELETE prototype
Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #1738:
URL: https://github.com/apache/iceberg/pull/1738#discussion_r520894166
##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteDelete.scala
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import java.util.UUID
+import org.apache.spark.sql.{sources, AnalysisException}
+import org.apache.spark.sql.catalyst.analysis.Resolver
+import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeReference, EqualNullSafe, Expression, InputFileName, Literal, Not, PredicateHelper, SubqueryExpression}
+import org.apache.spark.sql.catalyst.expressions.Literal.FalseLiteral
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, DeleteFromTable, DynamicFileFilter, Filter, LocalRelation, LogicalPlan, OverwriteFiles, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{SupportsMetadataOnlyDeletes, Table}
+import org.apache.spark.sql.connector.write.{LogicalWriteInfo, LogicalWriteInfoImpl, RowLevelOperationsBuilder, SupportsPushDownRowLevelFilters}
+import org.apache.spark.sql.execution.datasources.DataSourceStrategy
+import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, DataSourceV2ScanRelation}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.{BooleanType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+// TODO: should be part of early scan push down after the delete condition is optimized
+object RewriteDelete extends Rule[LogicalPlan] with PredicateHelper {
+
+ import org.apache.spark.sql.execution.datasources.v2.ExtendedDataSourceV2Implicits._
+
+ private val FILE_NAME_COL = "_file"
+
+ override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+ // no need to execute this delete as the condition is false
+ case DeleteFromTable(_, Some(FalseLiteral)) =>
+ LocalRelation()
Review comment:
I agree but I am not sure we can modify the exec node for deletes.
----------------------------------------------------------------
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org