-
Notifications
You must be signed in to change notification settings - Fork 276
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
Spark DataFrames handled as a type if using spark #267
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
07e2308
Spark DataFrames handled as a type if using spark
ea7ffd2
Update flytekit/types/schema.py
kumare3 f508bc9
update 3.7 also to use spark3
0c78dd4
updated
0b9c336
Merge branch 'annotations' into sparkdataframe
1bfc624
unit test workaround
de9e840
fmt fixed
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .schema import SparkDataFrameSchemaReader, SparkDataFrameSchemaWriter, SparkDataFrameTransformer | ||
from .task import Spark | ||
|
||
__all__ = [Spark] | ||
__all__ = [Spark, SparkDataFrameTransformer, SparkDataFrameSchemaReader, SparkDataFrameSchemaWriter] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import typing | ||
from typing import Type | ||
|
||
from flytekit import FlyteContext | ||
from flytekit.annotated.type_engine import T, TypeEngine, TypeTransformer | ||
from flytekit.models.literals import Literal, Scalar, Schema | ||
from flytekit.models.types import LiteralType, SchemaType | ||
from flytekit.plugins import pyspark | ||
from flytekit.types.schema import SchemaEngine, SchemaFormat, SchemaHandler, SchemaReader, SchemaWriter | ||
|
||
|
||
class SparkDataFrameSchemaReader(SchemaReader[pyspark.sql.DataFrame]): | ||
def __init__(self, from_path: str, cols: typing.Optional[typing.Dict[str, type]], fmt: SchemaFormat): | ||
super().__init__(from_path, cols, fmt) | ||
|
||
def iter(self, **kwargs) -> typing.Generator[T, None, None]: | ||
raise NotImplementedError("Spark DataFrame reader cannot iterate over individual chunks in spark dataframe") | ||
|
||
def all(self, **kwargs) -> pyspark.sql.DataFrame: | ||
if self._fmt == SchemaFormat.PARQUET: | ||
ctx = FlyteContext.current_context().user_space_params | ||
return ctx.spark_session.read.parquet(self.from_path) | ||
raise AssertionError("Only Parquet type files are supported for spark dataframe currently") | ||
|
||
|
||
class SparkDataFrameSchemaWriter(SchemaWriter[pyspark.sql.DataFrame]): | ||
def __init__(self, to_path: str, cols: typing.Optional[typing.Dict[str, type]], fmt: SchemaFormat): | ||
super().__init__(to_path, cols, fmt) | ||
|
||
def write(self, *dfs: pyspark.sql.DataFrame, **kwargs): | ||
if dfs is None or len(dfs) == 0: | ||
return | ||
if len(dfs) > 1: | ||
raise AssertionError("Only one Spark.DataFrame can be returned per return variable currently") | ||
if self._fmt == SchemaFormat.PARQUET: | ||
dfs[0].write.mode("overwrite").parquet(self.to_path) | ||
return | ||
raise AssertionError("Only Parquet type files are supported for spark dataframe currently") | ||
|
||
|
||
class SparkDataFrameTransformer(TypeTransformer[pyspark.sql.DataFrame]): | ||
""" | ||
Transforms Spark DataFrame's to and from a Schema (typed/untyped) | ||
""" | ||
|
||
def __init__(self): | ||
super(SparkDataFrameTransformer, self).__init__("spark-df-transformer", t=pyspark.sql.DataFrame) | ||
|
||
@staticmethod | ||
def _get_schema_type() -> SchemaType: | ||
return SchemaType(columns=[]) | ||
|
||
def get_literal_type(self, t: Type[pyspark.sql.DataFrame]) -> LiteralType: | ||
return LiteralType(schema=self._get_schema_type()) | ||
|
||
def to_literal( | ||
self, | ||
ctx: FlyteContext, | ||
python_val: pyspark.sql.DataFrame, | ||
python_type: Type[pyspark.sql.DataFrame], | ||
expected: LiteralType, | ||
) -> Literal: | ||
remote_path = ctx.file_access.get_random_remote_directory() | ||
w = SparkDataFrameSchemaWriter(to_path=remote_path, cols=None, fmt=SchemaFormat.PARQUET) | ||
w.write(python_val) | ||
return Literal(scalar=Scalar(schema=Schema(remote_path, self._get_schema_type()))) | ||
|
||
def to_python_value(self, ctx: FlyteContext, lv: Literal, expected_python_type: Type[pyspark.sql.DataFrame]) -> T: | ||
if not (lv and lv.scalar and lv.scalar.schema): | ||
return pyspark.sql.DataFrame() | ||
r = SparkDataFrameSchemaReader(from_path=lv.scalar.schema.uri, cols=None, fmt=SchemaFormat.PARQUET) | ||
return r.all() | ||
|
||
|
||
SchemaEngine.register_handler( | ||
SchemaHandler( | ||
"pyspark.sql.DataFrame-Schema", | ||
pyspark.sql.DataFrame, | ||
SparkDataFrameSchemaReader, | ||
SparkDataFrameSchemaWriter, | ||
handles_remote_io=True, | ||
) | ||
) | ||
TypeEngine.register(SparkDataFrameTransformer()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,3 @@ | ||
from flytekit.types.flyte_file import FlyteFile | ||
from flytekit.types.schema import ( | ||
FlyteSchema, | ||
PandasSchemaReader, | ||
PandasSchemaWriter, | ||
Schema, | ||
SchemaFormat, | ||
SchemaOpenMode, | ||
SchemaType, | ||
) | ||
from flytekit.types.pandas_schema import PandasSchemaReader, PandasSchemaWriter | ||
from flytekit.types.schema import FlyteSchema |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a huge fan of this function signature... can we think of a way around this? i'd rather pass in the parent FlyteContext and access the user space params from there. This function name makes it seem like a generic setup call, but it always takes in and returns just the user params? that seems limiting