HyukjinKwon commented on code in PR #44497:
URL: https://github.com/apache/spark/pull/44497#discussion_r1436660963
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/python/PythonDataSourceSuite.scala:
##########
@@ -548,4 +548,94 @@ class PythonDataSourceSuite extends QueryTest with
SharedSparkSession {
"cannot be written with ErrorIfExists mode, please use Append or
Overwrite modes instead."))
}
}
+
+ test("data source write commit and abort") {
+ assume(shouldTestPandasUDFs)
+ val dataSourceScript =
+ s"""
+ |import json
+ |from dataclasses import dataclass
+ |from pyspark import TaskContext
+ |from pyspark.sql.datasource import DataSource, DataSourceWriter,
WriterCommitMessage
+ |
+ |@dataclass
+ |class SimpleCommitMessage(WriterCommitMessage):
+ | partition_id: int
+ | count: int
+ |
+ |class SimpleDataSourceWriter(DataSourceWriter):
+ | def __init__(self, options):
+ | self.options = options
+ | self.path = self.options.get("path")
+ | assert self.path is not None
+ |
+ | def write(self, iterator):
+ | context = TaskContext.get()
+ | partition_id = context.partitionId()
+ | output_path = f"{self.path}/{partition_id}.json"
+ | cnt = 0
+ | with open(output_path, "w") as file:
+ | for row in iterator:
+ | if row.id >= 10:
+ | raise Exception("invalid value")
+ | file.write(json.dumps(row.asDict()) + "\\n")
+ | cnt += 1
+ | return SimpleCommitMessage(partition_id=partition_id,
count=cnt)
+ |
+ | def commit(self, messages) -> None:
+ | status = dict(num_files=len(messages), count=sum(m.count for
m in messages))
+ |
+ | with open(f"{self.path}/success.json", "a") as file:
+ | file.write(json.dumps(status) + "\\n")
Review Comment:
and add `os.path.sep`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]