Skip to content

Commit

Permalink
remove some typeid_cast usage
Browse files Browse the repository at this point in the history
  • Loading branch information
BiteTheDDDDt committed Sep 10, 2024
1 parent 903c05b commit bfa2055
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 95 deletions.
34 changes: 20 additions & 14 deletions be/src/vec/columns/column_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,17 +600,19 @@ ColumnPtr ColumnArray::filter_string(const Filter& filt, ssize_t result_size_hin
size_t col_size = get_offsets().size();
column_match_filter_size(col_size, filt.size());

if (0 == col_size) return ColumnArray::create(data);
if (!col_size) {
return ColumnArray::create(data);
}

auto res = ColumnArray::create(data->clone_empty());

const ColumnString& src_string = typeid_cast<const ColumnString&>(*data);
const auto& src_string = assert_cast<const ColumnString&>(*data);
const ColumnString::Chars& src_chars = src_string.get_chars();
const auto& src_string_offsets = src_string.get_offsets();
const auto& src_offsets = get_offsets();

ColumnString::Chars& res_chars = typeid_cast<ColumnString&>(res->get_data()).get_chars();
auto& res_string_offsets = typeid_cast<ColumnString&>(res->get_data()).get_offsets();
ColumnString::Chars& res_chars = assert_cast<ColumnString&>(res->get_data()).get_chars();
auto& res_string_offsets = assert_cast<ColumnString&>(res->get_data()).get_offsets();
auto& res_offsets = res->get_offsets();

if (result_size_hint < 0) {
Expand Down Expand Up @@ -667,7 +669,7 @@ size_t ColumnArray::filter_string(const Filter& filter) {
return ColumnArray::create(data);
}

ColumnString& src_string = typeid_cast<ColumnString&>(*data);
auto& src_string = assert_cast<ColumnString&>(*data);
auto* src_chars = src_string.get_chars().data();
auto* src_string_offsets = src_string.get_offsets().data();
auto* src_offsets = get_offsets().data();
Expand Down Expand Up @@ -871,16 +873,18 @@ ColumnPtr ColumnArray::replicate_number(const IColumn::Offsets& replicate_offset

MutableColumnPtr res = clone_empty();

if (0 == col_size) return res;
if (!col_size) {
return res;
}

ColumnArray& res_arr = typeid_cast<ColumnArray&>(*res);
auto& res_arr = assert_cast<ColumnArray&>(*res);

const typename ColumnVector<T>::Container& src_data =
typeid_cast<const ColumnVector<T>&>(*data).get_data();
assert_cast<const ColumnVector<T>&>(*data).get_data();
const auto& src_offsets = get_offsets();

typename ColumnVector<T>::Container& res_data =
typeid_cast<ColumnVector<T>&>(res_arr.get_data()).get_data();
assert_cast<ColumnVector<T>&>(res_arr.get_data()).get_data();
auto& res_offsets = res_arr.get_offsets();

res_data.reserve(data->size() / col_size * replicate_offsets.back());
Expand Down Expand Up @@ -918,17 +922,19 @@ ColumnPtr ColumnArray::replicate_string(const IColumn::Offsets& replicate_offset

MutableColumnPtr res = clone_empty();

if (0 == col_size) return res;
if (!col_size) {
return res;
}

ColumnArray& res_arr = assert_cast<ColumnArray&, TypeCheckOnRelease::DISABLE>(*res);
auto& res_arr = assert_cast<ColumnArray&, TypeCheckOnRelease::DISABLE>(*res);

const ColumnString& src_string = typeid_cast<const ColumnString&>(*data);
const auto& src_string = assert_cast<const ColumnString&>(*data);
const ColumnString::Chars& src_chars = src_string.get_chars();
const auto& src_string_offsets = src_string.get_offsets();
const auto& src_offsets = get_offsets();

ColumnString::Chars& res_chars = typeid_cast<ColumnString&>(res_arr.get_data()).get_chars();
auto& res_string_offsets = typeid_cast<ColumnString&>(res_arr.get_data()).get_offsets();
ColumnString::Chars& res_chars = assert_cast<ColumnString&>(res_arr.get_data()).get_chars();
auto& res_string_offsets = assert_cast<ColumnString&>(res_arr.get_data()).get_offsets();
auto& res_offsets = res_arr.get_offsets();

res_chars.reserve(src_chars.size() / col_size * replicate_offsets.back());
Expand Down
7 changes: 1 addition & 6 deletions be/src/vec/columns/column_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,7 @@ ColumnMap::ColumnMap(MutableColumnPtr&& keys, MutableColumnPtr&& values, Mutable
: keys_column(std::move(keys)),
values_column(std::move(values)),
offsets_column(std::move(offsets)) {
const COffsets* offsets_concrete = typeid_cast<const COffsets*>(offsets_column.get());

if (!offsets_concrete) {
throw doris::Exception(doris::ErrorCode::INTERNAL_ERROR,
"offsets_column must be a ColumnUInt64.");
}
const auto* offsets_concrete = assert_cast<const COffsets*>(offsets_column.get());

if (!offsets_concrete->empty() && keys_column && values_column) {
auto last_offset = offsets_concrete->get_data().back();
Expand Down
23 changes: 0 additions & 23 deletions be/src/vec/common/typeid_cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,10 @@
#include "common/status.h"
#include "vec/common/demangle.h"

#define TYPEID_MAP(_A) \
template <> \
inline constexpr TypeIndex TypeToTypeIndex<_A> = TypeIndex::_A; \
template <> \
struct TypeIndexToTypeHelper<TypeIndex::_A> : std::true_type { \
using T = _A; \
};

/** Checks type by comparing typeid.
* The exact match of the type is checked. That is, cast to the ancestor will be unsuccessful.
* In the rest, behaves like a dynamic_cast.
*/
template <typename To, typename From>
requires std::is_reference_v<To>
To typeid_cast(From& from) {
try {
if (typeid(from) == typeid(To)) {
return static_cast<To>(from);
}
} catch (const std::exception& e) {
throw doris::Exception(doris::ErrorCode::BAD_CAST, e.what());
}

throw doris::Exception(doris::ErrorCode::BAD_CAST,
"Bad cast from type " + demangle(typeid(from).name()) + " to " +
demangle(typeid(To).name()));
}

template <typename To, typename From>
To typeid_cast(From* from) {
Expand Down
60 changes: 28 additions & 32 deletions be/src/vec/functions/function_ip.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,40 +57,36 @@ class FunctionIPv4NumToString : public IFunction {
using ColumnType = ColumnVector<ArgType>;
const ColumnPtr& column = argument.column;

if (const auto* col = typeid_cast<const ColumnType*>(column.get())) {
const typename ColumnType::Container& vec_in = col->get_data();
auto col_res = ColumnString::create();

ColumnString::Chars& vec_res = col_res->get_chars();
ColumnString::Offsets& offsets_res = col_res->get_offsets();

vec_res.resize(vec_in.size() *
(IPV4_MAX_TEXT_LENGTH + 1)); /// the longest value is: 255.255.255.255\0
offsets_res.resize(vec_in.size());
char* begin = reinterpret_cast<char*>(vec_res.data());
char* pos = begin;

auto null_map = ColumnUInt8::create(vec_in.size(), 0);
size_t src_size = std::min(sizeof(ArgType), (unsigned long)4);
for (size_t i = 0; i < vec_in.size(); ++i) {
auto value = vec_in[i];
if (value < IPV4_MIN_NUM_VALUE || value > IPV4_MAX_NUM_VALUE) {
offsets_res[i] = pos - begin;
null_map->get_data()[i] = 1;
} else {
format_ipv4(reinterpret_cast<const unsigned char*>(&vec_in[i]), src_size, pos);
offsets_res[i] = pos - begin;
}
}
const auto* col = assert_cast<const ColumnType*>(column.get());
const typename ColumnType::Container& vec_in = col->get_data();
auto col_res = ColumnString::create();

vec_res.resize(pos - begin);
block.replace_by_position(
result, ColumnNullable::create(std::move(col_res), std::move(null_map)));
return Status::OK();
} else {
return Status::RuntimeError("Illegal column {} of argument of function {}",
argument.column->get_name(), get_name());
ColumnString::Chars& vec_res = col_res->get_chars();
ColumnString::Offsets& offsets_res = col_res->get_offsets();

vec_res.resize(vec_in.size() *
(IPV4_MAX_TEXT_LENGTH + 1)); /// the longest value is: 255.255.255.255\0
offsets_res.resize(vec_in.size());
char* begin = reinterpret_cast<char*>(vec_res.data());
char* pos = begin;

auto null_map = ColumnUInt8::create(vec_in.size(), 0);
size_t src_size = std::min(sizeof(ArgType), (unsigned long)4);
for (size_t i = 0; i < vec_in.size(); ++i) {
auto value = vec_in[i];
if (value < IPV4_MIN_NUM_VALUE || value > IPV4_MAX_NUM_VALUE) {
offsets_res[i] = pos - begin;
null_map->get_data()[i] = 1;
} else {
format_ipv4(reinterpret_cast<const unsigned char*>(&vec_in[i]), src_size, pos);
offsets_res[i] = pos - begin;
}
}

vec_res.resize(pos - begin);
block.replace_by_position(result,
ColumnNullable::create(std::move(col_res), std::move(null_map)));
return Status::OK();
}

public:
Expand Down
6 changes: 1 addition & 5 deletions be/src/vec/functions/function_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,7 @@ class FunctionMap : public IFunction {
size_t num_element = arguments.size();

auto result_col = block.get_by_position(result).type->create_column();
auto* map_column = typeid_cast<ColumnMap*>(result_col.get());
if (!map_column) {
return Status::RuntimeError("unsupported types for function {} return {}", get_name(),
block.get_by_position(result).type->get_name());
}
auto* map_column = assert_cast<ColumnMap*>(result_col.get());

// map keys column
auto& result_col_map_keys_data = map_column->get_keys();
Expand Down
6 changes: 1 addition & 5 deletions be/src/vec/functions/function_struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,7 @@ class FunctionStruct : public IFunction {
Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
size_t result, size_t input_rows_count) const override {
auto result_col = block.get_by_position(result).type->create_column();
auto struct_column = typeid_cast<ColumnStruct*>(result_col.get());
if (!struct_column) {
return Status::RuntimeError("unsupported types for function {} return {}", get_name(),
block.get_by_position(result).type->get_name());
}
auto struct_column = assert_cast<ColumnStruct*>(result_col.get());
ColumnNumbers args_num;
for (size_t i = 0; i < arguments.size(); i++) {
if (Impl::pred(i)) {
Expand Down
1 change: 0 additions & 1 deletion be/src/vec/functions/function_totype.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include "vec/data_types/data_type_nullable.h"
#include "vec/data_types/data_type_number.h"
#include "vec/data_types/data_type_string.h"
#include "vec/functions/cast_type_to_either.h"
#include "vec/functions/function.h"
#include "vec/utils/util.hpp"

Expand Down
11 changes: 2 additions & 9 deletions be/src/vec/functions/if.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ size_t count_true_with_notnull(const ColumnPtr& col) {
return null_count;
}
} else {
const auto* bool_col = typeid_cast<const ColumnUInt8*>(col.get());
const auto* bool_col = assert_cast<const ColumnUInt8*>(col.get());
const auto* __restrict bool_data = bool_col->get_data().data();
return count - simd::count_zero_num((const int8_t*)bool_data, count);
}
Expand Down Expand Up @@ -612,7 +612,7 @@ class FunctionIf : public IFunction {
return Status::OK();
}

const auto* cond_col = typeid_cast<const ColumnUInt8*>(arg_cond.column.get());
const auto* cond_col = assert_cast<const ColumnUInt8*>(arg_cond.column.get());
const ColumnConst* cond_const_col =
check_and_get_column_const<ColumnVector<UInt8>>(arg_cond.column.get());

Expand All @@ -622,13 +622,6 @@ class FunctionIf : public IFunction {
return Status::OK();
}

if (!cond_col) {
return Status::InvalidArgument(
"Illegal column {} of first argument of function {},Must be ColumnUInt8 or "
"ColumnConstUInt8.",
arg_cond.column->get_name(), get_name());
}

WhichDataType which_type(arg_then.type);
if (which_type.is_int() || which_type.is_float()) {
Status status;
Expand Down

0 comments on commit bfa2055

Please sign in to comment.