Skip to content
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

BigQuery: Add support to Dataset for project_ids with org prefix. #8877

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions bigquery/google/cloud/bigquery/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import six
import copy
import re

import google.cloud._helpers
from google.cloud.bigquery import _helpers
Expand All @@ -26,6 +27,14 @@
from google.cloud.bigquery.table import TableReference


_W_PREFIX = re.compile(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we pick a better name for this? Maybe _PROJECT_PREFIX_PATTERN?

r"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we using a multi-line string here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for the readability. I think I’ll switch this to the single line, after correcting the pattern implementation.

(\S*)\:(?P<ref>\S*)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since at least one character is required, this should probably be \S+, right?

Also, ref isn't all that meaningful to me. How about remaining, since it's everything after the : character?

""",
re.VERBOSE,
)


def _get_table_reference(self, table_id):
"""Constructs a TableReference.

Expand Down Expand Up @@ -269,7 +278,7 @@ def from_string(cls, dataset_id, default_project=None):
Args:
dataset_id (str):
A dataset ID in standard SQL format. If ``default_project``
is not specified, this must included both the project ID and
is not specified, this must include both the project ID and
the dataset ID, separated by ``.``.
default_project (str):
Optional. The project ID to use when ``dataset_id`` does not
Expand All @@ -290,13 +299,17 @@ def from_string(cls, dataset_id, default_project=None):
"""
output_dataset_id = dataset_id
output_project_id = default_project
parts = dataset_id.split(".")
with_prefix = _W_PREFIX.match(dataset_id)
if with_prefix is None:
parts = dataset_id.split(".")
else:
parts = with_prefix.group("ref").split(".")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused. What is this doing? I think the prefix needs to be part of the project ID, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the issue's Stack trace:
The error occurs due to the prefix google.com:. Previously the passed string was separated only by ., what led to ValueError raising because of the len(parts) > 2 at google.com:[project].ryan_dataset. As I see here prefix is not the part of the Project ID itself. I was trying to find out, how could I parse the string and fulfill both the previous and new format. That is why I decided to use regular expressions. Now, with template's help, it will separate prefix and solve several situations:

  • string-project.string_dataset - will pass the template successfully as it was before;
  • prefix:string-project.string_dataset - will group the part without prefix and then will divide it;
  • string-project:string_dataset - if the default_project was not defined raises ValueError;
  • google.com:project:dataset_id - same as above.

ValueError: Too many parts in dataset_id. Expected a fully-qualified dataset ID in standard SQL format. e.g. "project.dataset_id", got google.com:[project].ryan_dataset

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but it appears to me that we're discarding the prefix? Is that correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that is what I was thinking before this conversation. So, now I'm a bit confused. I thought the prefix is an extra part and should be just removed. But if it is actually the part of the Project ID, I'll need to reconfigure the pattern.


if len(parts) == 1 and not default_project:
raise ValueError(
"When default_project is not set, dataset_id must be a "
"fully-qualified dataset ID in standard SQL format. "
'e.g. "project.dataset_id", got {}'.format(dataset_id)
"fully-qualified dataset ID in standard SQL format, "
emar-kar marked this conversation as resolved.
Show resolved Hide resolved
'e.g., "project.dataset_id" got {}'.format(dataset_id)
)
elif len(parts) == 2:
output_project_id, output_dataset_id = parts
Expand Down Expand Up @@ -554,7 +567,7 @@ def from_string(cls, full_dataset_id):
Args:
full_dataset_id (str):
A fully-qualified dataset ID in standard SQL format. Must
included both the project ID and the dataset ID, separated by
include both the project ID and the dataset ID, separated by
``.``.

Returns:
Expand Down
11 changes: 11 additions & 0 deletions bigquery/tests/unit/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,22 @@ def test_from_string(self):
self.assertEqual(got.project, "string-project")
self.assertEqual(got.dataset_id, "string_dataset")

def test_from_string_w_prefix(self):
cls = self._get_target_class()
got = cls.from_string("prefix:string-project.string_dataset")
self.assertEqual(got.project, "string-project")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be prefix:string-project, since the prefix is actually part of the project ID?

self.assertEqual(got.dataset_id, "string_dataset")

def test_from_string_legacy_string(self):
cls = self._get_target_class()
with self.assertRaises(ValueError):
cls.from_string("string-project:string_dataset")

def test_from_string_w_several_prefixes(self):
cls = self._get_target_class()
with self.assertRaises(ValueError):
cls.from_string("google.com:project:dataset_id")

def test_from_string_not_fully_qualified(self):
cls = self._get_target_class()
with self.assertRaises(ValueError):
Expand Down