Skip to content

Commit

Permalink
[GLUTEN-8108][VL] Correct the logic of null on failure behavior for t…
Browse files Browse the repository at this point in the history
…ry cast (#8107)
  • Loading branch information
acvictor authored Dec 19, 2024
1 parent 0e9aba4 commit 0345fa5
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1477,4 +1477,42 @@ abstract class ScalarFunctionsValidateSuite extends FunctionsValidateSuite {
checkGlutenOperatorMatch[FilterExecTransformer](df)
}
}

testWithSpecifiedSparkVersion("Test try_cast", Some("3.4")) {
withTempView("try_cast_table") {
withTempPath {
path =>
Seq[(String)](("123456"), ("000A1234"))
.toDF("str")
.write
.parquet(path.getCanonicalPath)
spark.read.parquet(path.getCanonicalPath).createOrReplaceTempView("try_cast_table")
runQueryAndCompare("select try_cast(str as bigint) from try_cast_table") {
checkGlutenOperatorMatch[ProjectExecTransformer]
}
runQueryAndCompare("select try_cast(str as double) from try_cast_table") {
checkGlutenOperatorMatch[ProjectExecTransformer]
}
}
}
}

test("Test cast") {
withTempView("cast_table") {
withTempPath {
path =>
Seq[(String)](("123456"), ("000A1234"))
.toDF("str")
.write
.parquet(path.getCanonicalPath)
spark.read.parquet(path.getCanonicalPath).createOrReplaceTempView("cast_table")
runQueryAndCompare("select cast(str as bigint) from cast_table") {
checkGlutenOperatorMatch[ProjectExecTransformer]
}
runQueryAndCompare("select cast(str as double) from cast_table") {
checkGlutenOperatorMatch[ProjectExecTransformer]
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ public class CastNode implements ExpressionNode, Serializable {
private final TypeNode typeNode;
private final ExpressionNode expressionNode;

public final boolean ansiEnabled;
public final boolean throwOnFailure;

CastNode(TypeNode typeNode, ExpressionNode expressionNode, boolean ansiEnabled) {
CastNode(TypeNode typeNode, ExpressionNode expressionNode, boolean throwOnFailure) {
this.typeNode = typeNode;
this.expressionNode = expressionNode;
this.ansiEnabled = ansiEnabled;
this.throwOnFailure = throwOnFailure;
}

@Override
public Expression toProtobuf() {
Expression.Cast.Builder castBuilder = Expression.Cast.newBuilder();
castBuilder.setType(typeNode.toProtobuf());
castBuilder.setInput(expressionNode.toProtobuf());
if (ansiEnabled) {
if (throwOnFailure) {
// Throw exception on failure.
castBuilder.setFailureBehaviorValue(2);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ public static AggregateFunctionNode makeAggregateFunction(
}

public static CastNode makeCast(
TypeNode typeNode, ExpressionNode expressionNode, boolean ansiEnabled) {
return new CastNode(typeNode, expressionNode, ansiEnabled);
TypeNode typeNode, ExpressionNode expressionNode, boolean throwOnFailure) {
return new CastNode(typeNode, expressionNode, throwOnFailure);
}

public static StringMapNode makeStringMap(Map<String, String> values) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package org.apache.gluten.expression

import org.apache.gluten.backendsapi.BackendsApiManager
import org.apache.gluten.exception.GlutenNotSupportException
import org.apache.gluten.sql.shims.SparkShimLoader
import org.apache.gluten.substrait.`type`.ListNode
import org.apache.gluten.substrait.`type`.MapNode
import org.apache.gluten.substrait.expression.{ExpressionBuilder, ExpressionNode, StructLiteralNode}
Expand All @@ -43,7 +44,10 @@ case class CastTransformer(substraitExprName: String, child: ExpressionTransform
extends UnaryExpressionTransformer {
override def doTransform(args: java.lang.Object): ExpressionNode = {
val typeNode = ConverterUtils.getTypeNode(dataType, original.nullable)
ExpressionBuilder.makeCast(typeNode, child.doTransform(args), original.ansiEnabled)
ExpressionBuilder.makeCast(
typeNode,
child.doTransform(args),
SparkShimLoader.getSparkShims.withAnsiEvalMode(original))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ class VeloxTestSettings extends BackendTestSettings {
.exclude(
"Process Infinity, -Infinity, NaN in case insensitive manner" // +inf not supported in folly.
)
enableSuite[GlutenTryCastSuite]
.exclude(
"Process Infinity, -Infinity, NaN in case insensitive manner" // +inf not supported in folly.
)
.exclude("ANSI mode: Throw exception on casting out-of-range value to byte type")
.exclude("ANSI mode: Throw exception on casting out-of-range value to short type")
.exclude("ANSI mode: Throw exception on casting out-of-range value to int type")
.exclude("ANSI mode: Throw exception on casting out-of-range value to long type")
.exclude("cast from invalid string to numeric should throw NumberFormatException")
.exclude("SPARK-26218: Fix the corner case of codegen when casting float to Integer")
enableSuite[GlutenCollectionExpressionsSuite]
// Rewrite in Gluten to replace Seq with Array
.exclude("Shuffle")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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.spark.sql.catalyst.expressions

import org.apache.spark.sql.GlutenTestsTrait

class GlutenTryCastSuite extends TryCastSuite with GlutenTestsTrait {}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ class VeloxTestSettings extends BackendTestSettings {
.exclude(
"Process Infinity, -Infinity, NaN in case insensitive manner" // +inf not supported in folly.
)
enableSuite[GlutenTryCastSuite]
.exclude(
"Process Infinity, -Infinity, NaN in case insensitive manner" // +inf not supported in folly.
)
.exclude("ANSI mode: Throw exception on casting out-of-range value to byte type")
.exclude("ANSI mode: Throw exception on casting out-of-range value to short type")
.exclude("ANSI mode: Throw exception on casting out-of-range value to int type")
.exclude("ANSI mode: Throw exception on casting out-of-range value to long type")
.exclude("cast from invalid string to numeric should throw NumberFormatException")
.exclude("SPARK-26218: Fix the corner case of codegen when casting float to Integer")
enableSuite[GlutenCollectionExpressionsSuite]
// Rewrite in Gluten to replace Seq with Array
.exclude("Shuffle")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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.spark.sql.catalyst.expressions

import org.apache.spark.sql.GlutenTestsTrait

class GlutenTryCastSuite extends TryCastSuite with GlutenTestsTrait {}

0 comments on commit 0345fa5

Please sign in to comment.