-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Fixes #12129 : enhance bigquery sample data query #12130
Merged
TeddyCr
merged 6 commits into
open-metadata:main
from
duongphannamhung:hung.duong/sample_data_upgrade
Jul 4, 2023
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
60b29d6
upgrade BigQuery Sampler
vtlbk 78558fa
beautify code
vtlbk c423d2d
revert old way of profiler & data quality, keep fetch new way sample
vtlbk 50c82c1
Merge branch 'main' into hung.duong/sample_data_upgrade
TeddyCr 96b0bce
Update profiler_source.py
duongphannamhung 88efc1e
Update profiler_source.py
duongphannamhung 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
Empty file.
52 changes: 52 additions & 0 deletions
52
ingestion/src/metadata/profiler/interface/sqlalchemy/bigquery/profiler_interface.py
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,52 @@ | ||
# Copyright 2021 Collate | ||
# Licensed 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. | ||
|
||
""" | ||
Interfaces with database for BigQuery engine | ||
supporting sqlalchemy abstraction layer | ||
""" | ||
|
||
from metadata.profiler.interface.sqlalchemy.profiler_interface import ( | ||
SQAProfilerInterface, | ||
) | ||
from metadata.profiler.processor.sqlalchemy.bigquery_sampler import BigQuerySampler | ||
|
||
|
||
class BigQueryProfilerInterface(SQAProfilerInterface): | ||
""" | ||
Interface to interact with BigQuery registry. | ||
""" | ||
|
||
_profiler_type: str = "BigQuery" | ||
|
||
def __init__( | ||
self, | ||
**kwargs, | ||
): | ||
super().__init__(**kwargs) | ||
|
||
def _instantiate_sampler( | ||
self, | ||
session, | ||
table, | ||
sample_columns, | ||
profile_sample_config, | ||
partition_details, | ||
profile_sample_query, | ||
): | ||
return BigQuerySampler( | ||
session=session, | ||
table=table, | ||
sample_columns=sample_columns, | ||
profile_sample_config=profile_sample_config, | ||
partition_details=partition_details, | ||
profile_sample_query=profile_sample_query, | ||
) |
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
71 changes: 71 additions & 0 deletions
71
ingestion/src/metadata/profiler/processor/sqlalchemy/bigquery_sampler.py
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,71 @@ | ||
# Copyright 2021 Collate | ||
# Licensed 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. | ||
""" | ||
Helper module to handle BigQuery data sampling | ||
for the profiler | ||
""" | ||
|
||
from metadata.generated.schema.entity.data.table import ProfileSampleType, TableData | ||
from metadata.profiler.processor.sqlalchemy.sampler import Sampler | ||
from metadata.profiler.source.bigquery.queries import BIGQUERY_TABLESAMPLE | ||
|
||
|
||
class BigQuerySampler(Sampler): | ||
""" | ||
Generates a sample of the BigQuery to not | ||
run the query in the whole table. | ||
""" | ||
|
||
sample_stmt = BIGQUERY_TABLESAMPLE | ||
default_percent = 10 | ||
|
||
def __init__( | ||
self, | ||
**kwargs, | ||
): | ||
super().__init__(**kwargs) | ||
|
||
def get_bq_sample_query(self) -> str: | ||
"""get query for sample data""" | ||
if self.profile_sample_type == ProfileSampleType.PERCENTAGE: | ||
return self.sample_stmt.format( | ||
table=self.table.__tablename__, | ||
col=", ".join( | ||
"`" + col_name.lower() + "`" for col_name in self.sample_columns | ||
), | ||
relative_table=self.table.__table__, | ||
percent=self.profile_sample, | ||
result_limit=self.sample_limit, | ||
) | ||
|
||
return self.sample_stmt.format( | ||
table=self.table.__tablename__, | ||
col=", ".join( | ||
"`" + col_name.lower() + "`" for col_name in self.sample_columns | ||
), | ||
relative_table=self.table.__table__, | ||
percent=self.default_percent, | ||
result_limit=self.profile_sample, | ||
) | ||
|
||
def fetch_sqa_sample_data(self) -> TableData: | ||
""" | ||
Use the sampler to retrieve sample data rows as per limit given by user | ||
:return: TableData to be added to the Table Entity | ||
""" | ||
if self._profile_sample_query: | ||
return self._fetch_sample_data_from_user_query() | ||
|
||
bq_sample = self.session.execute(self.get_bq_sample_query()) | ||
return TableData( | ||
columns=list(self.sample_columns), | ||
rows=[list(row) for row in bq_sample], | ||
) |
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
26 changes: 26 additions & 0 deletions
26
ingestion/src/metadata/profiler/source/bigquery/queries.py
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 @@ | ||
# Copyright 2021 Collate | ||
# Licensed 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. | ||
""" | ||
BigQuery Queries for fetching sample data | ||
""" | ||
|
||
import textwrap | ||
|
||
BIGQUERY_TABLESAMPLE = textwrap.dedent( | ||
""" | ||
WITH {table}_cte AS ( | ||
SELECT {col} | ||
FROM `{relative_table}` TABLESAMPLE SYSTEM ({percent} PERCENT) | ||
) | ||
SELECT * FROM {table}_cte | ||
LIMIT {result_limit} | ||
""" | ||
) |
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.
Instead of modifying this base interface class we should
Sampler()
instantiation object inprofiler_interface.py
. We instantiate the object on ln. 391, 525, and 568. Instead of explicitly calling the object there we should define a method (e.g._instantiate_sampler(...)
that will instantiate a sampler objectsqlalchemy/bigquery/profiler_interface.py
BigQueryProfilerInterface
with_profiler_type: str = "bigquery"
._instantiate_sampler(...)
to return aBigQuerySampler()
object (inBigQueryInterface
).source/bigquery/profiler_source.py
overridecreate_profiler_interface
to something like belowThere 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.
thanks, @TeddyCr , super clear idea. I've already upgraded the old code and feel more sustainable with these new ones. Hope it's working out.