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

Add MapType as JSON-compatible #776

Merged
merged 8 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 3 additions & 1 deletion streaming/base/converters/dataframe_to_mds.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pyspark.sql.dataframe import DataFrame
from pyspark.sql.types import (ArrayType, BinaryType, BooleanType, ByteType, DateType,
DayTimeIntervalType, DecimalType, DoubleType, FloatType,
IntegerType, LongType, NullType, ShortType, StringType,
IntegerType, LongType, MapType, NullType, ShortType, StringType,
StructField, StructType, TimestampNTZType, TimestampType)
except ImportError as e:
e.msg = get_import_exception_message(e.name, extra_deps='spark') # pyright: ignore
Expand Down Expand Up @@ -70,6 +70,8 @@ def is_json_compatible(data_type: Any):
return all(is_json_compatible(field.dataType) for field in data_type.fields)
elif isinstance(data_type, ArrayType):
return is_json_compatible(data_type.elementType)
elif isinstance(data_type, MapType):
return is_json_compatible(data_type.keyType) and is_json_compatible(data_type.valueType)
elif isinstance(data_type, (StringType, IntegerType, FloatType, BooleanType, NullType)):
return True
else:
Expand Down
13 changes: 9 additions & 4 deletions tests/base/converters/test_dataframe_to_mds.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,13 +418,18 @@ def test_is_json_compatible(self):
]), True), True)
])

valid_schemas = [message_schema, prompt_response_schema, combined_schema]
schema_with_string_map_keys = StructType(
snarayan21 marked this conversation as resolved.
Show resolved Hide resolved
[StructField('map_field', MapType(StringType(), StringType()), nullable=True)])

valid_schemas = [
message_schema, prompt_response_schema, combined_schema, schema_with_string_map_keys
snarayan21 marked this conversation as resolved.
Show resolved Hide resolved
]

schema_with_binary = StructType([StructField('data', BinaryType(), nullable=True)])

# Schema with MapType having non-string keys
schema_with_non_string_map_keys = StructType(
[StructField('map_field', MapType(IntegerType(), StringType()), nullable=True)])
[StructField('map_field', MapType(BinaryType(), StringType()), nullable=True)])
snarayan21 marked this conversation as resolved.
Show resolved Hide resolved
srowen marked this conversation as resolved.
Show resolved Hide resolved

# Schema with DateType and TimestampType
schema_with_date_and_timestamp = StructType([
Expand All @@ -437,10 +442,10 @@ def test_is_json_compatible(self):
]

for s in valid_schemas:
assert is_json_compatible(s)
assert is_json_compatible(s), str(s)

for s in invalid_schemas:
assert not is_json_compatible(s)
assert not is_json_compatible(s), str(s)

def test_complex_schema(self,
complex_dataframe: Any,
Expand Down
Loading