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
18package org.apache.spark.sql.catalyst.expressions
19
20import org.apache.spark.SparkFunSuite
21import org.apache.spark.sql.types.{Decimal, DecimalType, LongType}
22
23class DecimalExpressionSuite extends SparkFunSuite with ExpressionEvalHelper {
24
25  test("UnscaledValue") {
26    val d1 = Decimal("10.1")
27    checkEvaluation(UnscaledValue(Literal(d1)), 101L)
28    val d2 = Decimal(101, 3, 1)
29    checkEvaluation(UnscaledValue(Literal(d2)), 101L)
30    checkEvaluation(UnscaledValue(Literal.create(null, DecimalType(2, 1))), null)
31  }
32
33  test("MakeDecimal") {
34    checkEvaluation(MakeDecimal(Literal(101L), 3, 1), Decimal("10.1"))
35    checkEvaluation(MakeDecimal(Literal.create(null, LongType), 3, 1), null)
36  }
37
38  test("PromotePrecision") {
39    val d1 = Decimal("10.1")
40    checkEvaluation(PromotePrecision(Literal(d1)), d1)
41    val d2 = Decimal(101, 3, 1)
42    checkEvaluation(PromotePrecision(Literal(d2)), d2)
43    checkEvaluation(PromotePrecision(Literal.create(null, DecimalType(2, 1))), null)
44  }
45
46  test("CheckOverflow") {
47    val d1 = Decimal("10.1")
48    checkEvaluation(CheckOverflow(Literal(d1), DecimalType(4, 0)), Decimal("10"))
49    checkEvaluation(CheckOverflow(Literal(d1), DecimalType(4, 1)), d1)
50    checkEvaluation(CheckOverflow(Literal(d1), DecimalType(4, 2)), d1)
51    checkEvaluation(CheckOverflow(Literal(d1), DecimalType(4, 3)), null)
52
53    val d2 = Decimal(101, 3, 1)
54    checkEvaluation(CheckOverflow(Literal(d2), DecimalType(4, 0)), Decimal("10"))
55    checkEvaluation(CheckOverflow(Literal(d2), DecimalType(4, 1)), d2)
56    checkEvaluation(CheckOverflow(Literal(d2), DecimalType(4, 2)), d2)
57    checkEvaluation(CheckOverflow(Literal(d2), DecimalType(4, 3)), null)
58
59    checkEvaluation(CheckOverflow(Literal.create(null, DecimalType(2, 1)), DecimalType(3, 2)), null)
60  }
61
62}
63