GitHub user HyukjinKwon opened a pull request:
https://github.com/apache/spark/pull/18618
[SPARK-20090][PYTHON] Add StructType.fieldNames in PySpark
## What changes were proposed in this pull request?
This PR proposes `StructType.fieldNames` that returns immutable tuple
rather than a mutable (undocumented) `StructType.names`.
There are two points here:
- API consistency with Scala/Java
- Provide a safe way to get the field names. Manipulating these might
cause unexpected behaviour as below:
```python
from pyspark.sql.types import *
struct = StructType([StructField("f1", StringType(), True)])
names = struct.names
del names[0]
spark.createDataFrame([{"f1": 1}], struct).show()
```
```
...
java.lang.IllegalStateException: Input row doesn't have expected number
of values required by the schema. 1 fields are required while 0 values are
provided.
at
org.apache.spark.sql.execution.python.EvaluatePython$.fromJava(EvaluatePython.scala:138)
at
org.apache.spark.sql.SparkSession$$anonfun$6.apply(SparkSession.scala:741)
at
org.apache.spark.sql.SparkSession$$anonfun$6.apply(SparkSession.scala:741)
...
```
## How was this patch tested?
Added tests in `python/pyspark/sql/tests.py`.
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/HyukjinKwon/spark SPARK-20090
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/spark/pull/18618.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #18618
----
commit efe113fdb8f2ec7ba57766b0b87a6c8df9728291
Author: hyukjinkwon <[email protected]>
Date: 2017-07-13T02:06:44Z
Add StructType.fieldNames in PySpark
----
---
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]