-
Notifications
You must be signed in to change notification settings - Fork 28k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
DataSourceStreamReader, | ||
InputPartition, | ||
DataSourceStreamWriter, | ||
SimpleDataSourceStreamReader, | ||
WriterCommitMessage, | ||
) | ||
from pyspark.sql.types import Row | ||
|
@@ -136,6 +137,33 @@ def streamWriter(self, schema, overwrite): | |
|
||
return TestDataSource | ||
|
||
def _get_simple_data_source(self): | ||
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() | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
output_dir = tempfile.TemporaryDirectory(prefix="test_data_stream_write_output") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
has to be
because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
|
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.
Maybe just define this class and directly pass
SimpleDataSource
toregister
?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.
Sounds good.