Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ message Read {
}

message DataSource {
// (Required) Supported formats include: parquet, orc, text, json, parquet, csv, avro.
string format = 1;
// (Optional) Supported formats include: parquet, orc, text, json, parquet, csv, avro.
//
// If not set, the value from SQL conf 'spark.sql.sources.default' will be used.
optional string format = 1;

// (Optional) If not set, Spark will infer the schema.
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -667,12 +667,11 @@ class SparkConnectPlanner(val session: SparkSession) {
UnresolvedRelation(multipartIdentifier)

case proto.Read.ReadTypeCase.DATA_SOURCE =>
if (rel.getDataSource.getFormat == "") {
throw InvalidPlanInput("DataSource requires a format")
}
val localMap = CaseInsensitiveMap[String](rel.getDataSource.getOptionsMap.asScala.toMap)
val reader = session.read
reader.format(rel.getDataSource.getFormat)
if (rel.getDataSource.hasFormat) {
reader.format(rel.getDataSource.getFormat)
}
localMap.foreach { case (key, value) => reader.option(key, value) }
if (rel.getDataSource.hasSchema && rel.getDataSource.getSchema.nonEmpty) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,18 +332,6 @@ class SparkConnectPlannerSuite extends SparkFunSuite with SparkConnectPlanTest {
assert(res.nodeName == "Aggregate")
}

test("Invalid DataSource") {
val dataSource = proto.Read.DataSource.newBuilder()

val e = intercept[InvalidPlanInput](
transform(
proto.Relation
.newBuilder()
.setRead(proto.Read.newBuilder().setDataSource(dataSource))
.build()))
assert(e.getMessage.contains("DataSource requires a format"))
}

test("Test invalid deduplicate") {
val deduplicate = proto.Deduplicate
.newBuilder()
Expand Down
8 changes: 4 additions & 4 deletions python/pyspark/sql/connect/plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,14 @@ class DataSource(LogicalPlan):

def __init__(
self,
format: str,
format: Optional[str] = None,
schema: Optional[str] = None,
options: Optional[Mapping[str, str]] = None,
paths: Optional[List[str]] = None,
) -> None:
super().__init__(None)

assert isinstance(format, str) and format != ""

assert format is None or isinstance(format, str)
assert schema is None or isinstance(schema, str)

if options is not None:
Expand All @@ -282,7 +281,8 @@ def __init__(

def plan(self, session: "SparkConnectClient") -> proto.Relation:
plan = self._create_proto_relation()
plan.read.data_source.format = self._format
if self._format is not None:
plan.read.data_source.format = self._format
if self._schema is not None:
plan.read.data_source.schema = self._schema
if self._options is not None and len(self._options) > 0:
Expand Down
186 changes: 93 additions & 93 deletions python/pyspark/sql/connect/proto/relations_pb2.py

Large diffs are not rendered by default.

26 changes: 23 additions & 3 deletions python/pyspark/sql/connect/proto/relations_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,10 @@ class Read(google.protobuf.message.Message):
OPTIONS_FIELD_NUMBER: builtins.int
PATHS_FIELD_NUMBER: builtins.int
format: builtins.str
"""(Required) Supported formats include: parquet, orc, text, json, parquet, csv, avro."""
"""(Optional) Supported formats include: parquet, orc, text, json, parquet, csv, avro.

If not set, the value from SQL conf 'spark.sql.sources.default' will be used.
"""
schema: builtins.str
"""(Optional) If not set, Spark will infer the schema.

Expand All @@ -624,17 +627,29 @@ class Read(google.protobuf.message.Message):
def __init__(
self,
*,
format: builtins.str = ...,
format: builtins.str | None = ...,
schema: builtins.str | None = ...,
options: collections.abc.Mapping[builtins.str, builtins.str] | None = ...,
paths: collections.abc.Iterable[builtins.str] | None = ...,
) -> None: ...
def HasField(
self, field_name: typing_extensions.Literal["_schema", b"_schema", "schema", b"schema"]
self,
field_name: typing_extensions.Literal[
"_format",
b"_format",
"_schema",
b"_schema",
"format",
b"format",
"schema",
b"schema",
],
) -> builtins.bool: ...
def ClearField(
self,
field_name: typing_extensions.Literal[
"_format",
b"_format",
"_schema",
b"_schema",
"format",
Expand All @@ -647,6 +662,11 @@ class Read(google.protobuf.message.Message):
b"schema",
],
) -> None: ...
@typing.overload
def WhichOneof(
self, oneof_group: typing_extensions.Literal["_format", b"_format"]
) -> typing_extensions.Literal["format"] | None: ...
@typing.overload
def WhichOneof(
self, oneof_group: typing_extensions.Literal["_schema", b"_schema"]
) -> typing_extensions.Literal["schema"] | None: ...
Expand Down
2 changes: 1 addition & 1 deletion python/pyspark/sql/connect/readwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class DataFrameReader(OptionUtils):

def __init__(self, client: "SparkSession"):
self._client = client
self._format = ""
self._format: Optional[str] = None
self._schema = ""
self._options: Dict[str, str] = {}

Expand Down
10 changes: 1 addition & 9 deletions python/pyspark/sql/tests/connect/test_parity_readwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,7 @@


class ReadwriterParityTests(ReadwriterTestsMixin, ReusedConnectTestCase):
# TODO(SPARK-41834): Implement SparkSession.conf
@unittest.skip("Fails in Spark Connect, should enable.")
def test_save_and_load(self):
super().test_save_and_load()

# TODO(SPARK-41834): Implement SparkSession.conf
@unittest.skip("Fails in Spark Connect, should enable.")
def test_save_and_load_builder(self):
super().test_save_and_load_builder()
pass


class ReadwriterV2ParityTests(ReadwriterV2TestsMixin, ReusedConnectTestCase):
Expand Down
126 changes: 64 additions & 62 deletions python/pyspark/sql/tests/test_readwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,75 +31,77 @@ def test_save_and_load(self):
df = self.df
tmpPath = tempfile.mkdtemp()
shutil.rmtree(tmpPath)
df.write.json(tmpPath)
actual = self.spark.read.json(tmpPath)
self.assertEqual(sorted(df.collect()), sorted(actual.collect()))

schema = StructType([StructField("value", StringType(), True)])
actual = self.spark.read.json(tmpPath, schema)
self.assertEqual(sorted(df.select("value").collect()), sorted(actual.collect()))

df.write.json(tmpPath, "overwrite")
actual = self.spark.read.json(tmpPath)
self.assertEqual(sorted(df.collect()), sorted(actual.collect()))

df.write.save(
format="json",
mode="overwrite",
path=tmpPath,
noUse="this options will not be used in save.",
)
actual = self.spark.read.load(
format="json", path=tmpPath, noUse="this options will not be used in load."
)
self.assertEqual(sorted(df.collect()), sorted(actual.collect()))

defaultDataSourceName = self.spark.conf.get(
"spark.sql.sources.default", "org.apache.spark.sql.parquet"
)
self.spark.sql("SET spark.sql.sources.default=org.apache.spark.sql.json")
actual = self.spark.read.load(path=tmpPath)
self.assertEqual(sorted(df.collect()), sorted(actual.collect()))
self.spark.sql("SET spark.sql.sources.default=" + defaultDataSourceName)
try:
Copy link
Member Author

@ueshin ueshin Feb 25, 2023

Choose a reason for hiding this comment

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

The changes in this file is to make the cleanup done properly.

df.write.json(tmpPath)
actual = self.spark.read.json(tmpPath)
self.assertEqual(sorted(df.collect()), sorted(actual.collect()))

schema = StructType([StructField("value", StringType(), True)])
actual = self.spark.read.json(tmpPath, schema)
self.assertEqual(sorted(df.select("value").collect()), sorted(actual.collect()))

df.write.json(tmpPath, "overwrite")
actual = self.spark.read.json(tmpPath)
self.assertEqual(sorted(df.collect()), sorted(actual.collect()))

df.write.save(
format="json",
mode="overwrite",
path=tmpPath,
noUse="this options will not be used in save.",
)
actual = self.spark.read.load(
format="json", path=tmpPath, noUse="this options will not be used in load."
)
self.assertEqual(sorted(df.collect()), sorted(actual.collect()))

csvpath = os.path.join(tempfile.mkdtemp(), "data")
df.write.option("quote", None).format("csv").save(csvpath)
try:
self.spark.sql("SET spark.sql.sources.default=org.apache.spark.sql.json").collect()
actual = self.spark.read.load(path=tmpPath)
self.assertEqual(sorted(df.collect()), sorted(actual.collect()))
finally:
self.spark.sql("RESET spark.sql.sources.default").collect()

shutil.rmtree(tmpPath)
csvpath = os.path.join(tempfile.mkdtemp(), "data")
df.write.option("quote", None).format("csv").save(csvpath)
finally:
shutil.rmtree(tmpPath)

def test_save_and_load_builder(self):
df = self.df
tmpPath = tempfile.mkdtemp()
shutil.rmtree(tmpPath)
df.write.json(tmpPath)
actual = self.spark.read.json(tmpPath)
self.assertEqual(sorted(df.collect()), sorted(actual.collect()))

schema = StructType([StructField("value", StringType(), True)])
actual = self.spark.read.json(tmpPath, schema)
self.assertEqual(sorted(df.select("value").collect()), sorted(actual.collect()))

df.write.mode("overwrite").json(tmpPath)
actual = self.spark.read.json(tmpPath)
self.assertEqual(sorted(df.collect()), sorted(actual.collect()))

df.write.mode("overwrite").options(noUse="this options will not be used in save.").option(
"noUse", "this option will not be used in save."
).format("json").save(path=tmpPath)
actual = self.spark.read.format("json").load(
path=tmpPath, noUse="this options will not be used in load."
)
self.assertEqual(sorted(df.collect()), sorted(actual.collect()))

defaultDataSourceName = self.spark.conf.get(
"spark.sql.sources.default", "org.apache.spark.sql.parquet"
)
self.spark.sql("SET spark.sql.sources.default=org.apache.spark.sql.json")
actual = self.spark.read.load(path=tmpPath)
self.assertEqual(sorted(df.collect()), sorted(actual.collect()))
self.spark.sql("SET spark.sql.sources.default=" + defaultDataSourceName)

shutil.rmtree(tmpPath)
try:
df.write.json(tmpPath)
actual = self.spark.read.json(tmpPath)
self.assertEqual(sorted(df.collect()), sorted(actual.collect()))

schema = StructType([StructField("value", StringType(), True)])
actual = self.spark.read.json(tmpPath, schema)
self.assertEqual(sorted(df.select("value").collect()), sorted(actual.collect()))

df.write.mode("overwrite").json(tmpPath)
actual = self.spark.read.json(tmpPath)
self.assertEqual(sorted(df.collect()), sorted(actual.collect()))

df.write.mode("overwrite").options(
noUse="this options will not be used in save."
).option("noUse", "this option will not be used in save.").format("json").save(
path=tmpPath
)
actual = self.spark.read.format("json").load(
path=tmpPath, noUse="this options will not be used in load."
)
self.assertEqual(sorted(df.collect()), sorted(actual.collect()))

try:
self.spark.sql("SET spark.sql.sources.default=org.apache.spark.sql.json").collect()
actual = self.spark.read.load(path=tmpPath)
self.assertEqual(sorted(df.collect()), sorted(actual.collect()))
finally:
self.spark.sql("RESET spark.sql.sources.default").collect()
finally:
shutil.rmtree(tmpPath)

def test_bucketed_write(self):
data = [
Expand Down