-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_table.lua
203 lines (180 loc) · 5.24 KB
/
create_table.lua
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
local ffi = require 'ffi'
local getopt = require 'getopt'
local nonoptions = {}
local port = 83301
local mem_size = 10 * 1024^3
local function config(portN, memSz)
box.cfg{ listen = tonumber(portN), memtx_memory = tonumber(memSz) }
end
local function show_usage()
print(arg[-1] .. ' ' .. arg[0],
[[
Usage: m:p:
-p N .. listen port N
-m N .. memtx memory size
]]
)
end
for opt, v in getopt(arg, 'm:p:', nonoptions) do
if opt == 'm' then
mem_size = v
elseif opt == 'p' then
port = v
elseif opt == '?' then
show_usage()
os.exit(1)
end
end
config(port, mem_size)
-- box.schema.user.drop('guest', {['read,write,execute']= 'universe'})
box.schema.user.grant('guest', 'read,write,execute', 'universe', nil,
{if_not_exists = true})
local function create_table(table_ddl)
print(table_ddl)
local lowcase = table_ddl:lower()
local table = string.match(lowcase, '^create%s+table%s+(%a+)%s')
if not table then return end
local ddl = string.format('drop table if exists %s', table)
box.execute(ddl)
box.execute(table_ddl)
end
create_table [[
CREATE TABLE REGION (
R_REGIONKEY INTEGER PRIMARY KEY NOT NULL,
R_NAME TEXT NOT NULL,
R_COMMENT TEXT
)
]]
create_table [[
CREATE TABLE NATION (
N_NATIONKEY INTEGER PRIMARY KEY NOT NULL,
N_NAME TEXT NOT NULL,
N_REGIONKEY INTEGER NOT NULL,
N_COMMENT TEXT,
FOREIGN KEY (N_REGIONKEY) REFERENCES REGION(R_REGIONKEY)
)
]]
create_table [[
CREATE TABLE PART (
P_PARTKEY INTEGER PRIMARY KEY NOT NULL,
P_NAME TEXT NOT NULL,
P_MFGR TEXT NOT NULL,
P_BRAND TEXT NOT NULL,
P_TYPE TEXT NOT NULL,
P_SIZE INTEGER NOT NULL,
P_CONTAINER TEXT NOT NULL,
P_RETAILPRICE DOUBLE NOT NULL,
P_COMMENT TEXT NOT NULL
)
]]
create_table [[
CREATE TABLE SUPPLIER (
S_SUPPKEY INTEGER PRIMARY KEY NOT NULL,
S_NAME TEXT NOT NULL,
S_ADDRESS TEXT NOT NULL,
S_NATIONKEY INTEGER NOT NULL,
S_PHONE TEXT NOT NULL,
S_ACCTBAL DOUBLE NOT NULL,
S_COMMENT TEXT NOT NULL,
FOREIGN KEY (S_NATIONKEY) REFERENCES NATION(N_NATIONKEY)
)
]]
create_table [[
CREATE TABLE PARTSUPP (
PS_PARTKEY INTEGER NOT NULL,
PS_SUPPKEY INTEGER NOT NULL,
PS_AVAILQTY INTEGER NOT NULL,
PS_SUPPLYCOST DOUBLE NOT NULL,
PS_COMMENT TEXT NOT NULL,
PRIMARY KEY (PS_PARTKEY, PS_SUPPKEY),
FOREIGN KEY (PS_SUPPKEY) REFERENCES SUPPLIER(S_SUPPKEY),
FOREIGN KEY (PS_PARTKEY) REFERENCES PART(P_PARTKEY)
)
]]
create_table [[
CREATE TABLE CUSTOMER (
C_CUSTKEY INTEGER PRIMARY KEY NOT NULL,
C_NAME TEXT NOT NULL,
C_ADDRESS TEXT NOT NULL,
C_NATIONKEY INTEGER NOT NULL,
C_PHONE TEXT NOT NULL,
C_ACCTBAL DOUBLE NOT NULL,
C_MKTSEGMENT TEXT NOT NULL,
C_COMMENT TEXT NOT NULL,
FOREIGN KEY (C_NATIONKEY) REFERENCES NATION(N_NATIONKEY)
)
]]
create_table [[
CREATE TABLE ORDERS (
O_ORDERKEY INTEGER PRIMARY KEY NOT NULL,
O_CUSTKEY INTEGER NOT NULL,
O_ORDERSTATUS TEXT NOT NULL,
O_TOTALPRICE DOUBLE NOT NULL,
O_ORDERDATE DATETIME NOT NULL,
O_ORDERPRIORITY TEXT NOT NULL,
O_CLERK TEXT NOT NULL,
O_SHIPPRIORITY INTEGER NOT NULL,
O_COMMENT TEXT NOT NULL,
FOREIGN KEY (O_CUSTKEY) REFERENCES CUSTOMER(C_CUSTKEY)
)
]]
create_table [[
CREATE TABLE LINEITEM (
L_ORDERKEY INTEGER NOT NULL,
L_PARTKEY INTEGER NOT NULL,
L_SUPPKEY INTEGER NOT NULL,
L_LINENUMBER INTEGER NOT NULL,
L_QUANTITY INTEGER NOT NULL,
L_EXTENDEDPRICE DOUBLE NOT NULL,
L_DISCOUNT DOUBLE NOT NULL,
L_TAX DOUBLE NOT NULL,
L_RETURNFLAG TEXT NOT NULL,
L_LINESTATUS TEXT NOT NULL,
L_SHIPDATE DATETIME NOT NULL,
L_COMMITDATE DATETIME NOT NULL,
L_RECEIPTDATE DATETIME NOT NULL,
L_SHIPINSTRUCT TEXT NOT NULL,
L_SHIPMODE TEXT NOT NULL,
L_COMMENT TEXT NOT NULL,
PRIMARY KEY (L_ORDERKEY, L_LINENUMBER),
FOREIGN KEY (L_ORDERKEY) REFERENCES ORDERS(O_ORDERKEY),
FOREIGN KEY (L_PARTKEY, L_SUPPKEY) REFERENCES PARTSUPP(PS_PARTKEY, PS_SUPPKEY)
)
]]
-- should obey topological sort order
-- to not violate foreign key constraints
local tables = {
'region', 'nation', 'part', 'supplier',
'partsupp', 'customer', 'orders', 'lineitem'
}
for _, tblname in ipairs(tables) do
local f = assert(io.open(string.format("tpch-dbgen/%s.tbl", tblname), 'rb'))
print (tblname)
while true do
local line = f:read('*line')
if not line then break end
local t = {}
for s in string.gmatch(line, '[^|]+') do
local cvt = tonumber(s)
if cvt ~= nil then
-- hack to retain doubleness for normalized numbers, e.g. 901.00
if s:match('-?%d+%.%d+$') then
cvt = ffi.cast('double', cvt)
end
-- hack to intercept datetime-like literals and convert to
-- proper cdata type
elseif s:match('%d%d%d%d%-%d%d%-%d%d') then
local datetime = require 'datetime'
cvt = datetime.parse(s)
else
cvt = s
end
table.insert(t, cvt)
end
local tuple = box.tuple.new(t)
box.space[tblname:upper()]:insert(tuple)
end
f:close()
end
box.snapshot()
os.exit(0)