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

Updating data frame reader extensions to support csv function #32

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
64 changes: 49 additions & 15 deletions snowpark_extensions/dataframe_reader_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import logging
DataFrameReader.___extended = True
DataFrameReader.__option = DataFrameReader.option
DataFrameReader.__csv = DataFrameReader.csv

def _option(self, key: str, value: Any) -> "DataFrameReader":
key = key.upper()
if key == "SEP" or key == "DELIMITER":
Expand Down Expand Up @@ -42,28 +44,17 @@ def _load(self,path: Union[str, List[str], None] = None, format: Optional[str] =
self.format(format)
if schema:
self.schema(schema)
files = []
if isinstance(path,list):
files.extend(path)
else:
files.append(path)
files = get_file_paths(path)
session = context.get_active_session()
if stage is None:
stage = f'{session.get_fully_qualified_current_schema()}.{_generate_prefix("TEMP_STAGE")}'
session.sql(f'create TEMPORARY stage if not exists {stage}').show()
stage_files = [x for x in path if x.startswith("@")]
if len(stage_files) > 1:
raise Exception("Currently only one staged file can be specified. You can use a pattern if you want to specify several files")
print(f"Uploading files using stage {stage}")
for file in files:
if file.startswith("file://"): # upload local file
session.file.put(file,stage)
elif file.startswith("@"): #ignore it is on an stage
return self._read_semi_structured_file(file,format)
else: #assume it is file too
session.file.put(f"file://{file}",f"@{stage}")
stage = get_stage(self, session, files, stage)
if self._file_type == "csv":
return self.csv(f"@{stage}")
return self.__csv(f"@{stage}")
return self._read_semi_structured_file(f"@{stage}",format)

def _format(self, file_type: str) -> "DataFrameReader":
Expand All @@ -72,7 +63,50 @@ def _format(self, file_type: str) -> "DataFrameReader":
self._file_type = file_type
else:
raise Exception(f"Unsupported file format {file_type}")

def _csv(self,path: Union[str, List[str]],schema: Optional[Union[StructType, str]] = None,sep: Optional[str] = None,encoding: Optional[str] = None,quote: Optional[str] = None,
escape: Optional[str] = None,comment: Optional[str] = None,header: Optional[Union[bool, str]] = None,inferSchema: Optional[Union[bool, str]] = None,
ignoreLeadingWhiteSpace: Optional[Union[bool, str]] = None,ignoreTrailingWhiteSpace: Optional[Union[bool, str]] = None,nullValue: Optional[str] = None,
nanValue: Optional[str] = None,positiveInf: Optional[str] = None,negativeInf: Optional[str] = None,dateFormat: Optional[str] = None,timestampFormat: Optional[str] = None,
maxColumns: Optional[Union[int, str]] = None,maxCharsPerColumn: Optional[Union[int, str]] = None,maxMalformedLogPerPartition: Optional[Union[int, str]] = None,
mode: Optional[str] = None,columnNameOfCorruptRecord: Optional[str] = None,multiLine: Optional[Union[bool, str]] = None,charToEscapeQuoteEscaping: Optional[str] = None,
samplingRatio: Optional[Union[float, str]] = None,enforceSchema: Optional[Union[bool, str]] = None,emptyValue: Optional[str] = None,locale: Optional[str] = None,
lineSep: Optional[str] = None,pathGlobFilter: Optional[Union[bool, str]] = None,recursiveFileLookup: Optional[Union[bool, str]] = None,modifiedBefore: Optional[Union[bool, str]] = None,
modifiedAfter: Optional[Union[bool, str]] = None,unescapedQuoteHandling: Optional[str] = None) -> "DataFrame":
params = {k: v for k, v in locals().items() if v is not None}
params.pop("self", None)
params.pop("path", None)
params.pop("schema", None)
if schema:
self.schema(schema)
files = get_file_paths(path)
session = context.get_active_session()
stage = f'{session.get_fully_qualified_current_schema()}.{_generate_prefix("TEMP_STAGE")}'
session.sql(f'create TEMPORARY stage if not exists {stage}').show()
stage = get_stage(self, session, files, stage)
for key, value in params.items():
self = self.option(key, value)
return self.__csv(f"@{stage}")

def get_file_paths(path: Union[str, List[str]]):
if isinstance(path,list):
return path
else:
return [path]

def get_stage(self, session, files: List[str], stage: str):
print(f"Uploading files using stage {stage}")
for file in files:
if file.startswith("file://"): # upload local file
session.file.put(file,stage)
elif file.startswith("@"): #ignore it is on an stage
return self._read_semi_structured_file(file,format)
sfc-gh-jvenegasvega-1 marked this conversation as resolved.
Show resolved Hide resolved
else: #assume it is file too
session.file.put(f"file://{file}",f"@{stage}")
return stage

DataFrameReader.format = _format
DataFrameReader.load = _load
DataFrameReader.option = _option
DataFrameReader.option = _option
DataFrameReader.csv = _csv

27 changes: 19 additions & 8 deletions tests/test_dataframe_reader_extensions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import pytest
from snowflake.snowpark import Session, Row
from snowflake.snowpark import Session, Row, DataFrameReader
from snowflake.snowpark.types import *
import snowpark_extensions

def test_load():
session = Session.builder.from_snowsql().getOrCreate()
cases = session.read.load(["./tests/data/test1_0.csv","./tests/data/test1_1.csv"],
schema=get_schema(),
format="csv",
sep=",",
header="true")
assert 10 == len(cases.collect())

def test_csv():

Choose a reason for hiding this comment

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

I think we should covert more scenarios for testing. a few examples:
We have loading with schema, what about loading with schema.
With and without header.
With different settings in the params

Choose a reason for hiding this comment

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

please have a look to this parametrize testing mode.
https://docs.pytest.org/en/latest/example/parametrize.html#paramexamples

session = Session.builder.from_snowsql().getOrCreate()
csvInfo = session.read.csv(["./tests/data/test1_0.csv","./tests/data/test1_1.csv"],
schema=get_schema(),
sep=",",
header="true")
assert 10 == len(csvInfo.collect())

def get_schema():
schema = StructType([ \
StructField("case_id", StringType()), \
StructField("province", StringType()), \
Expand All @@ -13,11 +29,6 @@ def test_load():
StructField("infection_case",StringType()), \
StructField("confirmed", IntegerType()), \
StructField("latitude", FloatType()), \
StructField("cilongitudety", FloatType()) \
StructField("longitude", FloatType()) \
sfc-gh-jvenegasvega-1 marked this conversation as resolved.
Show resolved Hide resolved
])
cases = session.read.load(["./tests/data/test1_0.csv","./tests/data/test1_1.csv"],
schema=schema,
format="csv",
sep=",",
header="true")
assert 10 == len(cases.collect())
return schema