forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecimal.cc
More file actions
226 lines (182 loc) · 7.61 KB
/
Copy pathdecimal.cc
File metadata and controls
226 lines (182 loc) · 7.61 KB
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
// 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 <algorithm>
#include <limits>
#include "arrow/python/common.h"
#include "arrow/python/decimal.h"
#include "arrow/python/helpers.h"
#include "arrow/util/decimal.h"
#include "arrow/util/logging.h"
#include <arrow/api.h>
namespace arrow {
namespace py {
namespace internal {
Status ImportDecimalType(OwnedRef* decimal_type) {
OwnedRef decimal_module;
RETURN_NOT_OK(ImportModule("decimal", &decimal_module));
RETURN_NOT_OK(ImportFromModule(decimal_module, "Decimal", decimal_type));
return Status::OK();
}
Status PythonDecimalToString(PyObject* python_decimal, std::string* out) {
// Call Python's str(decimal_object)
return PyObject_StdStringStr(python_decimal, out);
}
// \brief Infer the precision and scale of a Python decimal.Decimal instance
// \param python_decimal[in] An instance of decimal.Decimal
// \param precision[out] The value of the inferred precision
// \param scale[out] The value of the inferred scale
// \return The status of the operation
static Status InferDecimalPrecisionAndScale(PyObject* python_decimal, int32_t* precision,
int32_t* scale) {
DCHECK_NE(python_decimal, NULLPTR);
DCHECK_NE(precision, NULLPTR);
DCHECK_NE(scale, NULLPTR);
// TODO(phillipc): Make sure we perform PyDecimal_Check(python_decimal) as a DCHECK
OwnedRef as_tuple(PyObject_CallMethod(python_decimal, const_cast<char*>("as_tuple"),
const_cast<char*>("")));
RETURN_IF_PYERROR();
DCHECK(PyTuple_Check(as_tuple.obj()));
OwnedRef digits(PyObject_GetAttrString(as_tuple.obj(), "digits"));
RETURN_IF_PYERROR();
DCHECK(PyTuple_Check(digits.obj()));
const auto num_digits = static_cast<int32_t>(PyTuple_Size(digits.obj()));
RETURN_IF_PYERROR();
OwnedRef py_exponent(PyObject_GetAttrString(as_tuple.obj(), "exponent"));
RETURN_IF_PYERROR();
DCHECK(IsPyInteger(py_exponent.obj()));
const auto exponent = static_cast<int32_t>(PyLong_AsLong(py_exponent.obj()));
RETURN_IF_PYERROR();
const int32_t abs_exponent = std::abs(exponent);
int32_t num_additional_zeros;
if (num_digits <= abs_exponent) {
DCHECK_NE(exponent, 0) << "exponent should never be zero here";
// we have leading/trailing zeros, leading if exponent is negative
num_additional_zeros = exponent < 0 ? abs_exponent - num_digits : exponent;
*scale = static_cast<int32_t>(exponent < 0) * -exponent;
} else {
// we can use the number of digits as the precision
num_additional_zeros = 0;
*scale = -exponent;
}
*precision = num_digits + num_additional_zeros;
return Status::OK();
}
PyObject* DecimalFromString(PyObject* decimal_constructor,
const std::string& decimal_string) {
DCHECK_NE(decimal_constructor, nullptr);
auto string_size = decimal_string.size();
DCHECK_GT(string_size, 0);
auto string_bytes = decimal_string.c_str();
DCHECK_NE(string_bytes, nullptr);
return PyObject_CallFunction(decimal_constructor, const_cast<char*>("s#"), string_bytes,
string_size);
}
namespace {
Status DecimalFromStdString(const std::string& decimal_string,
const DecimalType& arrow_type, Decimal128* out) {
int32_t inferred_precision;
int32_t inferred_scale;
RETURN_NOT_OK(
Decimal128::FromString(decimal_string, out, &inferred_precision, &inferred_scale));
const int32_t precision = arrow_type.precision();
const int32_t scale = arrow_type.scale();
if (ARROW_PREDICT_FALSE(inferred_precision > precision)) {
return Status::Invalid(
"Decimal type with precision ", inferred_precision,
" does not fit into precision inferred from first array element: ", precision);
}
if (scale != inferred_scale) {
DCHECK_NE(out, NULLPTR);
RETURN_NOT_OK(out->Rescale(inferred_scale, scale, out));
}
return Status::OK();
}
} // namespace
Status DecimalFromPythonDecimal(PyObject* python_decimal, const DecimalType& arrow_type,
Decimal128* out) {
DCHECK_NE(python_decimal, NULLPTR);
DCHECK_NE(out, NULLPTR);
std::string string;
RETURN_NOT_OK(PythonDecimalToString(python_decimal, &string));
return DecimalFromStdString(string, arrow_type, out);
}
Status DecimalFromPyObject(PyObject* obj, const DecimalType& arrow_type,
Decimal128* out) {
DCHECK_NE(obj, NULLPTR);
DCHECK_NE(out, NULLPTR);
if (IsPyInteger(obj)) {
// TODO: add a fast path for small-ish ints
std::string string;
RETURN_NOT_OK(PyObject_StdStringStr(obj, &string));
return DecimalFromStdString(string, arrow_type, out);
} else if (PyDecimal_Check(obj)) {
return DecimalFromPythonDecimal(obj, arrow_type, out);
} else {
return Status::TypeError("int or Decimal object expected, got ",
Py_TYPE(obj)->tp_name);
}
}
bool PyDecimal_Check(PyObject* obj) {
static OwnedRef decimal_type;
if (!decimal_type.obj()) {
Status status = ImportDecimalType(&decimal_type);
DCHECK_OK(status);
DCHECK(PyType_Check(decimal_type.obj()));
}
// PyObject_IsInstance() is slower as it has to check for virtual subclasses
const int result =
PyType_IsSubtype(Py_TYPE(obj), reinterpret_cast<PyTypeObject*>(decimal_type.obj()));
DCHECK_NE(result, -1) << " error during PyType_IsSubtype check";
return result == 1;
}
bool PyDecimal_ISNAN(PyObject* obj) {
DCHECK(PyDecimal_Check(obj)) << "obj is not an instance of decimal.Decimal";
OwnedRef is_nan(
PyObject_CallMethod(obj, const_cast<char*>("is_nan"), const_cast<char*>("")));
return PyObject_IsTrue(is_nan.obj()) == 1;
}
DecimalMetadata::DecimalMetadata()
: DecimalMetadata(std::numeric_limits<int32_t>::min(),
std::numeric_limits<int32_t>::min()) {}
DecimalMetadata::DecimalMetadata(int32_t precision, int32_t scale)
: precision_(precision), scale_(scale) {}
Status DecimalMetadata::Update(int32_t suggested_precision, int32_t suggested_scale) {
const int32_t current_precision = precision_;
precision_ = std::max(current_precision, suggested_precision);
const int32_t current_scale = scale_;
scale_ = std::max(current_scale, suggested_scale);
// if our suggested scale is zero and we don't yet have enough precision then we need to
// add whatever the current scale is to the precision
if (suggested_scale == 0 && suggested_precision > current_precision) {
precision_ += scale_;
}
return Status::OK();
}
Status DecimalMetadata::Update(PyObject* object) {
bool is_decimal = PyDecimal_Check(object);
DCHECK(is_decimal) << "Object is not a Python Decimal";
if (ARROW_PREDICT_FALSE(!is_decimal || PyDecimal_ISNAN(object))) {
return Status::OK();
}
int32_t precision = 0;
int32_t scale = 0;
RETURN_NOT_OK(InferDecimalPrecisionAndScale(object, &precision, &scale));
return Update(precision, scale);
}
} // namespace internal
} // namespace py
} // namespace arrow