dongjoon-hyun commented on code in PR #60679:
URL: https://github.com/apache/spark/pull/60679#discussion_r4623160649
##########
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 =
Review Comment:
**[1] Could we drop the legacy conf?**
As the PR description says, only queries that used to fail change. Every
value that now reaches the new `null` branch, or the byte-array branch with a
decimal that overflows a long, used to end in `FAILED_READ_FILE`. So setting
this conf to `true` gives users nothing but the old error back, and no query
can depend on that. Legacy confs usually restore an old result, not an old
failure.
Meanwhile the conf costs an internal conf, a migration guide entry, a public
`appendObjects(int, Object, boolean)` overload, a new `OrcColumnarBatchReader`
constructor, and a parameter threaded through every recursive
`ParquetColumnVector` constructor. It also adds conf reads in
`VectorizedParquetRecordReader`, `OrcFileFormat` and
`OrcPartitionReaderFactory`, plus a `decimal.toUnscaledLong()` call
(`WritableColumnVector.java:906`) that runs only for its exception. Without the
conf, the fix is the `WritableColumnVector` change plus the first test. That
would also make it simpler to deliver to 4.4.0, as I asked in my [earlier
comment](https://github.com/apache/spark/pull/60679#discussion_r4623111022).
##########
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 " +
Review Comment:
**[2] The conf doc says that every default with a map fails, but most of
them still read.**
`containsMap` only turns off the new filling. It does not make a map default
fail. Take a map default with no NULL and no overflowing decimal, such as `m
MAP<STRING, INT> DEFAULT map('a', 1, 'b', 2)`. It still reads without an error
under both settings, because all of its keys and values are supported and the
map branch never returns `Optional.empty()`. That is the SPARK-60940 case that
drops entries. Only a map default that also holds a NULL or such a decimal
keeps failing.
The migration guide entry and the Javadoc of `appendObjects` ("keeps that
behavior") are precise about this. Could the conf doc say the same, for example
"Defaults that contain a map and also hold a NULL or such a decimal fail either
way"?
##########
sql/core/src/main/java/org/apache/spark/sql/execution/vectorized/WritableColumnVector.java:
##########
@@ -902,12 +969,12 @@ public Optional<Integer> appendObjects(int length, Object
value) {
int result = 0;
for (int i = 0; i < length; ++i) {
for (Object key : data.keyArray().array()) {
- if (childColumns[0].appendObjects(1, key).isEmpty()) {
+ if (childColumns[0].appendObjectsImpl(1, key, false).isEmpty()) {
Review Comment:
**[6] The map branch hardcodes `false`, which `containsMap` already
guarantees.**
`appendObjects` passes `fillNullsAndWideDecimals && !containsMap(value)`, so
this branch is only ever reached with `false` already. The same rule now lives
in two places. The description plans to remove the `containsMap` check once
SPARK-60940 fixes this branch. If only that check is removed, these two `false`
arguments would still make two kinds of map value fail even with the conf off:
a NULL value (`map('a', CAST(NULL AS INT))`) and an overflowing decimal value.
Could this pass `fillNullsAndWideDecimals` through, or carry a comment saying
that it has to change together with `containsMap`?
##########
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:
**[8] nit: the child `ParquetColumnVector`s never use this flag.**
Children are always built with `defaultValue = null`, so they never reach
`appendObjects`, and the flag passed here and at L139 is never read. Only the
top-level call in `VectorizedParquetRecordReader.initBatch` needs it. A
separate constructor for top-level columns, or a field set only there, would
leave the recursive calls unchanged. This also goes away with [1].
##########
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>> " +
Review Comment:
**[5] The `array(1, NULL, 3)` example from the PR description is only tested
inside a struct.**
Column `a` covers a NULL array element only inside `appendStruct(false)`,
with `length = 1`. A top-level `ARRAY<INT> DEFAULT array(1, NULL, 3)` column
takes the array branch with `length = capacity`. On the Parquet side it is also
a top-level missing array column, which this test never reads. Could we add
one? A NULL element of an `ARRAY<STRUCT<...>>` would also be worth adding,
since it takes the `appendStruct(true)` path inside an array.
##########
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 = {
Review Comment:
**[4] `assertOldReadError` accepts either old error for every column.**
The legacy-flag test is meant to show that the flag brings back each
column's old failure. This helper, however, passes when the causes contain
either message. Suppose a wide-decimal column (`d`, `e`, `f`, `w`, `md`, `sm`,
`sw`) started to fail with "Cannot assign default column value" instead of the
`ArithmeticException`. Or suppose a NULL column (`s`, `a`, `n`, `m`, `sn`)
failed with the decimal error. The test would still pass in both cases. Could
the caller pass the expected message for each column?
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/orc/OrcPartitionReaderFactory.scala:
##########
@@ -64,6 +64,8 @@ case class OrcPartitionReaderFactory(
private val isCaseSensitive = sqlConf.caseSensitiveAnalysis
private val capacity = sqlConf.orcVectorizedReaderBatchSize
private val orcFilterPushDown = sqlConf.orcFilterPushDown
+ private val fillNullsAndWideDecimalDefaults =
Review Comment:
**[3] No test goes through the DSv2 readers.**
Both new tests read a catalog table created with `USING parquet` or `USING
orc`. `spark.sql.sources.useV1SourceList` lists both formats by default, so the
tests only exercise `OrcFileFormat` and `ParquetFileFormat`. Nothing covers the
conf read here, or the Parquet path through `ParquetPartitionReaderFactory`. If
the `!` in this definition were dropped, or the factory went back to the
two-argument constructor, both tests would still pass. Could the tests also
read through the V2 readers, for example with `SQLConf.USE_V1_SOURCE_LIST.key
-> ""`? This goes away with [1].
##########
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:
**[7] Should this be `ConfigBindingPolicy.NOT_APPLICABLE`?**
The guide in `ConfigBindingPolicy.scala` first asks whether a config can
change the result of resolving the body of a view, UDF or procedure, and says
to use `NOT_APPLICABLE` if it cannot. This flag is only read when a scan runs:
in `OrcFileFormat`, in `OrcPartitionReaderFactory`, and from the Hadoop conf in
the Parquet reader. So it never changes a resolved plan. Both policies read the
session value, so nothing changes at runtime. Only the classification would be
more accurate.
--
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]