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

Add labels to BQ uploaded tables #131

Merged
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
15 changes: 15 additions & 0 deletions raster_loader/io/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import json
import pandas as pd
import rasterio
import re

from raster_loader import __version__
from raster_loader.errors import import_error_bigquery, IncompatibleRasterException
from raster_loader.utils import ask_yes_no_question, batched
from raster_loader.io.common import (
Expand Down Expand Up @@ -161,6 +163,9 @@ def done_callback(job):
print("Writing metadata to BigQuery...")
self.write_metadata(metadata, append_records, fqn)

print("Updating labels...")
self.update_labels(fqn, self.get_labels(__version__))

except IncompatibleRasterException as e:
raise IOError("Error uploading to BigQuery: {}".format(e.message))

Expand Down Expand Up @@ -224,6 +229,16 @@ def get_metadata(self, fqn):

return json.loads(rows[0]["metadata"])

def get_labels(self, version: str):
return {
"raster_loader": re.sub(r"[^a-z0-9_-]", "_", version.lower()),
}

def update_labels(self, fqn, labels):
table = self.client.get_table(fqn)
table.labels = labels
table = self.client.update_table(table, ["labels"])

def write_metadata(
self,
metadata,
Expand Down
22 changes: 22 additions & 0 deletions raster_loader/tests/bigquery/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ def test_rasterio_to_bigquery_with_raster_default_band_name():
list(expected_dataframe.band_1), key=lambda x: x if x is not None else b""
)

table = connector.client.get_table(fqn)
assert table.labels.get("raster_loader") is not None


@pytest.mark.integration_test
def test_rasterio_to_bigquery_appending_rows():
Expand Down Expand Up @@ -364,6 +367,7 @@ def test_rasterio_to_table_wrong_band_name_block(*args, **kwargs):
"raster_loader.io.bigquery.BigQueryConnection.check_if_table_exists",
return_value=False,
)
@patch("raster_loader.io.bigquery.BigQueryConnection.update_labels", return_value=None)
@patch("raster_loader.io.bigquery.ask_yes_no_question", return_value=False)
def test_rasterio_to_table(*args, **kwargs):
table_name = "test_mosaic_custom_band_column_1"
Expand All @@ -389,6 +393,7 @@ def test_rasterio_to_table(*args, **kwargs):
@patch("raster_loader.io.common.rasterio_metadata", return_value={})
@patch("raster_loader.io.common.get_number_of_blocks", return_value=1)
@patch("raster_loader.io.bigquery.BigQueryConnection.write_metadata", return_value=None)
@patch("raster_loader.io.bigquery.BigQueryConnection.update_labels", return_value=None)
def test_rasterio_to_table_overwrite(*args, **kwargs):
table_name = "test_mosaic_custom_band_column_1"
connector = mocks.MockBigQueryConnection()
Expand Down Expand Up @@ -424,6 +429,7 @@ def test_rasterio_to_table_overwrite(*args, **kwargs):
"num_pixels": 1,
},
)
@patch("raster_loader.io.bigquery.BigQueryConnection.update_labels", return_value=None)
def test_rasterio_to_table_is_not_empty_append(*args, **kwargs):
table_name = "test_mosaic_custom_band_column_1"
connector = mocks.MockBigQueryConnection()
Expand Down Expand Up @@ -498,6 +504,7 @@ def test_rasterio_to_table_keyboard_interrupt(*args, **kwargs):
"raster_loader.io.bigquery.BigQueryConnection.check_if_table_exists",
return_value=False,
)
@patch("raster_loader.io.bigquery.BigQueryConnection.update_labels", return_value=None)
def test_rasterio_to_table_with_chunk_size(*args, **kwargs):
table_name = "test_mosaic_custom_band_column_1"
connector = mocks.MockBigQueryConnection()
Expand All @@ -515,6 +522,7 @@ def test_rasterio_to_table_with_chunk_size(*args, **kwargs):
"raster_loader.io.bigquery.BigQueryConnection.check_if_table_exists",
return_value=False,
)
@patch("raster_loader.io.bigquery.BigQueryConnection.update_labels", return_value=None)
def test_rasterio_to_table_with_one_chunk_size(*args, **kwargs):
table_name = "test_mosaic_custom_band_column_1"
connector = mocks.MockBigQueryConnection()
Expand Down Expand Up @@ -567,6 +575,7 @@ def test_rasterio_to_table_invalid_raster(*args, **kwargs):
"num_pixels": 1,
},
)
@patch("raster_loader.io.bigquery.BigQueryConnection.update_labels", return_value=None)
def test_rasterio_to_bigquery_valid_raster(*args, **kwargs):
table_name = "test_mosaic_valid_raster".upper()
connector = mocks.MockBigQueryConnection()
Expand Down Expand Up @@ -599,3 +608,16 @@ def test_append_with_different_resolution(*args, **kwargs):
os.path.join(fixtures_dir, "mosaic_cog.tif"),
f"{BQ_PROJECT_ID}.{BQ_DATASET_ID}.{table_name}",
)


def test_get_labels(*args, **kwargs):
connector = mocks.MockBigQueryConnection()

cases = {
"": {"raster_loader": ""},
"0.1.0": {"raster_loader": "0_1_0"},
"0.1.0 something": {"raster_loader": "0_1_0_something"},
"0.1.0+17$g1d1f3a3H": {"raster_loader": "0_1_0_17_g1d1f3a3h"},
}
for version, expected_labels in cases.items():
assert connector.get_labels(version) == expected_labels
Loading