Skip to content

Commit 6cce880

Browse files
committed
[SQL] Add source position information to Window operators
Signed-off-by: Mihai Budiu <mbudiu@feldera.com>
1 parent 6e53d3d commit 6cce880

File tree

18 files changed

+150
-60
lines changed

18 files changed

+150
-60
lines changed

js-packages/profiler-layout/src/lib/components/ProfilerTooltip.svelte

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,39 @@
11
<script lang="ts" module>
2-
import type { NodeAttributes, TooltipRow } from "profiler-lib";
3-
import {
4-
measurementCategory,
5-
measurementDescription,
6-
shadeOfRed,
7-
} from "profiler-lib";
8-
import { SvelteSet } from "svelte/reactivity";
9-
import { groupBy } from "$lib/functions/array";
2+
import type { NodeAttributes, TooltipRow } from 'profiler-lib'
3+
import { measurementCategory, measurementDescription, shadeOfRed } from 'profiler-lib'
4+
import { SvelteSet } from 'svelte/reactivity'
5+
import { groupBy } from '$lib/functions/array'
106
11-
export type TooltipData =
12-
| { nodeAttributes: NodeAttributes }
13-
| {
14-
genericTable: {
15-
header: string;
16-
columns: string[];
17-
rows: {
18-
stub: { text: string; onclick?: () => void };
19-
cells: {
20-
text: string;
21-
operation: string;
22-
normalizedValue: number;
23-
}[];
24-
}[];
25-
};
26-
};
7+
export type TooltipData =
8+
| { nodeAttributes: NodeAttributes }
9+
| {
10+
genericTable: {
11+
header: string
12+
columns: string[]
13+
rows: {
14+
stub: { text: string; onclick?: () => void }
15+
cells: {
16+
text: string
17+
operation: string
18+
normalizedValue: number
19+
}[]
20+
}[]
21+
}
22+
}
2723
28-
// Track which metric categories are collapsed (true = collapsed, false = expanded)
29-
let collapsedCategories = $state(new SvelteSet<string>());
30-
// If true show the advanced categories
31-
let showAdvanced = $state(false);
24+
// Track which metric categories are collapsed (true = collapsed, false = expanded)
25+
let collapsedCategories = $state(new SvelteSet<string>())
26+
// If true show the advanced categories
27+
let showAdvanced = $state(false)
3228
33-
// Toggle category collapse state
34-
function toggleCategory(category: string) {
35-
if (collapsedCategories.has(category)) {
36-
collapsedCategories.delete(category);
37-
} else {
38-
collapsedCategories.add(category);
39-
}
29+
// Toggle category collapse state
30+
function toggleCategory(category: string) {
31+
if (collapsedCategories.has(category)) {
32+
collapsedCategories.delete(category)
33+
} else {
34+
collapsedCategories.add(category)
4035
}
36+
}
4137
</script>
4238

4339
<script lang="ts">
@@ -73,7 +69,7 @@
7369
type="checkbox"
7470
id="show-advanced"
7571
bind:checked={showAdvanced}
76-
/> show advanced</label
72+
/>show advanced&nbsp;</label
7773
></th
7874
>
7975
</tr>

sql-to-dbsp-compiler/SQL-compiler/src/main/java/org/dbsp/sqlCompiler/circuit/operator/DBSPNestedOperator.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.dbsp.sqlCompiler.circuit.annotation.Annotations;
77
import org.dbsp.sqlCompiler.compiler.backend.JsonDecoder;
88
import org.dbsp.sqlCompiler.compiler.errors.InternalCompilerError;
9+
import org.dbsp.sqlCompiler.compiler.errors.SourcePositionRange;
910
import org.dbsp.sqlCompiler.compiler.frontend.calciteCompiler.ProgramIdentifier;
1011
import org.dbsp.sqlCompiler.compiler.frontend.calciteObject.CalciteEmptyRel;
1112
import org.dbsp.sqlCompiler.compiler.frontend.calciteObject.CalciteRelNode;
@@ -57,6 +58,14 @@ public boolean hasOutput(int outputNumber) {
5758
return this.internalOutputs.get(outputNumber) != null;
5859
}
5960

61+
@Override
62+
public List<SourcePositionRange> getSourcePositions() {
63+
ArrayList<SourcePositionRange> result = new ArrayList<>();
64+
for (DBSPOperator op: this.allOperators)
65+
result.addAll(op.getSourcePositions());
66+
return result;
67+
}
68+
6069
public boolean contains(DBSPOperator operator) {
6170
return this.operators.contains(operator);
6271
}

sql-to-dbsp-compiler/SQL-compiler/src/main/java/org/dbsp/sqlCompiler/circuit/operator/DBSPOperator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.dbsp.sqlCompiler.circuit.annotation.Annotations;
2929
import org.dbsp.sqlCompiler.circuit.annotation.CompactName;
3030
import org.dbsp.sqlCompiler.circuit.annotation.OperatorHash;
31+
import org.dbsp.sqlCompiler.compiler.errors.SourcePositionRange;
3132
import org.dbsp.sqlCompiler.ir.expression.DBSPClosureExpression;
3233
import org.dbsp.sqlCompiler.ir.expression.DBSPExpression;
3334
import org.dbsp.sqlCompiler.ir.type.user.DBSPTypeStream;
@@ -213,4 +214,8 @@ public OutputPort getOutput(int outputNo) {
213214
public CalciteRelNode getRelNode() {
214215
return this.node.to(CalciteRelNode.class);
215216
}
217+
218+
public List<SourcePositionRange> getSourcePositions() {
219+
return this.getRelNode().getSourcePositions();
220+
}
216221
}

sql-to-dbsp-compiler/SQL-compiler/src/main/java/org/dbsp/sqlCompiler/circuit/operator/DBSPWindowOperator.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,9 @@ public final class DBSPWindowOperator extends DBSPBinaryOperator implements ICon
2828
public final boolean lowerInclusive;
2929
public final boolean upperInclusive;
3030

31-
public DBSPWindowOperator(
32-
CalciteRelNode node, CalciteObject object,
33-
boolean lowerInclusive, boolean upperInclusive,
31+
public DBSPWindowOperator(CalciteRelNode node, boolean lowerInclusive, boolean upperInclusive,
3432
OutputPort data, OutputPort control) {
35-
super(node, "window", new NoExpression(DBSPTypeVoid.INSTANCE), data.outputType(),
36-
data.isMultiset(), data, control);
33+
super(node, "window", null, data.outputType(), data.isMultiset(), data, control);
3734
// Check that the left input and output are indexed ZSets
3835
this.getOutputIndexedZSetType();
3936
this.lowerInclusive = lowerInclusive;
@@ -48,7 +45,7 @@ public DBSPSimpleOperator with(
4845
Utilities.enforce(newInputs.size() == 2, () -> "Expected 2 inputs, got " + newInputs.size());
4946
if (force || this.inputsDiffer(newInputs))
5047
return new DBSPWindowOperator(
51-
this.getRelNode(), this.getFunction().getNode(), this.lowerInclusive, this.upperInclusive,
48+
this.getRelNode(), this.lowerInclusive, this.upperInclusive,
5249
newInputs.get(0), newInputs.get(1)).copyAnnotations(this);
5350
}
5451
return this;
@@ -68,7 +65,7 @@ public static DBSPWindowOperator fromJson(JsonNode node, JsonDecoder decoder) {
6865
DBSPSimpleOperator.CommonInfo info = commonInfoFromJson(node, decoder);
6966
boolean lowerInclusive = Utilities.getBooleanProperty(node, "lowerInclusive");
7067
boolean upperInclusive = Utilities.getBooleanProperty(node, "upperInclusive");
71-
return new DBSPWindowOperator(CalciteEmptyRel.INSTANCE, CalciteObject.EMPTY,
68+
return new DBSPWindowOperator(CalciteEmptyRel.INSTANCE,
7269
lowerInclusive, upperInclusive, info.getInput(0), info.getInput(1))
7370
.addAnnotations(info.annotations(), DBSPWindowOperator.class);
7471
}

sql-to-dbsp-compiler/SQL-compiler/src/main/java/org/dbsp/sqlCompiler/compiler/backend/dot/ToDotNodesVisitor.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,16 @@ String getFunction(DBSPSimpleOperator node) {
203203
String chain = node.to(DBSPChainOperator.class).chain.toString();
204204
return rustToDot(chain);
205205
}
206-
if (expression == null)
207-
return "";
206+
if (expression == null) {
207+
if (this.details >= 3) {
208+
StringBuilder builder = new StringBuilder();
209+
for (SourcePositionRange pos: node.getSourcePositions())
210+
builder.append(pos.toShortString());
211+
return builder.toString();
212+
} else {
213+
return "";
214+
}
215+
}
208216
if (node.is(DBSPFlatMapOperator.class)) {
209217
if (expression.is(DBSPFlatmap.class)) {
210218
expression = LowerCircuitVisitor.rewriteFlatmap(

sql-to-dbsp-compiler/SQL-compiler/src/main/java/org/dbsp/sqlCompiler/compiler/frontend/calciteObject/CalciteEmptyRel.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
/** Represents a CalciteObject that does not exist.
99
* Similar to {@link CalciteObject#EMPTY}, but this is a subclass of {@link CalciteRelNode}. */
1010
public class CalciteEmptyRel extends CalciteRelNode {
11+
@Override
12+
public CalciteRelNode copy() {
13+
return INSTANCE;
14+
}
15+
1116
private CalciteEmptyRel() {}
1217

1318
public static final CalciteEmptyRel INSTANCE = new CalciteEmptyRel();

sql-to-dbsp-compiler/SQL-compiler/src/main/java/org/dbsp/sqlCompiler/compiler/frontend/calciteObject/CalciteRelNode.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,35 @@
1414
import org.dbsp.util.IHasId;
1515
import org.dbsp.util.IIndentStream;
1616

17+
import java.util.ArrayList;
18+
import java.util.List;
1719
import java.util.Map;
1820

1921
public abstract class CalciteRelNode extends CalciteObject implements IHasId {
2022
// Not clear these should be here
2123
public static final SqlDialect DIALECT = SqlDialect.DatabaseProduct.UNKNOWN.getDialect();
2224
static final RelToSqlConverter CONVERTER = new RelToSqlConverter(DIALECT);
25+
final List<SourcePositionRange> positions = new ArrayList<>();
26+
27+
public List<SourcePositionRange> getSourcePositions() {
28+
return this.positions;
29+
}
2330

2431
protected CalciteRelNode(SourcePositionRange pos) {
2532
super(pos);
33+
if (pos.isValid())
34+
this.positions.add(pos);
2635
}
2736

37+
public CalciteRelNode addSourcePositions(Iterable<SourcePositionRange> positions) {
38+
for (var pos: positions)
39+
if (pos.isValid())
40+
this.positions.add(pos);
41+
return this;
42+
}
43+
44+
public abstract CalciteRelNode copy();
45+
2846
protected CalciteRelNode() { this(SourcePositionRange.INVALID); }
2947

3048
@Override

sql-to-dbsp-compiler/SQL-compiler/src/main/java/org/dbsp/sqlCompiler/compiler/frontend/calciteObject/IntermediateRel.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public String toString() {
3939
return this.getId() + " PartOf(" + this.relNode.getDigest() + ")";
4040
}
4141

42+
@Override
43+
public CalciteRelNode copy() {
44+
return new IntermediateRel(this.relNode).addSourcePositions(this.positions);
45+
}
46+
4247
@Override
4348
public IIndentStream asJson(IIndentStream stream, Map<RelNode, Integer> idRemap) {
4449
return stream.append("{").increase()

sql-to-dbsp-compiler/SQL-compiler/src/main/java/org/dbsp/sqlCompiler/compiler/frontend/calciteObject/LastRel.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public String toString() {
2727
return this.getId() + " Last(" + this.relNode.getDigest() + ")";
2828
}
2929

30+
@Override
31+
public CalciteRelNode copy() {
32+
return new LastRel(this.relNode, super.position).addSourcePositions(this.positions);
33+
}
34+
3035
@Override
3136
public IIndentStream asJson(IIndentStream stream, Map<RelNode, Integer> idRemap) {
3237
return stream.append("{").increase()

sql-to-dbsp-compiler/SQL-compiler/src/main/java/org/dbsp/sqlCompiler/compiler/frontend/calciteObject/RelAnd.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ public class RelAnd extends CalciteRelNode {
1717
final Set<LastRel> nodes;
1818
SourcePositionRange position = SourcePositionRange.INVALID;
1919

20+
@Override
21+
public CalciteRelNode copy() {
22+
RelAnd result = new RelAnd();
23+
for (var last: this.nodes)
24+
result.add(last);
25+
return result.addSourcePositions(this.positions);
26+
}
27+
2028
public RelAnd() {
2129
this.nodes = new HashSet<>();
2230
}

0 commit comments

Comments
 (0)