buyingyi | c73348c | 2012-11-02 00:31:31 +0000 | [diff] [blame] | 1 | DROP TABLE partsupp; |
| 2 | DROP TABLE lineitem; |
| 3 | DROP TABLE supplier; |
| 4 | DROP TABLE nation; |
| 5 | DROP TABLE q20_tmp1; |
| 6 | DROP TABLE q20_tmp2; |
| 7 | DROP TABLE q20_tmp3; |
| 8 | DROP TABLE q20_tmp4; |
| 9 | DROP TABLE q20_potential_part_promotion; |
| 10 | |
| 11 | -- create tables and load data |
| 12 | create external table lineitem (L_ORDERKEY INT, L_PARTKEY INT, L_SUPPKEY INT, L_LINENUMBER INT, L_QUANTITY DOUBLE, L_EXTENDEDPRICE DOUBLE, L_DISCOUNT DOUBLE, L_TAX DOUBLE, L_RETURNFLAG STRING, L_LINESTATUS STRING, L_SHIPDATE STRING, L_COMMITDATE STRING, L_RECEIPTDATE STRING, L_SHIPINSTRUCT STRING, L_SHIPMODE STRING, L_COMMENT STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' STORED AS TEXTFILE LOCATION '/tpch/10/lineitem'; |
| 13 | create external table supplier (S_SUPPKEY INT, S_NAME STRING, S_ADDRESS STRING, S_NATIONKEY INT, S_PHONE STRING, S_ACCTBAL DOUBLE, S_COMMENT STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' STORED AS TEXTFILE LOCATION '/tpch/10/supplier'; |
| 14 | create external table nation (N_NATIONKEY INT, N_NAME STRING, N_REGIONKEY INT, N_COMMENT STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' STORED AS TEXTFILE LOCATION '/tpch/10/nation'; |
| 15 | create external table partsupp (PS_PARTKEY INT, PS_SUPPKEY INT, PS_AVAILQTY INT, PS_SUPPLYCOST DOUBLE, PS_COMMENT STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' STORED AS TEXTFILE LOCATION'/tpch/10/partsupp'; |
| 16 | |
| 17 | -- create the target table |
| 18 | create table q20_tmp1(p_partkey int); |
| 19 | create table q20_tmp2(l_partkey int, l_suppkey int, sum_quantity double); |
| 20 | create table q20_tmp3(ps_suppkey int, ps_availqty int, sum_quantity double); |
| 21 | create table q20_tmp4(ps_suppkey int); |
| 22 | create table q20_potential_part_promotion(s_name string, s_address string); |
| 23 | |
| 24 | set mapred.min.split.size=536870912; |
| 25 | |
| 26 | -- the query |
| 27 | insert overwrite table q20_tmp1 |
| 28 | select distinct p_partkey |
| 29 | from |
| 30 | part |
| 31 | where |
| 32 | p_name like 'forest%'; |
| 33 | |
| 34 | insert overwrite table q20_tmp2 |
| 35 | select |
| 36 | l_partkey, l_suppkey, 0.5 * sum(l_quantity) |
| 37 | from |
| 38 | lineitem |
| 39 | where |
| 40 | l_shipdate >= '1994-01-01' |
| 41 | and l_shipdate < '1995-01-01' |
| 42 | group by l_partkey, l_suppkey; |
| 43 | |
| 44 | insert overwrite table q20_tmp3 |
| 45 | select |
| 46 | ps_suppkey, ps_availqty, sum_quantity |
| 47 | from |
| 48 | partsupp ps join q20_tmp1 t1 |
| 49 | on |
| 50 | ps.ps_partkey = t1.p_partkey |
| 51 | join q20_tmp2 t2 |
| 52 | on |
| 53 | ps.ps_partkey = t2.l_partkey and ps.ps_suppkey = t2.l_suppkey; |
| 54 | |
| 55 | insert overwrite table q20_tmp4 |
| 56 | select |
| 57 | ps_suppkey |
| 58 | from |
| 59 | q20_tmp3 |
| 60 | where |
| 61 | ps_availqty > sum_quantity |
| 62 | group by ps_suppkey; |
| 63 | |
| 64 | insert overwrite table q20_potential_part_promotion |
| 65 | select |
| 66 | s_name, s_address |
| 67 | from |
| 68 | supplier s join nation n |
| 69 | on |
| 70 | s.s_nationkey = n.n_nationkey |
| 71 | and n.n_name = 'CANADA' |
| 72 | join q20_tmp4 t4 |
| 73 | on |
| 74 | s.s_suppkey = t4.ps_suppkey |
| 75 | order by s_name; |
| 76 | |
| 77 | |