You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### Converting an ordinary column to a Variant column
232
+
233
+
It is possible to convert an ordinary column with type `T` to a `Variant` column containing this type:
234
+
235
+
```sql
236
+
SELECT toTypeName(variant) as type_name, [1,2,3]::Array(UInt64)::Variant(UInt64, String, Array(UInt64)) as variant, variantType(variant) as variant_name
Note: converting from`String` type is always performed through parsing, if you need to convert`String` column to `String` variant of a `Variant` without parsing, you can do the following:
246
+
```sql
247
+
SELECT '[1, 2, 3]'::Variant(String)::Variant(String, Array(UInt64), UInt64) as variant, variantType(variant) as variant_type
248
+
```
249
+
250
+
```sql
251
+
┌─variant───┬─variant_type─┐
252
+
│ [1, 2, 3] │ String │
253
+
└───────────┴──────────────┘
209
254
```
210
255
211
256
### Converting a Variant column to an ordinary column
/// We allow converting NULL to Variant(...) as Variant can store NULLs.
@@ -4091,6 +4121,10 @@ class FunctionCast final : public IFunctionBase
4091
4121
}
4092
4122
4093
4123
auto variant_discr_opt = to_variant.tryGetVariantDiscriminator(*removeNullableOrLowCardinalityNullable(from_type));
4124
+
/// Cast String to Variant through parsing if it's not Variant(String).
4125
+
if (isStringOrFixedString(removeNullable(removeLowCardinality(from_type))) && (!variant_discr_opt || to_variant.getVariants().size() > 1))
4126
+
returncreateStringToVariantWrapper();
4127
+
4094
4128
if (!variant_discr_opt)
4095
4129
throwException(ErrorCodes::CANNOT_CONVERT_TYPE, "Cannot convert type {} to {}. Conversion to Variant allowed only for types from this Variant", from_type->getName(), to_variant.getName());
4096
4130
@@ -4692,7 +4726,7 @@ class FunctionCast final : public IFunctionBase
4692
4726
4693
4727
if (to_type->getCustomSerialization() && to_type->getCustomName())
4694
4728
{
4695
-
ret = [requested_result_is_nullable](
4729
+
ret = [this, requested_result_is_nullable](
4696
4730
ColumnsWithTypeAndName & arguments,
4697
4731
const DataTypePtr & result_type,
4698
4732
const ColumnNullable * column_nullable,
@@ -4701,7 +4735,10 @@ class FunctionCast final : public IFunctionBase
Copy file name to clipboardExpand all lines: tests/queries/0_stateless/02941_variant_type_1.sh
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ function test1_insert()
14
14
echo"test1 insert"
15
15
$CH_CLIENT -nmq "insert into test select number, NULL from numbers(3);
16
16
insert into test select number + 3, number from numbers(3);
17
-
insert into test select number + 6, 'str_' || toString(number) from numbers(3);
17
+
insert into test select number + 6, ('str_' || toString(number))::Variant(String) from numbers(3);
18
18
insert into test select number + 9, ('lc_str_' || toString(number))::LowCardinality(String) from numbers(3);
19
19
insert into test select number + 12, tuple(number, number + 1)::Tuple(a UInt32, b UInt32) from numbers(3);
20
20
insert into test select number + 15, range(number + 1)::Array(UInt64) from numbers(3);"
@@ -40,7 +40,7 @@ function test2_insert()
40
40
echo"test2 insert"
41
41
$CH_CLIENT -nmq "insert into test select number, NULL from numbers(3);
42
42
insert into test select number + 3, number % 2 ? NULL : number from numbers(3);
43
-
insert into test select number + 6, number % 2 ? NULL : 'str_' || toString(number) from numbers(3);
43
+
insert into test select number + 6, number % 2 ? NULL : ('str_' || toString(number))::Variant(String) from numbers(3);
44
44
insert into test select number + 9, number % 2 ? CAST(NULL, 'Variant(String, UInt64, LowCardinality(String), Tuple(a UInt32, b UInt32), Array(UInt64))') : CAST(('lc_str_' || toString(number))::LowCardinality(String), 'Variant(String, UInt64, LowCardinality(String), Tuple(a UInt32, b UInt32), Array(UInt64))') from numbers(3);
45
45
insert into test select number + 12, number % 2 ? CAST(NULL, 'Variant(String, UInt64, LowCardinality(String), Tuple(a UInt32, b UInt32), Array(UInt64))') : CAST(tuple(number, number + 1)::Tuple(a UInt32, b UInt32), 'Variant(String, UInt64, LowCardinality(String), Tuple(a UInt32, b UInt32), Array(UInt64))') from numbers(3);
46
46
insert into test select number + 15, number % 2 ? CAST(NULL, 'Variant(String, UInt64, LowCardinality(String), Tuple(a UInt32, b UInt32), Array(UInt64))') : CAST(range(number + 1)::Array(UInt64), 'Variant(String, UInt64, LowCardinality(String), Tuple(a UInt32, b UInt32), Array(UInt64))') from numbers(3);"
@@ -64,7 +64,7 @@ select v.\`Array(UInt64)\`.size0 from test order by id;"
64
64
functiontest3_insert()
65
65
{
66
66
echo"test3 insert"
67
-
$CH_CLIENT -q "insert into test with 'Variant(String, UInt64, LowCardinality(String), Tuple(a UInt32, b UInt32), Array(UInt64))' as type select number, multiIf(number % 6 == 0, CAST(NULL, type), number % 6 == 1, CAST('str_' || toString(number), type), number % 6 == 2, CAST(number, type), number % 6 == 3, CAST(('lc_str_' || toString(number))::LowCardinality(String), type), number % 6 == 4, CAST(tuple(number, number + 1)::Tuple(a UInt32, b UInt32), type), CAST(range(number + 1)::Array(UInt64), type)) as res from numbers(18);"
67
+
$CH_CLIENT -q "insert into test with 'Variant(String, UInt64, LowCardinality(String), Tuple(a UInt32, b UInt32), Array(UInt64))' as type select number, multiIf(number % 6 == 0, CAST(NULL, type), number % 6 == 1, CAST(('str_' || toString(number))::Variant(String), type), number % 6 == 2, CAST(number, type), number % 6 == 3, CAST(('lc_str_' || toString(number))::LowCardinality(String), type), number % 6 == 4, CAST(tuple(number, number + 1)::Tuple(a UInt32, b UInt32), type), CAST(range(number + 1)::Array(UInt64), type)) as res from numbers(18);"
0 commit comments