-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/bq improvements #507
Merged
+103
−41
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
28b95fe
request gdrive scope
drewbanin 501da00
re-add missing lines?
drewbanin ebbeeaf
whitespace
drewbanin 4ecb920
remove junk
drewbanin 0649399
bq tables working
drewbanin 7a987b6
optionally override query timeout
drewbanin c740728
bump requirements.txt
drewbanin 5e72e93
pep8
drewbanin be42117
don't mutate global state
drewbanin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,16 +16,23 @@ | |
import google.cloud.exceptions | ||
import google.cloud.bigquery | ||
|
||
import time | ||
import uuid | ||
|
||
|
||
class BigQueryAdapter(PostgresAdapter): | ||
|
||
context_functions = [ | ||
"query_for_existing", | ||
"drop_view", | ||
"execute_model", | ||
"drop", | ||
] | ||
|
||
QUERY_TIMEOUT = 60 * 1000 | ||
SCOPE = ('https://www.googleapis.com/auth/bigquery', | ||
'https://www.googleapis.com/auth/cloud-platform', | ||
'https://www.googleapis.com/auth/drive') | ||
|
||
QUERY_TIMEOUT = 300 | ||
|
||
@classmethod | ||
def handle_error(cls, error, message, sql): | ||
|
@@ -81,12 +88,12 @@ def get_bigquery_credentials(cls, config): | |
creds = google.oauth2.service_account.Credentials | ||
|
||
if method == 'oauth': | ||
credentials, project_id = google.auth.default() | ||
credentials, project_id = google.auth.default(scopes=cls.SCOPE) | ||
return credentials | ||
|
||
elif method == 'service-account': | ||
keyfile = config.get('keyfile') | ||
return creds.from_service_account_file(keyfile) | ||
return creds.from_service_account_file(keyfile, scopes=cls.SCOPE) | ||
|
||
elif method == 'service-account-json': | ||
details = config.get('keyfile_json') | ||
|
@@ -111,6 +118,9 @@ def open_connection(cls, connection): | |
result = connection.copy() | ||
credentials = connection.get('credentials', {}) | ||
|
||
if 'timeout_seconds' in credentials: | ||
cls.QUERY_TIMEOUT = credentials['timeout_seconds'] | ||
|
||
try: | ||
handle = cls.get_bigquery_client(credentials) | ||
|
||
|
@@ -141,7 +151,8 @@ def query_for_existing(cls, profile, schema, model_name=None): | |
|
||
relation_type_lookup = { | ||
'TABLE': 'table', | ||
'VIEW': 'view' | ||
'VIEW': 'view', | ||
'EXTERNAL': 'external' | ||
} | ||
|
||
existing = [(table.name, relation_type_lookup.get(table.table_type)) | ||
|
@@ -150,56 +161,92 @@ def query_for_existing(cls, profile, schema, model_name=None): | |
return dict(existing) | ||
|
||
@classmethod | ||
def drop_view(cls, profile, view_name, model_name): | ||
def drop(cls, profile, relation, relation_type, model_name=None): | ||
schema = cls.get_default_schema(profile) | ||
dataset = cls.get_dataset(profile, schema, model_name) | ||
view = dataset.table(view_name) | ||
view.delete() | ||
relation_object = dataset.table(relation) | ||
relation_object.delete() | ||
|
||
@classmethod | ||
def rename(cls, profile, from_name, to_name, model_name=None): | ||
raise dbt.exceptions.NotImplementedException( | ||
'`rename` is not implemented for this adapter!') | ||
|
||
# Hack because of current API limitations. We should set a flag on the | ||
# Table object indicating StandardSQL when it's implemented | ||
# https://github.com/GoogleCloudPlatform/google-cloud-python/issues/3388 | ||
@classmethod | ||
def format_sql_for_bigquery(cls, sql): | ||
return "#standardSQL\n{}".format(sql) | ||
def materialize_as_view(cls, profile, dataset, model_name, model_sql): | ||
view = dataset.table(model_name) | ||
view.view_query = model_sql | ||
view.view_use_legacy_sql = False | ||
|
||
@classmethod | ||
def execute_model(cls, profile, model, model_name=None): | ||
connection = cls.get_connection(profile, model.get('name')) | ||
logger.debug("Model SQL ({}):\n{}".format(model_name, model_sql)) | ||
|
||
if flags.STRICT_MODE: | ||
validate_connection(connection) | ||
with cls.exception_handler(profile, model_sql, model_name, model_name): | ||
view.create() | ||
|
||
model_name = model.get('name') | ||
model_sql = cls.format_sql_for_bigquery(model.get('injected_sql')) | ||
if view.created is None: | ||
msg = "Error creating view {}".format(model_name) | ||
raise dbt.exceptions.RuntimeException(msg) | ||
|
||
materialization = dbt.utils.get_materialization(model) | ||
allowed_materializations = ['view', 'ephemeral'] | ||
return "CREATE VIEW" | ||
|
||
if materialization not in allowed_materializations: | ||
msg = "Unsupported materialization: {}".format(materialization) | ||
raise dbt.exceptions.RuntimeException(msg) | ||
@classmethod | ||
def poll_until_job_completes(cls, job): | ||
retry_count = cls.QUERY_TIMEOUT | ||
while retry_count > 0 and job.state != 'DONE': | ||
retry_count -= 1 | ||
time.sleep(1) | ||
job.reload() | ||
|
||
schema = cls.get_default_schema(profile) | ||
dataset = cls.get_dataset(profile, schema, model_name) | ||
if job.state != 'DONE': | ||
raise dbt.exceptions.RuntimeException("BigQuery Timeout Exceeded") | ||
|
||
view = dataset.table(model_name) | ||
view.view_query = model_sql | ||
elif job.error_result: | ||
raise job.exception() | ||
|
||
@classmethod | ||
def materialize_as_table(cls, profile, dataset, model_name, model_sql): | ||
conn = cls.get_connection(profile, model_name) | ||
client = conn.get('handle') | ||
|
||
table = dataset.table(model_name) | ||
job_id = 'dbt-create-{}-{}'.format(model_name, uuid.uuid4()) | ||
job = client.run_async_query(job_id, model_sql) | ||
job.use_legacy_sql = False | ||
job.destination = table | ||
job.write_disposition = 'WRITE_TRUNCATE' | ||
job.begin() | ||
|
||
logger.debug("Model SQL ({}):\n{}".format(model_name, model_sql)) | ||
|
||
with cls.exception_handler(profile, model_sql, model_name, model_name): | ||
view.create() | ||
cls.poll_until_job_completes(job) | ||
|
||
if view.created is None: | ||
raise RuntimeError("Error creating view {}".format(model_name)) | ||
return "CREATE TABLE" | ||
|
||
return "CREATE VIEW" | ||
@classmethod | ||
def execute_model(cls, profile, model, materialization, model_name=None): | ||
connection = cls.get_connection(profile, model.get('name')) | ||
|
||
if flags.STRICT_MODE: | ||
validate_connection(connection) | ||
|
||
model_name = model.get('name') | ||
model_sql = model.get('injected_sql') | ||
|
||
schema = cls.get_default_schema(profile) | ||
dataset = cls.get_dataset(profile, schema, model_name) | ||
|
||
if materialization == 'view': | ||
res = cls.materialize_as_view(profile, dataset, model_name, | ||
model_sql) | ||
elif materialization == 'table': | ||
res = cls.materialize_as_table(profile, dataset, model_name, | ||
model_sql) | ||
else: | ||
msg = "Invalid relation type: '{}'".format(materialization) | ||
raise dbt.exceptions.RuntimeException(msg, model) | ||
|
||
return res | ||
|
||
@classmethod | ||
def fetch_query_results(cls, query): | ||
|
@@ -220,12 +267,12 @@ def execute_and_fetch(cls, profile, sql, model_name=None, **kwargs): | |
conn = cls.get_connection(profile, model_name) | ||
client = conn.get('handle') | ||
|
||
formatted_sql = cls.format_sql_for_bigquery(sql) | ||
query = client.run_sync_query(formatted_sql) | ||
query.timeout_ms = cls.QUERY_TIMEOUT | ||
query = client.run_sync_query(sql) | ||
query.timeout_ms = cls.QUERY_TIMEOUT * 1000 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. query timeout override is not respected here |
||
query.use_legacy_sql = False | ||
|
||
debug_message = "Fetching data for query {}:\n{}" | ||
logger.debug(debug_message.format(model_name, formatted_sql)) | ||
logger.debug(debug_message.format(model_name, sql)) | ||
|
||
query.run() | ||
|
||
|
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't mutate global state. a better way:
poll_until_job_completes
take a timeout as an argumentpoll_until_job_completes(job, timeout=connection.get('credentials', {}).get('timeout_seconds'), cls.DEFAULT_QUERY_TIMEOUT)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(and remove this logic altogether)