Skip to main content

Type Conversion

In Doris, each expression has a type, such as 1, col1, and from_unixtime(col2) in the expression select 1, col1, from_unixtime(col2) from table1. The process of converting an expression from one type to another is called "type conversion."

Type conversion occurs in two cases: explicit conversion and implicit conversion.

All type conversions follow specific rules. We describe the related rules according to the target type of the conversion. For example, converting from INT to DOUBLE and converting from STRING to DOUBLE are both described in the Conversion to FLOAT/DOUBLE document.

Whether a conversion can occur and whether the result is a nullable type depends on whether strict mode is enabled (session variable enable_strict_cast). Generally, when strict mode is enabled, data that fails conversion will immediately cause an error and result in SQL failure. When strict mode is disabled, data rows that fail conversion will result in NULL.

Explicit Conversion

Explicit conversion is done through the CAST function, for example:

CAST(1.23 as INT) converts the number 1.23 to INT type.

CAST(colA as DATETIME(6)) converts the column/expression colA to DATETIME(6) type (i.e., a DATETIME type with microsecond precision).

The following describes the type conversion relationships between different types under strict mode (enable_strict_cast = true) and non-strict mode (enable_strict_cast = false), including the following four cases:

SymbolMeaning
xNot allowed to convert
PThe return type will be Nullable only when the input parameter is already a Nullable type, i.e., the conversion will not convert non-Null values to Null
AThe return type is always Nullable. The conversion may convert non-Null values to Null
OThe return type is Nullable when the conversion from input type to output type may cause overflow. For non-Null input values, if the conversion actually causes overflow, the conversion result may be Null

The specific type conversion rules and Nullable properties, please check the type conversion documents in the current directory.

Strict Mode

From\TobooltinyintsmallintintbigintlargeintfloatdoubledecimaldatedatetimetimeIPv4IPv6charvarcharstringbitmaphlljsonarraymapstructvariant
boolPPPPPPPPPxxxxxxxPxxx
tinyintPPPPPPPPPPPPxxxxPxxx
smallintPPPPPPPPPPPPxxxxPxxx
intPPPPPPPPPPPPxxxxPxxx
bigintPPPPPPPPPPPPxxxxPxxx
largeintPPPPPPPPPPPPxxxxPxxx
floatPPPPPPPPPPPPxxxxPxxx
doublePPPPPPPPPPPPxxxxPxxx
decimalPPPPPPPPPPPPxxxxPxxx
datexxxPPPxxxPPxxxxxxxxx
datetimexxxxPPxxxPPPxxxxxxxx
timexPPPPPxxxPPPxxxxxxxx
IPv4xxxxxxxxxxxxPPxxxxxx
IPv6xxxxxxxxxxxxxPxxxxxx
charPPPPPPPPPPPPPPxxPPPP
varcharPPPPPPPPPPPPPPxxPPPP
stringPPPPPPPPPPPPPPxxPPPP
bitmapxxxxxxxxxxxxxxxxxPxxxxx
hllxxxxxxxxxxxxxxxxxxPxxxx
jsonAAAAAAAAAxxxxxAAAxxPAxA
arrayxxxxxxxxxxxxxxxxPPxx
mapxxxxxxxxxxxxxxxxxxPx
structxxxxxxxxxxxxxxxxPxxP
variant

Non-strict Mode

From\TobooltinyintsmallintintbigintlargeintfloatdoubledecimaldatedatetimetimeIPv4IPv6charvarcharstringbitmaphlljsonarraymapstructvariant
boolPPPPPPPPOxxxxxxxPxxx
tinyintPPPPPPPPOAAAxxxxPxxx
smallintPAPPPPPPOAAAxxxxPxxx
intPAAPPPPPOAAAxxxxPxxx
bigintPAAAPPPPOAAAxxxxPxxx
largeintPAAAAPPPOAAAxxxxPxxx
floatPAAAAAPPAAAAxxxxPxxx
doublePAAAAAPPAAAAxxxxPxxx
decimalPOOOOOPPOAAAxxxxPxxx
datexxxPPPPPxPPxxxxxxxxx
datetimexxxxPPPPxPAPxxxxxxxx
timexAAAPPPPxPPAxxxxxxxx
IPv4xxxxxxxxxxxxPPxxxxxx
IPv6xxxxxxxxxxxxxPxxxxxx
charAAAAAAAAAAAAAAxxAAAA
varcharAAAAAAAAAAAAAAxxAAAA
stringAAAAAAAAAAAAAAxxAAAA
bitmapxxxxxxxxxxxxxxxxxPxxxxx
hllxxxxxxxxxxxxxxxxxxPxxxx
jsonAAAAAAAAAxxxxxAAAxxPAxA
arrayxxxxxxxxxxxxxxxxPPxx
mapxxxxxxxxxxxxxxxxxxPx
structxxxxxxxxxxxxxxxxPxxP
variant

Implicit Conversion

Implicit conversion occurs in certain situations where the input SQL does not explicitly specify, but Doris automatically plans the CAST expression. It mainly occurs in scenarios such as:

  1. When a function call is made, the type of the actual parameter does not match the type of the function signature.

  2. When the types on both sides of a mathematical expression are inconsistent.

etc.

Conversion matrix

TODO

Common Type

When an implicit conversion is required due to the operands being used as mathematical operations, the first step is to determine the common type. If the operands on both sides are not consistent with the common type, each will plan a CAST expression to the common type.

TODO