§ High Compatibility (Oracle Compatible)


GreatSQL is 100% fully compatible with MySQL and Percona usage, and supports most common Oracle usage, including Data Type Compatibility, Function Compatibility, SQL Syntax Compatibility, Stored Routine Compatibility, and many other compatible extension usages.

§ Overview of Oracle Compatibility Design

The priority principles of GreatSQL's Oracle compatibility handling are as follows:

  • GreatSQL-style SQL and stored routines can work directly in the default SQL MODE.
  • Oracle compatibility features that do not conflict with GreatSQL-style SQL and stored routines can also work directly in the default SQL MODE.
  • Features that have syntax or semantic conflicts with GreatSQL-style SQL and stored routines require the user to explicitly switch to Oracle MODE before they can work.

GreatSQL's Oracle compatibility is mainly implemented through the following three different approaches:

  • Native mode: Oracle and GreatSQL both support or partially support common SQL standards. For some simple statements, compatibility is achieved without any adjustment.
  • Compatible extension: For some functions and syntax that are unique to Oracle, GreatSQL implements extensions at the GreatSQL Server layer, such as MERGE INTO and CONNECT BY. These types of compatibility features can be used directly without setting an additional SQL MODE.
  • Compatible mode: Setting SET sql_mode = ORACLE; in GreatSQL switches the current session to Oracle compatible mode. In this mode, when Oracle syntax conflicts with GreatSQL syntax in syntax or semantics, GreatSQL automatically chooses the Oracle compatible mode.

For more information about the Oracle compatible mode, please see the document: Oracle mode.

§ Data Type Compatibility

In GreatSQL, data type compatibility is implemented through mapping, which belongs to the extended compatibility (no need to set sql_mode) approach. The specific implementation method is simple aliasing, i.e., replacing keywords at the parsing stage. For example, if a table created with CLOB is used, it will be converted to LONGTEXT in the system.

The specific mappings implemented include:

Oracle type GreatSQL type Compatibility level
CLOB LONGTEXT Simple alias
NUMBER DECIMAL Simple alias
VARCHAR2 VARCHAR Simple alias
PLS_INTEGER INT Simple alias

Example:

greatsql> CREATE TABLE t1(
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, 
c1 CLOB NOT NULL DEFAULT '', 
c2 VARCHAR2(30) NOT NULL DEFAULT '');
ERROR 1101 (42000): BLOB, TEXT, GEOMETRY or JSON column 'c1' can't have a default value

greatsql> CREATE TABLE t1(
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, 
c1 CLOB NOT NULL, 
c2 VARCHAR2(30) NOT NULL DEFAULT '');
Query OK, 0 rows affected (0.25 sec)

greatsql> SHOW CREATE TABLE t1\G
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `c1` longtext NOT NULL,
  `c2` varchar(30) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.01 sec)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

For more details, please refer to Data Type Compatibility.

§ SQL Syntax Compatibility

§ SQL Syntax Supported by Extended Compatibility

The following SQL syntax is supported in GreatSQL without setting sql_mode = ORACLE:

§ SQL Syntax Supported in Compatible Mode

The following SQL syntax is supported in GreatSQL only after setting sql_mode = ORACLE:

§ Function Compatibility

§ Functions Supported by Extended Compatibility

The following functions are supported in GreatSQL without setting sql_mode = ORACLE:

§ Functions Supported in Compatible Mode

The following functions are supported in GreatSQL only after setting sql_mode = ORACLE:

Note: After setting sql_mode = ORACLE, the behavior of the above functions becomes closer to Oracle; otherwise, the native GreatSQL behavior is maintained.

§ Stored Routine Compatibility

GreatSQL supports Oracle-style stored routine usage. Some stored routines have undergone basic structural modifications in ORACLE mode. For details, see: Stored Routine Basic Structural Modifications.

§ Stored Routines Supported by Extended Compatibility

In GreatSQL, regardless of whether sql_mode is DEFAULT or ORACLE, the CREATE OR REPLACE extension is supported, but the behavior differs in different modes.

§ Stored Routines Supported in Compatible Mode

The following Oracle-compatible stored routine usages require setting sql_mode = ORACLE first (some usages are also supported in sql_mode = DEFAULT mode, but do not have Oracle compatibility features):

Example:

  • Stored routine usage in the Oracle environment:
CREATE OR REPLACE EDITIONABLE FUNCTION f0(delta INT DEFAULT 0) RETURN TIMESTAMP AS
    cnt INT := 10;
BEGIN
    RETURN SYSDATE + delta*cnt;
END;

SELECT f0(2) FROM DUAL ;
SELECT f0() FROM DUAL ;
1
2
3
4
5
6
7
8
  • Stored routine usage in GreatSQL native mode:
CREATE OR REPLACE FUNCTION f1(delta INT) RETURNS TIMESTAMP
BEGIN
    DECLARE cnt INT DEFAULT 10;
    SET delta = cnt * delta;
    RETURN DATE_ADD(SYSDATE(), INTERVAL delta DAY);
END;

SELECT f0(2) FROM DUAL ;
1
2
3
4
5
6
7
8
  • Stored routine usage in GreatSQL compatible mode:
SET sql_mode = ORACLE;

CREATE OR REPLACE FUNCTION f0(delta INT DEFAULT 0) RETURN TIMESTAMP AS
    cnt INT := 10;
BEGIN
    RETURN SYSDATE + delta*cnt;
END;

SELECT f0(2) FROM DUAL ;
SELECT f0() FROM DUAL ;
1
2
3
4
5
6
7
8
9
10

For common questions about Oracle compatibility, see: Oracle Compatibility FAQ.

Scan the QR code to follow our WeChat official account

greatsql-wx