-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-25498][SQL] InterpretedMutableProjection should handle UnsafeRow #22512
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
3faf314
ef20325
09767a0
df07fc4
388213f
243fae3
3553d91
95411c8
4cdc504
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 |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * 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.catalyst.expressions | ||
|
|
||
| import org.apache.spark.SparkFunSuite | ||
| import org.apache.spark.sql.Row | ||
| import org.apache.spark.sql.catalyst.{CatalystTypeConverters, InternalRow} | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.types._ | ||
| import org.apache.spark.unsafe.types.CalendarInterval | ||
|
|
||
| class MutableProjectionSuite extends SparkFunSuite with ExpressionEvalHelper { | ||
|
|
||
| val fixedLengthTypes = Array[DataType]( | ||
| BooleanType, ByteType, ShortType, IntegerType, LongType, FloatType, DoubleType, | ||
| DateType, TimestampType) | ||
|
|
||
| val variableLengthTypes = Array( | ||
| StringType, DecimalType.defaultConcreteType, CalendarIntervalType, BinaryType, | ||
| ArrayType(StringType), MapType(IntegerType, StringType), | ||
| StructType.fromDDL("a INT, b STRING"), ObjectType(classOf[java.lang.Integer])) | ||
|
|
||
| def createMutableProjection(dataTypes: Array[DataType]): MutableProjection = { | ||
| MutableProjection.create(dataTypes.zipWithIndex.map(x => BoundReference(x._2, x._1, true))) | ||
| } | ||
|
|
||
| testBothCodegenAndInterpreted("fixed-length types") { | ||
| val inputRow = InternalRow.fromSeq(Seq(true, 3.toByte, 15.toShort, -83, 129L, 1.0f, 5.0, 1, 2L)) | ||
| val proj = createMutableProjection(fixedLengthTypes) | ||
| assert(proj(inputRow) === inputRow) | ||
| } | ||
|
|
||
| testBothCodegenAndInterpreted("unsafe buffer") { | ||
| val inputRow = InternalRow.fromSeq(Seq(false, 1.toByte, 9.toShort, -18, 53L, 3.2f, 7.8, 4, 9L)) | ||
| val numBytes = UnsafeRow.calculateBitSetWidthInBytes(fixedLengthTypes.length) | ||
| val unsafeBuffer = UnsafeRow.createFromByteArray(numBytes, fixedLengthTypes.length) | ||
| val proj = createMutableProjection(fixedLengthTypes) | ||
| val projUnsafeRow = proj.target(unsafeBuffer)(inputRow) | ||
| assert(FromUnsafeProjection.apply(fixedLengthTypes)(projUnsafeRow) === inputRow) | ||
| } | ||
|
|
||
| testBothCodegenAndInterpreted("variable-length types") { | ||
| val proj = createMutableProjection(variableLengthTypes) | ||
|
||
| val scalaValues = Seq("abc", BigDecimal(10), CalendarInterval.fromString("interval 1 day"), | ||
| Array[Byte](1, 2), Array("123", "456"), Map(1 -> "a", 2 -> "b"), Row(1, "a"), | ||
| new java.lang.Integer(5)) | ||
| val inputRow = InternalRow.fromSeq(scalaValues.zip(variableLengthTypes).map { | ||
| case (v, dataType) => CatalystTypeConverters.createToCatalystConverter(dataType)(v) | ||
| }) | ||
| val projRow = proj(inputRow) | ||
| variableLengthTypes.zipWithIndex.foreach { case (dataType, index) => | ||
| val toScala = CatalystTypeConverters.createToScalaConverter(dataType) | ||
| assert(toScala(projRow.get(index, dataType)) === toScala(inputRow.get(index, dataType))) | ||
| } | ||
| } | ||
|
|
||
| test("unsupported types for unsafe buffer") { | ||
| withSQLConf(SQLConf.CODEGEN_FACTORY_MODE.key -> CodegenObjectFactoryMode.NO_CODEGEN.toString) { | ||
| val proj = createMutableProjection(Array(StringType)) | ||
| val errMsg = intercept[IllegalArgumentException] { | ||
| proj.target(new UnsafeRow(1)) | ||
| }.getMessage | ||
| assert(errMsg.contains("MutableProjection cannot use UnsafeRow for output data types:")) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,11 +22,13 @@ import java.util.{Locale, TimeZone} | |
|
|
||
| import scala.util.control.NonFatal | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.CodegenObjectFactoryMode._ | ||
| import org.apache.spark.sql.catalyst.planning.PhysicalOperation | ||
| import org.apache.spark.sql.catalyst.plans.logical._ | ||
| import org.apache.spark.sql.catalyst.rules.RuleExecutor | ||
| import org.apache.spark.sql.catalyst.util.{fileToString, stringToFile} | ||
| import org.apache.spark.sql.execution.command.{DescribeColumnCommand, DescribeTableCommand} | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.test.SharedSQLContext | ||
| import org.apache.spark.sql.types.StructType | ||
|
|
||
|
|
@@ -140,6 +142,12 @@ class SQLQueryTestSuite extends QueryTest with SharedSQLContext { | |
| val input = fileToString(new File(testCase.inputFile)) | ||
|
|
||
| val (comments, code) = input.split("\n").partition(_.startsWith("--")) | ||
|
|
||
| // Runs all the tests on both codegen-only and interpreter modes | ||
| val codegenConfigSets = Array(CODEGEN_ONLY, NO_CODEGEN).map { | ||
| case codegenFactoryMode => | ||
| Array(SQLConf.CODEGEN_FACTORY_MODE.key -> codegenFactoryMode.toString) | ||
| } | ||
| val configSets = { | ||
| val configLines = comments.filter(_.startsWith("--SET")).map(_.substring(5)) | ||
| val configs = configLines.map(_.split(",").map { confAndValue => | ||
|
|
@@ -148,12 +156,25 @@ class SQLQueryTestSuite extends QueryTest with SharedSQLContext { | |
| }) | ||
| // When we are regenerating the golden files we don't need to run all the configs as they | ||
| // all need to return the same result | ||
| if (regenerateGoldenFiles && configs.nonEmpty) { | ||
| configs.take(1) | ||
| if (regenerateGoldenFiles) { | ||
| if (configs.nonEmpty) { | ||
| configs.take(1) | ||
| } else { | ||
| Array.empty[Array[(String, String)]] | ||
|
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. nit: since configs don't matter when generating result, I think we can just return empty configs here. We can clean it up in a followup PR.
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. ok |
||
| } | ||
| } else { | ||
| configs | ||
| if (configs.nonEmpty) { | ||
| codegenConfigSets.flatMap { codegenConfig => | ||
| configs.map { config => | ||
| config ++ codegenConfig | ||
| } | ||
| } | ||
| } else { | ||
| codegenConfigSets | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // List of SQL queries to run | ||
| // note: this is not a robust way to split queries using semicolon, but works for now. | ||
| val queries = code.mkString("\n").split("(?<=[^\\\\]);").map(_.trim).filter(_ != "").toSeq | ||
|
|
||
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.
Since
fieldWritersis accessed via index, we should useIndexedSeqorArrayexplicitly?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.
ah, sounds reasonable. I'll update later.
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.
fixed in 95411c8