OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
_parsers.py
Go to the documentation of this file.
1 """
2 Utility methods for parsing data returned from HeavyDB
3 """
4 
5 import datetime
6 from collections import namedtuple
7 from sqlalchemy import text
8 import heavydb.common.ttypes as T
9 from ._utils import seconds_to_time, datetime_in_precisions
10 
11 Description = namedtuple(
12  "Description",
13  [
14  "name",
15  "type_code",
16  "display_size",
17  "internal_size",
18  "precision",
19  "scale",
20  "null_ok",
21  ],
22 )
23 ColumnDetails = namedtuple(
24  "ColumnDetails",
25  [
26  "name",
27  "type",
28  "nullable",
29  "precision",
30  "scale",
31  "comp_param",
32  "encoding",
33  "is_array",
34  ],
35 )
36 
37 _typeattr = {
38  'SMALLINT': 'int',
39  'INT': 'int',
40  'BIGINT': 'int',
41  'TIME': 'int',
42  'TIMESTAMP': 'int',
43  'DATE': 'int',
44  'BOOL': 'int',
45  'FLOAT': 'real',
46  'DECIMAL': 'real',
47  'DOUBLE': 'real',
48  'STR': 'str',
49  'POINT': 'str',
50  'MULTIPOINT': 'str',
51  'LINESTRING': 'str',
52  'MULTILINESTRING': 'str',
53  'POLYGON': 'str',
54  'MULTIPOLYGON': 'str',
55  'TINYINT': 'int',
56  'GEOMETRY': 'str',
57  'GEOGRAPHY': 'str',
58 }
59 _thrift_types_to_values = T.TDatumType._NAMES_TO_VALUES
60 _thrift_values_to_types = T.TDatumType._VALUES_TO_NAMES
61 _thrift_encodings_to_values = T.TEncodingType._NAMES_TO_VALUES
62 _thrift_values_to_encodings = T.TEncodingType._VALUES_TO_NAMES
63 
64 
65 def _format_result_timestamp(desc, arr):
66 
67  return [
68  None
69  if v is None
70  else datetime_in_precisions(v, desc.col_type.precision)
71  for v in arr
72  ]
73 
74 
76 
77  base = datetime.datetime(1970, 1, 1)
78  return [
79  None if v is None else (base + datetime.timedelta(seconds=v)).date()
80  for v in arr
81  ]
82 
83 
85 
86  return [None if v is None else seconds_to_time(v) for v in arr]
87 
88 
89 def _extract_col_vals(desc, val):
90 
91  typename = T.TDatumType._VALUES_TO_NAMES[desc.col_type.type]
92  nulls = val.nulls
93 
94  # arr_col has multiple levels to parse, not accounted for in original code
95  # https://github.com/omnisci/pymapd/issues/68
96  if hasattr(val.data, 'arr_col') and val.data.arr_col:
97  vals = [
98  None if null else getattr(v.data, _typeattr[typename] + '_col')
99  for null, v in zip(nulls, val.data.arr_col)
100  ]
101 
102  if typename == 'TIMESTAMP':
103  vals = [_format_result_timestamp(desc, v) for v in vals]
104  elif typename == 'DATE':
105  vals = [_format_result_date(v) for v in vals]
106  elif typename == 'TIME':
107  vals = [_format_result_time(v) for v in vals]
108 
109  # else clause original code path
110  else:
111  vals = getattr(val.data, _typeattr[typename] + '_col')
112  vals = [None if null else v for null, v in zip(nulls, vals)]
113 
114  if typename == 'TIMESTAMP':
115  vals = _format_result_timestamp(desc, vals)
116  elif typename == 'DATE':
117  vals = _format_result_date(vals)
118  elif typename == 'TIME':
119  vals = _format_result_time(vals)
120 
121  return vals
122 
123 
124 def _extract_description(row_desc):
125  """
126  Return a tuple of (name, type_code, display_size, internal_size,
127  precision, scale, null_ok)
128 
129  https://www.python.org/dev/peps/pep-0249/#description
130  """
131  return [
132  Description(
133  col.col_name,
134  col.col_type.type,
135  None,
136  None,
137  None,
138  None,
139  col.col_type.nullable,
140  )
141  for col in row_desc
142  ]
143 
144 
146  # For Connection.get_table_details
147  return [
149  x.col_name,
150  _thrift_values_to_types[x.col_type.type],
151  x.col_type.nullable,
152  x.col_type.precision,
153  x.col_type.scale,
154  x.col_type.comp_param,
155  _thrift_values_to_encodings[x.col_type.encoding],
156  x.col_type.is_array,
157  )
158  for x in row_desc
159  ]
160 
161 
162 def _bind_parameters(operation, parameters):
163  return (
164  text(operation)
165  .bindparams(**parameters)
166  .compile(compile_kwargs={"literal_binds": True})
167  )
def _format_result_date
Definition: _parsers.py:75
def _extract_column_details
Definition: _parsers.py:145
tuple Description
Definition: _parsers.py:11
def _format_result_time
Definition: _parsers.py:84
def _extract_col_vals
Definition: _parsers.py:89
def seconds_to_time
Definition: _utils.py:5
tuple ColumnDetails
Definition: _parsers.py:23
def datetime_in_precisions
Definition: _utils.py:19
def _bind_parameters
Definition: _parsers.py:162
def _format_result_timestamp
Definition: _parsers.py:65
def _extract_description
Definition: _parsers.py:124