This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Download Microsoft Edge
More info about Internet Explorer and Microsoft Edge
Applies to:
Databricks SQL
The
ANSI_MODE
configuration parameter controls key behaviors of built-in functions and cast operations.
This article describes ANSI mode in Databricks SQL. For ANSI compliance in Databricks Runtime, see
ANSI compliance in Databricks Runtime
.
Settings
Follows the SQL standard in how it deals with certain arithmetic operations and type conversions, similar to most databases and data warehouses. Following this standard promotes better data quality, integrity, and portability.
FALSE
Databricks SQL uses Hive-compatible behavior.
You can set this parameter at the session level using the
SET statement
and at the global level using
SQL configuration parameters
or
Global SQL Warehouses API
.
System default
The system default value is
TRUE
for accounts added on Databricks SQL 2022.35 and later.
Detailed description
The Databricks SQL reference documentation describes SQL standard behavior.
The following sections describe the differences between ANSI_MODE
TRUE
(ANSI mode) and
FALSE
(non-ANSI mode).
Operators
In non-ANSI mode, arithmetic operations performed on numeric types may return overflowed values or NULL, while in ANSI mode such operations return an error.
Operator
Description
Example
ANSI_MODE = true
ANSI_MODE = false
divisor div dividend
Returns the integral part of the division of divisor by dividend.
1 div 0
Error
Functions
The behavior of some built-in functions can be different under ANSI mode vs non-ANSI mode under the conditions specified below.
Operator
Description
Condition
ANSI_MODE = true
ANSI_MODE = false
element_at(arrayExpr, index)
Returns the element of an arrayExpr at index.
Invalid array index
Error
elt(index, expr1 [, …] )
Returns the nth expression.
Invalid index
Error
make_date(y,m,d)
Creates a date from year, month, and day fields.
Invalid result date
Error
make_timestamp(y,m,d,h,mi,s[,tz])
Creates a timestamp from fields.
Invalid result timestamp
Error
make_interval(y,m,w,d,h,mi,s)
Creates an interval from fields.
Invalid result interval
Error
mod(dividend, divisor)
Returns the remainder after dividend / divisor.
mod(1, 0)
Error
next_day(expr,dayOfWeek)
Returns the first date which is later than expr and named as in dayOfWeek.
Invalid day of week
Error
parse_url(url, partToExtract[, key])
Extracts a part from url.
Invalid URL
Error
pmod(dividend, divisor)
Returns the positive remainder after dividend / divisor.
pmod(1, 0)
Error
size(expr)
Returns the cardinality of expr.
size(NULL)
to_date(expr[,fmt])
Returns expr cast to a date using an optional formatting.
Invalid expr or format string
Error
to_timestamp(expr[,fmt])
Returns expr cast to a timestamp using an optional formatting.
Invalid expr or format string
Error
to_unix_timestamp(expr[,fmt])
Returns the timestamp in expr as a UNIX timestamp.
Invalid expr or format string
Error
unix_timestamp([expr[, fmt]])
Returns the UNIX timestamp of current or specified time.
Invalid expr or format string
Error
Casting rules
The rules and behaviors regarding CAST are stricter in ANSI mode. They can be divided into the following three categories:
Compile-time conversion rules
Runtime errors
Implicit type coercion rules
Compile-time conversion rules
Source type
Target type
Example
ANSI_MODE = true
ANSI_MODE = false
For each of these casts you can use
try_cast
instead of
cast
to return
NULL
rather than of an error.
Implicit type coercion rules
Under
ANSI_MODE = TRUE
, Databricks SQL uses clear
SQL data type casting rules
for:
type promotion
downcasting
crosscasting
By contrast
ANSI_MODE = FALSE
is inconsistent and more lenient. For example:
When using a
STRING
type with any arithmetic operator, the string is implicitly cast to
DOUBLE
.
When comparing a
STRING
to any numeric type the string is implicitly cast to the type it compares to.
When performing a
UNION
,
COALESCE
, or other operations where a least common type must be found all types are cast to
STRING
if there is any
STRING
type present.
Databricks recommends using the explicit
cast
or
try_cast
function instead of relying on
ANSI_MODE = FALSE
.
Examples
> SET ansi_mode = true;
-- Protects against integral numeric overflow
> SELECT cast(12345 AS TINYINT);
Casting 12345 to tinyint causes overflow
-- For invalid values raises errors instead of returning NULL.
> SELECT cast('a' AS INTEGER);
Invalid input syntax for type numeric: a.
To return NULL instead, use 'try_cast'
-- try_cast() is consistent for both modes
> SELECT try_cast('a' AS INTEGER);
-- Does not allow ambiguous crosscasting.
> SELECT c1 + c2 FROM VALUES('5', '7.6') AS T(c1, c2);
Cannot resolve '(T.c1 + T.c2)' due to data type mismatch:
'(T.c1 + T.c2)' requires (numeric or interval day to second or interval year to month or interval) type, not string
-- Promotes STRING to least common type (STRING, INTEGER --> BIGINT) for arithmetic operation.
> SELECT typeof(5 - '3');
bigint
-- Promotes STRING to least common type (INTEGER, STRING --> BIGINT) with runtime check
> SELECT c1 = c2 FROM VALUES(10, '10.1') AS T(c1, c2);
Invalid input syntax for type numeric: 10.1. To return NULL instead, use 'try_cast'.
-- Promotes STRING to least common type (STRING, INTEGER --> BIGINT) for set operation with runtime check.
> SELECT typeof(c1) FROM (SELECT 5 UNION ALL SELECT '6') AS T(c1);
bigint
bigint
> SET ansi_mode = false;
-- Silent integral numeric overflow
> SELECT cast(12345 AS TINYINT);
-- Returns NULL instead of an error
> SELECT cast('a' AS INTEGER);
-- try_cast() is safe for both modes
> SELECT try_cast('a' AS INTEGER);
-- Does allow ambiguous crosscasting using DOUBLE.
> SELECT c1 + c2 FROM VALUES('5', '7.6') AS T(c1, c2);
-- Crosscasts STRING to DOUBLE for arithmetic operation.
> SELECT typeof(5 - '3');
double
-- Implicitly casts STRING to INTEGER equating 10 with 10.1
> SELECT c1 = c2 FROM VALUES(10, '10.1') AS T(c1, c2);
-- Promotes to string for set operations
> SELECT typeof(c1) FROM (SELECT 5 UNION ALL SELECT '6') AS T(c1);
string
string
SQL data type rules
RESET
SET statement
SQL configuration parameters
Global SQL Warehouses API