Merge pull request #12275 from ClickHouse/supertype-low-cardinality

Supertype for LowCardinality
This commit is contained in:
alexey-milovidov 2020-07-08 14:27:48 +03:00 committed by GitHub
commit 3f0c257389
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 176 additions and 0 deletions

View File

@ -9,6 +9,7 @@
#include <DataTypes/DataTypeArray.h>
#include <DataTypes/DataTypeTuple.h>
#include <DataTypes/DataTypeNullable.h>
#include <DataTypes/DataTypeLowCardinality.h>
#include <DataTypes/DataTypeNothing.h>
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypeDateTime.h>
@ -160,6 +161,39 @@ DataTypePtr getLeastSupertype(const DataTypes & types)
}
}
/// For LowCardinality. This is above Nullable, because LowCardinality can contain Nullable but cannot be inside Nullable.
{
bool have_low_cardinality = false;
bool have_not_low_cardinality = false;
DataTypes nested_types;
nested_types.reserve(types.size());
for (const auto & type : types)
{
if (const DataTypeLowCardinality * type_low_cardinality = typeid_cast<const DataTypeLowCardinality *>(type.get()))
{
have_low_cardinality = true;
nested_types.emplace_back(type_low_cardinality->getDictionaryType());
}
else
{
have_not_low_cardinality = true;
nested_types.emplace_back(type);
}
}
/// All LowCardinality gives LowCardinality.
/// LowCardinality with high cardinality gives high cardinality.
if (have_low_cardinality)
{
if (have_not_low_cardinality)
return getLeastSupertype(nested_types);
else
return std::make_shared<DataTypeLowCardinality>(getLeastSupertype(nested_types));
}
}
/// For Nullable
{
bool have_nullable = false;

View File

@ -0,0 +1,71 @@
hello
hello
String
String
---
---
hello
hello
hello
hello
---
hello
hello
-
hello
hello
-
hello
hello
-
hello
hello
-
hello
hello
-
hello
hello
-
hello
hello
-
hello
hello
-
hello
hello
-
hello
hello
-
hello
hello
-
hello
hello
---
hello
hello
hello
-
hello
hello
hello
-
hello
hello
hello
-
hello
hello
hello
---
hello
hello
hello
hello
---
['abc','def']
['abc','def']
['abc','def']

View File

@ -0,0 +1,71 @@
SELECT 'hello' UNION ALL SELECT toLowCardinality('hello');
SELECT toTypeName(x) FROM (SELECT 'hello' AS x UNION ALL SELECT toLowCardinality('hello'));
SELECT '---';
create temporary table t1(a String);
create temporary table t2(a LowCardinality(String));
select a from t1 union all select a from t2;
SELECT '---';
CREATE TEMPORARY TABLE a (x String);
CREATE TEMPORARY TABLE b (x LowCardinality(String));
CREATE TEMPORARY TABLE c (x Nullable(String));
CREATE TEMPORARY TABLE d (x LowCardinality(Nullable(String)));
INSERT INTO a VALUES ('hello');
INSERT INTO b VALUES ('hello');
INSERT INTO c VALUES ('hello');
INSERT INTO d VALUES ('hello');
SELECT x FROM a;
SELECT x FROM b;
SELECT x FROM c;
SELECT x FROM d;
SELECT '---';
SELECT x FROM a UNION ALL SELECT x FROM b;
SELECT '-';
SELECT x FROM a UNION ALL SELECT x FROM c;
SELECT '-';
SELECT x FROM a UNION ALL SELECT x FROM d;
SELECT '-';
SELECT x FROM b UNION ALL SELECT x FROM a;
SELECT '-';
SELECT x FROM b UNION ALL SELECT x FROM c;
SELECT '-';
SELECT x FROM b UNION ALL SELECT x FROM d;
SELECT '-';
SELECT x FROM c UNION ALL SELECT x FROM a;
SELECT '-';
SELECT x FROM c UNION ALL SELECT x FROM b;
SELECT '-';
SELECT x FROM c UNION ALL SELECT x FROM d;
SELECT '-';
SELECT x FROM d UNION ALL SELECT x FROM a;
SELECT '-';
SELECT x FROM d UNION ALL SELECT x FROM c;
SELECT '-';
SELECT x FROM d UNION ALL SELECT x FROM b;
SELECT '---';
SELECT x FROM b UNION ALL SELECT x FROM c UNION ALL SELECT x FROM d;
SELECT '-';
SELECT x FROM a UNION ALL SELECT x FROM c UNION ALL SELECT x FROM d;
SELECT '-';
SELECT x FROM a UNION ALL SELECT x FROM b UNION ALL SELECT x FROM d;
SELECT '-';
SELECT x FROM a UNION ALL SELECT x FROM b UNION ALL SELECT x FROM c;
SELECT '---';
SELECT x FROM a UNION ALL SELECT x FROM b UNION ALL SELECT x FROM c UNION ALL SELECT x FROM d;
SELECT '---';
SELECT [CAST('abc' AS LowCardinality(String)), CAST('def' AS Nullable(String))];
SELECT [CAST('abc' AS LowCardinality(String)), CAST('def' AS FixedString(3))];
SELECT [CAST('abc' AS LowCardinality(String)), CAST('def' AS LowCardinality(FixedString(3)))];