Skip to content

Commit

Permalink
[Refactor] Remove wrapper_field (StarRocks#13191)
Browse files Browse the repository at this point in the history
wrapper_field is only used by ColumnMapping, but it is not necessary to use wrapper_field actually.
Before default_value will be stored in wrapper_field, and convert wrapper_field to string, then to datum, then push the datum into the Column.
In the PR, I convert the input default value string into Datum directly.
  • Loading branch information
imay authored Nov 10, 2022
1 parent 1b74fbf commit 07d1ed6
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 296 deletions.
1 change: 0 additions & 1 deletion be/src/storage/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ add_library(Storage STATIC
types.cpp
update_manager.cpp
utils.cpp
wrapper_field.cpp
compaction_utils.cpp
rowset/array_column_iterator.cpp
rowset/binary_plain_page.cpp
Expand Down
25 changes: 18 additions & 7 deletions be/src/storage/column_mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,36 @@

#pragma once

namespace starrocks {
#include <memory>

class WrapperField;
#include "column/datum.h"
#include "types/bitmap_value.h"
#include "types/hll.h"
#include "util/json.h"
#include "util/percentile_value.h"

struct ColumnMapping {
ColumnMapping() {}
virtual ~ColumnMapping() = default;
namespace starrocks {

struct ColumnMapping {
// <0: use default value
// >=0: use origin column
int32_t ref_column{-1};

// The base reader selects part of the column
// so it's different to ref_column
int32_t ref_base_reader_column_index = -1;
// normally for default value. stores values for filters
WrapperField* default_value{nullptr};
// materialize view transform function used in schema change
std::string materialized_function;

// the following data is used by default_value_datum, because default_value_datum only
// have the reference. We need to keep the content has the same life cycle as the
// default_value_datum;
std::unique_ptr<HyperLogLog> default_hll;
std::unique_ptr<BitmapValue> default_bitmap;
std::unique_ptr<PercentileValue> default_percentile;
std::unique_ptr<JsonValue> default_json;

vectorized::Datum default_value_datum;
};

typedef std::vector<ColumnMapping> SchemaMapping;
Expand Down
2 changes: 2 additions & 0 deletions be/src/storage/column_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

namespace starrocks {

class Field;

// struct that contains column data(null bitmap), data array in sub class.
class ColumnVectorBatch {
public:
Expand Down
4 changes: 0 additions & 4 deletions be/src/storage/olap_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,6 @@ struct HashOfVersion {
}
};

class Field;
class WrapperField;
using KeyRange = std::pair<WrapperField*, WrapperField*>;

// ReaderStatistics used to collect statistics when scan data from storage
struct OlapReaderStatistics {
int64_t create_segment_iter_ns = 0;
Expand Down
1 change: 0 additions & 1 deletion be/src/storage/rowset/column_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
#include "storage/rowset/zone_map_index.h"
#include "storage/types.h"
#include "storage/vectorized_column_predicate.h"
#include "storage/wrapper_field.h"
#include "util/compression/block_compression.h"
#include "util/rle_encoding.h"

Expand Down
1 change: 0 additions & 1 deletion be/src/storage/rowset/column_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class ColumnBlock;
class ColumnBlockView;
class ColumnVectorBatch;
class MemTracker;
class WrapperField;

namespace vectorized {
class ColumnPredicate;
Expand Down
127 changes: 32 additions & 95 deletions be/src/storage/schema_change_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "column/datum_convert.h"
#include "storage/chunk_helper.h"
#include "storage/wrapper_field.h"
#include "types/bitmap_value.h"
#include "types/hll.h"
#include "util/percentile_value.h"
Expand All @@ -16,9 +15,6 @@ ChunkChanger::ChunkChanger(const TabletSchema& tablet_schema) {
}

ChunkChanger::~ChunkChanger() {
for (auto it = _schema_mapping.begin(); it != _schema_mapping.end(); ++it) {
SAFE_DELETE(it->default_value);
}
_schema_mapping.clear();
}

Expand Down Expand Up @@ -321,48 +317,8 @@ bool ChunkChanger::change_chunk(ChunkPtr& base_chunk, ChunkPtr& new_chunk, const
}
} else {
ColumnPtr& new_col = new_chunk->get_column_by_index(i);
Datum dst_datum;
if (_schema_mapping[i].default_value->is_null()) {
dst_datum.set_null();
COLUMN_APPEND_DATUM();
} else {
Field new_field =
ChunkHelper::convert_field_to_format_v2(i, new_tablet_meta->tablet_schema().column(i));
const FieldType field_type = new_field.type()->type();
std::string tmp = _schema_mapping[i].default_value->to_string();
if (field_type == OLAP_FIELD_TYPE_HLL || field_type == OLAP_FIELD_TYPE_OBJECT ||
field_type == OLAP_FIELD_TYPE_PERCENTILE) {
switch (field_type) {
case OLAP_FIELD_TYPE_HLL: {
HyperLogLog hll(tmp);
dst_datum.set_hyperloglog(&hll);
COLUMN_APPEND_DATUM();
break;
}
case OLAP_FIELD_TYPE_OBJECT: {
BitmapValue bitmap(tmp);
dst_datum.set_bitmap(&bitmap);
COLUMN_APPEND_DATUM();
break;
}
case OLAP_FIELD_TYPE_PERCENTILE: {
PercentileValue percentile(tmp);
dst_datum.set_percentile(&percentile);
COLUMN_APPEND_DATUM();
break;
}
default:
LOG(WARNING) << "the column type is wrong. column_type: " << field_type_to_string(field_type);
return false;
}
} else {
Status st = datum_from_string(new_field.type().get(), &dst_datum, tmp, nullptr);
if (!st.ok()) {
LOG(WARNING) << "create datum from string failed: status=" << st;
return false;
}
COLUMN_APPEND_DATUM();
}
for (size_t row_index = 0; row_index < base_chunk->num_rows(); ++row_index) {
new_col->append_datum(_schema_mapping[i].default_value_datum);
}
}
}
Expand Down Expand Up @@ -463,47 +419,8 @@ bool ChunkChanger::change_chunk_v2(ChunkPtr& base_chunk, ChunkPtr& new_chunk, co
}
} else {
ColumnPtr& new_col = new_chunk->get_column_by_index(i);
Datum dst_datum;
if (_schema_mapping[i].default_value->is_null()) {
dst_datum.set_null();
COLUMN_APPEND_DATUM();
} else {
TypeInfoPtr new_type_info = new_schema.field(i)->type();
const FieldType field_type = new_type_info->type();
std::string tmp = _schema_mapping[i].default_value->to_string();
if (field_type == OLAP_FIELD_TYPE_HLL || field_type == OLAP_FIELD_TYPE_OBJECT ||
field_type == OLAP_FIELD_TYPE_PERCENTILE) {
switch (field_type) {
case OLAP_FIELD_TYPE_HLL: {
HyperLogLog hll(tmp);
dst_datum.set_hyperloglog(&hll);
COLUMN_APPEND_DATUM();
break;
}
case OLAP_FIELD_TYPE_OBJECT: {
BitmapValue bitmap(tmp);
dst_datum.set_bitmap(&bitmap);
COLUMN_APPEND_DATUM();
break;
}
case OLAP_FIELD_TYPE_PERCENTILE: {
PercentileValue percentile(tmp);
dst_datum.set_percentile(&percentile);
COLUMN_APPEND_DATUM();
break;
}
default:
LOG(WARNING) << "the column type is wrong. column_type: " << field_type_to_string(field_type);
return false;
}
} else {
Status st = datum_from_string(new_type_info.get(), &dst_datum, tmp, nullptr);
if (!st.ok()) {
LOG(WARNING) << "create datum from string failed: status=" << st;
return false;
}
COLUMN_APPEND_DATUM();
}
for (size_t row_index = 0; row_index < base_chunk->num_rows(); ++row_index) {
new_col->append_datum(_schema_mapping[i].default_value_datum);
}
}
}
Expand Down Expand Up @@ -703,16 +620,36 @@ Status SchemaChangeUtils::parse_request(const TabletSchema& base_schema, const T

Status SchemaChangeUtils::init_column_mapping(ColumnMapping* column_mapping, const TabletColumn& column_schema,
const std::string& value) {
column_mapping->default_value = WrapperField::create(column_schema);

if (column_mapping->default_value == nullptr) {
return Status::InternalError("malloc error");
}

if (column_schema.is_nullable() && value.length() == 0) {
column_mapping->default_value->set_null();
column_mapping->default_value_datum.set_null();
} else {
column_mapping->default_value->from_string(value);
auto field_type = column_schema.type();
auto type_info = get_type_info(column_schema);

switch (field_type) {
case OLAP_FIELD_TYPE_HLL: {
column_mapping->default_hll = std::make_unique<HyperLogLog>(value);
column_mapping->default_value_datum.set_hyperloglog(column_mapping->default_hll.get());
break;
}
case OLAP_FIELD_TYPE_OBJECT: {
column_mapping->default_bitmap = std::make_unique<BitmapValue>(value);
column_mapping->default_value_datum.set_bitmap(column_mapping->default_bitmap.get());
break;
}
case OLAP_FIELD_TYPE_PERCENTILE: {
column_mapping->default_percentile = std::make_unique<PercentileValue>(value);
column_mapping->default_value_datum.set_percentile(column_mapping->default_percentile.get());
break;
}
case OLAP_FIELD_TYPE_JSON: {
column_mapping->default_json = std::make_unique<JsonValue>(value);
column_mapping->default_value_datum.set_json(column_mapping->default_json.get());
break;
}
default:
return datum_from_string(type_info.get(), &column_mapping->default_value_datum, value, nullptr);
}
}

return Status::OK();
Expand Down
2 changes: 1 addition & 1 deletion be/src/storage/schema_change_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ChunkChanger {

ColumnMapping* get_mutable_column_mapping(size_t column_index);

SchemaMapping get_schema_mapping() const { return _schema_mapping; }
const SchemaMapping& get_schema_mapping() const { return _schema_mapping; }

std::vector<ColumnId>* get_mutable_selected_column_indexs() { return &_selected_column_indexs; }

Expand Down
1 change: 0 additions & 1 deletion be/src/storage/tablet_updates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include "storage/types.h"
#include "storage/update_compaction_state.h"
#include "storage/update_manager.h"
#include "storage/wrapper_field.h"
#include "util/defer_op.h"
#include "util/pretty_printer.h"
#include "util/scoped_cleanup.h"
Expand Down
75 changes: 0 additions & 75 deletions be/src/storage/wrapper_field.cpp

This file was deleted.

Loading

0 comments on commit 07d1ed6

Please sign in to comment.