redshift_query(handle, sql[, params := [...]])
redshift_query sends a SQL statement to Redshift through a Redshift handle and returns its result set.
The SQL statement is written in the SQL dialect of Redshift and sent as written.
While the DuckDB statement that calls redshift_query is planned, redshift_query asks Redshift to describe the SQL statement.
This determines the columns of the result set without running the SQL statement.
If the SQL statement returns other columns when it runs, an error is raised and the SQL statement is not run.
For a prepared DuckDB statement, the description is made when it is prepared.
sql can contain parameter markers.
The value for each parameter marker is given in params, as a list of strings.
Parameters
| Parameter | Description | Mandatory |
|---|---|---|
handle |
The alias of an attached Redshift handle, a VARCHAR. |
true |
sql |
The SQL statement to run, a VARCHAR. |
true |
params |
The values for the parameter markers in sql, a VARCHAR[]. |
handle and sql are given by position.
params is given by name, as in params := ['a', 'b'].
The following raise an error before anything is sent to Redshift:
handleorsqlisNULLor the empty string.handleis not the alias of an attached database, or is the alias of a database that is not a Redshift handle.paramsisNULL.- An element of
paramsisNULL. To passNULL, writeNULLinsql. - An element of
paramscontains a NUL byte, or is longer than the Redshift ODBC driver can send. sqlcontains a NUL byte.
Returns
The result set of sql.
A SQL statement that returns more than one result set raises an error.
A result column of a Redshift type that the redshift extension cannot read raises an error before the SQL statement is run.
Such a column can be cast to a supported type, such as VARCHAR, in sql.
Restrictions
A Redshift handle has one connection to Redshift, which runs one SQL statement at a time. This leads to the following restrictions. Each raises an error, and no SQL statement is run on Redshift.
- A DuckDB statement can use a Redshift handle in only one
redshift_querycall. redshift_querycannot be called on a Redshift handle while the result of an earlier call on that handle is still being read.redshift_querycannot be used in the recursive part of a recursive CTE, because it would run once per iteration.redshift_querycannot be the source of aPIVOTwithout anINlist, because DuckDB would run it once per pivoted column.
In each case, the result can be stored in a DuckDB temporary table by a separate DuckDB statement first.
redshift_query cannot be called in an explicit DuckDB transaction, unless redshift_allow_query_in_duckdb_transaction is true.
See Transactions.
Example
SELECT *
FROM redshift_query(
'rs',
'SELECT order_id, amount FROM public.orders WHERE status = ?',
params := ['shipped']
);