Skip to content

Commit 3238e3d

Browse files
committed
[SPARK-26348][SQL][TEST] make sure expression is resolved during test
## What changes were proposed in this pull request? cleanup some tests to make sure expression is resolved during test. ## How was this patch tested? test-only PR Closes apache#23297 from cloud-fan/test. Authored-by: Wenchen Fan <[email protected]> Signed-off-by: Wenchen Fan <[email protected]>
1 parent 05b68d5 commit 3238e3d

File tree

5 files changed

+20
-33
lines changed

5 files changed

+20
-33
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/dsl/package.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ package object dsl {
136136
implicit def longToLiteral(l: Long): Literal = Literal(l)
137137
implicit def floatToLiteral(f: Float): Literal = Literal(f)
138138
implicit def doubleToLiteral(d: Double): Literal = Literal(d)
139-
implicit def stringToLiteral(s: String): Literal = Literal(s)
139+
implicit def stringToLiteral(s: String): Literal = Literal.create(s, StringType)
140140
implicit def dateToLiteral(d: Date): Literal = Literal(d)
141141
implicit def bigDecimalToLiteral(d: BigDecimal): Literal = Literal(d.underlying())
142142
implicit def bigDecimalToLiteral(d: java.math.BigDecimal): Literal = Literal(d)

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ExpressionEvalHelper.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import org.apache.spark.{SparkConf, SparkFunSuite}
2828
import org.apache.spark.serializer.JavaSerializer
2929
import org.apache.spark.sql.Row
3030
import org.apache.spark.sql.catalyst.{CatalystTypeConverters, InternalRow}
31-
import org.apache.spark.sql.catalyst.analysis.{ResolveTimeZone, SimpleAnalyzer}
31+
import org.apache.spark.sql.catalyst.analysis.ResolveTimeZone
3232
import org.apache.spark.sql.catalyst.expressions.codegen._
3333
import org.apache.spark.sql.catalyst.optimizer.SimpleTestOptimizer
3434
import org.apache.spark.sql.catalyst.plans.PlanTestBase
@@ -70,7 +70,9 @@ trait ExpressionEvalHelper extends GeneratorDrivenPropertyChecks with PlanTestBa
7070
private def prepareEvaluation(expression: Expression): Expression = {
7171
val serializer = new JavaSerializer(new SparkConf()).newInstance
7272
val resolver = ResolveTimeZone(new SQLConf)
73-
resolver.resolveTimeZones(serializer.deserialize(serializer.serialize(expression)))
73+
val expr = resolver.resolveTimeZones(expression)
74+
assert(expr.resolved)
75+
serializer.deserialize(serializer.serialize(expr))
7476
}
7577

7678
protected def checkEvaluation(
@@ -296,9 +298,7 @@ trait ExpressionEvalHelper extends GeneratorDrivenPropertyChecks with PlanTestBa
296298
expected: Any,
297299
inputRow: InternalRow = EmptyRow): Unit = {
298300
val plan = Project(Alias(expression, s"Optimized($expression)")() :: Nil, OneRowRelation())
299-
// We should analyze the plan first, otherwise we possibly optimize an unresolved plan.
300-
val analyzedPlan = SimpleAnalyzer.execute(plan)
301-
val optimizedPlan = SimpleTestOptimizer.execute(analyzedPlan)
301+
val optimizedPlan = SimpleTestOptimizer.execute(plan)
302302
checkEvaluationWithoutCodegen(optimizedPlan.expressions.head, expected, inputRow)
303303
}
304304

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/JsonExpressionsSuite.scala

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import org.scalatest.exceptions.TestFailedException
2525
import org.apache.spark.{SparkException, SparkFunSuite}
2626
import org.apache.spark.sql.AnalysisException
2727
import org.apache.spark.sql.catalyst.InternalRow
28-
import org.apache.spark.sql.catalyst.errors.TreeNodeException
28+
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult
2929
import org.apache.spark.sql.catalyst.plans.PlanTestBase
3030
import org.apache.spark.sql.catalyst.util._
3131
import org.apache.spark.sql.internal.SQLConf
@@ -694,11 +694,10 @@ class JsonExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper with
694694
val mapType2 = MapType(IntegerType, CalendarIntervalType)
695695
val schema2 = StructType(StructField("a", mapType2) :: Nil)
696696
val struct2 = Literal.create(null, schema2)
697-
intercept[TreeNodeException[_]] {
698-
checkEvaluation(
699-
StructsToJson(Map.empty, struct2, gmtId),
700-
null
701-
)
697+
StructsToJson(Map.empty, struct2, gmtId).checkInputDataTypes() match {
698+
case TypeCheckResult.TypeCheckFailure(msg) =>
699+
assert(msg.contains("Unable to convert column a of type calendarinterval to JSON"))
700+
case _ => fail("from_json should not work on interval map value type.")
702701
}
703702
}
704703

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/PredicateSuite.scala

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import scala.collection.immutable.HashSet
2424
import org.apache.spark.SparkFunSuite
2525
import org.apache.spark.sql.RandomDataGenerator
2626
import org.apache.spark.sql.catalyst.InternalRow
27+
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult
2728
import org.apache.spark.sql.catalyst.encoders.ExamplePointUDT
2829
import org.apache.spark.sql.catalyst.expressions.codegen.CodegenContext
2930
import org.apache.spark.sql.catalyst.util.{ArrayData, GenericArrayData}
@@ -231,22 +232,12 @@ class PredicateSuite extends SparkFunSuite with ExpressionEvalHelper {
231232
testWithRandomDataGeneration(structType, nullable)
232233
}
233234

234-
// Map types: not supported
235-
for (
236-
keyType <- atomicTypes;
237-
valueType <- atomicTypes;
238-
nullable <- Seq(true, false)) {
239-
val mapType = MapType(keyType, valueType)
240-
val e = intercept[Exception] {
241-
testWithRandomDataGeneration(mapType, nullable)
242-
}
243-
if (e.getMessage.contains("Code generation of")) {
244-
// If the `value` expression is null, `eval` will be short-circuited.
245-
// Codegen version evaluation will be run then.
246-
assert(e.getMessage.contains("cannot generate equality code for un-comparable type"))
247-
} else {
248-
assert(e.getMessage.contains("Exception evaluating"))
249-
}
235+
// In doesn't support map type and will fail the analyzer.
236+
val map = Literal.create(create_map(1 -> 1), MapType(IntegerType, IntegerType))
237+
In(map, Seq(map)).checkInputDataTypes() match {
238+
case TypeCheckResult.TypeCheckFailure(msg) =>
239+
assert(msg.contains("function in does not support ordering on type map"))
240+
case _ => fail("In should not work on map type")
250241
}
251242
}
252243

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/StringExpressionsSuite.scala

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -744,16 +744,14 @@ class StringExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
744744

745745
test("ParseUrl") {
746746
def checkParseUrl(expected: String, urlStr: String, partToExtract: String): Unit = {
747-
checkEvaluation(
748-
ParseUrl(Seq(Literal(urlStr), Literal(partToExtract))), expected)
747+
checkEvaluation(ParseUrl(Seq(urlStr, partToExtract)), expected)
749748
}
750749
def checkParseUrlWithKey(
751750
expected: String,
752751
urlStr: String,
753752
partToExtract: String,
754753
key: String): Unit = {
755-
checkEvaluation(
756-
ParseUrl(Seq(Literal(urlStr), Literal(partToExtract), Literal(key))), expected)
754+
checkEvaluation(ParseUrl(Seq(urlStr, partToExtract, key)), expected)
757755
}
758756

759757
checkParseUrl("spark.apache.org", "http://spark.apache.org/path?query=1", "HOST")
@@ -798,7 +796,6 @@ class StringExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
798796
checkEvaluation(Sentences(nullString, nullString, nullString), null)
799797
checkEvaluation(Sentences(nullString, nullString), null)
800798
checkEvaluation(Sentences(nullString), null)
801-
checkEvaluation(Sentences(Literal.create(null, NullType)), null)
802799
checkEvaluation(Sentences("", nullString, nullString), Seq.empty)
803800
checkEvaluation(Sentences("", nullString), Seq.empty)
804801
checkEvaluation(Sentences(""), Seq.empty)

0 commit comments

Comments
 (0)