[SPARK-3501] [SQL] Fix the bug of Hive SimpleUDF creates unnecessary type cast

When do the query like:
```
select datediff(cast(value as timestamp), cast('2002-03-21 00:00:00' as timestamp)) from src;
```
SparkSQL will raise exception:
```
[info] scala.MatchError: TimestampType (of class org.apache.spark.sql.catalyst.types.TimestampType$)
[info] at org.apache.spark.sql.catalyst.expressions.Cast.castToTimestamp(Cast.scala:77)
[info] at org.apache.spark.sql.catalyst.expressions.Cast.cast$lzycompute(Cast.scala:251)
[info] at org.apache.spark.sql.catalyst.expressions.Cast.cast(Cast.scala:247)
[info] at org.apache.spark.sql.catalyst.expressions.Cast.eval(Cast.scala:263)
[info] at org.apache.spark.sql.catalyst.optimizer.ConstantFolding$$anonfun$apply$5$$anonfun$applyOrElse$2.applyOrElse(Optimizer.scala:217)
[info] at org.apache.spark.sql.catalyst.optimizer.ConstantFolding$$anonfun$apply$5$$anonfun$applyOrElse$2.applyOrElse(Optimizer.scala:210)
[info] at org.apache.spark.sql.catalyst.trees.TreeNode.transformDown(TreeNode.scala:144)
[info] at org.apache.spark.sql.catalyst.trees.TreeNode$$anonfun$4$$anonfun$apply$2.apply(TreeNode.scala:180)
[info] at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
[info] at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
```
Okay, I see.  I would argue that's a bug in the casting operator, not a
reason to change the ordering here though.
On Sep 14, 2014 8:09 PM, "Cheng Hao" <notifications@github.com> wrote:

> In
> sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
>
> > @@ -32,12 +32,12 @@ object Optimizer extends RuleExecutor[LogicalPlan] {
> >      Batch("Combine Limits", FixedPoint(100),
> >        CombineLimits) ::
> >      Batch("ConstantFolding", FixedPoint(100),
> > +      SimplifyCasts,
>
> I just added an new unit test SELECT CAST(CAST('123' AS binary) AS
> binary) FROM src LIMIT 1, this test will fail because the ConstantFolding
> will try to compute the foldable expression prior to SimplifyCasts.
> Sorry, I should add this test case earlier.
>
> —
> Reply to this email directly or view it on GitHub
> <https://github.com/apache/spark/pull/2368/files#r17525387>.
>