-
Notifications
You must be signed in to change notification settings - Fork 29k
[SQL][SPARK-17490] Optimize SerializeFromObject() for a primitive array #15044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
170e7de
327b7dd
e98cb1e
3d7ea2c
5d5ccd6
edfbce3
8b79146
b5473e3
c5378f9
d9e5b4f
d507cfc
c217395
4c679b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -441,6 +441,22 @@ object ScalaReflection extends ScalaReflection { | |
| val newPath = s"""- array element class: "$clsName"""" +: walkedTypePath | ||
| MapObjects(serializerFor(_, elementType, newPath), input, dt) | ||
|
|
||
| case dt @ (BooleanType | ByteType | ShortType | IntegerType | LongType | | ||
| FloatType | DoubleType) => | ||
| val cls = input.dataType.asInstanceOf[ObjectType].cls | ||
| if (cls.isArray && cls.getComponentType.isPrimitive) { | ||
| StaticInvoke( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can simplify it
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks, good catch. |
||
| classOf[UnsafeArrayData], | ||
| ArrayType(dt, false), | ||
| "fromPrimitiveArray", | ||
| input :: Nil) | ||
| } else { | ||
| NewInstance( | ||
| classOf[GenericArrayData], | ||
| input :: Nil, | ||
| dataType = ArrayType(dt, schemaFor(elementType).nullable)) | ||
| } | ||
|
|
||
| case dt => | ||
| NewInstance( | ||
| classOf[GenericArrayData], | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,7 @@ import scala.reflect.ClassTag | |
| import org.apache.spark.SparkException | ||
| import org.apache.spark.sql.Row | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.util.{ArrayBasedMapData, DateTimeUtils, GenericArrayData} | ||
| import org.apache.spark.sql.catalyst.util.{ArrayBasedMapData, ArrayData, DateTimeUtils, GenericArrayData} | ||
| import org.apache.spark.sql.catalyst.ScalaReflection | ||
| import org.apache.spark.sql.catalyst.analysis.GetColumnByOrdinal | ||
| import org.apache.spark.sql.catalyst.expressions.objects._ | ||
|
|
@@ -119,18 +119,19 @@ object RowEncoder { | |
| "fromString", | ||
| inputObject :: Nil) | ||
|
|
||
| case t @ ArrayType(et, _) => et match { | ||
| case BooleanType | ByteType | ShortType | IntegerType | LongType | FloatType | DoubleType => | ||
| // TODO: validate input type for primitive array. | ||
| NewInstance( | ||
| classOf[GenericArrayData], | ||
| inputObject :: Nil, | ||
| dataType = t) | ||
| case _ => MapObjects( | ||
| element => serializerFor(ValidateExternalType(element, et), et), | ||
| inputObject, | ||
| ObjectType(classOf[Object])) | ||
| } | ||
| case t @ ArrayType(et, cn) => | ||
| et match { | ||
| case BooleanType | ByteType | ShortType | IntegerType | LongType | FloatType | DoubleType => | ||
| StaticInvoke( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we do the same thing here? i.e. special handling primitive array. I know we don't have the class information here, bu we can do it in the runtime: Then we just use
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea. It worked well. |
||
| classOf[ArrayData], | ||
| t, | ||
| "toArrayData", | ||
| inputObject :: Nil) | ||
| case _ => MapObjects( | ||
| element => serializerFor(ValidateExternalType(element, et), et), | ||
| inputObject, | ||
| ObjectType(classOf[Object])) | ||
| } | ||
|
|
||
| case t @ MapType(kt, vt, valueNullable) => | ||
| val keys = | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.execution.benchmark | ||
|
|
||
| import scala.concurrent.duration._ | ||
|
|
||
| import org.apache.spark.SparkConf | ||
| import org.apache.spark.sql.catalyst.util._ | ||
| import org.apache.spark.util.Benchmark | ||
|
|
||
| /** | ||
| * Benchmark [[PrimitiveArray]] for DataFrame and Dataset program using primitive array | ||
| * To run this: | ||
| * 1. replace ignore(...) with test(...) | ||
| * 2. build/sbt "sql/test-only *benchmark.PrimitiveArrayBenchmark" | ||
| * | ||
| * Benchmarks in this file are skipped in normal builds. | ||
| */ | ||
| class PrimitiveArrayBenchmark extends BenchmarkBase { | ||
|
|
||
| def writeDatasetArray(iters: Int): Unit = { | ||
| import sparkSession.implicits._ | ||
|
|
||
| val count = 1024 * 1024 * 2 | ||
|
|
||
| val sc = sparkSession.sparkContext | ||
| val primitiveIntArray = Array.fill[Int](count)(65535) | ||
| val dsInt = sc.parallelize(Seq(primitiveIntArray), 1).toDS | ||
| dsInt.count // force to build dataset | ||
| val intArray = { i: Int => | ||
| var n = 0 | ||
| var len = 0 | ||
| while (n < iters) { | ||
| len += dsInt.map(e => e).queryExecution.toRdd.collect.length | ||
| n += 1 | ||
| } | ||
| } | ||
| val primitiveDoubleArray = Array.fill[Double](count)(65535.0) | ||
| val dsDouble = sc.parallelize(Seq(primitiveDoubleArray), 1).toDS | ||
| dsDouble.count // force to build dataset | ||
| val doubleArray = { i: Int => | ||
| var n = 0 | ||
| var len = 0 | ||
| while (n < iters) { | ||
| len += dsDouble.map(e => e).queryExecution.toRdd.collect.length | ||
| n += 1 | ||
| } | ||
| } | ||
|
|
||
| val benchmark = new Benchmark("Write an array in Dataset", count * iters) | ||
| benchmark.addCase("Int ")(intArray) | ||
| benchmark.addCase("Double")(doubleArray) | ||
| benchmark.run | ||
| /* | ||
| OpenJDK 64-Bit Server VM 1.8.0_91-b14 on Linux 4.4.11-200.fc22.x86_64 | ||
| Intel Xeon E3-12xx v2 (Ivy Bridge) | ||
| Write an array in Dataset: Best/Avg Time(ms) Rate(M/s) Per Row(ns) Relative | ||
| ------------------------------------------------------------------------------------------------ | ||
| Int 352 / 401 23.8 42.0 1.0X | ||
| Double 821 / 885 10.2 97.9 0.4X | ||
| */ | ||
| } | ||
|
|
||
| ignore("Write an array in Dataset") { | ||
| writeDatasetArray(4) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this cover all the code paths from an object to a Spark SQL internal type? For instance
RowEncoder.serializeFor? Also take a look atCatalystTypeConverters.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am afraid that I cannot understand your question correctly.
This case covers only cases that we are interested in (e.g. generate
UnsafeArrayData). Other cases are covered by here.Is this an answer to you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean that there a few more places in which we can apply this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your clarification. Let me check them.