Skip to content

Commit cc93546

Browse files
committed
SNOW-26262: create SnowflakeDateTimeFormat object once per column and not every single data for SnowSQL
1 parent 9622ebc commit cc93546

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

converter_snowsql.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from logging import getLogger
1010

1111
from .compat import TO_UNICODE
12+
from .constants import is_timestamp_type_name
1213
from .converter import (SnowflakeConverter, ZERO_EPOCH)
1314
from .sfbinaryformat import (binary_to_python, SnowflakeBinaryFormat)
1415
from .sfdatetime import (SnowflakeDateTimeFormat, SnowflakeDateTime)
@@ -17,12 +18,8 @@
1718

1819

1920
def _format_sftimestamp(fmt, value, franction_of_nanoseconds):
20-
if fmt:
21-
return SnowflakeDateTimeFormat(fmt).format(
22-
SnowflakeDateTime(
23-
value, nanosecond=franction_of_nanoseconds))
24-
return TO_UNICODE(SnowflakeDateTime(
25-
value, nanosecond=franction_of_nanoseconds))
21+
sf_datetime = SnowflakeDateTime(value, nanosecond=franction_of_nanoseconds)
22+
return fmt.format(sf_datetime) if fmt else TO_UNICODE(sf_datetime)
2623

2724

2825
class SnowflakeConverterSnowSQL(SnowflakeConverter):
@@ -60,8 +57,13 @@ def _get_format(self, type_name):
6057
#
6158
def to_python_method(self, type_name, row_type):
6259
try:
60+
fmt = None
61+
if is_timestamp_type_name(type_name):
62+
fmt = SnowflakeDateTimeFormat(self._get_format(type_name))
63+
elif type_name == u'BINARY':
64+
fmt = SnowflakeBinaryFormat(self._get_format(type_name))
6365
return getattr(self, u'_{type_name}_to_python'.format(
64-
type_name=type_name)), self._get_format(type_name)
66+
type_name=type_name)), fmt
6567
except KeyError:
6668
# no type is defined, pass through it
6769
return self._TEXT_to_python, None
@@ -88,8 +90,7 @@ def _BINARY_to_python(self, value, _, fmt):
8890
"""
8991
BINARY to a string formatted by BINARY_OUTPUT_FORMAT
9092
"""
91-
bytes_value = binary_to_python(value)
92-
return SnowflakeBinaryFormat(fmt).format(bytes_value)
93+
return fmt.format(binary_to_python(value))
9394

9495
def _DATE_to_python(self, value, _, fmt):
9596
"""
@@ -100,7 +101,7 @@ def _DATE_to_python(self, value, _, fmt):
100101
try:
101102
t = ZERO_EPOCH + timedelta(seconds=int(value) * (24 * 60 * 60))
102103
if fmt:
103-
return SnowflakeDateTimeFormat(fmt).format(t)
104+
return fmt.format(t)
104105
return TO_UNICODE(date(t.year, t.month, t.day))
105106
except OverflowError:
106107
self.logger.debug(
@@ -109,7 +110,7 @@ def _DATE_to_python(self, value, _, fmt):
109110
value)
110111
t = time.gmtime(value)
111112
if fmt:
112-
return SnowflakeDateTimeFormat(fmt).format(
113+
return fmt.format(
113114
SnowflakeDateTime(t, nanosecond=0)
114115
)
115116
return u'{year:d}-{month:02d}-{day:02d}'.format(

0 commit comments

Comments
 (0)