Skip to content

Commit 89d3a36

Browse files
BiteTheDDDDtdataroaring
authored andcommitted
[Bug] [Vectorized] add padding when load char type data (apache#9734)
1 parent 32a210f commit 89d3a36

File tree

8 files changed

+110
-3
lines changed

8 files changed

+110
-3
lines changed

‎be/CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ set(CXX_COMMON_FLAGS "${CXX_COMMON_FLAGS} -DBOOST_SYSTEM_NO_DEPRECATED")
396396
set(CXX_COMMON_FLAGS "${CXX_COMMON_FLAGS} -DBRPC_ENABLE_CPU_PROFILER")
397397

398398
if (USE_LLD)
399-
set(CXX_COMMON_FLAGS "${CXX_COMMON_FLAGS} -fuse-ld=lld")
399+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
400400
endif ()
401401

402402
if (USE_LIBCPP AND COMPILER_CLANG)

‎be/src/olap/memtable.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ void MemTable::_collect_vskiplist_results() {
282282
_input_mutable_block.swap(_output_mutable_block);
283283
//TODO(weixang):opt here.
284284
std::unique_ptr<vectorized::Block> empty_input_block =
285-
std::move(in_block.create_same_struct_block(0));
285+
in_block.create_same_struct_block(0);
286286
_output_mutable_block =
287287
vectorized::MutableBlock::build_mutable_block(empty_input_block.get());
288288
_output_mutable_block.clear_column_data();

‎be/src/vec/columns/column_string.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class ColumnString final : public COWHelper<IColumn, ColumnString> {
4545

4646
private:
4747
friend class COWHelper<IColumn, ColumnString>;
48+
friend class OlapBlockDataConvertor;
4849

4950
/// Maps i'th position to offset to i+1'th element. Last offset maps to the end of all chars (is the size of all chars).
5051
Offsets offsets;

‎be/src/vec/olap/olap_data_convertor.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,12 @@ Status OlapBlockDataConvertor::OlapColumnDataConvertorChar::convert_to_olap() {
358358

359359
assert(column_string);
360360

361+
// If column_string is not padded to full, we should do padding here.
362+
if (should_padding(column_string, _length)) {
363+
_column = clone_and_padding(column_string, _length);
364+
column_string = assert_cast<const vectorized::ColumnString*>(_column.get());
365+
}
366+
361367
const ColumnString::Char* char_data = column_string->get_chars().data();
362368
const ColumnString::Offset* offset_cur = column_string->get_offsets().data() + _row_pos;
363369
const ColumnString::Offset* offset_end = offset_cur + _num_rows;

‎be/src/vec/olap/olap_data_convertor.h‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
#pragma once
1919
#include "olap/tablet_schema.h"
20+
#include "vec/columns/column.h"
21+
#include "vec/columns/column_string.h"
22+
#include "vec/common/string_ref.h"
2023
#include "vec/core/block.h"
2124

2225
namespace doris::vectorized {
@@ -99,8 +102,36 @@ class OlapBlockDataConvertor {
99102
Status convert_to_olap() override;
100103

101104
private:
105+
static bool should_padding(const ColumnString* column, size_t padding_length) {
106+
// Check sum of data length, including terminating zero.
107+
return column->size() * (padding_length + 1) != column->chars.size();
108+
}
109+
110+
static ColumnPtr clone_and_padding(const ColumnString* input, size_t padding_length) {
111+
auto column = vectorized::ColumnString::create();
112+
auto padded_column =
113+
assert_cast<vectorized::ColumnString*>(column->assume_mutable().get());
114+
115+
column->offsets.resize(input->size());
116+
column->chars.resize(input->size() * (padding_length + 1));
117+
memset(padded_column->chars.data(), 0, input->size() * (padding_length + 1));
118+
119+
for (size_t i = 0; i < input->size(); i++) {
120+
column->offsets[i] = (i + 1) * (padding_length + 1);
121+
122+
auto str = input->get_data_at(i);
123+
if (str.size) {
124+
memcpy(padded_column->chars.data() + i * (padding_length + 1), str.data,
125+
str.size);
126+
}
127+
}
128+
129+
return column;
130+
}
131+
102132
size_t _length;
103133
PaddedPODArray<Slice> _slice;
134+
ColumnPtr _column = nullptr;
104135
};
105136

106137
class OlapColumnDataConvertorVarChar : public OlapColumnDataConvertorBase {

‎be/test/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ set(VEC_TEST_FILES
358358
vec/function/table_function_test.cpp
359359
vec/runtime/vdata_stream_test.cpp
360360
vec/utils/arrow_column_to_doris_column_test.cpp
361+
vec/olap/char_type_padding_test.cpp
361362
)
362363

363364
add_executable(doris_be_test

‎be/test/vec/exec/vtablet_sink_test.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class VTestInternalService : public PBackendService {
115115
void tablet_writer_add_block(google::protobuf::RpcController* controller,
116116
const PTabletWriterAddBlockRequest* request,
117117
PTabletWriterAddBlockResult* response,
118-
google::protobuf::Closure* done) {
118+
google::protobuf::Closure* done) override {
119119
brpc::ClosureGuard done_guard(done);
120120
{
121121
std::lock_guard<std::mutex> l(_lock);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
#include <gtest/gtest.h>
19+
20+
#include "vec/columns/column_string.h"
21+
#include "vec/olap/olap_data_convertor.h"
22+
23+
namespace doris::vectorized {
24+
25+
using ConvertorChar = OlapBlockDataConvertor::OlapColumnDataConvertorChar;
26+
27+
TEST(CharTypePaddingTest, CharTypePaddingFullTest) {
28+
auto input = ColumnString::create();
29+
30+
std::string str = "Allemande";
31+
size_t rows = 10;
32+
33+
for (size_t i = 0; i < rows; i++) {
34+
input->insert_data(str.data(), str.length());
35+
}
36+
EXPECT_FALSE(ConvertorChar::should_padding(input, str.length()));
37+
38+
input->insert_data(str.data(), str.length() - 1);
39+
EXPECT_TRUE(ConvertorChar::should_padding(input, str.length()));
40+
}
41+
42+
TEST(CharTypePaddingTest, CharTypePaddingDataTest) {
43+
auto input = ColumnString::create();
44+
45+
std::string str = "Allemande";
46+
47+
size_t rows = str.length();
48+
for (int i = 0; i < rows; i++) {
49+
input->insert_data(str.data(), str.length() - i);
50+
}
51+
52+
auto output = ConvertorChar::clone_and_padding(input, str.length());
53+
54+
for (int i = 0; i < rows; i++) {
55+
auto cell = output->get_data_at(i).to_string();
56+
EXPECT_EQ(cell.length(), str.length());
57+
58+
auto str_real = std::string(cell.data(), str.length() - i);
59+
auto str_expect = str.substr(0, str.length() - i);
60+
EXPECT_EQ(str_real, str_expect);
61+
62+
for (int j = str.length() - i; j < str.length(); j++) {
63+
EXPECT_EQ(cell[j], 0);
64+
}
65+
}
66+
}
67+
68+
} // namespace doris::vectorized

0 commit comments

Comments
 (0)