Bill Sun 2 years ago
commit 8d739f8f3e

@ -30,8 +30,8 @@ class ast_node:
def emit(self, code): def emit(self, code):
self.context.emit(code) self.context.emit(code)
def add(self, code): def add(self, code, sp = ' '):
self.sql += code + ' ' self.sql += code + sp
def addc(self, code): def addc(self, code):
self.ccode += code + '\n' self.ccode += code + '\n'
@ -83,7 +83,7 @@ class projection(ast_node):
def produce(self, node): def produce(self, node):
self.add('SELECT') self.add('SELECT')
self.has_postproc = False self.has_postproc = 'into' in node
if 'select' in node: if 'select' in node:
p = node['select'] p = node['select']
self.distinct = False self.distinct = False
@ -271,7 +271,11 @@ class projection(ast_node):
self.var_table[col.name] = offset self.var_table[col.name] = offset
for n in (col.table.alias): for n in (col.table.alias):
self.var_table[f'{n}.'+col.name] = offset self.var_table[f'{n}.'+col.name] = offset
# monetdb doesn't support select into table
# if 'into' in node:
# self.into_stub = f'{{INTOSTUB{base62uuid(20)}}}'
# self.add(self.into_stub, '')
def finialize(astnode:ast_node): def finialize(astnode:ast_node):
if(astnode is not None): if(astnode is not None):
self.add(astnode.sql) self.add(astnode.sql)
@ -425,18 +429,18 @@ class select_distinct(projection):
super().consume(node) super().consume(node)
if self.has_postproc: if self.has_postproc:
self.context.emitc( self.context.emitc(
f'{self.out_table.table_name}->distinct();' f'{self.out_table.contextname_cpp}->distinct();'
) )
self.finalize() self.finalize()
class select_into(ast_node): class select_into(ast_node):
def init(self, node): def init(self, _):
if isinstance(self.parent, projection): if isinstance(self.parent, projection):
if self.context.has_dll: # if self.parent.has_postproc:
# has postproc put back to monetdb # # has postproc put back to monetdb
self.produce = self.produce_cpp self.produce = self.produce_cpp
else: # else:
self.produce = self.produce_sql # self.produce = self.produce_sql
else: else:
raise ValueError('parent must be projection') raise ValueError('parent must be projection')
@ -448,7 +452,8 @@ class select_into(ast_node):
self.ccode = f'{self.parent.out_table.contextname_cpp}->monetdb_append_table(cxt->alt_server, \"{node.lower()}\");' self.ccode = f'{self.parent.out_table.contextname_cpp}->monetdb_append_table(cxt->alt_server, \"{node.lower()}\");'
def produce_sql(self, node): def produce_sql(self, node):
self.sql = f' INTO {node}' self.context.sql = self.context.sql.replace(
self.parent.into_stub, f'INTO {node}', 1)
class orderby(ast_node): class orderby(ast_node):
@ -1245,6 +1250,13 @@ class outfile(ast_node):
filename = node['loc']['literal'] if 'loc' in node else node['literal'] filename = node['loc']['literal'] if 'loc' in node else node['literal']
import os import os
p = os.path.abspath('.').replace('\\', '/') + '/' + filename p = os.path.abspath('.').replace('\\', '/') + '/' + filename
print('Warning: file {p} exists and will be overwritten')
if os.path.exists(p):
try:
os.remove(p)
except OSError:
print(f'Error: file {p} exists and cannot be removed')
self.sql = f'COPY {self.parent.sql} INTO \'{p}\'' self.sql = f'COPY {self.parent.sql} INTO \'{p}\''
d = ',' d = ','
e = '\\n' e = '\\n'

@ -346,7 +346,8 @@ class expr(ast_node):
self.type = ByteT self.type = ByteT
elif type(node) is float: elif type(node) is float:
self.type = DoubleT self.type = DoubleT
self.sql = f'{{"CAST({node} AS DOUBLE)" if not c_code else "{node}f"}}'
def finalize(self, override = False): def finalize(self, override = False):
from reconstruct.ast import udf from reconstruct.ast import udf
if self.codebuf is None or override: if self.codebuf is None or override:

Loading…
Cancel
Save