Understanding the Optimized Logical Plan in Spark

DZone 's Guide to

Understanding the Optimized Logical Plan in Spark

Logical plans are applied by Spark's optimizer for query optimization. Read on for a brief overview of the logical plans, their types, and much more.

· Big Data Zone ·
Free Resource

A logical plan is a tree that represents both schema and data. These trees are manipulated and optimized by catalyst framework.

There are three types of logical plans:

  1. Parsed logical plan.

  2. Analyzed logical plan.

  3. Optimized logical plan.

Analyzed logical plans go through a series of rules to resolve. Then, the optimized logical plan is produced. The optimized logical plan normally allows Spark to plug in a set of optimization rules. You can plug in your own rules for the optimized logical plan.

This optimized logical plan is converted to a physical plan for further execution. These plans lie inside the DataFrame API. Now, let's run an example to see these plans and observe the differences between them.


Using our RDD, we created a DataFrame with column names c1, c2, and c3 and data values 1 to 100. To see the plan of a DataFrame, we will be using the explain command. If you run it without the true argument, it gives only the physical plan. The physical plan is always an RDD.

To see all three plans, run the explain command with a true argument.

explain also shows the physical logical plan:


If we have a look here, all plans look the same. Then, what is the difference between the optimized logical plan and analyzed logical plan? Now, run this example with two filters:


Here is the actual difference:

== Analyzed Logical Plan ==
c1: string, c2: string, c3: string
Filter NOT (cast(c2#14 as double) = cast(0 as double))
+- Filter NOT (cast(c1#13 as double) = cast(0 as double))
+- LogicalRDD [c1#13, c2#14, c3#15]== Optimized Logical Plan ==
Filter (((isnotnull(c1#13) && NOT (cast(c1#13 as double) = 0.0)) && isnotnull(c2#14)) && NOT (cast(c2#14 as double) = 0.0))
+- LogicalRDD [c1#13, c2#14, c3#15]

In the optimized logical plan, Spark does optimization itself. It sees that there is no need for two filters. Instead, the same task can be done with only one filter using the and operator, so it does execution in one filter.

big data ,data analytics ,spark ,tutorial ,logical plans ,query optimization

Published at DZone with permission of Anubhav Tarar , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}