VVildVVolf opened a new pull request, #43391: URL: https://github.com/apache/airflow/pull/43391
This PR adds an operator for exporting from Google BigQuery's SQL result into Google Cloud Storage. The original [BigQueryToGCSOperator](https://github.com/apache/airflow/blob/main/providers/src/airflow/providers/google/cloud/transfers/bigquery_to_gcs.py) allows only full table movement, since is based on [insert job](https://github.com/apache/airflow/blob/f80233054c204eace74d54a90e5ae517a8548922/providers/src/airflow/providers/google/cloud/hooks/bigquery.py#L1644C9-L1644C19), which has no options to pass query. To simplify the interface of the task (switching cases of full/sql exporting modes), the SQL-based case of BQ->GCS job is implemented as a separate one. The exporting format might be different for BQ for different cases, so the `field_to_bigquery` and `convert_type` methods are implemented as easy overridable. The default output format is made to manual saving of BQ's result as local file. For example, for the query: ```sql SELECT True as _BOOL, CODE_POINTS_TO_BYTES([65, 98, 67, 100]) as _BYTES, DATE('0001-01-01') as _DATE, DATETIME('0001-01-01 00:00:00') as _DATETIME, JSON '{"id": 10,"type": "fruit","name": "apple","on_menu": true,"recipes":{"salads":[{ "id": 2001, "type": "Walnut Apple Salad" },{ "id": 2002, "type": "Apple Spinach Salad" }],"desserts":[{ "id": 3001, "type": "Apple Pie" },{ "id": 3002, "type": "Apple Scones" },{ "id": 3003, "type": "Apple Crumble" }]}}' as _JSON, -9223372036854775808 as _INT64, cast(-9.9999999999999999999999999999999999999E+28 as NUMERIC) as _NUMERIC, cast(-5.7896044618658097711785492504343953926634992332820282019728792003956564819968E+38 as BIGNUMERIC) as _BIGNUMERIC, 1.0 as _FLOAT64, "str" as _STRING, TIME '12:30:00.45' as _TIME, TIMESTAMP '2014-09-27 12:30:00.45-08' as _TIMESTAMP ``` The `BigQueryToGCSOperator` output (exported as a table): ``` _BOOL,_BYTES,_DATE,_DATETIME,_JSON,_INT64,_NUMERIC,_BIGNUMERIC,_FLOAT64,_STRING,_TIME,_TIMESTAMP true,QWJDZA==,0001-01-01,0001-01-01 00:00:00,"{""id"":10,""name"":""apple"",""on_menu"":true,""recipes"":{""desserts"":[{""id"":3001,""type"":""Apple Pie""},{""id"":3002,""type"":""Apple Scones""},{""id"":3003,""type"":""Apple Crumble""}],""salads"":[{""id"":2001,""type"":""Walnut Apple Salad""},{""id"":2002,""type"":""Apple Spinach Salad""}]},""type"":""fruit""}",-9223372036854775808,-99999999999999999999999999999.999999999,-578960446186580977117854925043439539266.34992332820282019728792003956564819968,1,str,12:30:00.450,2014-09-27 20:30:00.45 UTC ``` The cloud console's saving to file result (exported as query result): ``` _BOOL,_BYTES,_DATE,_DATETIME,_JSON,_INT64,_NUMERIC,_BIGNUMERIC,_FLOAT64,_STRING,_TIME,_TIMESTAMP true,QWJDZA==,0001-01-01,0001-01-01T00:00:00,"{""id"":10,""name"":""apple"",""on_menu"":true,""recipes"":{""desserts"":[{""id"":3001,""type"":""Apple Pie""},{""id"":3002,""type"":""Apple Scones""},{""id"":3003,""type"":""Apple Crumble""}],""salads"":[{""id"":2001,""type"":""Walnut Apple Salad""},{""id"":2002,""type"":""Apple Spinach Salad""}]},""type"":""fruit""}",-9223372036854775808,-99999999999999999999999999999.999999999,-578960446186580977117854925043439539266.34992332820282019728792003956564819968,1.0,str,12:30:00.450000,2014-09-27 20:30:00.450000 UTC ``` The result of new `BigQuerySqlToGCSOperator` (with default `field_to_bigquery` and `convert_type` methods) is: ``` _BOOL,_BYTES,_DATE,_DATETIME,_JSON,_INT64,_NUMERIC,_BIGNUMERIC,_FLOAT64,_STRING,_TIME,_TIMESTAMP true,QWJDZA==,0001-01-01,0001-01-01T00:00:00,"{""id"":10,""name"":""apple"",""on_menu"":true,""recipes"":{""desserts"":[{""id"":3001,""type"":""Apple Pie""},{""id"":3002,""type"":""Apple Scones""},{""id"":3003,""type"":""Apple Crumble""}],""salads"":[{""id"":2001,""type"":""Walnut Apple Salad""},{""id"":2002,""type"":""Apple Spinach Salad""}]},""type"":""fruit""}",-9223372036854775808,-99999999999999999999999999999.999999999,-578960446186580977117854925043439539266.34992332820282019728792003956564819968,1.0,str,12:30:00.450000,2014-09-27 20:30:00.450000 UTC ``` Since usage of `schema` is property simplifies formatting of the output data, but might be defined only at the moment of query execution - this property behaves overridable also, to let user decide if it is ok or not to change the property's value on a fly. <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- Thank you for contributing! Please make sure that your code changes are covered with tests. And in case of new features or big changes remember to adjust the documentation. Feel free to ping committers for the review! In case of an existing issue, reference it using one of the following: closes: #ISSUE related: #ISSUE How to write a good git commit message: http://chris.beams.io/posts/git-commit/ --> <!-- Please keep an empty line above the dashes. --> --- **^ Add meaningful description above** Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information. In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed. In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x). In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [newsfragments](https://github.com/apache/airflow/tree/main/newsfragments). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
