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

[SPARK-48062][PYTHON][SS][TESTS] Add pyspark test for SimpleDataSourceStreamingReader #46306

Closed
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions python/pyspark/sql/tests/test_python_streaming_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
DataSourceStreamReader,
InputPartition,
DataSourceStreamWriter,
SimpleDataSourceStreamReader,
WriterCommitMessage,
)
from pyspark.sql.types import Row
Expand Down Expand Up @@ -136,6 +137,33 @@ def streamWriter(self, schema, overwrite):

return TestDataSource

def _get_simple_data_source(self):
Copy link
Member

Choose a reason for hiding this comment

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

Maybe just define this class and directly pass SimpleDataSource to register?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good.

class SimpleStreamReader(SimpleDataSourceStreamReader):
def initialOffset(self):
return {"offset": 0}

def read(self, start: dict):
start_idx = start["offset"]
it = iter([(i,) for i in range(start_idx, start_idx + 2)])
return (it, {"offset": start_idx + 2})

def commit(self, end):
pass

def readBetweenOffsets(self, start: dict, end: dict):
start_idx = start["offset"]
end_idx = end["offset"]
return iter([(i,) for i in range(start_idx, end_idx)])

class SimpleDataSource(DataSource):
def schema(self):
return "id INT"

def simpleStreamReader(self, schema):
return SimpleStreamReader()

return SimpleDataSource

def test_stream_reader(self):
self.spark.dataSource.register(self._get_test_data_source())
df = self.spark.readStream.format("TestDataSource").load()
Expand All @@ -150,6 +178,20 @@ def check_batch(df, batch_id):
q.awaitTermination
self.assertIsNone(q.exception(), "No exception has to be propagated.")

def test_simple_stream_reader(self):
self.spark.dataSource.register(self._get_simple_data_source())
df = self.spark.readStream.format("SimpleDataSource").load()

def check_batch(df, batch_id):
assertDataFrameEqual(df, [Row(batch_id * 2), Row(batch_id * 2 + 1)])

q = df.writeStream.foreachBatch(check_batch).start()
while len(q.recentProgress) < 10:
time.sleep(0.2)
q.stop()
q.awaitTermination
Copy link
Member

Choose a reason for hiding this comment

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

awaitTermination()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch!

self.assertIsNone(q.exception(), "No exception has to be propagated.")

def test_stream_writer(self):
input_dir = tempfile.TemporaryDirectory(prefix="test_data_stream_write_input")
Copy link
Member

Choose a reason for hiding this comment

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

let's add cleanup

        input_dir.cleanup()
        output_dir.cleanup()
        checkpoint_dir.cleanup()

those into finally. Feel free to do it in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

output_dir = tempfile.TemporaryDirectory(prefix="test_data_stream_write_output")
Copy link
Member

Choose a reason for hiding this comment

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

class PythonStreamingDataSourceTests(BasePythonStreamingDataSourceTestsMixin, ReusedSQLTestCase):
    ...

has to be

class PythonStreamingDataSourceTests(BasePythonStreamingDataSourceTestsMixin, ReusedSQLTestCase):
    pass

because ... basically means to omit (and to be defined/implemented laster).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

Expand Down