Skip to content

Commit 060eee6

Browse files
committed
[SQL] Forbid negation when applied to unsigned values
Signed-off-by: Mihai Budiu <mbudiu@feldera.com>
1 parent 977bdd1 commit 060eee6

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

docs.feldera.com/docs/sql/integer.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ bits).
66

77
The legal operations are `+` (plus, unary and binary), `-` (minus,
88
unary and binary), `*` (multiplication), `/` (division), `%`
9-
(modulus).
9+
(modulus). Unary minus (negation) cannot be applied to unsigned
10+
values.
1011

1112
Modulus involving negative numbers happens as follows:
1213

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,11 @@ public DBSPExpression visitCall(RexCall call) {
908908
return makeUnaryExpression(node, type, DBSPOpcode.UNARY_PLUS, ops);
909909
case CHECKED_MINUS_PREFIX:
910910
case MINUS_PREFIX:
911+
Utilities.enforce(ops.size() == 1);
912+
DBSPType op0type = ops.get(0).getType();
913+
if (op0type.is(DBSPTypeInteger.class) && !op0type.to(DBSPTypeInteger.class).signed) {
914+
throw new CompilationError("Unary minus cannot be applied to unsigned values", node);
915+
}
911916
return makeUnaryExpression(node, type, DBSPOpcode.NEG, ops);
912917
case BIT_AND:
913918
return makeBinaryExpressions(node, type, DBSPOpcode.BW_AND, ops);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,4 +1717,15 @@ CREATE TABLE lookup (
17171717
FROM v a
17181718
LEFT JOIN lookup l ON a.key = l.key;""");
17191719
}
1720+
1721+
@Test
1722+
public void issue5386() {
1723+
this.statementsFailingInCompilation("""
1724+
CREATE TABLE T(x INT UNSIGNED);
1725+
CREATE VIEW V AS SELECT -x FROM T;""",
1726+
"Unary minus cannot be applied");
1727+
this.statementsFailingInCompilation(
1728+
"CREATE VIEW V AS SELECT - CAST(1 AS INT UNSIGNED)",
1729+
"Unary minus cannot be applied");
1730+
}
17201731
}

0 commit comments

Comments
 (0)