BigQuery compatibility mode

Updated at:

Feature overview

Migrating SQL from BigQuery to MaxCompute often requires extensive syntax rewriting. To simplify migration, MaxCompute provides BigQuery compatibility mode, which you enable by setting the odps.sql.BigQuery.compatible flag. This mode aligns MaxCompute's behavior with BigQuery's in key areas—such as query semantics, type conversion, built-in function behavior, and result formats—significantly reducing the effort required to migrate SQL from BigQuery to MaxCompute.

Usage notes

  • The BigQuery compatibility mode addresses syntactical and behavioral differences encountered in SQL rewriting and script migration. It does not completely replicate BigQuery SQL syntax or the behavior of its functions. For any syntax, functions, data types, or boundary behaviors not covered in this topic, the standard MaxCompute SQL rules apply.

  • For real-world migrations, we recommend using the LMH tool to rewrite BigQuery SQL to MaxCompute SQL in bulk. BigQuery compatibility mode complements the LMH tool but does not cover every syntax and function difference. The tool converts SQL automatically, while compatibility mode handles behavioral differences that are difficult to rewrite but can be aligned automatically at runtime. Using them together further reduces migration costs.

  • The MaxCompute BigQuery compatibility mode (odps.sql.BigQuery.compatible) and Hive compatibility mode (odps.sql.hive.compatible) align with different engine behaviors for type conversion, function return values, and overflow handling. You cannot enable both modes simultaneously. Doing so causes a compilation error. For more information, see Differences in SQL between normal mode and Hive compatibility mode.

Flags

Parameter

Scope

Description

Value

odps.sql.BigQuery.compatible

session/Project

Enables the BigQuery compatibility mode, aligning MaxCompute's behavior with BigQuery in query semantics, type conversion, built-in function behavior, and result formats. This mode is disabled by default.

  • false (default)

  • true

Recommended configuration

-- Enable at the Project level
SETPROJECT odps.sql.type.system.odps2 = true;
SETPROJECT odps.sql.hive.compatible = false;
SETPROJECT odps.sql.BigQuery.compatible = true;


-- Enable at the session level
SET odps.sql.type.system.odps2 = true;
SET odps.sql.hive.compatible = false;
SET odps.sql.BigQuery.compatible = true;
To use data types such as DATE, TIMESTAMP, TIMESTAMP_NTZ, JSON, and STRUCT, you must also enable the MaxCompute 2.0 data type version.

Behavior changes

The following tables summarize the key behavioral changes to MaxCompute SQL in BigQuery compatibility mode. These changes fall into three categories: type conversion and expressions, query semantics and syntax, and functions.

Type conversion and expressions

Affected feature

Description

Comparison between DATE and TIMESTAMP_NTZ

BigQuery compatibility mode supports direct comparison, treating a DATE value as a TIMESTAMP_NTZ value at 00:00:00 on that day.

Automatic conversion of date/time string literals

BigQuery compatibility mode automatically converts literals in the yyyy-MM-dd format to DATE or TIMESTAMP during binary operations and within the GREATEST and LEAST functions.

Result type of DATE + INTERVAL_DAY_TIME

BigQuery compatibility mode returns TIMESTAMP_NTZ. The default mode returns TIMESTAMP (with a time zone).

Mixed use of date strings in GREATEST / LEAST

BigQuery compatibility mode automatically converts STRING literals to DATE for comparison.

Cross-type inference for UNION / UNION ALL

BigQuery compatibility mode supports common type inference between DATE and TIMESTAMP_NTZ, and between STRING and time-related types.

STRUCT expressions containing NULL fields

BigQuery compatibility mode treats untyped NULL values as STRING when they are written to a STRUCT.

Implicit write/assignment for DECIMAL

BigQuery compatibility mode relaxes implicit conversion from DECIMAL to DOUBLE and between DECIMAL types with different precisions.

Query semantics and syntax

Affected feature

Description

Alias resolution priority in GROUP BY, HAVING, and QUALIFY

In BigQuery compatibility mode, SELECT aliases take precedence over input column names.

GROUP BY ALL

BigQuery compatibility mode supports this syntax and automatically infers the group keys.

SELECT * EXCEPT(col) with same-name columns across multiple tables

In BigQuery compatibility mode, you can exclude columns with the same name from multiple tables without causing an ambiguity error.

Accessing original columns on both sides of JOIN ... USING

In BigQuery compatibility mode, you can use table aliases to access the original columns on both sides of a USING clause.

Accessing STRUCT fields by index

BigQuery compatibility mode supports accessing STRUCT fields with integer literals starting from 0.

ORDER BY, QUALIFY, and window functions in correlated subqueries

BigQuery compatibility mode disallows these clauses in correlated subqueries. The default mode allows them.

Maximum column name length

BigQuery compatibility mode increases the maximum column name length to 256 bytes, up from the default of 128 bytes.

Concatenation order for PIVOT output column names

BigQuery compatibility mode formats output column names as alias_value, while the default mode uses value_alias.

Automatic deduplication of duplicate output column names

BigQuery compatibility mode automatically appends suffixes such as _1 and _2. The default mode throws an error.

Functions

Affected feature

Description

SUBSTR / SUBSTRING

Boundary behaviors differ for a start position of 0, out-of-range negative positions, and negative lengths.

REVERSE

BigQuery compatibility mode reverses strings by UTF-8 characters. The default mode reverses them by bytes, which can garble multi-byte characters.

REGEXP_EXTRACT / REGEXP_EXTRACT_ALL

In BigQuery compatibility mode, the function returns the full match if no capture group is present. The default mode throws an error.

DATEDIFF (with TIMESTAMP input)

BigQuery compatibility mode calculates differences in days, hours, minutes, or seconds based on the actual time difference. In contrast, the default mode first truncates the values and then subtracts them.

TO_DATE(timestamp, timezone)

BigQuery compatibility mode adds a new overload that includes a time zone to extract the date based on the specified time zone.

TO_JSON (STRUCT to JSON)

BigQuery compatibility mode retains NULL fields. The default mode omits them.

JSON_EXTRACT with STRING input

In BigQuery compatibility mode, the function accepts a STRING input and returns a STRING value.

STDDEV_SAMP / VAR_SAMP

For a single non-NULL data entry, BigQuery compatibility mode returns NULL, while the default mode returns 0.

Timestamp format parsing (CAST(STRING AS TIMESTAMP))

BigQuery compatibility mode supports the 'T' separator and recognizes 'Z', named time zones, and offsets.

CAST(STRING AS BOOLEAN)

BigQuery compatibility mode accepts only 'true' or 'false' (case-insensitive). The default mode treats any non-empty string as true.

CAST(TIMESTAMP AS STRING)

BigQuery compatibility mode appends a time zone suffix and pads fractional seconds to 3, 6, or 9 digits.

CAST(TIMESTAMP_NTZ AS STRING)

BigQuery compatibility mode pads fractional seconds to 3, 6, or 9 digits. The default mode may truncate them.

CAST(STRING AS TIMESTAMP)

BigQuery compatibility mode supports casting a date-only string to TIMESTAMP by automatically appending 00:00:00.

Sample data

-- Create a partitioned table named sale_detail.
CREATE TABLE IF NOT EXISTS sale_detail
(
shop_name     STRING,
customer_id   STRING,
total_price   DOUBLE
)
PARTITIONED BY (sale_date STRING, region STRING);

-- Add a partition to the source table.
ALTER TABLE sale_detail ADD PARTITION (sale_date='2013', region='china');

-- Insert data into the source table.
INSERT INTO sale_detail PARTITION (sale_date='2013', region='china') VALUES ('s1','c1',100.1),('s2','c2',100.2),('s3','c3',100.3);

-- Query the partitioned table sale_detail.
SET odps.sql.allow.fullscan=true;
SELECT * FROM sale_detail;

-- Result
+------------+-------------+-------------+------------+------------+
| shop_name  | customer_id | total_price | sale_date  | region     |
+------------+-------------+-------------+------------+------------+
| s1         | c1          | 100.1       | 2013       | china      |
| s2         | c2          | 100.2       | 2013       | china      |
| s3         | c3          | 100.3       | 2013       | china      |
+------------+-------------+-------------+------------+------------+

Type conversion and expressions

Comparing DATE and TIMESTAMP_NTZ types

  • When BigQuery compatibility mode is enabled: You can directly compare DATE and TIMESTAMP_NTZ values. A DATE value is treated as a TIMESTAMP_NTZ value at midnight (00:00:00) on that day.

  • When BigQuery compatible mode is disabled: Direct comparison between DATE and TIMESTAMP_NTZ types is not supported and causes an operand type mismatch error. You must use an explicit cast to convert them to a common type.

-- With BigQuery compatible mode enabled: DATE is treated as TIMESTAMP_NTZ '2025-01-01 00:00:00' for comparison.
SET odps.sql.BigQuery.compatible = true;
-- Returns true.
SELECT DATE '2025-01-01' < TIMESTAMP_NTZ '2025-01-01 00:00:30'; 

-- With BigQuery compatible mode disabled: A type mismatch error is returned.
SET odps.sql.BigQuery.compatible = false;
-- Returns an error: FAILED: ODPS-0130071:[1,8] Semantic analysis exception - invalid operand type(s) DATE,TIMESTAMP_NTZ for operator '<'
SELECT DATE '2025-01-01' < TIMESTAMP_NTZ '2025-01-01 00:00:30';    

Automatic conversion of date and time string literals

  • When BigQuery compatible mode is enabled: SQL string literals in yyyy-MM-dd format are automatically converted to DATE or TIMESTAMP when used in binary operations or in GREATEST/LEAST functions.

  • When BigQuery compatible mode is disabled: string literals are not automatically converted. For example, '2025-01-01' + 1 is treated as a numeric operation and returns NULL because the string cannot be converted to a valid number. Mixing a string literal with a date type results in a type error.

Note: This implicit conversion rule applies only to string literals (constants) in SQL. It does not automatically treat a STRING column as a date column.
-- With BigQuery compatible mode enabled
SET odps.sql.BigQuery.compatible = true;
-- Returns 2025-01-02 (The literal is converted to DATE, and 1 day is added.)
SELECT '2025-01-01' + 1;               
-- Returns 1 00:00:00.000000000 (Returns an INTERVAL_DAY_TIME type.)
SELECT DATE '2025-01-02' - '2025-01-01';
-- Returns 2025-01-02 00:00:00 (Returns a TIMESTAMP type.)
SELECT '2025-01-01' + INTERVAL 1 DAY;

-- With BigQuery compatible mode disabled
SET odps.sql.BigQuery.compatible = false;
-- Returns NULL (The string fails to convert to a number.)
SELECT '2025-01-01' + 1;            
-- Returns an error: FAILED: ODPS-0130071:[1,8] Semantic analysis exception - invalid operand type(s) DATE,STRING for operator '-'
SELECT DATE '2025-01-02' - '2025-01-01'; 
-- Returns an error: FAILED: ODPS-0130071:[1,8] Semantic analysis exception - invalid operand type(s) STRING,INTERVAL_DAY_TIME for operator '+'
SELECT '2025-01-01' + INTERVAL 1 DAY; 

Result type differences for DATE and INTERVAL operations

  • When BigQuery compatibility mode is enabled: Adding an INTERVAL_DAY_TIME value (such as days or hours) to, or subtracting one from, a DATE value produces a TIMESTAMP_NTZ result.

  • When BigQuery compatible mode is disabled: The result of such an operation is TIMESTAMP.

Note: In both modes, operations between DATE and INTERVAL_DAY_TIME retain the time component. Operations between DATE and INTERVAL_YEAR_MONTH (such as months or years) always produce a DATE result.

Expression

Standard mode

BigQuery compatible mode

DATE + INTERVAL 3 HOUR

TIMESTAMP (with time zone)

TIMESTAMP_NTZ (without time zone)

DATE + INTERVAL 1 MONTH

DATE

DATE

-- With BigQuery compatible mode enabled
SET odps.sql.BigQuery.compatible = true;
-- Returns 2025-01-01 03:00:00 (Returns a TIMESTAMP_NTZ type.)
SELECT DATE '2025-01-01' + INTERVAL 3 HOUR;  
-- Returns 2025-02-01 (Returns a DATE type.)
SELECT DATE '2025-01-01' + INTERVAL 1 MONTH;  

-- With BigQuery compatible mode disabled
SET odps.sql.BigQuery.compatible = false;
-- Returns 2025-01-01 03:00:00 (Returns a TIMESTAMP type.)
SELECT DATE '2025-01-01' + INTERVAL 3 HOUR;  
-- Returns 2025-02-01 (Returns a DATE type.)
SELECT DATE '2025-01-01' + INTERVAL 1 MONTH;  

Date string conversion in GREATEST and LEAST

  • When BigQuery compatibility mode is enabled: If GREATEST or LEAST receives a DATE argument, string literals in yyyy-m-d or yyyy-MM-dd format are automatically converted to DATE values for comparison.

  • When BigQuery compatible mode is disabled: string literals are not automatically converted. Mixing STRING and DATE arguments causes a type mismatch error, requiring an explicit cast.

Note: This rule does not extend to other functions such as COALESCE. Strings that do not conform to a valid date format will cause a conversion error.
-- With BigQuery compatible mode enabled
SET odps.sql.BigQuery.compatible = true;
-- Returns 2025-01-03 (Returns a DATE type.)
SELECT GREATEST('2025-01-01', DATE '2025-01-03');  
-- Returns 2025-01-01 (Returns a DATE type.)
SELECT LEAST('2025-01-01', DATE '2025-01-03'); 

-- With BigQuery compatible mode disabled
SET odps.sql.BigQuery.compatible = false;
-- Returns an error: FAILED: ODPS-0130071:[1,8] Semantic analysis exception - function greatest cannot match any overloaded functions with (STRING, DATE), candidates are DOUBLE GREATEST(VOID arg0...); T GREATEST(T arg0...)
SELECT GREATEST('2025-01-01', DATE '2025-01-03');  
-- Returns an error: FAILED: ODPS-0130071:[1,8] Semantic analysis exception - function least cannot match any overloaded functions with (STRING, DATE), candidates are DOUBLE LEAST(VOID arg0...); T LEAST(T arg0...)
SELECT LEAST('2025-01-01', DATE '2025-01-03');   

Common type promotion in UNION and UNION ALL

  • When BigQuery compatible mode is enabled: For UNION operations between DATE and TIMESTAMP_NTZ, the common type is promoted to TIMESTAMP_NTZ. For UNION operations between STRING and a date/time type, the result is promoted to the corresponding date/time type, and the string values are converted to that date/time type.

  • When BigQuery compatible mode is disabled: common type promotion does not occur between different types. These UNION operations cause a type mismatch error. You must use an explicit cast to convert the columns to a common type.

-- With BigQuery compatible mode enabled
SET odps.sql.BigQuery.compatible = true;
SELECT DATE '2025-01-01' AS value
UNION ALL SELECT TIMESTAMP_NTZ '2025-01-02 08:30:00';
-- Result (Result column type: TIMESTAMP_NTZ)
+-------+
| value |
+-------+
| 2025-01-01 00:00:00 |
| 2025-01-02 08:30:00 |
+-------+
SELECT '2025-01-01' AS value
UNION ALL SELECT DATE '2025-01-02';
-- Result (Result column type: DATE)
+-------+
| value |
+-------+
| 2025-01-01 |
| 2025-01-02 |
+-------+

-- With BigQuery compatible mode disabled: Both UNION operations above return a type mismatch error.
SET odps.sql.BigQuery.compatible = false;
-- Returns an error: FAILED: ODPS-0130241:[2,18] Illegal union operation - type mismatch for column 0 of UNION, left is DATE while right is TIMESTAMP_NTZ, left column name is value and right column name is _c0
SELECT DATE '2025-01-01' AS value
UNION ALL SELECT TIMESTAMP_NTZ '2025-01-02 08:30:00';
-- Returns an error: FAILED: ODPS-0130241:[2,18] Illegal union operation - type mismatch for column 0 of UNION, left is STRING while right is DATE, left column name is value and right column name is _c0
SELECT '2025-01-01' AS value
UNION ALL SELECT DATE '2025-01-02';

NULL fields in STRUCT constructors

  • When BigQuery compatibility mode is enabled: You can use untyped NULL values directly in STRUCT expressions. The field is treated as STRING by default. To use another type, explicitly cast the value.

  • When BigQuery compatible mode is disabled: Using an untyped NULL value for a field causes an error.

Note: This capability applies only to the STRUCT(... AS ...) syntax. The NAMED_STRUCT(...) function does not accept untyped NULL values in either mode.

For more information about constructing and using the STRUCT data type, see STRUCT data type.

-- With BigQuery compatible mode enabled
SET odps.sql.BigQuery.compatible = true;
-- The statement succeeds. The untyped NULL field is treated as STRING.
SELECT STRUCT(NULL AS c1, 1 AS c2);

-- With BigQuery compatible mode disabled
SET odps.sql.BigQuery.compatible = false;
-- Returns an error for the untyped NULL.
SELECT STRUCT(NULL AS c1, 1 AS c2);

Relaxed implicit conversion for DECIMAL

  • When BigQuery compatible mode is enabled: Restrictions on implicit conversion are relaxed for writing or assigning DECIMAL values.

  • When BigQuery compatible mode is disabled: The scenarios described below are rejected, requiring an explicit cast.

Scenario

BigQuery compatible mode

Standard mode

Assigning a DECIMAL value to a DOUBLE column

Allowed

Rejected (due to potential precision loss)

Assigning between DECIMAL types with different precision or scale

Allowed (may cause rounding or overflow)

Rejected (requires an exact match)

Note: The following examples require DECIMAL 2.0 to be enabled: SET odps.sql.decimal.odps2 = true;. Reducing the precision or scale can cause rounding or overflow. For production jobs, use an explicit CAST and check boundary values before migration.
CREATE TABLE t_dbl (v DOUBLE);
CREATE TABLE t_dec (v DECIMAL(5,1));

-- With BigQuery compatible mode enabled: Both INSERT statements succeed.
SET odps.sql.BigQuery.compatible = true;
INSERT INTO t_dbl SELECT CAST(1.25 AS DECIMAL(10,2));   -- DECIMAL → DOUBLE
INSERT INTO t_dec SELECT CAST(1.25 AS DECIMAL(10,2));   -- DECIMAL(10,2) → DECIMAL(5,1)

-- With BigQuery compatible mode disabled: Both INSERT statements fail and require an explicit CAST.
SET odps.sql.BigQuery.compatible = false;
-- Returns an error: FAILED: ODPS-0130071:[1,26] Semantic analysis exception - column _c0 in source has incompatible type DECIMAL(10,2) with destination column v, which has type DOUBLE. Implicit conversion is not applied because of potential data loss, but you can still consider to use explicit CAST. Please also note that the implicit conversion rule may be affected by altering odps.sql.type.system.odps2 or odps.sql.hive.compatible flag.
INSERT INTO t_dbl SELECT CAST(1.25 AS DECIMAL(10,2)); 
-- Returns an error: FAILED: ODPS-0130071:[1,26] Semantic analysis exception - column _c0 in source has incompatible type DECIMAL(10,2) with destination column v, which has type DECIMAL(5,1). Implicit conversion is not applied because of potential data loss, but you can still consider to use explicit CAST. Please also note that the implicit conversion rule may be affected by altering odps.sql.type.system.odps2 or odps.sql.hive.compatible flag.
INSERT INTO t_dec SELECT CAST(1.25 AS DECIMAL(10,2));

Query semantics and syntax

Priority of SELECT aliases in GROUP BY, HAVING, and QUALIFY

In both modes, SELECT aliases can be referenced in GROUP BY, HAVING, and QUALIFY clauses. However, name resolution precedence differs when a SELECT alias has the same name as an input column.

  • Enable BigQuery compatibility mode: Aliases take precedence, and a name is resolved to the alias expression in the SELECT statement.

  • If BigQuery compatibility mode is not enabled, the input column name takes precedence and is resolved as the input column.

In BigQuery compatible mode, to reference the input column instead of the alias, qualify the column name with a table alias, such as t.a.
-- Enable BigQuery compatible mode
SET odps.sql.BigQuery.compatible = true;
-- The input subquery has columns 'a' and 'b'. Here, 'b' is aliased as 'a', and the result is grouped by 'a'.
SELECT b AS a, COUNT(*) AS c
FROM (SELECT 1 AS a, 10 AS b UNION ALL SELECT 2 AS a, 10 AS b UNION ALL SELECT 3 AS a, 20 AS b) t
GROUP BY a;
-- Result
+------+------------+
| a    | c          |
+------+------------+
| 10   | 2          |
| 20   | 1          |
+------+------------+

-- Disable BigQuery compatible mode
SET odps.sql.BigQuery.compatible = false;
-- GROUP BY binds to the input column 'a'. The column 'b' does not appear in the grouping key.
SELECT b AS a, COUNT(*) AS c
FROM (SELECT 1 AS a, 10 AS b UNION ALL SELECT 2 AS a, 10 AS b UNION ALL SELECT 3 AS a, 20 AS b) t
GROUP BY a;
-- An error is returned: FAILED: ODPS-0130071:[1,8] Semantic analysis exception - column reference t.b should appear in GROUP BY key

GROUP BY ALL

  • In BigQuery compatibility mode: GROUP BY ALL is supported. The system automatically infers the grouping keys from the SELECT list. Aggregate expressions, window expressions, and constants are not used as grouping keys. Instead, the non-aggregate input columns that they reference are added to the grouping keys as needed.

  • Outside BigQuery compatible mode: GROUP BY ALL is not supported. The bare identifier all is parsed as a column name. If no column named all exists, an error occurs.

If an input table has a column named all, the all in GROUP BY ALL is interpreted as a reference to that column (that is, to group by the all column) and no longer functions as the keyword that automatically infers all grouping keys. In this case, if the SELECT list contains other non-aggregated columns, an error occurs because these columns are not included in the grouping key. To avoid ambiguity, explicitly specify the grouping columns or assign the column an alias.

For more information about GROUP BY, see SELECT Syntax.

-- Create a sample table
CREATE TABLE IF NOT EXISTS sales (
    region STRING,
    product STRING,
    amount BIGINT
);

-- Insert test data, including multiple combinations of region and product
INSERT INTO sales VALUES
    ('North', 'Apple', 100),
    ('North', 'Apple', 150),
    ('North', 'Banana', 80),
    ('South', 'Apple', 200),
    ('South', 'Banana', 120),
    ('South', 'Banana', 130);

-- Enable BigQuery compatible mode
SET odps.sql.BigQuery.compatible = true;
SELECT region, product, SUM(amount) AS total_amount
FROM sales
GROUP BY ALL;
-- Result (equivalent to GROUP BY region, product)
+--------+---------+--------------+
| region | product | total_amount |
+--------+---------+--------------+
| North  | Apple   | 250          |
| North  | Banana  | 80           |
| South  | Apple   | 200          |
| South  | Banana  | 250          |
+--------+---------+--------------+
-- Constants are not included in the grouping key, so the query groups only by region.
SELECT region, 'online' AS channel, SUM(amount) FROM sales GROUP BY ALL; 
-- Result
+--------+---------+------------+
| region | channel | _c2        |
+--------+---------+------------+
| North  | online  | 330        |
| South  | online  | 450        |
+--------+---------+------------+
 -- With only aggregate expressions, a global aggregation is performed.
SELECT SUM(amount) FROM sales GROUP BY ALL;     
-- Result
+------------+
| _c0        |
+------------+
| 780        |
+------------+

-- Disable BigQuery compatible mode
SET odps.sql.BigQuery.compatible = false;
SELECT region, product, SUM(amount) AS total_amount
FROM sales
GROUP BY ALL;
-- An error is returned: FAILED: ODPS-0130071:[3,10] Semantic analysis exception - column `all` cannot be resolved
ODPS-0130071:[1,8] Semantic analysis exception - column reference sales.region should appear in GROUP BY key
ODPS-0130071:[1,16] Semantic analysis exception - column reference sales.product should appear in GROUP BY key

SELECT * EXCEPT with duplicate column names

  • In BigQuery compatible mode: * EXCEPT(name) excludes all expanded columns that match the specified name. This does not cause an ambiguity error even if multiple input tables contain a column with the same name.

  • Outside BigQuery compatible mode: If the excluded column name exists in multiple input tables (for example, if both tables have an id column), * EXCEPT(id) fails with an ambiguity error.

-- Sample data
CREATE TABLE IF NOT EXISTS shop_info
(
    shop_name   STRING,
    city        STRING,
    manager     STRING
);
INSERT INTO shop_info VALUES ('s1', 'Beijing', 'Tom'), ('s2', 'Shanghai', 'Jerry');

-- Enable BigQuery compatible mode
SET odps.sql.BigQuery.compatible = true;
-- The shop_name columns from both the left and right tables are excluded. A non-existent column still causes a column resolution error.
SELECT * EXCEPT(shop_name)
FROM sale_detail AS l JOIN shop_info AS r ON l.shop_name = r.shop_name;
-- Result
+-------------+-------------+-----------+--------+------+---------+
| customer_id | total_price | sale_date | region | city | manager |
+-------------+-------------+-----------+--------+------+---------+
| c1          | 100.1       | 2013      | china  | Beijing | Tom     |
| c2          | 100.2       | 2013      | china  | Shanghai | Jerry   |
+-------------+-------------+-----------+--------+------+---------+

-- Disable BigQuery compatible mode: The statement above fails with an ambiguity error because shop_name exists in both tables.
SET odps.sql.bigquery.compatible = false;
-- An error is returned: FAILED: ODPS-0130071:[1,17] Semantic analysis exception - shop_name is ambiguouscan be both l.shop_name or r.shop_name
SELECT * EXCEPT(shop_name)
FROM sale_detail AS l JOIN shop_info AS r ON l.shop_name = r.shop_name;

Coalesced and original columns in JOIN ... USING

  • In BigQuery compatibility mode: JOIN ... USING(key) produces a coalesced column named key that you can access without qualification. It also preserves the original columns from the left (l.key) and right (r.key) tables, which you must qualify with table aliases. SELECT * outputs only the coalesced column.

  • Outside BigQuery compatible mode: USING(key) produces only a single coalesced column key. The original columns l.key and r.key cannot be accessed; referencing them results in a "cannot be resolved" error.

The value of the coalesced column follows standard USING semantics, which are consistent across both modes:

Join type

Value

INNER JOIN, LEFT JOIN

Join column from the left table

RIGHT JOIN

Join column from the right table

FULL JOIN

COALESCE(join column from left table, join column from right table)

-- Enable BigQuery compatible mode: You can access both the coalesced column and the original columns.
SET odps.sql.BigQuery.compatible = true;
WITH l AS (SELECT 1 AS id, 'L' AS left_value),
     r AS (SELECT 2 AS id, 'R' AS right_value)
SELECT id, l.id AS left_id, r.id AS right_id, left_value, right_value
FROM l FULL JOIN r USING (id);
-- Result
+------+---------+----------+------------+-------------+
| id   | left_id | right_id | left_value | right_value |
+------+---------+----------+------------+-------------+
| 2    | NULL    | 2        | NULL       | R           |
| 1    | 1       | NULL     | L          | NULL        |
+------+---------+----------+------------+-------------+

-- Disable BigQuery compatible mode: Referencing l.id or r.id results in a "cannot be resolved" error.
SET odps.sql.BigQuery.compatible = false;
WITH l AS (SELECT 1 AS id, 'L' AS left_value),
     r AS (SELECT 2 AS id, 'R' AS right_value)
SELECT id, l.id AS left_id, r.id AS right_id, left_value, right_value
FROM l FULL JOIN r USING (id);
-- An error is returned: FAILED: ODPS-0130071:[3,14] Semantic analysis exception - column l.id cannot be resolved
ODPS-0130071:[3,31] Semantic analysis exception - column r.id cannot be resolv

Accessing STRUCT fields by index

  • In BigQuery compatible mode: STRUCT fields can be accessed by their zero-based integer literal index. The index must be a non-negative integer. An error is returned if the index is out of bounds.

  • Outside BigQuery compatible mode: Accessing STRUCT fields by using an integer index is not supported.

-- Enable BigQuery compatible mode
SET odps.sql.BigQuery.compatible = true;
SELECT STRUCT(10 AS id, 'Alice' AS name)[0],   
       STRUCT(10 AS id, 'Alice' AS name)[1];   
-- Result
+------+------+
| id   | name |
+------+------+
| 10   | Alice |
+------+------+

-- Disable BigQuery compatible mode
SET odps.sql.BigQuery.compatible = false;
SELECT STRUCT(10 AS id, 'Alice' AS name)[0],   
       STRUCT(10 AS id, 'Alice' AS name)[1];   
-- An error is returned: FAILED: ODPS-0130071:[1,8] Semantic analysis exception - invalid operand type(s) struct<id:INT,name:STRING>,INT for operator '[]'
ODPS-0130071:[2,8] Semantic analysis exception - invalid operand type(s) struct<id:INT,name:STRING>,INT for operator '[]'

Correlated subquery restrictions

  • In BigQuery compatible mode: A correlated subquery that references a column from an outer query does not support ORDER BY, QUALIFY, or window functions. Using them results in a compilation error. As a workaround, perform the sorting or window calculation in a separate subquery and then join its result with the data from the outer query.

  • Outside BigQuery compatible mode: These restrictions do not apply to correlated subqueries. Queries containing ORDER BY, QUALIFY, or window functions execute and return results normally.

-- In BigQuery compatible mode, the following three types of correlated subqueries are rejected.
SET odps.sql.BigQuery.compatible = true;
-- 1) Correlated subquery with ORDER BY
SELECT (
  SELECT total_price 
  FROM sale_detail AS c
  WHERE c.customer_id = p.customer_id
  ORDER BY sale_date DESC 
  LIMIT 1
) 
FROM sale_detail AS p;
-- An error is returned: FAILED: ODPS-0130071:[5,3] Semantic analysis exception - correlated subquery cannot support ORDER BY clause

-- 2) Correlated subquery with QUALIFY
SELECT (
  SELECT total_price 
  FROM sale_detail AS c
  WHERE c.customer_id = p.customer_id
  QUALIFY ROW_NUMBER() OVER (ORDER BY sale_date DESC) = 1
) 
FROM sale_detail AS p;
-- An error is returned: FAILED: ODPS-0130071:[5,3] Semantic analysis exception - correlated subquery cannot support QUALIFY clause
ODPS-0130071:[5,11] Semantic analysis exception - correlated subquery cannot support WINDOW function

-- 3) Correlated subquery with a window function
SELECT (
  SELECT ROW_NUMBER() OVER (ORDER BY sale_date) 
  FROM sale_detail AS c
  WHERE c.customer_id = p.customer_id 
  LIMIT 1
) 
FROM sale_detail AS p;
-- An error is returned: FAILED: ODPS-0130071:[5,3] Semantic analysis exception - correlated subquery cannot support ORDER BY clause
ODPS-0130071:[2,10] Semantic analysis exception - correlated subquery cannot support WINDOW function


-- Outside BigQuery compatible mode, all three queries above execute successfully.
SET odps.sql.BigQuery.compatible = false;
-- 1) Correlated subquery with ORDER BY
SELECT (
  SELECT total_price 
  FROM sale_detail AS c
  WHERE c.customer_id = p.customer_id
  ORDER BY sale_date DESC 
  LIMIT 1
) 
FROM sale_detail AS p;
-- Result
+------------+
| _c0        |
+------------+
| 100.1      |
| NULL       |
| NULL       |
+------------+

-- 2) Correlated subquery with QUALIFY
SELECT (
  SELECT total_price 
  FROM sale_detail AS c
  WHERE c.customer_id = p.customer_id
  QUALIFY ROW_NUMBER() OVER (ORDER BY sale_date DESC) = 1
) 
FROM sale_detail AS p;
-- Result
+------------+
| _c0        |
+------------+
| NULL       |
| NULL       |
| 100.3      |
+------------+
-- 3) Correlated subquery with a window function
SELECT (
  SELECT ROW_NUMBER() OVER (ORDER BY sale_date) 
  FROM sale_detail AS c
  WHERE c.customer_id = p.customer_id 
  LIMIT 1
) 
FROM sale_detail AS p;
-- Result
+------------+
| _c0        |
+------------+
| 1          |
| 2          |
| 3          |
+------------+

Increased column name length limit

  • In BigQuery compatible mode: The maximum column name length is 256 bytes. An error is returned if this limit is exceeded.

  • Outside BigQuery compatible mode: The column name length is limited to 128 bytes. An error is returned if this limit is exceeded.

This length limit applies only to column names that are written to the table schema, such as columns created by CREATE TABLE or CREATE TABLE AS SELECT statements. Temporary column aliases in standard SELECT queries are not subject to this limit.

For more information about SQL limits, see SQL Usage Limits.

PIVOT output column names

  • In BigQuery compatible mode:

    • PIVOT (agg1 as a for axis1 in ('1', '2', '3', ...)): The resulting column names are the alias of the aggregate function followed by an underscore and the pivoted value, such as a_'1', a_'2', ...

    • PIVOT (agg1 as a, agg2 as b for axis1 in ('1', '2', '3', ...)): After the PIVOT operation, the column names are formatted as alias_value, such as a_'1', a_'2', ..., b_'1', b_'2', ...

    • PIVOT (agg1 as a, agg2 for axis1 in (expr1, expr2, '3', ...)): After the PIVOT operation, the column name is a combination of the aggregate function alias and the expression alias or value, separated by an underscore, i.e., a_generated_alias2, a_generated_alias3, a_'3' ..., generated_alias1_generated_alias2, generated_alias1_generated_alias3, generated_alias1_'3'....

  • Outside BigQuery compatible mode:

    • PIVOT (agg1 as a for axis1 in ('1', '2', '3', ...)): After the PIVOT operation, the resulting column names are in the format value_AggregateFunctionAlias, such as '1'_a, '2'_a, and so on.

    • PIVOT (agg1 as a, agg2 as b for axis1 in ('1', '2', '3', ...)): After the PIVOT operation, the column names are value_aggregate-function-alias, that is, '1'_a, '2'_a, ..., '1'_b, '2'_b, ...

    • PIVOT (agg1 as a, agg2 for axis1 in (expr1, expr2, '3', ...)): The column name after PIVOT processing is value or expression alias_aggregate function alias. For example, generated_alias2_a, generated_alias3_a,'3'_a ..., generated_alias2_generated_alias1, generated_alias3_generated_alias1,'3'_ generated_alias1....

As a best practice, explicitly specify aliases for aggregate functions and for values (constants or expressions) in the IN clause instead of relying on system-generated names.

For more information about PIVOT, see PIVOT and UNPIVOT.

-- Outside BigQuery compatible mode
SELECT  *
FROM    (
            SELECT  productid
                    ,tran_amt
            FROM    mf_cop_sales
        ) 
PIVOT (SUM(tran_amt)
AS sumbypro FOR productid IN ('apple','orange','banana'))
;
-- Result
+------------------+-------------------+-------------------+
| 'apple'_sumbypro | 'orange'_sumbypro | 'banana'_sumbypro |
+------------------+-------------------+-------------------+
| 1700             | 1900              | 1800              |
+------------------+-------------------+-------------------+

-- Enable BigQuery compatible mode
SET odps.sql.BigQuery.compatible=true;
SELECT  *
FROM    (
            SELECT  productid
                    ,tran_amt
            FROM    mf_cop_sales
        ) 
PIVOT (SUM(tran_amt)
AS sumbypro FOR productid IN ('apple','orange','banana'))
;
-- Result
+------------------+-------------------+-------------------+
| sumbypro_'apple' | sumbypro_'orange' | sumbypro_'banana' |
+------------------+-------------------+-------------------+
| 1700             | 1900              | 1800              |
+------------------+-------------------+-------------------+

Deduplication of duplicate column names

  • In BigQuery compatible mode: When a subquery or CTE (Common Table Expression) produces duplicate output column names, the system automatically appends a numeric suffix, such as _1 or _2, to duplicate names. This ensures that all column names in the result set are unique.

  • Outside BigQuery compatible mode: Duplicate output column names are not allowed. The system returns an ambiguity error, including when SELECT * is used.

The automatic suffix only ensures unique column names in the result structure; it does not change name resolution rules. An outer query such as SELECT a FROM t still fails with an ambiguity error. The auto-generated name a_1 is not a stable SQL alias and cannot be reliably used in queries. For CREATE TABLE AS SELECT (CTAS) queries, you must explicitly provide unique column names.
-- Enable BigQuery compatible mode
SET odps.sql.BigQuery.compatible = true;
WITH t AS (SELECT 1 AS a, 2 AS a) SELECT * FROM t; 
-- Result
+------+------+
| a    | a_1  |
+------+------+
| 1    | 2    |
+------+------+

-- Disable BigQuery compatible mode: The preceding statement results in an ambiguity error.
SET odps.sql.BigQuery.compatible = false;
WITH t AS (SELECT 1 AS a, 2 AS a) SELECT * FROM t;
-- An error is returned: FAILED: ODPS-0130071:[1,6] Semantic analysis exception - column reference @cte_1786696757513.a is ambiguous
ODPS-0130071:[1,49] Semantic analysis exception - column reference t.a is ambiguous

Functions

String functions

SUBSTR / SUBSTRING

  • When BigQuery compatibility mode is enabled: A starting position of 0 or a negative position whose absolute value exceeds the string length is treated as 1. A negative length always throws an error, regardless of the odps.sql.udf.strict.mode setting.

  • When BigQuery compatibility mode is disabled: A starting position of 0 or an out-of-range position returns an empty string. The behavior for a negative length is controlled by odps.sql.udf.strict.mode: an empty string is returned when this mode is disabled (false), and an error is thrown when it is enabled (true).

Scenario

BigQuery compatible mode

Standard mode

Starting position is 0

Treated as 1

Returns an empty string

Absolute value of a negative starting position exceeds the string length

Treated as 1

Returns an empty string

Negative length

Throws an error

Returns an empty string if odps.sql.udf.strict.mode=false; throws an error if true.

For a complete description and detailed usage of this function, see SUBSTR.

-- BigQuery compatibility mode enabled
SET odps.sql.BigQuery.compatible = true;
-- Returns 'ab'
SELECT SUBSTR('abc', 0, 2);
-- Returns 'ab'
SELECT SUBSTR('abc', -5, 2);
-- Returns 'hello'
SELECT SUBSTR('hello', 0);
-- Throws an error: FAILED: ODPS-0130071:[0,0] Semantic analysis exception - physical plan generation failed: SQL Runtime Unretryable Error: ODPS-0121095:Invalid argument - in function substr/substring, unsupported length value -1
SELECT SUBSTR('abc', 1, -1);


-- BigQuery compatibility mode disabled (default is false)
SET odps.sql.BigQuery.compatible = false;
-- Returns an empty string
SELECT SUBSTR('abc', 0, 2);  
-- Returns an empty string
SELECT SUBSTR('hello', 0);  

SET odps.sql.udf.strict.mode = false;
-- Returns an empty string
SELECT SUBSTR('abc', 1, -1);   

SET odps.sql.udf.strict.mode = true;
-- Throws an error: FAILED: ODPS-0130071:[1,8] Semantic analysis exception - encounter runtime exception while evaluating function substr, detailed message: Argument value out of range - in function substr, unsupported length value: -1
SELECT SUBSTR('abc', 1, -1); 

REVERSE

  • When BigQuery compatibility mode is enabled: The REVERSE function reverses a string based on UTF-8 encoding, correctly preserving multi-byte characters (such as Chinese characters).

  • When BigQuery compatibility mode is disabled: The REVERSE function performs a byte-wise reversal. This can corrupt character boundaries and produce garbled characters if the string contains multi-byte characters.

BINARY and ARRAY inputs are reversed byte-wise in both modes and are not affected by this setting.

For a complete description and detailed usage of this function, see REVERSE.

-- BigQuery compatibility mode enabled
SET odps.sql.BigQuery.compatible = true;
-- Returns '台平算计'
SELECT REVERSE('计算平台'); 

-- BigQuery compatibility mode disabled
SET odps.sql.BigQuery.compatible = false;
-- Returns garbled characters (due to byte-wise reversal)
SELECT REVERSE('计算平台');  

REGEXP_EXTRACT / REGEXP_EXTRACT_ALL

This behavior affects only the two-argument form in which group_id is omitted. The form with an explicit group_id behaves the same in both modes. The differences are as follows:

  • When BigQuery compatibility mode is enabled: If the regular expression contains a group, the function returns the content matched by the first group. If the regular expression does not contain a group, it returns the substring that matches the entire pattern.

  • When BigQuery compatibility mode is disabled (default): The function always returns the content matched by the first group. If the regular expression does not contain a group, an error is thrown.

For a complete description and detailed usage of these functions, see REGEXP_EXTRACT and REGEXP_EXTRACT_ALL.

-- BigQuery compatibility mode enabled
SET odps.sql.BigQuery.compatible = true;
-- Returns '12' (no group; returns the full match)
SELECT REGEXP_EXTRACT('a12b', '[0-9]+'); 
-- Returns '12' (extracts the first group)
SELECT REGEXP_EXTRACT('a12b', 'a([0-9]+)b'); 
-- Returns [100, 200]
SELECT REGEXP_EXTRACT_ALL('100-200', '[0-9]+'); 

-- BigQuery compatibility mode disabled
SET odps.sql.BigQuery.compatible = false;
-- No capturing group, throws an error: FAILED: ODPS-0130071:[0,0] Semantic analysis exception - physical plan generation failed: SQL Runtime Unretryable Error: ODPS-0121065:Argument value out of range - Regex group count is 0, but the specified group index is 1
SELECT REGEXP_EXTRACT('a12b', '[0-9]+');     
-- Returns '12'
SELECT REGEXP_EXTRACT('a12b', 'a([0-9]+)b');    

Date and time functions

DATEDIFF

BIGINT|INT DATEDIFF(TIMESTAMP <end_date>, TIMESTAMP <start_date>[, STRING <date_part>

When both input parameters are of the TIMESTAMP type, the DATEDIFF result is calculated differently.

  • When BigQuery compatibility mode is enabled:

    • If date_part is YEAR, MONTH, WEEK, or ISOWEEK, the function truncates lower-level parts of the timestamps before calculating the difference. For example, the year difference between TIMESTAMP'2025-01-01' and TIMESTAMP'2024-12-31' is 1, and the month difference is also 1.

    • If date_part is DAY, HOUR, MINUTE, SECOND, MILLISECOND, or MICROSECOND, the function first calculates the total elapsed time and then converts it to the specified date_part. For example, the day difference between TIMESTAMP'2025-01-01 00:00:00' and TIMESTAMP'2024-12-31 23:59:59' is 0, and the hour difference is also 0.

  • When BigQuery compatibility mode is disabled:

    Lower-level parts of the timestamps are truncated based on date_part before the difference is calculated. For example, the year difference between DATE'2025-01-01' and DATE'2024-12-31' is 1, and the month difference is also 1.

For a complete description and detailed usage of this function, see DATEDIFF.

-- BigQuery compatibility mode enabled
SET odps.sql.BigQuery.compatible = true;
-- Returns 1 (Calculated based on the number of full units elapsed between the two timestamps. 62 seconds = 1 full minute.)
SELECT DATEDIFF(TIMESTAMP '2025-03-11 00:02:01',
                TIMESTAMP '2025-03-11 00:00:59',
                'mi');
                
-- BigQuery compatibility mode disabled
SET odps.sql.BigQuery.compatible = false;
-- Returns 2 (Values are truncated to the minute boundary before subtraction.)
SELECT DATEDIFF(TIMESTAMP '2025-03-11 00:02:01',
                TIMESTAMP '2025-03-11 00:00:59',
                'mi');

TO_DATE

  • When BigQuery compatibility mode is enabled:

    Added the TO_DATE(timestamp_value, timezone) overload to extract the date from a timestamp value based on a specified time zone. The time zone can be a supported named time zone or a time zone offset. If either the timestamp or timezone parameter is NULL, the result is NULL.

  • When BigQuery compatibility mode is disabled:

    Because there is no time zone overload, the second argument of TO_DATE(x, y) is parsed as a date format string, and passing a time zone name such as 'Asia/Shanghai' results in a format error.

-- BigQuery compatibility mode enabled
SET odps.sql.BigQuery.compatible = true;
-- Returns 2025-07-28
SELECT TO_DATE(TIMESTAMP '2025-07-29 00:30:00.123', 'Asia/Jakarta');

-- BigQuery compatibility mode disabled: The timezone parameter is not supported.
SET odps.sql.BigQuery.compatible = false;
-- Throws a format error: FAILED: ODPS-0130071:[0,0] Semantic analysis exception - physical plan generation failed: SQL Runtime Unretryable Error: ODPS-0121095:Invalid argument - format string doesn't have year part : Asia/Jakarta
SELECT TO_DATE(TIMESTAMP '2025-07-29 00:30:00.123', 'Asia/Jakarta');

JSON functions

TO_JSON

  • When BigQuery compatibility mode is enabled: When converting a STRUCT to JSON text, the function retains fields with NULL values.

  • When BigQuery compatibility mode is disabled: The function omits fields with NULL values.

For a complete description and detailed usage of this function, see TO_JSON.

-- BigQuery compatibility mode enabled
SET odps.sql.BigQuery.compatible = true;
-- Returns {"f0":null,"f1":"300"}
SELECT TO_JSON(STRUCT(CAST(NULL AS STRING) AS f0, '300' AS f1));

-- BigQuery compatibility mode disabled
SET odps.sql.BigQuery.compatible = false;
-- Returns {"f1":"300"}
SELECT TO_JSON(STRUCT(CAST(NULL AS STRING) AS f0, '300' AS f1));

Aggregate functions

STDDEV_SAMP / VAR_SAMP

  • When BigQuery compatibility mode is enabled: If a group contains only one non-NULL entry, the function returns NULL.

  • When BigQuery compatibility mode is disabled: In the same scenario, the function returns 0.

For a complete description and detailed usage of this function, see STDDEV_SAMP.

-- BigQuery compatibility mode enabled
SET odps.sql.BigQuery.compatible = true;
SELECT STDDEV_SAMP(value), VAR_SAMP(value) FROM VALUES (1.0) t(value);
-- Result
+------------+------------+
| _c0        | _c1        |
+------------+------------+
| NULL       | NULL       |
+------------+------------+

-- BigQuery compatibility mode disabled
SET odps.sql.BigQuery.compatible = false;
SELECT STDDEV_SAMP(value), VAR_SAMP(value) FROM VALUES (1.0) t(value);
-- Result
+------------+------------+
| _c0        | _c1        |
+------------+------------+
| 0.0        | 0.0        |
+------------+------------+

Other functions

Expanded timestamp format parsing

When converting a string to TIMESTAMP, BigQuery compatibility mode supports a wider range of ISO-style text (assuming the project time zone is Asia/Shanghai (+08)).

Format

BigQuery compatible mode

Standard mode

T separator

Supported

Not supported, returns NULL

Z / numeric time zone offset / named time zone

Recognizes the time zone and converts to the project time zone

Ignores the time zone marker and processes as local time

Up to 9 digits of fractional seconds

Supported

Supported (same in both modes)

-- BigQuery compatibility mode enabled
SET odps.sql.BigQuery.compatible = true;
-- Returns 2025-03-24 10:00:00
SELECT CAST('2025-03-24T10:00:00' AS TIMESTAMP); 
-- Returns 2025-03-24 18:00:00 (UTC 10:00 converted to +08:00)
SELECT CAST('2025-03-24 10:00:00Z' AS TIMESTAMP);      
-- Returns 2025-03-24 12:30:00 (converted from +05:30 to +08:00)
SELECT CAST('2025-03-24 10:00:00+05:30' AS TIMESTAMP);    
-- Returns 2025-03-24 09:00:00 (converted from Tokyo time (+09:00) to +08:00)
SELECT CAST('2025-03-24 10:00:00 Asia/Tokyo' AS TIMESTAMP);
-- Returns 2025-03-24 10:00:00.123456789
SELECT CAST('2025-03-24 10:00:00.123456789' AS TIMESTAMP); 

-- BigQuery compatibility mode disabled
SET odps.sql.BigQuery.compatible = false;
-- Returns NULL (T separator is not supported)
SELECT CAST('2025-03-24T10:00:00' AS TIMESTAMP); 
-- Returns 2025-03-24 10:00:00 (Ignores Z and processes as local time)
SELECT CAST('2025-03-24 10:00:00Z' AS TIMESTAMP);     
The preceding shows the raw value of the TIMESTAMP column, without the +08 suffix. The +08 suffix appears only when the TIMESTAMP value is converted to a STRING.

CAST(STRING AS BOOLEAN)

  • When BigQuery compatibility mode is enabled:

    Only the case-insensitive strings true and false are supported, which are converted to the BOOLEAN values true and false respectively. Any other non-empty string or an empty string throws an error. If the input is NULL, the result is NULL.

  • When BigQuery compatibility mode is disabled:

    The function converts any non-empty string (including 'false' and 'yes') to true. An empty string is converted to false.

-- BigQuery compatibility mode enabled
SET odps.sql.BigQuery.compatible = true;
-- Returns true
SELECT CAST('true'  AS BOOLEAN);  
-- Returns false 
SELECT CAST('FaLsE' AS BOOLEAN);   
-- Throws an error: FAILED: ODPS-0130071:[0,0] Semantic analysis exception - physical plan generation failed: SQL Runtime Unretryable Error: ODPS-0121095:Invalid argument - Bad bool value: yes
SELECT CAST('yes'   AS BOOLEAN);  
-- Throws an error: FAILED: ODPS-0130071:[0,0] Semantic analysis exception - physical plan generation failed: SQL Runtime Unretryable Error: ODPS-0121095:Invalid argument - Bad bool value: 1
SELECT CAST('1'     AS BOOLEAN); 
-- Throws an error: FAILED: ODPS-0130071:[0,0] Semantic analysis exception - physical plan generation failed: SQL Runtime Unretryable Error: ODPS-0121095:Invalid argument - Bad bool value: 
SELECT CAST(''      AS BOOLEAN);   

-- BigQuery compatibility mode disabled
SET odps.sql.BigQuery.compatible = false;
-- Returns true
SELECT CAST('yes'   AS BOOLEAN);   
-- Returns true
SELECT CAST('false' AS BOOLEAN);   
-- Returns false
SELECT CAST(''      AS BOOLEAN);   

CAST(TIMESTAMP AS STRING)

  • When BigQuery compatibility mode is enabled:

    • If the timestamp has fractional seconds, the function aligns the fractional part in the output to 3, 6, or 9 digits, pads it with zeros if necessary, and truncates excess trailing zeros.

    • The resulting STRING value includes a time zone offset (e.g., +00, +08) based on the current session or project time zone. The offset format is +HH or -HH (hours only). For time zones that are not on a full-hour offset, such as Asia/Kolkata (+05:30), the function drops the minute part, and the suffix becomes +05.

    • If the timestamp itself contains time zone information, the output is adjusted accordingly and displays the corresponding time zone offset.

  • When BigQuery compatibility mode is disabled:

    The function does not append a time zone offset, and it may truncate fractional seconds (e.g., .120 becomes .12).

-- The following examples assume the project time zone is Asia/Shanghai

-- BigQuery compatibility mode enabled
SET odps.sql.BigQuery.compatible = true;
-- Returns 2015-09-14 14:24:09.120+08
SELECT CAST(TIMESTAMP '2015-09-14 14:24:09.120' AS STRING);

-- BigQuery compatibility mode disabled
SET odps.sql.BigQuery.compatible = false;
-- Returns 2015-09-14 14:24:09.12 (no time zone offset, fractional seconds truncated)
SELECT CAST(TIMESTAMP '2015-09-14 14:24:09.120' AS STRING);

CAST(TIMESTAMP_NTZ AS STRING)

  • When BigQuery compatibility mode is enabled:

    If the timestamp has fractional seconds, the function aligns the fractional part in the output to 3, 6, or 9 digits, pads it with zeros if necessary, and truncates excess trailing zeros.

  • When BigQuery compatibility mode is disabled:

    Fractional seconds may be truncated (e.g., .120 becomes .12).

-- BigQuery compatibility mode enabled
SET odps.sql.BigQuery.compatible = true;
-- Returns 2015-09-14 14:24:09.120
SELECT CAST(TIMESTAMP_NTZ '2015-09-14 14:24:09.120' AS STRING);

-- BigQuery compatibility mode disabled
SET odps.sql.BigQuery.compatible = false;
-- Returns 2015-09-14 14:24:09.12 (fractional seconds truncated)
SELECT CAST(TIMESTAMP_NTZ '2015-09-14 14:24:09.120' AS STRING);

CAST(STRING AS TIMESTAMP)

  • When BigQuery compatibility mode is enabled:

    The function supports casting a date-only string to the TIMESTAMP type. The missing time component is automatically padded with zeros (00:00:00).

  • When BigQuery compatibility mode is disabled:

    The function accepts only full date-time formats. Parsing a date-only string fails and returns NULL.

-- BigQuery compatibility mode enabled
SET odps.sql.BigQuery.compatible = true;
-- Returns 2025-04-22 00:00:00
SELECT CAST('2025-04-22' AS TIMESTAMP);   

-- BigQuery compatibility mode disabled
SET odps.sql.BigQuery.compatible = false;
-- Returns NULL
SELECT CAST('2025-04-22' AS TIMESTAMP);