def __init__(self, tree: Node): #do query for element in tree.get_children(): if element.get_type() == TK.TOK_FROM: (self._index, self._type) = parse_tok_table_name(element.get_child(0)) if element.get_children_count() == 2: self.route = parse_value(element.get_child(1)) if element.get_type() == TK.TOK_SELECT: self.selexpr = parse_tok_selexpr(element) if element.get_type() == TK.TOK_WHERE: self.query_body = QueryBody(element.get_child(0)) if element.get_type() == TK.TOK_LIMIT: (self._from, self._size) = parse_tok_limit(element) if element.get_type() == TK.TOK_ORDERBY: self.sorts = parse_tok_sorts(element) #do aggregations for element in tree.get_children(): if element.get_type() == TK.TOK_GROUPBY: agg_size = -1 if hasattr(self, '_size'): agg_size = self._size self.groupby = AggBuckets(element, agg_size)
def __init__(self, tree: Node): self.compare = tree.get_value().lower() if tree.get_child(0).get_type() == TK.TOK_EXPRESSION_LEFT: self.left_values = parse_left_values( tree.get_child(0).get_children()) if tree.get_child(1).get_type() == TK.TOK_EXPRESSION_RIGHT: self.right_value = parse_value(tree.get_child(1).get_child(0))
def __init__(self, tree: Node): if tree.get_child(0).get_type() == TK.TOK_FUNCTION: self.selexpr = FunctionXpr(tree.get_child(0)) else: self.selexpr = parse_value(tree.get_child(0)) if tree.get_children_count() == 2: self.alias = parse_value(tree.get_child(1))
def parse_table_name(tree: Node): if tree.get_type() == TK.TOK_DOT: _index = parse_value(tree.get_child(0)) _type = parse_value(tree.get_child(1)) elif tree.get_type() == TK.TOK_VALUE: _index = tree.get_value() _type = 'base' return (_index,_type)
def parse_value(tree: Node) -> str: if tree.get_type() == TK.TOK_DOT: retval = parse_value(tree.get_child(0)) retval += '.' retval += parse_value(tree.get_child(1)) return retval elif tree.get_type() == TK.TOK_VALUE: return tree.get_value() else: pass
def parse_tok_limit(tree: Node): _from = 0 _size = 0 if tree.get_children_count() == 2: _from = tree.get_child(0).get_value() _size = tree.get_child(1).get_value() else: _from = 0 _size = tree.get_child(0).get_value() return (_from, _size)
def parse_kv(tree: Node): right=None left=None if tree.get_type() == TK.TOK_KEY_VALUE: left = parse_value(tree.get_child(0).get_child(0)) if tree.get_child(1).get_child(0).get_type() in (TK.TOK_DICT,TK.TOK_LIST): right = parse_object(tree.get_child(1).get_child(0)) else: right = parse_value(tree.get_child(1).get_child(0)) else: pass return {left:right}
def __init__(self, tree: Node, root=True): if tree.get_type() == TK.TOK_REVERSED: tree = tree.get_child(0) self.reversed = True self.root = root self.combine = 'must' if tree.get_type() == TK.TOK_COMPOUND: self.combine = tree.get_value() self.lchild = QueryBody(tree.get_child(0), False) self.rchild = QueryBody(tree.get_child(1), False) else: self.lchild = query_expression(tree)
def parse_object(tree: Node): retval = None if tree.get_type() == TK.TOK_DICT: retval = {} for element in tree.get_children(): retval.update(parse_kv(element)) if tree.get_type() == TK.TOK_LIST: retval = [] for element in tree.get_children(): if element.get_type() in (TK.TOK_DICT,TK.TOK_LIST): retval.append(parse_object(element)) else: retval.append(parse_value(element)) return retval
def parse_insert_row(tree: Node): retval = [] for e in tree.get_children(): if e.get_type() == TK.TOK_VALUE: retval.append(parse_value(e)) elif e.get_type() in (TK.TOK_DICT, TK.TOK_LIST): retval.append(parse_object(e)) return retval
def __init__(self, tree: Node): for element in tree.get_children(): if element.get_type() == TK.TOK_TABLE_NAME: (self._index, self._type) = parse_tok_table_name(element) if element.get_type() == TK.TOK_SET_COLUMNS_CLAUSE: self.update_sets = parse_update_sets(element) if element.get_type() == TK.TOK_WHERE: self.conditions = parse_conditions(element)
def parse_tok_sorts(tree: Node): sorts = {} for sort in tree.get_children(): if sort.get_type() == TK.TOK_SORT: order = 'asc' if sort.get_children_count() == 2: order = sort.get_child(1).get_value() sorts[sort.get_child(0).get_value()] = {'order': order} return sorts
def __init__(self, tree: Node): self.insert_columns = [] self.bulk_rows = [] for element in tree.get_children(): if element.get_type() == TK.TOK_TABLE_NAME: (self._index, self._type) = parse_tok_table_name(element) elif element.get_type() == TK.TOK_INSERT_COLUMNS: self.insert_columns = parse_insert_columns(element) elif element.get_type() == TK.TOK_INSERT_ROWS: self.bulk_rows = parse_bulk_rows(element)
def bucket_function(tree: Node, _size): bucket = {} bucket[tree.get_value()] = {} for i in range(0, tree.get_children_count()): if tree.get_child(i).get_type() == TK.TOK_DICT: bucket[tree.get_value()].update(parse_object(tree.get_child(i))) if _size != -1: bucket[tree.get_value()]['size'] = _size aggs = {"aggs": {}} field = bucket[tree.get_value()]['field'] aggs['aggs'][field] = bucket return (field, aggs)
def __init__(self, tree: Node): exec_node = tree.get_child(0) stmt = None self.dsl_body = {} self.curl_str = '' es_url = 'http://localhost:9200/' if exec_node.get_type() == TK.TOK_QUERY: stmt = Query(exec_node) self.curl_str = 'curl -XPOST ' + es_url + stmt._index + '/' + stmt._type + '/_search' self.dsl_body = stmt.dsl()
def __init__(self, tree: Node): self.insert_columns = [] self.insert_row = [] self.metas = {} for element in tree.get_children(): if element.get_type() == TK.TOK_TABLE_NAME: (self._index, self._type) = parse_tok_table_name(element) elif element.get_type() == TK.TOK_INSERT_COLUMNS: self.insert_columns = parse_insert_columns(element) elif element.get_type() == TK.TOK_INSERT_ROW: self.insert_row = parse_insert_row(element) for i in range(0, len(self.insert_columns)): if self.insert_columns[i] in [ '_id', '_parent', '_routing', '_type' ]: self.metas[self.insert_columns[i][1:]] = self.insert_row[i]
def __init__(self, tree: Node): for element in tree.get_children(): if element.get_type() == TK.TOK_TABLE_NAME: (self._index, self._type) = parse_tok_table_name(element) if element.get_type() == TK.TOK_WHERE: self.conditions = QueryBody(element.get_child(0))
def parse_tok_selexpr(tree: Node): retval = [] for e in tree.get_children(): retval.append(Selexpr(e)) return retval
def parse_tok_table_name(tree : Node): if tree.get_type() == TK.TOK_TABLE_NAME: return parse_table_name(tree.get_child(0)) else: pass
def parse_tok_value(tree: Node): if tree.children != None: if tree.get_child(0).get_type() == TK.TOK_DQ_VALUE: return '"' + tree.get_value() + '"' return tree.get_value()
def __init__(self, tree: Node): self.function_name = tree.get_value() self.function_parms = tree.get_children()
def query_expression(tree: Node): if tree.get_type() == TK.TOK_COMPARE: return CompareExpression(tree) if tree.get_type() == TK.TOK_FUNCTION: return FunctionExpression(tree)
def __init__(self, tree: Node, _size, root=True): self.buckets = [] for element in tree.get_children(): self.buckets.append(bucket(element, _size))
def bucket(tree: Node, _size): if tree.get_type() == TK.TOK_FUNCTION: return bucket_function(tree, _size) else: return bucket_field(tree, _size)
def __init__(self, tree: Node): for element in tree.get_children(): if element.get_type() == TK.TOK_TABLE_NAME: (self._index, self._type) = parse_tok_table_name(element)
def parse_insert_columns(tree: Node): retval = [] for e in tree.get_children(): if e.get_type() == TK.TOK_VALUE: retval.append(parse_value(e)) return retval
def parse_conditions(tree: Node): query_body = QueryBody(tree.get_child(0)) condition = {} query_body_travel(query_body, condition) return condition
def parse_update_sets(tree: Node): retval = {} for e in tree.get_children(): retval.update(parse_kv(e)) return retval
def parse_bulk_rows(tree: Node): retval = [] for e in tree.get_children(): retval.append(parse_insert_row(e)) return retval