Merge pull request #23456 from kitaisreal/cast-to-array-from-empty-array

Cast to array from empty array
This commit is contained in:
alexey-milovidov 2021-04-22 08:41:35 +03:00 committed by GitHub
commit 8134c270a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 19 deletions

View File

@ -360,7 +360,8 @@ inline bool isEnum(const DataTypePtr & data_type) { return WhichDataType(data_ty
inline bool isDecimal(const DataTypePtr & data_type) { return WhichDataType(data_type).isDecimal(); }
inline bool isTuple(const DataTypePtr & data_type) { return WhichDataType(data_type).isTuple(); }
inline bool isArray(const DataTypePtr & data_type) { return WhichDataType(data_type).isArray(); }
inline bool isMap(const DataTypePtr & data_type) {return WhichDataType(data_type).isMap(); }
inline bool isMap(const DataTypePtr & data_type) { return WhichDataType(data_type).isMap(); }
inline bool isNothing(const DataTypePtr & data_type) { return WhichDataType(data_type).isNothing(); }
template <typename T>
inline bool isUInt8(const T & data_type)

View File

@ -2496,7 +2496,7 @@ private:
}
}
WrapperType createArrayWrapper(const DataTypePtr & from_type_untyped, const DataTypeArray * to_type) const
WrapperType createArrayWrapper(const DataTypePtr & from_type_untyped, const DataTypeArray & to_type) const
{
/// Conversion from String through parsing.
if (checkAndGetDataType<DataTypeString>(from_type_untyped.get()))
@ -2507,24 +2507,23 @@ private:
};
}
DataTypePtr from_nested_type;
DataTypePtr to_nested_type;
const auto * from_type = checkAndGetDataType<DataTypeArray>(from_type_untyped.get());
/// get the most nested type
if (from_type && to_type)
if (!from_type)
{
from_nested_type = from_type->getNestedType();
to_nested_type = to_type->getNestedType();
from_type = checkAndGetDataType<DataTypeArray>(from_nested_type.get());
to_type = checkAndGetDataType<DataTypeArray>(to_nested_type.get());
throw Exception(ErrorCodes::TYPE_MISMATCH,
"CAST AS Array can only be perforamed between same-dimensional Array or String types");
}
/// both from_type and to_type should be nullptr now is array types had same dimensions
if ((from_type == nullptr) != (to_type == nullptr))
throw Exception{"CAST AS Array can only be performed between same-dimensional array types or from String",
ErrorCodes::TYPE_MISMATCH};
DataTypePtr from_nested_type = from_type->getNestedType();
/// In query SELECT CAST([] AS Array(Array(String))) from type is Array(Nothing)
bool from_empty_array = isNothing(from_nested_type);
if (from_type->getNumberOfDimensions() != to_type.getNumberOfDimensions() && !from_empty_array)
throw Exception(ErrorCodes::TYPE_MISMATCH,
"CAST AS Array can only be perforamed between same-dimensional array types");
const DataTypePtr & to_nested_type = to_type.getNestedType();
/// Prepare nested type conversion
const auto nested_function = prepareUnpackDictionaries(from_nested_type, to_nested_type);
@ -3090,14 +3089,12 @@ private:
return createStringWrapper(from_type);
case TypeIndex::FixedString:
return createFixedStringWrapper(from_type, checkAndGetDataType<DataTypeFixedString>(to_type.get())->getN());
case TypeIndex::Array:
return createArrayWrapper(from_type, checkAndGetDataType<DataTypeArray>(to_type.get()));
return createArrayWrapper(from_type, static_cast<const DataTypeArray &>(*to_type));
case TypeIndex::Tuple:
return createTupleWrapper(from_type, checkAndGetDataType<DataTypeTuple>(to_type.get()));
case TypeIndex::Map:
return createMapWrapper(from_type, checkAndGetDataType<DataTypeMap>(to_type.get()));
case TypeIndex::AggregateFunction:
return createAggregateFunctionWrapper(from_type, checkAndGetDataType<DataTypeAggregateFunction>(to_type.get()));
default:

View File

@ -0,0 +1,2 @@
[]
[]

View File

@ -0,0 +1,2 @@
SELECT CAST([] AS Array(Array(String)));
SELECT CAST([] AS Array(Array(Array(String))));