Skip to content

Commit

Permalink
Merge branch 'main' into non_incremental_mat_view
Browse files Browse the repository at this point in the history
  • Loading branch information
mikealfare authored Feb 13, 2024
2 parents 02c9f7c + ea3abee commit d5ef7aa
Show file tree
Hide file tree
Showing 35 changed files with 331 additions and 279 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Dependencies-20240124-120321.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Dependencies
body: get dbt-tests-adapters from dbt-adapters repo
time: 2024-01-24T12:03:21.523295-08:00
custom:
Author: colin-rogers-dbt
PR: "1077"
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20240102-152030.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Update base adapter references as part of decoupling migration
time: 2024-01-02T15:20:30.038221-08:00
custom:
Author: colin-rogers-dbt
Issue: "1067"
7 changes: 7 additions & 0 deletions .changes/unreleased/Features-20240205-174614.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Features
body: Support all types for unit testing in dbt-bigquery, expand coverage of
safe_cast macro
time: 2024-02-05T17:46:14.505597-05:00
custom:
Author: michelleark
Issue: "1090"
6 changes: 6 additions & 0 deletions .changes/unreleased/Under the Hood-20231116-062142.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Under the Hood
body: Primary and foreign key constraints are not enforced in BigQuery
time: 2023-11-16T06:21:42.935367-08:00
custom:
Author: dbeatty10
Issue: "1018"
6 changes: 6 additions & 0 deletions .changes/unreleased/Under the Hood-20240116-154305.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Under the Hood
body: Migrate to dbt-common and dbt-adapters package
time: 2024-01-16T15:43:05.046735-08:00
custom:
Author: colin-rogers-dbt
Issue: "1071"
28 changes: 0 additions & 28 deletions .github/workflows/jira-creation.yml

This file was deleted.

28 changes: 0 additions & 28 deletions .github/workflows/jira-label.yml

This file was deleted.

29 changes: 0 additions & 29 deletions .github/workflows/jira-transition.yml

This file was deleted.

37 changes: 18 additions & 19 deletions dbt/adapters/bigquery/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from contextlib import contextmanager
from dataclasses import dataclass, field

from dbt.events.contextvars import get_node_info
from dbt_common.invocation import get_invocation_id

from dbt_common.events.contextvars import get_node_info
from mashumaro.helper import pass_through

from functools import lru_cache
Expand All @@ -25,23 +27,21 @@
)

from dbt.adapters.bigquery import gcloud
from dbt.clients import agate_helper
from dbt.config.profile import INVALID_PROFILE_MESSAGE
from dbt.tracking import active_user
from dbt.contracts.connection import ConnectionState, AdapterResponse
from dbt.exceptions import (
FailedToConnectError,
from dbt_common.clients import agate_helper
from dbt.adapters.contracts.connection import ConnectionState, AdapterResponse, Credentials
from dbt_common.exceptions import (
DbtRuntimeError,
DbtDatabaseError,
DbtProfileError,
DbtConfigError,
)
from dbt.adapters.base import BaseConnectionManager, Credentials
from dbt.events import AdapterLogger
from dbt.events.functions import fire_event
from dbt.events.types import SQLQuery
from dbt.version import __version__ as dbt_version
from dbt_common.exceptions import DbtDatabaseError
from dbt.adapters.exceptions.connection import FailedToConnectError
from dbt.adapters.base import BaseConnectionManager
from dbt.adapters.events.logging import AdapterLogger
from dbt.adapters.events.types import SQLQuery
from dbt_common.events.functions import fire_event
from dbt.adapters.bigquery import __version__ as dbt_version

from dbt.dataclass_schema import ExtensibleDbtClassMixin, StrEnum
from dbt_common.dataclass_schema import ExtensibleDbtClassMixin, StrEnum

logger = AdapterLogger("BigQuery")

Expand Down Expand Up @@ -85,7 +85,7 @@ def get_bigquery_defaults(scopes=None) -> Tuple[Any, Optional[str]]:
credentials, _ = google.auth.default(scopes=scopes)
return credentials, _
except google.auth.exceptions.DefaultCredentialsError as e:
raise DbtProfileError(INVALID_PROFILE_MESSAGE.format(error_string=e))
raise DbtConfigError(f"Failed to authenticate with supplied credentials\nerror:\n{e}")


class Priority(StrEnum):
Expand Down Expand Up @@ -382,7 +382,7 @@ def get_bigquery_client(cls, profile_credentials):
execution_project = profile_credentials.execution_project
location = getattr(profile_credentials, "location", None)

info = client_info.ClientInfo(user_agent=f"dbt-{dbt_version}")
info = client_info.ClientInfo(user_agent=f"dbt-bigquery-{dbt_version.version}")
return google.cloud.bigquery.Client(
execution_project,
creds,
Expand Down Expand Up @@ -470,8 +470,7 @@ def raw_execute(

labels = self.get_labels_from_query_comment()

if active_user:
labels["dbt_invocation_id"] = active_user.invocation_id
labels["dbt_invocation_id"] = get_invocation_id()

job_params = {
"use_legacy_sql": use_legacy_sql,
Expand Down
2 changes: 1 addition & 1 deletion dbt/adapters/bigquery/dataset.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List
from google.cloud.bigquery import Dataset, AccessEntry

from dbt.events import AdapterLogger
from dbt.adapters.events.logging import AdapterLogger

logger = AdapterLogger("BigQuery")

Expand Down
9 changes: 5 additions & 4 deletions dbt/adapters/bigquery/gcloud.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from dbt.events import AdapterLogger
import dbt.exceptions
from dbt.clients.system import run_cmd
from dbt_common.exceptions import DbtRuntimeError

from dbt.adapters.events.logging import AdapterLogger
from dbt_common.clients.system import run_cmd

NOT_INSTALLED_MSG = """
dbt requires the gcloud SDK to be installed to authenticate with BigQuery.
Expand All @@ -25,4 +26,4 @@ def setup_default_credentials():
if gcloud_installed():
run_cmd(".", ["gcloud", "auth", "application-default", "login"])
else:
raise dbt.exceptions.DbtRuntimeError(NOT_INSTALLED_MSG)
raise DbtRuntimeError(NOT_INSTALLED_MSG)
67 changes: 36 additions & 31 deletions dbt/adapters/bigquery/impl.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from dataclasses import dataclass
import json
import threading
from multiprocessing.context import SpawnContext

import time
from typing import Any, Dict, List, Optional, Type, Set, Union
from typing import Any, Dict, List, Optional, Type, Set, Union, FrozenSet, Tuple, Iterable

import agate
from dbt import ui # type: ignore
from dbt.adapters.contracts.relation import RelationConfig

import dbt_common.exceptions.base
from dbt.adapters.base import ( # type: ignore
AdapterConfig,
BaseAdapter,
Expand All @@ -17,17 +21,15 @@
available,
)
from dbt.adapters.cache import _make_ref_key_dict # type: ignore
import dbt.clients.agate_helper
from dbt.contracts.connection import AdapterResponse
from dbt.contracts.graph.manifest import Manifest
from dbt.contracts.graph.nodes import ColumnLevelConstraint, ConstraintType, ModelLevelConstraint # type: ignore
from dbt.dataclass_schema import dbtClassMixin
import dbt.deprecations
from dbt.events import AdapterLogger
from dbt.events.functions import fire_event
from dbt.events.types import SchemaCreation, SchemaDrop
import dbt.exceptions
from dbt.utils import filter_null_values
import dbt_common.clients.agate_helper
from dbt.adapters.contracts.connection import AdapterResponse
from dbt_common.contracts.constraints import ColumnLevelConstraint, ConstraintType, ModelLevelConstraint # type: ignore
from dbt_common.dataclass_schema import dbtClassMixin
from dbt.adapters.events.logging import AdapterLogger
from dbt_common.events.functions import fire_event
from dbt.adapters.events.types import SchemaCreation, SchemaDrop
import dbt_common.exceptions
from dbt_common.utils import filter_null_values
import google.api_core
import google.auth
import google.oauth2
Expand Down Expand Up @@ -113,12 +115,12 @@ class BigQueryAdapter(BaseAdapter):
ConstraintType.check: ConstraintSupport.NOT_SUPPORTED,
ConstraintType.not_null: ConstraintSupport.ENFORCED,
ConstraintType.unique: ConstraintSupport.NOT_SUPPORTED,
ConstraintType.primary_key: ConstraintSupport.ENFORCED,
ConstraintType.foreign_key: ConstraintSupport.ENFORCED,
ConstraintType.primary_key: ConstraintSupport.NOT_ENFORCED,
ConstraintType.foreign_key: ConstraintSupport.NOT_ENFORCED,
}

def __init__(self, config) -> None:
super().__init__(config)
def __init__(self, config, mp_context: SpawnContext) -> None:
super().__init__(config, mp_context)
self.connections: BigQueryConnectionManager = self.connections

###
Expand Down Expand Up @@ -146,7 +148,9 @@ def drop_relation(self, relation: BigQueryRelation) -> None:
conn.handle.delete_table(table_ref, not_found_ok=True)

def truncate_relation(self, relation: BigQueryRelation) -> None:
raise dbt.exceptions.NotImplementedError("`truncate` is not implemented for this adapter!")
raise dbt_common.exceptions.base.NotImplementedError(
"`truncate` is not implemented for this adapter!"
)

def rename_relation(
self, from_relation: BigQueryRelation, to_relation: BigQueryRelation
Expand All @@ -161,7 +165,7 @@ def rename_relation(
or from_relation.type == RelationType.View
or to_relation.type == RelationType.View
):
raise dbt.exceptions.DbtRuntimeError(
raise dbt_common.exceptions.DbtRuntimeError(
"Renaming of views is not currently supported in BigQuery"
)

Expand Down Expand Up @@ -387,7 +391,7 @@ def copy_table(self, source, destination, materialization):
elif materialization == "table":
write_disposition = WRITE_TRUNCATE
else:
raise dbt.exceptions.CompilationError(
raise dbt_common.exceptions.CompilationError(
'Copy table materialization must be "copy" or "table", but '
f"config.get('copy_materialization', 'table') was "
f"{materialization}"
Expand Down Expand Up @@ -434,11 +438,11 @@ def poll_until_job_completes(cls, job, timeout):
job.reload()

if job.state != "DONE":
raise dbt.exceptions.DbtRuntimeError("BigQuery Timeout Exceeded")
raise dbt_common.exceptions.DbtRuntimeError("BigQuery Timeout Exceeded")

elif job.error_result:
message = "\n".join(error["message"].strip() for error in job.errors)
raise dbt.exceptions.DbtRuntimeError(message)
raise dbt_common.exceptions.DbtRuntimeError(message)

def _bq_table_to_relation(self, bq_table) -> Union[BigQueryRelation, None]:
if bq_table is None:
Expand All @@ -455,15 +459,14 @@ def _bq_table_to_relation(self, bq_table) -> Union[BigQueryRelation, None]:
@classmethod
def warning_on_hooks(cls, hook_type):
msg = "{} is not supported in bigquery and will be ignored"
warn_msg = dbt.ui.color(msg, ui.COLOR_FG_YELLOW)
logger.info(warn_msg)
logger.info(msg)

@available
def add_query(self, sql, auto_begin=True, bindings=None, abridge_sql_log=False):
if self.nice_connection_name() in ["on-run-start", "on-run-end"]:
self.warning_on_hooks(self.nice_connection_name())
else:
raise dbt.exceptions.NotImplementedError(
raise dbt_common.exceptions.base.NotImplementedError(
"`add_query` is not implemented for this adapter!"
)

Expand Down Expand Up @@ -680,14 +683,16 @@ def upload_file(
self.poll_until_job_completes(job, timeout)

@classmethod
def _catalog_filter_table(cls, table: agate.Table, manifest: Manifest) -> agate.Table:
def _catalog_filter_table(
cls, table: agate.Table, used_schemas: FrozenSet[Tuple[str, str]]
) -> agate.Table:
table = table.rename(
column_names={col.name: col.name.replace("__", ":") for col in table.columns}
)
return super()._catalog_filter_table(table, manifest)
return super()._catalog_filter_table(table, used_schemas)

def _get_catalog_schemas(self, manifest: Manifest) -> SchemaSearchMap:
candidates = super()._get_catalog_schemas(manifest)
def _get_catalog_schemas(self, relation_config: Iterable[RelationConfig]) -> SchemaSearchMap:
candidates = super()._get_catalog_schemas(relation_config)
db_schemas: Dict[str, Set[str]] = {}
result = SchemaSearchMap()

Expand Down Expand Up @@ -773,7 +778,7 @@ def describe_relation(
bq_table = self.get_bq_table(relation)
parser = BigQueryMaterializedViewConfig
else:
raise dbt.exceptions.DbtRuntimeError(
raise dbt_common.exceptions.DbtRuntimeError(
f"The method `BigQueryAdapter.describe_relation` is not implemented "
f"for the relation type: {relation.type}"
)
Expand Down Expand Up @@ -839,7 +844,7 @@ def string_add_sql(
elif location == "prepend":
return f"concat('{value}', {add_to})"
else:
raise dbt.exceptions.DbtRuntimeError(
raise dbt_common.exceptions.DbtRuntimeError(
f'Got an unexpected location value of "{location}"'
)

Expand Down
2 changes: 1 addition & 1 deletion dbt/adapters/bigquery/python_submissions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import uuid
from typing import Dict, Union

from dbt.events import AdapterLogger
from dbt.adapters.events.logging import AdapterLogger

from dbt.adapters.base import PythonJobHelper
from google.api_core.future.polling import POLLING_PREDICATE
Expand Down
Loading

0 comments on commit d5ef7aa

Please sign in to comment.