PageRenderTime 70ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/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
  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.getSchemaFromString("x: int"), false, true));
  1199. lp = buildPlan(query + "b = foreach a generate 1L;" + store);
  1200. op = lp.getSinks().get(0);
  1201. Operator op = lp.getSinks().get(0);
  1202. foreach = (LOForEach)lp.getPredecessors(op);
  1203. // Assert.assertTrue(Schema.equals(foreach.getSchema(), Util.getSchemaFromString("x: long"), false, true));
  1204. lp = buildPlan(query + "b = foreach a generate 1.0;" + store);
  1205. op = lp.getSinks().get(0);
  1206. foreach = (LOForEach)lp.getPredecessors(op);
  1207. // Assert.assertTrue(Schema.equals(foreach.getSchema(), Util.getSchemaFromString("x: double"), false, true));
  1208. lp = buildPlan(query + "b = foreach a generate 1.0f;" + store);
  1209. op = lp.getSinks().get(0);
  1210. foreach = (LOForEach)lp.getPredecessors(op);
  1211. // Assert.assertTrue(Schema.equals(foreach.getSchema(), Util.getSchemaFromString("x: float"), false, true));
  1212. lp = buildPlan(query + "b = foreach a generate 'hello';" + store);
  1213. op = lp.getSinks().get(0);
  1214. foreach = (LOForEach)lp.getPredecessors(op);
  1215. // Assert.assertTrue(Schema.equals(foreach.getSchema(), Util.getSchemaFromString("x: chararray"), false, true));
  1216. }
  1217. @Test
  1218. public void testQuery98() throws FrontendException, ParseException {
  1219. LogicalPlan lp;
  1220. LOForEach foreach;
  1221. buildPlan("a = load 'one' as (name, age, gpa);");
  1222. lp = buildPlan("b = foreach a generate (1);");
  1223. foreach = (LOForEach) lp.getLeaves().get(0);
  1224. Assert.assertTrue(Schema.equals(foreach.getSchema(), Util.getSchemaFromString("t:(x: int)"), false, true));
  1225. lp = buildPlan("b = foreach a generate (1L);");
  1226. foreach = (LOForEach) lp.getLeaves().get(0);
  1227. Assert.assertTrue(Schema.equals(foreach.getSchema(), Util.getSchemaFromString("t:(x: long)"), false, true));
  1228. lp = buildPlan("b = foreach a generate (1.0);");
  1229. foreach = (LOForEach) lp.getLeaves().get(0);
  1230. Assert.assertTrue(Schema.equals(foreach.getSchema(), Util.getSchemaFromString("t:(x: double)"), false, true));
  1231. lp = buildPlan("b = foreach a generate (1.0f);");
  1232. foreach = (LOForEach) lp.getLeaves().get(0);
  1233. Assert.assertTrue(Schema.equals(foreach.getSchema(), Util.getSchemaFromString("t:(x: float)"), false, true));
  1234. lp = buildPlan("b = foreach a generate ('hello');");
  1235. foreach = (LOForEach) lp.getLeaves().get(0);
  1236. Assert.assertTrue(Schema.equals(foreach.getSchema(), Util.getSchemaFromString("t:(x: chararray)"), false, true));
  1237. lp = buildPlan("b = foreach a generate ('hello', 1, 1L, 1.0f, 1.0);");
  1238. foreach = (LOForEach) lp.getLeaves().get(0);
  1239. Assert.assertTrue(Schema.equals(foreach.getSchema(), Util.getSchemaFromString("t:(x: chararray, y: int, z: long, a: float, b: double)"), false, true));
  1240. lp = buildPlan("b = foreach a generate ('hello', {(1), (1.0)});");
  1241. foreach = (LOForEach) lp.getLeaves().get(0);
  1242. Assert.assertTrue(Schema.equals(foreach.getSchema(), Util.getSchemaFromString("t:(x: chararray, ib:{it:(d: double)})"), false, true));
  1243. }
  1244. @Test
  1245. public void testQuery99() throws FrontendException, ParseException {
  1246. LogicalPlan lp;
  1247. LOForEach foreach;
  1248. buildPlan("a = load 'one' as (name, age, gpa);");
  1249. lp = buildPlan("b = foreach a generate {(1, 'hello'), (2, 'world')};");
  1250. foreach = (LOForEach) lp.getLeaves().get(0);
  1251. Assert.assertTrue(Schema.equals(foreach.getSchema(), Util.getSchemaFromString("b:{t:(x: int, y: chararray)}"), false, true));
  1252. lp = buildPlan("b = foreach a generate {(1, 'hello'), (1L, 'world')};");
  1253. foreach = (LOForEach) lp.getLeaves().get(0);
  1254. Assert.assertTrue(Schema.equals(foreach.getSchema(), Util.getSchemaFromString("b:{t:(x: long, y: chararray)}"), false, true));
  1255. lp = buildPlan("b = foreach a generate {(1, 'hello'), (1.0f, 'world')};");
  1256. foreach = (LOForEach) lp.getLeaves().get(0);
  1257. Assert.assertTrue(Schema.equals(foreach.getSchema(), Util.getSchemaFromString("b:{t:(x: float, y: chararray)}"), false, true));
  1258. lp = buildPlan("b = foreach a generate {(1, 'hello'), (1.0, 'world')};");
  1259. foreach = (LOForEach) lp.getLeaves().get(0);
  1260. Assert.assertTrue(Schema.equals(foreach.getSchema(), Util.getSchemaFromString("b:{t:(x: double, y: chararray)}"), false, true));
  1261. lp = buildPlan("b = foreach a generate {(1L, 'hello'), (1.0f, 'world')};");
  1262. foreach = (LOForEach) lp.getLeaves().get(0);
  1263. Assert.assertTrue(Schema.equals(foreach.getSchema(), Util.getSchemaFromString("b:{t:(x: float, y: chararray)}"), false, true));
  1264. lp = buildPlan("b = foreach a generate {(1L, 'hello'), (1.0, 'world')};");
  1265. foreach = (LOForEach) lp.getLeaves().get(0);
  1266. Assert.assertTrue(Schema.equals(foreach.getSchema(), Util.getSchemaFromString("b:{t:(x: double, y: chararray)}"), false, true));
  1267. lp = buildPlan("b = foreach a generate {(1.0f, 'hello'), (1.0, 'world')};");
  1268. foreach = (LOForEach) lp.getLeaves().get(0);
  1269. Assert.assertTrue(Schema.equals(foreach.getSchema(), Util.getSchemaFromString("b:{t:(x: double, y: chararray)}"), false, true));
  1270. lp = buildPlan("b = foreach a generate {(1.0, 'hello'), (1.0f, 'world')};");
  1271. foreach = (LOForEach) lp.getLeaves().get(0);
  1272. Assert.assertTrue(Schema.equals(foreach.getSchema(), Util.getSchemaFromString("b:{t:(x: double, y: chararray)}"), false, true));
  1273. lp = buildPlan("b = foreach a generate {(1.0, 'hello', 3.14), (1.0f, 'world')};");
  1274. foreach = (LOForEach) lp.getLeaves().get(0);
  1275. Assert.assertTrue(Schema.equals(foreach.getSchema(), Util.getSchemaFromString("b:{t:()}"), false, true));
  1276. }
  1277. @Test
  1278. public void testQuery101() throws Exception {
  1279. // test usage of an alias from define
  1280. String query = "define FUNC ARITY();";
  1281. buildPlan(query);
  1282. query = "foreach (load 'data') generate FUNC($0);";
  1283. buildPlan(query);
  1284. }
  1285. */
  1286. @Test
  1287. public void testQuery102() throws Exception {
  1288. // test basic store
  1289. buildPlan( "a = load 'a';" + "store a into 'out';" );
  1290. }
  1291. @Test
  1292. public void testQuery103() throws Exception {
  1293. // test store with store function
  1294. buildPlan("a = load 'a';" + "store a into 'out' using PigStorage();");
  1295. }
  1296. // @Test // Commented out due to PIG-2037
  1297. // public void testQuery104() throws Exception {
  1298. // // check that a field alias can be referenced
  1299. // // by unambiguous free form alias, fully qualified alias
  1300. // // and partially qualified unambiguous alias
  1301. // String query = "a = load 'st10k' as (name, age, gpa);\n" +
  1302. // "b = group a by name;\n" +
  1303. // "c = foreach b generate flatten(a);\n" +
  1304. // "d = filter c by name != 'fred';\n" +
  1305. // "e = group d by name;\n" +
  1306. // "f = foreach e generate flatten(d);\n" +
  1307. // "g = foreach f generate name, d::a::name, a::name;\n" +
  1308. // "store g into 'output';";
  1309. // buildPlan( query );
  1310. // }
  1311. @Test
  1312. public void testQuery105() throws Exception {
  1313. // test that the alias "group" can be used
  1314. // after a flatten(group)
  1315. String query = "a = load 'st10k' as (name, age, gpa);" +
  1316. "b = group a by name;" +
  1317. "c = foreach b generate flatten(group), COUNT(a) as cnt;" +
  1318. "d = foreach c generate group;" +
  1319. "store d into 'output';";
  1320. buildPlan( query );
  1321. }
  1322. @Test
  1323. public void testQuery106() throws Exception {
  1324. String query = "a = load 'one' as (name, age, gpa);" +
  1325. "b = foreach a generate *;" + "store b into 'output';";
  1326. LogicalPlan lp = buildPlan(query);
  1327. Operator store = lp.getSinks().get(0);
  1328. LOForEach foreach = (LOForEach)lp.getPredecessors(store).get(0);
  1329. String s = foreach.getSchema().toString(false);
  1330. Assert.assertTrue( s.equals("name:bytearray,age:bytearray,gpa:bytearray"));
  1331. }
  1332. @Test
  1333. public void testQuery107() throws Exception {
  1334. String query = "a = load 'one';" + "b = foreach a generate *;" + "store b into 'output';";
  1335. LogicalPlan lp = buildPlan( query );
  1336. Operator store = lp.getSinks().get(0);
  1337. LOForEach foreach = (LOForEach)lp.getPredecessors(store).get(0);
  1338. LOGenerate gen = (LOGenerate) foreach.getInnerPlan().getSinks().get(0);
  1339. LogicalExpressionPlan foreachPlan = gen.getOutputPlans().get(0);
  1340. Assert.assertTrue(checkPlanForProjectStar(foreachPlan));
  1341. }
  1342. @Test
  1343. public void testQuery108() throws Exception {
  1344. String query = "a = load 'one' as (name, age, gpa);" +
  1345. "b = group a by *;" +
  1346. "store b into 'output';";
  1347. LogicalPlan lp = buildPlan(query);
  1348. Operator store = lp.getSinks().get(0);
  1349. LOCogroup cogroup = (LOCogroup)lp.getPredecessors(store).get(0);
  1350. String s = cogroup.getSchema().toString(false);
  1351. Assert.assertTrue(s.equals("group:tuple(name:bytearray,age:bytearray,gpa:bytearray),a:bag{:tuple(name:bytearray,age:bytearray,gpa:bytearray)}"));
  1352. }
  1353. @Test
  1354. public void testQuery109() throws Exception {
  1355. String query = "a = load 'one' as (name, age, gpa);" +
  1356. "b = load 'two' as (first_name, enrol_age, high_school_gpa);" +
  1357. "c = group a by *, b by *;" +
  1358. "store c into 'output';";
  1359. LogicalPlan lp = buildPlan(query);
  1360. Operator store = lp.getSinks().get(0);
  1361. LOCogroup cogroup = (LOCogroup)lp.getPredecessors(store).get(0);
  1362. String s = cogroup.getSchema().toString(false);
  1363. Assert.assertTrue(s.equals("group:tuple(name:bytearray,age:bytearray,gpa:bytearray),a:bag{:tuple(name:bytearray,age:bytearray,gpa:bytearray)},b:bag{:tuple(first_name:bytearray,enrol_age:bytearray,high_school_gpa:bytearray)}"));
  1364. }
  1365. @Test
  1366. public void testQuery110Fail() throws Exception {
  1367. String query = "a = load 'one' as (name, age, gpa);" +
  1368. "b = load 'two';" + "c = cogroup a by $0, b by *;";
  1369. try {
  1370. buildPlan( query );
  1371. } catch(AssertionFailedError e) {
  1372. Assert.assertTrue(e.getMessage().contains("Cogroup/Group by '*' or 'x..' (range of columns to the end) is only allowed " +
  1373. "if the input has a schema" ) );
  1374. return;
  1375. }
  1376. Assert.fail( "Test case should fail." );
  1377. }
  1378. @Test
  1379. public void testQuery111() throws Exception {
  1380. String query = "a = load 'one' as (name, age, gpa);" +
  1381. "b = order a by *;" + "store b into 'y';";
  1382. LogicalPlan lp = buildPlan(query);
  1383. Operator store = lp.getSinks().get(0);
  1384. LOSort sort = (LOSort)lp.getPredecessors(store).get(0);
  1385. for(LogicalExpressionPlan sortPlan: sort.getSortColPlans() ) {
  1386. Assert.assertTrue(checkPlanForProjectStar(sortPlan) == false);
  1387. }
  1388. }
  1389. @Test
  1390. public void testQuery112() throws Exception {
  1391. String query = "a = load 'one' as (name, age, gpa);" +
  1392. "b = group a by *;" +
  1393. "c = foreach b {a1 = order a by *; generate a1;};" +
  1394. "store c into 'y';";
  1395. LogicalPlan lp = buildPlan(query);
  1396. Operator store = lp.getSinks().get(0);
  1397. LOForEach foreach = (LOForEach)lp.getPredecessors(store).get(0);
  1398. LOGenerate gen = (LOGenerate) foreach.getInnerPlan().getSinks().get(0);
  1399. for(LogicalExpressionPlan foreachPlan: gen.getOutputPlans()) {
  1400. Assert.assertTrue(checkPlanForProjectStar(foreachPlan) == true);
  1401. }
  1402. LogicalPlan foreachPlan = foreach.getInnerPlan();
  1403. LOSort sort = (LOSort)foreachPlan.getPredecessors(gen).get(0);
  1404. // project (*) operator here is translated to a list of projection
  1405. // operators
  1406. for(LogicalExpressionPlan sortPlan: sort.getSortColPlans()) {
  1407. Assert.assertTrue(checkPlanForProjectStar(sortPlan) == false);
  1408. }
  1409. }
  1410. @Test
  1411. public void testQuery114() throws Exception {
  1412. String query = "a = load 'one' as (name, age, gpa);" +
  1413. "b = foreach a generate " + Identity.class.getName() + "(name, age);" +
  1414. "store b into 'y';";
  1415. LogicalPlan lp = buildPlan(query);
  1416. Operator store = lp.getSinks().get(0);
  1417. LOForEach foreach = (LOForEach)lp.getPredecessors(store).get(0);
  1418. String s = foreach.getSchema().toString(false);
  1419. Assert.assertTrue(s.equals(":tuple(name:bytearray,age:bytearray)"));
  1420. }
  1421. @Test
  1422. public void testQuery115() throws Exception {
  1423. String query = "a = load 'one' as (name, age, gpa);" +
  1424. "b = foreach a generate " + Identity.class.getName() + "(*);" +
  1425. "store b into 'y';";
  1426. LogicalPlan lp = buildPlan(query);
  1427. Operator store = lp.getSinks().get(0);
  1428. LOForEach foreach = (LOForEach)lp.getPredecessors(store).get(0);
  1429. String s = foreach.getSchema().toString(false);
  1430. Assert.assertTrue(s.equals(":tuple(name:bytearray,age:bytearray,gpa:bytearray)"));
  1431. }
  1432. @Test
  1433. public void testQuery116() throws Exception {
  1434. String query = "a = load 'one';" +
  1435. "b = foreach a generate " + Identity.class.getName() + "($0, $1);" +
  1436. "store b into 'y';";
  1437. LogicalPlan lp = buildPlan(query);
  1438. Operator store = lp.getSinks().get(0);
  1439. LOForEach foreach = (LOForEach)lp.getPredecessors(store).get(0);
  1440. String s = foreach.getSchema().toString(false);
  1441. Assert.assertTrue(s.equals(":tuple(:bytearray,:bytearray)"));
  1442. }
  1443. @Test
  1444. public void testQuery117() throws Exception {
  1445. String query = "a = load 'one';" +
  1446. "b = foreach a generate " + Identity.class.getName() + "(*);" +
  1447. "store b into 'y';";
  1448. LogicalPlan lp = buildPlan(query);
  1449. Operator store = lp.getSinks().get(0);
  1450. LOForEach foreach = (LOForEach)lp.getPredecessors(store).get(0);
  1451. String s = foreach.getSchema().toString(false);
  1452. Assert.assertTrue(s.equals(":tuple()"));
  1453. }
  1454. @Test
  1455. public void testNullConsArithExprs() throws Exception {
  1456. String query = "a = load 'a' as (x:int, y:double);" +
  1457. "b = foreach a generate x + null, x * null, x / null, x - null, null % x, " +
  1458. "y + null, y * null, y / null, y - null;" +
  1459. "store b into 'output';";
  1460. buildPlan( query );
  1461. }
  1462. @Test
  1463. public void testNullConsBincond1() throws Exception {
  1464. String query = "a = load 'a' as (x:int, y:double);" +
  1465. "b = foreach a generate (2 > 1? null : 1), ( 2 < 1 ? null : 1), " +
  1466. "(2 > 1 ? 1 : null), ( 2 < 1 ? 1 : null);"
  1467. + "store b into 'output';";
  1468. buildPlan( query );
  1469. }
  1470. @Test
  1471. public void testNullConsBincond2() throws Exception {
  1472. String query = "a = load 'a' as (x:int, y:double);" +
  1473. "b = foreach a generate (null is null ? 1 : 2), ( null is not null ? 2 : 1);" +
  1474. "store b into 'output';";
  1475. buildPlan( query );
  1476. }
  1477. @Test
  1478. public void testNullConsForEachGenerate() throws Exception {
  1479. String query = "a = load 'a' as (x:int, y:double);" +
  1480. "b = foreach a generate x, null, y, null;" +
  1481. "store b into 'output';";
  1482. buildPlan( query );
  1483. }
  1484. @Test
  1485. public void testNullConsOuterJoin() throws Exception {
  1486. String query = "a = load 'a' as (x:int, y:chararray);" +
  1487. "b = load 'b' as (u:int, v:chararray);" +
  1488. "c = cogroup a by x, b by u;" +
  1489. "d = foreach c generate flatten((SIZE(a) == 0 ? null : a)), " +
  1490. "flatten((SIZE(b) == 0 ? null : b));" +
  1491. "store d into 'output';";
  1492. buildPlan(query);
  1493. }
  1494. @Test
  1495. public void testNullConsConcatSize() throws Exception {
  1496. String query = "a = load 'a' as (x:int, y:double, str:chararray);" +
  1497. "b = foreach a generate SIZE(null), CONCAT(str, null), " +
  1498. "CONCAT(null, str);" +
  1499. "store b into 'output';";
  1500. buildPlan(query);
  1501. }
  1502. @Test
  1503. public void testFilterUdfDefine() throws Exception {
  1504. String query = "define isempty IsEmpty();" +
  1505. "a = load 'a' as (x:int, y:double, str:chararray);" +
  1506. "b = filter a by isempty(*);" + "store b into 'output';";
  1507. buildPlan(query);
  1508. }
  1509. @Test
  1510. public void testLoadUdfDefine() throws Exception {
  1511. String query = "define PS PigStorage();" +
  1512. "a = load 'a' using PS as (x:int, y:double, str:chararray);" +
  1513. "b = filter a by IsEmpty(*);" +
  1514. " store b into 'x' using PS;";
  1515. buildPlan(query);
  1516. }
  1517. @Test
  1518. public void testLoadUdfConstructorArgDefine() throws Exception {
  1519. String query = "define PS PigStorage(':');" +
  1520. "a = load 'a' using PS as (x:int, y:double, str:chararray);" +
  1521. "b = filter a by IsEmpty(*);" +
  1522. " store b into 'x' using PS;";
  1523. buildPlan(query);
  1524. }
  1525. @Test
  1526. public void testStoreUdfDefine() throws Exception {
  1527. String query = "define PS PigStorage();" +
  1528. "a = load 'a' using PS as (x:int, y:double, str:chararray);" +
  1529. "b = filter a by IsEmpty(*);" +
  1530. "store b into 'x' using PS;";
  1531. buildPlan(query);
  1532. }
  1533. @Test
  1534. public void testStoreUdfConstructorArgDefine() throws Exception {
  1535. String query = "define PS PigStorage(':');" +
  1536. " a = load 'a' using PS as (x:int, y:double, str:chararray);" +
  1537. " b = filter a by IsEmpty(*);" +
  1538. " store b into 'x' using PS;";
  1539. buildPlan(query);
  1540. }
  1541. @Test
  1542. public void testCastAlias() throws Exception {
  1543. String query = "a = load 'one.txt' as (x,y); " +
  1544. "b = foreach a generate (int)x, (double)y;" +
  1545. "c = group b by x;" +
  1546. "store c into 'output';";
  1547. buildPlan(query);
  1548. }
  1549. @Test
  1550. public void testCast() throws Exception {
  1551. String query = "a = load 'one.txt' as (x,y); " +
  1552. "b = foreach a generate (int)$0, (double)$1;" +
  1553. "c = group b by $0;"+
  1554. "store c into 'output';";
  1555. buildPlan(query);
  1556. }
  1557. @Test
  1558. public void testReservedWordsInFunctionNames() throws Exception {
  1559. // test that define can contain reserved words are later parts of
  1560. // fully qualified function name
  1561. String[] keywords = {
  1562. "define",
  1563. "load",
  1564. "filter",
  1565. "foreach",
  1566. "matches",
  1567. "order",
  1568. "arrange",
  1569. "distinct",
  1570. "cogroup",
  1571. "join",
  1572. "cross",
  1573. "union",
  1574. "split",
  1575. "into",
  1576. "if",
  1577. "all",
  1578. "any",
  1579. "as",
  1580. "by",
  1581. "using",
  1582. "inner",
  1583. "outer",
  1584. "parallel",
  1585. "partition",
  1586. "group",
  1587. "and",
  1588. "or",
  1589. "not",
  1590. "generate",
  1591. "flatten",
  1592. "eval",
  1593. "asc",
  1594. "desc",
  1595. "int",
  1596. "long",
  1597. "float",
  1598. "double",
  1599. "chararray",
  1600. "bytearray",
  1601. "bag",
  1602. "tuple",
  1603. "map",
  1604. "is",
  1605. "null",
  1606. "stream",
  1607. "through",
  1608. "store",
  1609. "ship",
  1610. "cache",
  1611. "input",
  1612. "output",
  1613. "stderr",
  1614. "stdin",
  1615. "stdout",
  1616. "limit",
  1617. "sample",
  1618. "left",
  1619. "right",
  1620. "full",
  1621. "eq",
  1622. "gt",
  1623. "lt",
  1624. "gte",
  1625. "lte",
  1626. "neq"
  1627. };
  1628. for(String keyword: keywords) {
  1629. String query = "define FUNC org.apache."+keyword+"();";
  1630. LogicalPlan lp = buildPlan( query );
  1631. }
  1632. }
  1633. @Test
  1634. public void testTokenizeSchema() throws Exception {
  1635. String query = "a = load 'one' as (f1: chararray);" +
  1636. "b = foreach a generate TOKENIZE(f1);" +
  1637. "store b into 'output';";
  1638. LogicalPlan lp = buildPlan(query);
  1639. Operator store = lp.getSinks().get(0);
  1640. LOForEach foreach = (LOForEach) lp.getPredecessors(store).get(0);
  1641. String s = foreach.getSchema().toString(false);
  1642. Assert.assertTrue( s.equals("bag_of_tokenTuples_from_f1:bag{tuple_of_tokens:tuple(token:chararray)}"));
  1643. }
  1644. @Test
  1645. public void testTokenizeSchema2() throws Exception {
  1646. String query = "a = load 'one' as (f1: chararray, f2: chararray);" +
  1647. "b = foreach a generate TOKENIZE(f1), TOKENIZE(f2);" +
  1648. "store b into 'output';";
  1649. LogicalPlan lp = buildPlan(query);
  1650. Operator store = lp.getSinks().get(0);
  1651. LOForEach foreach = (LOForEach) lp.getPredecessors(store).get(0);
  1652. String s = foreach.getSchema().toString(false);
  1653. assertEquals(s, "bag_of_tokenTuples_from_f1:bag{tuple_of_tokens:tuple(token:chararray)}"
  1654. +",bag_of_tokenTuples_from_f2:bag{tuple_of_tokens:tuple(token:chararray)}");
  1655. }
  1656. @Test
  1657. public void testEmptyTupleConst() throws Exception{
  1658. String query = "a = foreach (load 'b') generate ();" + "store a into 'output';";
  1659. LogicalPlan lp = buildPlan( query );
  1660. Operator store = lp.getSinks().get(0);
  1661. LOForEach foreach = (LOForEach) lp.getPredecessors(store).get(0);
  1662. LOGenerate gen = (LOGenerate)foreach.getInnerPlan().getSinks().get(0);
  1663. LogicalExpressionPlan exprPlan = gen.getOutputPlans().get(0);
  1664. Operator logOp = exprPlan.getSources().get(0);
  1665. Assert.assertTrue( logOp instanceof ConstantExpression);
  1666. ConstantExpression loConst = (ConstantExpression)logOp;
  1667. Assert.assertTrue(loConst.getType() == DataType.TUPLE);
  1668. Assert.assertTrue(loConst.getValue() instanceof Tuple);
  1669. Assert.assertTrue(loConst.getValue().equals(TupleFactory.getInstance().newTuple()));
  1670. String s = foreach.getSchema().toString(false);
  1671. Assert.assertTrue( s.equals(":tuple()"));
  1672. }
  1673. @Test
  1674. public void testEmptyMapConst() throws Exception{
  1675. String query = "a = foreach (load 'b') generate [];" + "store a into 'output';";
  1676. LogicalPlan lp = buildPlan(query);
  1677. Operator store = lp.getSinks().get(0);
  1678. LOForEach foreach = (LOForEach) lp.getPredecessors(store).get(0);
  1679. LOGenerate gen = (LOGenerate)foreach.getInnerPlan().getSinks().get(0);
  1680. LogicalExpressionPlan exprPlan = gen.getOutputPlans().get(0);
  1681. Operator logOp = exprPlan.getSources().get(0);
  1682. Assert.assertTrue( logOp instanceof ConstantExpression);
  1683. ConstantExpression loConst = (ConstantExpression)logOp;
  1684. Assert.assertTrue(loConst.getType() == DataType.MAP);
  1685. Assert.assertTrue(loConst.getValue() instanceof Map);
  1686. Assert.assertTrue(loConst.getValue().equals(new HashMap<String,Object>()));
  1687. String s = foreach.getSchema().toString(false);
  1688. Assert.assertTrue( s.equals(":map"));
  1689. }
  1690. @Test
  1691. public void testEmptyBagConst() throws Exception{
  1692. String query = "a = foreach (load 'b') generate {};" +
  1693. "store a into 'output';";
  1694. LogicalPlan lp = buildPlan(query);
  1695. Operator store = lp.getSinks().get(0);
  1696. LOForEach foreach = (LOForEach) lp.getPredecessors(store).get(0);
  1697. LOGenerate gen = (LOGenerate)foreach.getInnerPlan().getSinks().get(0);
  1698. LogicalExpressionPlan exprPlan = gen.getOutputPlans().get(0);
  1699. Operator logOp = exprPlan.getSources().get(0);
  1700. Assert.assertTrue( logOp instanceof ConstantExpression);
  1701. ConstantExpression loConst = (ConstantExpression)logOp;
  1702. Assert.assertTrue(loConst.getType() == DataType.BAG);
  1703. Assert.assertTrue(loConst.getValue() instanceof DataBag);
  1704. Assert.assertTrue(loConst.getValue().equals(BagFactory.getInstance().newDefaultBag()));
  1705. String s = foreach.getSchema().toString(false);
  1706. Assert.assertTrue( s.equals(":bag{}") );
  1707. }
  1708. @Test
  1709. public void testEmptyTupConstRecursive1() throws Exception{
  1710. String query = "a = foreach (load 'b') generate (());" +
  1711. "store a into 'output';";
  1712. LogicalPlan lp = buildPlan(query);
  1713. Operator store = lp.getSinks().get(0);
  1714. LOForEach foreach = (LOForEach) lp.getPredecessors(store).get(0);
  1715. String s = foreach.getSchema().toString(false);
  1716. Assert.assertTrue( s.equals(":tuple(:tuple())") );
  1717. }
  1718. @Test
  1719. public void testEmptyTupConstRecursive2() throws Exception{
  1720. String query = "a = foreach (load 'b') generate ([]);" +
  1721. "store a into 'output';";
  1722. LogicalPlan lp = buildPlan( query );
  1723. Operator store = lp.getSinks().get(0);
  1724. LOForEach foreach = (LOForEach) lp.getPredecessors(store).get(0);
  1725. String s = foreach.getSchema().toString(false);
  1726. Assert.assertTrue( s.equals(":tuple(:map)") );
  1727. }
  1728. @Test
  1729. public void testEmptyTupConstRecursive3() throws Exception{
  1730. String query = "a = foreach (load 'b') generate ({});" +
  1731. "store a into 'output';";
  1732. LogicalPlan lp = buildPlan(query);
  1733. Operator op = lp.getSinks().get(0);
  1734. LOForEach foreach = (LOForEach)lp.getPredecessors(op).get(0);
  1735. String s = foreach.getSchema().toString(false);
  1736. Assert.assertTrue( s.equals(":tuple(:bag{})") );
  1737. }
  1738. @Test
  1739. public void testEmptyBagConstRecursive() throws Exception{
  1740. String query = "a = foreach (load 'b') generate {()};" +
  1741. "store a into 'output';";
  1742. LogicalPlan lp = buildPlan(query);
  1743. Operator op = lp.getSinks().get(0);
  1744. LOForEach foreach = (LOForEach)lp.getPredecessors(op).get(0);
  1745. String s = foreach.getSchema().toString(false);
  1746. Assert.assertTrue( s.equals(":bag{:tuple()}") );
  1747. }
  1748. @Test
  1749. public void testRandomEmptyConst() throws Exception{
  1750. // Various random scripts to test recursive nature of parser with empty constants.
  1751. buildPlan("a = foreach (load 'b') generate {({})}; store a into 'output';");
  1752. buildPlan("a = foreach (load 'b') generate ({()}); store a into 'output';");
  1753. buildPlan("a = foreach (load 'b') generate {(),()}; store a into 'output';");
  1754. buildPlan("a = foreach (load 'b') generate ({},{}); store a into 'output';");
  1755. buildPlan("a = foreach (load 'b') generate ((),()); store a into 'output';");
  1756. buildPlan("a = foreach (load 'b') generate ([],[]); store a into 'output';");
  1757. buildPlan("a = foreach (load 'b') generate {({},{})}; store a into 'output';");
  1758. buildPlan("a = foreach (load 'b') generate {([],[])}; store a into 'output';");
  1759. buildPlan("a = foreach (load 'b') generate (({},{})); store a into 'output';");
  1760. buildPlan("a = foreach (load 'b') generate (([],[])); store a into 'output';");
  1761. }
  1762. @Test
  1763. // See PIG-1024, shall not throw exception
  1764. public void testLimitMultipleOutput() throws Exception {
  1765. String query = " a = load '1.txt' as (a0:int, a1:int, a2:int);" +
  1766. " b = group a by a0;" +
  1767. " c = foreach b { c1 = limit a 10; c2 = distinct c1.a1; c3 = distinct c1.a2; generate c2, c3;};" +
  1768. " store c into 'output';";
  1769. buildPlan( query );
  1770. }
  1771. @Test
  1772. public void testCogroupByStarFailure1() throws Exception {
  1773. boolean exceptionThrown = false;
  1774. try {
  1775. String query = " a = load '1.txt' as (a0:int, a1:int);" +
  1776. " b = load '2.txt'; " +
  1777. "c = cogroup a by *, b by *;" +
  1778. "store c into 'output';";
  1779. buildPlan(query);
  1780. } catch (AssertionFailedError e) {
  1781. Assert.assertTrue(e.getMessage().contains("Cogroup/Group by '*' or 'x..' (range of columns to the end) is only" +
  1782. " allowed if the input has a schema"));
  1783. exceptionThrown = true;
  1784. }
  1785. Assert.assertEquals("An exception was expected but did " +
  1786. "not occur", true, exceptionThrown);
  1787. }
  1788. @Test
  1789. public void testCogroupByStarFailure2() throws Exception {
  1790. boolean exceptionThrown = false;
  1791. try {
  1792. String query = " a = load '1.txt' ;" +
  1793. " b = load '2.txt' as (b0:int, b1:int); " +
  1794. "c = cogroup a by *, b by *;" +
  1795. "store c into 'output';";
  1796. buildPlan( query );
  1797. } catch (AssertionFailedError e) {
  1798. Assert.assertTrue(e.getMessage().contains("Cogroup/Group by '*' or 'x..' (range of columns to the end) is only allowed if the input has a schema"));
  1799. exceptionThrown = true;
  1800. }
  1801. Assert.assertEquals("An exception was expected but did " +
  1802. "not occur", true, exceptionThrown);
  1803. }
  1804. @Test
  1805. public void testMissingSemicolon() throws Exception {
  1806. try {
  1807. String query = "A = load '1.txt' \n" +
  1808. "B = load '2.txt' as (b0:int, b1:int);\n" +
  1809. "C = union A, B;\n" +
  1810. "store C into 'output';";
  1811. buildPlan( query );
  1812. } catch (AssertionFailedError e) {
  1813. Assert.assertTrue(e.getMessage().contains("mismatched input 'B' expecting SEMI_COLON"));
  1814. return;
  1815. }
  1816. Assert.fail("An exception was expected but did not occur");
  1817. }
  1818. @Test
  1819. public void testCogroupByIncompatibleSchemaFailure() throws Exception {
  1820. boolean exceptionThrown = false;
  1821. try {
  1822. String query = " a = load '1.txt' as (a0:int, a1:int);" +
  1823. " b = load '2.txt' as (a0:int, a1:chararray); " +
  1824. "c = cogroup a by (a0,a1), b by (a0,a1);" +
  1825. "store c into 'output';";
  1826. buildPlan( query );
  1827. } catch (AssertionFailedError e) {
  1828. String msg =
  1829. "group column no. 2 in relation no. 2 of group statement" +
  1830. " has datatype chararray which is incompatible with type of" +
  1831. " corresponding column in earlier relation(s) in the statement";
  1832. Assert.assertTrue(e.getMessage().contains(msg));
  1833. exceptionThrown = true;
  1834. }
  1835. Assert.assertEquals("An exception was expected but did " +
  1836. "not occur", true, exceptionThrown);
  1837. }
  1838. @Test
  1839. public void testLoaderSignature() throws Exception {
  1840. String query = "a = load '1.txt' using org.apache.pig.test.PigStorageWithSchema() as (a0:int, a1:int);" +
  1841. "store a into 'output';";
  1842. LogicalPlan plan = buildPlan( query );
  1843. Operator op = plan.getSinks().get(0);
  1844. LOLoad load = (LOLoad)plan.getPredecessors(op).get(0);
  1845. // the signature is now a unique string of the format "{alias}_{scope id}-{id}" example: "a_12-0"
  1846. String udfContextSignature = ((PigStorageWithSchema)(load).getLoadFunc()).getUDFContextSignature();
  1847. Assert.assertTrue(udfContextSignature, udfContextSignature.matches("a_[0-9]*-[0-9]*"));
  1848. query = " b = load '1.txt' using org.apache.pig.test.PigStorageWithSchema();" +
  1849. "store b into 'output';";
  1850. plan = buildPlan(query);
  1851. op = plan.getSinks().get(0);
  1852. load = (LOLoad)plan.getPredecessors(op).get(0);
  1853. udfContextSignature = ((PigStorageWithSchema)(load).getLoadFunc()).getUDFContextSignature();
  1854. Assert.assertTrue(udfContextSignature, udfContextSignature.matches("b_[0-9]*-[0-9]*"));
  1855. }
  1856. @Test
  1857. public void testLastAlias() throws Exception {
  1858. try {
  1859. String query = "B = load '2.txt' as (b0:int, b1:int);\n" +
  1860. "C = ORDER B by b0;" ;
  1861. buildPlan( query );
  1862. } catch (AssertionFailedError e) {
  1863. // Ignore the exception
  1864. }
  1865. Assert.assertEquals("C", pigServer.getPigContext().getLastAlias());
  1866. }
  1867. private void printPlan(LogicalExpressionPlan lp) {
  1868. System.err.println( lp.toString() );
  1869. }
  1870. private boolean checkPlanForProjectStar(LogicalExpressionPlan lp) {
  1871. List<Operator> leaves = lp.getSinks();
  1872. for(Operator op: leaves) {
  1873. if(op instanceof ProjectExpression) {
  1874. if(((ProjectExpression) op).isProjectStar()) {
  1875. return true;
  1876. }
  1877. }
  1878. }
  1879. return false;
  1880. }
  1881. // Helper Functions
  1882. public LogicalPlan buildPlan(String query) throws Exception {
  1883. try {
  1884. return Util.buildLp(pigServer, query);
  1885. } catch(Throwable t) {
  1886. PigException pigEx = LogUtils.getPigException(t);
  1887. Throwable cause = null;
  1888. if(pigEx != null){
  1889. cause = pigEx;
  1890. }else{
  1891. cause = t.getCause();
  1892. }
  1893. String msg = cause != null ? cause.toString() : t.toString();
  1894. throw new AssertionFailedError( msg );
  1895. }
  1896. }
  1897. }