OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TableFunctionsFactory_node.py
Go to the documentation of this file.
1 __all__ = ['UdtfNode', 'ArgNode', 'PrimitiveNode', 'ComposedNode',
2  'AnnotationNode', 'TemplateNode']
3 
4 
5 import sys
6 from abc import abstractmethod
7 
8 import TableFunctionsFactory_transformers as transformers
9 import TableFunctionsFactory_util as util
10 
11 if sys.version_info > (3, 0):
12  from abc import ABC
13  from collections.abc import Iterable
14 else:
15  from abc import ABCMeta as ABC
16  from collections import Iterable
17 
18 
19 class Node(object):
20 
21  __metaclass__ = ABC
22 
23  @abstractmethod
24  def accept(self, visitor):
25  pass
26 
27  def get_parent(self, cls):
28  if isinstance(self, cls):
29  return self
30 
31  if self.parent is not None:
32  return self.parent.get_parent(cls)
33 
34  raise ValueError("could not find parent with given class %s" % (cls))
35 
36  def copy(self, *args):
37  other = self.__class__(*args)
38 
39  # copy parent and arg_pos
40  for attr in ['parent', 'arg_pos']:
41  if attr in self.__dict__:
42  setattr(other, attr, getattr(self, attr))
43 
44  return other
45 
46 
47 class PrintNode(object):
48  def __str__(self):
49  return self.accept(transformers.AstPrinter())
50 
51  def __repr__(self):
52  return str(self)
53 
54 
55 class IterableNode(Iterable):
56  pass
57 
58 
59 class UdtfNode(Node, IterableNode, PrintNode):
60 
61  def __init__(self, name, inputs, outputs, annotations, templates, sizer, line):
62  """
63  Parameters
64  ----------
65  name : str
66  inputs : list[ArgNode]
67  outputs : list[ArgNode]
68  annotations : Optional[List[AnnotationNode]]
69  templates : Optional[list[TemplateNode]]
70  sizer : Optional[str]
71  line: str
72  """
73  self.name = name
74  self.inputs = inputs
75  self.outputs = outputs
76  self.annotations = annotations
77  self.templates = templates
78  self.sizer = sizer
79  self.line = line
80 
81  def accept(self, visitor):
82  return visitor.visit_udtf_node(self)
83 
84  def __iter__(self):
85  for i in self.inputs:
86  yield i
87  for o in self.outputs:
88  yield o
89  for a in self.annotations:
90  yield a
91  if self.templates:
92  for t in self.templates:
93  yield t
94 
95 
97 
98  def __init__(self, type, annotations):
99  """
100  Parameters
101  ----------
102  type : TypeNode
103  annotations : List[AnnotationNode]
104  """
105  self.type = type
106  self.annotations = annotations
107  self.arg_pos = None
108  self.kind = None
109 
110  def accept(self, visitor):
111  return visitor.visit_arg_node(self)
112 
113  def __iter__(self):
114  yield self.type
115  for a in self.annotations:
116  yield a
117 
118  def get_annotation(self, key, default=None):
119  for a in self.annotations:
120  if a.key == key:
121  return a.value
122  return default
123 
124  def set_annotation(self, key, value):
125  found = False
126  for i, a in enumerate(self.annotations):
127  if a.key == key:
128  assert not found, (i, a) # annotations with the same key not supported
129  self.annotations[i] = AnnotationNode(key, value)
130  found = True
131  if not found:
132  self.annotations.append(AnnotationNode(key, value))
133 
134 
135 class TypeNode(Node):
136  def is_array(self):
137  return self.type == "Array"
138 
139  def is_column_any(self):
140  return self.is_column() or self.is_column_list()
141 
142  def is_column(self):
143  return self.type == "Column"
144 
145  def is_column_list(self):
146  return self.type == "ColumnList"
147 
148  def is_cursor(self):
149  return self.type == "Cursor"
150 
152  t = self.type
153  return util.translate_map.get(t, t) in util.OutputBufferSizeTypes
154 
156  return self.type == 'TextEncodingDict'
157 
159  return self.type == 'ArrayTextEncodingDict'
160 
161  def is_integer_scalar(self):
162  return self.type.lower() in ('int8_t', 'int16_t', 'int32_t', 'int64_t')
163 
164  def is_float_scalar(self):
165  return self.type.lower() in ('float', 'double')
166 
167  def is_boolean_scalar(self):
168  return self.type.lower() == 'bool'
169 
170  def is_string_scalar(self):
171  # we only support 'TextEncodingNone' string scalars atm
172  return self.type == "TextEncodingNone"
173 
174  def is_scalar(self):
175  return self.is_integer_scalar() or self.is_float_scalar() or self.is_boolean_scalar() or self.is_string_scalar()
176 
177 
179 
180  def __init__(self, type):
181  """
182  Parameters
183  ----------
184  type : str
185  """
186  self.type = type
187 
188  def accept(self, visitor):
189  return visitor.visit_primitive_node(self)
190 
191  def __eq__(self, other):
192  if isinstance(other, PrimitiveNode):
193  return self.type == other.type
194  return False
195 
196 
198 
199  def __init__(self, type, inner):
200  """
201  Parameters
202  ----------
203  type : str
204  inner : list[TypeNode]
205  """
206  self.type = type
207  self.inner = inner
208 
209  def accept(self, visitor):
210  return visitor.visit_composed_node(self)
211 
212  def cursor_length(self):
213  assert self.is_cursor()
214  return len(self.inner)
215 
216  def __iter__(self):
217  for i in self.inner:
218  yield i
219 
221  return False
222 
224  return self.is_array() and self.inner[0].is_text_encoding_dict()
225 
227  return self.is_column() and self.inner[0].is_text_encoding_dict()
228 
230  return self.is_column_list() and self.inner[0].is_text_encoding_dict()
231 
233  return self.is_column() and self.inner[0].is_array_text_encoding_dict()
234 
236  return self.inner[0].is_text_encoding_dict() or self.inner[0].is_array_text_encoding_dict()
237 
238  def is_column_of(self, T):
239  return self.is_column() and self.inner[0] == T
240 
241  def is_column_list_of(self, T):
242  return self.is_column_list() and self.inner[0] == T
243 
244 
246 
247  def __init__(self, key, value):
248  """
249  Parameters
250  ----------
251  key : str
252  value : {str, list}
253  """
254  self.key = key
255  self.value = value
256 
257  def accept(self, visitor):
258  return visitor.visit_annotation_node(self)
259 
260 
262 
263  def __init__(self, key, types):
264  """
265  Parameters
266  ----------
267  key : str
268  types : tuple[str]
269  """
270  self.key = key
271  self.types = types
272 
273  def accept(self, visitor):
274  return visitor.visit_template_node(self)