Skip to content

Commit

Permalink
[CALCITE-2180] Invalid code generated for negative of byte and short …
Browse files Browse the repository at this point in the history
…values
  • Loading branch information
julianhyde committed Feb 16, 2018
1 parent becb6df commit 3c67a60
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.calcite.linq4j.tree.ParameterExpression;
import org.apache.calcite.linq4j.tree.Primitive;
import org.apache.calcite.linq4j.tree.Types;
import org.apache.calcite.linq4j.tree.UnaryExpression;
import org.apache.calcite.plan.RelOptTable;
import org.apache.calcite.prepare.Prepare;
import org.apache.calcite.rel.type.RelDataType;
Expand Down Expand Up @@ -1901,9 +1902,14 @@ public Expression implement(
RexToLixTranslator translator,
RexCall call,
List<Expression> translatedOperands) {
return Expressions.makeUnary(
expressionType,
translatedOperands.get(0));
final Expression operand = translatedOperands.get(0);
final UnaryExpression e = Expressions.makeUnary(expressionType, operand);
if (e.type.equals(operand.type)) {
return e;
}
// Certain unary operators do not preserve type. For example, the "-"
// operator applied to a "byte" expression returns an "int".
return Expressions.convert_(e, operand.type);
}
}

Expand Down
36 changes: 36 additions & 0 deletions core/src/test/resources/sql/misc.iq
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,42 @@ Expression 'DEPTNO' is not being grouped

!use scott

# ORDER BY expression with SELECT DISTINCT
select distinct deptno, job
from "scott".emp
order by substring(job from 2 for 1), -deptno;
+--------+-----------+
| DEPTNO | JOB |
+--------+-----------+
| 30 | SALESMAN |
| 30 | MANAGER |
| 20 | MANAGER |
| 10 | MANAGER |
| 30 | CLERK |
| 20 | CLERK |
| 10 | CLERK |
| 20 | ANALYST |
| 10 | PRESIDENT |
+--------+-----------+
(9 rows)

!ok

# [CALCITE-2180] Invalid code generated for negative of byte and short values
select -deptno as d
from "scott".dept;
+-----+
| D |
+-----+
| -40 |
| -30 |
| -20 |
| -10 |
+-----+
(4 rows)

!ok

# [CALCITE-2099] Incorrect code generated for UNION
select count(*) as c from "scott".emp group by deptno
union
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1605,8 +1605,14 @@ public static TryStatement makeTry(Type type, Expression body,
*/
public static UnaryExpression makeUnary(ExpressionType expressionType,
Expression expression) {
return new UnaryExpression(expressionType, expression.getType(),
expression);
Type type = expression.getType();
switch (expressionType) {
case Negate:
if (type == byte.class || type == short.class) {
type = int.class;
}
}
return new UnaryExpression(expressionType, type, expression);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,20 @@ public class ExpressionTest {
Expressions.constant(true),
Expressions.constant(0),
Expressions.constant(null)).getType());

// In Java, "-" applied to short and byte yield int.
assertEquals(double.class,
Expressions.negate(Expressions.constant((double) 1)).getType());
assertEquals(float.class,
Expressions.negate(Expressions.constant((float) 1)).getType());
assertEquals(long.class,
Expressions.negate(Expressions.constant((long) 1)).getType());
assertEquals(int.class,
Expressions.negate(Expressions.constant(1)).getType());
assertEquals(int.class,
Expressions.negate(Expressions.constant((short) 1)).getType());
assertEquals(int.class,
Expressions.negate(Expressions.constant((byte) 1)).getType());
}

@Test public void testCompile() throws NoSuchMethodException {
Expand Down

0 comments on commit 3c67a60

Please sign in to comment.