Skip to content

Commit afa94c4

Browse files
committed
[SQL] Fix incorrect field type computation for nullable structs
Signed-off-by: Mihai Budiu <mbudiu@feldera.com>
1 parent 6aedf79 commit afa94c4

3 files changed

Lines changed: 38 additions & 7 deletions

File tree

sql-to-dbsp-compiler/SQL-compiler/src/main/java/org/dbsp/sqlCompiler/ir/expression/DBSPFieldExpression.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public final class DBSPFieldExpression extends DBSPExpression {
4646
this.expression = expression;
4747
this.fieldNo = fieldNo;
4848
Utilities.enforce(fieldNo >= 0, () -> "Negative field index " + fieldNo);
49-
Utilities.enforce(!expression.getType().mayBeNull || type.mayBeNull);
49+
Utilities.enforce(!expression.getType().mayBeNull || type.mayBeNull,
50+
() -> "Non-nullable field from nullable struct");
5051
}
5152

5253
DBSPFieldExpression(DBSPExpression expression, int fieldNo, DBSPType type) {

sql-to-dbsp-compiler/SQL-compiler/src/main/java/org/dbsp/sqlCompiler/ir/expression/DBSPTupleExpression.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ public static DBSPTupleExpression flatten(List<DBSPExpression> expressions) {
110110
for (DBSPExpression expression: expressions) {
111111
DBSPTypeTupleBase type = expression.getType().to(DBSPTypeTupleBase.class);
112112
for (int i = 0; i < type.size(); i++) {
113-
DBSPType fieldType = type.tupFields[i];
114-
DBSPExpression field = new DBSPFieldExpression(expression.deepCopy(), i, fieldType)
113+
DBSPExpression field = new DBSPFieldExpression(expression.deepCopy(), i)
115114
.simplify()
116115
.applyCloneIfNeeded();
117116
fields.add(field);

sql-to-dbsp-compiler/SQL-compiler/src/test/java/org/dbsp/sqlCompiler/compiler/sql/simple/Regression1Tests.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,14 @@
88
import org.dbsp.sqlCompiler.compiler.sql.tools.CompilerCircuitStream;
99
import org.dbsp.sqlCompiler.compiler.sql.tools.SqlIoTest;
1010
import org.dbsp.sqlCompiler.compiler.visitors.inner.InnerVisitor;
11-
import org.dbsp.sqlCompiler.compiler.visitors.outer.CSE;
1211
import org.dbsp.sqlCompiler.compiler.visitors.outer.CircuitVisitor;
13-
import org.dbsp.sqlCompiler.compiler.visitors.outer.Passes;
1412
import org.dbsp.sqlCompiler.ir.expression.DBSPCloneExpression;
1513
import org.dbsp.sqlCompiler.ir.expression.DBSPTupleExpression;
1614
import org.dbsp.sqlCompiler.ir.expression.DBSPZSetExpression;
1715
import org.dbsp.sqlCompiler.ir.expression.literal.DBSPStringLiteral;
1816
import org.dbsp.sqlCompiler.ir.statement.DBSPStaticItem;
1917
import org.dbsp.util.Linq;
20-
import org.dbsp.util.Logger;
2118
import org.junit.Assert;
22-
import org.junit.Ignore;
2319
import org.junit.Test;
2420

2521
import java.sql.SQLException;
@@ -1381,5 +1377,40 @@ CREATE TABLE interval_tbl(
13811377
0 | -121| 1
13821378
0 | -318226680.000000| 1""");
13831379
}
1380+
1381+
@Test
1382+
public void issue5120() {
1383+
var ccs = this.getCCS("""
1384+
CREATE TYPE S AS(i1 INT NOT NULL, i2 INT);
1385+
CREATE TABLE tbl(id INT, c1_arr S ARRAY);
1386+
1387+
CREATE VIEW v AS SELECT
1388+
id, i1_val + 1, i2_val + 1, idx
1389+
FROM tbl,
1390+
UNNEST(c1_arr) WITH ORDINALITY AS t (i1_val, i2_val, idx);""");
1391+
ccs.step("""
1392+
INSERT INTO tbl VALUES (0, ARRAY[S(1, NULL), S(5, 6)]);
1393+
""", """
1394+
id | i1 | i2 | idx | weight
1395+
-----------------------------
1396+
0 | 2 | | 1 | 1
1397+
0 | 6 | 7 | 2 | 1""");
1398+
1399+
ccs = this.getCCS("""
1400+
CREATE TABLE tbl(id INT,
1401+
c1_arr ROW(i1 INT NOT NULL, i2 INT NULL) ARRAY NOT NULL);
1402+
1403+
CREATE VIEW v AS SELECT
1404+
id, i1_val + 1, i2_val + 1, idx
1405+
FROM tbl,
1406+
UNNEST(c1_arr) WITH ORDINALITY AS t (i1_val, i2_val, idx);""");
1407+
ccs.step("""
1408+
INSERT INTO tbl VALUES (0, ARRAY[ROW(1, NULL), ROW(5, 6)]);
1409+
""", """
1410+
id | i1 | i2 | idx | weight
1411+
-----------------------------
1412+
0 | 2 | | 1 | 1
1413+
0 | 6 | 7 | 2 | 1""");
1414+
}
13841415
}
13851416

0 commit comments

Comments
 (0)