Updated on 2025-10-23 GMT+08:00

Type Conversion

Context

In SQL, every data item has an associated data type which determines its behavior and allowed usage. There are four fundamental SQL structures requiring distinct type conversion rules in M-compatible databases:

  • Function calls

    Much of the SQL type system is built around a rich set of functions. Functions can have one or more arguments. Since SQL permits function overloading, the function name alone does not uniquely identify the function to be called. The parser must select the right function based on the data types of the supplied arguments.

  • Operators

    SQL allows expressions with prefix and postfix unary (one-argument) operators, as well as binary (two-argument) operators. Like functions, operators can be overloaded, so the same problem of selecting the right operator exists.

  • Value storage

    The INSERT and UPDATE statements place the results of expressions into a table. The expressions in the statements must be matched up with, and perhaps converted to, the types of the target columns.

  • UNION, EXCEPT, CASE, and related constructs

    Since all query results from a unionized SELECT statement must appear in a single set of columns, the types of the results of each SELECT clause must be matched up and converted to a uniform set. Similarly, the result expressions of a CASE construct must be converted to a common type so that the CASE expression as a whole has a known output type.

Based on type conversion rules of M-compatible databases, the four SQL structures are classified into three scenarios: implicit type conversion, explicit type conversion, UNION, EXCEPT, CASE, and related constructs. These three scenarios have different conversion behaviors and conversion relationships in M-compatible databases.

The return type and conversion behavior of an expression are determined during semantic analysis. In M-compatible databases, the basic data types include INTEGER, DECIMAL, STRING, BITSTRING, FLOAT, and TEMPORAL. To solve the type selection problem of expressions, implicit conversion is available between basic types in M-compatible databases. With the types and available implicit conversions, it is possible to ensure that ambiguous expressions can be resolved in a useful way.