ksqlDB supports both implicit and explicit conversion between SQL types.
Explicit conversion is supported by using the
CAST function, which
supports a superset of the conversions that ksqlDB performs using implicit type
coercion on your behalf.
Implicit type coercion¶
ksqlDB supports implicit type coercion that converts between related types.
- Numeric types can be coerced to a wider numeric type. For example, an
INTEGERexpression can be coerced to a
BIGINT, which can be coerced to a
DECIMAL, which can be coerced to a
ARRAYtypes can be coerced to any other
ARRAYtype where the source array's element type can be coerced to the target's type.
MAPtypes can be coerced to any other
MAPtype where both the source map's key and value types can be coerced to the target's types.
STRUCTtypes can be coerced to any other
STRUCTtype where the types of any field that exists in both can be coerced.
For example, in the following query, the
A in the expression
WHERE A > B
is coerced implicitly to a
BIGINT, to enable the comparison with
1 2 3 4 5 6
Literal values support more open coercion rules than other expression types.
- All of the above general rules apply, and:
- Any literal can be coerced to a
STRINGliteral containing a number can be coerced to a numeric type wide enough to store the number. If the type is a
DOUBLE, the result may be inexact, due to rounding.
STRINGliteral containing a boolean value can be coerced to a
BOOLEAN. Valid boolean values are
no, or any substring of these values that starts with the first character, for example,
y. Comparison is case-insensitive.
Where an operator or function takes multiple expressions of the same type, for
ARRAY or other structured type constructors or the
the previous coercion rules are applied to ensure that all expressions can be
coerced to a common type.
The common type is determined by inspecting each expression in order. The behavior depends on the type of the first non-null element.
STRING: all other expressions must be coercible to
- Numeric: all other expressions must be coercible to a number. The common type is a numeric type that's wide enough to hold all numbers found in the list.
BOOLEAN: all other expressions must be coercible to
ARRAY: all other expressions must be
ARRAYs and have element types that can be coerced to a common element type.
MAP: all other expressions must be
MAPs and have key and value types that can be coerced to a common key and value type.
STRUCT: all other expressions must be
STRUCTs. Common field names must have types that can be coerced to a common type. The common type is a
STRUCTwith the superset of all fields.
For example, the
IN operator uses a single expression to compare to a list
of other expressions. All expressions must be coercible to a single common type,
which is used to perform the comparison.
1 2 3 4 5 6