Skip to content

Add new aggregate function sumMap(key, value). - #1250

Merged
alexey-milovidov merged 8 commits into
ClickHouse:masterfrom
bocharov:master
Sep 19, 2017
Merged

alexey-milovidov merged 8 commits into
ClickHouse:masterfrom
bocharov:master

Conversation

@bocharov

@bocharov bocharov commented Sep 15, 2017 •

Copy link
Copy Markdown

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:

 k           v
 [1,2,3]     [10,10,10]
 [3,4,5]     [10,10,10]
 [4,5,6]     [10,10,10]
 [6,7,8]     [10,10,10]
 [7,5,3]     [5,15,25]
 [8,9,10]    [20,20,20]

will return:

([1,2,3,4,5,6,7,8,9,10],[10,10,45,20,35,20,15,30,20,20])

}

writeVarUInt(size, buf);
buf.write(reinterpret_cast<const char *>(&keys[0]), size * sizeof(keys[0]));

@alexey-milovidov alexey-milovidov Sep 18, 2017 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can obtain nested type from DataTypeArray: DataTypeArray::getNestedType.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excessive copy. Better to insert into nested column directly.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread dbms/src/Core/Field.h Outdated
return *this;
}

Field & operator+= (const Field & rhs)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to use FieldVisitorSum directly inside AggregateFunctionSumMap. You can move it out of SummingSortedBlockInputStream.cpp to FieldVisitors.h or a separate header file.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, moved FieldVisitorSum to FieldVisitors.h, though not sure if that more efficient than using overloaded operator +=.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost fine, but few minor changes required.

@alexey-milovidov

alexey-milovidov commented Sep 18, 2017 •

Copy link
Copy Markdown
Member

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 AggregateFunctionGroupUniqArray.

@bocharov

Copy link
Copy Markdown
Author

I changed return type to Tuple(Array(K), Array(V)) and did other requested changes.
Please review again and I hope we could merge then.

size_t size = merged_maps.size();

to_keys_col.reserve(size);
to_values_col.reserve(size);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we reserve size for just inserted array even if column can already have multiple array values?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to_keys_col and to_values_col could already contain many arrays, but you reserve just for one array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants