Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions python/pyspark/sql/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ def mean(self, *cols):
>>> df.groupBy().mean('age').collect()
[Row(AVG(age#0)=3.5)]
>>> df3.groupBy().mean('age', 'height').collect()
[Row(AVG(age#4)=3.5, AVG(height#5)=82.5)]
[Row(AVG(age#4L)=3.5, AVG(height#5L)=82.5)]
"""

@df_varargs_api
Expand All @@ -791,7 +791,7 @@ def avg(self, *cols):
>>> df.groupBy().avg('age').collect()
[Row(AVG(age#0)=3.5)]
>>> df3.groupBy().avg('age', 'height').collect()
[Row(AVG(age#4)=3.5, AVG(height#5)=82.5)]
[Row(AVG(age#4L)=3.5, AVG(height#5L)=82.5)]
"""

@df_varargs_api
Expand All @@ -802,7 +802,7 @@ def max(self, *cols):
>>> df.groupBy().max('age').collect()
[Row(MAX(age#0)=5)]
>>> df3.groupBy().max('age', 'height').collect()
[Row(MAX(age#4)=5, MAX(height#5)=85)]
[Row(MAX(age#4L)=5, MAX(height#5L)=85)]
"""

@df_varargs_api
Expand All @@ -813,7 +813,7 @@ def min(self, *cols):
>>> df.groupBy().min('age').collect()
[Row(MIN(age#0)=2)]
>>> df3.groupBy().min('age', 'height').collect()
[Row(MIN(age#4)=2, MIN(height#5)=80)]
[Row(MIN(age#4L)=2, MIN(height#5L)=80)]
"""

@df_varargs_api
Expand All @@ -824,7 +824,7 @@ def sum(self, *cols):
>>> df.groupBy().sum('age').collect()
[Row(SUM(age#0)=7)]
>>> df3.groupBy().sum('age', 'height').collect()
[Row(SUM(age#4)=7, SUM(height#5)=165)]
[Row(SUM(age#4L)=7, SUM(height#5L)=165)]
"""


Expand Down Expand Up @@ -1028,7 +1028,9 @@ def _test():
sc = SparkContext('local[4]', 'PythonTest')
globs['sc'] = sc
globs['sqlCtx'] = SQLContext(sc)
globs['df'] = sc.parallelize([Row(name='Alice', age=2), Row(name='Bob', age=5)]).toDF()
globs['df'] = sc.parallelize([(2, 'Alice'), (5, 'Bob')])\
.toDF(StructType([StructField('age', IntegerType()),
StructField('name', StringType())]))
globs['df2'] = sc.parallelize([Row(name='Tom', height=80), Row(name='Bob', height=85)]).toDF()
globs['df3'] = sc.parallelize([Row(name='Alice', age=2, height=80),
Row(name='Bob', age=5, height=85)]).toDF()
Expand Down
22 changes: 21 additions & 1 deletion python/pyspark/sql/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

from pyspark.sql import SQLContext, HiveContext, Column
from pyspark.sql.types import IntegerType, Row, ArrayType, StructType, StructField, \
UserDefinedType, DoubleType, LongType, StringType
UserDefinedType, DoubleType, LongType, StringType, _infer_type
from pyspark.tests import ReusedPySparkTestCase


Expand Down Expand Up @@ -322,6 +322,26 @@ def test_help_command(self):
pydoc.render_doc(df.foo)
pydoc.render_doc(df.take(1))

def test_infer_long_type(self):
longrow = [Row(f1='a', f2=100000000000000)]
df = self.sc.parallelize(longrow).toDF()
self.assertEqual(df.schema.fields[1].dataType, LongType())

# this saving as Parquet caused issues as well.
output_dir = os.path.join(self.tempdir.name, "infer_long_type")
df.saveAsParquetFile(output_dir)
df1 = self.sqlCtx.parquetFile(output_dir)
self.assertEquals('a', df1.first().f1)
self.assertEquals(100000000000000, df1.first().f2)

self.assertEqual(_infer_type(1), LongType())
self.assertEqual(_infer_type(2**10), LongType())
self.assertEqual(_infer_type(2**20), LongType())
self.assertEqual(_infer_type(2**31 - 1), LongType())
self.assertEqual(_infer_type(2**31), LongType())
self.assertEqual(_infer_type(2**61), LongType())
self.assertEqual(_infer_type(2**71), LongType())


class HiveContextSQLTests(ReusedPySparkTestCase):

Expand Down
8 changes: 4 additions & 4 deletions python/pyspark/sql/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ def _parse_datatype_json_value(json_value):
_type_mappings = {
type(None): NullType,
bool: BooleanType,
int: IntegerType,
int: LongType,
long: LongType,
float: DoubleType,
str: StringType,
Expand Down Expand Up @@ -933,11 +933,11 @@ def _infer_schema_type(obj, dataType):
>>> schema = _parse_schema_abstract("a b c d")
>>> row = (1, 1.0, "str", datetime.date(2014, 10, 10))
>>> _infer_schema_type(row, schema)
StructType...IntegerType...DoubleType...StringType...DateType...
StructType...LongType...DoubleType...StringType...DateType...
>>> row = [[1], {"key": (1, 2.0)}]
>>> schema = _parse_schema_abstract("a[] b{c d}")
>>> _infer_schema_type(row, schema)
StructType...a,ArrayType...b,MapType(StringType,...c,IntegerType...
StructType...a,ArrayType...b,MapType(StringType,...c,LongType...
"""
if dataType is None:
return _infer_type(obj)
Expand Down Expand Up @@ -992,7 +992,7 @@ def _verify_type(obj, dataType):

>>> _verify_type(None, StructType([]))
>>> _verify_type("", StringType())
>>> _verify_type(0, IntegerType())
>>> _verify_type(0, LongType())
>>> _verify_type(range(3), ArrayType(ShortType()))
>>> _verify_type(set(), ArrayType(StringType())) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,7 @@ class SQLContext(@transient val sparkContext: SparkContext)
def needsConversion(dataType: DataType): Boolean = dataType match {
case ByteType => true
case ShortType => true
case LongType => true
case FloatType => true
case DateType => true
case TimestampType => true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ object EvaluatePython {
case (c: Int, ShortType) => c.toShort
case (c: Long, ShortType) => c.toShort
case (c: Long, IntegerType) => c.toInt
case (c: Int, LongType) => c.toLong
case (c: Double, FloatType) => c.toFloat
case (c, StringType) if !c.isInstanceOf[String] => c.toString

Expand Down