Stay organized with collections Save and categorize content based on your preferences.
GoogleSQL for BigQuery supports the following federated query functions.
Function list Name SummaryEXTERNAL_QUERY
Executes a query on an external database and returns the results as a temporary table. EXTERNAL_QUERY
EXTERNAL_QUERY('connection_id', '''external_database_query'''[, 'options'])
Description
Executes a query on an external database and returns the results as a temporary table. The external database data type is converted to a GoogleSQL data type in the temporary result table with these data type mappings.
external_database_query
: The query to run on the external database.connection_id
: The ID of the connection resource. The connection resource contains settings for the connection between the external database and BigQuery. If you don't have a default project configured, prepend the project ID to the connection ID in following format:
projects/PROJECT_ID/locations/LOCATION/connections/CONNECTION_ID
Replace the following:
For example, projects/example-project/locations/us/connections/sql-bq
.
EXTERNAL_QUERY
, always use the fully qualified connection ID (projects/PROJECT_ID/locations/LOCATION/connections/CONNECTION_ID ), otherwise the wrong project might be used. options
: An optional string of a JSON format map with key value pairs of option name and value (both are case sensitive).
For example: '{"default_type_for_decimal_columns":"numeric"}'
Supported options:
Option Name Description "default_type_for_decimal_columns" Can be "float64", "numeric", "bignumeric" or "string". With this option, the MySQL Decimal type or PostgreSQL Numeric type will be mapped to the provided BigQuery type. When this option isn't provided, the MySQL Decimal type or PostgreSQL Numeric type will be mapped to BigQuery NUMERIC type. "query_execution_priority" Can be "low", "medium" or "high". Only supported in Spanner. Specifies priority for execution of the query. Execution priority is "medium" by default.Additional notes:
EXTERNAL_QUERY
function is usually used in a FROM
clause.EXTERNAL_QUERY()
function to access metadata about the external database.EXTERNAL_QUERY()
won't honor the ordering of the external query result, even if your external query includes ORDER BY
.Return Data Type
BigQuery table
Examples
Suppose you need the date of the first order for each of your customers to include in a report. This data is not currently in BigQuery but is available in your operational PostgreSQL database in . The following federated query example accomplishes this and includes 3 parts:
SELECT customer_id, MIN(order_date) AS first_order_date FROM orders GROUP BY customer_id
in the operational PostgreSQL database to get the first order date for each customer through the EXTERNAL_QUERY()
function.customer_id
.SELECT
c.customer_id, c.name, SUM(t.amount) AS total_revenue, rq.first_order_date
FROM customers AS c
INNER JOIN transaction_fact AS t ON c.customer_id = t.customer_id
LEFT OUTER JOIN
EXTERNAL_QUERY(
'connection_id',
'''SELECT customer_id, MIN(order_date) AS first_order_date
FROM orders
GROUP BY customer_id'''
) AS rq
ON rq.customer_id = c.customer_id
GROUP BY c.customer_id, c.name, rq.first_order_date;
You can use the EXTERNAL_QUERY()
function to query information_schema tables to access database metadata, such as list all tables in the database or show table schema. The following example information_schema queries work in both MySQL and PostgreSQL.
-- List all tables in a database.
SELECT *
FROM
EXTERNAL_QUERY(
'connection_id',
'''SELECT * FROM information_schema.tables'''
);
-- List all columns in a table.
SELECT *
FROM
EXTERNAL_QUERY(
'connection_id',
'''SELECT * FROM information_schema.columns WHERE table_name='x';'''
);
EXTERNAL_QUERY()
won't honor the ordering of the external query result, even if your external query includes ORDER BY
. The following example query orders rows by customer ID in the external database, but BigQuery will not output the result rows in that order.
-- ORDER BY will not order rows.
SELECT *
FROM
EXTERNAL_QUERY(
'connection_id',
'''SELECT * FROM customers AS c ORDER BY c.customer_id'''
);
Data type mappings
When you execute a federated query, the data from the external database are converted to GoogleSQL types. Below are the data type mappings from MySQL to BigQuery and PostgreSQL to BigQuery.
Things to know about mapping:
decimal
, timestamp
, and time
.money
, path
, uuid
, boxer
, and others.NUMERIC
value by default. The BigQuery NUMERIC
value range is smaller than in MySQL and PostgreSQL. It can also be mapped to BIGNUMERIC
, FLOAT64
, or STRING
with "default_type_for_decimal_columns" in EXTERNAL_QUERY
options.Error handling
If your external query contains a data type that's unsupported in BigQuery, the query will fail immediately. You can cast the unsupported data type to a different MySQL / PostgreSQL data type that is supported. See unsupported data types for more information on how to cast.
MySQL to BigQuery type mapping MySQL type MySQL Description BigQuery type Type difference Integer INT 4 bytes, 2^32 - 1 INT64 TINYINT 1 byte, 2^8 - 1 INT64 SMALLINT 2 bytes, 2^16 - 1 INT64 MEDIUMINT 3 bytes, 2^24 - 1 INT64 BIGINT 8 bytes, 2^64 - 1 INT64 UNSIGNED BIGINT 8 bytes, 2^64 - 1 NUMERIC Exact numeric DECIMAL (M,D) A decimal represents by (M,D) where M is the total number of digits and D is the number of decimals. M <= 65 NUMERIC, BIGNUMERIC, FLOAT64, or STRING DECIMAL (M,D) will to mapped to NUMERIC by default, or can be mapped to BIGNUMERIC, FLOAT64, or STRING with default_type_for_decimal_columns. Approximate numeric FLOAT (M,D) 4 bytes, M <= 23 FLOAT64 DOUBLE (M,D) 8 bytes, M <= 53 FLOAT64 Date and time TIMESTAMP '1970-01-01 00:00:01'UTC to '2038-01-19 03:14:07' UTC. TIMESTAMP MySQL TIMESTAMP is retrieved as UTC timezone no matter where user call BigQuery DATETIME '1000-01-01 00:00:00' to '9999-12-31 23:59:59' DATETIME DATE '1000-01-01' to '9999-12-31'. DATE TIME Time in 'HH:MM:SS' formatIf your external query contains a data type that's unsupported in BigQuery, the query will fail immediately. You can cast the unsupported data type to a different supported MySQL / PostgreSQL data type.
Invalid table-valued function external_query Found unsupported MySQL type in BigQuery. at [1:15]
GEOMETRY
, BIT
SELECT ST_AsText(ST_GeomFromText('POINT(1 1)'));
This command casts the unsupported data type GEOMETRY
to STRING
.Invalid table-valued function external_query Postgres type (OID = 790) isn't supported now at [1:15]
money, time with time zone, inet, path, pg_lsn, point, polygon, tsquery, tsvector, txid_snapshot, uuid, box, cidr, circle, interval, jsonb, line, lseg, macaddr, macaddr8
SELECT CAST('12.34'::float8::numeric::money AS varchar(30));
This command casts the unsupported data type money
to string
.When you execute a Spanner federated query, the data from Spanner is converted to GoogleSQL types.
Spanner GoogleSQL type Spanner PostgreSQL type BigQuery typeARRAY
- ARRAY
BOOL
bool
BOOL
BYTES
bytea
BYTES
DATE
date
DATE
FLOAT64
float8
FLOAT64
INT64
bigint
INT64
JSON
JSONB
JSON
NUMERIC
numeric
* NUMERIC
STRING
varchar
STRING
STRUCT
- Not supported for Spanner federated queries TIMESTAMP
timestamptz
TIMESTAMP
with nanoseconds truncated
* PostgreSQL numeric values with a precision that's greater than the precision that BigQuery supports are rounded. Values that are larger than the maximum value generate an Invalid NUMERIC value
error.
If your external query contains a data type that's unsupported for federated queries, the query fails immediately. You can cast the unsupported data type to a supported data type.
SAP Datasphere to BigQuery type mappingWhen you execute a SAP Datasphere federated query, the data from SAP Datasphere is converted to the following GoogleSQL types.
SAP Datasphere type SAP Datasphere description BigQuery type Integer Integer Standard signed integer. INT64 Integer64 Signed 64-bit integer. BIGNUMERIC hana.SMALLINT Signed 16-bit integer supporting the values -32,768 to 32,767. INT64 hana.TINYINT Unsigned 8-bit integer supporting the values 0 to 255. INT64 Exact numeric Decimal (p, s) Precision (p) defines the number of total digits and can be between 1 and 38.Scale (s) defines the number of digits after the decimal point and can be between 0 and p.
BIGNUMERIC DecimalFloat Decimal floating-point number with 34 mantissa digits. BIGNUMERIC hana.SMALLDECIMAL 64-bit decimal floating-point number, where (p) can be between 1 and 16 and s can be between -369 and 368. BIGNUMERIC Approximate numeric Double Double-precision, 64-bit floating-point number. FLOAT64 hana.REAL 32-bit binary floating-point number. FLOAT64 Date and time Date Default format YYYY-MM-DD. DATE Datetime Default format YYYY-MM-DD HH24:MI:SS. TIMESTAMP Time Default format HH24:MI:SS. TIME Timestamp Default format YYYY-MM-DD HH24:MI:SS. TIMESTAMP Character and strings LargeString Variable length string of up to 2GB. STRING String (n) Variable-length Unicode string of up to 5000 characters. STRING Binary Binary (n) Variable length byte string of up to 4000 bytes. BYTES LargeBinary Variable length byte string of up to 2GB. BYTES hana.BINARY (n) Byte string of fixed length (n). STRING Other Boolean TRUE, FALSE and UNKNOWN, where UNKNOWN is a synonym of NULL. BOOL UUID Universally unique identifier encoded as a 128-bit integer. STRING hana.ST_GEOMETRY Spatial data in any form, including 0-dimensional points, lines, multi-lines, and polygons. NOT SUPPORTED hana.ST_POINT Spatial data in the form of 0-dimensional points that represents a single location in coordinate space. NOT SUPPORTEDExcept as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-07 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["`EXTERNAL_QUERY` executes a query on an external database, returning the results as a temporary BigQuery table, with external data types mapped to GoogleSQL types."],["The `EXTERNAL_QUERY` function is commonly used in a `FROM` clause and can access external database metadata; however, it does not preserve the external query's `ORDER BY` clause."],["Connection to an external database is done using a `connection_id`, which is the resource ID containing settings for the connection, and can include optional parameters in JSON format for fine-tuning data type mappings."],["Data type mappings are defined between external database types (MySQL, PostgreSQL, Spanner, SAP Datasphere) and GoogleSQL, and in case of unsupported types, data can be casted to compatible types to avoid query failures."],["The default mapping for MySQL and PostgreSQL numeric data types is `NUMERIC`, but can also be set to `BIGNUMERIC`, `FLOAT64`, or `STRING` by using the `default_type_for_decimal_columns` option, while Spanner numerics with too much precision are rounded off, and values that are too large generate errors."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4