Skip to content

Commit b77b33a

Browse files
committed
Implemented reading values of SimpleAggregateFunction
1 parent 001a14e commit b77b33a

File tree

6 files changed

+62
-0
lines changed

6 files changed

+62
-0
lines changed

clickhouse/columns/factory.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ static ColumnRef CreateColumnFromAst(const TypeAst& ast) {
147147
throw std::runtime_error("LowCardinality(" + nested.name + ") is not supported");
148148
}
149149
}
150+
case TypeAst::SimpleAggregateFunction: {
151+
return CreateTerminalColumn(ast.elements.back());
152+
}
150153

151154
case TypeAst::Null:
152155
case TypeAst::Number:

clickhouse/types/type_parser.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ static TypeAst::Meta GetTypeMeta(const StringView& name) {
7979
return TypeAst::LowCardinality;
8080
}
8181

82+
if (name == "SimpleAggregateFunction") {
83+
return TypeAst::SimpleAggregateFunction;
84+
}
85+
8286
return TypeAst::Terminal;
8387
}
8488

clickhouse/types/type_parser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ struct TypeAst {
1919
Tuple,
2020
Enum,
2121
LowCardinality,
22+
SimpleAggregateFunction
2223
};
2324

2425
/// Type's category.

ut/client_ut.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,34 @@ TEST_P(ClientCase, Numbers) {
284284
EXPECT_EQ(100000U, num);
285285
}
286286

287+
TEST_P(ClientCase, SimpleAggregateFunction) {
288+
client_->Execute("DROP TABLE IF EXISTS test_clickhouse_cpp.SimpleAggregateFunction");
289+
client_->Execute(
290+
"CREATE TABLE IF NOT EXISTS test_clickhouse_cpp.SimpleAggregateFunction (saf SimpleAggregateFunction(sum, UInt64))"
291+
"ENGINE = Memory");
292+
293+
constexpr size_t EXPECTED_ROWS = 10;
294+
client_->Execute("INSERT INTO test_clickhouse_cpp.SimpleAggregateFunction (saf) SELECT number FROM system.numbers LIMIT 10");
295+
296+
size_t total_rows = 0;
297+
client_->Select("Select * FROM test_clickhouse_cpp.SimpleAggregateFunction", [&total_rows](const Block & block) {
298+
if (block.GetRowCount() == 0)
299+
return;
300+
301+
total_rows += block.GetRowCount();
302+
auto col = block[0]->As<ColumnUInt64>();
303+
ASSERT_NE(nullptr, col);
304+
305+
for (size_t r = 0; r < col->Size(); ++r) {
306+
EXPECT_EQ(r, col->At(r));
307+
}
308+
309+
EXPECT_EQ(total_rows, col->Size());
310+
});
311+
312+
EXPECT_EQ(EXPECTED_ROWS, total_rows);
313+
}
314+
287315
TEST_P(ClientCase, Cancellable) {
288316
/// Create a table.
289317
client_->Execute(

ut/columns_ut.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <clickhouse/columns/array.h>
22
#include <clickhouse/columns/date.h>
33
#include <clickhouse/columns/enum.h>
4+
#include <clickhouse/columns/factory.h>
45
#include <clickhouse/columns/lowcardinality.h>
56
#include <clickhouse/columns/nullable.h>
67
#include <clickhouse/columns/numeric.h>
@@ -258,3 +259,11 @@ TEST(ColumnsCase, LowCardinalityString_Save) {
258259
EXPECT_EQ(col.At(i), foobar(i)) << " at pos: " << i;
259260
}
260261
}
262+
263+
TEST(ColumnsCase, CreateSimpleAggregateFunction) {
264+
auto col = CreateColumnByType("SimpleAggregateFunction(funt, Int32");
265+
266+
ASSERT_EQ("Int32", col->Type()->GetName());
267+
ASSERT_EQ(Type::Int32, col->Type()->GetCode());
268+
ASSERT_NE(nullptr, col->As<ColumnInt32>());
269+
}

ut/type_parser_ut.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,20 @@ TEST(TypeParserCase, LowCardinality_FixedString) {
190190
auto param = TypeAst{TypeAst::Number, Type::Void, "", 10, {}};
191191
ASSERT_EQ(ast.elements[0].elements[0], param);
192192
}
193+
194+
TEST(TypeParserCase, SimpleAggregateFunction_UInt64) {
195+
TypeAst ast;
196+
TypeParser("SimpleAggregateFunction(func, UInt64)").Parse(&ast);
197+
ASSERT_EQ(ast.meta, TypeAst::SimpleAggregateFunction);
198+
ASSERT_EQ(ast.name, "SimpleAggregateFunction");
199+
ASSERT_EQ(ast.code, Type::Void);
200+
ASSERT_EQ(ast.elements.size(), 2u);
201+
ASSERT_EQ(ast.elements[0].name, "func");
202+
ASSERT_EQ(ast.elements[0].code, Type::Void);
203+
ASSERT_EQ(ast.elements[0].meta, TypeAst::Terminal);
204+
ASSERT_EQ(ast.elements[0].value, 0);
205+
ASSERT_EQ(ast.elements[1].name, "UInt64");
206+
ASSERT_EQ(ast.elements[1].code, Type::UInt64);
207+
ASSERT_EQ(ast.elements[1].meta, TypeAst::Terminal);
208+
ASSERT_EQ(ast.elements[1].value, 0);
209+
}

0 commit comments

Comments
 (0)