-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathir.py
More file actions
568 lines (470 loc) · 14 KB
/
ir.py
File metadata and controls
568 lines (470 loc) · 14 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
from __future__ import annotations
from dataclasses import dataclass, field
from enum import Enum
from typing import Union, Tuple, Sequence, Optional, Dict
import datetime as dt
from decimal import Decimal
# Tree representation of LQP. Each non-terminal (those with more than one
# option) is an "abstract" class and each terminal is its own class. All of
# which are children of LqpNode. Value is an exception -- it is just a value.
@dataclass(frozen=True)
class SourceInfo:
file: str
line: int
column: int
def __str__(self) -> str:
return f"{self.file}:{self.line}:{self.column}"
# --- Logic Types ---
@dataclass(frozen=True)
class LqpNode:
meta: Optional[SourceInfo]
# Declaration := Def | Algorithm
@dataclass(frozen=True)
class Declaration(LqpNode):
pass
# Def(name::RelationId, body::Abstraction, attrs::Attribute[])
@dataclass(frozen=True)
class Def(Declaration):
name: RelationId
body: Abstraction
attrs: Sequence[Attribute]
# Constraint := FunctionalDependency
@dataclass(frozen=True)
class Constraint(Declaration):
name: RelationId
pass
# FunctionalDependency(guard::Abstraction, x::Var[], y::Var[])
@dataclass(frozen=True)
class FunctionalDependency(Constraint):
guard: Abstraction
keys: Sequence[Var]
values: Sequence[Var]
# Algorithm(globals::RelationId[], body::Script)
@dataclass(frozen=True)
class Algorithm(Declaration):
global_: Sequence[RelationId]
body: Script
# Script := Construct[]
@dataclass(frozen=True)
class Script(LqpNode):
constructs: Sequence[Construct]
# Construct := Loop | Instruction
@dataclass(frozen=True)
class Construct(LqpNode):
pass
# Loop(init::Instruction[], body::Algorithm)
@dataclass(frozen=True)
class Loop(Construct):
init: Sequence[Instruction]
body: Script
# Instruction := Assign | Break | Upsert | MonoidDef | MonusDef
@dataclass(frozen=True)
class Instruction(Construct):
pass
# Assign(name::RelationId, body::Abstraction, attrs::Attribute[])
@dataclass(frozen=True)
class Assign(Instruction):
name: RelationId
body: Abstraction
attrs: Sequence[Attribute]
# Upsert(arity::int, name::RelationId, body::Abstraction, attrs::Attribute[])
@dataclass(frozen=True)
class Upsert(Instruction):
value_arity: int
name: RelationId
body: Abstraction
attrs: Sequence[Attribute]
# Break(name::RelationId, body::Abstraction, attrs::Attribute[])
@dataclass(frozen=True)
class Break(Instruction):
name: RelationId
body: Abstraction
attrs: Sequence[Attribute]
# MonoidDef(arity::int, monoid::Monoid, name::RelationId, body::Abstraction, attrs::Attribute[])
@dataclass(frozen=True)
class MonoidDef(Instruction):
value_arity: int
monoid: Monoid
name: RelationId
body: Abstraction
attrs: Sequence[Attribute]
# MonusDef(arity::int, monoid::Monoid, name::RelationId, body::Abstraction, attrs::Attribute[])
@dataclass(frozen=True)
class MonusDef(Instruction):
value_arity: int
monoid: Monoid
name: RelationId
body: Abstraction
attrs: Sequence[Attribute]
# Monoid := OrMonoid | MinMonoid | MaxMonoid | SumMonoid
@dataclass(frozen=True)
class Monoid(LqpNode):
pass
# OrMonoid
@dataclass(frozen=True)
class OrMonoid(Monoid):
pass
# MinMonoid
@dataclass(frozen=True)
class MinMonoid(Monoid):
type: Type
# MaxMonoid
@dataclass(frozen=True)
class MaxMonoid(Monoid):
type: Type
# SumMonoid
@dataclass(frozen=True)
class SumMonoid(Monoid):
type: Type
# Abstraction(vars::Binding[], value::Formula)
@dataclass(frozen=True)
class Abstraction(LqpNode):
vars: Sequence[Tuple[Var, Type]]
value: Formula
# Formula := Exists | Reduce | Conjunction | Disjunction | Not | FFI | Atom | Pragma | Primitive | TrueVal | FalseVal | RelAtom | Cast
@dataclass(frozen=True)
class Formula(LqpNode):
pass
# Exists(body::Abstraction)
@dataclass(frozen=True)
class Exists(Formula):
body: Abstraction
# Reduce(op::Abstraction, body::Abstraction, terms::Term[])
@dataclass(frozen=True)
class Reduce(Formula):
op: Abstraction
body: Abstraction
terms: Sequence[Term]
# Conjunction(args::Formula[])
@dataclass(frozen=True)
class Conjunction(Formula):
args: Sequence[Formula]
# Disjunction(args::Formula[])
@dataclass(frozen=True)
class Disjunction(Formula):
args: Sequence[Formula]
# Not(arg::Formula)
@dataclass(frozen=True)
class Not(Formula):
arg: Formula
# FFI(name::string, args::Abstraction[], terms::Term[])
@dataclass(frozen=True)
class FFI(Formula):
name: str
args: Sequence[Abstraction]
terms: Sequence[Term]
# Atom(name::RelationId, terms::Term[])
@dataclass(frozen=True)
class Atom(Formula):
name: RelationId
terms: Sequence[Term]
# Pragma(name::string, terms::Term[])
@dataclass(frozen=True)
class Pragma(Formula):
name: str
terms: Sequence[Term]
# Primitive(name::string, terms::RelTerm[])
@dataclass(frozen=True)
class Primitive(Formula):
name: str
terms: Sequence[RelTerm]
# RelAtom(name::string, terms::RelTerm[])
@dataclass(frozen=True)
class RelAtom(Formula):
name: str
terms: Sequence[RelTerm]
# Cast(input::Term, result::Term)
@dataclass(frozen=True)
class Cast(Formula):
input: Term
result: Term
# Var(name::string)
@dataclass(frozen=True)
class Var(LqpNode):
name: str
# UInt128Value(low::fixed64, high::fixed64)
@dataclass(frozen=True)
class UInt128Value(LqpNode):
value: int
# Int128Value(low::fixed64, high::fixed64)
@dataclass(frozen=True)
class Int128Value(LqpNode):
value: int
@dataclass(frozen=True)
class MissingValue(LqpNode):
pass
# DateValue(year: int, month: int, day: int)
@dataclass(frozen=True)
class DateValue(LqpNode):
value: dt.date
# DatetimeValue(year: int, month: int, day: int, hour: int, minute: int, second: int, microsecond: int)
@dataclass(frozen=True)
class DateTimeValue(LqpNode):
value: dt.datetime
# DecimalValue(precision: int, scale: int, value: Decimal)
@dataclass(frozen=True)
class DecimalValue(LqpNode):
precision: int
scale: int
value: Decimal
# BooleanValue(value: bool)
# Note: We need a custom BooleanValue class to distinguish it from Python's `int` type.
# Python's built-in `bool` is a subclass of `int`.
@dataclass(frozen=True)
class BooleanValue(LqpNode):
value: bool
@dataclass(frozen=True)
class Value(LqpNode):
value: Union[
str,
int,
float,
UInt128Value,
Int128Value,
MissingValue,
DateValue,
DateTimeValue,
DecimalValue,
BooleanValue
]
# SpecializedValue(value::Value)
@dataclass(frozen=True)
class SpecializedValue(LqpNode):
value: Value
# Term := Var | Value
Term = Union[Var, Value]
# RelTerm := Term | SpecializedValue
RelTerm = Union[Term, SpecializedValue]
# Attribute(name::string, args::Constant[])
@dataclass(frozen=True)
class Attribute(LqpNode):
name: str
args: Sequence[Value]
# RelationId(id::UInt128)
@dataclass(frozen=True)
class RelationId(LqpNode):
id: int
def __post_init__(self):
if self.id < 0 or self.id > 0xffffffffffffffffffffffffffffffff:
raise ValueError("RelationId constructed with out of range (UInt128) number: {}".format(self.id))
def __str__(self) -> str:
if self.meta:
return f"RelationId(meta={self.meta}, id={self.id})"
return f"RelationId(id={self.id})"
def __eq__(self, other) -> bool:
return self.id == other.id
def __hash__(self) -> int:
return hash(self.id)
class TypeName(Enum):
UNSPECIFIED = 0
STRING = 1
INT = 2
FLOAT = 3
UINT128 = 4
INT128 = 5
DATE = 6
DATETIME = 7
MISSING = 8
DECIMAL = 9
BOOLEAN = 10
def __str__(self) -> str:
return self.name
@dataclass(frozen=True)
class Type(LqpNode):
type_name: TypeName
parameters: Sequence[Value]
# --- Data Types (Base Relations, BeTree Config) ---
# BeTreeConfig(epsilon::float, max_pivots::int, max_deltas::int, max_leaf::int)
@dataclass(frozen=True)
class BeTreeConfig(LqpNode):
epsilon: float
max_pivots: int
max_deltas: int
max_leaf: int
# BeTreeLocator(root_pageid::UInt128Value?, inline_data::bytes?, element_count::int, tree_height::int)
# Note: Exactly one of root_pageid or inline_data must be set (oneof in protobuf)
@dataclass(frozen=True)
class BeTreeLocator(LqpNode):
root_pageid: Optional[UInt128Value]
inline_data: Optional[bytes]
element_count: int
tree_height: int
# BeTreeInfo(key_types::Type[], value_types::Type[], storage_config::BeTreeConfig, relation_locator::BeTreeLocator)
@dataclass(frozen=True)
class BeTreeInfo(LqpNode):
key_types: Sequence[Type]
value_types: Sequence[Type]
storage_config: BeTreeConfig
relation_locator: BeTreeLocator
# Data := RelEDB | BeTreeRelation | CSVData | IcebergRelation
@dataclass(frozen=True)
class Data(Declaration):
pass
# RelEDB(target_id::RelationId, path::string[], types::Type[])
@dataclass(frozen=True)
class RelEDB(Data):
target_id: RelationId
path: Sequence[str]
types: Sequence[Type]
# BeTreeRelation(name::RelationId, relation_info::BeTreeInfo)
@dataclass(frozen=True)
class BeTreeRelation(Data):
name: RelationId
relation_info: BeTreeInfo
# CSVData(locator::CSVLocator, config::CSVConfig, columns::CSVColumn[], asof::string)
@dataclass(frozen=True)
class CSVData(Data):
locator: 'CSVLocator'
config: 'CSVConfig'
columns: Sequence['CSVColumn']
asof: str
# CSVLocator(paths::string[], inline_data::bytes?)
# Note: Exactly one of paths or inline_data should be set (mutually exclusive)
@dataclass(frozen=True)
class CSVLocator(LqpNode):
paths: Sequence[str]
inline_data: Optional[bytes]
# CSVConfig(header_row::int, skip::int, new_line::string, delimiter::string, quotechar::string,
# escapechar::string, comment::string, missing_strings::string[], decimal_separator::string,
# encoding::string, compression::string)
@dataclass(frozen=True)
class CSVConfig(LqpNode):
header_row: int
skip: int
new_line: str
delimiter: str
quotechar: str
escapechar: str
comment: str
missing_strings: Sequence[str]
decimal_separator: str
encoding: str
compression: str
# CSVColumn(column_name::string, target_id::RelationId, types::Type[])
@dataclass(frozen=True)
class CSVColumn(LqpNode):
column_name: str
target_id: RelationId
types: Sequence[Type]
# --- Fragment Types ---
# FragmentId(id::bytes)
@dataclass(frozen=True)
class FragmentId(LqpNode):
id: bytes
def __eq__(self, other) -> bool:
return self.id == other.id
def __hash__(self) -> int:
return hash(self.id)
# Fragment(id::FragmentId, declarations::Declaration[], debug_info::DebugInfo)
@dataclass(frozen=True)
class Fragment(LqpNode):
id: FragmentId
declarations: Sequence[Declaration]
debug_info: DebugInfo
@dataclass(frozen=True)
class DebugInfo(LqpNode):
id_to_orig_name: Dict[RelationId, str]
# --- Transaction Types ---
# Define(fragment::Fragment)
@dataclass(frozen=True)
class Define(LqpNode):
fragment: Fragment
# Undefine(fragment_id::FragmentId)
@dataclass(frozen=True)
class Undefine(LqpNode):
fragment_id: FragmentId
# Context(relations::RelationId[])
@dataclass(frozen=True)
class Context(LqpNode):
relations: Sequence[RelationId]
# Write := Define | Undefine | Context
@dataclass(frozen=True)
class Write(LqpNode):
write_type: Union[Define, Undefine, Context]
# Demand(relation_id::RelationId)
@dataclass(frozen=True)
class Demand(LqpNode):
relation_id: RelationId
# Output(name::string?, relation_id::RelationId)
@dataclass(frozen=True)
class Output(LqpNode):
name: Union[str, None]
relation_id: RelationId
# ExportCSVConfig
@dataclass(frozen=True)
class ExportCSVConfig(LqpNode):
path: str
data_columns: Sequence[ExportCSVColumn]
partition_size: Optional[int] = None
compression: Optional[str] = None
syntax_header_row: Optional[int] = None
syntax_missing_string: Optional[str] = None
syntax_delim: Optional[str] = None
syntax_quotechar: Optional[str] = None
syntax_escapechar: Optional[str] = None
@dataclass(frozen=True)
class ExportCSVColumn(LqpNode):
column_name: str
column_data: RelationId
# ExportCSVTableConfig
@dataclass(frozen=True)
class ExportCSVTableConfig(LqpNode):
path: str
table_def: RelationId
partition_size: Optional[int] = None
compression: Optional[str] = None
syntax_header_row: Optional[int] = None
syntax_missing_string: Optional[str] = None
syntax_delim: Optional[str] = None
syntax_quotechar: Optional[str] = None
syntax_escapechar: Optional[str] = None
# Export(name::string, relation_id::RelationId)
@dataclass(frozen=True)
class Export(LqpNode):
# TODO: Once we add a JSON export, this should be union[ExportCSVConfig, ExportCSVTableConfig, ExportJSONConfig]
config: Union[ExportCSVConfig, ExportCSVTableConfig]
# Abort(name::string?, relation_id::RelationId)
@dataclass(frozen=True)
class Abort(LqpNode):
name: Union[str, None]
relation_id: RelationId
# Read := Demand | Output | Export | WhatIf | Abort
@dataclass(frozen=True)
class Read(LqpNode):
read_type: Union[Demand, Output, Export, WhatIf, Abort]
# Epoch(writes::Write[], reads::Read[])
@dataclass(frozen=True)
class Epoch(LqpNode):
writes: Sequence[Write] = field(default_factory=list)
reads: Sequence[Read] = field(default_factory=list)
# WhatIf(branch::string?, epoch::Epoch)
@dataclass(frozen=True)
class WhatIf(LqpNode):
branch: Union[str, None]
epoch: Epoch
# Transaction(epochs::Epoch[], configure::Configure)
@dataclass(frozen=True)
class Transaction(LqpNode):
epochs: Sequence[Epoch]
configure: Configure
sync: Optional[Sync]
# Configure(semantics_version::int, ivm_config::IVMConfig)
@dataclass(frozen=True)
class Configure(LqpNode):
semantics_version: int
ivm_config: IVMConfig
# Sync(fragments::FragmentId[])
@dataclass(frozen=True)
class Sync(LqpNode):
fragments: Sequence[FragmentId]
# IVMConfig(level::MaintenanceLevel)
@dataclass(frozen=True)
class IVMConfig(LqpNode):
level: MaintenanceLevel
class MaintenanceLevel(Enum):
UNSPECIFIED = 0
OFF = 1
AUTO = 2
ALL = 3
def __str__(self) -> str:
return self.name