Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug of divide and modulus not handling a 0 denominator with byte data type #272

Merged
merged 7 commits into from
Jun 6, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ private static DefaultFunctionResolver addFunction() {
private static DefaultFunctionResolver divideBase(FunctionName functionName) {
return define(functionName,
impl(nullMissingHandling(
(v1, v2) -> new ExprByteValue(v1.byteValue() / v2.byteValue())),
(v1, v2) -> v2.byteValue() == 0 ? ExprNullValue.of() :
new ExprByteValue(v1.byteValue() / v2.byteValue())),
BYTE, BYTE, BYTE),
impl(nullMissingHandling(
(v1, v2) -> v2.shortValue() == 0 ? ExprNullValue.of() :
Expand Down Expand Up @@ -149,7 +150,8 @@ private static DefaultFunctionResolver divideFunction() {
private static DefaultFunctionResolver modulusBase(FunctionName functionName) {
return define(functionName,
impl(nullMissingHandling(
(v1, v2) -> new ExprByteValue(v1.byteValue() % v2.byteValue())),
(v1, v2) -> v2.byteValue() == 0 ? ExprNullValue.of() :
new ExprByteValue(v1.byteValue() % v2.byteValue())),
BYTE, BYTE, BYTE),
impl(nullMissingHandling(
(v1, v2) -> v2.shortValue() == 0 ? ExprNullValue.of() :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void mod(ExprValue op1, ExprValue op2) {
assertEquals(String.format("mod(%s, %s)", op1.toString(), op2.toString()),
expression.toString());

expression = DSL.mod(literal(op1), literal(new ExprShortValue(0)));
expression = DSL.mod(literal(op1), literal(new ExprByteValue(0)));
matthewryanwells marked this conversation as resolved.
Show resolved Hide resolved
assertTrue(expression.valueOf(valueEnv()).isNull());
assertEquals(String.format("mod(%s, 0)", op1.toString()), expression.toString());
}
Expand All @@ -128,7 +128,7 @@ public void modulus(ExprValue op1, ExprValue op2) {
assertEquals(String.format("%%(%s, %s)", op1.toString(), op2.toString()),
expression.toString());

expression = DSL.modulus(literal(op1), literal(new ExprShortValue(0)));
expression = DSL.modulus(literal(op1), literal(new ExprByteValue(0)));
assertTrue(expression.valueOf(valueEnv()).isNull());
assertEquals(String.format("%%(%s, 0)", op1.toString()), expression.toString());
}
Expand All @@ -144,7 +144,7 @@ public void modulusFunction(ExprValue op1, ExprValue op2) {
assertEquals(String.format("modulus(%s, %s)", op1.toString(), op2.toString()),
expression.toString());

expression = DSL.modulusFunction(literal(op1), literal(new ExprShortValue(0)));
expression = DSL.modulusFunction(literal(op1), literal(new ExprByteValue(0)));
assertTrue(expression.valueOf(valueEnv()).isNull());
assertEquals(String.format("modulus(%s, 0)", op1.toString()), expression.toString());
}
Expand Down Expand Up @@ -183,7 +183,7 @@ public void divide(ExprValue op1, ExprValue op2) {
assertEquals(String.format("/(%s, %s)", op1.toString(), op2.toString()),
expression.toString());

expression = DSL.divide(literal(op1), literal(new ExprShortValue(0)));
expression = DSL.divide(literal(op1), literal(new ExprByteValue(0)));
assertTrue(expression.valueOf(valueEnv()).isNull());
assertEquals(String.format("/(%s, 0)", op1.toString()), expression.toString());
}
Expand All @@ -199,7 +199,7 @@ public void divideFunction(ExprValue op1, ExprValue op2) {
assertEquals(String.format("divide(%s, %s)", op1.toString(), op2.toString()),
expression.toString());

expression = DSL.divideFunction(literal(op1), literal(new ExprShortValue(0)));
expression = DSL.divideFunction(literal(op1), literal(new ExprByteValue(0)));
assertTrue(expression.valueOf(valueEnv()).isNull());
assertEquals(String.format("divide(%s, 0)", op1.toString()), expression.toString());
}
Expand Down