c21 opened a new pull request #31958:
URL: https://github.com/apache/spark/pull/31958


   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: 
https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: 
https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., 
'[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a 
faster review.
     7. If you want to add a new configuration, please read the guideline first 
for naming configurations in
        
'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section 
is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster 
reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class 
hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other 
DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   This PR is to support nested column type in Spark ORC vectorized reader. 
Currently ORC vectorized reader [does not support nested column type (struct, 
array and 
map)](https://github.com/apache/spark/blob/master/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/orc/OrcFileFormat.scala#L138).
 We implemented nested column vectorized reader for FB-ORC in our internal fork 
of Spark. We are seeing performance improvement compared to non-vectorized 
reader when reading nested columns. In addition, this can also help improve the 
non-nested column performance when reading non-nested and nested columns 
together in one query.
   
   Before this PR:
   
   * `OrcColumnVector` is the implementation class for Spark's `ColumnVector` 
to wrap Hive's/ORC's `ColumnVector` to read `AtomicType` data.
   
   After this PR:
   
   * `OrcColumnVector` is an abstract class to keep interface being shared 
between multiple implementation class of orc column vectors, namely 
`OrcAtomicColumnVector` (for `AtomicType`), `OrcArrayColumnVector` (for 
`ArrayType`), `OrcMapColumnVector` (for `MapType`), `OrcStructColumnVector` 
(for `StructType`). So the original logic to read `AtomicType` data is moved 
from `OrcColumnVector` to `OrcAtomicColumnVector`. The abstract class of 
`OrcColumnVector` is needed here because of supporting nested column (i.e. 
nested column vectors).
   * A utility method `OrcColumnVectorUtils.toOrcColumnVector` is added to 
create Spark's `OrcColumnVector` from Hive's/ORC's `ColumnVector`.
   * A new user-facing config 
`spark.sql.orc.enableNestedColumnVectorizedReader` is added to control 
enabling/disabling vectorized reader for nested columns. The default value is 
true (i.e. enabling by default). For certain tables having deep nested columns, 
vectorized reader might take too much memory for each sub-column vectors, 
compared to non-vectorized reader. So providing a config here to work around 
OOM for query reading wide and deep nested columns if any.
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   Improve query performance when reading nested columns from ORC file format.
   Tested with locally adding a small benchmark in `OrcReadBenchmark.scala`. 
Seeing more than 1x run time improvement.
   
   ```
   Running benchmark: SQL Nested Column Scan
     Running case: Native ORC MR
     Stopped after 2 iterations, 37850 ms
     Running case: Native ORC Vectorized (Enabled Nested Column)
     Stopped after 2 iterations, 15892 ms
     Running case: Native ORC Vectorized (Disabled Nested Column)
     Stopped after 2 iterations, 37954 ms
     Running case: Hive built-in ORC
     Stopped after 2 iterations, 35118 ms
   
   Java HotSpot(TM) 64-Bit Server VM 1.8.0_181-b13 on Mac OS X 10.15.7
   Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
   SQL Nested Column Scan:                         Best Time(ms)   Avg Time(ms) 
  Stdev(ms)    Rate(M/s)   Per Row(ns)   Relative
   
------------------------------------------------------------------------------------------------------------------------------
   Native ORC MR                                           18706          18925 
        310          0.1       17839.6       1.0X
   Native ORC Vectorized (Enabled Nested Column)            7625           7946 
        455          0.1        7271.6       2.5X
   Native ORC Vectorized (Disabled Nested Column)          18415          18977 
        796          0.1       17561.5       1.0X
   Hive built-in ORC                                       17469          17559 
        127          0.1       16660.1       1.1X
   ```
   
   Benchmark:
   
   ```
   nestedColumnScanBenchmark(1024 * 1024)
   def nestedColumnScanBenchmark(values: Int): Unit = {
       val benchmark = new Benchmark(s"SQL Nested Column Scan", values, output 
= output)
   
       withTempPath { dir =>
         withTempTable("t1", "nativeOrcTable", "hiveOrcTable") {
           import spark.implicits._
           spark.range(values).map(_ => Random.nextLong).map { x =>
             val arrayOfStructColumn = (0 until 5).map(i => (x + i, s"$x" * 5))
             val mapOfStructColumn = Map(
               s"$x" -> (x * 0.1, (x, s"$x" * 100)),
               (s"$x" * 2) -> (x * 0.2, (x, s"$x" * 200)),
               (s"$x" * 3) -> (x * 0.3, (x, s"$x" * 300)))
             (arrayOfStructColumn, mapOfStructColumn)
           }.toDF("col1", "col2")
             .createOrReplaceTempView("t1")
   
           prepareTable(dir, spark.sql(s"SELECT * FROM t1"))
   
           benchmark.addCase("Native ORC MR") { _ =>
             withSQLConf(SQLConf.ORC_VECTORIZED_READER_ENABLED.key -> "false") {
               spark.sql("SELECT SUM(SIZE(col1)), SUM(SIZE(col2)) FROM 
nativeOrcTable").noop()
             }
           }
   
           benchmark.addCase("Native ORC Vectorized (Enabled Nested Column)") { 
_ =>
             spark.sql("SELECT SUM(SIZE(col1)), SUM(SIZE(col2)) FROM 
nativeOrcTable").noop()
           }
   
           benchmark.addCase("Native ORC Vectorized (Disabled Nested Column)") 
{ _ =>
             
withSQLConf(SQLConf.ORC_VECTORIZED_READER_NESTED_COLUMN_ENABLED.key -> "false") 
{
               spark.sql("SELECT SUM(SIZE(col1)), SUM(SIZE(col2)) FROM 
nativeOrcTable").noop()
             }
           }
   
           benchmark.addCase("Hive built-in ORC") { _ =>
             spark.sql("SELECT SUM(SIZE(col1)), SUM(SIZE(col2)) FROM 
hiveOrcTable").noop()
           }
   
           benchmark.run()
         }
       }
     }
   ```
   
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as 
the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes 
- provide the console output, description and/or an example to show the 
behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to 
the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some 
test cases that check the changes thoroughly including negative and positive 
cases if possible.
   If it was tested in a way different from regular unit tests, please clarify 
how you tested step by step, ideally copy and paste-able, so that other 
reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why 
it was difficult to add.
   -->
   


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

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to