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 missing format method and load method implementation #197

Merged
merged 4 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions sqlframe/base/mixins/readwriter_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def load(

assert path is not None, "path is required"
assert isinstance(path, str), "path must be a string"
format = format or self.state_format_to_read
format = format or _infer_format(path)
eredzik marked this conversation as resolved.
Show resolved Hide resolved
kwargs = {k: v for k, v in options.items() if v is not None}
if format == "json":
Expand Down
39 changes: 39 additions & 0 deletions sqlframe/base/readerwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
class _BaseDataFrameReader(t.Generic[SESSION, DF]):
def __init__(self, spark: SESSION):
self._session = spark
self.state_format_to_read = None
eredzik marked this conversation as resolved.
Show resolved Hide resolved

@property
def session(self) -> SESSION:
Expand Down Expand Up @@ -67,6 +68,44 @@ def _to_casted_columns(self, column_mapping: t.Dict) -> t.List[Column]:
for k, v in column_mapping.items()
]

def format(self, source: str) -> "Self":
"""Specifies the input data source format.

.. versionadded:: 1.4.0

.. versionchanged:: 3.4.0
Supports Spark Connect.

Parameters
----------
source : str
string, name of the data source, e.g. 'json', 'parquet'.

Examples
--------
>>> spark.read.format('json')
<...readwriter.DataFrameReader object ...>

Write a DataFrame into a JSON file and read it back.

>>> import tempfile
>>> with tempfile.TemporaryDirectory() as d:
... # Write a DataFrame into a JSON file
... spark.createDataFrame(
... [{"age": 100, "name": "Hyukjin Kwon"}]
... ).write.mode("overwrite").format("json").save(d)
...
... # Read the JSON file as a DataFrame.
... spark.read.format('json').load(d).show()
+---+------------+
|age| name|
+---+------------+
|100|Hyukjin Kwon|
+---+------------+
"""
self.state_format_to_read = source
return self

def load(
self,
path: t.Optional[PathOrPaths] = None,
Expand Down
1 change: 1 addition & 0 deletions sqlframe/duckdb/readwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def load(
|100|NULL|
+---+----+
"""
format = format or self.state_format_to_read
if schema:
column_mapping = ensure_column_mapping(schema)
select_column_mapping = column_mapping.copy()
Expand Down
Loading