-
Notifications
You must be signed in to change notification settings - Fork 295
fix: handle inf/-inf/nan in ShimSparkErrorConverter cast overflow #3768
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
|
@@ -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 | ||
| } | ||
|
|
||
| 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")) | ||
| } | ||
| } |
There was a problem hiding this comment.
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
infbut do we need to do anything withnanas well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Covered
nanas well.