Skip to content

Commit

Permalink
support string in cast()
Browse files Browse the repository at this point in the history
  • Loading branch information
Davies Liu committed Feb 3, 2015
1 parent 83c92fe commit 467332c
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions python/pyspark/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2565,6 +2565,8 @@ def alias(self, alias):
def cast(self, dataType):
""" Convert the column into type `dataType`
>>> df.select(df.age.cast("string").As('ages')).collect()
[Row(ages=u'2'), Row(ages=u'5')]
>>> df.select(df.age.cast(StringType()).As('ages')).collect()
[Row(ages=u'2'), Row(ages=u'5')]
"""
Expand All @@ -2573,8 +2575,12 @@ def cast(self, dataType):
ssql_ctx = sc._jvm.SQLContext(sc._jsc.sc())
else:
ssql_ctx = self.sql_ctx._ssql_ctx
jdt = ssql_ctx.parseDataType(dataType.json())
return Column(self._jc.cast(jdt), self.sql_ctx)
if isinstance(dataType, basestring):
jc = self._jc.cast(dataType)
elif isinstance(dataType, DataType):
jdt = ssql_ctx.parseDataType(dataType.json())
jc = self._jc.cast(jdt)
return Column(jc, self.sql_ctx)


def _aggregate_func(name, doc=""):
Expand All @@ -2593,9 +2599,9 @@ class Dsl(object):
A collections of builtin aggregators
"""
DSLS = {
'lit': 'Creates a [[Column]] of literal value.',
'col': 'Returns a [[Column]] based on the given column name.',
'column': 'Returns a [[Column]] based on the given column name.',
'lit': 'Creates a :class:`Column` of literal value.',
'col': 'Returns a :class:`Column` based on the given column name.',
'column': 'Returns a :class:`Column` based on the given column name.',
'upper': 'Converts a string expression to upper case.',
'lower': 'Converts a string expression to upper case.',
'sqrt': 'Computes the square root of the specified float value.',
Expand Down

0 comments on commit 467332c

Please sign in to comment.