dtenedor commented on code in PR #60679:
URL: https://github.com/apache/spark/pull/60679#discussion_r4649100243
##########
sql/core/src/main/java/org/apache/spark/sql/execution/vectorized/WritableColumnVector.java:
##########
@@ -816,28 +814,82 @@ public final int appendStruct(boolean isNull) {
return elementsAppended;
}
+ // Types whose NULL values must be appended with appendStruct(true), which
also appends a NULL
+ // to every child column.
+ private static boolean isStructLike(DataType type) {
+ return type instanceof StructType || type instanceof VariantType
+ || type instanceof CalendarIntervalType
+ || type instanceof AnyTimestampNanoType;
+ }
+
+ // Whether `value` contains a map at any depth.
+ private static boolean containsMap(Object value) {
+ if (value instanceof ArrayBasedMapData) {
+ return true;
+ }
+ Object[] elements = null;
+ if (value instanceof GenericArrayData arrayData) {
+ elements = arrayData.array();
+ } else if (value instanceof GenericInternalRow row) {
+ elements = row.values();
+ }
+ if (elements != null) {
+ for (Object element : elements) {
+ if (containsMap(element)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
/**
* Appends multiple copies of a Java Object to the vector using the
corresponding append* method
- * above.
+ * above. A NULL nested in a struct or array, and a decimal whose unscaled
value overflows a
+ * long, are appended too, unless the value contains a map: the map branch
does not give each
+ * row its own entries yet (SPARK-60940), so such a value keeps failing as
it did before, with
+ * Optional.empty() for the NULL and ArithmeticException for the decimal.
* @param length: The number of instances to append
* @param value value to append to the vector
* @return the number of values appended if the value maps to one of the
append* methods above,
* or Optional.empty() otherwise.
*/
public Optional<Integer> appendObjects(int length, Object value) {
+ return appendObjectsImpl(length, value, !containsMap(value));
Review Comment:
Done in 01b0470b. `appendObjects(length, value)` now builds a
`RowToColumnConverter` for a one-field schema of the vector's type and appends
the value once per row. `containsMap`, the `fillNullsAndWideDecimals` flag,
`isStructLike`, the `toUnscaledLong()` call kept for its exception, and the
per-class branches are all gone. `WritableColumnVector` shrinks by about 100
lines. The converter throws for an unsupported type, so `appendObjects` now
returns nothing, and the two `Cannot assign default column value` checks in
`ParquetColumnVector` and `OrcColumnarBatchReader` are removed.
One consequence: because `MapConverter` calls `appendArray` once per row,
map defaults now read correctly, including the six map columns, which the test
now expects to read their values. So this PR also fixes the SPARK-60940 case
(#60143), and a `BINARY` default now reads too. I added a `b BINARY DEFAULT
X'0102'` column for it.
##########
sql/core/src/test/scala/org/apache/spark/sql/sources/InsertSuite.scala:
##########
@@ -2063,6 +2065,106 @@ class InsertSuite extends DataSourceTest with
SharedSparkSession {
}
}
+ // 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 (s, a,
n, ta, as, d, e, f,
+ // w); compact and wide decimals that fit in a long (i, l, c); and defaults
that contain a map
+ // and also a NULL or an overflowing decimal, which keep the old error: in
the map itself (m,
+ // md), in a map in a struct (sm), next to a map in a struct (sn, sw), and
next to a map in an
+ // array (am).
+ 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") {
+ 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>)), " +
+ "ta array<int> default array(1, null, 3), " +
+ "as array<struct<x: int>> " +
+ "default array(named_struct('x', 1), cast(null as struct<x: 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)), " +
+ "am array<map<string, int>> " +
+ "default array(map('a', 1, 'b', 2), cast(null as map<string,
int>)))")
+ f
+ }
+ }
+ }
+
+ test("vectorized readers fill existence defaults with NULL fields and wide
decimals") {
+ val wide = BigDecimal("99999999999999999999999999999999999999")
+ val filledColumns = Seq("s", "a", "n", "ta", "as", "d", "e", "f", "w",
"i", "l", "c")
+ val filledValues = Seq(Row(1, null), Row(1, Seq(1, null, 3)), Row(1,
null), Seq(1, null, 3),
+ Seq(Row(1), null), wide, BigDecimal("-9999999999999999999"),
BigDecimal("1.5"),
+ // checkAnswer does not convert decimals inside arrays to Scala
BigDecimal.
+ Row(wide, Seq(wide.bigDecimal)),
+ BigDecimal("1.25"), BigDecimal("999999999999999999"),
BigDecimal("123.5"))
+ // The map branch does not give each row its own entries yet, so filling
the NULL or the wide
+ // decimal of a default that contains a map would drop entries. Such
defaults keep the old
+ // error: the reader's NULL check for a NULL, and an ArithmeticException
for the decimal.
+ val nullError = "Cannot assign default column value"
+ val decimalError = "BigInteger out of long range"
+ val mapColumnErrors = Seq("m" -> nullError, "md" -> decimalError, "sm" ->
decimalError,
+ "sn" -> nullError, "sw" -> decimalError, "am" -> nullError)
+ Seq("parquet", "orc").foreach { format =>
+ withNullFieldAndWideDecimalDefaults(format) {
+ // The catalog table reads through the V1 file format. Its files, read
by path with the
+ // table schema (which carries the existence defaults) and no V1
fallback, read through
+ // the V2 reader factories.
+ val table =
spark.sessionState.catalog.getTableMetadata(TableIdentifier("t"))
+ Seq(true, false).foreach { useV1 =>
+ withSQLConf(SQLConf.USE_V1_SOURCE_LIST.key -> (if (useV1)
"parquet,orc" else "")) {
+ val clue = s"$format, useV1 = $useV1"
+ def read(columns: String): DataFrame = if (useV1) {
+ sql(s"select id, $columns from t")
+ } else {
+
spark.read.schema(table.schema).format(format).load(table.location.toString)
+ .selectExpr("id" +: columns.split(", ").toSeq: _*)
+ }
+ val df = read(filledColumns.mkString(", "))
+ val plan = df.queryExecution.executedPlan
+ assert(plan.exists(_.isInstanceOf[FileSourceScanExec]) == useV1,
clue)
+ assert(plan.exists(_.isInstanceOf[BatchScanExec]) == !useV1, clue)
Review Comment:
Done in 01b0470b. The test now takes the `FileSourceScanExec` or
`BatchScanExec` from the plan, asserts it is the expected one, and asserts
`supportsColumnar` on it for Parquet and ORC, V1 and V2.
##########
sql/core/src/main/java/org/apache/spark/sql/execution/vectorized/WritableColumnVector.java:
##########
@@ -816,28 +814,82 @@ public final int appendStruct(boolean isNull) {
return elementsAppended;
}
+ // Types whose NULL values must be appended with appendStruct(true), which
also appends a NULL
+ // to every child column.
+ private static boolean isStructLike(DataType type) {
+ return type instanceof StructType || type instanceof VariantType
+ || type instanceof CalendarIntervalType
+ || type instanceof AnyTimestampNanoType;
+ }
+
+ // Whether `value` contains a map at any depth.
+ private static boolean containsMap(Object value) {
+ if (value instanceof ArrayBasedMapData) {
+ return true;
+ }
+ Object[] elements = null;
+ if (value instanceof GenericArrayData arrayData) {
+ elements = arrayData.array();
+ } else if (value instanceof GenericInternalRow row) {
+ elements = row.values();
+ }
+ if (elements != null) {
+ for (Object element : elements) {
+ if (containsMap(element)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
/**
* Appends multiple copies of a Java Object to the vector using the
corresponding append* method
- * above.
+ * above. A NULL nested in a struct or array, and a decimal whose unscaled
value overflows a
+ * long, are appended too, unless the value contains a map: the map branch
does not give each
+ * row its own entries yet (SPARK-60940), so such a value keeps failing as
it did before, with
+ * Optional.empty() for the NULL and ArithmeticException for the decimal.
* @param length: The number of instances to append
* @param value value to append to the vector
* @return the number of values appended if the value maps to one of the
append* methods above,
* or Optional.empty() otherwise.
*/
public Optional<Integer> appendObjects(int length, Object value) {
+ return appendObjectsImpl(length, value, !containsMap(value));
+ }
+
+ private Optional<Integer> appendObjectsImpl(
+ int length, Object value, boolean fillNullsAndWideDecimals) {
+ if (value == null) {
Review Comment:
Added `SPARK-62034: appendObjects appends each row of a value by the
vector's data type` to `ColumnarBatchSuite` in 01b0470b. It runs for
`MemoryMode.ON_HEAP` and `MemoryMode.OFF_HEAP`, with an int, a wide decimal, a
binary value, a struct with a NULL field, an array with a NULL element, an
array of structs with a NULL element, a map with a NULL value and a map with a
wide-decimal value. It appends each one three times and compares every row with
the expected value through an `UnsafeProjection`. With the reader code reverted
to master, it fails with `ArithmeticException: BigInteger out of long range`.
##########
sql/core/src/test/scala/org/apache/spark/sql/sources/InsertSuite.scala:
##########
@@ -2063,6 +2065,106 @@ class InsertSuite extends DataSourceTest with
SharedSparkSession {
}
}
+ // 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 (s, a,
n, ta, as, d, e, f,
+ // w); compact and wide decimals that fit in a long (i, l, c); and defaults
that contain a map
+ // and also a NULL or an overflowing decimal, which keep the old error: in
the map itself (m,
+ // md), in a map in a struct (sm), next to a map in a struct (sn, sw), and
next to a map in an
+ // array (am).
+ 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") {
+ 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>)), " +
+ "ta array<int> default array(1, null, 3), " +
+ "as array<struct<x: int>> " +
+ "default array(named_struct('x', 1), cast(null as struct<x: 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)), " +
+ "am array<map<string, int>> " +
+ "default array(map('a', 1, 'b', 2), cast(null as map<string,
int>)))")
+ f
+ }
+ }
+ }
+
+ test("vectorized readers fill existence defaults with NULL fields and wide
decimals") {
+ val wide = BigDecimal("99999999999999999999999999999999999999")
+ val filledColumns = Seq("s", "a", "n", "ta", "as", "d", "e", "f", "w",
"i", "l", "c")
+ val filledValues = Seq(Row(1, null), Row(1, Seq(1, null, 3)), Row(1,
null), Seq(1, null, 3),
+ Seq(Row(1), null), wide, BigDecimal("-9999999999999999999"),
BigDecimal("1.5"),
+ // checkAnswer does not convert decimals inside arrays to Scala
BigDecimal.
+ Row(wide, Seq(wide.bigDecimal)),
+ BigDecimal("1.25"), BigDecimal("999999999999999999"),
BigDecimal("123.5"))
+ // The map branch does not give each row its own entries yet, so filling
the NULL or the wide
+ // decimal of a default that contains a map would drop entries. Such
defaults keep the old
+ // error: the reader's NULL check for a NULL, and an ArithmeticException
for the decimal.
+ val nullError = "Cannot assign default column value"
+ val decimalError = "BigInteger out of long range"
+ val mapColumnErrors = Seq("m" -> nullError, "md" -> decimalError, "sm" ->
decimalError,
+ "sn" -> nullError, "sw" -> decimalError, "am" -> nullError)
+ Seq("parquet", "orc").foreach { format =>
+ withNullFieldAndWideDecimalDefaults(format) {
+ // The catalog table reads through the V1 file format. Its files, read
by path with the
+ // table schema (which carries the existence defaults) and no V1
fallback, read through
+ // the V2 reader factories.
+ val table =
spark.sessionState.catalog.getTableMetadata(TableIdentifier("t"))
+ Seq(true, false).foreach { useV1 =>
+ withSQLConf(SQLConf.USE_V1_SOURCE_LIST.key -> (if (useV1)
"parquet,orc" else "")) {
+ val clue = s"$format, useV1 = $useV1"
+ def read(columns: String): DataFrame = if (useV1) {
+ sql(s"select id, $columns from t")
+ } else {
+
spark.read.schema(table.schema).format(format).load(table.location.toString)
+ .selectExpr("id" +: columns.split(", ").toSeq: _*)
+ }
+ val df = read(filledColumns.mkString(", "))
+ val plan = df.queryExecution.executedPlan
+ assert(plan.exists(_.isInstanceOf[FileSourceScanExec]) == useV1,
clue)
+ assert(plan.exists(_.isInstanceOf[BatchScanExec]) == !useV1, clue)
+ checkAnswer(df, Seq(Row(1 +: filledValues: _*), Row(2 +:
filledValues: _*)))
+ // Only V1: the V2 reader factories do not close a file whose
batch fails to
Review Comment:
Thanks for tracking that down. With every map default now reading correctly,
the test has no failing reads left, so the V1-only branch and its comment are
gone, and every column now reads through Parquet V2 and ORC V2. The ORC V2 leak
you found (no task-completion listener before `batchReader.initialize` in
`OrcPartitionReaderFactory.buildColumnarReader`) is pre-existing and still
worth a separate JIRA.
--
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]