scala> spark.range(1).select(lit(3) > 2).explain(true)
...
TRACE SparkOptimizer:
=== Applying Rule org.apache.spark.sql.catalyst.optimizer.ConstantFolding ===
!Project [(3 > 2) AS (3 > 2)#3] Project [true AS (3 > 2)#3]
+- Range (0, 1, step=1, splits=Some(8)) +- Range (0, 1, step=1, splits=Some(8))
Constant Folding
ConstantFolding
is a operator optimization rule in Catalyst that replaces expressions that can be statically evaluated with their equivalent literal values.
ConstantFolding
object is a logical plan optimization rule in Operator Optimizations batch in the base Optimizer.
scala> spark.range(1).select('id + 'id > 0).explain(true)
...
TRACE SparkOptimizer:
=== Applying Rule org.apache.spark.sql.catalyst.optimizer.ConstantFolding ===
!Project [((id#7L + id#7L) > cast(0 as bigint)) AS ((id + id) > 0)#10] Project [((id#7L + id#7L) > 0) AS ((id + id) > 0)#10]
+- Range (0, 1, step=1, splits=Some(8)) +- Range (0, 1, step=1, splits=Some(8))