EXPLAIN

Description

The EXPLAIN statement is used to provide logical/physical plans for an input statement. By default, this clause provides information about a physical plan only.

Syntax

  1. EXPLAIN [ EXTENDED | CODEGEN | COST | FORMATTED ] statement

Parameters

  • EXTENDED

    Generates parsed logical plan, analyzed logical plan, optimized logical plan and physical plan. Parsed Logical plan is a unresolved plan that extracted from the query. Analyzed logical plans transforms which translates unresolvedAttribute and unresolvedRelation into fully typed objects. The optimized logical plan transforms through a set of optimization rules, resulting in the physical plan.

  • CODEGEN

    Generates code for the statement, if any and a physical plan.

  • COST

    If plan node statistics are available, generates a logical plan and the statistics.

  • FORMATTED

    Generates two sections: a physical plan outline and node details.

  • statement

    Specifies a SQL statement to be explained.

Examples

  1. -- Default Output
  2. EXPLAIN select k, sum(v) from values (1, 2), (1, 3) t(k, v) group by k;
  3. +----------------------------------------------------+
  4. | plan|
  5. +----------------------------------------------------+
  6. | == Physical Plan ==
  7. *(2) HashAggregate(keys=[k#33], functions=[sum(cast(v#34 as bigint))])
  8. +- Exchange hashpartitioning(k#33, 200), true, [id=#59]
  9. +- *(1) HashAggregate(keys=[k#33], functions=[partial_sum(cast(v#34 as bigint))])
  10. +- *(1) LocalTableScan [k#33, v#34]
  11. |
  12. +----------------------------------------------------
  13. -- Using Extended
  14. EXPLAIN EXTENDED select k, sum(v) from values (1, 2), (1, 3) t(k, v) group by k;
  15. +----------------------------------------------------+
  16. | plan|
  17. +----------------------------------------------------+
  18. | == Parsed Logical Plan ==
  19. 'Aggregate ['k], ['k, unresolvedalias('sum('v), None)]
  20. +- 'SubqueryAlias `t`
  21. +- 'UnresolvedInlineTable [k, v], [List(1, 2), List(1, 3)]
  22. == Analyzed Logical Plan ==
  23. k: int, sum(v): bigint
  24. Aggregate [k#47], [k#47, sum(cast(v#48 as bigint)) AS sum(v)#50L]
  25. +- SubqueryAlias `t`
  26. +- LocalRelation [k#47, v#48]
  27. == Optimized Logical Plan ==
  28. Aggregate [k#47], [k#47, sum(cast(v#48 as bigint)) AS sum(v)#50L]
  29. +- LocalRelation [k#47, v#48]
  30. == Physical Plan ==
  31. *(2) HashAggregate(keys=[k#47], functions=[sum(cast(v#48 as bigint))], output=[k#47, sum(v)#50L])
  32. +- Exchange hashpartitioning(k#47, 200), true, [id=#79]
  33. +- *(1) HashAggregate(keys=[k#47], functions=[partial_sum(cast(v#48 as bigint))], output=[k#47, sum#52L])
  34. +- *(1) LocalTableScan [k#47, v#48]
  35. |
  36. +----------------------------------------------------+
  37. -- Using Formatted
  38. EXPLAIN FORMATTED select k, sum(v) from values (1, 2), (1, 3) t(k, v) group by k;
  39. +----------------------------------------------------+
  40. | plan|
  41. +----------------------------------------------------+
  42. | == Physical Plan ==
  43. * HashAggregate (4)
  44. +- Exchange (3)
  45. +- * HashAggregate (2)
  46. +- * LocalTableScan (1)
  47. (1) LocalTableScan [codegen id : 1]
  48. Output: [k#19, v#20]
  49. (2) HashAggregate [codegen id : 1]
  50. Input: [k#19, v#20]
  51. (3) Exchange
  52. Input: [k#19, sum#24L]
  53. (4) HashAggregate [codegen id : 2]
  54. Input: [k#19, sum#24L]
  55. |
  56. +----------------------------------------------------+