-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-24119][SQL]Add interpreted execution to SortPrefix expression #21231
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
Closed
Closed
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
280b941
Checkpoint changes
bersprockets 4a810ae
Checkpoint commit
bersprockets d1a7e22
Checkpoint commit
bersprockets 4dbb0b7
Comment on testing oddity
bersprockets 227b6ac
Add boolean test for Sortprefix
bersprockets bde7b5e
Fix local timezone issue with tests
bersprockets 71007e5
Respond to review comments
bersprockets 1060a66
Small cleanup
bersprockets dc9a070
Fix test
bersprockets 2044aed
Split decimal handling function into two
bersprockets b81419f
Collapse function creation for Integral, Date, and Timestamp types
bersprockets 28f1b70
Remove extraneous empty line
bersprockets 590ba26
Move out of closure those variable evaluations that need to happen on…
bersprockets File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ import org.apache.spark.sql.catalyst.InternalRow | |
| import org.apache.spark.sql.catalyst.analysis.TypeCheckResult | ||
| import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, ExprCode} | ||
| import org.apache.spark.sql.types._ | ||
| import org.apache.spark.unsafe.types.UTF8String | ||
| import org.apache.spark.util.collection.unsafe.sort.PrefixComparators._ | ||
|
|
||
| abstract sealed class SortDirection { | ||
|
|
@@ -147,7 +148,40 @@ case class SortPrefix(child: SortOrder) extends UnaryExpression { | |
| (!child.isAscending && child.nullOrdering == NullsLast) | ||
| } | ||
|
|
||
| override def eval(input: InternalRow): Any = throw new UnsupportedOperationException | ||
| private lazy val calcPrefix: Any => Long = child.child.dataType match { | ||
| case BooleanType => (raw) => | ||
| if (raw.asInstanceOf[Boolean]) 1 else 0 | ||
| case DateType | TimestampType | _: IntegralType => (raw) => | ||
| raw.asInstanceOf[java.lang.Number].longValue() | ||
| case FloatType | DoubleType => (raw) => { | ||
| val dVal = raw.asInstanceOf[java.lang.Number].doubleValue() | ||
| DoublePrefixComparator.computePrefix(dVal) | ||
| } | ||
| case StringType => (raw) => | ||
| StringPrefixComparator.computePrefix(raw.asInstanceOf[UTF8String]) | ||
| case BinaryType => (raw) => | ||
| BinaryPrefixComparator.computePrefix(raw.asInstanceOf[Array[Byte]]) | ||
| case dt: DecimalType if dt.precision <= Decimal.MAX_LONG_DIGITS => | ||
| _.asInstanceOf[Decimal].toUnscaledLong | ||
| case dt: DecimalType if dt.precision - dt.scale <= Decimal.MAX_LONG_DIGITS => (raw) => { | ||
| val value = raw.asInstanceOf[Decimal] | ||
| val p = Decimal.MAX_LONG_DIGITS | ||
| val s = p - (dt.precision - dt.scale) | ||
|
Member
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. This is a nit but we can move L168 & L169 out of closure too. Like: case dt: DecimalType if dt.precision - dt.scale <= Decimal.MAX_LONG_DIGITS =>
val p = Decimal.MAX_LONG_DIGITS
val s = p - (dt.precision - dt.scale)
(raw) => {
val value = raw.asInstanceOf[Decimal]
if (value.changePrecision(p, s)) value.toUnscaledLong else Long.MinValue
} |
||
| if (value.changePrecision(p, s)) value.toUnscaledLong else Long.MinValue | ||
| } | ||
| case dt: DecimalType => (raw) => | ||
| DoublePrefixComparator.computePrefix(raw.asInstanceOf[Decimal].toDouble) | ||
| case _ => (Any) => 0L | ||
| } | ||
|
|
||
| override def eval(input: InternalRow): Any = { | ||
| val value = child.child.eval(input) | ||
| if (value == null) { | ||
| null | ||
| } else { | ||
| calcPrefix(value) | ||
| } | ||
|
Member
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. Is it ok to not have a default case? |
||
| } | ||
|
|
||
| override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { | ||
| val childCode = child.child.genCode(ctx) | ||
|
|
||
95 changes: 95 additions & 0 deletions
95
.../src/test/scala/org/apache/spark/sql/catalyst/expressions/SortOrderExpressionsSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* | ||
| * 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 java.sql.{Date, Timestamp} | ||
| import java.util.TimeZone | ||
|
|
||
| import org.apache.spark.SparkFunSuite | ||
| import org.apache.spark.sql.types._ | ||
| import org.apache.spark.unsafe.types.UTF8String | ||
| import org.apache.spark.util.collection.unsafe.sort.PrefixComparators._ | ||
|
|
||
| class SortOrderExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper { | ||
|
|
||
| test("SortPrefix") { | ||
| val b1 = Literal.create(false, BooleanType) | ||
| val b2 = Literal.create(true, BooleanType) | ||
| val i1 = Literal.create(20132983, IntegerType) | ||
| val i2 = Literal.create(-20132983, IntegerType) | ||
| val l1 = Literal.create(20132983, LongType) | ||
| val l2 = Literal.create(-20132983, LongType) | ||
| val millis = 1524954911000L; | ||
| // Explicitly choose a time zone, since Date objects can create different values depending on | ||
| // local time zone of the machine on which the test is running | ||
| val oldDefaultTZ = TimeZone.getDefault | ||
| val d1 = try { | ||
| TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles")) | ||
| Literal.create(new java.sql.Date(millis), DateType) | ||
| } finally { | ||
| TimeZone.setDefault(oldDefaultTZ) | ||
| } | ||
| val t1 = Literal.create(new Timestamp(millis), TimestampType) | ||
| val f1 = Literal.create(0.7788229f, FloatType) | ||
| val f2 = Literal.create(-0.7788229f, FloatType) | ||
| val db1 = Literal.create(0.7788229d, DoubleType) | ||
| val db2 = Literal.create(-0.7788229d, DoubleType) | ||
| val s1 = Literal.create("T", StringType) | ||
| val s2 = Literal.create("This is longer than 8 characters", StringType) | ||
| val bin1 = Literal.create(Array[Byte](12), BinaryType) | ||
| val bin2 = Literal.create(Array[Byte](12, 17, 99, 0, 0, 0, 2, 3, 0xf4.asInstanceOf[Byte]), | ||
| BinaryType) | ||
| val dec1 = Literal(Decimal(20132983L, 10, 2)) | ||
| val dec2 = Literal(Decimal(20132983L, 19, 2)) | ||
| val dec3 = Literal(Decimal(20132983L, 21, 2)) | ||
| val list1 = Literal(List(1, 2), ArrayType(IntegerType)) | ||
| val nullVal = Literal.create(null, IntegerType) | ||
|
|
||
| checkEvaluation(SortPrefix(SortOrder(b1, Ascending)), 0L) | ||
| checkEvaluation(SortPrefix(SortOrder(b2, Ascending)), 1L) | ||
| checkEvaluation(SortPrefix(SortOrder(i1, Ascending)), 20132983L) | ||
| checkEvaluation(SortPrefix(SortOrder(i2, Ascending)), -20132983L) | ||
| checkEvaluation(SortPrefix(SortOrder(l1, Ascending)), 20132983L) | ||
| checkEvaluation(SortPrefix(SortOrder(l2, Ascending)), -20132983L) | ||
| // For some reason, the Literal.create code gives us the number of days since the epoch | ||
| checkEvaluation(SortPrefix(SortOrder(d1, Ascending)), 17649L) | ||
| checkEvaluation(SortPrefix(SortOrder(t1, Ascending)), millis * 1000) | ||
| checkEvaluation(SortPrefix(SortOrder(f1, Ascending)), | ||
| DoublePrefixComparator.computePrefix(f1.value.asInstanceOf[Float].toDouble)) | ||
| checkEvaluation(SortPrefix(SortOrder(f2, Ascending)), | ||
| DoublePrefixComparator.computePrefix(f2.value.asInstanceOf[Float].toDouble)) | ||
| checkEvaluation(SortPrefix(SortOrder(db1, Ascending)), | ||
| DoublePrefixComparator.computePrefix(db1.value.asInstanceOf[Double])) | ||
| checkEvaluation(SortPrefix(SortOrder(db2, Ascending)), | ||
| DoublePrefixComparator.computePrefix(db2.value.asInstanceOf[Double])) | ||
| checkEvaluation(SortPrefix(SortOrder(s1, Ascending)), | ||
| StringPrefixComparator.computePrefix(s1.value.asInstanceOf[UTF8String])) | ||
| checkEvaluation(SortPrefix(SortOrder(s2, Ascending)), | ||
| StringPrefixComparator.computePrefix(s2.value.asInstanceOf[UTF8String])) | ||
| checkEvaluation(SortPrefix(SortOrder(bin1, Ascending)), | ||
| BinaryPrefixComparator.computePrefix(bin1.value.asInstanceOf[Array[Byte]])) | ||
| checkEvaluation(SortPrefix(SortOrder(bin2, Ascending)), | ||
| BinaryPrefixComparator.computePrefix(bin2.value.asInstanceOf[Array[Byte]])) | ||
| checkEvaluation(SortPrefix(SortOrder(dec1, Ascending)), 20132983L) | ||
| checkEvaluation(SortPrefix(SortOrder(dec2, Ascending)), 2013298L) | ||
| checkEvaluation(SortPrefix(SortOrder(dec3, Ascending)), | ||
| DoublePrefixComparator.computePrefix(201329.83d)) | ||
| checkEvaluation(SortPrefix(SortOrder(list1, Ascending)), 0L) | ||
| checkEvaluation(SortPrefix(SortOrder(nullVal, Ascending)), null) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back 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.
We could do the same patten matching in
doGenCode.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'm a little apprehensive about changing existing and properly functioning code (in doGenCode), even though it would make it slightly more readable. If you think I should, though, I will.