2017-07-16 03:05:40 +00:00
# include <DataTypes/DataTypesNumber.h>
# include <DataTypes/DataTypeFactory.h>
2020-05-18 23:53:41 +00:00
# include <Parsers/IAST.h>
# include <Parsers/ASTLiteral.h>
2017-07-16 03:05:40 +00:00
namespace DB
{
2020-05-18 23:53:41 +00:00
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH ;
extern const int UNEXPECTED_AST_STRUCTURE ;
}
static DataTypePtr createForInt8 ( const ASTPtr & arguments )
{
if ( arguments ) {
if ( arguments - > children . size ( ) > 1 )
throw Exception ( " INT8 data type family must not have more than one argument - display width " , ErrorCodes : : NUMBER_OF_ARGUMENTS_DOESNT_MATCH ) ;
const auto * argument = arguments - > children [ 0 ] - > as < ASTLiteral > ( ) ;
if ( ! argument | | argument - > value . getType ( ) ! = Field : : Types : : UInt64 | | argument - > value . get < UInt64 > ( ) = = 0 )
throw Exception ( " INT8 data type family may have only a number (positive integer) as its argument " , ErrorCodes::UNEXPECTED_AST_STRUCTURE) ;
}
return std : : make_shared < DataTypeInt8 > ( ) ;
}
static DataTypePtr createForInt16 ( const ASTPtr & arguments )
{
if ( arguments ) {
if ( arguments - > children . size ( ) > 1 )
throw Exception ( " INT16 data type family must not have more than one argument - display width " , ErrorCodes : : NUMBER_OF_ARGUMENTS_DOESNT_MATCH ) ;
const auto * argument = arguments - > children [ 0 ] - > as < ASTLiteral > ( ) ;
if ( ! argument | | argument - > value . getType ( ) ! = Field : : Types : : UInt64 | | argument - > value . get < UInt64 > ( ) = = 0 )
throw Exception ( " INT16 data type family may have only a number (positive integer) as its argument " , ErrorCodes::UNEXPECTED_AST_STRUCTURE) ;
}
return std : : make_shared < DataTypeInt16 > ( ) ;
}
static DataTypePtr createForInt32 ( const ASTPtr & arguments )
{
if ( arguments ) {
if ( arguments - > children . size ( ) > 1 )
throw Exception ( " INT32 data type family must not have more than one argument - display width " , ErrorCodes : : NUMBER_OF_ARGUMENTS_DOESNT_MATCH ) ;
const auto * argument = arguments - > children [ 0 ] - > as < ASTLiteral > ( ) ;
if ( ! argument | | argument - > value . getType ( ) ! = Field : : Types : : UInt64 | | argument - > value . get < UInt64 > ( ) = = 0 )
throw Exception ( " INT32 data type family may have only a number (positive integer) as its argument " , ErrorCodes::UNEXPECTED_AST_STRUCTURE) ;
}
return std : : make_shared < DataTypeInt32 > ( ) ;
}
static DataTypePtr createForInt64 ( const ASTPtr & arguments )
{
if ( arguments ) {
if ( arguments - > children . size ( ) > 1 )
throw Exception ( " INT64 data type family must not have more than one argument - display width " , ErrorCodes : : NUMBER_OF_ARGUMENTS_DOESNT_MATCH ) ;
const auto * argument = arguments - > children [ 0 ] - > as < ASTLiteral > ( ) ;
if ( ! argument | | argument - > value . getType ( ) ! = Field : : Types : : UInt64 | | argument - > value . get < UInt64 > ( ) = = 0 )
throw Exception ( " INT64 data type family may have only a number (positive integer) as its argument " , ErrorCodes::UNEXPECTED_AST_STRUCTURE) ;
}
return std : : make_shared < DataTypeInt64 > ( ) ;
}
static DataTypePtr createForFloat32 ( const ASTPtr & arguments )
{
if ( arguments ) {
if ( arguments - > children . size ( ) > 2 )
throw Exception ( " FLOAT32 data type family must not have more than two arguments - total number of digits and number of digits following the decimal point " , ErrorCodes : : NUMBER_OF_ARGUMENTS_DOESNT_MATCH ) ;
else if ( arguments - > children . size ( ) = = 1 ) {
const auto * argument = arguments - > children [ 0 ] - > as < ASTLiteral > ( ) ;
if ( ! argument | | argument - > value . getType ( ) ! = Field : : Types : : UInt64 )
throw Exception ( " FLOAT32 data type family may have a non negative number as its argument " , ErrorCodes : : UNEXPECTED_AST_STRUCTURE ) ;
} else if ( arguments - > children . size ( ) = = 2 ) {
const auto * beforePoint = arguments - > children [ 0 ] - > as < ASTLiteral > ( ) ;
const auto * afterPoint = arguments - > children [ 1 ] - > as < ASTLiteral > ( ) ;
if ( ! beforePoint | | beforePoint - > value . getType ( ) ! = Field : : Types : : UInt64 | |
! afterPoint | | afterPoint - > value . getType ( ) ! = Field : : Types : : UInt64 )
throw Exception ( " FLOAT32 data type family may have a non negative number as its arguments " , ErrorCodes : : UNEXPECTED_AST_STRUCTURE ) ;
}
}
return std : : make_shared < DataTypeFloat32 > ( ) ;
}
static DataTypePtr createForFloat64 ( const ASTPtr & arguments )
{
if ( arguments ) {
if ( arguments - > children . size ( ) ! = 2 )
throw Exception ( " FLOAT64 data type family must have only two arguments - total number of digits and number of digits following the decimal point " , ErrorCodes : : NUMBER_OF_ARGUMENTS_DOESNT_MATCH ) ;
else {
const auto * beforePoint = arguments - > children [ 0 ] - > as < ASTLiteral > ( ) ;
const auto * afterPoint = arguments - > children [ 1 ] - > as < ASTLiteral > ( ) ;
if ( ! beforePoint | | beforePoint - > value . getType ( ) ! = Field : : Types : : UInt64 | |
! afterPoint | | afterPoint - > value . getType ( ) ! = Field : : Types : : UInt64 )
throw Exception ( " FLOAT64 data type family may have a non negative number as its arguments " , ErrorCodes : : UNEXPECTED_AST_STRUCTURE ) ;
}
}
return std : : make_shared < DataTypeFloat64 > ( ) ;
}
2017-07-16 03:05:40 +00:00
void registerDataTypeNumbers ( DataTypeFactory & factory )
{
2020-03-10 18:16:14 +00:00
factory . registerSimpleDataType ( " UInt8 " , [ ] { return DataTypePtr ( std : : make_shared < DataTypeUInt8 > ( ) ) ; } ) ;
factory . registerSimpleDataType ( " UInt16 " , [ ] { return DataTypePtr ( std : : make_shared < DataTypeUInt16 > ( ) ) ; } ) ;
factory . registerSimpleDataType ( " UInt32 " , [ ] { return DataTypePtr ( std : : make_shared < DataTypeUInt32 > ( ) ) ; } ) ;
factory . registerSimpleDataType ( " UInt64 " , [ ] { return DataTypePtr ( std : : make_shared < DataTypeUInt64 > ( ) ) ; } ) ;
2020-05-18 23:53:41 +00:00
factory . registerDataType ( " Int8 " , createForInt8 ) ;
factory . registerDataType ( " Int16 " , createForInt16 ) ;
factory . registerDataType ( " Int32 " , createForInt32 ) ;
factory . registerDataType ( " Int64 " , createForInt64 ) ;
factory . registerDataType ( " Float32 " , createForFloat32 ) ;
factory . registerDataType ( " Float64 " , createForFloat64 ) ;
2017-07-16 05:09:47 +00:00
2019-01-22 19:56:53 +00:00
/// These synonyms are added for compatibility.
2017-07-16 05:09:47 +00:00
2018-07-23 16:10:57 +00:00
factory . registerAlias ( " TINYINT " , " Int8 " , DataTypeFactory : : CaseInsensitive ) ;
2020-05-12 13:29:20 +00:00
factory . registerAlias ( " BOOL " , " Int8 " , DataTypeFactory : : CaseInsensitive ) ;
factory . registerAlias ( " BOOLEAN " , " Int8 " , DataTypeFactory : : CaseInsensitive ) ;
factory . registerAlias ( " INT1 " , " Int8 " , DataTypeFactory : : CaseInsensitive ) ;
2018-07-23 16:10:57 +00:00
factory . registerAlias ( " SMALLINT " , " Int16 " , DataTypeFactory : : CaseInsensitive ) ;
2020-05-12 13:29:20 +00:00
factory . registerAlias ( " INT2 " , " Int16 " , DataTypeFactory : : CaseInsensitive ) ;
2018-07-23 16:10:57 +00:00
factory . registerAlias ( " INT " , " Int32 " , DataTypeFactory : : CaseInsensitive ) ;
2020-05-12 13:29:20 +00:00
factory . registerAlias ( " INT4 " , " Int32 " , DataTypeFactory : : CaseInsensitive ) ;
2018-07-23 16:10:57 +00:00
factory . registerAlias ( " INTEGER " , " Int32 " , DataTypeFactory : : CaseInsensitive ) ;
factory . registerAlias ( " BIGINT " , " Int64 " , DataTypeFactory : : CaseInsensitive ) ;
factory . registerAlias ( " FLOAT " , " Float32 " , DataTypeFactory : : CaseInsensitive ) ;
factory . registerAlias ( " DOUBLE " , " Float64 " , DataTypeFactory : : CaseInsensitive ) ;
2017-07-16 03:05:40 +00:00
}
}