|
| 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.codegen |
| 19 | + |
| 20 | +import org.apache.spark.SparkFunSuite |
| 21 | +import org.apache.spark.sql.types.Decimal |
| 22 | + |
| 23 | +class UnsafeRowWriterSuite extends SparkFunSuite { |
| 24 | + |
| 25 | + def checkDecimalSizeInBytes(decimal: Decimal, numBytes: Int): Unit = { |
| 26 | + assert(decimal.toJavaBigDecimal.unscaledValue().toByteArray.length == numBytes) |
| 27 | + } |
| 28 | + |
| 29 | + test("SPARK-25538: zero-out all bits for decimals") { |
| 30 | + val decimal1 = Decimal(0.431) |
| 31 | + decimal1.changePrecision(38, 18) |
| 32 | + checkDecimalSizeInBytes(decimal1, 8) |
| 33 | + |
| 34 | + val decimal2 = Decimal(123456789.1232456789) |
| 35 | + decimal2.changePrecision(38, 18) |
| 36 | + checkDecimalSizeInBytes(decimal2, 11) |
| 37 | + // On an UnsafeRowWriter we write decimal2 first and then decimal1 |
| 38 | + val unsafeRowWriter1 = new UnsafeRowWriter(1) |
| 39 | + unsafeRowWriter1.resetRowWriter() |
| 40 | + unsafeRowWriter1.write(0, decimal2, decimal2.precision, decimal2.scale) |
| 41 | + unsafeRowWriter1.reset() |
| 42 | + unsafeRowWriter1.write(0, decimal1, decimal1.precision, decimal1.scale) |
| 43 | + val res1 = unsafeRowWriter1.getRow |
| 44 | + // On a second UnsafeRowWriter we write directly decimal1 |
| 45 | + val unsafeRowWriter2 = new UnsafeRowWriter(1) |
| 46 | + unsafeRowWriter2.resetRowWriter() |
| 47 | + unsafeRowWriter2.write(0, decimal1, decimal1.precision, decimal1.scale) |
| 48 | + val res2 = unsafeRowWriter2.getRow |
| 49 | + // The two rows should be the equal |
| 50 | + assert(res1 == res2) |
| 51 | + } |
| 52 | + |
| 53 | +} |
0 commit comments