Github user HyukjinKwon commented on a diff in the pull request:
https://github.com/apache/spark/pull/18498#discussion_r126284016
--- Diff: python/pyspark/sql/functions.py ---
@@ -1856,7 +1860,9 @@ def from_json(col, schema, options={}):
"""
sc = SparkContext._active_spark_context
- jc = sc._jvm.functions.from_json(_to_java_column(col), schema.json(),
options)
+ if isinstance(schema, DataType):
--- End diff --
Hm.. I am not very sure on this actually. For the current state, yes. It
should work only for `StructType` and `ArrayType` but `from_json` in
`functions.scala` takes both `StructType` and `DataType`.
This will give a proper message from JVM side. This gets parsed correctly
as below:
```python
from pyspark.sql.types import *
from pyspark.sql.functions import *
df = spark.createDataFrame([(1,)], ("value",))
df.select(from_json(df.value, IntegerType()).alias("json")).collect()
```
throws an `pyspark.sql.utils.AnalysisException`:
```
...
cannot resolve 'jsontostructs(`value`)' due to data type mismatch: Input
schema int must be a struct or an array of structs.
...
```
For the context, we started to support `StructType` only from 2.1 but we
decided to support `ArrayType` later. The method signature was decided as
`DataType` just in case for more types in the future and we had to keep
`StructType` one for binary compatibility.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]