Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ trait InvokeLike extends Expression with NonSQLExpression with ImplicitCastInput

def propagateNull: Boolean

override def foldable: Boolean = children.forall(_.foldable) && deterministic
Copy link
Contributor

Choose a reason for hiding this comment

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

shall we remove deterministic? I don't see other expressions require it when defining foldable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

shall we remove deterministic? I don't see other expressions require it when defining foldable.

If it's not deterministic, such as Invoke return a rand result, if we fold this, we may return incorrect answer

protected lazy val needNullCheck: Boolean = needNullCheckForIndex.contains(true)
protected lazy val needNullCheckForIndex: Array[Boolean] =
arguments.map(a => a.nullable && (propagateNull ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ import org.apache.spark.sql.catalyst.analysis.{EliminateSubqueryAliases, Unresol
import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.catalyst.dsl.plans._
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.expressions.objects.{Invoke, NewInstance, StaticInvoke}
import org.apache.spark.sql.catalyst.plans.PlanTest
import org.apache.spark.sql.catalyst.plans.logical.{LocalRelation, LogicalPlan}
import org.apache.spark.sql.catalyst.rules.RuleExecutor
import org.apache.spark.sql.catalyst.util.GenericArrayData
import org.apache.spark.sql.types._
import org.apache.spark.unsafe.types.ByteArray

class ConstantFoldingSuite extends PlanTest {

Expand Down Expand Up @@ -299,4 +302,41 @@ class ConstantFoldingSuite extends PlanTest {

comparePlans(optimized, correctAnswer)
}

test("SPARK-37907: InvokeLike support ConstantFolding") {
val originalQuery =
testRelation
.select(
StaticInvoke(
classOf[ByteArray],
BinaryType,
"lpad",
Seq(Literal("Spark".getBytes), Literal(7), Literal("W".getBytes)),
Seq(BinaryType, IntegerType, BinaryType),
returnNullable = false).as("c1"),
Invoke(
Literal.create("a", StringType),
"substring",
StringType,
Seq(Literal(0), Literal(1))).as("c2"),
NewInstance(
cls = classOf[GenericArrayData],
arguments = Literal.fromObject(List(1, 2, 3)) :: Nil,
inputTypes = Nil,
propagateNull = false,
dataType = ArrayType(IntegerType),
outerPointer = None).as("c3"))

val optimized = Optimize.execute(originalQuery.analyze)

val correctAnswer =
testRelation
.select(
Literal("WWSpark".getBytes()).as("c1"),
Literal.create("a", StringType).as("c2"),
Literal.create(new GenericArrayData(List(1, 2, 3)), ArrayType(IntegerType)).as("c3"))
.analyze

comparePlans(optimized, correctAnswer)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ trait CharVarcharTestSuite extends QueryTest with SQLTestUtils {
sql(s"CREATE TABLE t(c STRUCT<c: $typeName(5)>) USING $format")
sql("INSERT INTO t SELECT struct(null)")
checkAnswer(spark.table("t"), Row(Row(null)))
val e = intercept[SparkException](sql("INSERT INTO t SELECT struct('123456')"))
assert(e.getCause.getMessage.contains(s"Exceeds char/varchar type length limitation: 5"))
val e = intercept[RuntimeException](sql("INSERT INTO t SELECT struct('123456')"))
assert(e.getMessage.contains(s"Exceeds char/varchar type length limitation: 5"))
}
}

Expand Down