Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
custom exception for both missing file and BQ source
Signed-off-by: Cody Lin <codyl@twitter.com>
  • Loading branch information
Cody Lin committed Jun 19, 2021
commit 50831ef692421c7bba40efca2bc8ec21a8d4c8d0
14 changes: 9 additions & 5 deletions sdk/python/feast/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from feast import type_map
from feast.data_format import FileFormat, StreamFormat
from feast.errors import BigQuerySourceNotFoundException, FileSourceNotFoundException
from feast.protos.feast.core.DataSource_pb2 import DataSource as DataSourceProto
from feast.value_type import ValueType

Expand Down Expand Up @@ -561,6 +562,7 @@ def __init__(
file_url = path

self._file_options = FileOptions(file_format=file_format, file_url=file_url)
self._assert_table_exists()

super().__init__(
event_timestamp_column,
Expand Down Expand Up @@ -623,6 +625,12 @@ def get_table_column_names_and_types(self) -> Iterable[Tuple[str, str]]:
schema = ParquetFile(self.path).schema_arrow
return zip(schema.names, map(str, schema.types))

def _assert_table_exists(self):
try:
ParquetFile(self.path).schema_arrow
except (FileNotFoundError):
raise FileSourceNotFoundException(self.path)


class BigQuerySource(DataSource):
def __init__(
Expand Down Expand Up @@ -734,11 +742,7 @@ def _assert_table_exists(self):
try:
client.get_table(self.table_ref)
except NotFound:
raise NameError(
f"""
Unable to find table {self.table_ref} in BigQuery. Please check that table exists.
"""
)
raise BigQuerySourceNotFoundException(self.table_ref)


class KafkaSource(DataSource):
Expand Down
16 changes: 16 additions & 0 deletions sdk/python/feast/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
from colorama import Fore, Style


class DataSourceNotFoundException(Exception):
pass


class BigQuerySourceNotFoundException(DataSourceNotFoundException):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure if it's a good idea to have implementation specific errors. We are moving to having all implementations be self-contained. Any downside to using DataSourceNotFoundException instead of subclassing?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah thanks for letting me know - I was following the example of the FeastObjectNotFoundException subclassing pattern here, but since the DataSourceNotFoundExceptions are so similar (and we're not checking FileSource yet), I'll just change it to use DataSourceNotFoundException instead.

def __init__(self, table_ref):
super().__init__(
f"Unable to find table '{table_ref}' in BigQuery. Please check that table exists."
)


class FileSourceNotFoundException(DataSourceNotFoundException):
def __init__(self, path):
super().__init__(f"Unable to find table at '{path}'")


class FeastObjectNotFoundException(Exception):
pass

Expand Down
19 changes: 16 additions & 3 deletions sdk/python/tests/test_data_source.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import pytest
from utils.data_source_utils import simple_bq_source_using_table_ref_arg
from utils.data_source_utils import (
prep_file_source,
simple_bq_source_using_table_ref_arg,
)

from feast.data_source import BigQuerySource
from feast.data_source import BigQuerySource, FileSource
from feast.errors import BigQuerySourceNotFoundException, FileSourceNotFoundException


def test_existent_file_source(simple_dataset_1):
prep_file_source(df=simple_dataset_1)


def test_nonexistent_file_source(simple_dataset_1):
with pytest.raises(FileSourceNotFoundException):
FileSource(file_url="nonexistent_file")


@pytest.mark.integration
Expand All @@ -11,7 +24,7 @@ def test_existent_bq_source(simple_dataset_1):

@pytest.mark.integration
def test_nonexistent_bq_source():
with pytest.raises(NameError):
with pytest.raises(BigQuerySourceNotFoundException):
BigQuerySource(
table_ref="project.dataset.nonexistent_table", event_timestamp_column=""
)