-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
81 lines (60 loc) · 2.08 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
import uuid
import inspect
from dpark import DparkContext, optParser
from sql.parser import parse
from collections import OrderedDict
from models import Model
optParser.add_option("-s") # "option used for py.test"
optParser.add_option("-x")
class Table(object):
dialect = "excel"
columns = ()
def __init__(self, name, paths=None, columns=None, query=None):
self.name = name
self.columns = OrderedDict(columns or self.__class__.columns)
self.paths = paths or []
self.query = query
def index(self, field):
return self.columns.keys().index(field)
def rdd(self, dpark=None):
if self.query:
return self.query.rdd
if dpark is None:
# should not happen
return
def coercion(r):
return [m.cast(r[i]) for i, m
in enumerate(self.columns.values())]
return dpark.union([dpark.csvFile(p, dialect=self.dialect)
for p in self.paths]).map(coercion)
class Schema(object):
def __init__(self, tables):
self.table_dict = {t.name: t for t in tables}
def find_table(self, name):
try:
return self.table_dict[name]
except:
raise Exception("table not found: %s" % name)
class Query(object):
def __init__(self, sql, schema):
self.sql = sql
self.schema = schema
self.dpark = DparkContext()
self.rdd = None
def execute(self):
# pylint: disable=E1101
self.select = parse(self.sql)
self.table = self.schema.find_table(self.select.table_name.value())
name = str(uuid.uuid4())
columns = self.select.select_list.column_defs(self.table)
self.result_table = Table(name, columns=columns, query=self)
self.select.visit(self)
return self
def collect(self):
# pylint: disable=E1101
outfile = self.select.outfile
if outfile:
self.rdd.saveAsCSVFile(outfile.filedir)
return outfile.filedir
return tuple(self.rdd.collect())