Skip to content

Commit c6fb9c5

Browse files
committed
temp
1 parent bc36a7d commit c6fb9c5

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

python/pyspark/sql/connect/types.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ def pyspark_types_to_proto_types(data_type: DataType) -> pb2.DataType:
129129
ret = pb2.DataType()
130130
if isinstance(data_type, NullType):
131131
ret.null.CopyFrom(pb2.DataType.NULL())
132+
elif isinstance(data_type, CharType):
133+
ret.char.length = data_type.length
134+
elif isinstance(data_type, VarcharType):
135+
ret.varchar.length = data_type.length
132136
elif isinstance(data_type, StringType):
133137
ret.string.collation = data_type.collation
134138
elif isinstance(data_type, BooleanType):

python/pyspark/sql/tests/test_udf.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,36 @@ def my_udf(input_val):
14021402
result_type = df_result.schema["result"].dataType
14031403
self.assertEqual(result_type, StringType("fr"))
14041404

1405+
def test_udf_with_char_varchar_return_type(self):
1406+
char_type = "char(10)"
1407+
varchar_type = "varchar(8)"
1408+
array_with_char_type = "array<char(5)>"
1409+
array_with_varchar_type = "array<varchar(12)>"
1410+
map_type = f"map<{char_type}, {varchar_type}>"
1411+
struct_type = f"struct<f1: {char_type}, f2: {varchar_type}>"
1412+
1413+
return_types = [
1414+
char_type,
1415+
varchar_type,
1416+
array_with_char_type,
1417+
array_with_varchar_type,
1418+
map_type,
1419+
struct_type,
1420+
f"struct<f1: {array_with_char_type}, f2: {array_with_varchar_type}, f3: {map_type}>",
1421+
f"map<{array_with_char_type}, {array_with_varchar_type}>",
1422+
f"array<{struct_type}>"
1423+
]
1424+
1425+
for return_type in return_types:
1426+
with self.assertRaisesRegex(
1427+
AnalysisException,
1428+
"(Please use a different output data type for your UDF or DataFrame|"
1429+
"Invalid return type with Arrow-optimized Python UDF)"
1430+
):
1431+
@udf(return_type)
1432+
def my_udf():
1433+
return return_type
1434+
14051435

14061436
class UDFTests(BaseUDFTestsMixin, ReusedSQLTestCase):
14071437
@classmethod

0 commit comments

Comments
 (0)