Skip to content

BigQuery: add from_string factory methods to Dataset and Table - #5255

Merged
tswast merged 3 commits into
googleapis:masterfrom
tswast:bq-from-string
Apr 27, 2018
Merged

tswast merged 3 commits into
googleapis:masterfrom
tswast:bq-from-string

Conversation

@tswast

@tswast tswast commented Apr 27, 2018

Copy link
Copy Markdown
Contributor

To make it easier to construct Datasets and Tables when the
full-qualified ID is known ahead of time, this PR adds helper methods
called from_string to Dataset, DatasetReference, Table, and
TableReference. In each case, the expected format is a fully-qualified
ID (including the project ID) in standard SQL format.

dataset = client.create_dataset(
    bigquery.Dataset(
        client.dataset('mydataset', project='other-project')
    )
)

becomes

dataset = client.create_dataset(
    bigquery.Dataset.from_string('other-project.mydataset')
)

Also, while writing doctests/examples, I noticed __repr__ was
inconsistent with the actual constructor on some of these classes. I
fixed that while I was here so those string representations don't cause
confusion.

/cc @maxim-lian

To make it easier to construct Datasets and Tables when the
full-qualified ID is known ahead of time, this PR adds helper methods
called `from_string` to Dataset, DatasetReference, Table, and
TableReference. In each case, the expected format is a fully-qualified
ID (including the project ID) in standard SQL format.

```
dataset = client.create_dataset(
    bigquery.Dataset(
        client.dataset('mydataset', project='other-project')
    )
)
```

becomes

```
dataset = client.create_dataset(
    bigquery.Dataset.from_string('other-project.mydataset')
)
```

Also, while writing doctests/examples, I noticed `__repr__` was
inconsistent with the actual constructor on some of these classes, so I
fixed that while I was here so those string representations don't cause
confusion.
@tswast
tswast requested review from alixhami and tseaver April 27, 2018 17:27
@tswast
tswast requested a review from lukesneeringer as a code owner April 27, 2018 17:27
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Apr 27, 2018
@max-sixty

Copy link
Copy Markdown
Contributor

Thanks a lot!

'standard SQL format. e.g. "project.dataset.table", got '
'{}'.format(full_table_id))

return cls(DatasetReference(parts[0], parts[1]), parts[2])

This comment was marked as spam.

standard SQL format.
"""
dataset_ref = DatasetReference.from_string(full_dataset_id)
return cls(dataset_ref)

This comment was marked as spam.

Comment thread bigquery/google/cloud/bigquery/table.py Outdated
standard SQL format.
"""
table_ref = TableReference.from_string(full_table_id)
return cls(table_ref)

This comment was marked as spam.

@tswast
tswast merged commit 20ff503 into googleapis:master Apr 27, 2018
@tswast
tswast deleted the bq-from-string branch April 27, 2018 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants