Skip to content

Commit

Permalink
fix json cast
Browse files Browse the repository at this point in the history
  • Loading branch information
eldenmoon committed Sep 10, 2024
1 parent 480d8f0 commit 9e8806d
Showing 1 changed file with 18 additions and 34 deletions.
52 changes: 18 additions & 34 deletions be/src/vec/functions/function_cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ struct ConvertNothingToJsonb {
}
};

template <TypeIndex type_index, typename ColumnType>
template <TypeIndex type_index, typename ColumnType, typename ToDataType>
struct ConvertImplFromJsonb {
static Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
const size_t result, size_t input_rows_count) {
Expand All @@ -851,16 +851,12 @@ struct ConvertImplFromJsonb {
auto& null_map = null_map_col->get_data();
auto col_to = ColumnType::create();

//IColumn & col_to = *res;
// size_t size = col_from.size();
col_to->reserve(input_rows_count);
auto& res = col_to->get_data();
res.resize(input_rows_count);

for (size_t i = 0; i < input_rows_count; ++i) {
const auto& val = column_string->get_data_at(i);
// ReadBuffer read_buffer((char*)(val.data), val.size);
// RETURN_IF_ERROR(data_type_to->from_string(read_buffer, col_to));

if (val.size == 0) {
null_map[i] = 1;
Expand All @@ -883,6 +879,15 @@ struct ConvertImplFromJsonb {
res[i] = 0;
continue;
}
if (value->isString()) {
// convert by parse
const auto& data = static_cast<const JsonbBlobVal*>(value)->getBlob();
size_t len = static_cast<const JsonbBlobVal*>(value)->getBlobLen();
ReadBuffer rb((char*)(data), len);
bool parsed = try_parse_impl<ToDataType>(res[i], rb, context);
null_map[i] = !parsed;
continue;
}
if constexpr (type_index == TypeIndex::UInt8) {
// cast from json value to boolean type
if (value->isTrue()) {
Expand Down Expand Up @@ -915,17 +920,6 @@ struct ConvertImplFromJsonb {
res[i] = 1;
} else if (value->isFalse()) {
res[i] = 0;
} else if (value->isString()) {
const auto& data = static_cast<const JsonbBlobVal*>(value)->getBlob();
size_t len = static_cast<const JsonbBlobVal*>(value)->getBlobLen();
int128_t val = 0;
ReadBuffer rb((char*)(data), len);
if (!read_int_text_impl(val, rb)) {
return Status::InvalidArgument(
"parse number fail, string: '{}'",
std::string(rb.position(), rb.count()).c_str());
}
res[i] = static_cast<ColumnType::value_type>(val);
} else {
null_map[i] = 1;
res[i] = 0;
Expand All @@ -943,17 +937,6 @@ struct ConvertImplFromJsonb {
res[i] = 0;
} else if (value->isInt()) {
res[i] = ((const JsonbIntVal*)value)->val();
} else if (value->isString()) {
const auto& data = static_cast<const JsonbBlobVal*>(value)->getBlob();
size_t len = static_cast<const JsonbBlobVal*>(value)->getBlobLen();
double val = 0;
ReadBuffer rb((char*)(data), len);
if (!read_float_text_fast_impl(val, rb)) {
return Status::InvalidArgument(
"parse number fail, string: '{}'",
std::string(rb.position(), rb.count()).c_str());
}
res[i] = static_cast<ColumnType::value_type>(val);
} else {
null_map[i] = 1;
res[i] = 0;
Expand Down Expand Up @@ -1999,19 +1982,20 @@ class FunctionCast final : public IFunctionBase {
bool jsonb_string_as_string) const {
switch (to_type->get_type_id()) {
case TypeIndex::UInt8:
return &ConvertImplFromJsonb<TypeIndex::UInt8, ColumnUInt8>::execute;
return &ConvertImplFromJsonb<TypeIndex::UInt8, ColumnUInt8, DataTypeUInt8>::execute;
case TypeIndex::Int8:
return &ConvertImplFromJsonb<TypeIndex::Int8, ColumnInt8>::execute;
return &ConvertImplFromJsonb<TypeIndex::Int8, ColumnInt8, DataTypeInt8>::execute;
case TypeIndex::Int16:
return &ConvertImplFromJsonb<TypeIndex::Int16, ColumnInt16>::execute;
return &ConvertImplFromJsonb<TypeIndex::Int16, ColumnInt16, DataTypeInt16>::execute;
case TypeIndex::Int32:
return &ConvertImplFromJsonb<TypeIndex::Int32, ColumnInt32>::execute;
return &ConvertImplFromJsonb<TypeIndex::Int32, ColumnInt32, DataTypeInt32>::execute;
case TypeIndex::Int64:
return &ConvertImplFromJsonb<TypeIndex::Int64, ColumnInt64>::execute;
return &ConvertImplFromJsonb<TypeIndex::Int64, ColumnInt64, DataTypeInt64>::execute;
case TypeIndex::Int128:
return &ConvertImplFromJsonb<TypeIndex::Int128, ColumnInt128>::execute;
return &ConvertImplFromJsonb<TypeIndex::Int128, ColumnInt128, DataTypeInt128>::execute;
case TypeIndex::Float64:
return &ConvertImplFromJsonb<TypeIndex::Float64, ColumnFloat64>::execute;
return &ConvertImplFromJsonb<TypeIndex::Float64, ColumnFloat64,
DataTypeFloat64>::execute;
case TypeIndex::String:
if (!jsonb_string_as_string) {
// Conversion from String through parsing.
Expand Down

0 comments on commit 9e8806d

Please sign in to comment.