malinjawi commented on code in PR #13674:
URL: https://github.com/apache/gluten/pull/13674#discussion_r4665420389
##########
gluten-ut/spark35/src-delta/test/scala/org/apache/spark/sql/execution/datasources/GlutenDeltaNativePartitionWriteSuite.scala:
##########
@@ -158,4 +159,60 @@ class GlutenDeltaNativePartitionWriteSuite extends
GlutenQueryTest with SharedSp
}
}
}
+
+ for (partitioned <- Seq(false, true); nativeWrite <- Seq(false, true)) {
+ test(s"Delta streaming appends: partitioned=$partitioned,
nativeWrite=$nativeWrite") {
+ assume(BackendTestUtils.isVeloxBackendLoaded())
+ import testImplicits._
+ withSQLConf(
+ "spark.gluten.sql.columnar.backend.velox.delta.enableNativeWrite" ->
nativeWrite.toString,
+ "spark.databricks.delta.stats.collect" -> "false") {
+ withTempDir {
+ dir =>
+ val path = dir.getCanonicalPath + "/data"
+ val input = MemoryStream[(Option[Long], Option[String])]
+ val writer = input.toDF().toDF("id", "part")
+ .filter($"id".isNull || $"id" >= 0L)
+ .writeStream.format("delta").outputMode("append")
+ .option("checkpointLocation", dir.getCanonicalPath +
"/checkpoint")
+ val query = (if (partitioned) writer.partitionBy("part") else
writer).start(path)
+ try {
+ input.addData((Some(1L), Some("a")), (None, Some("a")))
+ query.processAllAvailable()
+ input.addData((Some(2L), None), (Some(3L), Some("b")))
+ query.processAllAvailable()
+ // Advance the source offset with an empty output batch.
+ input.addData((Some(-1L), Some("discarded")))
Review Comment:
I think this fails for partitioned=false, nativeWrite=true. Velox writes a
0-byte file when task 0 gets no rows (#13390), and with stats off Delta commits
it. Did this run? Streaming hits empty batches all the time, so this needs
#13390 first.
##########
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:
Please give this its own flag, default false. As it is, anyone running
enableNativeWrite for batch gets their streams moved to Velox on upgrade. Same
idea as enableChangeDataFeedScan.
Also, the scan-only/ANSI checks below are copies of Validators /
FallbackOnANSIMode. A comment pointing there would help keep them in sync.
##########
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:
The stream runs on a cloned session, so this can't change after start. The
check in the rule is enough:
```suggestion
override def addBatch(batchId: Long, data: DataFrame): Unit =
DeltaV2WriteOperators.withColumnarTransaction {
delegate.addBatch(batchId, data)
}
```
##########
gluten-ut/spark35/src-delta/test/scala/org/apache/spark/sql/execution/datasources/GlutenDeltaNativePartitionWriteSuite.scala:
##########
@@ -158,4 +159,60 @@ class GlutenDeltaNativePartitionWriteSuite extends
GlutenQueryTest with SharedSp
}
}
}
+
+ for (partitioned <- Seq(false, true); nativeWrite <- Seq(false, true)) {
+ test(s"Delta streaming appends: partitioned=$partitioned,
nativeWrite=$nativeWrite") {
+ assume(BackendTestUtils.isVeloxBackendLoaded())
+ import testImplicits._
+ withSQLConf(
+ "spark.gluten.sql.columnar.backend.velox.delta.enableNativeWrite" ->
nativeWrite.toString,
+ "spark.databricks.delta.stats.collect" -> "false") {
Review Comment:
Can we run this with stats on as well? That's the default, and it's the path
most streams take.
##########
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)
+ }
+ }
+
+ override def toString: String = s"Gluten$delegate"
Review Comment:
This shows up as the sink description in progress JSON, and people match on
DeltaSink[:
```suggestion
override def toString: String = delegate.toString
```
##########
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:
Why not Complete? It goes through the same writeFiles.
##########
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:
Streaming picks up the open native write gaps too: column mapping tables
don't get field IDs until #13383, and row tracking is #13390/#13391. Either
fall back for those tables or mention it 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.
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]