Skip to content

Commit efceb91

Browse files
committed
[SQL] Vec/Map/ZSet/Variant expressions are not always literals (#3164)
Signed-off-by: Mihai Budiu <mbudiu@feldera.com>
1 parent d16c788 commit efceb91

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+781
-727
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.dbsp.sqlCompiler.ir.expression.DBSPApplyExpression;
88
import org.dbsp.sqlCompiler.ir.expression.DBSPExpression;
99
import org.dbsp.sqlCompiler.ir.expression.DBSPTupleExpression;
10-
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPZSetLiteral;
10+
import org.dbsp.sqlCompiler.ir.expression.DBSPZSetExpression;
1111
import org.dbsp.sqlCompiler.ir.type.DBSPType;
1212
import org.dbsp.sqlCompiler.ir.type.primitive.DBSPTypeTimestamp;
1313

@@ -18,7 +18,7 @@
1818
public final class DBSPNowOperator extends DBSPSimpleOperator {
1919
// zset!(Tup1::new(now()))
2020
static DBSPExpression createFunction(CalciteObject node) {
21-
return new DBSPZSetLiteral(
21+
return new DBSPZSetExpression(
2222
new DBSPTupleExpression(new DBSPApplyExpression(
2323
"now", new DBSPTypeTimestamp(node, false))));
2424
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.dbsp.sqlCompiler.compiler.visitors.inner.InnerVisitor;
3030
import org.dbsp.sqlCompiler.ir.expression.DBSPExpression;
3131
import org.dbsp.sqlCompiler.ir.expression.DBSPTupleExpression;
32+
import org.dbsp.sqlCompiler.ir.expression.DBSPZSetExpression;
3233
import org.dbsp.sqlCompiler.ir.expression.literal.*;
3334
import org.dbsp.util.Utilities;
3435

@@ -173,13 +174,13 @@ public VisitDecision preorder(DBSPTupleExpression node) {
173174
}
174175

175176
@Override
176-
public VisitDecision preorder(DBSPZSetLiteral literal) {
177-
for (Map.Entry<DBSPExpression, Long> entry: literal.data.entrySet()) {
177+
public VisitDecision preorder(DBSPZSetExpression expression) {
178+
for (Map.Entry<DBSPExpression, Long> entry: expression.data.entrySet()) {
178179
DBSPExpression key = entry.getKey();
179180
long value = entry.getValue();
180181
if (value < 0)
181182
throw new UnsupportedException("ZSet with negative weights is not representable as CSV",
182-
literal.getNode());
183+
expression.getNode());
183184
for (; value != 0; value--) {
184185
key.accept(this);
185186
this.appendable.append("\n");
@@ -191,12 +192,12 @@ public VisitDecision preorder(DBSPZSetLiteral literal) {
191192
/**
192193
* Write a literal to a file as a csv format.
193194
* @param file File to write to.
194-
* @param literal Literal to write.
195+
* @param expression Literal to write.
195196
*/
196-
public static File toCsv(DBSPCompiler compiler, File file, DBSPZSetLiteral literal) throws IOException {
197+
public static File toCsv(DBSPCompiler compiler, File file, DBSPZSetExpression expression) throws IOException {
197198
StringBuilder builder = new StringBuilder();
198199
ToCsvVisitor visitor = new ToCsvVisitor(compiler, builder, () -> "");
199-
visitor.apply(literal);
200+
visitor.apply(expression);
200201
FileWriter writer = new FileWriter(file);
201202
writer.write(builder.toString());
202203
writer.close();

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPRealLiteral;
1515
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPStringLiteral;
1616
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPTimestampLiteral;
17-
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPZSetLiteral;
17+
import org.dbsp.sqlCompiler.ir.expression.DBSPZSetExpression;
1818
import org.dbsp.util.Utilities;
1919

2020
import java.util.Map;
@@ -103,13 +103,13 @@ public VisitDecision preorder(DBSPTupleExpression node) {
103103
}
104104

105105
@Override
106-
public VisitDecision preorder(DBSPZSetLiteral literal) {
107-
for (Map.Entry<DBSPExpression, Long> entry: literal.data.entrySet()) {
106+
public VisitDecision preorder(DBSPZSetExpression expression) {
107+
for (Map.Entry<DBSPExpression, Long> entry: expression.data.entrySet()) {
108108
DBSPExpression key = entry.getKey();
109109
long value = entry.getValue();
110110
if (value < 0)
111111
throw new UnsupportedException("ZSet with negative weights is not representable as SQL",
112-
literal.getNode());
112+
expression.getNode());
113113
for (; value != 0; value--) {
114114
key.accept(this);
115115
this.appendable.append("\n");

sql-to-dbsp-compiler/SQL-compiler/src/main/java/org/dbsp/sqlCompiler/compiler/backend/rust/ToRustInnerVisitor.java

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@
8989
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPI64Literal;
9090
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPI8Literal;
9191
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPISizeLiteral;
92-
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPIndexedZSetLiteral;
92+
import org.dbsp.sqlCompiler.ir.expression.DBSPIndexedZSetExpression;
9393
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPIntervalMillisLiteral;
9494
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPIntervalMonthsLiteral;
9595
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPLiteral;
96-
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPMapLiteral;
96+
import org.dbsp.sqlCompiler.ir.expression.DBSPMapExpression;
9797
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPNullLiteral;
9898
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPRealLiteral;
9999
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPStrLiteral;
@@ -105,10 +105,10 @@
105105
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPU32Literal;
106106
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPU64Literal;
107107
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPUSizeLiteral;
108-
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPVariantLiteral;
108+
import org.dbsp.sqlCompiler.ir.expression.DBSPVariantExpression;
109109
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPVariantNullLiteral;
110-
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPVecLiteral;
111-
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPZSetLiteral;
110+
import org.dbsp.sqlCompiler.ir.expression.DBSPVecExpression;
111+
import org.dbsp.sqlCompiler.ir.expression.DBSPZSetExpression;
112112
import org.dbsp.sqlCompiler.ir.path.DBSPPath;
113113
import org.dbsp.sqlCompiler.ir.path.DBSPPathSegment;
114114
import org.dbsp.sqlCompiler.ir.path.DBSPSimplePathSegment;
@@ -405,45 +405,44 @@ public VisitDecision preorder(DBSPBoolLiteral literal) {
405405
}
406406

407407
@Override
408-
public VisitDecision preorder(DBSPVecLiteral literal) {
409-
if (literal.isNull())
410-
return this.doNull(literal);
411-
assert literal.data != null;
412-
if (literal.mayBeNull())
408+
public VisitDecision preorder(DBSPVecExpression expression) {
409+
if (expression.data == null)
410+
return this.doNullExpression(expression);
411+
if (expression.getType().mayBeNull)
413412
this.builder.append("Some(");
414413
this.builder.append("vec!(");
415-
if (literal.data.size() > 1)
414+
if (expression.data.size() > 1)
416415
this.builder.increase();
417-
for (DBSPExpression exp: literal.data) {
416+
for (DBSPExpression exp: expression.data) {
418417
exp.accept(this);
419418
this.builder.append(", ");
420419
}
421-
if (literal.data.size() > 1)
420+
if (expression.data.size() > 1)
422421
this.builder.decrease();
423422
this.builder.append(")");
424-
if (literal.mayBeNull())
423+
if (expression.getType().mayBeNull)
425424
this.builder.append(")");
426425
return VisitDecision.STOP;
427426
}
428427

429428
@Override
430-
public VisitDecision preorder(DBSPMapLiteral literal) {
431-
if (literal.isNull())
432-
return this.doNull(literal);
433-
if (literal.mayBeNull())
429+
public VisitDecision preorder(DBSPMapExpression expression) {
430+
if (expression.isNull())
431+
return this.doNullExpression(expression);
432+
if (expression.getType().mayBeNull)
434433
this.builder.append("Some(");
435434
this.builder.append("BTreeMap::from([")
436435
.increase();
437-
assert literal.values != null;
438-
for (int i = 0; i < Objects.requireNonNull(literal.keys).size(); i++) {
436+
assert expression.values != null;
437+
for (int i = 0; i < Objects.requireNonNull(expression.keys).size(); i++) {
439438
this.builder.append("(");
440-
literal.keys.get(i).accept(this);
439+
expression.keys.get(i).accept(this);
441440
this.builder.append(", ");
442-
literal.values.get(i).accept(this);
441+
expression.values.get(i).accept(this);
443442
this.builder.append("), ");
444443
}
445444
this.builder.decrease().append("])");
446-
if (literal.mayBeNull())
445+
if (expression.getType().mayBeNull)
447446
this.builder.append(")");
448447
return VisitDecision.STOP;
449448
}
@@ -467,12 +466,12 @@ public VisitDecision preorder(DBSPBinaryLiteral literal) {
467466
}
468467

469468
@Override
470-
public VisitDecision preorder(DBSPZSetLiteral literal) {
469+
public VisitDecision preorder(DBSPZSetExpression expression) {
471470
this.builder.append("zset!(");
472-
boolean large = literal.data.entrySet().size() > 1;
471+
boolean large = expression.data.entrySet().size() > 1;
473472
if (large)
474473
this.builder.increase();
475-
for (Map.Entry<DBSPExpression, Long> e: literal.data.entrySet()) {
474+
for (Map.Entry<DBSPExpression, Long> e: expression.data.entrySet()) {
476475
e.getKey().accept(this);
477476
this.builder.append(" => ")
478477
.append(e.getValue());
@@ -486,7 +485,7 @@ public VisitDecision preorder(DBSPZSetLiteral literal) {
486485
}
487486

488487
@Override
489-
public VisitDecision preorder(DBSPIndexedZSetLiteral literal) {
488+
public VisitDecision preorder(DBSPIndexedZSetExpression expression) {
490489
this.builder.append("indexed_zset!(")
491490
.append(")");
492491
return VisitDecision.STOP;
@@ -566,20 +565,19 @@ public VisitDecision preorder(DBSPUSizeLiteral literal) {
566565
}
567566

568567
@Override
569-
public VisitDecision preorder(DBSPVariantLiteral literal) {
570-
if (literal.isNull())
571-
return this.doNull(literal);
572-
if (literal.mayBeNull())
568+
public VisitDecision preorder(DBSPVariantExpression expression) {
569+
if (expression.value == null)
570+
return this.doNullExpression(expression);
571+
if (expression.getType().mayBeNull)
573572
this.builder.append("Some(");
574-
if (literal.isSqlNull) {
573+
if (expression.isSqlNull) {
575574
this.builder.append("Variant::SqlNull");
576575
} else {
577576
this.builder.append("Variant::from(");
578-
assert literal.value != null;
579-
literal.value.accept(this);
577+
expression.value.accept(this);
580578
this.builder.append(")");
581579
}
582-
if (literal.mayBeNull())
580+
if (expression.getType().mayBeNull)
583581
this.builder.append(")");
584582
return VisitDecision.STOP;
585583
}

sql-to-dbsp-compiler/SQL-compiler/src/main/java/org/dbsp/sqlCompiler/compiler/backend/rust/ToRustVisitor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@
9595
import org.dbsp.sqlCompiler.ir.expression.DBSPWindowBoundExpression;
9696
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPBoolLiteral;
9797
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPISizeLiteral;
98-
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPIndexedZSetLiteral;
98+
import org.dbsp.sqlCompiler.ir.expression.DBSPIndexedZSetExpression;
9999
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPStrLiteral;
100-
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPZSetLiteral;
100+
import org.dbsp.sqlCompiler.ir.expression.DBSPZSetExpression;
101101
import org.dbsp.sqlCompiler.ir.statement.DBSPFunctionItem;
102102
import org.dbsp.sqlCompiler.ir.statement.DBSPStructItem;
103103
import org.dbsp.sqlCompiler.ir.statement.DBSPStructWithHelperItem;
@@ -1509,11 +1509,11 @@ VisitDecision constantLike(DBSPSimpleOperator operator) {
15091509
operator.function.accept(this.innerVisitor);
15101510
this.builder.append("} else {");
15111511
if (operator.outputType.is(DBSPTypeZSet.class)) {
1512-
DBSPZSetLiteral empty = DBSPZSetLiteral.emptyWithElementType(
1512+
DBSPZSetExpression empty = DBSPZSetExpression.emptyWithElementType(
15131513
operator.getOutputZSetElementType());
15141514
empty.accept(this.innerVisitor);
15151515
} else {
1516-
assert operator.function.to(DBSPIndexedZSetLiteral.class).isEmpty();
1516+
assert operator.function.to(DBSPIndexedZSetExpression.class).isEmpty();
15171517
operator.function.accept(this.innerVisitor);
15181518
}
15191519
this.builder.append("}));");

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPBoolLiteral;
6060
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPI64Literal;
6161
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPLiteral;
62-
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPVecLiteral;
62+
import org.dbsp.sqlCompiler.ir.expression.DBSPVecExpression;
6363
import org.dbsp.sqlCompiler.ir.type.DBSPType;
6464
import org.dbsp.sqlCompiler.ir.type.derived.DBSPTypeTuple;
6565
import org.dbsp.sqlCompiler.ir.type.IsNumericType;
@@ -296,7 +296,7 @@ void processArrayAgg(SqlBasicAggFunction function) {
296296
boolean ignoreNulls = this.call.ignoreNulls();
297297
boolean distinct = this.call.isDistinct();
298298
DBSPType elementType = this.resultType.to(DBSPTypeVec.class).getElementType();
299-
DBSPExpression zero = DBSPVecLiteral.emptyWithElementType(elementType, this.resultType.mayBeNull);
299+
DBSPExpression zero = DBSPVecExpression.emptyWithElementType(elementType, this.resultType.mayBeNull);
300300
DBSPExpression aggregatedValue = this.getAggregatedValue();
301301
DBSPVariablePath accumulator = this.resultType.var();
302302
String functionName;

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@
172172
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPI32Literal;
173173
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPIntervalMillisLiteral;
174174
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPLiteral;
175-
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPMapLiteral;
176-
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPVecLiteral;
177-
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPZSetLiteral;
175+
import org.dbsp.sqlCompiler.ir.expression.DBSPMapExpression;
176+
import org.dbsp.sqlCompiler.ir.expression.DBSPVecExpression;
177+
import org.dbsp.sqlCompiler.ir.expression.DBSPZSetExpression;
178178
import org.dbsp.sqlCompiler.ir.path.DBSPPath;
179179
import org.dbsp.sqlCompiler.ir.path.DBSPSimplePathSegment;
180180
import org.dbsp.sqlCompiler.ir.statement.DBSPFunctionItem;
@@ -1570,7 +1570,7 @@ private void visitCollect(Collect collect) {
15701570
assert elementType.sameType(flatten ? inputRowType.getFieldType(0) : inputRowType);
15711571

15721572
row = inputRowType.ref().var();
1573-
DBSPExpression zero = DBSPVecLiteral.emptyWithElementType(elementType, type.mayBeNull);
1573+
DBSPExpression zero = DBSPVecExpression.emptyWithElementType(elementType, type.mayBeNull);
15741574
DBSPVariablePath accumulator = vecType.var();
15751575
String functionName;
15761576
DBSPExpression[] arguments;
@@ -1598,7 +1598,7 @@ private void visitCollect(Collect collect) {
15981598
assert valueType.sameType(inputRowType.getFieldType(1));
15991599

16001600
row = inputRowType.ref().var();
1601-
DBSPExpression zero = new DBSPMapLiteral(mapType, Linq.list());
1601+
DBSPExpression zero = new DBSPMapExpression(mapType, Linq.list());
16021602
DBSPVariablePath accumulator = mapType.var();
16031603
String functionName;
16041604
DBSPExpression[] arguments;
@@ -1648,7 +1648,7 @@ void visitLogicalValues(LogicalValues values) {
16481648
resultType = sourceType;
16491649
}
16501650

1651-
DBSPZSetLiteral result = DBSPZSetLiteral.emptyWithElementType(resultType);
1651+
DBSPZSetExpression result = DBSPZSetExpression.emptyWithElementType(resultType);
16521652
for (List<RexLiteral> t : values.getTuples()) {
16531653
List<DBSPExpression> expressions = new ArrayList<>();
16541654
if (t.size() != sourceType.size())
@@ -2860,7 +2860,7 @@ DBSPNode compileModifyTable(TableModifyStatement modify) {
28602860
CreateTableStatement def = this.tableContents.getTableDefinition(modify.tableName);
28612861
this.modifyTableTranslation = new ModifyTableTranslation(
28622862
modify, def, targetColumnList, this.compiler);
2863-
DBSPZSetLiteral result;
2863+
DBSPZSetExpression result;
28642864
if (modify.rel instanceof LogicalTableScan scan) {
28652865
// Support for INSERT INTO table (SELECT * FROM otherTable)
28662866
ProgramIdentifier sourceTable = Utilities.toIdentifier(scan.getTable().getQualifiedName());
@@ -3025,7 +3025,7 @@ public DBSPNode compile(RelStatement statement) {
30253025
throw new UnsupportedException(statement.getCalciteObject());
30263026
}
30273027

3028-
private DBSPZSetLiteral compileConstantProject(LogicalProject project) {
3028+
private DBSPZSetExpression compileConstantProject(LogicalProject project) {
30293029
// Specialization of the visitor's visit method for LogicalProject
30303030
// Does not produce a DBSPOperator, but only a literal.
30313031
CalciteObject node = CalciteObject.create(project);
@@ -3034,7 +3034,7 @@ private DBSPZSetLiteral compileConstantProject(LogicalProject project) {
30343034
DBSPType inputType = this.convertType(project.getInput().getRowType(), false);
30353035
DBSPVariablePath row = inputType.ref().var(); // should not be used
30363036
ExpressionCompiler expressionCompiler = new ExpressionCompiler(project, row, this.compiler);
3037-
DBSPZSetLiteral result = DBSPZSetLiteral.emptyWithElementType(outputElementType);
3037+
DBSPZSetExpression result = DBSPZSetExpression.emptyWithElementType(outputElementType);
30383038

30393039
List<DBSPExpression> resultColumns = new ArrayList<>();
30403040
int index = 0;

0 commit comments

Comments
 (0)