PageRenderTime 56ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/test/org/apache/pig/test/TestLogicalPlanBuilder.java

https://github.com/dorefiend/pig
Java | 2151 lines | 1657 code | 221 blank | 273 comment | 19 complexity | 982b2ab5858dbef794f71fc44491c377 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 static org.junit.Assert.assertEquals;
  20. import java.io.IOException;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Properties;
  25. import org.apache.hadoop.fs.Path;
  26. import org.apache.hadoop.mapreduce.InputFormat;
  27. import org.apache.hadoop.mapreduce.Job;
  28. import org.apache.hadoop.mapreduce.RecordReader;
  29. import org.apache.pig.ExecType;
  30. import org.apache.pig.FuncSpec;
  31. import org.apache.pig.LoadCaster;
  32. import org.apache.pig.LoadFunc;
  33. import org.apache.pig.PigException;
  34. import org.apache.pig.PigServer;
  35. import org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.PigSplit;
  36. import org.apache.pig.builtin.PigStorage;
  37. import org.apache.pig.data.BagFactory;
  38. import org.apache.pig.data.DataBag;
  39. import org.apache.pig.data.DataType;
  40. import org.apache.pig.data.Tuple;
  41. import org.apache.pig.data.TupleFactory;
  42. import org.apache.pig.impl.PigContext;
  43. import org.apache.pig.impl.builtin.GFAny;
  44. import org.apache.pig.impl.logicalLayer.schema.Schema;
  45. import org.apache.pig.impl.util.LogUtils;
  46. import org.apache.pig.impl.util.Utils;
  47. import org.apache.pig.newplan.Operator;
  48. import org.apache.pig.newplan.logical.expression.ConstantExpression;
  49. import org.apache.pig.newplan.logical.expression.LogicalExpressionPlan;
  50. import org.apache.pig.newplan.logical.expression.ProjectExpression;
  51. import org.apache.pig.newplan.logical.relational.LOCogroup;
  52. import org.apache.pig.newplan.logical.relational.LOForEach;
  53. import org.apache.pig.newplan.logical.relational.LOGenerate;
  54. import org.apache.pig.newplan.logical.relational.LOLoad;
  55. import org.apache.pig.newplan.logical.relational.LOSort;
  56. import org.apache.pig.newplan.logical.relational.LogicalPlan;
  57. import org.apache.pig.newplan.logical.relational.LogicalSchema;
  58. import org.apache.pig.test.utils.Identity;
  59. import org.junit.Before;
  60. import org.junit.Test;
  61. import junit.framework.Assert;
  62. import junit.framework.AssertionFailedError;
  63. public class TestLogicalPlanBuilder {
  64. PigContext pigContext = new PigContext(ExecType.LOCAL, new Properties());
  65. private PigServer pigServer = null;
  66. @Before
  67. public void setUp() throws Exception {
  68. pigServer = new PigServer( pigContext );
  69. pigContext.connect();
  70. }
  71. @Test
  72. public void testQuery1() throws Exception {
  73. String query = "foreach (load 'a') generate $1,$2;";
  74. buildPlan(query);
  75. }
  76. @Test
  77. public void testQuery2() throws Exception {
  78. String query = "foreach (load 'a' using " + PigStorage.class.getName() + "(':')) generate $1, 'aoeuaoeu' ;";
  79. buildPlan(query);
  80. }
  81. // TODO FIX Query3 and Query4
  82. @Test
  83. public void testQuery3() throws Exception {
  84. String query = "foreach (cogroup (load 'a' as (u:int)) by $0, (load 'b' as (v:int) ) by $0) generate org.apache.pig.builtin.AVG($1);";
  85. buildPlan(query);
  86. }
  87. @Test
  88. public void testQuery4() throws Exception {
  89. String query = "foreach (load 'a' as (u:int, v:bag{T:tuple(t:double)})) generate AVG($1) ;";
  90. buildPlan(query);
  91. }
  92. @Test
  93. public void testQuery5() throws Exception {
  94. String query = "foreach (group (load 'a') ALL) generate $1 ;";
  95. buildPlan(query);
  96. }
  97. @Test
  98. public void testQuery6() throws Exception {
  99. String query = "foreach (group (load 'a') by $1) generate group, '1' ;";
  100. buildPlan(query);
  101. }
  102. @Test
  103. public void testQuery7() throws Exception {
  104. String query = "foreach (load 'a' using " + PigStorage.class.getName() + "()) generate $1 ;";
  105. buildPlan(query);
  106. }
  107. @Test
  108. public void testQuery10() throws Exception {
  109. String query = "foreach (cogroup (load 'a') by ($1), (load 'b') by ($1)) generate $1.$1, $2.$1 ;";
  110. buildPlan(query);
  111. }
  112. // TODO FIX Query11 and Query12
  113. @Test
  114. public void testQuery11() throws Exception {
  115. String query = " foreach (group (load 'a' as (u:int)) by $0, (load 'b' as (v:long)) by $0) generate group, AVG($1) ;";
  116. buildPlan(query);
  117. }
  118. @Test
  119. public void testQuery12() throws Exception {
  120. String query = "foreach (load 'a' using " + PigStorage.class.getName() + "() as (v: long, u:bag{T:tuple(t:double)} ) ) generate AVG($1) ;";
  121. buildPlan(query);
  122. }
  123. @Test
  124. public void testQuery13() throws Exception {
  125. String query = "foreach (cogroup (load 'a') ALL) generate group ;";
  126. buildPlan(query);
  127. }
  128. @Test
  129. public void testQuery14() throws Exception {
  130. String query = "foreach (group (load 'a') by ($6, $7)) generate flatten(group) ;";
  131. buildPlan(query);
  132. }
  133. @Test
  134. public void testQuery15() throws Exception {
  135. String query = " foreach (load 'a') generate $1, 'hello', $3 ;";
  136. buildPlan(query);
  137. }
  138. @Test
  139. public void testQuery100() throws Exception {
  140. // test define syntax
  141. String query = "define FUNC ARITY();";
  142. buildPlan(query);
  143. }
  144. @Test
  145. public void testQueryFail1() throws Exception {
  146. String query = " foreach (group (A = load 'a') by $1) generate A.'1' ;";
  147. try {
  148. buildPlan(query);
  149. } catch (AssertionFailedError e) {
  150. return;
  151. }
  152. Assert.fail("Test case should fail" );
  153. }
  154. @Test
  155. public void testQueryFail2() throws Exception {
  156. String query = "foreach group (load 'a') by $1 generate $1.* ;";
  157. try {
  158. buildPlan(query);
  159. } catch (AssertionFailedError e) {
  160. return;
  161. }
  162. Assert.fail("Test case should fail" );
  163. }
  164. @Test
  165. public void testQueryFail3() throws Exception {
  166. String query = "A = generate DISTINCT foreach (load 'a');";
  167. try {
  168. LogicalPlan lp = buildPlan(query);
  169. System.out.println( lp.toString() );
  170. } catch (AssertionFailedError e) {
  171. return;
  172. }
  173. Assert.fail("Test case should fail" );
  174. }
  175. @Test
  176. public void testQueryFail4() throws Exception {
  177. String query = "A = generate [ORDER BY $0][$3, $4] foreach (load 'a');";
  178. try {
  179. buildPlan(query);
  180. } catch (AssertionFailedError e) {
  181. return;
  182. }
  183. Assert.fail("Test case should fail" );
  184. }
  185. @Test
  186. public void testQueryFail5() throws Exception {
  187. String query = "A = generate " + TestApplyFunc.class.getName() + "($2.*) foreach (load 'a');";
  188. try {
  189. buildPlan(query);
  190. } catch (AssertionFailedError e) {
  191. return;
  192. }
  193. Assert.fail("Test case should fail" );
  194. }
  195. /**
  196. * User generate functions must be in default package Bug 831620 - fixed
  197. */
  198. // TODO FIX Query17
  199. @Test
  200. public void testQuery17() throws Exception {
  201. String query = "foreach (load 'A')" + "generate " + TestApplyFunc.class.getName() + "($1);";
  202. buildPlan(query);
  203. }
  204. static public class TestApplyFunc extends org.apache.pig.EvalFunc<Tuple> {
  205. @Override
  206. public Tuple exec(Tuple input) throws IOException {
  207. Tuple output = TupleFactory.getInstance().newTuple(input.getAll());
  208. return output;
  209. }
  210. }
  211. /**
  212. * Validate that parallel is parsed correctly Bug 831714 - fixed
  213. */
  214. @Test //PIG-1996
  215. public void testQuery18() throws Exception {
  216. String query = "store (FOREACH (group (load 'a') ALL PARALLEL 16) generate group ) into 'y';";
  217. LogicalPlan lp = buildPlan(query);
  218. Operator root = lp.getSources().get(0);
  219. List<Operator> listOp = lp.getSuccessors(root);
  220. Operator lo = listOp.get(0);
  221. if (lo instanceof LOCogroup) {
  222. Assert.assertEquals( 1, ((LOCogroup) lo).getRequestedParallelism() );//Local mode, paraallel = 1
  223. } else {
  224. Assert.fail("Error: Unexpected Parse Tree output");
  225. }
  226. }
  227. @Test
  228. public void testQuery19() throws Exception {
  229. String query = "a = load 'a';" +
  230. "b = filter a by $1 == '3';";
  231. buildPlan( query );
  232. }
  233. @Test
  234. public void testQuery20() throws Exception {
  235. String query = "foreach (load 'a') generate ($1 == '3'? $2 : $3) ;";
  236. buildPlan(query);
  237. }
  238. @Test
  239. public void testQuery21() throws Exception {
  240. String query = "A = load 'a';" +
  241. "B = load 'b';" +
  242. "foreach (cogroup A by ($1), B by ($1)) generate A, flatten(B.($1, $2, $3));";
  243. buildPlan( query );
  244. }
  245. @Test
  246. public void testQuery22() throws Exception {
  247. String query = "A = load 'a';" +
  248. "B = load 'b';" +
  249. "C = cogroup A by ($1), B by ($1);" +
  250. "foreach C { " +
  251. "B = order B by $0; " +
  252. "generate FLATTEN(A), B.($1, $2, $3) ;" + "};" ;
  253. buildPlan(query);
  254. }
  255. @Test
  256. public void testQuery22Fail() throws Exception {
  257. String query = "A = load 'a' as (a:int, b: double);" +
  258. "B = group A by (*, $0);";
  259. try {
  260. buildPlan(query);
  261. } catch (AssertionFailedError e) {
  262. Assert.assertTrue(e.getMessage().contains("Grouping attributes can either be star (*"));
  263. }
  264. }
  265. @Test
  266. public void testQuery23() throws Exception {
  267. String query = "A = load 'a';" +
  268. "B = load 'b';" +
  269. "C = cogroup A by ($1), B by ($1);" +
  270. "foreach C { " +
  271. "A = Distinct A; " +
  272. "B = FILTER A BY $1 < 'z'; " +
  273. //TODO
  274. //A sequence of filters within a foreach translates to
  275. //a split statement. Currently it breaks as adding an
  276. //additional output to the filter fails as filter supports
  277. //single output
  278. "C = FILTER A BY $2 == $3;" +
  279. "B = ORDER B BY $1;" +
  280. "GENERATE A, FLATTEN(B.$0);" +
  281. "};";
  282. buildPlan(query);
  283. }
  284. @Test
  285. public void testQuery23Fail() throws Exception {
  286. String query = "A = load 'a' as (a: int, b:double);" +
  287. "B = load 'b';" +
  288. "C = cogroup A by (*, $0), B by ($0, $1);";
  289. boolean exceptionThrown = false;
  290. try {
  291. buildPlan(query);
  292. } catch (AssertionFailedError e) {
  293. Assert.assertTrue(e.getMessage().contains("The arity of cogroup/group by columns " +
  294. "do not match"));
  295. exceptionThrown = true;
  296. }
  297. Assert.assertTrue(exceptionThrown);
  298. }
  299. @Test
  300. public void testQuery23Fail2() throws Exception {
  301. String query = "A = load 'a';" +
  302. "B = load 'b';" +
  303. "C = cogroup A by (*, $0), B by ($0, $1);";
  304. boolean exceptionThrown = false;
  305. try {
  306. buildPlan(query);
  307. } catch (AssertionFailedError e) {
  308. exceptionThrown = true;
  309. }
  310. Assert.assertTrue(exceptionThrown);
  311. }
  312. @Test
  313. public void testQuery23Fail3() throws Exception {
  314. String query = "A = load 'a' as (a: int, b:double);" +
  315. "B = load 'b' as (a:int);" +
  316. "C = cogroup A by *, B by *;";
  317. boolean exceptionThrown = false;
  318. try {
  319. buildPlan(query);
  320. } catch (AssertionFailedError e) {
  321. Assert.assertTrue(e.getMessage().contains("The arity of cogroup/group by columns " +
  322. "do not match"));
  323. exceptionThrown = true;
  324. }
  325. Assert.assertTrue(exceptionThrown);
  326. }
  327. @Test
  328. public void testQuery24() throws Exception {
  329. String query = "a = load 'a';" + "foreach a generate (($0 == $1) ? 'a' : $2), $4 ;";
  330. buildPlan(query);
  331. }
  332. @Test
  333. public void testQuery25() throws Exception {
  334. String query = "foreach (load 'a' as (u:bag{}, v, w) ) {" +
  335. "B = FILTER $0 BY (($1 == $2) AND ('a' < 'b'));" +
  336. "generate B;" +
  337. "};";
  338. buildPlan(query);
  339. }
  340. @Test
  341. public void testQuery26() throws Exception {
  342. String query = "foreach (load 'a') generate ((NOT (($1 == $2) OR ('a' < 'b'))) ? 'a' : $2), 'x' ;";
  343. buildPlan(query);
  344. }
  345. // TODO FIX Query27 and Query28
  346. @Test
  347. public void testQuery27() throws Exception {
  348. String query = "foreach (load 'a' as (u, v:bag{}, w, x:bag{}, y) ){" +
  349. "A = DISTINCT $3.$1;" +
  350. " generate " + TestApplyFunc.class.getName() + "($2, $1.($1, $4));" +
  351. "};";
  352. buildPlan(query);
  353. }
  354. @Test
  355. public void testQuery28() throws Exception {
  356. String query = "foreach (load 'a') generate " + TestApplyFunc.class.getName() + "($2, " + TestApplyFunc.class.getName() + "($2.$3));";
  357. buildPlan(query);
  358. }
  359. @Test
  360. public void testQuery29() throws Exception {
  361. String query = "load 'myfile' using " + TestStorageFunc.class.getName() + "() as (col1);";
  362. buildPlan(query);
  363. }
  364. @Test
  365. public void testQuery30() throws Exception {
  366. String query = "load 'myfile' using " + TestStorageFunc.class.getName() + "() as (col1, col2);";
  367. buildPlan(query);
  368. }
  369. public static class TestStorageFunc extends LoadFunc{
  370. public Tuple getNext() throws IOException {
  371. return null;
  372. }
  373. @Override
  374. public InputFormat getInputFormat() throws IOException {
  375. return null;
  376. }
  377. @Override
  378. public LoadCaster getLoadCaster() throws IOException {
  379. return null;
  380. }
  381. @Override
  382. public void prepareToRead(RecordReader reader, PigSplit split)
  383. throws IOException {
  384. }
  385. @Override
  386. public String relativeToAbsolutePath(String location, Path curDir)
  387. throws IOException {
  388. return null;
  389. }
  390. @Override
  391. public void setLocation(String location, Job job) throws IOException {
  392. }
  393. }
  394. @Test
  395. public void testQuery31() throws Exception {
  396. String query = "load 'myfile' as (col1, col2);";
  397. buildPlan(query);
  398. }
  399. @Test
  400. public void testQuery32() throws Exception {
  401. String query = "foreach (load 'myfile' as (col1, col2 : tuple(sub1, sub2), col3 : tuple(bag1))) generate col1 ;";
  402. buildPlan(query);
  403. }
  404. @Test
  405. public void testQuery33() throws Exception {
  406. String query = "A = load 'a' as (aCol1, aCol2);" +
  407. "B = load 'b' as (bCol1, bCol2);" +
  408. "C = cogroup A by (aCol1), B by bCol1;" +
  409. "foreach C generate group, A.aCol1;";
  410. buildPlan(query);
  411. }
  412. @Test
  413. //TODO: Nested schemas don't work now. Probably a bug in the new parser.
  414. public void testQuery34() throws Exception {
  415. String query = "A = load 'a' as (aCol1, aCol2 : tuple(subCol1, subCol2));" +
  416. "A = filter A by aCol1 == '1';" +
  417. "B = load 'b' as (bCol1, bCol2);" +
  418. "foreach (cogroup A by (aCol1), B by bCol1 ) generate A.aCol2, B.bCol2 ;";
  419. buildPlan(query);
  420. }
  421. @Test
  422. public void testQuery35() throws Exception {
  423. String query = "foreach (load 'a' as (col1, col2)) generate col1, col2 ;";
  424. buildPlan(query);
  425. }
  426. @Test
  427. public void testQuery36() throws Exception {
  428. String query = "foreach (cogroup ( load 'a' as (col1, col2)) by col1) generate $1.(col2, col1);";
  429. buildPlan(query);
  430. }
  431. @Test
  432. public void testQueryFail37() throws Exception {
  433. String query = "A = load 'a'; asdasdas";
  434. try{
  435. buildPlan(query);
  436. }catch(AssertionFailedError e){
  437. return;
  438. }
  439. Assert.fail( "Query should fail." );
  440. }
  441. @Test
  442. public void testQuery38() throws Exception {
  443. String query = "c = cross (load 'a'), (load 'b');";
  444. buildPlan(query);
  445. }
  446. // TODO FIX Query39 and Query40
  447. @Test
  448. public void testQuery39() throws Exception{
  449. String query = "a = load 'a' as (url, host, ranking:double);" +
  450. "b = group a by (url,host); " +
  451. "c = foreach b generate flatten(group.url), SUM(a.ranking) as totalRank;";
  452. buildPlan(query);
  453. query += "d = filter c by totalRank > 10;" +
  454. "e = foreach d generate totalRank;";
  455. buildPlan( query );
  456. }
  457. @Test
  458. public void testQueryFail39() throws Exception{
  459. String query = "a = load 'a' as (url, host, ranking);" +
  460. "b = group a by (url,host); " +
  461. "c = foreach b generate flatten(group.url), SUM(a.ranking) as totalRank;" +
  462. "d = filter c by totalRank > '10';" +
  463. "e = foreach d generate url;";
  464. try {
  465. buildPlan(query);//url has been falttened and hence the failure
  466. } catch(AssertionFailedError e) {
  467. Assert.assertTrue(e.getMessage().contains("Exception"));
  468. }
  469. }
  470. @Test
  471. public void testQuery40() throws Exception {
  472. String query = "a = FILTER (load 'a') BY IsEmpty($2);";
  473. buildPlan( query +"a = FILTER (load 'a') BY (IsEmpty($2) AND ($3 == $2));" );
  474. }
  475. @Test
  476. public void testQueryFail41() throws Exception {
  477. try {
  478. buildPlan("a = load 'a';" + "b = a as (host,url);");
  479. } catch (AssertionFailedError e) {
  480. return;
  481. }
  482. // TODO
  483. // the following statement was earlier present
  484. // eventually when we do allow assignments of the form
  485. // above, we should test with the line below
  486. // uncommented
  487. //buildPlan("foreach b generate host;");
  488. Assert.fail( "Query should fail." );
  489. }
  490. @Test
  491. public void testQuery42() throws Exception {
  492. String q = "a = load 'a';" +
  493. "b = foreach a generate $0 as url, $1 as ranking;" +
  494. "foreach b generate url;";
  495. buildPlan( q );
  496. }
  497. @Test
  498. public void testQuery43() throws Exception {
  499. String q = "a = load 'a' as (url,hitCount);" +
  500. "b = load 'a' as (url,ranking);" +
  501. "c = cogroup a by url, b by url;" +
  502. "d = foreach c generate group,flatten(a),flatten(b);" +
  503. "e = foreach d generate group, a::url, b::url, b::ranking;";
  504. buildPlan( q );
  505. }
  506. @Test
  507. public void testQueryFail43() throws Exception {
  508. String q = "a = load 'a' as (name, age, gpa);" +
  509. "b = load 'b' as (name, height);";
  510. try {
  511. String query = q + "c = cogroup a by (name, age), b by (height);";
  512. buildPlan(query);
  513. } catch (AssertionFailedError e) {
  514. return;
  515. }
  516. Assert.fail( "Query should fail." );
  517. }
  518. @Test
  519. public void testQuery44() throws Exception {
  520. String q = "a = load 'a' as (url, pagerank);" +
  521. "b = load 'b' as (url, query, ranking);" +
  522. "c = cogroup a by (pagerank#'nonspam', url) , b by (ranking, url) ;" +
  523. "foreach c generate group.url;";
  524. buildPlan( q );
  525. }
  526. @Test
  527. public void testQueryFail44() throws Throwable {
  528. PigServer pig = null;
  529. try {
  530. pig = new PigServer("local");
  531. } catch (IOException e) {
  532. Assert.assertTrue(false); // pig server failed for some reason
  533. }
  534. pig.registerFunction("myTr",
  535. new FuncSpec(GFAny.class.getName() + "('tr o 0')"));
  536. try{
  537. pig.registerQuery("b = foreach (load 'a') generate myTr(myTr(*));");
  538. }catch(Exception e){
  539. return;
  540. }
  541. Assert.assertTrue(false);
  542. }
  543. @Test
  544. public void testQuery57() throws Exception {
  545. String query = "foreach (load 'a' as (u:int, v:long, w:int)) generate ($1+$2), ($1-$2), ($1*$2), ($1/$2), ($1%$2), -($1) ;";
  546. buildPlan(query);
  547. }
  548. @Test
  549. public void testQuery58() throws Exception {
  550. String query = "a = load 'a' as (name, age, gpa);" +
  551. "b = group a by name;" +
  552. "foreach b {d = a.name; generate group, d;};";
  553. buildPlan(query);
  554. }
  555. @Test
  556. public void testQueryFail58() throws Exception{
  557. String query = "a = load 'a' as (url, host, ranking);" +
  558. "b = group a by url; ";
  559. try {
  560. buildPlan(query + "c = foreach b generate group.url;");
  561. } catch (AssertionFailedError e) {
  562. Assert.assertTrue(e.getMessage().contains("Exception"));
  563. }
  564. }
  565. @Test
  566. public void testQuery59() throws Exception {
  567. String query = "a = load 'a' as (name, age, gpa);" +
  568. "b = load 'b' as (name, height);" +
  569. "c = join a by name, b by name;";
  570. buildPlan(query);
  571. }
  572. @Test
  573. public void testQuery60() throws Exception {
  574. String query = "a = load 'a' as (name, age, gpa);" +
  575. "b = load 'b' as (name, height);" +
  576. "c = cross a,b;";
  577. buildPlan(query);
  578. }
  579. @Test
  580. public void testQuery61() throws Exception {
  581. String query = "a = load 'a' as (name, age, gpa);" +
  582. "b = load 'b' as (name, height);" +
  583. "c = union a,b;";
  584. buildPlan(query);
  585. }
  586. @Test
  587. public void testQuery62() throws Exception {
  588. String query = "a = load 'a' as (name, age, gpa);" +
  589. "b = load 'b' as (name, height);" +
  590. "c = cross a,b;" +
  591. "d = order c by b::name, height, a::gpa;" +
  592. "e = order a by name, age, gpa desc;" +
  593. "f = order a by $0 asc, age, gpa desc;" +
  594. "g = order a by * asc;" +
  595. "h = cogroup a by name, b by name;" +
  596. "i = foreach h {i1 = order a by *; generate i1;};";
  597. buildPlan(query);
  598. }
  599. @Test
  600. public void testQueryFail62() throws Exception {
  601. String query = "a = load 'a' as (name, age, gpa);" +
  602. "b = load 'b' as (name, height);" +
  603. "c = cross a,b;" +
  604. "d = order c by name, b::name, height, a::gpa;";
  605. try {
  606. buildPlan(query);
  607. } catch (AssertionFailedError e) {
  608. Assert.assertTrue(e.getMessage().contains("Exception"));
  609. }
  610. }
  611. @Test
  612. public void testQuery63() throws Exception {
  613. String query = "a = load 'a' as (name, details: tuple(age, gpa));" +
  614. "b = group a by details;" +
  615. "d = foreach b generate group.age;" +
  616. "e = foreach a generate name, details;";
  617. buildPlan(query);
  618. }
  619. @Test
  620. public void testQueryFail63() throws Exception {
  621. String query = "foreach (load 'myfile' as (col1, col2 : (sub1, sub2), col3 : (bag1))) generate col1 ;";
  622. try {
  623. buildPlan(query);
  624. } catch (AssertionFailedError e) {
  625. Assert.assertTrue(e.getMessage().contains("Exception"));
  626. }
  627. }
  628. @Test
  629. public void testQuery64() throws Exception {
  630. String query = "a = load 'a' as (name: chararray, details: tuple(age, gpa), mymap: map[]);" +
  631. "c = load 'a' as (name, details: bag{mytuple: tuple(age: int, gpa)});" +
  632. "b = group a by details;" +
  633. "d = foreach b generate group.age;" +
  634. "e = foreach a generate name, details;" +
  635. "f = LOAD 'myfile' AS (garage: bag{tuple1: tuple(num_tools: int)}, links: bag{tuple2: tuple(websites: chararray)}, page: bag{something_stupid: tuple(yeah_double: double)}, coordinates: bag{another_tuple: tuple(ok_float: float, bite_the_array: bytearray, bag_of_unknown: bag{})});";
  636. buildPlan(query);
  637. }
  638. @Test
  639. public void testQueryFail64() throws Exception {
  640. String query = "foreach (load 'myfile' as (col1, col2 : bag{age: int})) generate col1 ;";
  641. try {
  642. buildPlan(query);
  643. } catch (AssertionFailedError e) {
  644. return;
  645. }
  646. Assert.fail( "query should fail" );
  647. }
  648. @Test
  649. public void testQuery65() throws Exception {
  650. String q = "a = load 'a' as (name, age, gpa);" +
  651. "b = load 'b' as (name, height);" +
  652. "c = cogroup a by (name, age), b by (name, height);" +
  653. "d = foreach c generate group.name, a.name as aName, b.name as bname;";
  654. buildPlan( q );
  655. }
  656. @Test
  657. public void testQueryFail65() throws Exception {
  658. String q = "a = load 'a' as (name, age, gpa);" +
  659. "b = load 'b' as (name, height);" +
  660. "c = cogroup a by (name, age), b by (name, height);" +
  661. "d = foreach c generate group.name, a.name, b.height as age, a.age;";
  662. try {
  663. buildPlan( q );
  664. } catch (AssertionFailedError e) {
  665. Assert.assertTrue(e.getMessage().contains("Exception"));
  666. }
  667. }
  668. @Test
  669. public void testQuery67() throws Exception {
  670. String q = " a = load 'input1' as (name, age, gpa);" +
  671. " b = foreach a generate age, age * 10L, gpa/0.2f, {(16, 4.0e-2, 'hello')};";
  672. buildPlan( q );
  673. }
  674. @Test
  675. public void testQuery68() throws Exception {
  676. String q = " a = load 'input1';" +
  677. " b = foreach a generate 10, {(16, 4.0e-2, 'hello'), (0.5f, 12l, 'another tuple')};";
  678. buildPlan( q );
  679. }
  680. @Test
  681. public void testQuery69() throws Exception {
  682. String q = " a = load 'input1';" +
  683. " b = foreach a generate {(16, 4.0e-2, 'hello'), (0.5f, 'another tuple', 12L, (1))};";
  684. buildPlan( q );
  685. }
  686. @Test
  687. public void testQuery70() throws Exception {
  688. String q = " a = load 'input1';" +
  689. " b = foreach a generate ['10'#'hello', '4.0e-2'#10L, '0.5f'#(1), 'world'#42, '42'#{('guide')}] as mymap:map[];" +
  690. " c = foreach b generate mymap#'10';";
  691. buildPlan( q );
  692. }
  693. @Test
  694. public void testQueryFail67() throws Exception {
  695. String q = " a = load 'input1' as (name, age, gpa);" +
  696. " b = foreach a generate age, age * 10L, gpa/0.2f, {16, 4.0e-2, 'hello'};";
  697. try {
  698. buildPlan(q);
  699. } catch (AssertionFailedError e) {
  700. return;
  701. }
  702. Assert.fail( "query should fail" );
  703. }
  704. @Test
  705. public void testQueryFail68() throws Exception {
  706. String q = " a = load 'input1' as (name, age, gpa);";
  707. try {
  708. buildPlan( q +
  709. " b = foreach a generate {(16 L, 4.0e-2, 'hello'), (0.5f, 'another tuple', 12L, {()})};");
  710. } catch (AssertionFailedError e) {
  711. return;
  712. }
  713. Assert.fail( "query should fail" );
  714. }
  715. @Test
  716. public void testQuery71() throws Exception {
  717. String q = "split (load 'a') into x if $0 > '7', y if $0 < '7';" +
  718. "b = foreach x generate $0;" +
  719. "c = foreach y generate $1;";
  720. buildPlan( q );
  721. }
  722. @Test
  723. public void testQuery72() throws Exception {
  724. String q = "split (load 'a') into x if $0 > 7, y if $0 < 7;" +
  725. "b = foreach x generate (int)$0;" +
  726. "c = foreach y generate (bag{})$1;" +
  727. "d = foreach y generate (int)($2/2);" +
  728. "e = foreach y generate (bag{tuple(int, float)})($2);" +
  729. "f = foreach x generate (tuple(int, float))($3);" +
  730. "g = foreach x generate (tuple())($4);" +
  731. "h = foreach x generate (chararray)($5);";
  732. buildPlan( q );
  733. }
  734. @Test
  735. public void testQueryFail72() throws Exception {
  736. boolean catchEx = false;
  737. String q = "split (load 'a') into x if $0 > '7', y if $0 < '7';";
  738. try {
  739. buildPlan( q + "c = foreach y generate (bag)$1;");
  740. } catch (AssertionFailedError e) {
  741. catchEx = true;
  742. }
  743. Assert.assertTrue( catchEx );
  744. catchEx = false;
  745. try {
  746. buildPlan( q + "c = foreach y generate (bag{int, float})$1;");
  747. } catch (AssertionFailedError e) {
  748. catchEx = true;
  749. }
  750. Assert.assertTrue( catchEx );
  751. catchEx = false;
  752. try {
  753. buildPlan( q + "c = foreach y generate (tuple)$1;");
  754. } catch (AssertionFailedError e) {
  755. catchEx = true;
  756. }
  757. Assert.assertTrue( catchEx );
  758. }
  759. @Test
  760. public void testQuery73() throws Exception {
  761. String q = "split (load 'a') into x if $0 > '7', y if $0 < '7';" +
  762. "b = filter x by $0 matches '^fred.*';" +
  763. "c = foreach y generate $0, ($0 matches 'yuri.*' ? $1 - 10 : $1);";
  764. buildPlan( q );
  765. }
  766. @Test
  767. public void testQuery74() throws Exception {
  768. String q = "a = load 'a' as (field1: int, field2: long);" +
  769. "b = load 'a' as (field1: bytearray, field2: double);" +
  770. "c = group a by field1, b by field1;" +
  771. "d = cogroup a by ((field1+field2)*field1), b by field1;";
  772. buildPlan( q );
  773. }
  774. @Test
  775. public void testQuery77() throws Exception {
  776. buildPlan("limit (load 'a') 100;");
  777. }
  778. @Test
  779. public void testLimitWithLong() throws Exception {
  780. buildPlan("limit (load 'a') 100L;");
  781. }
  782. @Test
  783. public void testQuery75() throws Exception {
  784. String q = "a = union (load 'a'), (load 'b'), (load 'c');";
  785. buildPlan( q + "b = foreach a {generate $0;};");
  786. }
  787. @Test
  788. public void testQuery76() throws Exception {
  789. String q = "split (load 'a') into x if $0 > '7', y if $0 < '7';" +
  790. "b = filter x by $0 IS NULL;" +
  791. "c = filter y by $0 IS NOT NULL;" +
  792. "d = foreach b generate $0, ($1 IS NULL ? 0 : $1 - 7);" +
  793. "e = foreach c generate $0, ($1 IS NOT NULL ? $1 - 5 : 0);";
  794. buildPlan( q );
  795. }
  796. @Test
  797. public void testQuery80() throws Exception {
  798. String q = "a = load 'input1' as (name, age, gpa);" +
  799. "b = filter a by age < '20';" +
  800. "c = group b by age;" +
  801. "d = foreach c {"
  802. + "cf = filter b by gpa < '3.0';"
  803. + "cp = cf.gpa;"
  804. + "cd = distinct cp;"
  805. + "co = order cd by gpa;"
  806. + "generate group, flatten(co);"
  807. //+ "generate group, flatten(cd);"
  808. + "};";
  809. buildPlan(q);
  810. }
  811. @Test
  812. public void testQuery81() throws Exception {
  813. String q = "a = load 'input1' using PigStorage() as (name, age, gpa);" +
  814. "split a into b if name lt 'f', c if (name gte 'f' and name lte 'h'), d if name gt 'h';";
  815. buildPlan( q );
  816. }
  817. @Test
  818. public void testQueryFail81() throws Exception {
  819. String q = "a = load 'input1' using PigStorage() as (name, age, gpa);";
  820. try {
  821. buildPlan(q + "split a into b if name lt 'f', c if (name ge 'f' and name le 'h'), d if name gt 'h';");
  822. } catch (AssertionFailedError e) {
  823. return;
  824. }
  825. Assert.fail( "Query should fail." );
  826. }
  827. @Test
  828. public void testQuery82() throws Exception {
  829. String q = "a = load 'myfile';" +
  830. "b = group a by $0;" +
  831. "c = foreach b {"
  832. + "c1 = order $1 by *;"
  833. + "c2 = $1.$0;"
  834. + "generate flatten(c1), c2;"
  835. + "};";
  836. buildPlan(q);
  837. }
  838. @Test
  839. public void testQueryFail82() throws Exception {
  840. String q = "a = load 'myfile';" +
  841. "b = group a by $0;" +
  842. "c = foreach b {"
  843. + "c1 = order $1 by *;"
  844. + "c2 = $1;"
  845. + "generate flatten(c1), c2;"
  846. + "};";
  847. try {
  848. buildPlan(q);
  849. } catch (AssertionFailedError e) {
  850. Assert.assertTrue(e.getMessage().contains("Exception"));
  851. }
  852. }
  853. @Test
  854. public void testQuery83() throws Exception {
  855. String q = "a = load 'input1' as (name, age, gpa);" +
  856. "b = filter a by age < '20';" +
  857. "c = group b by (name,age);" +
  858. "d = foreach c {"
  859. + "cf = filter b by gpa < '3.0';"
  860. + "cp = cf.gpa;"
  861. + "cd = distinct cp;"
  862. + "co = order cd by gpa;"
  863. + "generate group, flatten(co);"
  864. + "};";
  865. buildPlan(q);
  866. }
  867. @Test
  868. public void testQuery84() throws Exception {
  869. String q = "a = load 'input1' as (name, age, gpa);" +
  870. "b = filter a by age < '20';" +
  871. "c = group b by (name,age);" +
  872. "d = foreach c {"
  873. + "cf = filter b by gpa < '3.0';"
  874. + "cp = cf.$2;"
  875. + "cd = distinct cp;"
  876. + "co = order cd by gpa;"
  877. + "generate group, flatten(co);"
  878. + "};";
  879. buildPlan(q);
  880. }
  881. @Test
  882. public void testQuery85() throws Exception {
  883. LogicalPlan lp;
  884. String query = "a = load 'myfile' as (name, age, gpa);" +
  885. "b = group a by (name, age);";
  886. lp = buildPlan( query + "store b into 'output';");
  887. Operator store = lp.getSinks().get(0);
  888. LOCogroup cogroup = (LOCogroup) lp.getPredecessors(store).get(0);
  889. LogicalSchema actual = cogroup.getSchema();
  890. System.out.println( actual.toString( false ) );
  891. Assert.assertTrue( actual.toString( false ).equals( "group:tuple(name:bytearray,age:bytearray),a:bag{:tuple(name:bytearray,age:bytearray,gpa:bytearray)}" ) );
  892. lp = buildPlan(query +
  893. "c = foreach b generate group.name, group.age, COUNT(a.gpa);" +
  894. "store c into 'output';");
  895. store = lp.getSinks().get(0);
  896. LOForEach foreach = (LOForEach) lp.getPredecessors(store).get(0);
  897. Assert.assertTrue( foreach.getSchema().toString( false ).equals("name:bytearray,age:bytearray,:long") );
  898. }
  899. @Test
  900. public void testQuery86() throws Exception {
  901. LogicalPlan lp;
  902. String query = "a = load 'myfile' as (name:Chararray, age:Int, gpa:Float);" +
  903. "b = group a by (name, age);" +
  904. "store b into 'output';";
  905. lp = buildPlan( query );
  906. Operator store = lp.getSinks().get(0);
  907. LOCogroup cogroup = (LOCogroup) lp.getPredecessors(store).get(0);
  908. Schema.FieldSchema nameFs = new Schema.FieldSchema("name", DataType.CHARARRAY);
  909. Schema.FieldSchema ageFs = new Schema.FieldSchema("age", DataType.INTEGER);
  910. Schema.FieldSchema gpaFs = new Schema.FieldSchema("gpa", DataType.FLOAT);
  911. Schema groupSchema = new Schema(nameFs);
  912. groupSchema.add(ageFs);
  913. Schema.FieldSchema groupFs = new Schema.FieldSchema("group", groupSchema, DataType.TUPLE);
  914. Schema loadSchema = new Schema(nameFs);
  915. loadSchema.add(ageFs);
  916. loadSchema.add(gpaFs);
  917. Schema.FieldSchema bagFs = new Schema.FieldSchema("a", loadSchema, DataType.BAG);
  918. Schema cogroupExpectedSchema = new Schema(groupFs);
  919. cogroupExpectedSchema.add(bagFs);
  920. Assert.assertTrue(cogroup.getSchema().toString(false).equals("group:tuple(name:chararray,age:int),a:bag{:tuple(name:chararray,age:int,gpa:float)}"));
  921. }
  922. @Test
  923. public void testQuery87() throws Exception {
  924. String query = "a = load 'myfile';" +
  925. "b = group a by $0;" +
  926. "c = foreach b {c1 = order $1 by $1; generate flatten(c1); };" +
  927. "store c into 'output';";
  928. LogicalPlan lp = buildPlan( query );
  929. Operator store = lp.getSinks().get(0);
  930. LOForEach foreach = (LOForEach) lp.getPredecessors(store).get(0);
  931. LogicalPlan nestedPlan = foreach.getInnerPlan();
  932. LOGenerate gen = (LOGenerate) nestedPlan.getSinks().get(0);
  933. LOSort nestedSort = (LOSort)nestedPlan.getPredecessors(gen).get(0);
  934. LogicalExpressionPlan sortPlan = nestedSort.getSortColPlans().get(0);
  935. Assert.assertTrue(sortPlan.getSinks().size() == 1);
  936. }
  937. @Test
  938. public void testQuery88() throws Exception {
  939. String query = "a = load 'myfile';" +
  940. "b = group a by $0;" +
  941. "c = order b by $1 ;" +
  942. "store c into 'output';";
  943. LogicalPlan lp = buildPlan( query );
  944. Operator store = lp.getSinks().get(0);
  945. LOSort sort = (LOSort) lp.getPredecessors(store).get(0);
  946. // LOProject project1 = (LOProject) sort.getSortColPlans().get(0).getSinks().get(0) ;
  947. // LOCogroup cogroup = (LOCogroup) lp.getPredecessors(sort).get(0) ;
  948. // assertEquals(project1.getExpression(), cogroup) ;
  949. }
  950. @Test
  951. public void testQuery89() throws Exception {
  952. String query = "a = load 'myfile';" +
  953. "b = foreach a generate $0, $100;" +
  954. "c = load 'myfile' as (i: int);" +
  955. "d = foreach c generate $0 as zero, i;";
  956. buildPlan( query );
  957. }
  958. @Test
  959. public void testQueryFail89() throws Exception {
  960. String q = "c = load 'myfile' as (i: int);";
  961. try {
  962. buildPlan(q + "d = foreach c generate $0, $5;");
  963. } catch (AssertionFailedError e) {
  964. Assert.assertTrue(e.getMessage().contains("Out of bound access"));
  965. }
  966. }
  967. @Test
  968. public void testQuery90() throws Exception {
  969. LogicalPlan lp;
  970. LOForEach foreach;
  971. String query = "a = load 'myfile' as (name:Chararray, age:Int, gpa:Float);" +
  972. "b = group a by (name, age);";
  973. //the first and second elements in group, i.e., name and age are renamed as myname and myage
  974. lp = buildPlan(query +
  975. "c = foreach b generate flatten(group) as (myname, myage), COUNT(a) as mycount;" +
  976. "store c into 'output';");
  977. Operator store = lp.getSinks().get(0);
  978. foreach = (LOForEach)lp.getPredecessors(store).get(0);
  979. Assert.assertTrue(foreach.getSchema().isEqual(Utils.parseSchema("myname: chararray, age: int, mycount: long")));
  980. //the schema of group is unchanged
  981. lp = buildPlan( query +
  982. "c = foreach b generate flatten(group), COUNT(a) as mycount;" +
  983. "store c into 'output';" );
  984. store = lp.getSinks().get(0);
  985. foreach = (LOForEach)lp.getPredecessors(store).get(0);
  986. Assert.assertTrue(foreach.getSchema().toString( false ).equals("group::name:chararray,group::age:int,mycount:long"));
  987. //group is renamed as mygroup
  988. lp = buildPlan(query +
  989. "c = foreach b generate group as mygroup, COUNT(a) as mycount;" +
  990. "store c into 'output';");
  991. store = lp.getSinks().get(0);
  992. foreach = (LOForEach)lp.getPredecessors(store).get(0);
  993. Assert.assertTrue(foreach.getSchema().toString( false ).equals("mygroup:tuple(name:chararray,age:int),mycount:long"));
  994. //group is renamed as mygroup and the elements are renamed as myname and myage
  995. lp = buildPlan(query +
  996. "c = foreach b generate group as mygroup:(myname, myage), COUNT(a) as mycount;" +
  997. "store c into 'output';");
  998. store = lp.getSinks().get(0);
  999. foreach = (LOForEach)lp.getPredecessors(store).get(0);
  1000. Assert.assertTrue(foreach.getSchema().toString( false ).equals("mygroup:tuple(myname:chararray,myage:int),mycount:long"));
  1001. /*
  1002. //setting the schema of flattened bag that has no schema with the user defined schema
  1003. String q = "a = load 'myfile' as (name:Chararray, age:Int, gpa:Float);" +
  1004. "c = load 'another_file';" +
  1005. "d = cogroup a by $0, c by $0;";
  1006. lp = buildPlan( q + "e = foreach d generate flatten(DIFF(a, c)) as (x, y, z), COUNT(a) as mycount;" + "store e into 'output';" );
  1007. store = lp.getSinks().get(0);
  1008. foreach = (LOForEach)lp.getPredecessors(store).get(0);
  1009. Assert.assertTrue(foreach.getSchema().equals(Util.getSchemaFromString("x: bytearray, y: bytearray, z: bytearray, mycount: long")));
  1010. //setting the schema of flattened bag that has no schema with the user defined schema
  1011. q = query +
  1012. "c = load 'another_file';" +
  1013. "d = cogroup a by $0, c by $0;" +
  1014. "e = foreach d generate flatten(DIFF(a, c)) as (x: int, y: float, z), COUNT(a) as mycount;";
  1015. lp = buildPlan(q);
  1016. store = lp.getSinks().get(0);
  1017. foreach = (LOForEach)lp.getPredecessors(store).get(0);
  1018. Assert.assertTrue(foreach.getSchema().equals(Util.getSchemaFromString("x: int, y: float, z: bytearray, mycount: long")));
  1019. //setting the schema of flattened bag that has no schema with the user defined schema
  1020. q = query +
  1021. "c = load 'another_file';" +
  1022. "d = cogroup a by $0, c by $0;" +
  1023. "e = foreach d generate flatten(DIFF(a, c)) as x, COUNT(a) as mycount;";
  1024. lp = buildPlan(q);
  1025. store = lp.getSinks().get(0);
  1026. foreach = (LOForEach)lp.getPredecessors(store).get(0);
  1027. Assert.assertTrue(foreach.getSchema().equals(Util.getSchemaFromString("x: bytearray, mycount: long")));
  1028. //setting the schema of flattened bag that has no schema with the user defined schema
  1029. q = query +
  1030. "c = load 'another_file';" +
  1031. "d = cogroup a by $0, c by $0;" +
  1032. "e = foreach d generate flatten(DIFF(a, c)) as x: int, COUNT(a) as mycount;";
  1033. lp = buildPlan(q);
  1034. store = lp.getSinks().get(0);
  1035. foreach = (LOForEach)lp.getPredecessors(store).get(0);
  1036. Assert.assertTrue(foreach.getSchema().equals(Util.getSchemaFromString("x: int, mycount: long")));
  1037. */
  1038. }
  1039. @Test
  1040. public void testQueryFail90() throws Exception {
  1041. String query = "a = load 'myfile' as (name:Chararray, age:Int, gpa:Float);" +
  1042. "b = group a by (name, age);";
  1043. try {
  1044. buildPlan( query + "c = foreach b generate group as mygroup:(myname, myage), COUNT(a) as mycount;");
  1045. } catch (AssertionFailedError e) {
  1046. Assert.assertTrue(e.getMessage().contains("Schema size mismatch"));
  1047. }
  1048. try {
  1049. buildPlan( query + "c = foreach b generate group as mygroup:(myname: int, myage), COUNT(a) as mycount;");
  1050. } catch (AssertionFailedError e) {
  1051. Assert.assertTrue(e.getMessage().contains("Type mismatch"));
  1052. }
  1053. try {
  1054. buildPlan( query + "c = foreach b generate group as mygroup:(myname, myage: chararray), COUNT(a) as mycount;");
  1055. } catch (AssertionFailedError e) {
  1056. Assert.assertTrue(e.getMessage().contains("Type mismatch"));
  1057. }
  1058. try {
  1059. buildPlan( query + "c = foreach b generate group as mygroup:{t: (myname, myage)}, COUNT(a) as mycount;");
  1060. } catch (AssertionFailedError e) {
  1061. Assert.assertTrue(e.getMessage().contains("Incompatable field schema"));
  1062. }
  1063. try {
  1064. buildPlan( query + "c = foreach b generate flatten(group) as (myname, myage, mygpa), COUNT(a) as mycount;");
  1065. } catch (AssertionFailedError e) {
  1066. Assert.assertTrue(e.getMessage().contains("Incompatable schema"));
  1067. }
  1068. }
  1069. @Test
  1070. public void testQuery91() throws Exception {
  1071. String query = "a = load 'myfile' as (name:Chararray, age:Int, gpa:Float);" +
  1072. "b = group a by name;";
  1073. buildPlan(query + "c = foreach b generate SUM(a.age) + SUM(a.gpa);");
  1074. }
  1075. @Test
  1076. public void testQuery92() throws Exception {
  1077. String query = "a = load 'myfile' as (name, age, gpa);" +
  1078. "b = group a by name;" +
  1079. "c = foreach b { "
  1080. + " alias = name#'alias'; "
  1081. + " af = alias#'first'; "
  1082. + " al = alias#'last'; "
  1083. + " generate SUM(a.age) + SUM(a.gpa); "
  1084. + "};";
  1085. buildPlan( query );
  1086. }
  1087. @Test
  1088. public void testQuery93() throws Exception {
  1089. String query = "a = load 'one' as (name, age, gpa);" +
  1090. "b = group a by name;" +
  1091. "c = foreach b generate flatten(a);" +
  1092. "d = foreach c generate name;" +
  1093. // test that we can refer to "name" field and not a::name
  1094. "e = foreach d generate name;";
  1095. buildPlan( query );
  1096. }
  1097. @Test
  1098. public void testQueryFail93() throws Exception {
  1099. String query = "a = load 'one' as (name, age, gpa);" +
  1100. "b = group a by name;"+
  1101. "c = foreach b generate flatten(a);"+
  1102. "d = foreach c generate name;"+
  1103. // test that we can refer to "name" field and a::name
  1104. "e = foreach d generate a::name;";
  1105. buildPlan( query );
  1106. }
  1107. @Test
  1108. public void testQuery94() throws Exception {
  1109. String query = "a = load 'one' as (name, age, gpa);" +
  1110. "b = load 'two' as (name, age, somethingelse);"+
  1111. "c = cogroup a by name, b by name;"+
  1112. "d = foreach c generate flatten(a), flatten(b);"+
  1113. // test that we can refer to "a::name" field and not name
  1114. // test that we can refer to "b::name" field and not name
  1115. "e = foreach d generate a::name, b::name;"+
  1116. // test that we can refer to gpa and somethingelse
  1117. "f = foreach d generate gpa, somethingelse;";
  1118. buildPlan( query );
  1119. }
  1120. @Test
  1121. public void testQueryFail94() throws Exception {
  1122. String query = "a = load 'one' as (name, age, gpa);" +
  1123. "b = load 'two' as (name, age, somethingelse);"+
  1124. "c = cogroup a by name, b by name;"+
  1125. "d = foreach c generate flatten(a), flatten(b);"+
  1126. "e = foreach d generate name;";
  1127. // test that we can refer to "a::name" field and not name
  1128. try {
  1129. buildPlan(query);
  1130. } catch (AssertionFailedError e) {
  1131. Assert.assertTrue(e.getMessage().contains("Invalid field projection. Projected field [name] does not exist"));
  1132. }
  1133. }
  1134. @Test
  1135. public void testQuery95() throws Exception {
  1136. String query = "a = load 'myfile' as (name, age, gpa);" +
  1137. "b = group a by name;" +
  1138. "c = foreach b {d = order a by $1; generate flatten(d), MAX(a.age) as max_age;};" +
  1139. "store c into 'output';";
  1140. LogicalPlan lp = buildPlan(query);
  1141. Operator store = lp.getSinks().get(0);
  1142. LOForEach foreach = (LOForEach)lp.getPredecessors(store).get(0);
  1143. LOCogroup cogroup = (LOCogroup) lp.getPredecessors(foreach).get(0);
  1144. String s = cogroup.getSchema().toString(false);
  1145. Assert.assertTrue( s.equals("group:bytearray,a:bag{:tuple(name:bytearray,age:bytearray,gpa:bytearray)}"));
  1146. s = foreach.getSchema().toString(false);
  1147. Assert.assertTrue( s.equals("d::name:bytearray,d::age:bytearray,d::gpa:bytearray,max_age:double"));
  1148. }
  1149. @Test
  1150. public void testQuery96() throws Exception {
  1151. String query = "a = load 'input' as (name, age, gpa);" +
  1152. "b = filter a by age < 20;" +
  1153. "c = group b by age;" +
  1154. "d = foreach c {"
  1155. + "cf = filter b by gpa < 3.0;"
  1156. + "cd = distinct cf.gpa;"
  1157. + "co = order cd by $0;"
  1158. + "generate group, flatten(co);"
  1159. + "};" +
  1160. "store d into 'output';";
  1161. LogicalPlan lp = buildPlan(query);
  1162. Operator store = lp.getSinks().get(0);
  1163. LOForEach foreach = (LOForEach)lp.getPredecessors(store).get(0);
  1164. LogicalPlan foreachPlans = foreach.getInnerPlan();
  1165. // LogicalPlan flattenPlan = foreachPlans.get(1);
  1166. // LogicalOperator project = flattenPlan.getLeaves().get(0);
  1167. // Assert.assertTrue(project instanceof LOProject);
  1168. // LogicalOperator sort = flattenPlan.getPredecessors(project).get(0);
  1169. // Assert.assertTrue(sort instanceof LOSort);
  1170. // LogicalOperator distinct = flattenPlan.getPredecessors(sort).get(0);
  1171. // Assert.assertTrue(distinct instanceof LODistinct);
  1172. //
  1173. // //testing the presence of the nested foreach
  1174. // LogicalOperator nestedForeach = flattenPlan.getPredecessors(distinct).get(0);
  1175. // Assert.assertTrue(nestedForeach instanceof LOForEach);
  1176. // LogicalPlan nestedForeachPlan = ((LOForEach)nestedForeach).getForEachPlans().get(0);
  1177. // LogicalOperator nestedProject = nestedForeachPlan.getRoots().get(0);
  1178. // Assert.assertTrue(nestedProject instanceof LOProject);
  1179. // Assert.assertTrue(((LOProject)nestedProject).getCol() == 2);
  1180. //
  1181. // //testing the filter inner plan for the absence of the project connected to project
  1182. // LogicalOperator filter = flattenPlan.getPredecessors(nestedForeach).get(0);
  1183. // Assert.assertTrue(filter instanceof LOFilter);
  1184. // LogicalPlan comparisonPlan = ((LOFilter)filter).getComparisonPlan();
  1185. // LOLesserThan lessThan = (LOLesserThan)comparisonPlan.getLeaves().get(0);
  1186. // LOProject filterProject = (LOProject)lessThan.getLhsOperand();
  1187. // Assert.assertTrue(null == comparisonPlan.getPredecessors(filterProject));
  1188. }
  1189. /*
  1190. @Test
  1191. public void testQuery97() throws FrontendException, ParseException {
  1192. LogicalPlan lp;
  1193. LOForEach foreach;
  1194. String query = "a = load 'one' as (name, age, gpa);";
  1195. String store = "store b into 'output';";
  1196. lp = buildPlan(query + "b = foreach a generate 1;" + store);
  1197. foreach = (LOForEach)lp.getPredecessors(op);
  1198. // Assert.assertTrue(Schema.equals(foreach.getSchema(), Util.getSchemaFromSt…

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