richardc-db commented on code in PR #47253:
URL: https://github.com/apache/spark/pull/47253#discussion_r1676341542


##########
python/pyspark/sql/types.py:
##########
@@ -194,16 +194,7 @@ def fromDDL(cls, ddl: str) -> "DataType":
         >>> DataType.fromDDL("b: string, a: int")
         StructType([StructField('b', StringType(), True), StructField('a', 
IntegerType(), True)])
         """
-        from pyspark.sql import SparkSession
-        from pyspark.sql.functions import udf
-
-        # Intentionally uses SparkSession so one implementation can be shared 
with/without
-        # Spark Connect.
-        schema = (
-            SparkSession.active().range(0).select(udf(lambda x: x, 
returnType=ddl)("id")).schema
-        )
-        assert len(schema) == 1
-        return schema[0].dataType
+        return _parse_datatype_string(ddl)

Review Comment:
   yeah makes sense. The function comment mentions that this was added for 
Spark 4.0.0 (which won't be GA until 2025 IIUC?) so I thought the risk of a 
breaking change for a public API would be a bit lower.
   
   The error message without changing `fromDDL` for something like "a int v 
variant" would be
   ```
   org.apache.spark.sql.AnalysisException: 
[DATATYPE_MISMATCH.UNSUPPORTED_UDF_OUTPUT_TYPE] Cannot resolve "<lambda>(id)" 
due to data type mismatch: UDFs do not support 'STRUCT<a: INT, v: VARIANT>' as 
an output data type. SQLSTATE: 42K09
   ```
   which is pretty confusing for `fromDDL` in my opinion (i.e. why does 
`fromDDL` need to call a UDF in the first place?)
   
   It seems that the UDF `returnType` implementation directly calls 
`_parse_datatype_string` 
[here](https://github.com/apache/spark/blob/master/python/pyspark/sql/udf.py#L370).
   
   I added an additional test to test that the old udf behavior matches the new 
behavior
   



-- 
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: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to