spark-test commented on code in PR #60679:
URL: https://github.com/apache/spark/pull/60679#discussion_r4623633308


##########
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:
   **Nit (P3):** Each default in this list puts its map at the top level (m, 
md) or directly inside a struct (sm, sn, sw), and the only array-valued 
defaults, a.scores and w.vs, hold no map, so nothing in either test needs the 
GenericArrayData branch of containsMap (WritableColumnVector.java:831-832) to 
find a map. containsMap handles arrays correctly today, but if that branch 
stopped finding maps, both tests would still pass while a default such as 
array(map('a', 1, 'b', 2), null) would be filled instead of failing. By the 
review's reading of the code, the map branch then records a length-1 map while 
appending both entries, so the column would read [{'a': 1}, null] with entry 
'b' silently missing. A column such as am array<map<string, int>> DEFAULT 
array(map('a', 1, 'b', 2), null), added to withNullFieldAndWideDecimalDefaults 
and to this list, fails with 'Cannot assign default column value' under both 
settings and closes the gap.
   
   **Verification:**
   - **Regression:** For both formats, a missing column whose default nests a 
two-entry map in an array next to a NULL element fails with FAILED_READ_FILE 
caused by 'Cannot assign default column value' with the legacy flag on and off, 
and the fill test fails if containsMap stops looking inside arrays, because the 
column then reads without an error and its map loses its second entry.



##########
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:
   **Nit (P3):** With the flag set to "false" explicitly, no read in either new 
test runs with it unset: the legacy test sets it to "true", and the fixture 
sets "true" only while it creates the table. Unset is the state users get, and 
on Parquet it takes its own path: SessionState.newHadoopConf copies only 
explicitly set SQL confs (SessionState.scala:147-151), so 
VectorizedParquetRecordReader.initBatch falls back to the entry's default 
(VectorizedParquetRecordReader.java:297-301), while the ORC readers read the 
default through getConf. The default works today, but a regression such as 
createWithDefault(true) or a hardcoded true fallback would make these reads 
fail again while both tests still pass. Dropping this withSQLConf, so the reads 
run with the flag unset, covers it; the fixture's withSQLConf already unsets 
the flag after creating the table.
   
   **Verification:**
   - **Regression:** With the legacy flag unset, vectorized Parquet and ORC 
reads of the affected columns return their existence defaults while the 
map-containing columns keep the old error, and the test fails if the conf's 
default or the Parquet reader's fallback for an absent key is changed to true.



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