-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathdataflow.ts
More file actions
206 lines (177 loc) · 6.67 KB
/
dataflow.ts
File metadata and controls
206 lines (177 loc) · 6.67 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
// Parse a dataflow.json file produced by the SQL compiler using the --dataflow flag,
// and create a (reduced) representation of the program representation
import { Comparable } from "./util.js";
/** Serialized JSON representation of a Dataflow graph coming from the SQL compiler. */
export interface Dataflow {
calcite_plan: any;
mir: MirNodes;
sources?: Array<string>; // This field is generated by SQL-to-DBSP compiler, but is not included in the dataflow file from the support bundle. We should fall back to pipeline.program_code in the latter case
}
export interface MirNodes {
[key: string]: MirNode | NestedMirNode;
}
export interface OutputPort {
node: string;
port: number;
}
export interface MirNode {
operation: string;
table: string | null;
view: string | null;
inputs: Array<OutputPort>;
// We don't care about this yet
calcite: any;
positions: Array<JsonPositionRange>;
persistent_id?: string;
}
export interface NestedNodeFixed {
operation: "nested";
outputs: Array<OutputPort>;
}
type NestedMirNode = NestedNodeFixed & {
[key: string]: MirNode;
}
export interface OutputPort {
node: string,
output: number,
}
/** Represents the (SQL) sources of a program. */
export class Sources {
constructor(readonly lines: Array<string>) {
this.lines = lines;
}
static prefix(line: number): string {
return line.toString().padStart(5, " ") + "| ";
}
static truncate(s: string): string {
if (s.length > 100) {
return s.substring(0, 97) + "...";
}
return s;
}
getLine(lineNo: number): string {
// Sources may be missing
return this.lines[lineNo] || "";
}
/** Given a position range, generate a string with the relevant source fragment.
* Currently only full lines are generated, ignoring the column information. */
public toStringFromPositionRange(range: SourcePositionRange): string {
if (this.lines.length === 0) {
return "";
}
if (range.is_empty()) return "";
let result = "";
for (let line = range.start.line; line <= range.end.line; line++) {
let l = Sources.truncate(this.getLine(line - 1));
result += Sources.prefix(line) + l + "\n";
}
return result;
}
/** Given multiple position ranges, generate the relevant source fragment.
* Since the source fragment includes only full lines, we do not really currently
* use the column information in any way. */
public toString(positions: SourcePositionRanges): string {
if (this.lines.length === 0) {
return "";
}
let result = "";
let lastLine = 0;
for (const range of positions.sort()) {
let nonOverlap = range.after(lastLine);
result += this.toStringFromPositionRange(nonOverlap);
lastLine = range.end.line;
}
return result;
}
}
/** Zero or more SourcePositionRange values. */
export class SourcePositionRanges {
constructor(readonly positions: Array<SourcePositionRange>) { }
static empty(): SourcePositionRanges {
return new SourcePositionRanges(new Array<SourcePositionRange>());
}
/** Return the source positions ranges sorted and with duplicates eliminated. */
sort(): Array<SourcePositionRange> {
this.positions.sort((a, b) => a.compareTo(b));
if (this.positions.length > 1) {
let write = 1;
for (let read = 1; read < this.positions.length; read++) {
if (!this.positions[read]!.equals(this.positions[read - 1]!)) {
this.positions[write] = this.positions[read]!;
write++;
}
}
this.positions.length = write; // truncate
}
return this.positions;
}
// Append includes deduplication
append(other: SourcePositionRanges) {
for (const position of other.positions) {
if (!this.positions.find(v => v.equals(position))) {
this.positions.push(position);
}
}
}
}
interface JsonPositionRange {
start_line_number: number;
start_column: number;
end_line_number: number;
end_column: number;
}
/** A position in the source code; lines and columns are numbered from 1. */
export class SourcePosition implements Comparable<SourcePosition> {
constructor(readonly line: number, readonly column: number) { }
public compareTo(other: SourcePosition): number {
let c = Comparable.compareTo(this.line, other.line);
if (c !== 0) return c;
return Comparable.compareTo(this.column, other.column);
}
}
/** A range between two source positions; the second one must be after the first one. */
export class SourcePositionRange implements Comparable<SourcePositionRange> {
constructor(readonly range: JsonPositionRange) { }
static empty(): SourcePositionRange {
return new SourcePositionRange({ start_line_number: 0, start_column: 0, end_line_number: 0, end_column: 0 });
}
public get start(): SourcePosition {
return new SourcePosition(this.range.start_line_number, this.range.start_column);
}
public get end(): SourcePosition {
return new SourcePosition(this.range.end_line_number, this.range.end_column);
}
/** Compare two ranges; comparison is done lexicographically on start, end. */
public compareTo(other: SourcePositionRange): number {
let c = this.start.compareTo(other.start);
if (c != 0) {
return c;
}
return this.end.compareTo(other.end);
}
public is_empty(): boolean {
return this.start.line === 0 && this.start.column === 0 && this.end.line === 0 && this.end.column === 0;
}
public equals(position: SourcePositionRange): unknown {
return this.range.start_column === position.range.start_column &&
this.range.start_line_number === position.range.start_line_number &&
this.range.end_line_number === position.range.end_line_number &&
this.range.end_column === position.range.end_column;
}
/** Return a range containing only the lines strictly after the specified line. */
public after(line: number): SourcePositionRange {
if (line >= this.end.line) {
return SourcePositionRange.empty();
}
let start = this.start;
if (start.line <= line) {
start = new SourcePosition(line, 1);
}
return new SourcePositionRange({
start_line_number: start.line,
start_column: start.column,
end_line_number: this.end.line,
end_column: this.end.column
});
}
}