Skip to content

Commit

Permalink
use int for timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
Davies Liu committed Jun 11, 2015
1 parent 10aa7ca commit 99d9d9c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
6 changes: 3 additions & 3 deletions python/pyspark/sql/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,10 +612,10 @@ def test_time_with_timezone(self):
utcnow = datetime.datetime.fromtimestamp(ts, utc)
df = self.sqlCtx.createDataFrame([(now, utcnow)])
now1, utcnow1 = df.first()
# Spark SQL does not support microsecond, the error should be
# Pyrolite does not support microsecond, the error should be
# less than 1 millisecond
self.assertTrue(now1 - now < datetime.timedelta(0.001))
self.assertTrue(utcnow1 - now < datetime.timedelta(0.001))
self.assertTrue(now - now1 < datetime.timedelta(0.001))
self.assertTrue(now - utcnow1 < datetime.timedelta(0.001))

def test_dropna(self):
schema = StructType([
Expand Down
20 changes: 5 additions & 15 deletions python/pyspark/sql/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,6 @@ def _need_python_to_sql_conversion(dataType):
elif isinstance(dataType, MapType):
return _need_python_to_sql_conversion(dataType.keyType) or \
_need_python_to_sql_conversion(dataType.valueType)
elif isinstance(dataType, TimestampType):
return True
elif isinstance(dataType, UserDefinedType):
return True
elif isinstance(dataType, TimestampType):
Expand Down Expand Up @@ -711,25 +709,17 @@ def converter(obj):
value_converter = _python_to_sql_converter(dataType.valueType)
return lambda m: dict([(key_converter(k), value_converter(v)) for k, v in m.items()])

elif isinstance(dataType, TimestampType):

def to_posix_timstamp(dt):
if dt.tzinfo is None:
return time.mktime(dt.timetuple()) + dt.microsecond / 1e6
else:
return calendar.timegm(dt.utctimetuple()) + dt.microsecond / 1e6
return to_posix_timstamp

elif isinstance(dataType, UserDefinedType):
return lambda obj: dataType.serialize(obj)

elif isinstance(dataType, TimestampType):

def to_posix_timstamp(dt):
if dt.tzinfo is None:
return int(time.mktime(dt.timetuple()) * 1e7 + dt.microsecond * 10)
else:
return int(calendar.timegm(dt.utctimetuple()) * 1e7 + dt.microsecond * 10)
seconds = (calendar.timegm(dt.utctimetuple()) if dt.tzinfo
else time.mktime(dt.timetuple()))
return int(seconds * 1e7 + dt.microsecond * 10)
return to_posix_timstamp

else:
raise ValueError("Unexpected type %r" % dataType)

Expand Down

0 comments on commit 99d9d9c

Please sign in to comment.