Skip to content

Commit

Permalink
Merge pull request #7982 from Mats-SX/3.0-fix-toint-type-bug
Browse files Browse the repository at this point in the history
Fix type-checking bug for `toInt()`
  • Loading branch information
Stefan Plantikow committed Sep 21, 2016
2 parents deb953b + 6b10720 commit c7d2446
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ class FunctionsAcceptanceTest extends ExecutionEngineFunSuite with NewPlannerTes
result.toList should equal(List(Map("int_numbers" -> List(2, 2, 1))))
}

test("toInt on a complex-typed expression") {
val result = executeWithAllPlannersAndCompatibilityMode(
// this subtraction expression will get the strange type 'Float, Integer'
"RETURN toInt(1 - {p0}) AS result",
"p0" -> 1
)

result.toList should equal(List(Map("result" -> 0)))
}

test("toInt should fail statically on type boolean") {
val query = "RETURN toInt(true)"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,16 @@ case object ToInt extends Function {
invocation.specifyType(CTInteger)

private def checkTypeOfArgument(invocation: FunctionInvocation): SemanticCheck = (s: SemanticState) => {
val e = invocation.args.head

s.expressionType(e).specified match {
case CTFloat.invariant |
CTInteger.invariant |
CTString.invariant |
CTNumber.invariant |
CTAny.invariant => SemanticCheckResult.success(s)

case
CTAny.covariant => SemanticCheckResult.success(s)
val argument = invocation.args.head
val specifiedType = s.expressionType(argument).specified
val correctType = Seq(CTFloat, CTInteger, CTString, CTNumber, CTAny).foldLeft(false) {
case (acc, t) => acc || specifiedType.contains(t)
}

case x =>
val message = s"Type mismatch: expected Number or String but was ${x.mkString(", ")}"
SemanticCheckResult.error(s, SemanticError(message, invocation.args.head.position))
if (correctType) SemanticCheckResult.success(s)
else {
val message = s"Type mismatch: expected Number or String but was ${specifiedType.mkString(", ")}"
SemanticCheckResult.error(s, SemanticError(message, argument.position))
}
}
}

0 comments on commit c7d2446

Please sign in to comment.