feat: support job reservation by Linchin · Pull Request #2186 · googleapis/python-bigquery · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

feat: support job reservation #2186

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

Merged
merged 6 commits into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 32 additions & 0 deletions google/cloud/bigquery/job/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,26 @@ def job_timeout_ms(self, value):
else:
self._properties.pop("jobTimeoutMs", None)

@property
def reservation(self):
"""str: Optional. The reservation that job would use.

User can specify a reservation to execute the job. If reservation is
not set, reservation is determined based on the rules defined by the
reservation assignments. The expected format is
projects/{project}/locations/{location}/reservations/{reservation}.

Raises:
ValueError: If ``value`` type is not None or of string type.
"""
return self._properties.setdefault("reservation", None)

@reservation.setter
def reservation(self, value):
if value and not isinstance(value, str):
raise ValueError("Reservation must be None or a string.")
self._properties["reservation"] = value

@property
def labels(self):
"""Dict[str, str]: Labels for the job.
Expand Down Expand Up @@ -488,6 +508,18 @@ def location(self):
"""str: Location where the job runs."""
return _helpers._get_sub_prop(self._properties, ["jobReference", "location"])

@property
def reservation_id(self):
"""str: Name of the primary reservation assigned to this job.

Note that this could be different than reservations reported in
the reservation field if parent reservations were used to execute
this job.
"""
return _helpers._get_sub_prop(
self._properties, ["statistics", "reservation_id"]
)

def _require_client(self, client):
"""Check client or verify over-ride.

Expand Down
42 changes: 42 additions & 0 deletions tests/unit/job/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,16 @@ def test_state(self):
status["state"] = state
self.assertEqual(job.state, state)

def test_reservation_id(self):
reservation_id = "RESERVATION-ID"
client = _make_client(project=self.PROJECT)
job = self._make_one(self.JOB_ID, client)
self.assertIsNone(job.reservation_id)
stats = job._properties["statistics"] = {}
self.assertIsNone(job.reservation_id)
stats["reservation_id"] = reservation_id
self.assertEqual(job.reservation_id, reservation_id)

def _set_properties_job(self):
client = _make_client(project=self.PROJECT)
job = self._make_one(self.JOB_ID, client)
Expand Down Expand Up @@ -1188,31 +1198,37 @@ def test_fill_query_job_config_from_default(self):
job_config = QueryJobConfig()
job_config.dry_run = True
job_config.maximum_bytes_billed = 1000
job_config.reservation = "reservation_1"

default_job_config = QueryJobConfig()
default_job_config.use_query_cache = True
default_job_config.maximum_bytes_billed = 2000
default_job_config.reservation = "reservation_2"

final_job_config = job_config._fill_from_default(default_job_config)
self.assertTrue(final_job_config.dry_run)
self.assertTrue(final_job_config.use_query_cache)
self.assertEqual(final_job_config.maximum_bytes_billed, 1000)
self.assertEqual(final_job_config.reservation, "reservation_1")

def test_fill_load_job_from_default(self):
from google.cloud.bigquery import LoadJobConfig

job_config = LoadJobConfig()
job_config.create_session = True
job_config.encoding = "UTF-8"
job_config.reservation = "reservation_1"

default_job_config = LoadJobConfig()
default_job_config.ignore_unknown_values = True
default_job_config.encoding = "ISO-8859-1"
default_job_config.reservation = "reservation_2"

final_job_config = job_config._fill_from_default(default_job_config)
self.assertTrue(final_job_config.create_session)
self.assertTrue(final_job_config.ignore_unknown_values)
self.assertEqual(final_job_config.encoding, "UTF-8")
self.assertEqual(final_job_config.reservation, "reservation_1")

def test_fill_from_default_conflict(self):
from google.cloud.bigquery import QueryJobConfig
Expand All @@ -1232,10 +1248,12 @@ def test_fill_from_empty_default_conflict(self):
job_config = QueryJobConfig()
job_config.dry_run = True
job_config.maximum_bytes_billed = 1000
job_config.reservation = "reservation_1"

final_job_config = job_config._fill_from_default(default_job_config=None)
self.assertTrue(final_job_config.dry_run)
self.assertEqual(final_job_config.maximum_bytes_billed, 1000)
self.assertEqual(final_job_config.reservation, "reservation_1")

@mock.patch("google.cloud.bigquery._helpers._get_sub_prop")
def test__get_sub_prop_wo_default(self, _get_sub_prop):
Expand Down Expand Up @@ -1338,3 +1356,27 @@ def test_job_timeout_properties(self):
job_config.job_timeout_ms = None
assert job_config.job_timeout_ms is None
assert "jobTimeoutMs" not in job_config._properties

def test_reservation_miss(self):
job_config = self._make_one()
self.assertEqual(job_config.reservation, None)

def test_reservation_hit(self):
job_config = self._make_one()
job_config._properties["reservation"] = "foo"
self.assertEqual(job_config.reservation, "foo")

def test_reservation_update_in_place(self):
job_config = self._make_one()
job_config.reservation = "bar" # update in place
self.assertEqual(job_config.reservation, "bar")

def test_reservation_setter_invalid(self):
job_config = self._make_one()
with self.assertRaises(ValueError):
job_config.reservation = object()

def test_reservation_setter(self):
job_config = self._make_one()
job_config.reservation = "foo"
self.assertEqual(job_config._properties["reservation"], "foo")

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.