ReemaAlzaid commented on code in PR #13674: URL: https://github.com/apache/gluten/pull/13674#discussion_r4667337208
########## backends-velox/src-delta33/main/scala/org/apache/spark/sql/delta/sources/OffloadDeltaStreamingWrite.scala: ########## @@ -0,0 +1,68 @@ +/* + * 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.delta.sources + +import org.apache.gluten.config.{GlutenConfig, VeloxDeltaConfig} + +import org.apache.spark.sql.DataFrame +import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan +import org.apache.spark.sql.catalyst.rules.Rule +import org.apache.spark.sql.catalyst.streaming.WriteToStream +import org.apache.spark.sql.execution.datasources.v2.DeltaV2WriteOperators +import org.apache.spark.sql.execution.streaming.Sink +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.streaming.OutputMode + +// Delta's V1 streaming sink starts transactions outside physical write command execution +case class OffloadDeltaStreamingWrite() extends Rule[LogicalPlan] { + override def apply(plan: LogicalPlan): LogicalPlan = { + if (!OffloadDeltaStreamingWrite.enabled(SQLConf.get)) { + return plan + } + plan.transformDown { + case write: WriteToStream if write.outputMode == OutputMode.Append() => + write.sink match { + case sink: DeltaSink => write.copy(sink = new GlutenDeltaSink(sink)) + case _ => write + } + } + } +} + +object OffloadDeltaStreamingWrite { + private[sources] def enabled(sqlConf: SQLConf): Boolean = { + val conf = new GlutenConfig(sqlConf) + // This sink bypasses physical write command validation, so retain its session level gates + conf.enableGluten && new VeloxDeltaConfig(sqlConf).enableNativeWrite && Review Comment: Added `enableNativeStreamingWrite`, defaulting to false. Streaming now requires both this flag and `enableNativeWrite`. Also added comments pointing to `Validators` and `FallbackOnANSIMode` to help keep the checks aligned ########## backends-velox/src-delta33/main/scala/org/apache/spark/sql/delta/sources/OffloadDeltaStreamingWrite.scala: ########## @@ -0,0 +1,68 @@ +/* + * 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.delta.sources + +import org.apache.gluten.config.{GlutenConfig, VeloxDeltaConfig} + +import org.apache.spark.sql.DataFrame +import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan +import org.apache.spark.sql.catalyst.rules.Rule +import org.apache.spark.sql.catalyst.streaming.WriteToStream +import org.apache.spark.sql.execution.datasources.v2.DeltaV2WriteOperators +import org.apache.spark.sql.execution.streaming.Sink +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.streaming.OutputMode + +// Delta's V1 streaming sink starts transactions outside physical write command execution +case class OffloadDeltaStreamingWrite() extends Rule[LogicalPlan] { + override def apply(plan: LogicalPlan): LogicalPlan = { + if (!OffloadDeltaStreamingWrite.enabled(SQLConf.get)) { + return plan + } + plan.transformDown { + case write: WriteToStream if write.outputMode == OutputMode.Append() => + write.sink match { + case sink: DeltaSink => write.copy(sink = new GlutenDeltaSink(sink)) + case _ => write + } + } + } +} + +object OffloadDeltaStreamingWrite { + private[sources] def enabled(sqlConf: SQLConf): Boolean = { + val conf = new GlutenConfig(sqlConf) + // This sink bypasses physical write command validation, so retain its session level gates + conf.enableGluten && new VeloxDeltaConfig(sqlConf).enableNativeWrite && + !conf.enableScanOnly && !(conf.enableAnsiMode && conf.enableAnsiFallback) + } +} + +// Keep Delta's schema checks, epoch deduplication and commit protocol in the original sink +class GlutenDeltaSink(delegate: DeltaSink) extends Sink { + override def addBatch(batchId: Long, data: DataFrame): Unit = { + if (OffloadDeltaStreamingWrite.enabled(data.sparkSession.sessionState.conf)) { + DeltaV2WriteOperators.withColumnarTransaction { + delegate.addBatch(batchId, data) + } + } else { + delegate.addBatch(batchId, data) + } + } Review Comment: Removed the per batch checks as suggested, the rule checks configuration at startup, and `addBatch` now delegates directly inside `withColumnarTransaction` ########## docs/backends/velox/get-started/VeloxDelta.md: ########## @@ -213,7 +213,7 @@ Gluten's CI checks that a partitioned `save(path)` on 3.5 to 4.1 and OPTIMIZE on | `INSERT INTO ... SELECT`, `INSERT OVERWRITE`, `INSERT INTO ... REPLACE WHERE` | | PartialOffload | From code. To append natively, use `save(path)` in append mode. | | | `insertInto(t)`, `saveAsTable(t)` in append mode on an existing table, `writeTo(t).append()`, `overwrite(condition)`, `overwritePartitions()` | PartialOffload | From code. | | `MERGE INTO` | | PartialOffload | Scans, the DML target scan and joins can run in Velox. On Spark 3.5 and 4.0, Gluten's tests check that a deletion-vector MERGE runs a scan of the target table in Velox. The other cases are from code. Native MERGE write is in progress in [#13324](https://github.com/apache/gluten/pull/13324). | -| | `writeStream.format("delta")` | PartialOffload | Inside `foreachBatch`, batch writes follow the rows above. From code. | +| | `writeStream.format("delta")` | Offload on Spark 3.5 in append mode; otherwise PartialOffload | Requires native write on. Inside `foreachBatch`, batch writes follow the rows above. From code. | Review Comment: Added a warning in the existing streaming documentation row: column-mapping files lack the required Parquet field IDs, and row tracking remains unvalidated with native scan limitations ########## backends-velox/src-delta33/main/scala/org/apache/spark/sql/delta/sources/OffloadDeltaStreamingWrite.scala: ########## @@ -0,0 +1,68 @@ +/* + * 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.delta.sources + +import org.apache.gluten.config.{GlutenConfig, VeloxDeltaConfig} + +import org.apache.spark.sql.DataFrame +import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan +import org.apache.spark.sql.catalyst.rules.Rule +import org.apache.spark.sql.catalyst.streaming.WriteToStream +import org.apache.spark.sql.execution.datasources.v2.DeltaV2WriteOperators +import org.apache.spark.sql.execution.streaming.Sink +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.streaming.OutputMode + +// Delta's V1 streaming sink starts transactions outside physical write command execution +case class OffloadDeltaStreamingWrite() extends Rule[LogicalPlan] { + override def apply(plan: LogicalPlan): LogicalPlan = { + if (!OffloadDeltaStreamingWrite.enabled(SQLConf.get)) { + return plan + } + plan.transformDown { + case write: WriteToStream if write.outputMode == OutputMode.Append() => Review Comment: I’ve kept this patch append only because I haven’t validated Complete mode’s replacement and restart behavior yet -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
