You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AQuery/engine/scan.py

99 lines
4.2 KiB

from xmlrpc.client import Boolean
from engine.ast import ColRef, TableInfo, View, ast_node, Context
from engine.utils import base62uuid
from engine.expr import expr
class scan(ast_node):
name = 'scan'
3 years ago
def __init__(self, parent: "ast_node", node, size = None, context: Context = None, const = False):
self.type = type
self.size = size
3 years ago
self.const = "const " if const else ""
super().__init__(parent, node, context)
def init(self, _):
self.datasource = self.context.datasource
3 years ago
self.initializers = ''
self.start = ''
3 years ago
self.front = ''
self.body = ''
self.end = '}'
3 years ago
self.mode = None
self.filters = []
scan_vars = set(s.it_var for s in self.context.scans)
self.it_ver = 'i' + base62uuid(2)
while(self.it_ver in scan_vars):
self.it_ver = 'i' + base62uuid(6)
self.parent.context.scans.append(self)
def produce(self, node):
if type(node) is ColRef:
3 years ago
self.colref = node
if self.size is None:
3 years ago
self.mode = ["col", node.table]
3 years ago
self.start += f'for ({self.const}auto& {self.it_ver} : {node.reference()}) {{\n'
else:
3 years ago
self.mode = ["idx", node.table]
self.start += f"for (uint32_t {self.it_ver} = 0; {self.it_ver} < {node.reference()}.size; ++{self.it_ver}){{\\n"
elif type(node) is str:
3 years ago
self.mode = ["idx", None]
3 years ago
self.start+= f'for({self.const}auto& {self.it_ver} : {node}) {{\n'
else:
3 years ago
self.mode = ["idx", node] # Node is the TableInfo
self.start += f"for (uint32_t {self.it_ver} = 0; {self.it_ver} < {self.size}; ++{self.it_ver}){{\n"
3 years ago
def add(self, stmt, position = "body"):
if position == "body":
self.body += stmt + '\n'
3 years ago
elif position == "init":
3 years ago
self.initializers += stmt + '\n'
3 years ago
else:
self.front += stmt + '\n'
def finalize(self):
3 years ago
for f in self.filters:
self.start += f
self.end += '}'
3 years ago
self.context.remove_scan(self, self.initializers + self.start + self.front + self.body + self.end)
class filter(ast_node):
name = 'filter'
def __init__(self, parent: "ast_node", node, materialize = False, context = None):
self.materialize = materialize
super().__init__(parent, node, context)
def init(self, _):
self.datasource = self.context.datasource
self.view = View(self.context, self.datasource)
self.value = None
3 years ago
def spawn(self, node):
# TODO: deal with subqueries
3 years ago
self.modified_node = node
return super().spawn(node)
def __materialize__(self):
if self.materialize:
cols = [] if self.datasource is None else self.datasource.columns
self.output = TableInfo('tn'+base62uuid(6), cols, self.context)
self.output.construct()
if type(self.value) is View: # cond filtered on tables.
self.emit(f'{self.value.name}:&{self.value.name}')
for o, c in zip(self.output.columns,self.value.table.columns):
3 years ago
self.emit(f'{o.cname}:{c.cname}[{self.value.name}]')
elif self.value is not None: # cond is scalar
tmpVar = 't'+base62uuid(7)
self.emit(f'{tmpVar}:{self.value}')
for o, c in zip(self.output.columns, self.datasource.columns):
3 years ago
self.emit(f'{o.cname}:$[{tmpVar};{c.cname};()]')
3 years ago
def finalize(self):
self.scanner.finalize()
def consume(self, _):
# TODO: optimizations after converting expr to cnf
3 years ago
self.scanner = None
for s in self.context.scans:
if self.datasource == s.mode[1]:
self.scanner = s
break
if self.scanner is None:
self.scanner = scan(self, self.datasource, self.datasource.get_size())
3 years ago
self.expr = expr(self, self.modified_node)
3 years ago
self.scanner.filters.append(f'if ({self.expr.cexpr(self.scanner.it_ver)}) {{\n')