docs/src/main/sphinx/connector/mariadb.md
The MariaDB connector allows querying and creating tables in an external MariaDB database.
To connect to MariaDB, you need:
To configure the MariaDB connector, create a catalog properties file in
etc/catalog named, for example, example.properties, to mount the MariaDB
connector as the example catalog. Create the file with the following
contents, replacing the connection properties as appropriate for your setup:
connector.name=mariadb
connection-url=jdbc:mariadb://example.net:3306
connection-user=root
connection-password=secret
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.
(mariadb-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 MariaDB connector provides a schema for every MariaDB database.
You can see the available MariaDB databases by running SHOW SCHEMAS:
SHOW SCHEMAS FROM example;
If you have a MariaDB database named web, you can view the tables
in this database by running SHOW TABLES:
SHOW TABLES FROM example.web;
You can see a list of the columns in the clicks table in the web
database using either of the following:
DESCRIBE example.web.clicks;
SHOW COLUMNS FROM example.web.clicks;
Finally, you can access the clicks table in the web database:
SELECT * FROM example.web.clicks;
If you used a different name for your catalog properties file, use
that catalog name instead of example in the above examples.
% mariadb-type-mapping:
Because Trino and MariaDB 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.
The connector maps MariaDB types to the corresponding Trino types according to the following table:
:::{list-table} MariaDB type to Trino type mapping :widths: 30, 30, 50 :header-rows: 1
BOOLEANTINYINTBOOL and BOOLEAN are aliases of TINYINT(1)TINYINTTINYINTTINYINT UNSIGNEDSMALLINTSMALLINTSMALLINTSMALLINT UNSIGNEDINTEGERINTINTEGERINT UNSIGNEDBIGINTBIGINTBIGINTBIGINT UNSIGNEDDECIMAL(20, 0)FLOATREALDOUBLEDOUBLEDECIMAL(p, s)DECIMAL(p, s) or NUMBERDECIMAL when p ≤ 38. Otherwise, maps to NUMBER.CHAR(n)CHAR(n)TINYTEXTVARCHAR(255)TEXTVARCHAR(65535)MEDIUMTEXTVARCHAR(16777215)LONGTEXTVARCHARVARCHAR(n)VARCHAR(n)TINYBLOBVARBINARYBLOBVARBINARYMEDIUMBLOBVARBINARYLONGBLOBVARBINARYVARBINARY(n)VARBINARYDATEDATETIME(n)TIME(n)TIMESTAMP(n)TIMESTAMP(n)NULL as the default value.DATETIME(n)TIMESTAMP(n):::
No other types are supported.
The connector maps Trino types to the corresponding MariaDB types according to the following table:
:::{list-table} Trino type mapping to MariaDB type mapping :widths: 30, 25, 50 :header-rows: 1
BOOLEANBOOLEANTINYINTTINYINTSMALLINTSMALLINTINTEGERINTBIGINTBIGINTREALFLOATDOUBLEDOUBLEDECIMAL(p,s)DECIMAL(p,s)CHAR(n)CHAR(n)VARCHAR(255)TINYTEXTVARCHAR of length 255 or less.VARCHAR(65535)TEXTVARCHAR of length between 256 and 65535, inclusive.VARCHAR(16777215)MEDIUMTEXTVARCHAR of length between 65536 and 16777215, inclusive.VARCHARLONGTEXTVARCHAR of length greater than 16777215 and unbounded VARCHAR map
to LONGTEXT.VARBINARYMEDIUMBLOBDATEDATETIME(n)TIME(n)TIMESTAMP(n)TIMESTAMP(n)explicit_defaults_for_timestamp <https://mariadb.com/docs/reference/mdb/system-variables/explicit_defaults_for_timestamp/>_
to avoid implicit default values and use NULL as the default value.:::
No other types are supported.
Complete list of MariaDB data types.
(mariadb-sql-support)=
The connector provides read access and write access to data and metadata in a MariaDB database. In addition to the globally available and read operation statements, the connector supports the following features:
(mariadb-insert)=
(mariadb-update)=
(mariadb-delete)=
(mariadb-procedures)=
(mariadb-table-functions)=
The connector provides specific {doc}table functions </functions/table> to
access MariaDB.
(mariadb-query-function)=
query(varchar) -> tableThe query function allows you to query the underlying database directly. It
requires syntax native to MariaDB, because the full query is pushed down and
processed in MariaDB. 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 an example, query the example catalog and select the age of employees by
using TIMESTAMPDIFF and CURDATE:
SELECT
age
FROM
TABLE(
example.system.query(
query => 'SELECT
TIMESTAMPDIFF(
YEAR,
date_of_birth,
CURDATE()
) AS age
FROM
tiny.employees'
)
);
The connector includes a number of performance improvements, detailed in the following sections.
(mariadb-table-statistics)=
The MariaDB connector can use table and column statistics for cost based optimizations to improve query processing performance based on the actual data in the data source.
The statistics are collected by MariaDB and retrieved by the connector.
To collect statistics for a table, execute the following statement in MariaDB.
ANALYZE TABLE table_name;
Refer to MariaDB documentation for additional information.
(mariadb-pushdown)=
The connector supports pushdown for a number of operations:
join-pushdownlimit-pushdowntopn-pushdown{ref}Aggregate pushdown <aggregation-pushdown> for the following functions:
avgcountmaxminsumstddevstddev_popstddev_sampvariancevar_popvar_samp