fix distinct on single string field [#CLICKHOUSE-28]

This commit is contained in:
artpaul 2017-01-12 20:34:41 +05:00
parent eb2c8196fd
commit 3292ca81cb
4 changed files with 43 additions and 4 deletions

View File

@ -37,7 +37,8 @@ private:
Method & method,
const ConstColumnPlainPtrs & key_columns,
IColumn::Filter & filter,
size_t rows) const;
size_t rows,
SetVariants & variants) const;
Names columns_names;

View File

@ -56,7 +56,7 @@ Block DistinctBlockInputStream::readImpl()
break;
#define M(NAME) \
case SetVariants::Type::NAME: \
buildFilter(*data.NAME, column_ptrs, filter, rows); \
buildFilter(*data.NAME, column_ptrs, filter, rows, data); \
break;
APPLY_FOR_SET_VARIANTS(M)
#undef M
@ -104,7 +104,8 @@ void DistinctBlockInputStream::buildFilter(
Method & method,
const ConstColumnPlainPtrs & columns,
IColumn::Filter & filter,
size_t rows) const
size_t rows,
SetVariants & variants) const
{
typename Method::State state;
state.init(columns);
@ -114,9 +115,16 @@ void DistinctBlockInputStream::buildFilter(
/// Make a key.
typename Method::Key key = state.getKey(columns, columns.size(), i, key_sizes);
typename Method::Data::iterator it = method.data.find(key);
bool inserted;
method.data.emplace(key, it, inserted);
if (inserted)
method.onNewKey(*it, columns.size(), i, variants.string_pool);
/// Emit the record if there is no such key in the current set yet.
/// Skip it otherwise.
filter[i] = method.data.insert(key).second;
filter[i] = inserted;
}
}

View File

@ -0,0 +1,8 @@
Bill
John
Mary
1
3
4
5
7

View File

@ -0,0 +1,22 @@
DROP TABLE IF EXISTS test.distinct;
CREATE TABLE test.distinct (Num UInt32, Name String) ENGINE = Memory;
INSERT INTO test.distinct (Num, Name) VALUES (1, 'John');
INSERT INTO test.distinct (Num, Name) VALUES (1, 'John');
INSERT INTO test.distinct (Num, Name) VALUES (3, 'Mary');
INSERT INTO test.distinct (Num, Name) VALUES (3, 'Mary');
INSERT INTO test.distinct (Num, Name) VALUES (3, 'Mary');
INSERT INTO test.distinct (Num, Name) VALUES (4, 'Mary');
INSERT INTO test.distinct (Num, Name) VALUES (4, 'Mary');
INSERT INTO test.distinct (Num, Name) VALUES (5, 'Bill');
INSERT INTO test.distinct (Num, Name) VALUES (7, 'Bill');
INSERT INTO test.distinct (Num, Name) VALUES (7, 'Bill');
INSERT INTO test.distinct (Num, Name) VALUES (7, 'Mary');
INSERT INTO test.distinct (Num, Name) VALUES (7, 'John');
-- String field
SELECT Name FROM (SELECT DISTINCT Name FROM test.distinct) ORDER BY Name;
-- Num field
SELECT Num FROM (SELECT DISTINCT Num FROM test.distinct) ORDER BY Num;
DROP TABLE IF EXISTS test.distinct;