dtenedor commented on code in PR #60679:
URL: https://github.com/apache/spark/pull/60679#discussion_r4636248163


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -6834,6 +6834,19 @@ object SQLConf {
       .booleanConf
       .createWithDefault(true)
 
+  val LEGACY_VECTORIZED_READER_FAIL_ON_NULL_FIELD_OR_WIDE_DECIMAL_DEFAULT =
+    
buildConf("spark.sql.legacy.vectorizedReader.failOnNullFieldOrWideDecimalDefault")
+      .internal()
+      .doc("When true, the vectorized Parquet and ORC readers fail on a 
missing " +
+        "column whose existence default contains no map and holds a NULL 
inside a struct or " +
+        "array or a decimal whose unscaled value overflows a long, as before 
Spark 5.0. When " +
+        "false (the default), they return the default value. Defaults that 
contain a map fail " +
+        "either way.")
+      .version("5.0.0")
+      .withBindingPolicy(ConfigBindingPolicy.SESSION)

Review Comment:
   This goes away with the conf.



##########
sql/core/src/main/java/org/apache/spark/sql/execution/datasources/parquet/ParquetColumnVector.java:
##########
@@ -103,7 +105,8 @@ final class ParquetColumnVector {
       WritableColumnVector fileContent = vector.reserveNewColumn(
         capacity, fileContentCol.sparkType());
       ParquetColumnVector contentVector = new 
ParquetColumnVector(fileContentCol, fileContent,
-        capacity, missingColumns, /* isTopLevel= */ false, /* defaultValue= */ 
null);
+        capacity, missingColumns, /* isTopLevel= */ false, /* defaultValue= */ 
null,
+        fillNullsAndWideDecimalDefaults);

Review Comment:
   This goes away with the conf. `ParquetColumnVector` is back to its master 
version.



##########
sql/core/src/test/scala/org/apache/spark/sql/sources/InsertSuite.scala:
##########
@@ -2063,6 +2063,109 @@ class InsertSuite extends DataSourceTest with 
SharedSparkSession {
     }
   }
 
+  private val legacyNullFieldOrWideDecimalDefault =
+    SQLConf.LEGACY_VECTORIZED_READER_FAIL_ON_NULL_FIELD_OR_WIDE_DECIMAL_DEFAULT
+
+  // Creates a table in `format` with two old rows, then adds columns whose 
existence defaults
+  // have a NULL field, a NULL array element, a NULL nested struct or a 
decimal whose unscaled
+  // value overflows a long, at the top level or in a struct and array (d, e, 
f, w); compact and
+  // wide decimals that fit in a long (i, l, c); and defaults that contain a 
map, which keep the
+  // old error: maps with a NULL or an overflowing decimal value, also nested 
in a struct (m, md,
+  // sm), and a struct with an ordinary map next to a NULL or an overflowing 
decimal (sn, sw).
+  private def withNullFieldAndWideDecimalDefaults(format: String)(f: => Unit): 
Unit = {
+    val wide = "99999999999999999999999999999999999999"
+    withSQLConf(
+      SQLConf.PARQUET_VECTORIZED_READER_ENABLED.key -> "true",
+      SQLConf.PARQUET_VECTORIZED_READER_NESTED_COLUMN_ENABLED.key -> "true",
+      SQLConf.ORC_VECTORIZED_READER_ENABLED.key -> "true",
+      SQLConf.ORC_VECTORIZED_READER_NESTED_COLUMN_ENABLED.key -> "true") {
+      withTable("t") {
+        // The table and its defaults are written with the legacy flag on (the 
reader's old
+        // behavior); the flag changes nothing the writer persists.
+        withSQLConf(legacyNullFieldOrWideDecimalDefault.key -> "true") {
+          sql(s"create table t(id int) using $format")
+          sql("insert into t values (1), (2)")
+          sql("alter table t add columns (" +
+            "s struct<tier: int, nickname: string> " +
+            "default named_struct('tier', 1, 'nickname', cast(null as 
string)), " +
+            "a struct<tier: int, scores: array<int>> " +
+            "default named_struct('tier', 1, 'scores', array(1, null, 3)), " +
+            "n struct<x: int, inner: struct<y: int>> " +
+            "default named_struct('x', 1, 'inner', cast(null as struct<y: 
int>)), " +
+            s"d decimal(38, 0) default $wide, " +
+            "e decimal(19, 0) default -9999999999999999999, " +
+            "f decimal(38, 30) default 1.5, " +
+            "i decimal(9, 2) default 1.25, " +
+            "l decimal(18, 0) default 999999999999999999, " +
+            "c decimal(38, 10) default 123.5, " +
+            "w struct<v: decimal(38, 0), vs: array<decimal(38, 0)>> " +
+            s"default named_struct('v', $wide, 'vs', array($wide)), " +
+            "m map<string, int> default map('a', cast(null as int), 'b', 2), " 
+
+            s"md map<string, decimal(38, 0)> default map('a', $wide), " +
+            "sm struct<m: map<string, decimal(38, 0)>> " +
+            s"default named_struct('m', map('a', $wide)), " +
+            "sn struct<n: int, m: map<string, int>> " +
+            "default named_struct('n', cast(null as int), 'm', map('a', 1, 
'b', 2)), " +
+            "sw struct<v: decimal(38, 0), m: map<string, int>> " +
+            s"default named_struct('v', $wide, 'm', map('a', 1, 'b', 2)))")
+        }
+        f
+      }
+    }
+  }
+
+  private val fixedColumns = Seq("s", "a", "n", "d", "e", "f", "w")
+  private val mapColumns = Seq("m", "md", "sm", "sn", "sw")

Review Comment:
   Added in 42937cdf: `am ARRAY<MAP<STRING, INT>> DEFAULT array(map('a', 1, 
'b', 2), CAST(NULL AS MAP<STRING, INT>))`, which is expected to fail with 
`Cannot assign default column value`. With the `GenericArrayData` branch of 
`containsMap` removed, the test now fails, because the `am` read no longer 
throws.



##########
sql/core/src/test/scala/org/apache/spark/sql/sources/InsertSuite.scala:
##########
@@ -2063,6 +2063,109 @@ class InsertSuite extends DataSourceTest with 
SharedSparkSession {
     }
   }
 
+  private val legacyNullFieldOrWideDecimalDefault =
+    SQLConf.LEGACY_VECTORIZED_READER_FAIL_ON_NULL_FIELD_OR_WIDE_DECIMAL_DEFAULT
+
+  // Creates a table in `format` with two old rows, then adds columns whose 
existence defaults
+  // have a NULL field, a NULL array element, a NULL nested struct or a 
decimal whose unscaled
+  // value overflows a long, at the top level or in a struct and array (d, e, 
f, w); compact and
+  // wide decimals that fit in a long (i, l, c); and defaults that contain a 
map, which keep the
+  // old error: maps with a NULL or an overflowing decimal value, also nested 
in a struct (m, md,
+  // sm), and a struct with an ordinary map next to a NULL or an overflowing 
decimal (sn, sw).
+  private def withNullFieldAndWideDecimalDefaults(format: String)(f: => Unit): 
Unit = {
+    val wide = "99999999999999999999999999999999999999"
+    withSQLConf(
+      SQLConf.PARQUET_VECTORIZED_READER_ENABLED.key -> "true",
+      SQLConf.PARQUET_VECTORIZED_READER_NESTED_COLUMN_ENABLED.key -> "true",
+      SQLConf.ORC_VECTORIZED_READER_ENABLED.key -> "true",
+      SQLConf.ORC_VECTORIZED_READER_NESTED_COLUMN_ENABLED.key -> "true") {
+      withTable("t") {
+        // The table and its defaults are written with the legacy flag on (the 
reader's old
+        // behavior); the flag changes nothing the writer persists.
+        withSQLConf(legacyNullFieldOrWideDecimalDefault.key -> "true") {
+          sql(s"create table t(id int) using $format")
+          sql("insert into t values (1), (2)")
+          sql("alter table t add columns (" +
+            "s struct<tier: int, nickname: string> " +
+            "default named_struct('tier', 1, 'nickname', cast(null as 
string)), " +
+            "a struct<tier: int, scores: array<int>> " +
+            "default named_struct('tier', 1, 'scores', array(1, null, 3)), " +
+            "n struct<x: int, inner: struct<y: int>> " +
+            "default named_struct('x', 1, 'inner', cast(null as struct<y: 
int>)), " +
+            s"d decimal(38, 0) default $wide, " +
+            "e decimal(19, 0) default -9999999999999999999, " +
+            "f decimal(38, 30) default 1.5, " +
+            "i decimal(9, 2) default 1.25, " +
+            "l decimal(18, 0) default 999999999999999999, " +
+            "c decimal(38, 10) default 123.5, " +
+            "w struct<v: decimal(38, 0), vs: array<decimal(38, 0)>> " +
+            s"default named_struct('v', $wide, 'vs', array($wide)), " +
+            "m map<string, int> default map('a', cast(null as int), 'b', 2), " 
+
+            s"md map<string, decimal(38, 0)> default map('a', $wide), " +
+            "sm struct<m: map<string, decimal(38, 0)>> " +
+            s"default named_struct('m', map('a', $wide)), " +
+            "sn struct<n: int, m: map<string, int>> " +
+            "default named_struct('n', cast(null as int), 'm', map('a', 1, 
'b', 2)), " +
+            "sw struct<v: decimal(38, 0), m: map<string, int>> " +
+            s"default named_struct('v', $wide, 'm', map('a', 1, 'b', 2)))")
+        }
+        f
+      }
+    }
+  }
+
+  private val fixedColumns = Seq("s", "a", "n", "d", "e", "f", "w")
+  private val mapColumns = Seq("m", "md", "sm", "sn", "sw")
+  private val controlColumns = Seq("i", "l", "c")
+  private val controlValues =
+    Seq(BigDecimal("1.25"), BigDecimal("999999999999999999"), 
BigDecimal("123.5"))
+
+  // Asserts that reading `column` fails with FAILED_READ_FILE caused by the 
old reader error.
+  private def assertOldReadError(column: String, clue: String): Unit = {
+    val e = intercept[SparkException](sql(s"select id, $column from 
t").collect())
+    assert(e.getCondition.startsWith("FAILED_READ_FILE"), clue)
+    val causes = Iterator.iterate[Throwable](e)(_.getCause).takeWhile(_ != 
null).mkString("\n")
+    assert(causes.contains("BigInteger out of long range") ||
+      causes.contains("Cannot assign default column value"), clue)
+  }
+
+  test("vectorized readers fill existence defaults with NULL fields and wide 
decimals") {
+    Seq("parquet", "orc").foreach { format =>
+      withNullFieldAndWideDecimalDefaults(format) {
+        withSQLConf(legacyNullFieldOrWideDecimalDefault.key -> "false") {

Review Comment:
   This goes away with the conf: the readers have no flag now, so every read in 
the test runs with the shipped behavior.



-- 
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