Repository navigation
Add new aggregate function sumMap(key, value). - #1250
Conversation
| } | ||
|
|
||
| writeVarUInt(size, buf); | ||
| buf.write(reinterpret_cast<const char *>(&keys[0]), size * sizeof(keys[0])); |
There was a problem hiding this comment.
This is not suitable, because:
- arbitary key types are not supported;
- keys will be serialized with garbage;
- and also you don't have to do extra copying.
Better to just write a loop and to use IDataType::serializeBinary method.
There was a problem hiding this comment.
It makes sense to serialize as little as possible. The only question I have - how to get access to data type of keys and values? In method setArgumentsImpl I can get access only to data type of arguments itself and that is DataTypeArray, but I need to know exact data type of the array elements. Is there a way to do this?
There was a problem hiding this comment.
You can obtain nested type from DataTypeArray: DataTypeArray::getNestedType.
There was a problem hiding this comment.
Thanks! It worked, new serialization method consumes much less memory now.
| const auto & merged_maps = this->data(place).merged_maps; | ||
| size_t size = merged_maps.size(); | ||
|
|
||
| Array keys, values; |
There was a problem hiding this comment.
Excessive copy. Better to insert into nested column directly.
There was a problem hiding this comment.
I've tried to do that but couldn't access sub-arrays of ColumnArray. I also tried to find similar implementation with usage of getNestedColumn but existing examples are quite different. There is no NestedColumn column type.
Can you please explain bit more how to implement the suggested change?
There was a problem hiding this comment.
Return type is Array(Array(T))
auto & to_data = to_array.getData();
to_data is elements of result arrays, so Array(T).
Now if you get static_cast<DataTypeArray &>(to_data).getData(), you will get column of type T that will contain all elements of all arrays.
And you can insert individual elements into it, one by one.
There was a problem hiding this comment.
PS. Why return type is not Tuple(Array(K), Array(V))?
This will allow different types of keys and values.
For example, keys can be strings and values - integers.
And manipulation with the result will be more simple.
Better to change signature before merge.
| return *this; | ||
| } | ||
|
|
||
| Field & operator+= (const Field & rhs) |
There was a problem hiding this comment.
Better to use FieldVisitorSum directly inside AggregateFunctionSumMap. You can move it out of SummingSortedBlockInputStream.cpp to FieldVisitors.h or a separate header file.
There was a problem hiding this comment.
Fixed, moved FieldVisitorSum to FieldVisitors.h, though not sure if that more efficient than using overloaded operator +=.
alexey-milovidov
left a comment
There was a problem hiding this comment.
Almost fine, but few minor changes required.
|
Implementation is rather inefficient, also we lack limit on array sizes and memory tracking. This is Ok for first version (will merge after few modifications). Superior implementation can use something similar to |
…ilePath after merge with master.
… return type to Tuple(Array(K), Array(V)).
|
I changed return type to |
| size_t size = merged_maps.size(); | ||
|
|
||
| to_keys_col.reserve(size); | ||
| to_values_col.reserve(size); |
There was a problem hiding this comment.
Why we reserve size for just inserted array even if column can already have multiple array values?
There was a problem hiding this comment.
What do you mean? I do not see just inserted array. to_keys and to_values are of the type ColumnVector and I thought allocating memory for vector elements before inserting them will be more efficient. Please feel free to remove this or change it to something more efficient.
There was a problem hiding this comment.
to_keys_col and to_values_col could already contain many arrays, but you reserve just for one array.
Adding new aggregate function sumMap, which takes two arguments: keys and values, and as a result, builds an array of 2 arrays - ordered keys and values summed up by corresponding keys.
This function is the most useful when using SummingMergeTree to sum Nested columns, which name ends in "Map".
Example: sumMap(k, v) of:
will return: