Skip to content

Commit

Permalink
[Refactor] Remove unused fields from descriptors (StarRocks#13082)
Browse files Browse the repository at this point in the history
Just remove unused fields, no changes to any logical
  • Loading branch information
imay authored Nov 8, 2022
1 parent ab50e1f commit 26f06ce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 213 deletions.
139 changes: 14 additions & 125 deletions be/src/runtime/descriptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,34 +50,30 @@ SlotDescriptor::SlotDescriptor(const TSlotDescriptor& tdesc)
: _id(tdesc.id),
_type(TypeDescriptor::from_thrift(tdesc.slotType)),
_parent(tdesc.parent),
_col_pos(tdesc.columnPos),
_tuple_offset(tdesc.byteOffset),
_null_indicator_offset(tdesc.nullIndicatorByte, tdesc.nullIndicatorBit),
_col_name(tdesc.colName),
_slot_idx(tdesc.slotIdx),
_slot_size(_type.get_slot_size()),
_field_idx(-1),
_is_materialized(tdesc.isMaterialized) {}

SlotDescriptor::SlotDescriptor(const PSlotDescriptor& pdesc)
: _id(pdesc.id()),
_type(TypeDescriptor::from_protobuf(pdesc.slot_type())),
_parent(pdesc.parent()),
_col_pos(pdesc.column_pos()),
_tuple_offset(pdesc.byte_offset()),
_null_indicator_offset(pdesc.null_indicator_byte(), pdesc.null_indicator_bit()),
_col_name(pdesc.col_name()),
_slot_idx(pdesc.slot_idx()),
_slot_size(_type.get_slot_size()),
_field_idx(-1),
_is_materialized(pdesc.is_materialized()) {}

void SlotDescriptor::to_protobuf(PSlotDescriptor* pslot) const {
pslot->set_id(_id);
pslot->set_parent(_parent);
*pslot->mutable_slot_type() = _type.to_protobuf();
pslot->set_column_pos(_col_pos);
pslot->set_byte_offset(_tuple_offset);
// NOTE: column_pos is not used anymore, use default value 0
pslot->set_column_pos(0);
// NOTE: _tuple_offset is not used anymore, use default value 0.
pslot->set_byte_offset(0);
pslot->set_null_indicator_byte(_null_indicator_offset.byte_offset);
pslot->set_null_indicator_bit(_null_indicator_offset.bit_offset);
pslot->set_col_name(_col_name);
Expand All @@ -87,21 +83,17 @@ void SlotDescriptor::to_protobuf(PSlotDescriptor* pslot) const {

std::string SlotDescriptor::debug_string() const {
std::stringstream out;
out << "Slot(id=" << _id << " type=" << _type << " col=" << _col_pos << " name=" << _col_name
<< " offset=" << _tuple_offset << " null=" << _null_indicator_offset.debug_string() << ")";
out << "Slot(id=" << _id << " type=" << _type << " name=" << _col_name
<< " null=" << _null_indicator_offset.debug_string() << ")";
return out.str();
}

TableDescriptor::TableDescriptor(const TTableDescriptor& tdesc)
: _name(tdesc.tableName),
_database(tdesc.dbName),
_id(tdesc.id),
_num_cols(tdesc.numCols),
_num_clustering_cols(tdesc.numClusteringCols) {}
: _name(tdesc.tableName), _database(tdesc.dbName), _id(tdesc.id) {}

std::string TableDescriptor::debug_string() const {
std::stringstream out;
out << "#cols=" << _num_cols << " #clustering_cols=" << _num_clustering_cols;
out << "#name=" << _name;
return out.str();
}

Expand Down Expand Up @@ -305,66 +297,25 @@ std::string JDBCTableDescriptor::debug_string() const {
}

TupleDescriptor::TupleDescriptor(const TTupleDescriptor& tdesc)
: _id(tdesc.id),
_table_desc(nullptr),
_byte_size(tdesc.byteSize),
_num_null_bytes(tdesc.numNullBytes),
_has_varlen_slots(false) {
if (false == tdesc.__isset.numNullSlots) {
//be compatible for existing tables with no NULL value
_num_null_slots = 0;
} else {
_num_null_slots = tdesc.numNullSlots;
}
}
: _id(tdesc.id), _table_desc(nullptr), _byte_size(tdesc.byteSize) {}

TupleDescriptor::TupleDescriptor(const PTupleDescriptor& pdesc)
: _id(pdesc.id()),
_table_desc(nullptr),
_byte_size(pdesc.byte_size()),
_num_null_bytes(pdesc.num_null_bytes()),

_has_varlen_slots(false) {
if (!pdesc.has_num_null_slots()) {
//be compatible for existing tables with no NULL value
_num_null_slots = 0;
} else {
_num_null_slots = pdesc.num_null_slots();
}
}
: _id(pdesc.id()), _table_desc(nullptr), _byte_size(pdesc.byte_size()) {}

void TupleDescriptor::add_slot(SlotDescriptor* slot) {
_slots.push_back(slot);
_decoded_slots.push_back(slot);
}

std::vector<SlotDescriptor*> TupleDescriptor::slots_ordered_by_idx() const {
std::vector<SlotDescriptor*> sorted_slots(slots().size());
for (SlotDescriptor* slot : slots()) {
sorted_slots[slot->_slot_idx] = slot;
}
return sorted_slots;
}

bool TupleDescriptor::layout_equals(const TupleDescriptor& other_desc) const {
if (byte_size() != other_desc.byte_size()) return false;
if (slots().size() != other_desc.slots().size()) return false;

std::vector<SlotDescriptor*> slots = slots_ordered_by_idx();
std::vector<SlotDescriptor*> other_slots = other_desc.slots_ordered_by_idx();
for (int i = 0; i < slots.size(); ++i) {
if (!slots[i]->layout_equals(*other_slots[i])) return false;
}
return true;
}

void TupleDescriptor::to_protobuf(PTupleDescriptor* ptuple) const {
ptuple->Clear();
ptuple->set_id(_id);
ptuple->set_byte_size(_byte_size);
ptuple->set_num_null_bytes(_num_null_bytes);
// NOTE: _num_null_bytes is not used, set a default value 1
ptuple->set_num_null_bytes(1);
ptuple->set_table_id(-1);
ptuple->set_num_null_slots(_num_null_slots);
// NOTE: _num_null_slots is not used, set a default value 1
ptuple->set_num_null_slots(1);
}

std::string TupleDescriptor::debug_string() const {
Expand All @@ -383,7 +334,6 @@ std::string TupleDescriptor::debug_string() const {
}

out << "]";
out << " has_varlen_slots=" << _has_varlen_slots;
out << ")";
return out.str();
}
Expand All @@ -393,24 +343,19 @@ RowDescriptor::RowDescriptor(const DescriptorTbl& desc_tbl, const std::vector<TT
: _tuple_idx_nullable_map(nullable_tuples) {
DCHECK(nullable_tuples.size() == row_tuples.size());
DCHECK_GT(row_tuples.size(), 0);
_num_null_slots = 0;

for (int row_tuple : row_tuples) {
TupleDescriptor* tupleDesc = desc_tbl.get_tuple_descriptor(row_tuple);
_num_null_slots += tupleDesc->num_null_slots();
_tuple_desc_map.push_back(tupleDesc);
DCHECK(_tuple_desc_map.back() != nullptr);
}
_num_null_bytes = (_num_null_slots + 7) / 8;

init_tuple_idx_map();
init_has_varlen_slots();
}

RowDescriptor::RowDescriptor(TupleDescriptor* tuple_desc, bool is_nullable)
: _tuple_desc_map(1, tuple_desc), _tuple_idx_nullable_map(1, is_nullable) {
init_tuple_idx_map();
init_has_varlen_slots();
}

void RowDescriptor::init_tuple_idx_map() {
Expand All @@ -426,45 +371,11 @@ void RowDescriptor::init_tuple_idx_map() {
}
}

void RowDescriptor::init_has_varlen_slots() {
_has_varlen_slots = false;
for (auto& i : _tuple_desc_map) {
if (i->has_varlen_slots()) {
_has_varlen_slots = true;
break;
}
}
}

int RowDescriptor::get_row_size() const {
int size = 0;

for (auto i : _tuple_desc_map) {
size += i->byte_size();
}

return size;
}

int RowDescriptor::get_tuple_idx(TupleId id) const {
DCHECK_LT(id, _tuple_idx_map.size()) << "RowDescriptor: " << debug_string();
return _tuple_idx_map[id];
}

bool RowDescriptor::tuple_is_nullable(int tuple_idx) const {
DCHECK_LT(tuple_idx, _tuple_idx_nullable_map.size()) << "RowDescriptor: " << debug_string();
return _tuple_idx_nullable_map[tuple_idx];
}

bool RowDescriptor::is_any_tuple_nullable() const {
for (bool i : _tuple_idx_nullable_map) {
if (i) {
return true;
}
}
return false;
}

void RowDescriptor::to_thrift(std::vector<TTupleId>* row_tuple_ids) {
row_tuple_ids->clear();

Expand Down Expand Up @@ -510,19 +421,6 @@ bool RowDescriptor::equals(const RowDescriptor& other_desc) const {
return true;
}

bool RowDescriptor::layout_is_prefix_of(const RowDescriptor& other_desc) const {
if (_tuple_desc_map.size() > other_desc._tuple_desc_map.size()) return false;
for (int i = 0; i < _tuple_desc_map.size(); ++i) {
if (!_tuple_desc_map[i]->layout_equals(*other_desc._tuple_desc_map[i])) return false;
}
return true;
}

bool RowDescriptor::layout_equals(const RowDescriptor& other_desc) const {
if (_tuple_desc_map.size() != other_desc._tuple_desc_map.size()) return false;
return layout_is_prefix_of(other_desc);
}

std::string RowDescriptor::debug_string() const {
std::stringstream ss;

Expand Down Expand Up @@ -683,15 +581,6 @@ void DescriptorTbl::get_tuple_descs(std::vector<TupleDescriptor*>* descs) const
}
}

bool SlotDescriptor::layout_equals(const SlotDescriptor& other_desc) const {
if (type().type != other_desc.type().type) return false;
if (is_nullable() != other_desc.is_nullable()) return false;
if (slot_size() != other_desc.slot_size()) return false;
if (tuple_offset() != other_desc.tuple_offset()) return false;
if (!null_indicator_offset().equals(other_desc.null_indicator_offset())) return false;
return true;
}

std::string DescriptorTbl::debug_string() const {
std::stringstream out;
out << "tuples:\n";
Expand Down
Loading

0 comments on commit 26f06ce

Please sign in to comment.