|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql.catalyst.expressions |
| 19 | + |
| 20 | +import java.sql.{Date, Timestamp} |
| 21 | +import java.util.TimeZone |
| 22 | + |
| 23 | +import org.apache.spark.SparkFunSuite |
| 24 | +import org.apache.spark.sql.types._ |
| 25 | +import org.apache.spark.unsafe.types.UTF8String |
| 26 | +import org.apache.spark.util.collection.unsafe.sort.PrefixComparators._ |
| 27 | + |
| 28 | +class SortOrderExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper { |
| 29 | + |
| 30 | + test("SortPrefix") { |
| 31 | + val b1 = Literal.create(false, BooleanType) |
| 32 | + val b2 = Literal.create(true, BooleanType) |
| 33 | + val i1 = Literal.create(20132983, IntegerType) |
| 34 | + val i2 = Literal.create(-20132983, IntegerType) |
| 35 | + val l1 = Literal.create(20132983, LongType) |
| 36 | + val l2 = Literal.create(-20132983, LongType) |
| 37 | + val millis = 1524954911000L; |
| 38 | + // Explicitly choose a time zone, since Date objects can create different values depending on |
| 39 | + // local time zone of the machine on which the test is running |
| 40 | + val oldDefaultTZ = TimeZone.getDefault |
| 41 | + val d1 = try { |
| 42 | + TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles")) |
| 43 | + Literal.create(new java.sql.Date(millis), DateType) |
| 44 | + } finally { |
| 45 | + TimeZone.setDefault(oldDefaultTZ) |
| 46 | + } |
| 47 | + val t1 = Literal.create(new Timestamp(millis), TimestampType) |
| 48 | + val f1 = Literal.create(0.7788229f, FloatType) |
| 49 | + val f2 = Literal.create(-0.7788229f, FloatType) |
| 50 | + val db1 = Literal.create(0.7788229d, DoubleType) |
| 51 | + val db2 = Literal.create(-0.7788229d, DoubleType) |
| 52 | + val s1 = Literal.create("T", StringType) |
| 53 | + val s2 = Literal.create("This is longer than 8 characters", StringType) |
| 54 | + val bin1 = Literal.create(Array[Byte](12), BinaryType) |
| 55 | + val bin2 = Literal.create(Array[Byte](12, 17, 99, 0, 0, 0, 2, 3, 0xf4.asInstanceOf[Byte]), |
| 56 | + BinaryType) |
| 57 | + val dec1 = Literal(Decimal(20132983L, 10, 2)) |
| 58 | + val dec2 = Literal(Decimal(20132983L, 19, 2)) |
| 59 | + val dec3 = Literal(Decimal(20132983L, 21, 2)) |
| 60 | + val list1 = Literal(List(1, 2), ArrayType(IntegerType)) |
| 61 | + val nullVal = Literal.create(null, IntegerType) |
| 62 | + |
| 63 | + checkEvaluation(SortPrefix(SortOrder(b1, Ascending)), 0L) |
| 64 | + checkEvaluation(SortPrefix(SortOrder(b2, Ascending)), 1L) |
| 65 | + checkEvaluation(SortPrefix(SortOrder(i1, Ascending)), 20132983L) |
| 66 | + checkEvaluation(SortPrefix(SortOrder(i2, Ascending)), -20132983L) |
| 67 | + checkEvaluation(SortPrefix(SortOrder(l1, Ascending)), 20132983L) |
| 68 | + checkEvaluation(SortPrefix(SortOrder(l2, Ascending)), -20132983L) |
| 69 | + // For some reason, the Literal.create code gives us the number of days since the epoch |
| 70 | + checkEvaluation(SortPrefix(SortOrder(d1, Ascending)), 17649L) |
| 71 | + checkEvaluation(SortPrefix(SortOrder(t1, Ascending)), millis * 1000) |
| 72 | + checkEvaluation(SortPrefix(SortOrder(f1, Ascending)), |
| 73 | + DoublePrefixComparator.computePrefix(f1.value.asInstanceOf[Float].toDouble)) |
| 74 | + checkEvaluation(SortPrefix(SortOrder(f2, Ascending)), |
| 75 | + DoublePrefixComparator.computePrefix(f2.value.asInstanceOf[Float].toDouble)) |
| 76 | + checkEvaluation(SortPrefix(SortOrder(db1, Ascending)), |
| 77 | + DoublePrefixComparator.computePrefix(db1.value.asInstanceOf[Double])) |
| 78 | + checkEvaluation(SortPrefix(SortOrder(db2, Ascending)), |
| 79 | + DoublePrefixComparator.computePrefix(db2.value.asInstanceOf[Double])) |
| 80 | + checkEvaluation(SortPrefix(SortOrder(s1, Ascending)), |
| 81 | + StringPrefixComparator.computePrefix(s1.value.asInstanceOf[UTF8String])) |
| 82 | + checkEvaluation(SortPrefix(SortOrder(s2, Ascending)), |
| 83 | + StringPrefixComparator.computePrefix(s2.value.asInstanceOf[UTF8String])) |
| 84 | + checkEvaluation(SortPrefix(SortOrder(bin1, Ascending)), |
| 85 | + BinaryPrefixComparator.computePrefix(bin1.value.asInstanceOf[Array[Byte]])) |
| 86 | + checkEvaluation(SortPrefix(SortOrder(bin2, Ascending)), |
| 87 | + BinaryPrefixComparator.computePrefix(bin2.value.asInstanceOf[Array[Byte]])) |
| 88 | + checkEvaluation(SortPrefix(SortOrder(dec1, Ascending)), 20132983L) |
| 89 | + checkEvaluation(SortPrefix(SortOrder(dec2, Ascending)), 2013298L) |
| 90 | + checkEvaluation(SortPrefix(SortOrder(dec3, Ascending)), |
| 91 | + DoublePrefixComparator.computePrefix(201329.83d)) |
| 92 | + checkEvaluation(SortPrefix(SortOrder(list1, Ascending)), 0L) |
| 93 | + checkEvaluation(SortPrefix(SortOrder(nullVal, Ascending)), null) |
| 94 | + } |
| 95 | +} |
0 commit comments