PageRenderTime 53ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/test/org/apache/pig/test/TestMultiQueryCompiler.java

https://github.com/zjffdu/pig
Java | 1533 lines | 1146 code | 362 blank | 25 comment | 9 complexity | e513178cb8cac05cfba542f6cce32035 MD5 | raw file
Possible License(s): Apache-2.0, CPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.pig.test;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.File;
  21. import java.io.FileWriter;
  22. import java.io.IOException;
  23. import java.io.PrintWriter;
  24. import java.io.StringReader;
  25. import java.util.ArrayList;
  26. import java.util.Collections;
  27. import java.util.Iterator;
  28. import junit.framework.Assert;
  29. import org.apache.pig.ExecType;
  30. import org.apache.pig.PigException;
  31. import org.apache.pig.PigServer;
  32. import org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.MapReduceLauncher;
  33. import org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.MapReduceOper;
  34. import org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.plans.MROperPlan;
  35. import org.apache.pig.backend.hadoop.executionengine.physicalLayer.PhysicalOperator;
  36. import org.apache.pig.backend.hadoop.executionengine.physicalLayer.plans.PhysicalPlan;
  37. import org.apache.pig.backend.hadoop.executionengine.physicalLayer.relationalOperators.POSplit;
  38. import org.apache.pig.backend.hadoop.executionengine.physicalLayer.relationalOperators.POStore;
  39. import org.apache.pig.backend.hadoop.executionengine.util.MapRedUtil;
  40. import org.apache.pig.impl.PigContext;
  41. import org.apache.pig.impl.io.FileLocalizer;
  42. import org.apache.pig.impl.plan.Operator;
  43. import org.apache.pig.impl.plan.OperatorPlan;
  44. import org.apache.pig.impl.util.LogUtils;
  45. import org.apache.pig.newplan.logical.relational.LogicalPlan;
  46. import org.apache.pig.tools.grunt.GruntParser;
  47. import org.apache.pig.tools.pigscript.parser.ParseException;
  48. import org.junit.After;
  49. import org.junit.AfterClass;
  50. import org.junit.Before;
  51. import org.junit.BeforeClass;
  52. import org.junit.Test;
  53. import org.junit.runner.RunWith;
  54. import org.junit.runners.JUnit4;
  55. @RunWith(JUnit4.class)
  56. public class TestMultiQueryCompiler {
  57. private static MiniCluster cluster;
  58. private PigServer myPig;
  59. @BeforeClass
  60. public static void setUpBeforeClass() throws IOException {
  61. cluster = MiniCluster.buildCluster();
  62. Util.copyFromLocalToCluster(cluster,
  63. "test/org/apache/pig/test/data/passwd", "passwd");
  64. Util.copyFromLocalToCluster(cluster,
  65. "test/org/apache/pig/test/data/passwd2", "passwd2");
  66. }
  67. @AfterClass
  68. public static void tearDownAfterClass() throws IOException {
  69. Util.deleteFile(cluster, "passwd");
  70. Util.deleteFile(cluster, "passwd2");
  71. cluster.shutDown();
  72. }
  73. @Before
  74. public void setUp() throws Exception {
  75. cluster.setProperty("opt.multiquery", ""+true);
  76. myPig = new PigServer(ExecType.MAPREDUCE, cluster.getProperties());
  77. deleteOutputFiles();
  78. }
  79. @After
  80. public void tearDown() throws Exception {
  81. myPig = null;
  82. }
  83. @Test
  84. public void testMultiQueryJiraPig1438() {
  85. // test case: merge multiple distinct jobs -- one group by job, one distinct job
  86. String INPUT_FILE = "abc";
  87. try {
  88. myPig.setBatchOn();
  89. myPig.registerQuery("A = load '" + INPUT_FILE + "' as (col1:int, col2:int, col3:int);");
  90. myPig.registerQuery("B1 = foreach A generate col1, col2;");
  91. myPig.registerQuery("B2 = foreach A generate col2, col3;");
  92. myPig.registerQuery("C1 = distinct B1;");
  93. myPig.registerQuery("C2 = group B2 by (col2, col3);");
  94. myPig.registerQuery("D1 = foreach C1 generate col1, col2;");
  95. myPig.registerQuery("D2 = foreach C2 generate B2.col2, B2.col3;");
  96. myPig.registerQuery("store D1 into '/tmp/output1';");
  97. myPig.registerQuery("store D2 into '/tmp/output2';");
  98. LogicalPlan lp = checkLogicalPlan(1, 2, 9);
  99. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 2, 15);
  100. checkMRPlan(pp, 1, 1, 2);
  101. } catch (Exception e) {
  102. e.printStackTrace();
  103. Assert.fail();
  104. }
  105. }
  106. @Test
  107. public void testMultiQueryJiraPig1060() {
  108. // test case:
  109. String INPUT_FILE = "pig-1060.txt";
  110. try {
  111. PrintWriter w = new PrintWriter(new FileWriter(INPUT_FILE));
  112. w.println("apple\t2");
  113. w.println("apple\t12");
  114. w.println("orange\t3");
  115. w.println("orange\t23");
  116. w.println("strawberry\t10");
  117. w.println("strawberry\t34");
  118. w.close();
  119. Util.copyFromLocalToCluster(cluster, INPUT_FILE, INPUT_FILE);
  120. myPig.setBatchOn();
  121. myPig.registerQuery("data = load '" + INPUT_FILE +
  122. "' as (name:chararray, gid:int);");
  123. myPig.registerQuery("f1 = filter data by gid < 5;");
  124. myPig.registerQuery("g1 = group f1 by name;");
  125. myPig.registerQuery("p1 = foreach g1 generate group, COUNT(f1.gid);");
  126. myPig.registerQuery("store p1 into '/tmp/output1';");
  127. myPig.registerQuery("f2 = filter data by gid > 5;");
  128. myPig.registerQuery("g2 = group f2 by name;");
  129. myPig.registerQuery("p2 = foreach g2 generate group, COUNT(f2.gid);");
  130. myPig.registerQuery("store p2 into '/tmp/output2';");
  131. myPig.registerQuery("f3 = filter f2 by gid > 10;");
  132. myPig.registerQuery("g3 = group f3 by name;");
  133. myPig.registerQuery("p3 = foreach g3 generate group, COUNT(f3.gid);");
  134. myPig.registerQuery("store p3 into '/tmp/output3';");
  135. myPig.registerQuery("f4 = filter f3 by gid < 20;");
  136. myPig.registerQuery("g4 = group f4 by name;");
  137. myPig.registerQuery("p4 = foreach g4 generate group, COUNT(f4.gid);");
  138. myPig.registerQuery("store p4 into '/tmp/output4';");
  139. LogicalPlan lp = checkLogicalPlan(1, 4, 17);
  140. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 4, 35);
  141. checkMRPlan(pp, 1, 1, 1);
  142. } catch (Exception e) {
  143. e.printStackTrace();
  144. Assert.fail();
  145. } finally {
  146. new File(INPUT_FILE).delete();
  147. try {
  148. Util.deleteFile(cluster, INPUT_FILE);
  149. } catch (IOException e) {
  150. e.printStackTrace();
  151. Assert.fail();
  152. }
  153. }
  154. }
  155. @Test
  156. public void testMultiQueryJiraPig920() {
  157. // test case: a simple diamond query
  158. try {
  159. myPig.setBatchOn();
  160. myPig.registerQuery("a = load 'passwd' " +
  161. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int, gid:int);");
  162. myPig.registerQuery("b = filter a by uid < 5;");
  163. myPig.registerQuery("c = filter a by gid >= 5;");
  164. myPig.registerQuery("d = cogroup c by $0, b by $0;");
  165. myPig.registerQuery("e = foreach d generate group, COUNT(c), COUNT(b);");
  166. myPig.registerQuery("store e into '/tmp/output1';");
  167. LogicalPlan lp = checkLogicalPlan(1, 1, 6);
  168. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 1, 13);
  169. checkMRPlan(pp, 1, 1, 1);
  170. } catch (Exception e) {
  171. e.printStackTrace();
  172. Assert.fail();
  173. }
  174. }
  175. @Test
  176. public void testMultiQueryJiraPig920_1() {
  177. // test case: a query with two diamonds
  178. try {
  179. myPig.setBatchOn();
  180. myPig.registerQuery("a = load 'passwd' " +
  181. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int, gid:int);");
  182. myPig.registerQuery("b = filter a by uid < 5;");
  183. myPig.registerQuery("c = filter a by gid >= 5;");
  184. myPig.registerQuery("d = filter a by uid >= 5;");
  185. myPig.registerQuery("e = filter a by gid < 5;");
  186. myPig.registerQuery("f = cogroup c by $0, b by $0;");
  187. myPig.registerQuery("f1 = foreach f generate group, COUNT(c), COUNT(b);");
  188. myPig.registerQuery("store f1 into '/tmp/output1';");
  189. myPig.registerQuery("g = cogroup d by $0, e by $0;");
  190. myPig.registerQuery("g1 = foreach g generate group, COUNT(d), COUNT(e);");
  191. myPig.registerQuery("store g1 into '/tmp/output2';");
  192. LogicalPlan lp = checkLogicalPlan(1, 2, 11);
  193. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 2, 23);
  194. checkMRPlan(pp, 2, 2, 2);
  195. } catch (Exception e) {
  196. e.printStackTrace();
  197. Assert.fail();
  198. }
  199. }
  200. @Test
  201. public void testMultiQueryWithDemoCase() {
  202. System.out.println("===== multi-query with demo case 2 =====");
  203. try {
  204. myPig.setBatchOn();
  205. myPig.registerQuery("a = load 'passwd' " +
  206. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int, gid:int);");
  207. myPig.registerQuery("b = foreach a generate uname, uid, gid;");
  208. myPig.registerQuery("c = filter b by uid < 5;");
  209. myPig.registerQuery("d = filter c by gid >= 5;");
  210. myPig.registerQuery("store d into '/tmp/output1';");
  211. myPig.registerQuery("e = filter b by uid >= 5;");
  212. myPig.registerQuery("store e into '/tmp/output2';");
  213. myPig.registerQuery("f = filter c by gid < 5;");
  214. myPig.registerQuery("g = group f by uname;");
  215. myPig.registerQuery("h = foreach g generate group, COUNT(f.uid);");
  216. myPig.registerQuery("store h into '/tmp/output3';");
  217. LogicalPlan lp = checkLogicalPlan(1, 3, 11);
  218. // NOTE: old way seemingly generated a useless foreach operator. Now we have one less operator. Reason unknow.
  219. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 3, 19);
  220. checkMRPlan(pp, 1, 1, 1);
  221. } catch (Exception e) {
  222. e.printStackTrace();
  223. Assert.fail();
  224. }
  225. }
  226. @Test
  227. public void testMultiQueryWithSingleMapReduceSplittee() {
  228. System.out.println("===== multi-query with single map reduce splittee =====");
  229. try {
  230. myPig.setBatchOn();
  231. myPig.registerQuery("a = load 'passwd' " +
  232. "using PigStorage(':') as (uname, passwd, uid, gid);");
  233. myPig.registerQuery("b = foreach a generate uname, uid, gid;");
  234. myPig.registerQuery("split b into c1 if uid > 5, c2 if uid <= 5 ;");
  235. myPig.registerQuery("f = group c2 by uname;");
  236. myPig.registerQuery("f1 = foreach f generate group, SUM(c2.gid);");
  237. myPig.registerQuery("store f1 into '/tmp/output1';");
  238. LogicalPlan lp = checkLogicalPlan(1, 1, 7);
  239. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 1, 9);
  240. checkMRPlan(pp, 1, 1, 1);
  241. } catch (Exception e) {
  242. e.printStackTrace();
  243. Assert.fail();
  244. }
  245. }
  246. @Test
  247. public void testMultiQueryPhase3BaseCase() {
  248. System.out.println("===== multi-query phase 3 base case =====");
  249. try {
  250. myPig.setBatchOn();
  251. myPig.registerQuery("a = load 'passwd' " +
  252. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int, gid:int);");
  253. myPig.registerQuery("b = filter a by uid < 5;");
  254. myPig.registerQuery("c = filter a by uid >= 5 and uid < 10;");
  255. myPig.registerQuery("d = filter a by uid >= 10;");
  256. myPig.registerQuery("b1 = group b by gid;");
  257. myPig.registerQuery("b2 = foreach b1 generate group, COUNT(b.uid);");
  258. myPig.registerQuery("b3 = filter b2 by $1 > 5;");
  259. myPig.registerQuery("store b3 into '/tmp/output1';");
  260. myPig.registerQuery("c1 = group c by gid;");
  261. myPig.registerQuery("c2 = foreach c1 generate group, SUM(c.uid);");
  262. myPig.registerQuery("store c2 into '/tmp/output2';");
  263. myPig.registerQuery("d1 = group d by gid;");
  264. myPig.registerQuery("d2 = foreach d1 generate group, AVG(d.uid);");
  265. myPig.registerQuery("store d2 into '/tmp/output3';");
  266. LogicalPlan lp = checkLogicalPlan(1, 3, 14);
  267. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 3, 25);
  268. checkMRPlan(pp, 1, 1, 1);
  269. } catch (Exception e) {
  270. e.printStackTrace();
  271. Assert.fail();
  272. }
  273. }
  274. @Test
  275. public void testMultiQueryJiraPig983() {
  276. System.out.println("===== multi-query Jira Pig-983 =====");
  277. try {
  278. myPig.setBatchOn();
  279. myPig.registerQuery("a = load 'passwd' " +
  280. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int, gid:int);");
  281. myPig.registerQuery("b = filter a by uid < 5;");
  282. myPig.registerQuery("c = filter a by uid >= 5;");
  283. myPig.registerQuery("d = join b by uname, c by uname;");
  284. myPig.registerQuery("e = group d by b::gid;");
  285. myPig.registerQuery("e1 = foreach e generate group, COUNT(d.b::uid);");
  286. myPig.registerQuery("store e1 into '/tmp/output1';");
  287. myPig.registerQuery("f = group d by c::gid;");
  288. myPig.registerQuery("f1 = foreach f generate group, SUM(d.c::uid);");
  289. myPig.registerQuery("store f1 into '/tmp/output2';");
  290. LogicalPlan lp = checkLogicalPlan(1, 2, 10);
  291. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 2, 25);
  292. checkMRPlan(pp, 1, 1, 2);
  293. } catch (Exception e) {
  294. e.printStackTrace();
  295. Assert.fail();
  296. }
  297. }
  298. @Test
  299. public void testMultiQueryPhase3WithoutCombiner() {
  300. System.out.println("===== multi-query phase 3 without combiner =====");
  301. try {
  302. myPig.setBatchOn();
  303. myPig.registerQuery("a = load 'passwd' " +
  304. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int, gid:int);");
  305. myPig.registerQuery("b = filter a by uid < 5;");
  306. myPig.registerQuery("c = filter a by uid >= 5 and uid < 10;");
  307. myPig.registerQuery("d = filter a by uid >= 10;");
  308. myPig.registerQuery("b1 = group b by gid;");
  309. myPig.registerQuery("b2 = foreach b1 generate group, COUNT(b.uid) + SUM(b.uid);");
  310. myPig.registerQuery("b3 = filter b2 by $1 > 5;");
  311. myPig.registerQuery("store b3 into '/tmp/output1';");
  312. myPig.registerQuery("c1 = group c by gid;");
  313. myPig.registerQuery("c2 = foreach c1 generate group, SUM(c.uid) - COUNT(c.uid);");
  314. myPig.registerQuery("store c2 into '/tmp/output2';");
  315. myPig.registerQuery("d1 = group d by gid;");
  316. myPig.registerQuery("d2 = foreach d1 generate group, MAX(d.uid) - MIN(d.uid);");
  317. myPig.registerQuery("store d2 into '/tmp/output3';");
  318. LogicalPlan lp = checkLogicalPlan(1, 3, 14);
  319. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 3, 25);
  320. checkMRPlan(pp, 1, 1, 1);
  321. } catch (Exception e) {
  322. e.printStackTrace();
  323. Assert.fail();
  324. }
  325. }
  326. @Test
  327. public void testMultiQueryPhase3WithMixedCombiner() {
  328. System.out.println("===== multi-query phase 3 with mixed combiner =====");
  329. try {
  330. myPig.setBatchOn();
  331. myPig.registerQuery("a = load 'passwd' " +
  332. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int, gid:int);");
  333. myPig.registerQuery("b = filter a by uid < 5;");
  334. myPig.registerQuery("c = filter a by uid >= 5 and uid < 10;");
  335. myPig.registerQuery("d = filter a by uid >= 10;");
  336. myPig.registerQuery("b1 = group b by gid;");
  337. myPig.registerQuery("b2 = foreach b1 generate group, COUNT(b.uid);");
  338. myPig.registerQuery("b3 = filter b2 by $1 > 5;");
  339. myPig.registerQuery("store b3 into '/tmp/output1';");
  340. myPig.registerQuery("c1 = group c by gid;");
  341. myPig.registerQuery("c2 = foreach c1 generate group, SUM(c.uid);");
  342. myPig.registerQuery("store c2 into '/tmp/output2';");
  343. myPig.registerQuery("d1 = group d by gid;");
  344. myPig.registerQuery("d2 = foreach d1 generate group, d.uname, MAX(d.uid) - MIN(d.uid);");
  345. myPig.registerQuery("store d2 into '/tmp/output3';");
  346. LogicalPlan lp = checkLogicalPlan(1, 3, 14);
  347. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 3, 25);
  348. checkMRPlan(pp, 1, 1, 2);
  349. } catch (Exception e) {
  350. e.printStackTrace();
  351. Assert.fail();
  352. }
  353. }
  354. @Test
  355. public void testMultiQueryPhase3WithDifferentMapDataTypes() {
  356. System.out.println("===== multi-query phase 3 with different map datatypes =====");
  357. try {
  358. myPig.setBatchOn();
  359. myPig.registerQuery("a = load 'passwd' " +
  360. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int, gid:int);");
  361. myPig.registerQuery("b = filter a by uid < 5;");
  362. myPig.registerQuery("c = filter a by uid >= 5 and uid < 10;");
  363. myPig.registerQuery("d = filter a by uid >= 10;");
  364. myPig.registerQuery("b1 = group b by gid parallel 2;");
  365. myPig.registerQuery("b2 = foreach b1 generate group, COUNT(b.uid);");
  366. myPig.registerQuery("b3 = filter b2 by $1 > 5;");
  367. myPig.registerQuery("store b3 into '/tmp/output1';");
  368. myPig.registerQuery("c1 = group c by $1 parallel 3;");
  369. myPig.registerQuery("c2 = foreach c1 generate group, SUM(c.uid);");
  370. myPig.registerQuery("store c2 into '/tmp/output2';");
  371. myPig.registerQuery("d1 = group d by $1 parallel 4;");
  372. myPig.registerQuery("d2 = foreach d1 generate group, COUNT(d.uid);");
  373. myPig.registerQuery("store d2 into '/tmp/output3';");
  374. LogicalPlan lp = checkLogicalPlan(1, 3, 14);
  375. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 3, 25);
  376. checkMRPlan(pp, 1, 1, 1);
  377. } catch (Exception e) {
  378. e.printStackTrace();
  379. Assert.fail();
  380. }
  381. }
  382. @Test
  383. public void testMultiQueryPhase3StreamingInReducer() {
  384. System.out.println("===== multi-query phase 3 with streaming in reducer =====");
  385. try {
  386. myPig.setBatchOn();
  387. myPig.registerQuery("A = load 'passwd';");
  388. myPig.registerQuery("Split A into A1 if $2 > 5, A2 if $2 >= 5;");
  389. myPig.registerQuery("Split A1 into A3 if $0 > 'm', A4 if $0 >= 'm';");
  390. myPig.registerQuery("B = group A3 by $2;");
  391. myPig.registerQuery("C = foreach B generate flatten(A3);");
  392. myPig.registerQuery("D = stream B through `cat`;");
  393. myPig.registerQuery("store D into '/tmp/output1';");
  394. myPig.registerQuery("E = group A4 by $2;");
  395. myPig.registerQuery("F = foreach E generate group, COUNT(A4);");
  396. myPig.registerQuery("store F into '/tmp/output2';");
  397. myPig.registerQuery("G = group A1 by $2;");
  398. myPig.registerQuery("H = foreach G generate group, COUNT(A1);");
  399. myPig.registerQuery("store H into '/tmp/output3';");
  400. LogicalPlan lp = checkLogicalPlan(1, 3, 15);
  401. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 3, 24);
  402. checkMRPlan(pp, 1, 1, 2);
  403. } catch (Exception e) {
  404. e.printStackTrace();
  405. Assert.fail();
  406. }
  407. }
  408. @Test
  409. public void testMultiQueryWithPigMixL12() {
  410. System.out.println("===== multi-query with PigMix L12 =====");
  411. try {
  412. myPig.setBatchOn();
  413. myPig.registerQuery("a = load 'passwd' " +
  414. "using PigStorage(':') as (uname, passwd, uid, gid);");
  415. myPig.registerQuery("b = foreach a generate uname, passwd, uid, gid;");
  416. myPig.registerQuery("split b into c1 if uid > 5, c2 if uid <= 5 ;");
  417. myPig.registerQuery("split c1 into d1 if gid < 5, d2 if gid >= 5;");
  418. myPig.registerQuery("e = group d1 by uname;");
  419. myPig.registerQuery("e1 = foreach e generate group, MAX(d1.uid);");
  420. myPig.registerQuery("store e1 into '/tmp/output1';");
  421. myPig.registerQuery("f = group c2 by uname;");
  422. myPig.registerQuery("f1 = foreach f generate group, SUM(c2.gid);");
  423. myPig.registerQuery("store f1 into '/tmp/output2';");
  424. myPig.registerQuery("g = group d2 by uname;");
  425. myPig.registerQuery("g1 = foreach g generate group, COUNT(d2);");
  426. myPig.registerQuery("store g1 into '/tmp/output3';");
  427. LogicalPlan lp = checkLogicalPlan(1, 3, 17);
  428. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 3, 23);
  429. checkMRPlan(pp, 1, 1, 1);
  430. } catch (Exception e) {
  431. e.printStackTrace();
  432. Assert.fail();
  433. }
  434. }
  435. @Test
  436. public void testMultiQueryWithCoGroup() {
  437. System.out.println("===== multi-query with CoGroup =====");
  438. try {
  439. myPig.setBatchOn();
  440. myPig.registerQuery("a = load 'passwd' " +
  441. "using PigStorage(':') as (uname, passwd, uid, gid);");
  442. myPig.registerQuery("store a into '/tmp/output1' using BinStorage();");
  443. myPig.registerQuery("b = load '/tmp/output1' using BinStorage() as (uname, passwd, uid, gid);");
  444. myPig.registerQuery("c = load 'passwd2' " +
  445. "using PigStorage(':') as (uname, passwd, uid, gid);");
  446. myPig.registerQuery("d = cogroup b by (uname, uid) inner, c by (uname, uid) inner;");
  447. myPig.registerQuery("e = foreach d generate flatten(b), flatten(c);");
  448. myPig.registerQuery("store e into '/tmp/output2';");
  449. LogicalPlan lp = checkLogicalPlan(2, 1, 7);
  450. PhysicalPlan pp = checkPhysicalPlan(lp, 2, 1, 13);
  451. checkMRPlan(pp, 1, 1, 2);
  452. } catch (Exception e) {
  453. e.printStackTrace();
  454. Assert.fail();
  455. }
  456. }
  457. @Test
  458. public void testMultiQueryWithFJ() {
  459. System.out.println("===== multi-query with FJ =====");
  460. try {
  461. myPig.setBatchOn();
  462. myPig.registerQuery("a = load 'passwd' " +
  463. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int, gid:int);");
  464. myPig.registerQuery("b = load 'passwd' " +
  465. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int, gid:int);");
  466. myPig.registerQuery("c = filter a by uid > 5;");
  467. myPig.registerQuery("store c into '/tmp/output1';");
  468. myPig.registerQuery("d = filter b by gid > 10;");
  469. myPig.registerQuery("store d into '/tmp/output2';");
  470. myPig.registerQuery("e = join c by gid, d by gid using 'repl';");
  471. myPig.registerQuery("store e into '/tmp/output3';");
  472. LogicalPlan lp = checkLogicalPlan(2, 3, 8);
  473. PhysicalPlan pp = checkPhysicalPlan(lp, 2, 3, 16);
  474. checkMRPlan(pp, 2, 1, 3);
  475. } catch (Exception e) {
  476. e.printStackTrace();
  477. Assert.fail();
  478. }
  479. }
  480. @Test
  481. public void testMultiQueryWithExplicitSplitAndSideFiles() {
  482. System.out.println("===== multi-query with explicit split and side files =====");
  483. try {
  484. myPig.setBatchOn();
  485. myPig.registerQuery("a = load 'passwd' " +
  486. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int, gid:int);");
  487. myPig.registerQuery("split a into b if uid > 500, c if uid <= 500;");
  488. myPig.registerQuery("store b into '/tmp/output1';");
  489. myPig.registerQuery("store c into '/tmp/output2';");
  490. myPig.registerQuery("e = cogroup b by gid, c by gid;");
  491. myPig.registerQuery("d = foreach e generate flatten(c), flatten(b);");
  492. myPig.registerQuery("store d into '/tmp/output3';");
  493. LogicalPlan lp = checkLogicalPlan(1, 3, 9);
  494. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 3, 19);
  495. checkMRPlan(pp, 1, 1, 2);
  496. } catch (Exception e) {
  497. e.printStackTrace();
  498. Assert.fail();
  499. }
  500. }
  501. @Test
  502. public void testMultiQueryWithExplicitSplitAndOrderByAndSideFiles() {
  503. System.out.println("===== multi-query with explicit split, orderby and side files =====");
  504. try {
  505. myPig.setBatchOn();
  506. myPig.registerQuery("a = load 'passwd' " +
  507. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int, gid:int);");
  508. myPig.registerQuery("split a into a1 if uid > 500, a2 if gid > 500;");
  509. myPig.registerQuery("b1 = distinct a1;");
  510. myPig.registerQuery("b2 = order a2 by uname;");
  511. myPig.registerQuery("store b1 into '/tmp/output1';");
  512. myPig.registerQuery("store b2 into '/tmp/output2';");
  513. myPig.registerQuery("c = cogroup b1 by uname, b2 by uname;");
  514. myPig.registerQuery("d = foreach c generate flatten(group), flatten($1), flatten($2);");
  515. myPig.registerQuery("store d into '/tmp/output3';");
  516. LogicalPlan lp = checkLogicalPlan(1, 3, 11);
  517. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 3, 21);
  518. checkMRPlan(pp, 1, 1, 4);
  519. } catch (Exception e) {
  520. e.printStackTrace();
  521. Assert.fail();
  522. }
  523. }
  524. @Test
  525. public void testMultiQueryWithIntermediateStores() {
  526. System.out.println("===== multi-query with intermediate stores =====");
  527. try {
  528. myPig.setBatchOn();
  529. myPig.registerQuery("a = load 'passwd' " +
  530. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int, gid:int);");
  531. myPig.registerQuery("store a into '/tmp/output1';");
  532. myPig.registerQuery("b = load '/tmp/output1' using PigStorage(':'); ");
  533. myPig.registerQuery("store b into '/tmp/output2';");
  534. LogicalPlan lp = checkLogicalPlan(1, 1, 4);
  535. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 1, 5);
  536. checkMRPlan(pp, 1, 1, 2);
  537. } catch (Exception e) {
  538. e.printStackTrace();
  539. Assert.fail();
  540. }
  541. }
  542. @Test
  543. public void testMultiQueryWithImplicitSplitAndSideFiles() {
  544. System.out.println("===== multi-query with implicit split and side files =====");
  545. try {
  546. myPig.setBatchOn();
  547. myPig.registerQuery("a = load 'passwd' " +
  548. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int, gid:int);");
  549. myPig.registerQuery("b = filter a by uid > 500;");
  550. myPig.registerQuery("c = filter a by gid > 500;");
  551. myPig.registerQuery("store c into '/tmp/output1';");
  552. myPig.registerQuery("d = cogroup b by uname, c by uname;");
  553. myPig.registerQuery("e = foreach d generate flatten(c), flatten(b);");
  554. myPig.registerQuery("store e into '/tmp/output2';");
  555. myPig.registerQuery("f = filter e by b::uid < 1000;");
  556. myPig.registerQuery("store f into '/tmp/output3';");
  557. LogicalPlan lp = checkLogicalPlan(1, 3, 9);
  558. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 3, 22);
  559. checkMRPlan(pp, 1, 1, 2);
  560. } catch (Exception e) {
  561. e.printStackTrace();
  562. Assert.fail();
  563. }
  564. }
  565. @Test
  566. public void testMultiQueryWithTwoLoadsAndTwoStores() {
  567. System.out.println("===== multi-query with two loads and two stores =====");
  568. try {
  569. myPig.setBatchOn();
  570. myPig.registerQuery("a = load 'passwd' " +
  571. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int,gid:int);");
  572. myPig.registerQuery("b = load 'passwd2' " +
  573. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int,gid:int);");
  574. myPig.registerQuery("c = filter a by uid > 5;");
  575. myPig.registerQuery("d = filter b by uid > 10;");
  576. myPig.registerQuery("e = cogroup c by uid, d by uid;");
  577. myPig.registerQuery("f = foreach e generate flatten(c), flatten(d);");
  578. myPig.registerQuery("g = group f by d::gid;");
  579. myPig.registerQuery("h = filter f by c::gid > 5;");
  580. myPig.registerQuery("store g into '/tmp/output1';");
  581. myPig.registerQuery("store h into '/tmp/output2';");
  582. LogicalPlan lp = checkLogicalPlan(2, 2, 10);
  583. PhysicalPlan pp = checkPhysicalPlan(lp, 2, 2, 20);
  584. checkMRPlan(pp, 1, 1, 2);
  585. } catch (Exception e) {
  586. e.printStackTrace();
  587. Assert.fail();
  588. }
  589. }
  590. @Test
  591. public void testMultiQueryWithSplitInReduce() {
  592. System.out.println("===== multi-query with split in reduce =====");
  593. try {
  594. myPig.setBatchOn();
  595. myPig.registerQuery("a = load 'passwd' " +
  596. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int, gid:int);");
  597. myPig.registerQuery("b = filter a by uid > 5;");
  598. myPig.registerQuery("c = group b by gid;");
  599. myPig.registerQuery("d = foreach c generate group, COUNT(b.uid);");
  600. myPig.registerQuery("store d into '/tmp/output1';");
  601. myPig.registerQuery("e = filter d by $1 > 5;");
  602. myPig.registerQuery("store e into '/tmp/output2';");
  603. LogicalPlan lp = checkLogicalPlan(1, 2, 7);
  604. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 2, 13);
  605. checkMRPlan(pp, 1, 1, 1);
  606. } catch (Exception e) {
  607. e.printStackTrace();
  608. Assert.fail();
  609. }
  610. }
  611. @Test
  612. public void testMultiQueryWithSplitInReduceAndReduceSplitee() {
  613. System.out.println("===== multi-query with split in reduce and reduce splitee =====");
  614. try {
  615. myPig.setBatchOn();
  616. myPig.registerQuery("a = load 'passwd' " +
  617. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int, gid:int);");
  618. myPig.registerQuery("b = filter a by uid > 5;");
  619. myPig.registerQuery("c = group b by gid;");
  620. myPig.registerQuery("d = foreach c generate group, COUNT(b.uid);");
  621. myPig.registerQuery("store d into '/tmp/output1';");
  622. myPig.registerQuery("e = filter d by $1 > 5;");
  623. myPig.registerQuery("f = group e by $1;");
  624. myPig.registerQuery("g = foreach f generate group, SUM(e.$0);");
  625. myPig.registerQuery("store g into '/tmp/output2';");
  626. LogicalPlan lp = checkLogicalPlan(1, 2, 9);
  627. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 2, 17);
  628. checkMRPlan(pp, 1, 1, 2);
  629. } catch (Exception e) {
  630. e.printStackTrace();
  631. Assert.fail();
  632. }
  633. }
  634. @Test
  635. public void testMultiQueryWithSplitInReduceAndReduceSplitees() {
  636. System.out.println("===== multi-query with split in reduce and reduce splitees =====");
  637. try {
  638. myPig.setBatchOn();
  639. myPig.registerQuery("a = load 'passwd' " +
  640. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int, gid:int);");
  641. myPig.registerQuery("b = filter a by uid > 5;");
  642. myPig.registerQuery("c = group b by gid;");
  643. myPig.registerQuery("d = foreach c generate group, COUNT(b.uid);");
  644. myPig.registerQuery("e = filter d by $1 > 5;");
  645. myPig.registerQuery("e1 = group e by $1;");
  646. myPig.registerQuery("e2 = foreach e1 generate group, SUM(e.$0);");
  647. myPig.registerQuery("store e2 into '/tmp/output1';");
  648. myPig.registerQuery("f = filter d by $1 < 5;");
  649. myPig.registerQuery("f1 = group f by $1;");
  650. myPig.registerQuery("f2 = foreach f1 generate group, COUNT(f.$0);");
  651. myPig.registerQuery("store f2 into '/tmp/output2';");
  652. LogicalPlan lp = checkLogicalPlan(1, 2, 12);
  653. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 2, 22);
  654. checkMRPlan(pp, 1, 1, 2);
  655. } catch (Exception e) {
  656. e.printStackTrace();
  657. Assert.fail();
  658. }
  659. }
  660. @Test
  661. public void testMultiQueryWithSplitInReduceAndReduceSpliteesAndMore() {
  662. System.out.println("===== multi-query with split in reduce and reduce splitees and more =====");
  663. try {
  664. myPig.setBatchOn();
  665. myPig.registerQuery("a = load 'passwd' " +
  666. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int, gid:int);");
  667. myPig.registerQuery("b = filter a by uid > 500;");
  668. myPig.registerQuery("c = group b by gid;");
  669. myPig.registerQuery("d = foreach c generate group, COUNT(b.uid);");
  670. myPig.registerQuery("e = filter d by $1 > 5;");
  671. myPig.registerQuery("e1 = group e by $1;");
  672. myPig.registerQuery("e2 = foreach e1 generate group, SUM(e.$0);");
  673. myPig.registerQuery("e3 = filter e2 by $1 > 10;");
  674. myPig.registerQuery("e4 = group e3 by $1;");
  675. myPig.registerQuery("e5 = foreach e4 generate group, SUM(e3.$0);");
  676. myPig.registerQuery("store e5 into '/tmp/output1';");
  677. myPig.registerQuery("f = filter d by $1 < 5;");
  678. myPig.registerQuery("f1 = group f by $1;");
  679. myPig.registerQuery("f2 = foreach f1 generate group, COUNT(f.$0);");
  680. myPig.registerQuery("f3 = filter f2 by $1 < 100;");
  681. myPig.registerQuery("f4 = group f3 by $1;");
  682. myPig.registerQuery("f5 = foreach f4 generate group, COUNT(f3.$0);");
  683. myPig.registerQuery("store f5 into '/tmp/output2';");
  684. LogicalPlan lp = checkLogicalPlan(1, 2, 18);
  685. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 2, 32);
  686. checkMRPlan(pp, 1, 2, 4);
  687. } catch (Exception e) {
  688. e.printStackTrace();
  689. Assert.fail();
  690. }
  691. }
  692. @Test
  693. public void testMultiQueryWithSplitInMapAndReduceSplitees() {
  694. System.out.println("===== multi-query with split in map and reduce splitees =====");
  695. try {
  696. myPig.setBatchOn();
  697. myPig.registerQuery("a = load 'passwd' " +
  698. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int, gid:int);");
  699. myPig.registerQuery("b = filter a by uid < 5;");
  700. myPig.registerQuery("c = filter a by uid >= 5 and uid < 10;");
  701. myPig.registerQuery("d = filter a by uid >= 10;");
  702. myPig.registerQuery("b1 = group b by gid;");
  703. myPig.registerQuery("b2 = foreach b1 generate group, COUNT(b.uid);");
  704. myPig.registerQuery("b3 = filter b2 by $1 > 5;");
  705. myPig.registerQuery("store b3 into '/tmp/output1';");
  706. myPig.registerQuery("c1 = group c by $1;");
  707. myPig.registerQuery("c2 = foreach c1 generate group, SUM(c.uid);");
  708. myPig.registerQuery("store c2 into '/tmp/output2';");
  709. myPig.registerQuery("d1 = group d by $1;");
  710. myPig.registerQuery("d2 = foreach d1 generate group, COUNT(d.uid);");
  711. myPig.registerQuery("store d2 into '/tmp/output3';");
  712. LogicalPlan lp = checkLogicalPlan(1, 3, 14);
  713. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 3, 25);
  714. checkMRPlan(pp, 1, 1, 1);
  715. } catch (Exception e) {
  716. e.printStackTrace();
  717. Assert.fail();
  718. }
  719. }
  720. @Test
  721. public void testMultiQueryWithTwoStores() {
  722. System.out.println("===== multi-query with 2 stores =====");
  723. try {
  724. myPig.setBatchOn();
  725. myPig.registerQuery("a = load 'passwd' " +
  726. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int,gid:int);");
  727. myPig.registerQuery("b = filter a by uid > 5;");
  728. myPig.registerQuery("store b into '/tmp/output1';");
  729. myPig.registerQuery("c = group b by gid;");
  730. myPig.registerQuery("store c into '/tmp/output2';");
  731. LogicalPlan lp = checkLogicalPlan(1, 2, 5);
  732. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 2, 11);
  733. checkMRPlan(pp, 1, 1, 1);
  734. } catch (Exception e) {
  735. e.printStackTrace();
  736. Assert.fail();
  737. }
  738. }
  739. @Test
  740. public void testMultiQueryWithThreeStores() {
  741. System.out.println("===== multi-query with 3 stores =====");
  742. try {
  743. myPig.setBatchOn();
  744. myPig.registerQuery("a = load 'passwd' " +
  745. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int,gid:int);");
  746. myPig.registerQuery("b = filter a by uid > 5;");
  747. myPig.registerQuery("store b into '/tmp/output1';");
  748. myPig.registerQuery("c = filter b by uid > 10;");
  749. myPig.registerQuery("store c into '/tmp/output2';");
  750. myPig.registerQuery("d = filter c by uid > 15;");
  751. myPig.registerQuery("store d into '/tmp/output3';");
  752. LogicalPlan lp = checkLogicalPlan(1, 3, 7);
  753. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 3, 14);
  754. checkMRPlan(pp, 1, 1, 1);
  755. } catch (Exception e) {
  756. e.printStackTrace();
  757. Assert.fail();
  758. }
  759. }
  760. @Test
  761. public void testMultiQueryWithTwoLoads() {
  762. System.out.println("===== multi-query with two loads =====");
  763. try {
  764. myPig.setBatchOn();
  765. myPig.registerQuery("a = load 'passwd' " +
  766. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int,gid:int);");
  767. myPig.registerQuery("b = load 'passwd2' " +
  768. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int,gid:int);");
  769. myPig.registerQuery("c = filter a by uid > 5;");
  770. myPig.registerQuery("d = filter b by uid > 10;");
  771. myPig.registerQuery("store c into '/tmp/output1';");
  772. myPig.registerQuery("store d into '/tmp/output2';");
  773. myPig.registerQuery("e = cogroup c by uid, d by uid;");
  774. myPig.registerQuery("store e into '/tmp/output3';");
  775. LogicalPlan lp = checkLogicalPlan(2, 3, 8);
  776. PhysicalPlan pp = checkPhysicalPlan(lp, 2, 3, 19);
  777. checkMRPlan(pp, 2, 1, 3);
  778. } catch (Exception e) {
  779. e.printStackTrace();
  780. Assert.fail();
  781. }
  782. }
  783. @Test
  784. public void testStoreOrder() {
  785. System.out.println("===== multi-query store order =====");
  786. try {
  787. myPig.setBatchOn();
  788. myPig.registerQuery("a = load 'passwd';");
  789. myPig.registerQuery("store a into '/tmp/output1' using BinStorage();");
  790. myPig.registerQuery("a = load '/tmp/output1';");
  791. myPig.registerQuery("store a into '/tmp/output2';");
  792. myPig.registerQuery("a = load '/tmp/output1';");
  793. myPig.registerQuery("store a into '/tmp/output3';");
  794. myPig.registerQuery("a = load '/tmp/output2' using BinStorage();");
  795. myPig.registerQuery("store a into '/tmp/output4';");
  796. myPig.registerQuery("a = load '/tmp/output2';");
  797. myPig.registerQuery("b = load '/tmp/output1';");
  798. myPig.registerQuery("c = cogroup a by $0, b by $0;");
  799. myPig.registerQuery("store c into '/tmp/output5';");
  800. LogicalPlan lp = checkLogicalPlan(1, 3, 12);
  801. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 3, 15);
  802. MROperPlan mp = checkMRPlan(pp, 1, 3, 5);
  803. myPig.executeBatch();
  804. myPig.discardBatch();
  805. Assert.assertTrue(myPig.getPigContext().getDfs().isContainer("/tmp/output1"));
  806. Assert.assertTrue(myPig.getPigContext().getDfs().isContainer("/tmp/output2"));
  807. Assert.assertTrue(myPig.getPigContext().getDfs().isContainer("/tmp/output3"));
  808. Assert.assertTrue(myPig.getPigContext().getDfs().isContainer("/tmp/output4"));
  809. Assert.assertTrue(myPig.getPigContext().getDfs().isContainer("/tmp/output5"));
  810. } catch (Exception e) {
  811. e.printStackTrace();
  812. Assert.fail();
  813. }
  814. }
  815. @Test
  816. public void testUnnecessaryStoreRemoval() {
  817. System.out.println("===== multi-query unnecessary stores =====");
  818. try {
  819. myPig.setBatchOn();
  820. myPig.registerQuery("a = load 'passwd' " +
  821. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int,gid:int);");
  822. myPig.registerQuery("b = group a by uname;");
  823. myPig.registerQuery("store b into '/tmp/output1';");
  824. myPig.registerQuery("store b into '/tmp/output2';");
  825. myPig.registerQuery("c = load '/tmp/output1';");
  826. myPig.registerQuery("d = group c by $0;");
  827. myPig.registerQuery("e = store d into '/tmp/output3';");
  828. myPig.registerQuery("f = load '/tmp/output2';");
  829. myPig.registerQuery("g = group f by $0;");
  830. myPig.registerQuery("store g into '/tmp/output4';");
  831. LogicalPlan lp = checkLogicalPlan(1, 2, 10);
  832. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 2, 20);
  833. MROperPlan mp = checkMRPlan(pp, 1, 2, 3);
  834. MapReduceOper mo1 = mp.getRoots().get(0);
  835. MapReduceOper mo2 = mp.getLeaves().get(0);
  836. MapReduceOper mo3 = mp.getLeaves().get(1);
  837. checkPhysicalPlan(mo1.mapPlan, 1, 1, 3);
  838. checkPhysicalPlan(mo1.reducePlan, 1, 1, 2);
  839. PhysicalOperator leaf = mo1.reducePlan.getLeaves().get(0);
  840. Assert.assertTrue(leaf instanceof POSplit);
  841. POSplit split = (POSplit)leaf;
  842. int i = 0;
  843. for (PhysicalPlan p: split.getPlans()) {
  844. checkPhysicalPlan(p, 1, 1, 1);
  845. ++i;
  846. }
  847. Assert.assertEquals(i,2);
  848. checkPhysicalPlan(mo2.mapPlan, 1, 1, 2);
  849. checkPhysicalPlan(mo2.reducePlan, 1, 1, 2);
  850. leaf = mo2.reducePlan.getLeaves().get(0);
  851. Assert.assertTrue(leaf instanceof POStore);
  852. checkPhysicalPlan(mo3.mapPlan, 1, 1, 2);
  853. checkPhysicalPlan(mo3.reducePlan, 1, 1, 2);
  854. leaf = mo3.reducePlan.getLeaves().get(0);
  855. Assert.assertTrue(leaf instanceof POStore);
  856. myPig.executeBatch();
  857. myPig.discardBatch();
  858. Assert.assertTrue(myPig.getPigContext().getDfs().isContainer("/tmp/output1"));
  859. Assert.assertTrue(myPig.getPigContext().getDfs().isContainer("/tmp/output2"));
  860. Assert.assertTrue(myPig.getPigContext().getDfs().isContainer("/tmp/output3"));
  861. Assert.assertTrue(myPig.getPigContext().getDfs().isContainer("/tmp/output4"));
  862. } catch (Exception e) {
  863. e.printStackTrace();
  864. Assert.fail();
  865. }
  866. }
  867. @Test
  868. public void testUnnecessaryStoreRemovalCollapseSplit() {
  869. System.out.println("===== multi-query unnecessary stores collapse split =====");
  870. try {
  871. myPig.setBatchOn();
  872. myPig.registerQuery("a = load 'passwd' " +
  873. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int,gid:int);");
  874. myPig.registerQuery("b = group a by uname;");
  875. myPig.registerQuery("store b into '/tmp/output1';");
  876. myPig.registerQuery("c = load '/tmp/output1';");
  877. myPig.registerQuery("d = group c by $0;");
  878. myPig.registerQuery("e = store d into '/tmp/output2';");
  879. LogicalPlan lp = checkLogicalPlan(1, 1, 6);
  880. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 1, 11);
  881. MROperPlan mp = checkMRPlan(pp, 1, 1, 2);
  882. MapReduceOper mo1 = mp.getRoots().get(0);
  883. MapReduceOper mo2 = mp.getLeaves().get(0);
  884. checkPhysicalPlan(mo1.mapPlan, 1, 1, 3);
  885. checkPhysicalPlan(mo1.reducePlan, 1, 1, 2);
  886. PhysicalOperator leaf = mo1.reducePlan.getLeaves().get(0);
  887. Assert.assertTrue(leaf instanceof POStore);
  888. checkPhysicalPlan(mo2.mapPlan, 1, 1, 2);
  889. checkPhysicalPlan(mo2.reducePlan, 1, 1, 2);
  890. leaf = mo2.reducePlan.getLeaves().get(0);
  891. Assert.assertTrue(leaf instanceof POStore);
  892. myPig.executeBatch();
  893. myPig.discardBatch();
  894. Assert.assertTrue(myPig.getPigContext().getDfs().isContainer("/tmp/output1"));
  895. Assert.assertTrue(myPig.getPigContext().getDfs().isContainer("/tmp/output2"));
  896. } catch (Exception e) {
  897. e.printStackTrace();
  898. Assert.fail();
  899. }
  900. }
  901. @Test
  902. public void testEmptyFilterRemoval() {
  903. System.out.println("===== multi-query empty filters =====");
  904. try {
  905. myPig.setBatchOn();
  906. myPig.registerQuery("a = load 'passwd' " +
  907. "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int,gid:int);");
  908. myPig.registerQuery("b = filter a by uid>0;");
  909. myPig.registerQuery("c = filter a by uid>5;");
  910. myPig.registerQuery("d = filter c by uid<10;");
  911. myPig.registerQuery("store b into '/tmp/output1';");
  912. myPig.registerQuery("store b into '/tmp/output2';");
  913. myPig.registerQuery("store b into '/tmp/output3';");
  914. LogicalPlan lp = checkLogicalPlan(1, 3, 5);
  915. PhysicalPlan pp = checkPhysicalPlan(lp, 1, 3, 10);
  916. MROperPlan mp = checkMRPlan(pp, 1, 1, 1);
  917. MapReduceOper mo = mp.getRoots().get(0);
  918. checkPhysicalPlan(mo.mapPlan, 1, 1, 4);
  919. PhysicalOperator leaf = mo.mapPlan.getLeaves().get(0);
  920. Assert.assertTrue(leaf instanceof POSplit);
  921. POSplit split = (POSplit)leaf;
  922. int i = 0;
  923. for (PhysicalPlan p: split.getPlans()) {
  924. checkPhysicalPlan(p, 1, 1, 1);
  925. ++i;
  926. }
  927. Assert.assertEquals(i,3);
  928. myPig.executeBatch();
  929. myPig.discardBatch();
  930. } catch (Exception e) {
  931. e.printStackTrace();
  932. Assert.fail();
  933. }
  934. }
  935. @Test
  936. public void testMultiQueryWithDescribe() {
  937. System.out.println("===== multi-query with describe =====");
  938. try {
  939. String script = "a = load 'passwd' "
  940. + "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int,gid:int);"
  941. + "b = filter a by uid > 5;"
  942. + "describe b;"
  943. + "store b into '/tmp/output1';\n";
  944. GruntParser parser = new GruntParser(new StringReader(script));
  945. parser.setInteractive(false);
  946. parser.setParams(myPig);
  947. parser.parseStopOnError();
  948. } catch (Exception e) {
  949. e.printStackTrace();
  950. Assert.fail();
  951. }
  952. }
  953. @Test
  954. public void testMultiQueryWithIllustrate() {
  955. System.out.println("===== multi-query with illustrate =====");
  956. try {
  957. String script = "a = load 'passwd' "
  958. + "using PigStorage(':') as (uname:chararray, passwd:chararray, uid:int,gid:int);"
  959. + "b = filter a by uid > 5;"
  960. + "illustrate b;"
  961. + "store b into '/tmp/output1';\n";
  962. GruntParser parser = new GruntParser(new StringReader(script));
  963. parser.setInteractive(false);
  964. parser.setParams(myPig);
  965. parser.parseStopOnError();
  966. } catch (Exception e) {

Large files files are truncated, but you can click here to view the full file