-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from gabfl/casting
Implement casting
- Loading branch information
Showing
6 changed files
with
207 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Casting (convert column type to another type) | ||
|
||
bigquery_fdw has an option to implement BigQuery's casting feature: | ||
|
||
> Cast syntax is used in a query to indicate that the result type of an expression should be converted to some other type. | ||
## Foreign table creation syntax | ||
|
||
```sql | ||
CREATE FOREIGN TABLE table_name ( | ||
[...] | ||
) SERVER bigquery_srv | ||
OPTIONS ( | ||
[...] | ||
fdw_casting '{"column1": "CAST_TO_TYPE", "column2": "CAST_TO_TYPE", ...}' | ||
); | ||
``` | ||
|
||
## Usage example | ||
|
||
### Without casting option | ||
|
||
```sql | ||
test=# DROP FOREIGN TABLE tmp; | ||
|
||
test=# CREATE FOREIGN TABLE tmp ( | ||
column1 bigint, | ||
timestamp timestamp | ||
) SERVER bigquery_srv | ||
OPTIONS ( | ||
fdw_dataset 'my_dataset', | ||
fdw_table 'my_table', | ||
fdw_key '/opt/bigquery_fdw/user.json' | ||
); | ||
|
||
test=# SELECT timestamp FROM tmp LIMIT 5; | ||
timestamp | ||
--------------------- | ||
2017-09-14 09:12:32 | ||
2017-09-14 09:12:32 | ||
2017-09-14 09:12:32 | ||
2017-09-12 00:36:16 | ||
2017-09-12 17:23:12 | ||
(5 rows) | ||
``` | ||
|
||
### With casting option | ||
|
||
```sql | ||
test=# DROP FOREIGN TABLE tmp; | ||
|
||
test=# CREATE FOREIGN TABLE tmp ( | ||
column1 bigint, | ||
timestamp timestamp | ||
) SERVER bigquery_srv | ||
OPTIONS ( | ||
fdw_dataset 'my_dataset', | ||
fdw_table 'my_table', | ||
fdw_key '/opt/bigquery_fdw/user.json', | ||
fdw_casting '{"timestamp": "DATE"}' | ||
); | ||
|
||
test=# SELECT timestamp FROM tmp LIMIT 5; | ||
timestamp | ||
--------------------- | ||
2017-09-11 00:00:00 | ||
2017-09-11 00:00:00 | ||
2017-09-11 00:00:00 | ||
2017-09-11 00:00:00 | ||
2017-09-11 00:00:00 | ||
(5 rows) | ||
``` | ||
|
||
|
||
## External links | ||
|
||
- [BigQuery casting](https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#casting) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
## Table partitioning | ||
|
||
BigQuery **table partitioning is supported**. When partitioning a table, BigQuery creates a pseudo column called `_PARTITIONTIME`. | ||
|
||
To use partitions, you need to add a column `partition_date` with the type `date` in the foreign table definition, for example: | ||
|
||
```sql | ||
CREATE FOREIGN TABLE my_bigquery_table ( | ||
column1 text, | ||
column2 bigint, | ||
partition_date date -- <-- partition! | ||
) SERVER bigquery_srv | ||
OPTIONS ( | ||
fdw_dataset 'my_dataset', | ||
fdw_table 'my_table', | ||
fdw_key '/opt/bigquery_fdw/user.json' | ||
); | ||
``` | ||
|
||
You can then use the partition in the `WHERE` clause: | ||
|
||
```sql | ||
SELECT column1, column2 | ||
FROM my_bigquery_table | ||
WHERE column1 = 'abc' AND partition_date = '2017-12-01' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters