Skip to content

Commit

Permalink
support non-ascii character in column names
Browse files Browse the repository at this point in the history
  • Loading branch information
Davies Liu committed Jul 1, 2015
1 parent 31b4a3d commit 867754a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
8 changes: 8 additions & 0 deletions python/pyspark/sql/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,14 @@ def test_access_column(self):
self.assertRaises(IndexError, lambda: df["bad_key"])
self.assertRaises(TypeError, lambda: df[{}])

def test_column_name_with_non_ascii(self):
df = self.sqlCtx.createDataFrame([(1,)], ["数量"])
self.assertEqual(StructType([StructField("数量", LongType(), True)]), df.schema)
self.assertEqual("DataFrame[数量: bigint]", str(df))
self.assertEqual([("数量", 'bigint')], df.dtypes)
self.assertEqual(1, df.select("数量").first()[0])
self.assertEqual(1, df.select(df["数量"]).first()[0])

def test_access_nested_types(self):
df = self.sc.parallelize([Row(l=[1], r=Row(a=1, b="b"), d={"k": "v"})]).toDF()
self.assertEqual(1, df.select(df.l[0]).first()[0])
Expand Down
2 changes: 2 additions & 0 deletions python/pyspark/sql/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ def __init__(self, name, dataType, nullable=True, metadata=None):
False
"""
assert isinstance(dataType, DataType), "dataType should be DataType"
if not isinstance(name, str):
name = name.encode('utf-8')
self.name = name
self.dataType = dataType
self.nullable = nullable
Expand Down
6 changes: 3 additions & 3 deletions python/pyspark/sql/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def deco(*a, **kw):
try:
return f(*a, **kw)
except py4j.protocol.Py4JJavaError as e:
cls, msg = e.java_exception.toString().split(': ', 1)
if cls == 'org.apache.spark.sql.AnalysisException':
raise AnalysisException(msg)
s = e.java_exception.toString()
if s.startswith('org.apache.spark.sql.AnalysisException: '):
raise AnalysisException(s.split(': ', 1)[1])
raise
return deco

Expand Down

0 comments on commit 867754a

Please sign in to comment.