summaryrefslogtreecommitdiffstats
path: root/src/arrow/cpp/src/gandiva/tests/decimal_single_test.cc
blob: 666ee4a68d5de9f533c2448d21bd71be6a61ce69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// 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.

#include <sstream>

#include <gtest/gtest.h>
#include "arrow/memory_pool.h"
#include "arrow/status.h"

#include "gandiva/decimal_scalar.h"
#include "gandiva/decimal_type_util.h"
#include "gandiva/projector.h"
#include "gandiva/tests/test_util.h"
#include "gandiva/tree_expr_builder.h"

using arrow::Decimal128;

namespace gandiva {

#define EXPECT_DECIMAL_RESULT(op, x, y, expected, actual)                                \
  EXPECT_EQ(expected, actual) << op << " (" << (x).ToString() << "),(" << (y).ToString() \
                              << ")"                                                     \
                              << " expected : " << (expected).ToString()                 \
                              << " actual : " << (actual).ToString();

DecimalScalar128 decimal_literal(const char* value, int precision, int scale) {
  std::string value_string = std::string(value);
  return DecimalScalar128(value_string, precision, scale);
}

class TestDecimalOps : public ::testing::Test {
 public:
  void SetUp() { pool_ = arrow::default_memory_pool(); }

  ArrayPtr MakeDecimalVector(const DecimalScalar128& in);

  void Verify(DecimalTypeUtil::Op, const std::string& function, const DecimalScalar128& x,
              const DecimalScalar128& y, const DecimalScalar128& expected);

  void AddAndVerify(const DecimalScalar128& x, const DecimalScalar128& y,
                    const DecimalScalar128& expected) {
    Verify(DecimalTypeUtil::kOpAdd, "add", x, y, expected);
  }

  void SubtractAndVerify(const DecimalScalar128& x, const DecimalScalar128& y,
                         const DecimalScalar128& expected) {
    Verify(DecimalTypeUtil::kOpSubtract, "subtract", x, y, expected);
  }

  void MultiplyAndVerify(const DecimalScalar128& x, const DecimalScalar128& y,
                         const DecimalScalar128& expected) {
    Verify(DecimalTypeUtil::kOpMultiply, "multiply", x, y, expected);
  }

  void DivideAndVerify(const DecimalScalar128& x, const DecimalScalar128& y,
                       const DecimalScalar128& expected) {
    Verify(DecimalTypeUtil::kOpDivide, "divide", x, y, expected);
  }

  void ModAndVerify(const DecimalScalar128& x, const DecimalScalar128& y,
                    const DecimalScalar128& expected) {
    Verify(DecimalTypeUtil::kOpMod, "mod", x, y, expected);
  }

 protected:
  arrow::MemoryPool* pool_;
};

ArrayPtr TestDecimalOps::MakeDecimalVector(const DecimalScalar128& in) {
  std::vector<arrow::Decimal128> ret;

  Decimal128 decimal_value = in.value();

  auto decimal_type = std::make_shared<arrow::Decimal128Type>(in.precision(), in.scale());
  return MakeArrowArrayDecimal(decimal_type, {decimal_value}, {true});
}

void TestDecimalOps::Verify(DecimalTypeUtil::Op op, const std::string& function,
                            const DecimalScalar128& x, const DecimalScalar128& y,
                            const DecimalScalar128& expected) {
  auto x_type = std::make_shared<arrow::Decimal128Type>(x.precision(), x.scale());
  auto y_type = std::make_shared<arrow::Decimal128Type>(y.precision(), y.scale());
  auto field_x = field("x", x_type);
  auto field_y = field("y", y_type);
  auto schema = arrow::schema({field_x, field_y});

  Decimal128TypePtr output_type;
  auto status = DecimalTypeUtil::GetResultType(op, {x_type, y_type}, &output_type);
  ARROW_EXPECT_OK(status);

  // output fields
  auto res = field("res", output_type);

  // build expression : x op y
  auto expr = TreeExprBuilder::MakeExpression(function, {field_x, field_y}, res);

  // Build a projector for the expression.
  std::shared_ptr<Projector> projector;
  status = Projector::Make(schema, {expr}, TestConfiguration(), &projector);
  ARROW_EXPECT_OK(status);

  // Create a row-batch with some sample data
  auto array_a = MakeDecimalVector(x);
  auto array_b = MakeDecimalVector(y);

  // prepare input record batch
  auto in_batch = arrow::RecordBatch::Make(schema, 1 /*num_records*/, {array_a, array_b});

  // Evaluate expression
  arrow::ArrayVector outputs;
  status = projector->Evaluate(*in_batch, pool_, &outputs);
  ARROW_EXPECT_OK(status);

  // Validate results
  auto out_array = dynamic_cast<arrow::Decimal128Array*>(outputs[0].get());
  const Decimal128 out_value(out_array->GetValue(0));

  auto dtype = dynamic_cast<arrow::Decimal128Type*>(out_array->type().get());
  std::string value_string = out_value.ToString(0);
  DecimalScalar128 actual{value_string, dtype->precision(), dtype->scale()};

  EXPECT_DECIMAL_RESULT(function, x, y, expected, actual);
}

TEST_F(TestDecimalOps, TestAdd) {
  // fast-path
  AddAndVerify(decimal_literal("201", 30, 3),   // x
               decimal_literal("301", 30, 3),   // y
               decimal_literal("502", 31, 3));  // expected

  AddAndVerify(decimal_literal("201", 30, 3),    // x
               decimal_literal("301", 30, 2),    // y
               decimal_literal("3211", 32, 3));  // expected

  AddAndVerify(decimal_literal("201", 30, 3),    // x
               decimal_literal("301", 30, 4),    // y
               decimal_literal("2311", 32, 4));  // expected

  // max precision, but no overflow
  AddAndVerify(decimal_literal("201", 38, 3),   // x
               decimal_literal("301", 38, 3),   // y
               decimal_literal("502", 38, 3));  // expected

  AddAndVerify(decimal_literal("201", 38, 3),    // x
               decimal_literal("301", 38, 2),    // y
               decimal_literal("3211", 38, 3));  // expected

  AddAndVerify(decimal_literal("201", 38, 3),    // x
               decimal_literal("301", 38, 4),    // y
               decimal_literal("2311", 38, 4));  // expected

  AddAndVerify(decimal_literal("201", 38, 3),      // x
               decimal_literal("301", 38, 7),      // y
               decimal_literal("201030", 38, 6));  // expected

  AddAndVerify(decimal_literal("1201", 38, 3),   // x
               decimal_literal("1801", 38, 3),   // y
               decimal_literal("3002", 38, 3));  // carry-over from fractional

  // max precision
  AddAndVerify(decimal_literal("09999999999999999999999999999999000000", 38, 5),  // x
               decimal_literal("100", 38, 7),                                     // y
               decimal_literal("99999999999999999999999999999990000010", 38, 6));

  AddAndVerify(decimal_literal("-09999999999999999999999999999999000000", 38, 5),  // x
               decimal_literal("100", 38, 7),                                      // y
               decimal_literal("-99999999999999999999999999999989999990", 38, 6));

  AddAndVerify(decimal_literal("09999999999999999999999999999999000000", 38, 5),  // x
               decimal_literal("-100", 38, 7),                                    // y
               decimal_literal("99999999999999999999999999999989999990", 38, 6));

  AddAndVerify(decimal_literal("-09999999999999999999999999999999000000", 38, 5),  // x
               decimal_literal("-100", 38, 7),                                     // y
               decimal_literal("-99999999999999999999999999999990000010", 38, 6));

  AddAndVerify(decimal_literal("09999999999999999999999999999999999999", 38, 6),  // x
               decimal_literal("89999999999999999999999999999999999999", 38, 7),  // y
               decimal_literal("18999999999999999999999999999999999999", 38, 6));

  // Both -ve
  AddAndVerify(decimal_literal("-201", 30, 3),    // x
               decimal_literal("-301", 30, 2),    // y
               decimal_literal("-3211", 32, 3));  // expected

  AddAndVerify(decimal_literal("-201", 38, 3),    // x
               decimal_literal("-301", 38, 4),    // y
               decimal_literal("-2311", 38, 4));  // expected

  // Mix of +ve and -ve
  AddAndVerify(decimal_literal("-201", 30, 3),   // x
               decimal_literal("301", 30, 2),    // y
               decimal_literal("2809", 32, 3));  // expected

  AddAndVerify(decimal_literal("-201", 38, 3),    // x
               decimal_literal("301", 38, 4),     // y
               decimal_literal("-1709", 38, 4));  // expected

  AddAndVerify(decimal_literal("201", 38, 3),      // x
               decimal_literal("-301", 38, 7),     // y
               decimal_literal("200970", 38, 6));  // expected

  AddAndVerify(decimal_literal("-1901", 38, 4),  // x
               decimal_literal("1801", 38, 4),   // y
               decimal_literal("-100", 38, 4));  // expected

  AddAndVerify(decimal_literal("1801", 38, 4),   // x
               decimal_literal("-1901", 38, 4),  // y
               decimal_literal("-100", 38, 4));  // expected

  // rounding +ve
  AddAndVerify(decimal_literal("1000999", 38, 6),   // x
               decimal_literal("10000999", 38, 7),  // y
               decimal_literal("2001099", 38, 6));

  AddAndVerify(decimal_literal("1000999", 38, 6),   // x
               decimal_literal("10000995", 38, 7),  // y
               decimal_literal("2001099", 38, 6));

  AddAndVerify(decimal_literal("1000999", 38, 6),   // x
               decimal_literal("10000992", 38, 7),  // y
               decimal_literal("2001098", 38, 6));

  // rounding -ve
  AddAndVerify(decimal_literal("-1000999", 38, 6),   // x
               decimal_literal("-10000999", 38, 7),  // y
               decimal_literal("-2001099", 38, 6));

  AddAndVerify(decimal_literal("-1000999", 38, 6),   // x
               decimal_literal("-10000995", 38, 7),  // y
               decimal_literal("-2001099", 38, 6));

  AddAndVerify(decimal_literal("-1000999", 38, 6),   // x
               decimal_literal("-10000992", 38, 7),  // y
               decimal_literal("-2001098", 38, 6));
}

// subtract is a wrapper over add. so, minimal tests are sufficient.
TEST_F(TestDecimalOps, TestSubtract) {
  // fast-path
  SubtractAndVerify(decimal_literal("201", 30, 3),    // x
                    decimal_literal("301", 30, 3),    // y
                    decimal_literal("-100", 31, 3));  // expected

  // max precision
  SubtractAndVerify(
      decimal_literal("09999999999999999999999999999999000000", 38, 5),  // x
      decimal_literal("100", 38, 7),                                     // y
      decimal_literal("99999999999999999999999999999989999990", 38, 6));

  // Mix of +ve and -ve
  SubtractAndVerify(decimal_literal("-201", 30, 3),    // x
                    decimal_literal("301", 30, 2),     // y
                    decimal_literal("-3211", 32, 3));  // expected
}

// Lots of unit tests for multiply/divide/mod in decimal_ops_test.cc. So, keeping these
// basic.
TEST_F(TestDecimalOps, TestMultiply) {
  // fast-path
  MultiplyAndVerify(decimal_literal("201", 10, 3),     // x
                    decimal_literal("301", 10, 2),     // y
                    decimal_literal("60501", 21, 5));  // expected

  // max precision
  MultiplyAndVerify(DecimalScalar128(std::string(35, '9'), 38, 20),  // x
                    DecimalScalar128(std::string(36, '9'), 38, 20),  // x
                    DecimalScalar128("9999999999999999999999999999999999890", 38, 6));
}

TEST_F(TestDecimalOps, TestDivide) {
  DivideAndVerify(decimal_literal("201", 10, 3),              // x
                  decimal_literal("301", 10, 2),              // y
                  decimal_literal("6677740863787", 23, 14));  // expected

  DivideAndVerify(DecimalScalar128(std::string(38, '9'), 38, 20),  // x
                  DecimalScalar128(std::string(35, '9'), 38, 20),  // x
                  DecimalScalar128("1000000000", 38, 6));
}

TEST_F(TestDecimalOps, TestMod) {
  ModAndVerify(decimal_literal("201", 20, 2),   // x
               decimal_literal("301", 20, 3),   // y
               decimal_literal("204", 20, 3));  // expected

  ModAndVerify(DecimalScalar128(std::string(38, '9'), 38, 20),  // x
               DecimalScalar128(std::string(35, '9'), 38, 21),  // x
               DecimalScalar128("9990", 38, 21));
}

}  // namespace gandiva