Github user JoshRosen commented on a diff in the pull request:
https://github.com/apache/spark/pull/4421#discussion_r24247234
--- Diff: python/pyspark/sql.py ---
@@ -1469,6 +1470,44 @@ def applySchema(self, rdd, schema):
df = self._ssql_ctx.applySchemaToPythonRDD(jrdd.rdd(),
schema.json())
return DataFrame(df, self)
+ def applyNames(self, nameString, plainRdd):
+ """
+ Builds a DataFrame from an RDD based on column names.
+
+ Assumes RDD contains iterables of equal length.
+ >>> unparsedStrings = sc.parallelize(["1, A1, true", "2, B2,
false", "3, C3, true", "4, D4, false"])
+ >>> input = unparsedStrings.map(lambda x: x.split(",")).map(lambda
x: [int(x[0]), x[1], bool(x[2])])
+ >>> df1 = sqlCtx.applyNames("a b c", input)
+ >>> df1.registerTempTable("df1")
+ >>> sqlCtx.sql("select a from df1").collect()
+ [Row(a=1), Row(a=2), Row(a=3), Row(a=4)]
+ >>> input2 = unparsedStrings.map(lambda x:
x.split(",")).map(lambda x: [int(x[0]), x[1], bool(x[2]), {"k":int(x[0]),
"v":2*int(x[0])}, x])
+ >>> df2 = sqlCtx.applyNames("a b c d e", input2)
+ >>> df2.registerTempTable("df2")
+ >>> sqlCtx.sql("select d['k']+d['v'] from df2").collect()
+ [Row(c0=3), Row(c0=6), Row(c0=9), Row(c0=12)]
+ >>> sqlCtx.sql("select b, e[1] from df2").collect()
+ [Row(b=u' A1', c1=u' A1'), Row(b=u' B2', c1=u' B2'), Row(b=u' C3',
c1=u' C3'), Row(b=u' D4', c1=u' D4')]
+ """
+ fieldNames = [f for f in re.split("( |\\\".*?\\\"|'.*?')",
nameString) if f.strip()]
+ reservedWords = set(map(string.lower,["ABS","ALL","AND",
"APPROXIMATE", "AS", "ASC", "AVG", "BETWEEN", "BY", \
--- End diff --
I can't really speak to this patch in general, since I don't know much
about this part of Spark SQL, but to avoid duplication it probably makes sense
to keep the list of reserved words in the JVM and fetch it into Python from
there.
---
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]