Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Free O'Reilly eBook: Learn how to architect always-on apps that scale. Brought to you by Mesosphere DC/OS–the premier platform for containers and big data.

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.

spark.png

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:

spark2.png

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:

spark3.png

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.

Easily deploy & scale your data pipelines in clicks. Run Spark, Kafka, Cassandra + more on shared infrastructure and blow away your data silos. Learn how with Mesosphere DC/OS.

Topics:
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.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}