Skip to content

Commit

Permalink
refactor: completely remove get_slice for snowflake translator
Browse files Browse the repository at this point in the history
Signed-off-by: Luka Peschke <[email protected]>
  • Loading branch information
lukapeschke committed Apr 25, 2024
1 parent 0abbf58 commit 1fe77f6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 21 deletions.
7 changes: 4 additions & 3 deletions tests/snowflake/test_snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def test_retrieve_data_slice_offset_limit(
snowflake_connector: SnowflakeConnector, snowflake_datasource: SnowflakeDataSource
):
df_result: DataSlice = snowflake_connector.get_slice(snowflake_datasource, offset=5, limit=3)
assert 11 == len(df_result.df)
assert len(df_result.df) == 3
assert df_result.pagination_info.pagination_info.type == "unknown_size"


Expand All @@ -240,13 +240,14 @@ def test_retrieve_data_slice_too_much(
snowflake_connector: SnowflakeConnector, snowflake_datasource: SnowflakeDataSource
):
df_result: DataSlice = snowflake_connector.get_slice(snowflake_datasource, offset=10, limit=20)
assert 11 == len(df_result.df)
# Result has only 11 rows
assert len(df_result.df) == 1


@pytest.mark.usefixtures("snowflake_retrieve_data")
def test_retrieve_data_fetch(snowflake_connector: SnowflakeConnector, snowflake_datasource: SnowflakeDataSource):
df_result = snowflake_connector._fetch_data(snowflake_datasource)
assert 11 == len(df_result)
assert len(df_result) == 11


def test_schema_fields_order():
Expand Down
18 changes: 1 addition & 17 deletions toucan_connectors/snowflake/snowflake_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
from snowflake.connector.cursor import DictCursor as SfDictCursor

from toucan_connectors.common import ConnectorStatus
from toucan_connectors.pagination import build_pagination_info
from toucan_connectors.snowflake_common import (
build_database_model_extraction_query,
type_code_mapping,
)
from toucan_connectors.sql_query_helper import SqlQueryHelper
from toucan_connectors.toucan_connector import (
Category,
DataSlice,
DiscoverableConnector,
PlainJsonSecretStr,
TableInfo,
Expand Down Expand Up @@ -422,23 +420,9 @@ def _fetch_data(self, data_source: SnowflakeDataSource) -> pd.DataFrame:
)

def _retrieve_data(self, data_source: SnowflakeDataSource) -> pd.DataFrame:
return self._fetch_data(data_source)

def get_slice(
self,
data_source: SnowflakeDataSource,
permissions: dict | None = None,
offset: int = 0,
limit: int | None = None,
get_row_count: bool | None = False,
) -> DataSlice:
# We assume permissions have been applied earlier and that OFFSET and LIMIT have been
# applied by the PyPika translator
df = self._fetch_data(data_source)
return DataSlice(
df=df,
pagination_info=build_pagination_info(offset=0, limit=limit, total_rows=None, retrieved_rows=len(df)),
)
return self._fetch_data(data_source)

def describe(self, data_source: SnowflakeDataSource) -> dict[str, str]:
return self._describe_query(data_source.query)
Expand Down
4 changes: 3 additions & 1 deletion toucan_connectors/toucan_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,10 @@ def get_slice(
df = self.get_df(data_source, permissions)
truncated_df = df[offset : offset + limit] if limit is not None else df[offset:]

# FIXME: We should only have total_rows=None for SQL connectors here, where we cannot
# know if we fetched all rows since offset and limit are part of the query
pagination_info = build_pagination_info(
offset=offset, limit=limit, retrieved_rows=len(truncated_df), total_rows=len(df)
offset=offset, limit=limit, retrieved_rows=len(truncated_df), total_rows=None
)

return DataSlice(
Expand Down

0 comments on commit 1fe77f6

Please sign in to comment.