Skip to content

Commit

Permalink
temp check
Browse files Browse the repository at this point in the history
  • Loading branch information
projjal committed Sep 18, 2020
1 parent 544d39f commit eece673
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 44 deletions.
173 changes: 132 additions & 41 deletions cpp/src/gandiva/gdv_function_stubs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,138 @@

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

const char* gdv_fn_castVARCHAR_int32_int64(int64_t context, int32_t value, int64_t len,
int32_t* out_len) {
if (len < 0) {
gdv_fn_context_set_error_msg(context, "Buffer length can not be negative");
*out_len = 0;
return "";
}
if (len == 0) {
*out_len = 0;
return "";
}
arrow::internal::StringFormatter<arrow::Int32Type> formatter;
char* ret = reinterpret_cast<char*>(
gdv_fn_context_arena_malloc(context, static_cast<int32_t>(len)));
if (ret == nullptr) {
gdv_fn_context_set_error_msg(context, "Could not allocate memory");
*out_len = 0;
return "";
}
arrow::Status status = formatter(value, [&](arrow::util::string_view v) {
int64_t size = static_cast<int64_t>(v.size());
*out_len = static_cast<int32_t>(len < size ? len : size);
memcpy(ret, v.data(), *out_len);
return arrow::Status::OK();
});
if (!status.ok()) {
gdv_fn_context_set_error_msg(context, "Could not cast to string");
*out_len = 0;
return "";
}
return ret;
}

const char* gdv_fn_castVARCHAR_int64_int64(int64_t context, int64_t value, int64_t len,
int32_t* out_len) {
if (len < 0) {
gdv_fn_context_set_error_msg(context, "Buffer length can not be negative");
*out_len = 0;
return "";
}
if (len == 0) {
*out_len = 0;
return "";
}
arrow::internal::StringFormatter<arrow::Int64Type> formatter;
char* ret = reinterpret_cast<char*>(
gdv_fn_context_arena_malloc(context, static_cast<int32_t>(len)));
if (ret == nullptr) {
gdv_fn_context_set_error_msg(context, "Could not allocate memory");
*out_len = 0;
return "";
}
arrow::Status status = formatter(value, [&](arrow::util::string_view v) {
int64_t size = static_cast<int64_t>(v.size());
*out_len = static_cast<int32_t>(len < size ? len : size);
memcpy(ret, v.data(), *out_len);
return arrow::Status::OK();
});
if (!status.ok()) {
gdv_fn_context_set_error_msg(context, "Could not cast to string");
*out_len = 0;
return "";
}
return ret;
}

const char* gdv_fn_castVARCHAR_float32_int64(int64_t context, float value, int64_t len,
int32_t* out_len) {
if (len < 0) {
gdv_fn_context_set_error_msg(context, "Buffer length can not be negative");
*out_len = 0;
return "";
}
if (len == 0) {
*out_len = 0;
return "";
}
arrow::internal::StringFormatter<arrow::FloatType> formatter;
char* ret = reinterpret_cast<char*>(
gdv_fn_context_arena_malloc(context, static_cast<int32_t>(len)));
if (ret == nullptr) {
gdv_fn_context_set_error_msg(context, "Could not allocate memory");
*out_len = 0;
return "";
}
arrow::Status status = formatter(value, [&](arrow::util::string_view v) {
int64_t size = static_cast<int64_t>(v.size());
*out_len = static_cast<int32_t>(len < size ? len : size);
memcpy(ret, v.data(), *out_len);
return arrow::Status::OK();
});
if (!status.ok()) {
gdv_fn_context_set_error_msg(context, "Could not cast to string");
*out_len = 0;
return "";
}
return ret;
}

const char* gdv_fn_castVARCHAR_float64_int64(int64_t context, double value, int64_t len,
int32_t* out_len) {
if (len < 0) {
gdv_fn_context_set_error_msg(context, "Buffer length can not be negative");
*out_len = 0;
return "";
}
if (len == 0) {
*out_len = 0;
return "";
}
arrow::internal::StringFormatter<arrow::DoubleType> formatter;
char* ret = reinterpret_cast<char*>(
gdv_fn_context_arena_malloc(context, static_cast<int32_t>(len)));
if (ret == nullptr) {
gdv_fn_context_set_error_msg(context, "Could not allocate memory");
*out_len = 0;
return "";
}
arrow::Status status = formatter(value, [&](arrow::util::string_view v) {
int64_t size = static_cast<int64_t>(v.size());
*out_len = static_cast<int32_t>(len < size ? len : size);
memcpy(ret, v.data(), *out_len);
return arrow::Status::OK();
});
if (!status.ok()) {
gdv_fn_context_set_error_msg(context, "Could not cast to string");
*out_len = 0;
return "";
}
return ret;
}

extern "C" {

bool gdv_fn_like_utf8_utf8(int64_t ptr, const char* data, int data_len,
Expand Down Expand Up @@ -143,47 +275,6 @@ char* gdv_fn_dec_to_string(int64_t context, int64_t x_high, uint64_t x_low,
memcpy(ret, dec_str.data(), *dec_str_len);
return ret;
}

#define GDV_FN_CAST_VARCHAR(IN_TYPE, ARROW_TYPE) \
const char* gdv_fn_castVARCHAR_##IN_TYPE##_int64(int64_t context, gdv_##IN_TYPE value, \
int64_t len, int32_t * out_len) { \
if (len < 0) { \
gdv_fn_context_set_error_msg(context, "Buffer length can not be negative"); \
*out_len = 0; \
return ""; \
} \
if (len == 0) { \
*out_len = 0; \
return ""; \
} \
arrow::internal::StringFormatter<arrow::ARROW_TYPE> formatter; \
char* ret = reinterpret_cast<char*>( \
gdv_fn_context_arena_malloc(context, static_cast<int32_t>(len))); \
if (ret == nullptr) { \
gdv_fn_context_set_error_msg(context, "Could not allocate memory"); \
*out_len = 0; \
return ""; \
} \
arrow::Status status = formatter(value, [&](arrow::util::string_view v) { \
int64_t size = static_cast<int64_t>(v.size()); \
*out_len = static_cast<int32_t>(len < size ? len : size); \
memcpy(ret, v.data(), *out_len); \
return arrow::Status::OK(); \
}); \
if (!status.ok()) { \
gdv_fn_context_set_error_msg(context, "Could not cast to string"); \
*out_len = 0; \
return ""; \
} \
return ret; \
}

GDV_FN_CAST_VARCHAR(int32, Int32Type)
GDV_FN_CAST_VARCHAR(int64, Int64Type)
GDV_FN_CAST_VARCHAR(float32, FloatType)
GDV_FN_CAST_VARCHAR(float64, DoubleType)

#undef GDV_FN_CAST_VARCHAR
}

namespace gandiva {
Expand Down
3 changes: 2 additions & 1 deletion cpp/src/gandiva/gdv_function_stubs.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ int32_t gdv_fn_dec_from_string(int64_t context, const char* in, int32_t in_lengt

char* gdv_fn_dec_to_string(int64_t context, int64_t x_high, uint64_t x_low,
int32_t x_scale, int32_t* dec_str_len);
}

const char* gdv_fn_castVARCHAR_int32_int64(int64_t context, int32_t value, int64_t len,
int32_t* out_len);
Expand All @@ -61,4 +62,4 @@ const char* gdv_fn_castVARCHAR_float32_int64(int64_t context, float value, int64
int32_t* out_len);
const char* gdv_fn_castVARCHAR_float64_int64(int64_t context, double value, int64_t len,
int32_t* out_len);
}

2 changes: 0 additions & 2 deletions cpp/src/gandiva/gdv_function_stubs_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
// specific language governing permissions and limitations
// under the License.

extern "C" {
#include "gandiva/gdv_function_stubs.h"
}

#include <gmock/gmock.h>
#include <gtest/gtest.h>
Expand Down

0 comments on commit eece673

Please sign in to comment.