Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/pr_build_linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ jobs:
org.apache.comet.CometCsvExpressionSuite
org.apache.comet.CometJsonExpressionSuite
org.apache.comet.CometDateTimeUtilsSuite
org.apache.comet.SparkErrorConverterSuite
org.apache.comet.expressions.conditional.CometIfSuite
org.apache.comet.expressions.conditional.CometCoalesceSuite
org.apache.comet.expressions.conditional.CometCaseWhenSuite
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/pr_build_macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ jobs:
org.apache.comet.CometJsonExpressionSuite
org.apache.comet.CometCsvExpressionSuite
org.apache.comet.CometDateTimeUtilsSuite
org.apache.comet.SparkErrorConverterSuite
org.apache.comet.expressions.conditional.CometIfSuite
org.apache.comet.expressions.conditional.CometCoalesceSuite
org.apache.comet.expressions.conditional.CometCaseWhenSuite
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ trait ShimSparkErrorConverter {
private def sqlCtx(context: Array[QueryContext]): SQLQueryContext =
context.headOption.map(_.asInstanceOf[SQLQueryContext]).getOrElse(null)

private def parseFloatLiteral(value: String): Float = {
value.toLowerCase match {
case "inf" | "+inf" | "infinity" | "+infinity" => Float.PositiveInfinity
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this issue is focused on inf but do we need to do anything with nan as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Covered nan as well.

case "-inf" | "-infinity" => Float.NegativeInfinity
case "nan" | "+nan" | "-nan" => Float.NaN
case _ => value.toFloat
}
}

private def parseDoubleLiteral(value: String): Double = {
value.toLowerCase match {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In conversion_funcs/numeric.rs:spark_cast_nonintegral_numeric_to_integral the calls to cast_float_to_int16_down and cast_float_to_int32_up explicitly format the string with "{:e}D" (a suffix D).
I think inf and nan will get this D suffix and the resultant string infD or nanD would not match.
The unit tests below will not catch this either.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's handled now. Please review again.

case "inf" | "+inf" | "infinity" | "+infinity" | "infd" => Double.PositiveInfinity
case "-inf" | "-infinity" | "-infd" => Double.NegativeInfinity
case "nan" | "+nan" | "-nan" | "nand" | "-nand" => Double.NaN
case _ => value.toDouble
}
}

def convertErrorType(
errorType: String,
errorClass: String,
Expand Down Expand Up @@ -207,8 +225,8 @@ trait ShimSparkErrorConverter {
case LongType =>
val cleanStr = if (valueStr.endsWith("L")) valueStr.dropRight(1) else valueStr
cleanStr.toLong
case FloatType => valueStr.toFloat
case DoubleType => valueStr.toDouble
case FloatType => parseFloatLiteral(valueStr)
case DoubleType => parseDoubleLiteral(valueStr)
case StringType => UTF8String.fromString(valueStr)
case _ => valueStr
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ trait ShimSparkErrorConverter {
private def sqlCtx(context: Array[QueryContext]): SQLQueryContext =
context.headOption.map(_.asInstanceOf[SQLQueryContext]).getOrElse(null)

private def parseFloatLiteral(value: String): Float = {
value.toLowerCase match {
case "inf" | "+inf" | "infinity" | "+infinity" => Float.PositiveInfinity
case "-inf" | "-infinity" => Float.NegativeInfinity
case "nan" | "+nan" | "-nan" => Float.NaN
case _ => value.toFloat
}
}

private def parseDoubleLiteral(value: String): Double = {
value.toLowerCase match {
case "inf" | "+inf" | "infinity" | "+infinity" | "infd" => Double.PositiveInfinity
case "-inf" | "-infinity" | "-infd" => Double.NegativeInfinity
case "nan" | "+nan" | "-nan" | "nand" | "-nand" => Double.NaN
case _ => value.toDouble
}
}

def convertErrorType(
errorType: String,
errorClass: String,
Expand Down Expand Up @@ -205,8 +223,8 @@ trait ShimSparkErrorConverter {
case LongType =>
val cleanStr = if (valueStr.endsWith("L")) valueStr.dropRight(1) else valueStr
cleanStr.toLong
case FloatType => valueStr.toFloat
case DoubleType => valueStr.toDouble
case FloatType => parseFloatLiteral(valueStr)
case DoubleType => parseDoubleLiteral(valueStr)
case StringType => UTF8String.fromString(valueStr)
case _ => valueStr
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ object ShimSparkErrorConverter {
*/
trait ShimSparkErrorConverter {

private def parseFloatLiteral(value: String): Float = {
value.toLowerCase match {
case "inf" | "+inf" | "infinity" | "+infinity" => Float.PositiveInfinity
case "-inf" | "-infinity" => Float.NegativeInfinity
case "nan" | "+nan" | "-nan" => Float.NaN
case _ => value.toFloat
}
}

private def parseDoubleLiteral(value: String): Double = {
value.toLowerCase match {
case "inf" | "+inf" | "infinity" | "+infinity" | "infd" => Double.PositiveInfinity
case "-inf" | "-infinity" | "-infd" => Double.NegativeInfinity
case "nan" | "+nan" | "-nan" | "nand" | "-nand" => Double.NaN
case _ => value.toDouble
}
}

/**
* Convert error type string and parameters to appropriate Spark exception. Version-specific
* implementations call the correct QueryExecutionErrors.* methods.
Expand Down Expand Up @@ -213,8 +231,8 @@ trait ShimSparkErrorConverter {
// Strip "L" suffix for BIGINT literals
val cleanStr = if (valueStr.endsWith("L")) valueStr.dropRight(1) else valueStr
cleanStr.toLong
case FloatType => valueStr.toFloat
case DoubleType => valueStr.toDouble
case FloatType => parseFloatLiteral(valueStr)
case DoubleType => parseDoubleLiteral(valueStr)
case StringType => UTF8String.fromString(valueStr)
case _ => valueStr // Fallback to string
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.comet

import org.scalatest.funsuite.AnyFunSuite

class SparkErrorConverterSuite extends AnyFunSuite {
private def castOverflowError(fromType: String, value: String): Throwable = {
SparkErrorConverter
.convertErrorType(
"CastOverFlow",
"CAST_OVERFLOW",
Map("fromType" -> fromType, "toType" -> "INT", "value" -> value),
Array.empty,
null)
.getOrElse(fail("Expected CastOverFlow to be converted to a Spark exception"))
}

test("CastOverFlow conversion handles float inf") {
val err = castOverflowError("FLOAT", "inf")
assert(!err.isInstanceOf[NumberFormatException])
assert(err.getMessage.contains("Infinity"))
}

test("CastOverFlow conversion handles float -inf") {
val err = castOverflowError("FLOAT", "-inf")
assert(!err.isInstanceOf[NumberFormatException])
assert(err.getMessage.contains("-Infinity"))
}

test("CastOverFlow conversion handles double inf") {
val err = castOverflowError("DOUBLE", "inf")
assert(!err.isInstanceOf[NumberFormatException])
assert(err.getMessage.contains("Infinity"))
}

test("CastOverFlow conversion handles double -inf") {
val err = castOverflowError("DOUBLE", "-inf")
assert(!err.isInstanceOf[NumberFormatException])
assert(err.getMessage.contains("-Infinity"))
}

test("CastOverFlow conversion handles float nan") {
val err = castOverflowError("FLOAT", "nan")
assert(!err.isInstanceOf[NumberFormatException])
assert(err.getMessage.toLowerCase.contains("nan"))
}

test("CastOverFlow conversion handles double nan") {
val err = castOverflowError("DOUBLE", "nan")
assert(!err.isInstanceOf[NumberFormatException])
assert(err.getMessage.toLowerCase.contains("nan"))
}
}
Loading