native_query is a JDBC catalog table function. It executes a database-native SELECT statement through a JDBC catalog and exposes the result as a database relation.
Use this function when the source database must run SQL that is hard to express against a single JDBC external table, such as a pre-filtered subquery, a join in the source database, or vendor-specific SQL syntax. After native_query returns a relation, you can use the database to apply additional filters, joins, aggregations, projections, or load the result with INSERT INTO.
This function is supported from v4.1 onwards.
Syntax
Parameters
jdbc_catalog
The name of a JDBC catalog. Only JDBC catalogs support this function.
select_sql
A string literal that contains the pass-through SQL statement executed by the source database.
After the database removes leading comments and trailing semicolons from select_sql, the statement must start with SELECT. Use the SQL dialect, object names, quoting rules, and functions of the source database.
If select_sql contains a single quotation mark ('), escape it as two single quotation marks ('') in the database SQL string.
alias
An optional table alias for the result relation.
Column aliases after the table alias, such as AS q(c1, c2), are not supported. Define column aliases inside select_sql, for example SELECT id AS id_alias FROM ....
Return value
native_query returns a relation whose columns are inferred from the JDBC result set metadata of select_sql. The database maps JDBC column types to the database column types by using the schema resolver of the JDBC catalog.
Usage notes
- The user needs the
USAGEprivilege on the JDBC catalog. Permissions on source database objects are checked by the remote database with the user configured in the JDBC catalog. select_sqlmust be a string literal and must be the only argument. Named arguments are not supported.select_sqlmust start withSELECTafter leading comments are removed. Statements that start withWITH,INSERT,UPDATE,DELETE, or other non-SELECTkeywords are not supported.- The legacy
<catalog>.system.query(...)form is not supported. - If the source query returns no columns, the database returns an error during analysis.
Examples
The following examples assume that the source MySQL database contains a table namedapp.orders and that a JDBC catalog named jdbc0 has been created in the database.
Example 1: Run a source-side subquery and apply an outer the database filter.