Skip to content

Commit 523e60a

Browse files
Add custom constructor inside the class
1 parent 34f2f92 commit 523e60a

File tree

5 files changed

+107
-34
lines changed

5 files changed

+107
-34
lines changed

cpp/src/arrow/util/formatting.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,29 @@ struct FloatToStringFormatter::Impl {
4343
: converter_(DoubleToStringConverter::EMIT_POSITIVE_EXPONENT_SIGN, "inf", "nan",
4444
'e', -6, 10, 6, 0) {}
4545

46+
Impl(int flags, const char* inf_symbol, const char* nan_symbol, char exp_character,
47+
int decimal_in_shortest_low, int decimal_in_shortest_high,
48+
int max_leading_padding_zeroes_in_precision_mode,
49+
int max_trailing_padding_zeroes_in_precision_mode)
50+
: converter_(flags, inf_symbol, nan_symbol, exp_character, decimal_in_shortest_low,
51+
decimal_in_shortest_high, max_leading_padding_zeroes_in_precision_mode,
52+
max_trailing_padding_zeroes_in_precision_mode) {}
53+
4654
DoubleToStringConverter converter_;
4755
};
4856

4957
FloatToStringFormatter::FloatToStringFormatter() : impl_(new Impl()) {}
5058

59+
FloatToStringFormatter::FloatToStringFormatter(
60+
int flags, const char* inf_symbol, const char* nan_symbol, char exp_character,
61+
int decimal_in_shortest_low, int decimal_in_shortest_high,
62+
int max_leading_padding_zeroes_in_precision_mode,
63+
int max_trailing_padding_zeroes_in_precision_mode)
64+
: impl_(new Impl(flags, inf_symbol, nan_symbol, exp_character,
65+
decimal_in_shortest_low, decimal_in_shortest_high,
66+
max_leading_padding_zeroes_in_precision_mode,
67+
max_trailing_padding_zeroes_in_precision_mode)) {}
68+
5169
FloatToStringFormatter::~FloatToStringFormatter() {}
5270

5371
int FloatToStringFormatter::FormatFloat(float v, char* out_buffer, int out_size) {

cpp/src/arrow/util/formatting.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "arrow/status.h"
3232
#include "arrow/type.h"
3333
#include "arrow/type_traits.h"
34+
#include "arrow/util/double_conversion.h"
3435
#include "arrow/util/string_view.h"
3536
#include "arrow/util/time.h"
3637
#include "arrow/util/visibility.h"
@@ -219,6 +220,11 @@ class StringFormatter<UInt64Type> : public IntToStringFormatterMixin<UInt64Type>
219220
class ARROW_EXPORT FloatToStringFormatter {
220221
public:
221222
FloatToStringFormatter();
223+
FloatToStringFormatter(int flags, const char* inf_symbol, const char* nan_symbol,
224+
char exp_character, int decimal_in_shortest_low,
225+
int decimal_in_shortest_high,
226+
int max_leading_padding_zeroes_in_precision_mode,
227+
int max_trailing_padding_zeroes_in_precision_mode);
222228
~FloatToStringFormatter();
223229

224230
// Returns the number of characters written
@@ -239,6 +245,16 @@ class FloatToStringFormatterMixin : public FloatToStringFormatter {
239245

240246
explicit FloatToStringFormatterMixin(const std::shared_ptr<DataType>& = NULLPTR) {}
241247

248+
FloatToStringFormatterMixin(int flags, const char* inf_symbol, const char* nan_symbol,
249+
char exp_character, int decimal_in_shortest_low,
250+
int decimal_in_shortest_high,
251+
int max_leading_padding_zeroes_in_precision_mode,
252+
int max_trailing_padding_zeroes_in_precision_mode)
253+
: FloatToStringFormatter(flags, inf_symbol, nan_symbol, exp_character,
254+
decimal_in_shortest_low, decimal_in_shortest_high,
255+
max_leading_padding_zeroes_in_precision_mode,
256+
max_trailing_padding_zeroes_in_precision_mode) {}
257+
242258
template <typename Appender>
243259
Return<Appender> operator()(value_type value, Appender&& append) {
244260
char buffer[buffer_size];

cpp/src/gandiva/formatting_utils.h

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
1918
#pragma once
2019

21-
#include "arrow/util/formatting.h"
2220
#include "arrow/type.h"
21+
#include "arrow/util/formatting.h"
2322
#include "arrow/vendored/double-conversion/double-conversion.h"
2423

2524
#ifndef ARROW_SRC_GANDIVA_FORMATTING_UTILS_H_
@@ -31,39 +30,34 @@ namespace gandiva {
3130
template <typename ARROW_TYPE, typename Enable = void>
3231
class GdvStringFormatter;
3332

34-
template<typename ARROW_TYPE>
35-
class FloatToStringGdvMixin :
36-
public arrow::internal::FloatToStringFormatterMixin<ARROW_TYPE>{
33+
using double_conversion::DoubleToStringConverter;
3734

35+
template <typename ARROW_TYPE>
36+
class FloatToStringGdvMixin
37+
: public arrow::internal::FloatToStringFormatterMixin<ARROW_TYPE> {
3838
public:
39-
using arrow::internal::FloatToStringFormatterMixin<ARROW_TYPE>::FloatToStringFormatterMixin;
39+
using arrow::internal::FloatToStringFormatterMixin<
40+
ARROW_TYPE>::FloatToStringFormatterMixin;
4041

4142
explicit FloatToStringGdvMixin(const std::shared_ptr<arrow::DataType>& = NULLPTR)
42-
: arrow::internal::FloatToStringFormatterMixin<ARROW_TYPE>()
43-
{
44-
const int flags =
45-
double_conversion::DoubleToStringConverter::EMIT_TRAILING_ZERO_AFTER_POINT |
46-
double_conversion::DoubleToStringConverter::EMIT_TRAILING_DECIMAL_POINT;
47-
48-
double_conversion::DoubleToStringConverter return_(flags, "inf", "nan",
49-
'E', -3, 7, 6, 1);
50-
51-
this->impl_->converter_ = return_;
52-
}
43+
: arrow::internal::FloatToStringFormatterMixin<ARROW_TYPE>(
44+
DoubleToStringConverter::EMIT_TRAILING_ZERO_AFTER_POINT |
45+
DoubleToStringConverter::EMIT_TRAILING_DECIMAL_POINT,
46+
"inf", "nan", 'E', -3, 7, 3, 1) {}
5347
};
5448

5549
template <>
56-
class GdvStringFormatter<arrow::FloatType> :
57-
public FloatToStringGdvMixin<arrow::FloatType> {
50+
class GdvStringFormatter<arrow::FloatType>
51+
: public FloatToStringGdvMixin<arrow::FloatType> {
5852
public:
5953
using FloatToStringGdvMixin::FloatToStringGdvMixin;
6054
};
6155

6256
template <>
63-
class GdvStringFormatter<arrow::DoubleType> :
64-
public FloatToStringGdvMixin<arrow::DoubleType> {
57+
class GdvStringFormatter<arrow::DoubleType>
58+
: public FloatToStringGdvMixin<arrow::DoubleType> {
6559
public:
6660
using FloatToStringGdvMixin::FloatToStringGdvMixin;
6761
};
68-
}
69-
#endif //ARROW_SRC_GANDIVA_FORMATTING_UTILS_H_
62+
} // namespace gandiva
63+
#endif // ARROW_SRC_GANDIVA_FORMATTING_UTILS_H_

cpp/src/gandiva/gdv_function_stubs.cc

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
#include "gandiva/engine.h"
2626
#include "gandiva/exported_funcs.h"
2727
#include "gandiva/hash_utils.h"
28+
#include "gandiva/formatting_utils.h"
2829
#include "gandiva/in_holder.h"
2930
#include "gandiva/like_holder.h"
3031
#include "gandiva/precompiled/types.h"
3132
#include "gandiva/random_generator_holder.h"
3233
#include "gandiva/to_date_holder.h"
33-
#include "gandiva/formatting_utils.h"
3434

3535
/// Stub functions that can be accessed from LLVM or the pre-compiled library.
3636

@@ -307,7 +307,7 @@ CAST_NUMERIC_FROM_STRING(double, arrow::DoubleType, FLOAT8)
307307

308308
#undef CAST_NUMERIC_FROM_STRING
309309

310-
#define GDV_FN_CAST_VARCHAR(IN_TYPE, ARROW_TYPE) \
310+
#define GDV_FN_CAST_VARCHAR_INTEGER(IN_TYPE, ARROW_TYPE) \
311311
GANDIVA_EXPORT \
312312
const char* gdv_fn_castVARCHAR_##IN_TYPE##_int64(int64_t context, gdv_##IN_TYPE value, \
313313
int64_t len, int32_t * out_len) { \
@@ -343,12 +343,49 @@ CAST_NUMERIC_FROM_STRING(double, arrow::DoubleType, FLOAT8)
343343
return ret; \
344344
}
345345

346-
GDV_FN_CAST_VARCHAR(int32, Int32Type)
347-
GDV_FN_CAST_VARCHAR(int64, Int64Type)
348-
GDV_FN_CAST_VARCHAR(float32, FloatType)
349-
GDV_FN_CAST_VARCHAR(float64, DoubleType)
346+
#define GDV_FN_CAST_VARCHAR_REAL(IN_TYPE, ARROW_TYPE) \
347+
GANDIVA_EXPORT \
348+
const char* gdv_fn_castVARCHAR_##IN_TYPE##_int64(int64_t context, gdv_##IN_TYPE value, \
349+
int64_t len, int32_t * out_len) { \
350+
if (len < 0) { \
351+
gdv_fn_context_set_error_msg(context, "Buffer length can not be negative"); \
352+
*out_len = 0; \
353+
return ""; \
354+
} \
355+
if (len == 0) { \
356+
*out_len = 0; \
357+
return ""; \
358+
} \
359+
gandiva::GdvStringFormatter<arrow::ARROW_TYPE> formatter; \
360+
char* ret = reinterpret_cast<char*>( \
361+
gdv_fn_context_arena_malloc(context, static_cast<int32_t>(len))); \
362+
if (ret == nullptr) { \
363+
gdv_fn_context_set_error_msg(context, "Could not allocate memory"); \
364+
*out_len = 0; \
365+
return ""; \
366+
} \
367+
arrow::Status status = formatter(value, [&](arrow::util::string_view v) { \
368+
int64_t size = static_cast<int64_t>(v.size()); \
369+
*out_len = static_cast<int32_t>(len < size ? len : size); \
370+
memcpy(ret, v.data(), *out_len); \
371+
return arrow::Status::OK(); \
372+
}); \
373+
if (!status.ok()) { \
374+
std::string err = "Could not cast " + std::to_string(value) + " to string"; \
375+
gdv_fn_context_set_error_msg(context, err.c_str()); \
376+
*out_len = 0; \
377+
return ""; \
378+
} \
379+
return ret; \
380+
}
381+
382+
GDV_FN_CAST_VARCHAR_INTEGER(int32, Int32Type)
383+
GDV_FN_CAST_VARCHAR_INTEGER(int64, Int64Type)
384+
GDV_FN_CAST_VARCHAR_REAL(float32, FloatType)
385+
GDV_FN_CAST_VARCHAR_REAL(float64, DoubleType)
350386

351-
#undef GDV_FN_CAST_VARCHAR
387+
#undef GDV_FN_CAST_VARCHAR_INTEGER
388+
#undef GDV_FN_CAST_VARCHAR_REAL
352389
}
353390

354391
namespace gandiva {

cpp/src/gandiva/gdv_function_stubs_test.cc

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,15 @@ TEST(TestGdvFnStubs, TestCastVARCHARFromFloat) {
234234
EXPECT_FALSE(ctx.has_error());
235235

236236
out_str = gdv_fn_castVARCHAR_float32_int64(ctx_ptr, 0.00001f, 100, &out_len);
237-
EXPECT_EQ(std::string(out_str, out_len), "0.00001");
237+
EXPECT_EQ(std::string(out_str, out_len), "1E-5");
238+
EXPECT_FALSE(ctx.has_error());
239+
240+
out_str = gdv_fn_castVARCHAR_float32_int64(ctx_ptr, 0.00099999f, 100, &out_len);
241+
EXPECT_EQ(std::string(out_str, out_len), "9.9999E-4");
238242
EXPECT_FALSE(ctx.has_error());
239243

240244
out_str = gdv_fn_castVARCHAR_float32_int64(ctx_ptr, 0.0f, 100, &out_len);
241-
EXPECT_EQ(std::string(out_str, out_len), "0");
245+
EXPECT_EQ(std::string(out_str, out_len), "0.0");
242246
EXPECT_FALSE(ctx.has_error());
243247

244248
// test with required length less than actual buffer length
@@ -261,11 +265,15 @@ TEST(TestGdvFnStubs, TestCastVARCHARFromDouble) {
261265
EXPECT_FALSE(ctx.has_error());
262266

263267
out_str = gdv_fn_castVARCHAR_float64_int64(ctx_ptr, 0.00001, 100, &out_len);
264-
EXPECT_EQ(std::string(out_str, out_len), "0.00001");
268+
EXPECT_EQ(std::string(out_str, out_len), "1E-5");
269+
EXPECT_FALSE(ctx.has_error());
270+
271+
out_str = gdv_fn_castVARCHAR_float32_int64(ctx_ptr, 0.00099999f, 100, &out_len);
272+
EXPECT_EQ(std::string(out_str, out_len), "9.9999E-4");
265273
EXPECT_FALSE(ctx.has_error());
266274

267275
out_str = gdv_fn_castVARCHAR_float64_int64(ctx_ptr, 0.0, 100, &out_len);
268-
EXPECT_EQ(std::string(out_str, out_len), "0");
276+
EXPECT_EQ(std::string(out_str, out_len), "0.0");
269277
EXPECT_FALSE(ctx.has_error());
270278

271279
// test with required length less than actual buffer length

0 commit comments

Comments
 (0)