Repository navigation
Closed
Milestone
Description
In Spark 3, projecting 'data_file' fields in any metadata tables that has them (like 'entries'), leads to following NPE:
org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 5.0 failed 1 times, most recent failure: Lost task 0.0 in stage 5.0 (TID 5) (10.0.0.45 executor driver): java.lang.IllegalStateException: Unknown type for int field. Type name: java.lang.String
at org.apache.iceberg.spark.source.StructInternalRow.getInt(StructInternalRow.java:131)
at org.apache.spark.sql.catalyst.expressions.GeneratedClass$SpecificUnsafeProjection.writeFields_0_0$(Unknown Source)
at org.apache.spark.sql.catalyst.expressions.GeneratedClass$SpecificUnsafeProjection.apply(Unknown Source)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.apache.iceberg.common.DynMethods$UnboundMethod.invokeChecked(DynMethods.java:65)
at org.apache.iceberg.common.DynMethods$UnboundMethod.invoke(DynMethods.java:77)
at org.apache.iceberg.common.DynMethods$BoundMethod.invoke(DynMethods.java:180)
at org.apache.iceberg.spark.source.RowDataReader.lambda$newDataIterable$3(RowDataReader.java:193)
at org.apache.iceberg.io.CloseableIterable$4$1.next(CloseableIterable.java:113)
at org.apache.iceberg.io.FilterIterator.advance(FilterIterator.java:66)
at org.apache.iceberg.io.FilterIterator.hasNext(FilterIterator.java:50)
at org.apache.iceberg.spark.source.BaseDataReader.next(BaseDataReader.java:87)
at org.apache.spark.sql.execution.datasources.v2.PartitionIterator.hasNext(DataSourceRDD.scala:79)
at org.apache.spark.sql.execution.datasources.v2.MetricsIterator.hasNext(DataSourceRDD.scala:112)
at org.apache.spark.InterruptibleIterator.hasNext(InterruptibleIterator.scala:37)
at scala.collection.Iterator$$anon$10.hasNext(Iterator.scala:460)
at org.apache.spark.sql.catalyst.expressions.GeneratedClass$GeneratedIteratorForCodegenStage1.processNext(Unknown Source)
at org.apache.spark.sql.execution.BufferedRowIterator.hasNext(BufferedRowIterator.java:43)
at org.apache.spark.sql.execution.WholeStageCodegenExec$$anon$1.hasNext(WholeStageCodegenExec.scala:755)
at org.apache.spark.sql.execution.SparkPlan.$anonfun$getByteArrayRdd$1(SparkPlan.scala:345)
at org.apache.spark.rdd.RDD.$anonfun$mapPartitionsInternal$2(RDD.scala:898)
at org.apache.spark.rdd.RDD.$anonfun$mapPartitionsInternal$2$adapted(RDD.scala:898)
at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:52)
at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:373)
at org.apache.spark.rdd.RDD.iterator(RDD.scala:337)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:90)
at org.apache.spark.scheduler.Task.run(Task.scala:131)
at org.apache.spark.executor.Executor$TaskRunner.$anonfun$run$3(Executor.scala:497)
at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1439)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:500)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
Metadata
Metadata
Assignees
Labels
No labels
Activity
szehon-ho commentedon Jul 5, 2021
Adding a reproduction test (can run in spark3-extensions test case, for example):
Side note: I use "data_file" field to reproduce it, if I do not then I hit other error: #1378 and #1735 (same underlying error)
There are two tests, it shows that even a simple aggregation now fails even with the workaround of using "data_file".
szehon-ho commentedon Jul 5, 2021
Preliminary analysis: The BaseFile class uses some pruning logic, keeping a mapping called 'fromProjectionPos' that maps indexes in a projection (a subset of its columns) to real indexes in its column list.
Ie, if the projected array has three elements which are fields 1,2, and 10 of the original schema respectively, fromProjectionPos becomes {1, 2, 10}.
This is usually fine but the Spark generated code in Spark 3 uses the original index to get all the fields (It's entry point is StructInternalRow: https://github.com/apache/iceberg/blob/master/spark/src/main/java/org/apache/iceberg/spark/source/StructInternalRow.java#L124. (here 'struct' is the BaseDataFile with the pruning mapping, BUT spark initializes 'type' as the original (unpruned) list of fields).
The calll getInt(0) expects an int as per the original schema at index 0, but due to the mapping we return value at index 1 which is actually a string and hence the error.
szehon-ho commentedon Jul 5, 2021
@rdblue @RussellSpitzer fyi, if you have any ideas about this.
My first thought is in this method: https://github.com/apache/iceberg/blob/master/spark/src/main/java/org/apache/iceberg/spark/source/StructInternalRow.java#L78. , if the struct's schema does not match the type, then set a flag on this struct to skip projection.
We would need to add the method (StructLike.useProjection(boolean)) to the interface, and have BaseFile implement it, so maybe not the best approach. I don't have the most context on this area, so maybe the idea is not good.
Eliminate projection to Spark query to avoid apache#2783
szehon-ho commentedon Jul 6, 2021
One more note: this issue happens only after upgrade to Spark 3.1.
Eliminate projection to Spark query to avoid apache#2783
Eliminate projection to Spark query to avoid apache#2783
RussellSpitzer commentedon Jul 26, 2021
So I think I tracked this down, the basic issue is that Spark 3.1 correctly prunes nested structs and Spark 3.0 does not. You may wonder, if Spark3.1 correctly prunes nested structs why is this an issue?
The issue is that we end up reading only 2 fields out of our metadata tables and correctly present them. But our create UnsafeProjection code assumes that if a nested struct is read, then all fields are read so we end up building a projection which requires all columns, rather than just the ones we have actually extracted. This means we build a broken projection.
See RowDataReader projection, which only does a top-level pruning.
iceberg/spark/src/main/java/org/apache/iceberg/spark/source/RowDataReader.java
Lines 208 to 211 in a79de57
If we never prune columns out of the struct this is fine, if we do then we have a problem.
RussellSpitzer commentedon Jul 26, 2021
@karuppayya Has a temporary workaround by setting
RussellSpitzer commentedon Jul 27, 2021
Pr Posted ^ For those interested
Eliminate projection to Spark query to avoid apache#2783