Hive Join Optimizations MR and Spark Szehon Ho

  • Slides: 30
Download presentation
Hive Join Optimizations: MR and Spark Szehon Ho @hkszehon Cloudera Software Engineer, Hive Committer

Hive Join Optimizations: MR and Spark Szehon Ho @hkszehon Cloudera Software Engineer, Hive Committer and PMC

Background • Joins were one of the more challenging pieces of the Hive on

Background • Joins were one of the more challenging pieces of the Hive on Spark project • Many joins added throughout the years in Hive • • • Common (Reduce-side) Join Broadcast (Map-side) Join Bucket Map Join Sort Merge Bucket Join Skew Join More to come • Share our research on how different joins work in MR • Share how joins are implemented in Hive on Spark © 2014 Cloudera, Inc. All rights reserved. 2

Common Join • Known as Reduce-side join • Background: Hive (Equi) Join High-Level Requirement:

Common Join • Known as Reduce-side join • Background: Hive (Equi) Join High-Level Requirement: • Scan n tables • Rows with same value on join. Keys are combined -> Result • Process: • Mapper: scan, process n tables and produces Hive. Key = {Join. Key, Table. Alias}, Value = {row} • Shuffle Phase: • Join. Key used to hash rows of same join. Key value to same reducer • Table. Alias makes sure reducers gets rows in sorted order by origin table • Reducer: Join operator combine rows from different tables to produce Join. Result • Worst performance • All table data is shuffled around © 2014 Cloudera, Inc. All rights reserved. 3

Common Join • Ex: Join by City. Id • City. Id=1 goes to First

Common Join • Ex: Join by City. Id • City. Id=1 goes to First Reducer (sorted by table) • City. Id=2 goes to Second Reducer (sorted by table) Mapper (Reduce Sink) Cities: C Sales: S Result Reducer (Join Operator) Hive. Key City. Id City. Name {1, C} City. Id Table. Alias Row Value 1 San Jose {1, S} 1 C San Jose 2 SF 1 S 500 City. Id Sales 1 500 2 600 2 400 {1, San Jose, 500} City. Id Table. Alias Row Value 2 C SF {2, S} 2 S 600 {2, S} 2 S 400 {2, C} {2, San Jose, 600} {2, San Francisco, 400} © 2014 Cloudera, Inc. All rights reserved. 4

Common Join (MR) Produces HIve. Key TS Sel/FIl RS Operator Tree Join TS Sel/FIl

Common Join (MR) Produces HIve. Key TS Sel/FIl RS Operator Tree Join TS Sel/FIl File. Sink RS Map. Red. Work MR Work Tree Map. Work Reduce. Work TS Sel/FIl RS Join Execute on Mapper File. Sink Sel/FIl Execute on Reducer © 2014 Cloudera, Inc. All rights reserved. 5

Common Join In Spark: • Table = RDD • Data Operation = RDD transformation

Common Join In Spark: • Table = RDD • Data Operation = RDD transformation Spark. Work Map. Work Spark Work Tree TS Sel/FIl Reduce. Sink Join Map. Work TS Sel/FIl Reduce. Work Sel/FIl Reduce. Sink map. Partition() Table RDD Map. Work Spark RDD Transforms union() Repartition. And. Sort Within. Partitions() map. Partition() Reduce. Work map. Partition() Table RDD Map. Work Shuffle-Sort Transform (SPARK-2978) © 2014 Cloudera, Inc. All rights reserved. 6

Map. Join • Known as Broadcast join • Create hashtable from (n-1) small table(s)

Map. Join • Known as Broadcast join • Create hashtable from (n-1) small table(s) keyed by Joinkey, broadcasted them in-memory to mappers processing big-table. • Each big-table mapper does lookup of joinkey in small table(s) hashmap -> Join Result • Ex: Join by “City. Id” Small Table (Hash. Table) City. Id City. Name 1 San Jose 2 San Francisco Big Table (Mapper) City. Id Sales 1 500 2 600 2 400 City. Id Sales 1 700 2 200 2 100 {1, San Jose, 500} {2, San Francisco, 600} {2, San Francisco, 400} {1, San Jose, 700} {2, San Francisco, 200} © 2014 Cloudera, Inc. All rights reserved. 7

Map. Join Overview (MR) HS 2 Local. Work Node 1 (Small Table Data) Node

Map. Join Overview (MR) HS 2 Local. Work Node 1 (Small Table Data) Node 2 (Small Table Data) Node 1 Node 3 Node 4 Node 3 1. Local Work read, process small table HS 2 Local. Work Node 2 Node 4 2. Create/Upload hashtable file to distributed cache Node 1 (Big Table) Node 3 (Big Table) Node 2 Map. Work Node 4 (Big Table) Map. Work 3. Big Table Mapper Reads hash. Table from Distributed Cache • More efficient than common join • Only small-table(s) are moved around © 2014 Cloudera, Inc. All rights reserved. 8

Map. Join Overview (Spark) • Spark Work for Mapjoin very similar to MR Version,

Map. Join Overview (Spark) • Spark Work for Mapjoin very similar to MR Version, use hashtable file with high replication factor • Note 1: We run the small-table processing non-local (parallel) • Note 2: Consideration of Spark broadcast variables for broadcast © 2014 Cloudera, Inc. All rights reserved. 9

Map. Join Decision Implementation • Memory req: N-1 tables need to fit into mapper

Map. Join Decision Implementation • Memory req: N-1 tables need to fit into mapper memory • Two ways Hive decides a mapjoin • Query Hints: • SELECT /*+ MAPJOIN(cities) */ * FROM cities JOIN sales on cities. city. Id=sales. city. Id; • Auto-converesion based on file-size (“hive. auto. convert. join”) • If N-1 small tables smaller than: “hive. mapjoin. smalltable. filesize” © 2014 Cloudera, Inc. All rights reserved. 10

Map. Join Optimizers • Multiple decision-points in query-planning = Different Optimizer Paths • Map.

Map. Join Optimizers • Multiple decision-points in query-planning = Different Optimizer Paths • Map. Join Optimizers are processors that convert Query Plan • “Logical (Compile-time) optimizers” modify a operator-tree, if known at compile-time how to optimize to mapjoin • “Physical (Runtime) optimizers” modify a physical work (Map. Red. Work, Tez. Work, Spark. Work), involves more-complex conditional task, when Hive has no info at compile-time Logical Optimizers => TS (Small Table) Sel/FIl TS (Big Table) Sel/FIl RS Map. Join Sel/FIl File. Sink Physical Optimizers => Map. Red. Work Map. Red. Local. Work TS (Small Table) Sel/FIl Hash. Table. Sin k Local Work TS (Big Table) Map. Work Hash. Table. Dummy Sel/FI l Sel/FI File. Sink Map. Join © 2014 Cloudera, l Inc. All rights reserved. 11

Map. Join Optimizers (MR) • Query Hint: Big/Small Table(s) known at compile-time from hints.

Map. Join Optimizers (MR) • Query Hint: Big/Small Table(s) known at compile-time from hints. • Logical Optimizer: Map. Join. Processor • Auto-conversion: Table size not known at compile-time • Physical Optimizer: Common. Join. Resolver, Map. Join. Resolver. • Create Conditional Tasks with all big/small table possibilities: one picked at runtime • Noconditional mode: For some cases, table file-size is known at compile-time and can skip conditional task, but cannot do this for all queries (join of intermediate results. . ) Condition 1 (Cities Small) Map. Red. Local. Work Cities to Hash. Table Condition 2 (Sales Small) Map. Red. Local. Work Sales to Hash. Table Condition 3 (Neither Small Enough) Map. Red. Local. Work Cities Sales Join Map. Red. Work Sales to Map. Join Cities to Map. Join © 2014 Cloudera, Inc. All rights reserved. 12

Map. Join Optimizers: Spark • Spark Plan: Support for both query-hints and auto-conversion decisions.

Map. Join Optimizers: Spark • Spark Plan: Support for both query-hints and auto-conversion decisions. • Query Hints • Logical Optimizer: Reuse Map. Join. Processor • Auto-conversion: Use statistics annotated on operators that give estimated output size (like Tez, CBO), so big/small tables known at compile-time too • Logical Optimizer: Spark. Map. Join. Optimizer Logical Optimizers => Map. Join Operators TS (Small Table) Sel/FIl TS (Big Table) Sel/FIl RS Map. Join Sel/FIl File. Sink © 2014 Cloudera, Inc. All rights reserved. 13

Bucket. Map. Join • Bucketed tables: rows hash to different bucket files based on

Bucket. Map. Join • Bucketed tables: rows hash to different bucket files based on bucket-key • CREATE TABLE cities (cityid int, value string) CLUSTERED BY (city. Id) INTO 2 BUCKETS; • Join tables bucketed on join key: For each bucket of table, rows with matching join. Key values will be in corresponding bucket of other table • Like Mapjoin, but big-table mappers load to memory only relevant small-table bucket’s hashmap • Ex: Bucketed by “City. Id”, Join by “City. Id” City. Id City. Name 3 New York 1 San Jose City. Id City. Name 2 San Francisco 4 Los Angeles City. Id Sales 1 500 3 6000 1 400 City. Id Sales 4 50 2 200 4 45 {1, San Jose, 500} {3, New York, 6000} {1, San Jose, 400} {4, Los Angeles, 50} {2, San Francisco, 200} {4, Los Angeles, 45} © 2014 Cloudera, Inc. All rights reserved. 14

Bucket Map. Join Execution • Very similar to Map. Join • Hash. Table. Sink

Bucket Map. Join Execution • Very similar to Map. Join • Hash. Table. Sink (small-table) writes per-bucket instead of per-table • Hash. Table. Loader (big-table mapper) reads per-bucket © 2014 Cloudera, Inc. All rights reserved. 15

Bucket. Map. Join Optimizers (MR, Spark) • Memory Req: Corresponding bucket(s) of small table(s)

Bucket. Map. Join Optimizers (MR, Spark) • Memory Req: Corresponding bucket(s) of small table(s) fit into memory of big table mapper (less than mapjoin) • MR: • Query hint && “hive. optimize. bucket. mapjoin”, all information known at compile-time • Logical Optimizer: Map. Join. Processor (intermediate operator tree) • Spark: • Query hint && “hive. optimize. buckert. mapjoin” • Logical Optimizer: Reuse Map. Join. Processor • Auto-Trigger, done via stats like mapjoin (size calculation estimated to be size/num. Buckets) • Logical Optimizer: Spark. Map. Join. Optimizer, does size calculation of small tables via statistics, divides original number by num. Buckets © 2014 Cloudera, Inc. All rights reserved. 16

SMB Join • CREATE TABLE cities (cityid int, city. Name string) CLUSTERED BY (city.

SMB Join • CREATE TABLE cities (cityid int, city. Name string) CLUSTERED BY (city. Id) SORTED BY (city. Id) INTO 2 BUCKETS; • Join tables are bucketed and sorted (per bucket) • This allows sort-merge join per bucket. • Advance table until find a match City. Id City. Name 1 San Jose 3 New York City. Id City. Name 2 San Francisco 4 Los Angeles City. Id Sales 1 500 1 400 3 6000 City. Id Sales 2 200 4 50 4 45 {1, San Jose, 500} {1, San Jose, 400} {3, New York, 6000} {2, San Francisco, 200} {4, Los Angeles, 50} {4, Los Angeles, 45} © 2014 Cloudera, Inc. All rights reserved. 17

SMB Join • Same Execution in MR and Spark • Run mapper process against

SMB Join • Same Execution in MR and Spark • Run mapper process against a “big-table”, which loads corresponding small-table buckets • Mapper reads directly from small-table, no need to create, broadcast small-table hashmap. • No size limit on small table (no need to load table into memory) HS 2 MR: Mappers Spark: Map. Partition() Transform Node: Small Table Bucket 1 Node: Big Table Bucket 1 Map. Work Node: Small Table Bucket 2 Node: Big Table Bucket 2 Map. Work © 2014 Cloudera, Inc. All rights reserved. 18

SMB Join Optimizers: MR • SMB plan needs to identify ‘big-table’: one that mappers

SMB Join Optimizers: MR • SMB plan needs to identify ‘big-table’: one that mappers run against, will load ‘small-tables’. Generally can be determined at compile-time • User gives query-hints to identify small-tables • Triggered by “hive. optimize. bucketmapjoin. sortedmerge” • Logical Optimizer: Sorted. Merge. Bucket. Map. Join. Proc • Auto-trigger: “hive. auto. convert. sortmerge. join. bigtable. selection. policy” class chooses big-table • Triggered by “hive. auto. convert. sortmerge. join” • Logical Optimizer: Sorted. Bucket. Map. Join. Proc Logical Optimizers: SMB Join Operator TS (Small Table) Sel/FIl TS (Big Table) Sel/FIl Dummy. Store SMBMap. Join Sel/FIl File. Sink © 2014 Cloudera, Inc. All rights reserved. 19

SMB Join Optimizers: Spark • Query-hints • Logical Optimizer: Spark. SMBJoin. Hint. Optimizer •

SMB Join Optimizers: Spark • Query-hints • Logical Optimizer: Spark. SMBJoin. Hint. Optimizer • Auto-Conversion • Logical Optimizer: Spark. Sort. Merge. Join. Optimizer Logical Optimizers: SMB Join Operator TS (Small Table) Sel/FIl TS (Big Table) Sel/FIl Dummy. Store SMBMap. Join Sel/FIl File. Sink © 2014 Cloudera, Inc. All rights reserved. 20

SMB vs Map. Join Decision (MR) • SMB->Map. Join path • In many cases,

SMB vs Map. Join Decision (MR) • SMB->Map. Join path • In many cases, mapjoin is faster than SMB join so we choose mapjoin if possible • We spawn 1 mapper bucket = large overhead if table has huge number bucket files • Enabled by “hive. auto. convert. sortmerge. join. to. mapjoin” • Physical Optimizer: Sort. Merge. Join. Resolver Conditional Map. Join Work SMB Join Work Map. Red. Work Map. Join Option Map. Red. Local. Work Map. Join Option SMB Option Map. Red. Local. Work Map. Red. Work © 2014 Cloudera, Inc. All rights reserved. 21

SMB vs Map. Join Decision (Spark) • Make decision at compile-time via stats and

SMB vs Map. Join Decision (Spark) • Make decision at compile-time via stats and config for Mapjoin vs SMB join (can determine mapjoin at compile-time) • Logical Optimizer: Spark. Join. Optimizer • If hive. auto. convert. join && hive. auto. convert. sortmerge. join. to. mapjoin and tables fit into memory, delegate to Map. Join logical optimizers • If SMB enabled && (! hive. auto. convert. sortmerge. join. to. mapjoin or tables do not fit into memory) , delegate to SMB Join logical optimizers. © 2014 Cloudera, Inc. All rights reserved. 22

Skew Join • Skew keys = key with high frequencies, will overwhelm that key’s

Skew Join • Skew keys = key with high frequencies, will overwhelm that key’s reducer in common join • Perform a common join for non-skew keys, and perform map join for skewed keys. • A join B on A. id=B. id, with A skewing for id=1, becomes • A join B on A. id=B. id and A. id!=1 union • A join B on A. id=B. id and A. id=1 • If B doesn’t skew on id=1, then #2 will be a map join. © 2014 Cloudera, Inc. All rights reserved. 23

Skew Join Optimizers (Compile Time, MR) • Skew keys identified by: create table …

Skew Join Optimizers (Compile Time, MR) • Skew keys identified by: create table … skewed by (key) on (key_value); • Activated by “hive. optimize. skewjoin. compiletime” • Logical Optimizer: Skew. Join. Optimizer looks at table metadata • We fixed bug with converting to mapjoin for skewed rows, HIVE-8610 TS Fil (Skewed Rows) Reduce. Sink Join TS Fil (Skewed Rows) Reduce. Sink Union TS Fil (non-skewed) Reduce. Sink Join TS Fil (non-skewed) Reduce. Sink © 2014 Cloudera, Inc. All rights reserved. 24

Skew Join Optimizers (Runtime, MR) • Activated by “hive. optimize. skewjoin” • Physical Optimizer:

Skew Join Optimizers (Runtime, MR) • Activated by “hive. optimize. skewjoin” • Physical Optimizer: Skew. Join. Resolver • During join operator, key is skewed if it passes “hive. skewjoin. key” threshold • Skew key is skipped and values are copied to separate directories • Those directories are processed by conditional mapjoin task. Task 3 Tab 1 Skew Keys Map. Red. Local. Work Tab 1 Condition 1(Skew Key Join) Map. Red. Local. Work Tab 2 Tab 1 to Hash. Table Condition 2(Skew Key Join) Map. Red. Local. Work Tab 2 to Hash. Table Join Tab 2 Skew Keys Map. Red. Work Tab 2 is bigtable Tab 1 is bigtable © 2014 Cloudera, Inc. All rights reserved. 25

Skew Join (Spark) • Compile-time optimizer • Logical Optimizer: Re-use Skew. Join. Optimizer •

Skew Join (Spark) • Compile-time optimizer • Logical Optimizer: Re-use Skew. Join. Optimizer • Runtime optimizer • Physical Optimizer: Spark. Skew. Join. Resolver, similar to Skew. Join. Resolver. • Main challenge is to break up some Spark. Task that involve aggregations follow by join, in skewjoin case, in order to insert conditional task. © 2014 Cloudera, Inc. All rights reserved. 26

MR Join Class Diagram (Enjoy) Tables are skewed, Skew metadata available Tables are skewed

MR Join Class Diagram (Enjoy) Tables are skewed, Skew metadata available Tables are skewed N-1 join tables fit in memory User provides Join hints Skew. Join. Optimizer (hive. optimize. skewjoin. compiletime) User provides join hints && Tables bucketed Users provides Join hints && Tables bucketed && Tables Sorted Map. Join. Processor Bucket. Map. Join. Optimizer (hive. optimize. bucket. mapjoin) Sorted. Merge. Bucket. Map. Join. Optimizer (hive. optimize. bucketmapjoin. sortedmerge) Sorted. Merge. Bucket. Map. Join. Proc (if contains Map. Join operator) Sorted. Bucket. Map. Join. Proc (ihive. auto. convert. sortmerge. join) SMB Map. Join. Factory (if contains Map. Join, SMBJoin operator) Skew. Join. Resolver (hive. optimize. skew. join) Common. Join. Resolver (hive. auto. convert. join) Sort. Merge. Join. Resolver (hive. auto. convert. join && hive. auto. convert. sortmerge. join. to. mapjoin) Map. Join. Resolver (if contains Map. Work with Map. Local. Work) Skew Join With Map. Join Bucket Map. Join © 2014 Cloudera, Inc. All rights reserved. 27

Spark Join Class Diagram (Enjoy) Tables are skewed, Skew metadata available N-1 join tables

Spark Join Class Diagram (Enjoy) Tables are skewed, Skew metadata available N-1 join tables fit in memory User provides Join hints Skew. Join. Optimizer (hive. optimize. skewjoin. compiletime) User provides join hints && Tables bucketed Users provides Join hints && Tables bucketed && Tables Sorted Spark. Map. Join. Processor Bucket. Map. Join. Optimizer (hive. optimize. bucket. mapjoin) Spark. Map. Join. Optimizer (hive. auto. convert. join && hive. auto. convert. sortmerge. join. to. mapjoin) Spark. Sort. Merge. Join. Optimizer (hive. auto. convert. sortmerge. join && ! hive. auto. convert. sortmerge. join. to. mapjoin) Spark. SMBJoin. Hint. Optimizer (if contains Map. Join operator) Gen. Spark. Work Skew. Join. Resolver (hive. optimize. skew. join) Spark. Sort. Merge. Map. Join. Factory (if contains SMBMap. Join operator) Spark. Map. Join. Resolver (if Spark. Work contains Map. Join. Operator) Skew Join With Map. Join or Bucket Mapjoin Map. Join Bucket Map. Join SMB Map. Join © 2014 Cloudera, Inc. All rights reserved. 28

Hive on Spark Join Team • Szehon Ho (Cloudera) • Chao Sun (Cloudera) •

Hive on Spark Join Team • Szehon Ho (Cloudera) • Chao Sun (Cloudera) • Jimmy Xiang (Cloudera) • Rui Li (Intel) • Suhas Satish (Map. R) • Na Yang (Map. R) © 2014 Cloudera, Inc. All rights reserved. 29

Thank you.

Thank you.