docs/src/main/sphinx/connector/oracle.md
The Oracle connector allows querying and creating tables in an external Oracle database. Connectors let Trino join data provided by different databases, like Oracle and Hive, or different Oracle database instances.
To connect to Oracle, you need:
To configure the Oracle connector as the example catalog, create a file
named example.properties in etc/catalog. Include the following
connection properties in the file:
connector.name=oracle
# The correct syntax of the connection-url varies by Oracle version and
# configuration. The following example URL connects to an Oracle SID named
# "orcl".
connection-url=jdbc:oracle:thin:@example.net:1521:orcl
connection-user=root
connection-password=secret
The connection-url defines the connection information and parameters to pass
to the JDBC driver. The Oracle connector uses the Oracle JDBC Thin driver,
and the syntax of the URL may be different depending on your Oracle
configuration. For example, the connection URL is different if you are
connecting to an Oracle SID or an Oracle service name. See the Oracle
Database JDBC driver documentation
for more information.
The connection-user and connection-password are typically required and
determine the user credentials for the connection, often a service user. You can
use {doc}secrets </security/secrets> to avoid actual values in the catalog
properties files.
:::{note}
Oracle does not expose metadata comment via REMARKS column by default
in JDBC driver. You can enable it using oracle.remarks-reporting.enabled
config option. See Additional Oracle Performance Extensions
for more details.
:::
By default, the Oracle connector uses connection pooling for performance improvement. The below configuration shows the typical default values. To update them, change the properties in the catalog configuration file:
oracle.connection-pool.max-size=30
oracle.connection-pool.min-size=1
oracle.connection-pool.inactive-timeout=20m
oracle.connection-pool.wait-duration=3s
To disable connection pooling, update properties to include the following:
oracle.connection-pool.enabled=false
If you want to connect to multiple Oracle servers, configure another instance of the Oracle connector as a separate catalog.
To add another Oracle catalog, create a new properties file. For example, if
you name the property file sales.properties, Trino creates a catalog named
sales.
(oracle-fte-support)=
The connector supports {doc}/admin/fault-tolerant-execution of query
processing. Read and write operations are both supported with any retry policy.
The Oracle connector provides a schema for every Oracle database.
Run SHOW SCHEMAS to see the available Oracle databases:
SHOW SCHEMAS FROM example;
If you used a different name for your catalog properties file, use that catalog
name instead of example.
:::{note} The Oracle user must have access to the table in order to access it from Trino. The user configuration, in the connection properties file, determines your privileges in these schemas. :::
If you have an Oracle database named web, run SHOW TABLES to see the
tables it contains:
SHOW TABLES FROM example.web;
To see a list of the columns in the clicks table in the web
database, run either of the following:
DESCRIBE example.web.clicks;
SHOW COLUMNS FROM example.web.clicks;
To access the clicks table in the web database, run the following:
SELECT * FROM example.web.clicks;
(oracle-type-mapping)=
Because Trino and Oracle each support types that the other does not, this
connector {ref}modifies some types <type-mapping-overview> when reading or
writing data. Data types may not map the same way in both directions between
Trino and the data source. Refer to the following sections for type mapping in
each direction.
Trino supports selecting Oracle database types. This table shows the Oracle to Trino data type mapping:
:::{list-table} Oracle to Trino type mapping :widths: 30, 25, 50 :header-rows: 1
NUMBER(p, s)DECIMAL(pʹ, sʹ) or NUMBERDECIMAL when input data can be represented as Trino DECIMAL losslessly.
When 1 ≤ p ≤ 38 and 0 ≤ s ≤ p, then pʹ = p and sʹ = s, otherwise, a wider type is used. DECIMAL losslessly, maps to NUMBER.NUMBERNUMBERFLOAT[(p)]DOUBLEp exceeds 53, numeric values may be subject to precision loss.
The default precision of the FLOAT data type
in Oracle is 53.BINARY_FLOATREALBINARY_DOUBLEDOUBLEVARCHAR2(n CHAR)VARCHAR(n)VARCHAR2(n BYTE)VARCHAR(n)NVARCHAR2(n)VARCHAR(n)CHAR(n)CHAR(n)NCHAR(n)CHAR(n)CLOBVARCHARNCLOBVARCHARRAW(n)VARBINARYBLOBVARBINARYNo other types are supported.
Trino supports creating tables with the following types in an Oracle database. The table shows the mappings from Trino to Oracle data types:
:::{note}
For types not listed in the table below, Trino can't perform the CREATE TABLE <table> AS SELECT operations. When data is inserted into existing
tables, Oracle to Trino type mapping is used.
:::
:::{list-table} Trino to Oracle Type Mapping :widths: 30, 25, 50 :header-rows: 1
TINYINTNUMBER(3)SMALLINTNUMBER(5)INTEGERNUMBER(10)BIGINTNUMBER(19)DECIMAL(p, s)NUMBER(p, s)NUMBERNUMBERREALBINARY_FLOATDOUBLEBINARY_DOUBLEVARCHARNCLOBVARBINARYBLOBNo other types are supported.
(oracle-datetime-mapping)=
Writing a timestamp with fractional second precision (p) greater than 9
rounds the fractional seconds to nine digits.
Oracle DATE type stores hours, minutes, and seconds, so it is mapped
to Trino TIMESTAMP(0).
:::{warning}
Due to date and time differences in the libraries used by Trino and the
Oracle JDBC driver, attempting to insert or select a datetime value earlier
than 1582-10-15 results in an incorrect date inserted.
:::
(oracle-character-mapping)=
Trino's VARCHAR(n) maps to VARCHAR2(n CHAR) if n is no greater
than 4000. A larger or unbounded VARCHAR maps to NCLOB.
Trino's CHAR(n) maps to CHAR(n CHAR) if n is no greater than 2000.
A larger CHAR maps to NCLOB.
Using CREATE TABLE AS to create an NCLOB column from a CHAR value
removes the trailing spaces from the initial values for the column. Inserting
CHAR values into existing NCLOB columns keeps the trailing spaces. For
example:
CREATE TABLE vals AS SELECT CAST('A' as CHAR(2001)) col;
INSERT INTO vals (col) VALUES (CAST('BB' as CHAR(2001)));
SELECT LENGTH(col) FROM vals;
_col0
-------
2001
1
(2 rows)
Attempting to write a CHAR that doesn't fit in the column's actual size
fails. This is also true for the equivalent VARCHAR types.
:::{list-table} :widths: 20, 20, 50, 10 :header-rows: 1
oracle.number.default-scalenumber_default_scaleDECIMAL scale for Oracle NUMBER (without precision and
scale) date type. When not set then such column is treated as not supported.oracle.number.rounding-mode
number_rounding_mode
Rounding mode for the Oracle NUMBER data type. This is useful when Oracle
NUMBER data type specifies higher scale than is supported in Trino.
Possible values are:
UNNECESSARY - Rounding mode to assert that the
requested operation has an exact result,
hence no rounding is necessary.CEILING - Rounding mode to round towards
positive infinity.FLOOR - Rounding mode to round towards negative
infinity.HALF_DOWN - Rounding mode to round towards
nearest neighbor unless both neighbors are
equidistant, in which case rounding down is used.HALF_EVEN - Rounding mode to round towards the
nearest neighbor unless both neighbors are equidistant,
in which case rounding towards the even neighbor is
performed.HALF_UP - Rounding mode to round towards
nearest neighbor unless both neighbors are
equidistant, in which case rounding up is usedUP - Rounding mode to round towards zero.DOWN - Rounding mode to round towards zero.UNNECESSARY
:::
(oracle-sql-support)=
The connector provides read access and write access to data and metadata in Oracle. In addition to the globally available and read operation statements, the connector supports the following features:
(oracle-insert)=
(oracle-update)=
(oracle-delete)=
(oracle-alter-table)=
(oracle-procedures)=
(oracle-table-functions)=
The connector provides specific {doc}table functions </functions/table> to
access Oracle.
(oracle-query-function)=
query(varchar) -> tableThe query function allows you to query the underlying database directly. It
requires syntax native to Oracle, because the full query is pushed down and
processed in Oracle. This can be useful for accessing native features which are
not available in Trino or for improving query performance in situations where
running a query natively may be faster.
As a simple example, query the example catalog and select an entire table:
SELECT
*
FROM
TABLE(
example.system.query(
query => 'SELECT
*
FROM
tpch.nation'
)
);
As a practical example, you can use the MODEL clause from Oracle SQL:
SELECT
SUBSTR(country, 1, 20) country,
SUBSTR(product, 1, 15) product,
year,
sales
FROM
TABLE(
example.system.query(
query => 'SELECT
*
FROM
sales_view
MODEL
RETURN UPDATED ROWS
MAIN
simple_model
PARTITION BY
country
MEASURES
sales
RULES
(sales['Bounce', 2001] = 1000,
sales['Bounce', 2002] = sales['Bounce', 2001] + sales['Bounce', 2000],
sales['Y Box', 2002] = sales['Y Box', 2001])
ORDER BY
country'
)
);
The connector includes a number of performance improvements, detailed in the following sections.
Based on performance reasons, Trino disables support for Oracle SYNONYM. To
include SYNONYM, add the following configuration property:
oracle.synonyms.enabled=true
(oracle-pushdown)=
The connector supports pushdown for a number of operations:
join-pushdownlimit-pushdownIn addition, the connector supports {ref}aggregation-pushdown for the
following functions:
avg()count(), also count(distinct x)max()min()sum()Pushdown is only supported for DOUBLE type columns with the
following functions:
stddev() and {func}stddev_samp()stddev_pop()var_pop()variance() and {func}var_samp()Pushdown is only supported for REAL or DOUBLE type column
with the following functions:
covar_samp()covar_pop()(oracle-predicate-pushdown)=
The connector does not support pushdown of any predicates on columns that use
the CLOB, NCLOB, BLOB, or RAW(n) Oracle database types, or Trino
data types that {ref}map <oracle-type-mapping> to these Oracle database types.
In the following example, the predicate is not pushed down for either query
since name is a column of type VARCHAR, which maps to NCLOB in
Oracle:
SHOW CREATE TABLE nation;
-- Create Table
----------------------------------------
-- CREATE TABLE oracle.trino_test.nation (
-- name VARCHAR
-- )
-- (1 row)
SELECT * FROM nation WHERE name > 'CANADA';
SELECT * FROM nation WHERE name = 'CANADA';
In the following example, the predicate is pushed down for both queries
since name is a column of type VARCHAR(25), which maps to
VARCHAR2(25) in Oracle:
SHOW CREATE TABLE nation;
-- Create Table
----------------------------------------
-- CREATE TABLE oracle.trino_test.nation (
-- name VARCHAR(25)
-- )
-- (1 row)
SELECT * FROM nation WHERE name > 'CANADA';
SELECT * FROM nation WHERE name = 'CANADA';