Skip to content

Commit 02faba4

Browse files
authored
Merge pull request ClickHouse#77446 from ClickHouse/small-serialization-refactoring
Remove copy-paste code
2 parents 82fa2ca + a7d4ee1 commit 02faba4

File tree

2 files changed

+24
-44
lines changed

2 files changed

+24
-44
lines changed

src/Core/Joins.cpp

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ JoinKind reverseJoinKind(JoinKind kind)
9797
void serializeJoinKind(JoinKind kind, WriteBuffer & out)
9898
{
9999
uint8_t val = uint8_t(kind);
100+
chassert(val <= JoinKindMax);
100101
writeIntBinary(val, out);
101102
}
102103

@@ -105,27 +106,16 @@ JoinKind deserializeJoinKind(ReadBuffer & in)
105106
uint8_t val;
106107
readIntBinary(val, in);
107108

108-
if (val == uint8_t(JoinKind::Inner))
109-
return JoinKind::Inner;
110-
if (val == uint8_t(JoinKind::Left))
111-
return JoinKind::Left;
112-
if (val == uint8_t(JoinKind::Right))
113-
return JoinKind::Right;
114-
if (val == uint8_t(JoinKind::Full))
115-
return JoinKind::Full;
116-
if (val == uint8_t(JoinKind::Cross))
117-
return JoinKind::Cross;
118-
if (val == uint8_t(JoinKind::Comma))
119-
return JoinKind::Comma;
120-
if (val == uint8_t(JoinKind::Paste))
121-
return JoinKind::Paste;
122-
123-
throw Exception(ErrorCodes::INCORRECT_DATA, "Cannot convert {} to JoinKind", UInt16(val));
109+
if (val > JoinKindMax)
110+
throw Exception(ErrorCodes::INCORRECT_DATA, "Cannot convert {} to JoinKind", val);
111+
112+
return static_cast<JoinKind>(val);
124113
}
125114

126115
void serializeJoinStrictness(JoinStrictness strictness, WriteBuffer & out)
127116
{
128117
uint8_t val = uint8_t(strictness);
118+
chassert(val <= JoinStrictnessMax);
129119
writeIntBinary(val, out);
130120
}
131121

@@ -134,43 +124,27 @@ JoinStrictness deserializeJoinStrictness(ReadBuffer & in)
134124
uint8_t val;
135125
readIntBinary(val, in);
136126

137-
if (val == uint8_t(JoinStrictness::Unspecified))
138-
return JoinStrictness::Unspecified;
139-
if (val == uint8_t(JoinStrictness::RightAny))
140-
return JoinStrictness::RightAny;
141-
if (val == uint8_t(JoinStrictness::Any))
142-
return JoinStrictness::Any;
143-
if (val == uint8_t(JoinStrictness::All))
144-
return JoinStrictness::All;
145-
if (val == uint8_t(JoinStrictness::Asof))
146-
return JoinStrictness::Asof;
147-
if (val == uint8_t(JoinStrictness::Semi))
148-
return JoinStrictness::Semi;
149-
if (val == uint8_t(JoinStrictness::Anti))
150-
return JoinStrictness::Anti;
151-
152-
throw Exception(ErrorCodes::INCORRECT_DATA, "Cannot convert {} to JoinStrictness", UInt16(val));
127+
if (val > JoinStrictnessMax)
128+
throw Exception(ErrorCodes::INCORRECT_DATA, "Cannot convert {} to JoinStrictness", val);
129+
130+
return static_cast<JoinStrictness>(val);
153131
}
154132

155133
void serializeJoinLocality(JoinLocality locality, WriteBuffer & out)
156134
{
157135
uint8_t val = uint8_t(locality);
136+
chassert(val <= JoinLocalityMax);
158137
writeIntBinary(val, out);
159138
}
160139
JoinLocality deserializeJoinLocality(ReadBuffer & in)
161140
{
162141
uint8_t val;
163142
readIntBinary(val, in);
164143

165-
if (val == uint8_t(JoinLocality::Unspecified))
166-
return JoinLocality::Unspecified;
167-
if (val == uint8_t(JoinLocality::Local))
168-
return JoinLocality::Local;
169-
if (val == uint8_t(JoinLocality::Global))
170-
return JoinLocality::Global;
171-
144+
if (val > JoinLocalityMax)
145+
throw Exception(ErrorCodes::INCORRECT_DATA, "Cannot convert {} to JoinLocality", UInt16(val));
172146

173-
throw Exception(ErrorCodes::INCORRECT_DATA, "Cannot convert {} to JoinLocality", UInt16(val));
147+
return static_cast<JoinLocality>(val);
174148
}
175149

176150
}

src/Core/Joins.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class WriteBuffer;
1111
/// Join method.
1212
enum class JoinKind : uint8_t
1313
{
14-
Inner, /// Leave only rows that was JOINed.
14+
Inner = 0, /// Leave only rows that was JOINed.
1515
Left, /// If in "right" table there is no corresponding rows, use default values instead.
1616
Right,
1717
Full,
@@ -20,6 +20,8 @@ enum class JoinKind : uint8_t
2020
Paste, /// Used to join parts without `ON` clause.
2121
};
2222

23+
constexpr uint8_t JoinKindMax = static_cast<uint8_t>(JoinKind::Paste);
24+
2325
void serializeJoinKind(JoinKind kind, WriteBuffer & out);
2426
JoinKind deserializeJoinKind(ReadBuffer & in);
2527

@@ -41,7 +43,7 @@ JoinKind reverseJoinKind(JoinKind kind);
4143
/// Allows more optimal JOIN for typical cases.
4244
enum class JoinStrictness : uint8_t
4345
{
44-
Unspecified,
46+
Unspecified = 0,
4547
RightAny, /// Old ANY JOIN. If there are many suitable rows in right table, use any from them to join.
4648
Any, /// Semi Join with any value from filtering table. For LEFT JOIN with Any and RightAny are the same.
4749
All, /// If there are many suitable rows to join, use all of them and replicate rows of "left" table (usual semantic of JOIN).
@@ -50,6 +52,8 @@ enum class JoinStrictness : uint8_t
5052
Anti, /// LEFT or RIGHT. Same as SEMI JOIN but filter values that are NOT exists in other table.
5153
};
5254

55+
constexpr uint8_t JoinStrictnessMax = static_cast<uint8_t>(JoinStrictness::Anti);
56+
5357
void serializeJoinStrictness(JoinStrictness strictness, WriteBuffer & out);
5458
JoinStrictness deserializeJoinStrictness(ReadBuffer & in);
5559

@@ -58,11 +62,13 @@ const char * toString(JoinStrictness strictness);
5862
/// Algorithm for distributed query processing.
5963
enum class JoinLocality : uint8_t
6064
{
61-
Unspecified,
65+
Unspecified = 0,
6266
Local, /// Perform JOIN, using only data available on same servers (co-located data).
63-
Global /// Collect and merge data from remote servers, and broadcast it to each server.
67+
Global, /// Collect and merge data from remote servers, and broadcast it to each server.
6468
};
6569

70+
constexpr uint8_t JoinLocalityMax = static_cast<uint8_t>(JoinLocality::Global);
71+
6672
void serializeJoinLocality(JoinLocality locality, WriteBuffer & out);
6773
JoinLocality deserializeJoinLocality(ReadBuffer & in);
6874

0 commit comments

Comments
 (0)