Skip to content

Commit 50f2145

Browse files
committed
Improve performance of the whole JSON column reading in Wide parts from S3
1 parent 01f7623 commit 50f2145

42 files changed

Lines changed: 578 additions & 37 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/Common/CurrentMetrics.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@
243243
M(ParquetEncoderThreads, "Number of threads in ParquetBlockOutputFormat thread pool.") \
244244
M(ParquetEncoderThreadsActive, "Number of threads in ParquetBlockOutputFormat thread pool running a task.") \
245245
M(ParquetEncoderThreadsScheduled, "Number of queued or active jobs in ParquetBlockOutputFormat thread pool.") \
246+
M(MergeTreeSubcolumnsReaderThreads, "Number of threads in the thread pool used for subcolumns reading in MergeTree.") \
247+
M(MergeTreeSubcolumnsReaderThreadsActive, "Number of threads in the thread pool used for subcolumns reading in MergeTree running a task.") \
248+
M(MergeTreeSubcolumnsReaderThreadsScheduled, "Number of queued or active jobs in the thread pool used for subcolumns reading in MergeTree.") \
246249
M(DWARFReaderThreads, "Number of threads in the DWARFBlockInputFormat thread pool.") \
247250
M(DWARFReaderThreadsActive, "Number of threads in the DWARFBlockInputFormat thread pool running a task.") \
248251
M(DWARFReaderThreadsScheduled, "Number of queued or active jobs in the DWARFBlockInputFormat thread pool.") \

‎src/Core/Settings.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,12 @@ The cache of uncompressed blocks stores data extracted for queries. ClickHouse u
12971297
Possible values:
12981298

12991299
- Any positive integer.
1300+
)", 0) \
1301+
DECLARE(UInt64, merge_tree_use_deserialization_prefixes_cache, true, R"(
1302+
Enables caching of deserialization prefixes during reading from Wide parts in MergeTree.
1303+
)", 0) \
1304+
DECLARE(UInt64Auto, merge_tree_prefixes_deserialization_thread_pool_size, "auto", R"(
1305+
The number of threads used in a thread pool for parallel prefixes reading in Wide parts in MergeTree.
13001306
)", 0) \
13011307
DECLARE(Bool, do_not_merge_across_partitions_select_final, false, R"(
13021308
Merge parts only in one partition in select final

‎src/Core/SettingsChangesHistory.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ const VersionToSettingsChangesMap & getSettingsChangesHistory()
9898
{"validate_enum_literals_in_operators", false, false, "A new setting"},
9999
{"allow_experimental_kusto_dialect", true, false, "A new setting"},
100100
{"allow_experimental_prql_dialect", true, false, "A new setting"},
101+
{"merge_tree_use_deserialization_prefixes_cache", true, true, "A new setting to control the usage of deserialization prefixes cache in MergeTree"},
102+
{"merge_tree_prefixes_deserialization_thread_pool_size", "auto", "auto", "A new setting controlling the size of the thread pool used in prefixes deserialization in MergeTree"},
101103
});
102104
addSettingsChanges(settings_changes_history, "24.12",
103105
{
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#pragma once
2+
3+
#include <functional>
4+
#include <atomic>
5+
#include <condition_variable>
6+
7+
namespace DB
8+
{
9+
10+
/// Simple task that can be executed and waited by several threads.
11+
class DeserializationTask
12+
{
13+
public:
14+
15+
DeserializationTask(std::function<void()> deserialize_) : deserialize(std::move(deserialize_))
16+
{
17+
}
18+
19+
/// Try to execute, if another thread already started execution, just return.
20+
void tryExecute()
21+
{
22+
bool expected = false;
23+
if (!started.compare_exchange_strong(expected, true))
24+
return;
25+
26+
try
27+
{
28+
deserialize();
29+
}
30+
catch (...)
31+
{
32+
exception = std::current_exception();
33+
}
34+
35+
finished = true;
36+
finished.notify_all();
37+
}
38+
39+
/// Wait until task is executed and rethrow an exception if any.
40+
void wait()
41+
{
42+
finished.wait(false);
43+
if (exception)
44+
std::rethrow_exception(exception);
45+
}
46+
47+
private:
48+
std::function<void()> deserialize;
49+
std::atomic_bool started = false;
50+
std::atomic_bool finished = false;
51+
std::exception_ptr exception;
52+
};
53+
54+
}

‎src/DataTypes/Serializations/ISerialization.cpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,24 @@ bool ISerialization::isDynamicOrObjectStructureSubcolumn(const DB::ISerializatio
450450
return path[path.size() - 1].type == SubstreamType::DynamicStructure || path[path.size() - 1].type == SubstreamType::ObjectStructure;
451451
}
452452

453+
bool ISerialization::hasPrefix(const DB::ISerialization::SubstreamPath & path)
454+
{
455+
if (path.empty())
456+
return false;
457+
458+
switch (path[path.size() - 1].type)
459+
{
460+
case SubstreamType::DynamicStructure: [[fallthrough]];
461+
case SubstreamType::ObjectStructure: [[fallthrough]];
462+
case SubstreamType::DeprecatedObjectStructure: [[fallthrough]];
463+
case SubstreamType::DictionaryKeys: [[fallthrough]];
464+
case SubstreamType::VariantDiscriminators:
465+
return true;
466+
default:
467+
return false;
468+
}
469+
}
470+
453471
ISerialization::SubstreamData ISerialization::createFromPath(const SubstreamPath & path, size_t prefix_len)
454472
{
455473
assert(prefix_len <= path.size());

‎src/DataTypes/Serializations/ISerialization.h‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <base/demangle.h>
66
#include <Common/typeid_cast.h>
77
#include <Columns/IColumn.h>
8+
#include <Common/ThreadPool_fwd.h>
89

910
#include <boost/noncopyable.hpp>
1011
#include <unordered_map>
@@ -106,7 +107,12 @@ class ISerialization : private boost::noncopyable, public std::enable_shared_fro
106107

107108
struct DeserializeBinaryBulkState
108109
{
110+
DeserializeBinaryBulkState() {}
111+
DeserializeBinaryBulkState(const DeserializeBinaryBulkState &) = default;
112+
109113
virtual ~DeserializeBinaryBulkState() = default;
114+
115+
virtual std::shared_ptr<DeserializeBinaryBulkState> clone() const { return std::make_shared<DeserializeBinaryBulkState>(); }
110116
};
111117

112118
using SerializeBinaryBulkStatePtr = std::shared_ptr<SerializeBinaryBulkState>;
@@ -310,6 +316,11 @@ class ISerialization : private boost::noncopyable, public std::enable_shared_fro
310316
double avg_value_size_hint = 0;
311317

312318
bool object_and_dynamic_read_statistics = false;
319+
320+
/// Callback to start prefetches for specific substreams during prefixes deserialization.
321+
StreamCallback prefixes_prefetch_callback;
322+
/// ThreadPool that can be used to read prefixes of subcolumns in parallel.
323+
ThreadPool * prefixes_deserialization_thread_pool = nullptr;
313324
};
314325

315326
/// Call before serializeBinaryBulkWithMultipleStreams chain to write something before first mark.
@@ -329,8 +340,12 @@ class ISerialization : private boost::noncopyable, public std::enable_shared_fro
329340
/// Call before before deserializeBinaryBulkWithMultipleStreams chain to get DeserializeBinaryBulkStatePtr.
330341
virtual void deserializeBinaryBulkStatePrefix(
331342
DeserializeBinaryBulkSettings & /*settings*/,
332-
DeserializeBinaryBulkStatePtr & /*state*/,
333-
SubstreamsDeserializeStatesCache * /*cache*/) const {}
343+
DeserializeBinaryBulkStatePtr & state,
344+
SubstreamsDeserializeStatesCache * /*cache*/) const
345+
{
346+
/// Create empty state to avoid working with nullptr state.
347+
state = std::make_shared<DeserializeBinaryBulkState>();
348+
}
334349

335350
/** 'offset' and 'limit' are used to specify range.
336351
* limit = 0 - means no limit.
@@ -469,6 +484,9 @@ class ISerialization : private boost::noncopyable, public std::enable_shared_fro
469484
static bool isLowCardinalityDictionarySubcolumn(const SubstreamPath & path);
470485
static bool isDynamicOrObjectStructureSubcolumn(const SubstreamPath & path);
471486

487+
/// Return true if the specified path contains prefix that should be deserialized in deserializeBinaryBulkStatePrefix.
488+
static bool hasPrefix(const SubstreamPath & path);
489+
472490
protected:
473491
template <typename State, typename StatePtr>
474492
State * checkAndGetState(const StatePtr & state) const;

‎src/DataTypes/Serializations/SerializationDynamic.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ struct DeserializeBinaryBulkStateDynamic : public ISerialization::DeserializeBin
4949
SerializationPtr variant_serialization;
5050
ISerialization::DeserializeBinaryBulkStatePtr variant_state;
5151
ISerialization::DeserializeBinaryBulkStatePtr structure_state;
52+
53+
ISerialization::DeserializeBinaryBulkStatePtr clone() const override
54+
{
55+
auto new_state = std::make_shared<DeserializeBinaryBulkStateDynamic>(*this);
56+
new_state->variant_state = variant_state->clone();
57+
new_state->structure_state = structure_state->clone();
58+
return new_state;
59+
}
5260
};
5361

5462
void SerializationDynamic::enumerateStreams(

‎src/DataTypes/Serializations/SerializationDynamic.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ class SerializationDynamic : public ISerialization
132132
: structure_version(structure_version_)
133133
{
134134
}
135+
136+
ISerialization::DeserializeBinaryBulkStatePtr clone() const override
137+
{
138+
return std::make_shared<DeserializeBinaryBulkStateDynamicStructure>(*this);
139+
}
135140
};
136141

137142
size_t max_dynamic_types;

‎src/DataTypes/Serializations/SerializationDynamicElement.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ struct DeserializeBinaryBulkStateDynamicElement : public ISerialization::Deseria
2626
ISerialization::DeserializeBinaryBulkStatePtr variant_element_state;
2727
bool read_from_shared_variant;
2828
ColumnPtr shared_variant;
29+
30+
31+
ISerialization::DeserializeBinaryBulkStatePtr clone() const override
32+
{
33+
auto new_state = std::make_shared<DeserializeBinaryBulkStateDynamicElement>(*this);
34+
new_state->structure_state = structure_state->clone();
35+
new_state->variant_element_state = variant_element_state->clone();
36+
return new_state;
37+
}
2938
};
3039

3140
void SerializationDynamicElement::enumerateStreams(

‎src/DataTypes/Serializations/SerializationLowCardinality.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ struct DeserializeStateLowCardinality : public ISerialization::DeserializeBinary
219219
bool need_update_dictionary = false;
220220

221221
explicit DeserializeStateLowCardinality(UInt64 key_version_) : key_version(key_version_) {}
222+
223+
ISerialization::DeserializeBinaryBulkStatePtr clone() const override
224+
{
225+
return std::make_shared<DeserializeStateLowCardinality>(*this);
226+
}
222227
};
223228

224229
void SerializationLowCardinality::serializeBinaryBulkStatePrefix(

0 commit comments

Comments
 (0)