felipepessoto commented on code in PR #13674:
URL: https://github.com/apache/gluten/pull/13674#discussion_r4732891202


##########
backends-velox/src-delta33/main/scala/org/apache/spark/sql/delta/GlutenOptimisticTransaction.scala:
##########
@@ -58,9 +61,40 @@ class GlutenOptimisticTransaction(delegate: 
OptimisticTransaction)
           "falling back to the vanilla Delta write path.")
       return super.writeFiles(inputData, writeOptions, isOptimize, 
additionalConstraints)
     }
-    hasWritten = true
-
     val spark = inputData.sparkSession
+    // Check the current transaction metadata, including table properties 
added after stream startup.
+    // Use the same effective configuration as Delta's writer and the Delta 
4.x native write gate.
+    // scalastyle:off deltahadoopconfiguration
+    val jobHadoopConf =
+      spark.sessionState.newHadoopConfWithOptions(metadata.configuration ++ 
deltaLog.options)
+    // scalastyle:on deltahadoopconfiguration
+    val encrypted =
+      
jobHadoopConf.get(EncryptionPropertiesFactory.CRYPTO_FACTORY_CLASS_PROPERTY_NAME)
 != null
+    val maxRecordsPerFile = writeOptions
+      .flatMap(_.options.get(DeltaOptions.MAX_RECORDS_PER_FILE))
+      .map(_.toLong)
+      .getOrElse(spark.sessionState.conf.maxRecordsPerFile)
+    // Only the partitioned native writer can split a columnar batch across 
output files.
+    if (encrypted || (metadata.partitionColumns.isEmpty && maxRecordsPerFile > 
0)) {
+      logInfo("Writing with vanilla Delta to honor Parquet encryption or 
maxRecordsPerFile.")
+      return super.writeFiles(inputData, writeOptions, isOptimize, 
additionalConstraints)
+    }
+    // Velox does not rebase dates or reject ancient dates. Check nested data 
columns too
+    // partition only dates are stored in directory names, not Parquet data 
columns.
+    val hasDateData = inputData.schema.fields.exists {

Review Comment:
   `inputData.schema` misses DATE columns that Delta adds during 
`normalizeData`. I reproduced this on `916742e` with an unpartitioned table 
containing `id BIGINT, d DATE GENERATED ALWAYS AS (date_add(DATE '1500-01-01', 
CAST(id AS INT)))` and a stream supplying only `id = 0`. Under `EXCEPTION`, 
Delta raised `WRITE_ANCIENT_DATETIME`, stayed at table version 0, and wrote no 
`commits/0`. The native sink committed version 1 and advanced the checkpoint. 
Under `LEGACY` it also wrote a different physical day without Spark’s legacy 
marker.
   
   Please check the non-partition fields that will actually be written, 
including generated columns, rather than only the incoming schema. This would 
make a useful regression case alongside the direct DATE test.



##########
docs/backends/velox/get-started/VeloxDelta.md:
##########
@@ -531,7 +532,8 @@ A blank cell means unknown.
 | spark.sql.catalog.spark_catalog | builtin on Spark 4.x, (unset) on 3.x | Set 
to `org.apache.spark.sql.delta.catalog.DeltaCatalog`. | ✅ |
 | spark.sql.ansi.enabled | true on Spark 4.x, false on 3.x | When true, every 
query falls back unless `spark.gluten.sql.ansiFallback.enabled=false`. From 
code. | ⚠️ |
 | spark.sql.parquet.compression.codec | snappy | Codec for native writes when 
the `compression` option is unset. On Spark 4.x, a codec Velox cannot write 
makes the write fall back (from code). | ⚠️ |
-| spark.sql.files.maxRecordsPerFile | 0 | Exact on partitioned native writes. 
On unpartitioned tables, Spark 4.x falls back (from code); for Spark 3.5, see 
[Write problems](#write-problems). | ⚠️ |
+| spark.sql.files.maxRecordsPerFile | 0 | Exact on partitioned native writes. 
Positive limits on unpartitioned tables use Delta's writer on Spark 3.5 and 
4.x, for batch and streaming writes. | ⚠️ |
+| spark.sql.parquet.datetimeRebaseModeInWrite | EXCEPTION | DATE data columns 
use Delta's writer unless the mode is `CORRECTED`, including nested dates. 
Applies to batch and streaming on Spark 3.5 and 4.x. Spark 4.x also checks 
TIMESTAMP columns. | ⚠️ |

Review Comment:
   The default here depends on the Spark version: `EXCEPTION` on 3.5, but 
`CORRECTED` starting in [Spark 
4.0](https://github.com/apache/spark/blob/v4.0.2/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala#L4856-L4871).
 Listing both matters because the default determines whether DATE writes fall 
back.



##########
backends-velox/src-delta33/main/scala/org/apache/spark/sql/delta/GlutenOptimisticTransaction.scala:
##########
@@ -58,9 +61,40 @@ class GlutenOptimisticTransaction(delegate: 
OptimisticTransaction)
           "falling back to the vanilla Delta write path.")
       return super.writeFiles(inputData, writeOptions, isOptimize, 
additionalConstraints)
     }
-    hasWritten = true
-
     val spark = inputData.sparkSession
+    // Check the current transaction metadata, including table properties 
added after stream startup.
+    // Use the same effective configuration as Delta's writer and the Delta 
4.x native write gate.
+    // scalastyle:off deltahadoopconfiguration
+    val jobHadoopConf =
+      spark.sessionState.newHadoopConfWithOptions(metadata.configuration ++ 
deltaLog.options)
+    // scalastyle:on deltahadoopconfiguration
+    val encrypted =
+      
jobHadoopConf.get(EncryptionPropertiesFactory.CRYPTO_FACTORY_CLASS_PROPERTY_NAME)
 != null
+    val maxRecordsPerFile = writeOptions
+      .flatMap(_.options.get(DeltaOptions.MAX_RECORDS_PER_FILE))
+      .map(_.toLong)
+      .getOrElse(spark.sessionState.conf.maxRecordsPerFile)
+    // Only the partitioned native writer can split a columnar batch across 
output files.
+    if (encrypted || (metadata.partitionColumns.isEmpty && maxRecordsPerFile > 
0)) {
+      logInfo("Writing with vanilla Delta to honor Parquet encryption or 
maxRecordsPerFile.")
+      return super.writeFiles(inputData, writeOptions, isOptimize, 
additionalConstraints)
+    }
+    // Velox does not rebase dates or reject ancient dates. Check nested data 
columns too
+    // partition only dates are stored in directory names, not Parquet data 
columns.
+    val hasDateData = inputData.schema.fields.exists {
+      field =>
+        !metadata.partitionColumns.exists(spark.sessionState.conf.resolver(_, 
field.name)) &&
+        SchemaUtils.typeExistsRecursively(field.dataType)(_ == DateType)

Review Comment:
   Historical TIMESTAMP data columns still bypass the write policy because this 
predicate only matches `DateType`. I appended `TIMESTAMP '1500-01-01 00:00:00'` 
in UTC with Spark 3.5.5. With default `INT96` output, Delta raised 
`WRITE_ANCIENT_DATETIME` and left no Delta or checkpoint commit; the native 
sink committed both. I got the same difference with `TIMESTAMP_MICROS` and 
`datetimeRebaseModeInWrite=EXCEPTION`.
   
   Please honor the requested timestamp encoding and its corresponding rebase 
policy: Spark uses `int96RebaseModeInWrite` for INT96 and 
`datetimeRebaseModeInWrite` for MICROS/MILLIS. Fall back when the native writer 
can't match 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.

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]

Reply via email to