parent
b9a8ad3ac7
commit
8b182cf0f2
@ -1,38 +1,59 @@
|
|||||||
from engine.ast import ColRef, TableInfo, ast_node
|
from engine.ast import ColRef, TableInfo, View, ast_node, Context
|
||||||
from engine.utils import base62uuid
|
from engine.utils import base62uuid, seps
|
||||||
from engine.expr import expr
|
from engine.expr import expr
|
||||||
|
import k
|
||||||
|
|
||||||
|
class order_item:
|
||||||
|
def __init__(self, name, node, order = True):
|
||||||
|
self.name = name
|
||||||
|
self.order = order
|
||||||
|
self.node = node
|
||||||
|
self.materialized = False
|
||||||
|
|
||||||
|
def materialize(self):
|
||||||
|
if not self.materialized:
|
||||||
|
self.name = expr(self.node, self.name, False).k9expr
|
||||||
|
self.materialized = True
|
||||||
|
return ('' if self.order else '-') + f'({self.name})'
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.materialize()
|
||||||
|
def __repr__(self):
|
||||||
|
return self.__str__()
|
||||||
|
|
||||||
|
class orders:
|
||||||
|
def __init__(self, node, datasource):
|
||||||
|
self.order_items = []
|
||||||
|
self.materialized = False
|
||||||
|
self.view = None
|
||||||
|
self.node = node
|
||||||
|
self.datasource = datasource
|
||||||
|
self.n_attrs = -1
|
||||||
|
|
||||||
|
def materialize(self):
|
||||||
|
if not self.materialized:
|
||||||
|
self.view = View(self.node.context, self.datasource, False)
|
||||||
|
keys = ';'.join([f'{o}' for o in self.order_items])
|
||||||
|
self.n_attrs = len(self.order_items)
|
||||||
|
self.node.emit(f"{self.view.name}: > +`j (({',' if self.n_attrs == 1 else ''}{keys}))")
|
||||||
|
self.materialized = True
|
||||||
|
|
||||||
|
def append(self, o):
|
||||||
|
self.order_items.append(o)
|
||||||
|
|
||||||
class orderby(ast_node):
|
class orderby(ast_node):
|
||||||
name = '_orderby'
|
name = '_orderby'
|
||||||
|
|
||||||
def init(self, _):
|
def init(self, _):
|
||||||
self.group = 'g' + base62uuid(7)
|
|
||||||
self.datasource = self.parent.datasource
|
self.datasource = self.parent.datasource
|
||||||
self.datasource.rec = []
|
self.order = orders(self, self.datasource)
|
||||||
|
self.view = ''
|
||||||
def produce(self, node):
|
def produce(self, node):
|
||||||
if type(node) is not list:
|
if type(node) is not list:
|
||||||
node = [node]
|
node = [node]
|
||||||
g_contents = '('
|
for n in node:
|
||||||
first_col = ''
|
order = not ('sort' in n and n['sort'] == 'desc')
|
||||||
for i, g in enumerate(node):
|
self.order.append(order_item(n['value'], self, order))
|
||||||
v = g['value']
|
|
||||||
e = expr(self, v).k9expr
|
|
||||||
# if v is compound expr, create tmp cols
|
|
||||||
if type(v) is not str:
|
|
||||||
tmpcol = 't' + base62uuid(7)
|
|
||||||
self.emit(f'{tmpcol}:{e}')
|
|
||||||
e = tmpcol
|
|
||||||
if i == 0:
|
|
||||||
first_col = e
|
|
||||||
g_contents += e + (';'if i < len(node)-1 else '')
|
|
||||||
|
|
||||||
self.emit(f'{self.group}:'+g_contents+')')
|
|
||||||
self.n_grps = len(node)
|
|
||||||
if self.n_grps <= 1:
|
|
||||||
self.emit(f'{self.group}:={self.group}')
|
|
||||||
else:
|
|
||||||
self.emit(f'{self.group}:groupby[+({self.group},(,!(#({first_col}))))]')
|
|
||||||
|
|
||||||
def consume(self, _):
|
def consume(self, _):
|
||||||
self.referenced = self.datasource.rec
|
self.datasource.order.append(self.order)
|
||||||
self.datasource.rec = None
|
|
||||||
return super().consume(_)
|
|
|
Loading…
Reference in new issue