OmniSciDB  c1a53651b2
 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  'LINESTRING': 'str',
51  'POLYGON': 'str',
52  'MULTIPOLYGON': 'str',
53  'TINYINT': 'int',
54  'GEOMETRY': 'str',
55  'GEOGRAPHY': 'str',
56 }
57 _thrift_types_to_values = T.TDatumType._NAMES_TO_VALUES
58 _thrift_values_to_types = T.TDatumType._VALUES_TO_NAMES
59 _thrift_encodings_to_values = T.TEncodingType._NAMES_TO_VALUES
60 _thrift_values_to_encodings = T.TEncodingType._VALUES_TO_NAMES
61 
62 
63 def _format_result_timestamp(desc, arr):
64 
65  return [
66  None
67  if v is None
68  else datetime_in_precisions(v, desc.col_type.precision)
69  for v in arr
70  ]
71 
72 
74 
75  base = datetime.datetime(1970, 1, 1)
76  return [
77  None if v is None else (base + datetime.timedelta(seconds=v)).date()
78  for v in arr
79  ]
80 
81 
83 
84  return [None if v is None else seconds_to_time(v) for v in arr]
85 
86 
87 def _extract_col_vals(desc, val):
88 
89  typename = T.TDatumType._VALUES_TO_NAMES[desc.col_type.type]
90  nulls = val.nulls
91 
92  # arr_col has multiple levels to parse, not accounted for in original code
93  # https://github.com/omnisci/pymapd/issues/68
94  if hasattr(val.data, 'arr_col') and val.data.arr_col:
95  vals = [
96  None if null else getattr(v.data, _typeattr[typename] + '_col')
97  for null, v in zip(nulls, val.data.arr_col)
98  ]
99 
100  if typename == 'TIMESTAMP':
101  vals = [_format_result_timestamp(desc, v) for v in vals]
102  elif typename == 'DATE':
103  vals = [_format_result_date(v) for v in vals]
104  elif typename == 'TIME':
105  vals = [_format_result_time(v) for v in vals]
106 
107  # else clause original code path
108  else:
109  vals = getattr(val.data, _typeattr[typename] + '_col')
110  vals = [None if null else v for null, v in zip(nulls, vals)]
111 
112  if typename == 'TIMESTAMP':
113  vals = _format_result_timestamp(desc, vals)
114  elif typename == 'DATE':
115  vals = _format_result_date(vals)
116  elif typename == 'TIME':
117  vals = _format_result_time(vals)
118 
119  return vals
120 
121 
122 def _extract_description(row_desc):
123  """
124  Return a tuple of (name, type_code, display_size, internal_size,
125  precision, scale, null_ok)
126 
127  https://www.python.org/dev/peps/pep-0249/#description
128  """
129  return [
130  Description(
131  col.col_name,
132  col.col_type.type,
133  None,
134  None,
135  None,
136  None,
137  col.col_type.nullable,
138  )
139  for col in row_desc
140  ]
141 
142 
144  # For Connection.get_table_details
145  return [
147  x.col_name,
148  _thrift_values_to_types[x.col_type.type],
149  x.col_type.nullable,
150  x.col_type.precision,
151  x.col_type.scale,
152  x.col_type.comp_param,
153  _thrift_values_to_encodings[x.col_type.encoding],
154  x.col_type.is_array,
155  )
156  for x in row_desc
157  ]
158 
159 
160 def _bind_parameters(operation, parameters):
161  return (
162  text(operation)
163  .bindparams(**parameters)
164  .compile(compile_kwargs={"literal_binds": True})
165  )
def _format_result_date
Definition: _parsers.py:73
def _extract_column_details
Definition: _parsers.py:143
tuple Description
Definition: _parsers.py:11
def _format_result_time
Definition: _parsers.py:82
def _extract_col_vals
Definition: _parsers.py:87
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:160
def _format_result_timestamp
Definition: _parsers.py:63
def _extract_description
Definition: _parsers.py:122