How to use check_result method in hypothesis

Best Python code snippet using hypothesis

query_tests.py

Source:query_tests.py Github

copy

Full Screen

...29 query = """30 emp = SCAN(%s);31 STORE(emp, OUTPUT);32 """ % self.emp_key33 self.check_result(query, self.emp_table)34 def test_scan_dept(self):35 query = """36 dept = SCAN(%s);37 STORE(dept, OUTPUT);38 """ % self.dept_key39 self.check_result(query, self.dept_table)40 def test_bag_comp_emit_star(self):41 query = """42 emp = SCAN(%s);43 bc = [FROM emp EMIT *];44 STORE(bc, OUTPUT);45 """ % self.emp_key46 self.check_result(query, self.emp_table)47 def test_bag_comp_emit_table_wildcard(self):48 query = """49 emp = SCAN(%s);50 bc = [FROM emp EMIT emp.*];51 STORE(bc, OUTPUT);52 """ % self.emp_key53 self.check_result(query, self.emp_table)54 def test_hybrid_emit_clause(self):55 query = """56 emp = SCAN(%s);57 dept = SCAN(%s);58 x = [FROM dept, emp as X EMIT 5, X.salary * 2 AS k, X.*, *];59 STORE(x, OUTPUT);60 """ % (self.emp_key, self.dept_key)61 expected = [(5, e[3] * 2) + e + d + e for e in self.emp_table62 for d in self.dept_table]63 self.check_result(query, collections.Counter(expected))64 salary_filter_query = """65 emp = SCAN(%s);66 rich = [FROM emp WHERE %s > 25 * 10 * 10 * (5 + 5) EMIT *];67 STORE(rich, OUTPUT);68 """69 salary_expected_result = collections.Counter(70 [x for x in FakeData.emp_table.elements() if x[3] > 25000])71 def test_bag_comp_filter_large_salary_by_name(self):72 query = TestQueryFunctions.salary_filter_query % (self.emp_key,73 'salary')74 self.check_result(query, TestQueryFunctions.salary_expected_result)75 def test_bag_comp_filter_large_salary_by_position(self):76 query = TestQueryFunctions.salary_filter_query % (self.emp_key, '$3')77 self.check_result(query, TestQueryFunctions.salary_expected_result)78 def test_bag_comp_filter_empty_result(self):79 query = """80 emp = SCAN(%s);81 poor = [FROM emp WHERE $3 < (5 * 2) EMIT *];82 STORE(poor, OUTPUT);83 """ % self.emp_key84 expected = collections.Counter()85 self.check_result(query, expected)86 def test_bag_comp_filter_column_compare_ge(self):87 query = """88 emp = SCAN(%s);89 out = [FROM emp WHERE 2 * $1 >= $0 EMIT *];90 STORE(out, OUTPUT);91 """ % self.emp_key92 expected = collections.Counter(93 [x for x in self.emp_table.elements() if 2 * x[1] >= x[0]])94 self.check_result(query, expected)95 def test_bag_comp_filter_column_compare_ge2(self):96 query = u"""97 emp = SCAN(%s);98 out = [FROM emp WHERE 2 * $1 ≥ $0 EMIT *];99 STORE(out, OUTPUT);100 """ % self.emp_key101 expected = collections.Counter(102 [x for x in self.emp_table.elements() if 2 * x[1] >= x[0]])103 self.check_result(query, expected)104 def test_bag_comp_filter_column_compare_le(self):105 query = """106 emp = SCAN(%s);107 out = [FROM emp WHERE $1 <= 2 * $0 EMIT *];108 STORE(out, OUTPUT);109 """ % self.emp_key110 expected = collections.Counter(111 [x for x in self.emp_table.elements() if x[1] <= 2 * x[0]])112 self.check_result(query, expected)113 def test_bag_comp_filter_column_compare_le2(self):114 query = u"""115 emp = SCAN(%s);116 out = [FROM emp WHERE $1 ≤ 2 * $0 EMIT *];117 STORE(out, OUTPUT);118 """ % self.emp_key119 expected = collections.Counter(120 [x for x in self.emp_table.elements() if x[1] <= 2 * x[0]])121 self.check_result(query, expected)122 def test_bag_comp_filter_column_compare_gt(self):123 query = """124 emp = SCAN(%s);125 out = [FROM emp WHERE 2 * $1 > $0 EMIT *];126 STORE(out, OUTPUT);127 """ % self.emp_key128 expected = collections.Counter(129 [x for x in self.emp_table.elements() if 2 * x[1] > x[0]])130 self.check_result(query, expected)131 def test_bag_comp_filter_column_compare_lt(self):132 query = """133 emp = SCAN(%s);134 out = [FROM emp WHERE $1 < 2 * $0 EMIT *];135 STORE(out, OUTPUT);136 """ % self.emp_key137 expected = collections.Counter(138 [x for x in self.emp_table.elements() if x[1] < 2 * x[0]])139 self.check_result(query, expected)140 def test_bag_comp_filter_column_compare_eq(self):141 query = """142 emp = SCAN(%s);143 out = [FROM emp WHERE $0 * 2 == $1 EMIT *];144 STORE(out, OUTPUT);145 """ % self.emp_key146 expected = collections.Counter(147 [x for x in self.emp_table.elements() if x[0] * 2 == x[1]])148 self.check_result(query, expected)149 def test_bag_comp_filter_column_compare_ne(self):150 query = """151 emp = SCAN(%s);152 out = [FROM emp WHERE $0 // $1 != $1 EMIT *];153 STORE(out, OUTPUT);154 """ % self.emp_key155 expected = collections.Counter(156 [x for x in self.emp_table.elements() if x[0] / x[1] != x[1]])157 self.check_result(query, expected)158 def test_bag_comp_filter_column_compare_ne2(self):159 query = """160 emp = SCAN(%s);161 out = [FROM emp WHERE $0 // $1 <> $1 EMIT *];162 STORE(out, OUTPUT);163 """ % self.emp_key164 expected = collections.Counter(165 [x for x in self.emp_table.elements() if x[0] / x[1] != x[1]])166 self.check_result(query, expected)167 def test_bag_comp_filter_column_compare_ne3(self):168 query = u"""169 emp = SCAN(%s);170 out = [FROM emp WHERE $0 // $1 ≠ $1 EMIT *];171 STORE(out, OUTPUT);172 """ % self.emp_key173 expected = collections.Counter(174 [x for x in self.emp_table.elements() if x[0] / x[1] != x[1]])175 self.check_result(query, expected)176 def test_bag_comp_filter_minus(self):177 query = """178 emp = SCAN(%s);179 out = [FROM emp WHERE $0 + -$1 == $1 EMIT *];180 STORE(out, OUTPUT);181 """ % self.emp_key182 expected = collections.Counter(183 [x for x in self.emp_table.elements() if x[0] - x[1] == x[1]])184 self.check_result(query, expected)185 def test_bag_comp_filter_and(self):186 query = """187 emp = SCAN(%s);188 out = [FROM emp WHERE salary == 25000 AND id > dept_id EMIT *];189 STORE(out, OUTPUT);190 """ % self.emp_key191 expected = collections.Counter(192 [x for x in self.emp_table.elements() if x[3] == 25000 and193 x[0] > x[1]])194 self.check_result(query, expected)195 def test_bag_comp_filter_or(self):196 query = """197 emp = SCAN(%s);198 out = [FROM emp WHERE $3 > 25 * 1000 OR id > dept_id EMIT *];199 STORE(out, OUTPUT);200 """ % self.emp_key201 expected = collections.Counter(202 [x for x in self.emp_table.elements() if x[3] > 25000 or203 x[0] > x[1]])204 self.check_result(query, expected)205 def test_bag_comp_filter_not(self):206 query = """207 emp = SCAN(%s);208 out = [FROM emp WHERE not salary > 25000 EMIT *];209 STORE(out, OUTPUT);210 """ % self.emp_key211 expected = collections.Counter(212 [x for x in self.emp_table.elements() if not x[3] > 25000])213 self.check_result(query, expected)214 def test_bag_comp_filter_or_and(self):215 query = """216 emp = SCAN(%s);217 out = [FROM emp WHERE salary == 25000 OR salary == 5000 AND218 dept_id == 1 EMIT *];219 STORE(out, OUTPUT);220 """ % self.emp_key221 expected = collections.Counter(222 [x for x in self.emp_table.elements() if x[3] == 25000 or223 (x[3] == 5000 and x[1] == 1)])224 self.check_result(query, expected)225 def test_bag_comp_filter_or_and_not(self):226 query = """227 emp = SCAN(%s);228 out = [FROM emp WHERE salary == 25000 OR NOT salary == 5000 AND229 dept_id == 1 EMIT *];230 STORE(out, OUTPUT);231 """ % self.emp_key232 expected = collections.Counter(233 [x for x in self.emp_table.elements() if x[3] == 25000 or not234 x[3] == 5000 and x[1] == 1])235 self.check_result(query, expected)236 def test_bag_comp_emit_columns(self):237 query = """238 emp = SCAN(%s);239 out = [FROM emp WHERE dept_id == 1 EMIT $2, salary AS salary];240 STORE(out, OUTPUT);241 """ % self.emp_key242 expected = collections.Counter(243 [(x[2], x[3]) for x in self.emp_table.elements() if x[1] == 1])244 self.check_result(query, expected)245 def test_bag_comp_emit_literal(self):246 query = """247 emp = SCAN(%s);248 out = [FROM emp EMIT salary, "bugga bugga"];249 STORE(out, OUTPUT);250 """ % self.emp_key251 expected = collections.Counter(252 [(x[3], "bugga bugga") for x in self.emp_table.elements()])253 self.check_result(query, expected)254 def test_bag_comp_emit_with_math(self):255 query = """256 emp = SCAN(%s);257 out = [FROM emp EMIT salary + 5000, salary - 5000, salary // 5000,258 salary * 5000];259 STORE(out, OUTPUT);260 """ % self.emp_key261 expected = collections.Counter(262 [(x[3] + 5000, x[3] - 5000, x[3] / 5000, x[3] * 5000)263 for x in self.emp_table.elements()])264 self.check_result(query, expected)265 def test_bag_comp_rename(self):266 query = """267 emp = SCAN(%s);268 out = [FROM emp EMIT name, salary * 2 AS double_salary];269 out = [FROM out WHERE double_salary > 10000 EMIT *];270 STORE(out, OUTPUT);271 """ % self.emp_key272 expected = collections.Counter(273 [(x[2], x[3] * 2) for x in self.emp_table.elements() if274 x[3] * 2 > 10000])275 self.check_result(query, expected)276 join_expected = collections.Counter(277 [('Bill Howe', 'human resources'),278 ('Dan Halperin', 'accounting'),279 ('Andrew Whitaker', 'accounting'),280 ('Shumo Chu', 'human resources'),281 ('Victor Almeida', 'accounting'),282 ('Dan Suciu', 'engineering'),283 ('Magdalena Balazinska', 'accounting')])284 def test_explicit_join_unicode(self):285 query = u"""286 emp = SCAN(%s);287 dept = SCAN(%s);288 out = JOIN(emp, dept_id, dept, id);289 out2 = [FROM out EMIT $2 AS emp_name, $5 AS dept_name];290 STORE(out2, OUTPUT);291 """ % (self.emp_key, self.dept_key)292 self.check_result(query, self.join_expected)293 def test_explicit_join(self):294 query = """295 emp = SCAN(%s);296 dept = SCAN(%s);297 out = JOIN(emp, dept_id, dept, id);298 out2 = [FROM out EMIT $2 AS emp_name, $5 AS dept_name];299 STORE(out2, OUTPUT);300 """ % (self.emp_key, self.dept_key)301 self.check_result(query, self.join_expected)302 def test_explicit_join_twocols(self):303 query = """304 query = [1 as dept_id, 25000 as salary];305 emp = SCAN({emp});306 out = JOIN(query, (dept_id, salary), emp, (dept_id, salary));307 out2 = [FROM out EMIT name];308 STORE(out2, OUTPUT);309 """.format(emp=self.emp_key)310 expected = collections.Counter([('Victor Almeida',),311 ('Magdalena Balazinska',)])312 self.check_result(query, expected)313 def test_bagcomp_join_via_names(self):314 query = """315 out = [FROM SCAN(%s) E, SCAN(%s) AS D WHERE E.dept_id == D.id316 EMIT E.name AS emp_name, D.name AS dept_name];317 STORE(out, OUTPUT);318 """ % (self.emp_key, self.dept_key)319 self.check_result(query, self.join_expected)320 def test_bagcomp_join_via_pos(self):321 query = """322 E = SCAN(%s);323 D = SCAN(%s);324 out = [FROM E, D WHERE E.$1 == D.$0325 EMIT E.name AS emp_name, D.$1 AS dept_name];326 STORE(out, OUTPUT);327 """ % (self.emp_key, self.dept_key)328 self.check_result(query, self.join_expected)329 def test_two_column_join(self):330 query = """331 D = [1 as dept_id, 25000 as salary];332 out = [FROM D, SCAN({emp}) E333 WHERE E.dept_id == D.dept_id AND E.salary == D.salary334 EMIT E.name AS emp_name];335 STORE(out, OUTPUT);336 """.format(emp=self.emp_key)337 expected = collections.Counter([('Victor Almeida',),338 ('Magdalena Balazinska',)])339 self.check_result(query, expected)340 def test_join_with_select(self):341 query = """342 out = [FROM SCAN(%s) AS D, SCAN(%s) E343 WHERE E.dept_id == D.id AND E.salary < 6000344 EMIT E.name AS emp_name, D.name AS dept_name];345 STORE(out, OUTPUT);346 """ % (self.dept_key, self.emp_key)347 expected = collections.Counter([('Andrew Whitaker', 'accounting'),348 ('Shumo Chu', 'human resources')])349 self.check_result(query, expected)350 def test_join_with_reordering(self):351 # Try both FROM orders of the query and verify they both get the352 # correct answer.353 query = """354 out = [FROM SCAN({d}) AS D, SCAN({e}) E355 WHERE E.dept_id == D.id AND E.salary < 6000356 EMIT E.name, D.id];357 STORE(out, OUTPUT);358 """.format(d=self.dept_key, e=self.emp_key)359 expected = collections.Counter([('Andrew Whitaker', 1),360 ('Shumo Chu', 2)])361 self.check_result(query, expected)362 # Swap E and D363 query = """364 out = [FROM SCAN({e}) E, SCAN({d}) AS D365 WHERE E.dept_id == D.id AND E.salary < 6000366 EMIT E.name, D.id];367 STORE(out, OUTPUT);368 """.format(d=self.dept_key, e=self.emp_key)369 expected = collections.Counter([('Andrew Whitaker', 1),370 ('Shumo Chu', 2)])371 self.check_result(query, expected)372 def test_sql_join(self):373 """SQL-style select-from-where join"""374 query = """375 E = SCAN(%s);376 D = SCAN(%s);377 out = SELECT E.name, D.name FROM E, D WHERE E.dept_id = D.id;378 STORE(out, OUTPUT);379 """ % (self.emp_key, self.dept_key)380 self.check_result(query, self.join_expected)381 def test_bagcomp_nested_sql(self):382 """Test nesting SQL inside a bag comprehension"""383 query = """384 out = [FROM (SELECT name, salary385 FROM SCAN(%s) AS X386 WHERE salary > 5000) AS Y387 WHERE salary < 80000388 EMIT *];389 STORE(out, OUTPUT);390 """ % (self.emp_key,)391 tuples = [(e[2], e[3]) for e in self.emp_table.elements()392 if e[3] < 80000 and e[3] > 5000]393 expected = collections.Counter(tuples)394 self.check_result(query, expected)395 def test_sql_nested_sql(self):396 """Test nesting SQL inside SQL"""397 query = """398 out = SELECT Y.name, Y.salary399 FROM (SELECT name, salary400 FROM SCAN(%s) AS X401 WHERE salary > 5000) AS Y402 WHERE Y.salary < 80000;403 STORE(out, OUTPUT);404 """ % (self.emp_key,)405 tuples = [(e[2], e[3]) for e in self.emp_table.elements()406 if e[3] < 80000 and e[3] > 5000]407 expected = collections.Counter(tuples)408 self.check_result(query, expected)409 def test_sql_nested_bagcomp(self):410 """Test nesting a bag comprehension inside SQL"""411 query = """412 out = SELECT Y.name, Y.salary FROM413 [FROM SCAN(%s) AS X WHERE salary > 5000 EMIT X.*] AS Y414 WHERE Y.salary < 80000;415 STORE(out, OUTPUT);416 """ % (self.emp_key,)417 tuples = [(e[2], e[3]) for e in self.emp_table.elements()418 if e[3] < 80000 and e[3] > 5000]419 expected = collections.Counter(tuples)420 self.check_result(query, expected)421 def test_bagcomp_projection(self):422 """Test that column names are preserved across projection."""423 query = """424 E = SCAN(%s);425 F = [FROM E EMIT $2];426 out = [FROM F EMIT name];427 STORE(out, OUTPUT);428 """ % (self.emp_key,)429 tpls = [tuple([x[2]]) for x in self.emp_table]430 expected = collections.Counter(tpls)431 self.check_result(query, expected)432 def test_bagcomp_no_column_name(self):433 """Test that the system handles an omitted output column name."""434 query = """435 E = SCAN(%s);436 F = [FROM E EMIT salary*E.salary];437 out = [FROM F EMIT $0];438 STORE(out, OUTPUT);439 """ % (self.emp_key,)440 tpls = [tuple([x[3] * x[3]]) for x in self.emp_table]441 expected = collections.Counter(tpls)442 self.check_result(query, expected)443 def test_explicit_cross(self):444 query = """445 out = CROSS(SCAN(%s), SCAN(%s));446 STORE(out, OUTPUT);447 """ % (self.emp_key, self.dept_key)448 tuples = [e + d for e in self.emp_table.elements() for449 d in self.dept_table.elements()]450 expected = collections.Counter(tuples)451 self.check_result(query, expected)452 def test_bagcomp_cross(self):453 query = """454 out = [FROM SCAN(%s) E, SCAN(%s) AS D EMIT *];455 STORE(out, OUTPUT);456 """ % (self.emp_key, self.dept_key)457 tuples = [e + d for e in self.emp_table.elements() for458 d in self.dept_table.elements()]459 expected = collections.Counter(tuples)460 self.check_result(query, expected)461 def test_distinct(self):462 query = """463 out = DISTINCT([FROM SCAN(%s) AS X EMIT salary]);464 STORE(out, OUTPUT);465 """ % self.emp_key466 expected = collections.Counter([(25000,), (5000,), (90000,)])467 self.check_result(query, expected)468 def test_sql_distinct(self):469 query = """470 out = SELECT DISTINCT salary AS salary FROM SCAN(%s) AS X;471 STORE(out, OUTPUT);472 """ % self.emp_key473 expected = collections.Counter(set([(x[3],) for x in self.emp_table]))474 self.check_result(query, expected)475 def test_sql_repeated(self):476 query = """477 out = SELECT salary AS salary FROM SCAN(%s) AS X;478 STORE(out, OUTPUT);479 """ % self.emp_key480 expected = collections.Counter([(x[3],) for x in self.emp_table])481 self.check_result(query, expected)482 def test_limit(self):483 query = """484 out = LIMIT(SCAN(%s), 3);485 STORE(out, OUTPUT);486 """ % self.emp_key487 result = self.execute_query(query)488 self.assertEquals(len(result), 3)489 def test_sql_limit(self):490 query = """491 out = SELECT * FROM SCAN(%s) as X LIMIT 3;492 STORE(out, OUTPUT);493 """ % self.emp_key494 result = self.execute_query(query)495 self.assertEquals(len(result), 3)496 def test_table_literal_boolean(self):497 query = """498 X = [truE as MyTrue, FaLse as MyFalse];499 Y = [FROM scan(%s) as E, X where X.MyTrue emit *];500 STORE(Y, OUTPUT);501 """ % self.emp_key502 res = [x + (True, False) for x in self.emp_table]503 self.check_result(query, collections.Counter(res))504 def test_table_literal_scalar_expression(self):505 query = """506 X = [FROM ["Andrew", (50 * (500 + 500)) AS salary] Z EMIT salary];507 STORE(X, OUTPUT);508 """509 expected = collections.Counter([(50000,)])510 self.check_result(query, expected)511 def test_table_literal_unbox(self):512 query = """513 A = [1 AS one, 2 AS two, 3 AS three];514 B = [1 AS one, 2 AS two, 3 AS three];515 C = [*A.two * *B.three];516 STORE(C, OUTPUT);517 """518 expected = collections.Counter([(6,)])519 self.check_result(query, expected)520 def test_unbox_from_where_single(self):521 query = """522 TH = [25 * 1000];523 emp = SCAN(%s);524 out = [FROM emp WHERE $3 > *TH EMIT *];525 STORE(out, OUTPUT);526 """ % self.emp_key527 expected = collections.Counter(528 [x for x in self.emp_table.elements() if x[3] > 25000])529 self.check_result(query, expected)530 def test_unbox_from_where_multi(self):531 query = """532 TWO = [2];533 FOUR = [4];534 EIGHT = [8];535 emp = SCAN(%s);536 out = [FROM emp WHERE *EIGHT == *TWO**FOUR EMIT *];537 STORE(out, OUTPUT);538 """ % self.emp_key539 self.check_result(query, self.emp_table)540 def test_unbox_from_where_nary_name(self):541 query = """542 _CONST = [25 AS twenty_five, 1000 AS thousand];543 emp = SCAN(%s);544 out = [FROM emp WHERE salary == *_CONST.twenty_five *545 *_CONST.thousand EMIT *];546 STORE(out, OUTPUT);547 """ % self.emp_key548 expected = collections.Counter(549 [x for x in self.emp_table.elements() if x[3] == 25000])550 self.check_result(query, expected)551 def test_unbox_from_where_nary_pos(self):552 query = """553 _CONST = [25 AS twenty_five, 1000 AS thousand];554 emp = SCAN(%s);555 out = [FROM emp WHERE salary == *_CONST.$0 *556 *_CONST.$1 EMIT *];557 STORE(out, OUTPUT);558 """ % self.emp_key559 expected = collections.Counter(560 [x for x in self.emp_table.elements() if x[3] == 25000])561 self.check_result(query, expected)562 def test_unbox_from_emit_single(self):563 query = """564 THOUSAND = [1000];565 emp = SCAN(%s);566 out = [FROM emp EMIT salary * *THOUSAND AS salary];567 STORE(out, OUTPUT);568 """ % self.emp_key569 expected = collections.Counter(570 [(x[3] * 1000,) for x in self.emp_table.elements()])571 self.check_result(query, expected)572 def test_unbox_kitchen_sink(self):573 query = """574 C1 = [25 AS a, 100 AS b];575 C2 = [50 AS a, 1000 AS b];576 emp = SCAN(%s);577 out = [FROM emp WHERE salary==*C1.a * *C2.b OR $3==*C1.b * *C2578 EMIT dept_id * *C1.b // *C2.a];579 STORE(out, OUTPUT);580 """ % self.emp_key581 expected = collections.Counter(582 [(x[1] * 2,) for x in self.emp_table.elements() if583 x[3] == 5000 or x[3] == 25000])584 self.check_result(query, expected)585 def test_unbox_arbitrary_expression(self):586 query = """587 emp = SCAN(%s);588 dept = SCAN(%s);589 out = [FROM emp, COUNTALL(dept) as size WHERE id > *size EMIT emp.id];590 STORE(out, OUTPUT);591 """ % (self.emp_key, self.dept_key)592 expected = collections.Counter(593 [(x[0],) for x in self.emp_table.elements() if594 x[0] > len(self.dept_table)])595 self.check_result(query, expected)596 def test_inline_table_literal(self):597 query = """598 emp = SCAN(%s);599 dept = SCAN(%s);600 out = [FROM emp, [1,2,3] as tl WHERE id > tl.$2 EMIT emp.id];601 STORE(out, OUTPUT);602 """ % (self.emp_key, self.dept_key)603 expected = collections.Counter(604 [(x[0],) for x in self.emp_table.elements() if605 x[0] > 3])606 self.check_result(query, expected)607 def __aggregate_expected_result(self, apply_func, grouping_col=1,608 agg_col=3):609 result_dict = collections.defaultdict(list)610 for t in self.emp_table.elements():611 result_dict[t[grouping_col]].append(t[agg_col])612 tuples = [(key, apply_func(values)) for key, values in613 result_dict.iteritems()]614 return collections.Counter(tuples)615 def test_max(self):616 query = """617 out = [FROM SCAN(%s) AS X EMIT dept_id, MAX(salary)];618 STORE(out, OUTPUT);619 """ % self.emp_key620 self.check_result(query, self.__aggregate_expected_result(max))621 def test_min(self):622 query = """623 out = [FROM SCAN(%s) AS X EMIT dept_id, MIN(salary)];624 STORE(out, OUTPUT);625 """ % self.emp_key626 self.check_result(query, self.__aggregate_expected_result(min))627 def test_sum(self):628 query = """629 out = [FROM SCAN(%s) as X EMIT dept_id, SUM(salary)];630 STORE(out, OUTPUT);631 """ % self.emp_key632 self.check_result(query, self.__aggregate_expected_result(sum))633 def test_avg(self):634 query = """635 out = [FROM SCAN(%s) AS X EMIT dept_id, AVG(salary)];636 STORE(out, OUTPUT);637 """ % self.emp_key638 def avg(it):639 sum = 0640 cnt = 0641 for val in it:642 sum += val643 cnt += 1644 return sum / cnt645 self.check_result(query, self.__aggregate_expected_result(avg))646 self.check_result(query, self.__aggregate_expected_result(avg),647 test_logical=True)648 def test_stdev(self):649 query = """650 out = [FROM SCAN(%s) AS X EMIT STDEV(salary)];651 STORE(out, OUTPUT);652 """ % self.emp_key653 res = self.execute_query(query)654 tp = res.elements().next()655 self.assertAlmostEqual(tp[0], 34001.8006726)656 res = self.execute_query(query, test_logical=True)657 tp = res.elements().next()658 self.assertAlmostEqual(tp[0], 34001.8006726)659 def test_count(self):660 query = """661 out = [FROM SCAN(%s) AS X EMIT dept_id, COUNT(salary)];662 STORE(out, OUTPUT);663 """ % self.emp_key664 self.check_result(query, self.__aggregate_expected_result(len))665 self.check_result(query, self.__aggregate_expected_result(len),666 test_logical=True)667 def test_countall(self):668 query = """669 out = [FROM SCAN(%s) AS X EMIT dept_id, COUNTALL()];670 STORE(out, OUTPUT);671 """ % self.emp_key672 self.check_result(query, self.__aggregate_expected_result(len))673 self.check_result(query, self.__aggregate_expected_result(len),674 test_logical=True)675 def test_count_star(self):676 query = """677 out = [FROM SCAN(%s) AS X EMIT dept_id, COUNT(*)];678 STORE(out, OUTPUT);679 """ % self.emp_key680 self.check_result(query, self.__aggregate_expected_result(len))681 self.check_result(query, self.__aggregate_expected_result(len),682 test_logical=True)683 def test_count_star_sql(self):684 query = """685 out = SELECT dept_id, COUNT(*) FROM SCAN(%s) AS X;686 STORE(out, OUTPUT);687 """ % self.emp_key688 self.check_result(query, self.__aggregate_expected_result(len))689 self.check_result(query, self.__aggregate_expected_result(len),690 test_logical=True)691 def test_max_reversed(self):692 query = """693 out = [FROM SCAN(%s) AS X EMIT MAX(salary) AS max_salary, dept_id];694 STORE(out, OUTPUT);695 """ % self.emp_key696 ex = self.__aggregate_expected_result(max)697 ex = collections.Counter([(y, x) for (x, y) in ex])698 self.check_result(query, ex)699 self.check_result(query, ex, test_logical=True)700 def test_compound_aggregate(self):701 query = """702 out = [FROM SCAN(%s) AS X703 EMIT (2 * (MAX(salary) - MIN(salary))) AS range,704 dept_id AS did];705 out = [FROM out EMIT did AS dept_id, range AS rng];706 STORE(out, OUTPUT);707 """ % self.emp_key708 result_dict = collections.defaultdict(list)709 for t in self.emp_table.elements():710 result_dict[t[1]].append(t[3])711 tuples = [(key, 2 * (max(values) - min(values))) for key, values in712 result_dict.iteritems()]713 expected = collections.Counter(tuples)714 self.check_result(query, expected)715 self.check_result(query, expected, test_logical=True)716 def test_aggregate_with_unbox(self):717 query = """718 C = [1 AS one, 2 AS two];719 out = [FROM SCAN(%s) AS X720 EMIT MAX(*C.two * salary) - MIN( *C.$1 * salary) AS range,721 dept_id AS did];722 out = [FROM out EMIT did AS dept_id, range AS rng];723 STORE(out, OUTPUT);724 """ % self.emp_key725 result_dict = collections.defaultdict(list)726 for t in self.emp_table.elements():727 result_dict[t[1]].append(2 * t[3])728 tuples = [(key, (max(values) - min(values))) for key, values in729 result_dict.iteritems()]730 expected = collections.Counter(tuples)731 self.check_result(query, expected)732 self.check_result(query, expected, test_logical=True)733 def test_nary_groupby(self):734 query = """735 out = [FROM SCAN(%s) AS X EMIT dept_id, salary, COUNT(name)];736 STORE(out, OUTPUT);737 """ % self.emp_key738 result_dict = collections.defaultdict(list)739 for t in self.emp_table.elements():740 result_dict[(t[1], t[3])].append(t[2])741 tuples = [key + (len(values),)742 for key, values in result_dict.iteritems()]743 expected = collections.Counter(tuples)744 self.check_result(query, expected)745 def test_empty_groupby(self):746 query = """747 out = [FROM SCAN(%s) AS X EMIT MAX(salary), COUNT($0), MIN(dept_id*4)];748 STORE(out, OUTPUT);749 """ % self.emp_key750 expected = collections.Counter([(90000, len(self.emp_table), 4)])751 self.check_result(query, expected)752 def test_compound_groupby(self):753 query = """754 out = [FROM SCAN(%s) AS X EMIT id+dept_id, AVG(salary), COUNT(salary)];755 STORE(out, OUTPUT);756 """ % self.emp_key757 result_dict = collections.defaultdict(list)758 for t in self.emp_table.elements():759 result_dict[t[0] + t[1]].append(t[3])760 tuples1 = [(key, sum(values), len(values)) for key, values761 in result_dict.iteritems()]762 tuples2 = [(t[0], t[1] / t[2], t[2]) for t in tuples1]763 expected = collections.Counter(tuples2)764 self.check_result(query, expected)765 def test_impure_aggregate_colref(self):766 """Test of aggregate column that refers to a grouping column"""767 query = """768 out = [FROM SCAN(%s) AS X EMIT769 ( X.dept_id + (MAX(X.salary) - MIN(X.salary))) AS val,770 X.dept_id AS did];771 out = [FROM out EMIT did AS dept_id, val AS rng];772 STORE(out, OUTPUT);773 """ % self.emp_key774 result_dict = collections.defaultdict(list)775 for t in self.emp_table.elements():776 result_dict[t[1]].append(t[3])777 tuples = [(key, key + (max(values) - min(values))) for key, values in778 result_dict.iteritems()]779 expected = collections.Counter(tuples)780 self.check_result(query, expected)781 def test_impure_aggregate_unbox(self):782 """Test of an aggregate column that contains an unbox."""783 query = """784 TWO = [2];785 out = [FROM SCAN(%s) AS X786 EMIT (*TWO * (MAX(salary) - MIN(salary))) AS range,787 dept_id AS did];788 out = [FROM out EMIT did AS dept_id, range AS rng];789 STORE(out, OUTPUT);790 """ % self.emp_key791 result_dict = collections.defaultdict(list)792 for t in self.emp_table.elements():793 result_dict[t[1]].append(t[3])794 tuples = [(key, 2 * (max(values) - min(values))) for key, values in795 result_dict.iteritems()]796 expected = collections.Counter(tuples)797 self.check_result(query, expected)798 def test_aggregate_illegal_colref(self):799 query = """800 out = [FROM SCAN(%s) AS X EMIT801 X.dept_id + COUNT(X.salary) AS val];802 STORE(out, OUTPUT);803 """ % self.emp_key804 with self.assertRaises(raco.myrial.groupby.NonGroupedAccessException): # noqa805 self.check_result(query, None)806 def test_nested_aggregates_are_illegal(self):807 query = """808 out = [FROM SCAN(%s) AS X809 EMIT id+dept_id, MIN(53 + MAX(salary)) AS foo];810 STORE(out, OUTPUT);811 """ % self.emp_key812 with self.assertRaises(NestedAggregateException):813 self.check_result(query, collections.Counter())814 def test_standalone_countall(self):815 query = """816 out = COUNTALL(SCAN(%s));817 STORE(out, OUTPUT);818 """ % self.emp_key819 expected = collections.Counter([(len(self.emp_table),)])820 self.check_result(query, expected)821 def test_multiway_bagcomp_with_unbox(self):822 """Return all employees in accounting making less than 30000"""823 query = """824 Salary = [30000];825 Dept = ["accounting"];826 out = [FROM SCAN(%s) AS E, SCAN(%s) AS D827 WHERE E.dept_id == D.id AND D.name == *Dept828 AND E.salary < *Salary EMIT E.$2 AS name];829 STORE(out, OUTPUT);830 """ % (self.emp_key, self.dept_key)831 expected = collections.Counter([832 ("Andrew Whitaker",),833 ("Victor Almeida",),834 ("Magdalena Balazinska",)])835 self.check_result(query, expected)836 def test_duplicate_bagcomp_aliases_are_illegal(self):837 query = """838 X = SCAN(%s);839 out = [FROM X, X EMIT *];840 STORE(out, OUTPUT);841 """ % (self.emp_key,)842 with self.assertRaises(interpreter.DuplicateAliasException):843 self.check_result(query, collections.Counter())844 def test_bagcomp_column_index_out_of_bounds(self):845 query = """846 E = SCAN(%s);847 D = SCAN(%s);848 out = [FROM E, D WHERE E.$1 == D.$77849 EMIT E.name AS emp_name, D.$1 AS dept_name];850 STORE(out, OUTPUT);851 """ % (self.emp_key, self.dept_key)852 with self.assertRaises(ColumnIndexOutOfBounds):853 self.check_result(query, collections.Counter())854 def test_abs(self):855 query = """856 out = [FROM SCAN(%s) AS X EMIT id, ABS(val)];857 STORE(out, OUTPUT);858 """ % self.numbers_key859 expected = collections.Counter(860 [(a, abs(b)) for a, b in self.numbers_table.elements()])861 self.check_result(query, expected)862 def test_ceil(self):863 query = """864 out = [FROM SCAN(%s) AS X EMIT id, CEIL(val)];865 STORE(out, OUTPUT);866 """ % self.numbers_key867 expected = collections.Counter(868 [(a, math.ceil(b)) for a, b in self.numbers_table.elements()])869 self.check_result(query, expected)870 def test_cos(self):871 query = """872 out = [FROM SCAN(%s) AS X EMIT id, COS(val)];873 STORE(out, OUTPUT);874 """ % self.numbers_key875 expected = collections.Counter(876 [(a, math.cos(b)) for a, b in self.numbers_table.elements()])877 self.check_result(query, expected)878 def test_floor(self):879 query = """880 out = [FROM SCAN(%s) AS X EMIT id, FLOOR(val)];881 STORE(out, OUTPUT);882 """ % self.numbers_key883 expected = collections.Counter(884 [(a, math.floor(b)) for a, b in self.numbers_table.elements()])885 self.check_result(query, expected)886 def test_log(self):887 query = """888 out = [FROM SCAN(%s) AS X WHERE val > 0 EMIT id, LOG(val)];889 STORE(out, OUTPUT);890 """ % self.numbers_key891 expected = collections.Counter(892 [(a, math.log(b)) for a, b in self.numbers_table.elements()893 if b > 0])894 self.check_result(query, expected)895 def test_sin(self):896 query = """897 out = [FROM SCAN(%s) AS X EMIT id, SIN(val)];898 STORE(out, OUTPUT);899 """ % self.numbers_key900 expected = collections.Counter(901 [(a, math.sin(b)) for a, b in self.numbers_table.elements()])902 self.check_result(query, expected)903 def test_sqrt(self):904 query = """905 out = [FROM SCAN(%s) X WHERE val >= 0 EMIT id, SQRT(val)];906 STORE(out, OUTPUT);907 """ % self.numbers_key908 expected = collections.Counter(909 [(a, math.sqrt(b)) for a, b in self.numbers_table.elements()910 if b >= 0])911 self.check_result(query, expected)912 def test_tan(self):913 query = """914 out = [FROM SCAN(%s) AS X EMIT id, TAN(val)];915 STORE(out, OUTPUT);916 """ % self.numbers_key917 expected = collections.Counter(918 [(a, math.tan(b)) for a, b in self.numbers_table.elements()])919 self.check_result(query, expected)920 def test_md5(self):921 query = """922 out = [FROM SCAN(%s) AS X EMIT id, md5(name)];923 STORE(out, OUTPUT);924 """ % self.emp_key925 def md5_as_long(x):926 m = md5.new()927 m.update(x)928 return int(m.hexdigest(), 16) >> 64929 expected = collections.Counter(930 [(x[0], md5_as_long(x[2])) for x in self.emp_table.elements()])931 self.check_result(query, expected)932 def test_pow(self):933 query = """934 THREE = [3];935 out = [FROM SCAN(%s) X EMIT id, POW(X.val, *THREE)];936 STORE(out, OUTPUT);937 """ % self.numbers_key938 expected = collections.Counter(939 [(a, pow(b, 3)) for a, b in self.numbers_table.elements()])940 self.check_result(query, expected)941 def test_no_such_relation(self):942 query = """943 out = [FROM SCAN(foo:bar:baz) x EMIT id, TAN(val)];944 STORE(out, OUTPUT);945 """946 with self.assertRaises(NoSuchRelationException):947 self.check_result(query, collections.Counter())948 def test_bad_relation_name(self):949 query = """950 y = empty(a:int);951 z = [from s y -- bug: s does not exist952 emit y.a];953 store(z, debug);954 """955 with self.assertRaises(NoSuchRelationException):956 self.check_result(query, collections.Counter())957 def test_bad_alias(self):958 query = """959 y = empty(a:int);960 z = [from y s -- bug: extra s961 emit y.a];962 store(z, debug);963 """964 with self.assertRaises(NoSuchRelationException):965 self.check_result(query, collections.Counter())966 def test_bad_alias_wildcard(self):967 query = """968 y = empty(a:int);969 z = [from y s -- bug: errant s970 emit y.*];971 store(z, debug);972 """973 with self.assertRaises(NoSuchRelationException):974 self.check_result(query, collections.Counter())975 def test_scan_error(self):976 query = """977 out = [FROM SCAN(%s) AS X EMIT id, !!!FROG(val)];978 STORE(out, OUTPUT);979 """ % self.emp_key980 with self.assertRaises(MyrialCompileException):981 self.check_result(query, collections.Counter())982 def test_relation_scope_error(self):983 query = """984 out = [FROM EMPTY(x:INT) AS X EMIT z.*];985 STORE(out, OUTPUT);986 """987 with self.assertRaises(NoSuchRelationException):988 self.check_result(query, collections.Counter())989 def test_relation_scope_error2(self):990 query = """991 z = EMPTY(z:INT);992 out = [FROM EMPTY(x:INT) AS X EMIT z.*];993 STORE(out, OUTPUT);994 """995 with self.assertRaises(NoSuchRelationException):996 self.check_result(query, collections.Counter())997 def test_parse_error(self):998 query = """999 out = [FROM SCAN(%s) AS X EMIT $(val)];1000 STORE(out, OUTPUT);1001 """ % self.emp_key1002 with self.assertRaises(MyrialCompileException):1003 self.check_result(query, collections.Counter())1004 def test_no_such_udf(self):1005 query = """1006 out = [FROM SCAN(%s) AS X EMIT FooFunction(X.salary)];1007 STORE(out, OUTPUT);1008 """ % self.emp_key1009 with self.assertRaises(NoSuchFunctionException):1010 self.check_result(query, collections.Counter())1011 def test_reserved_udf(self):1012 query = """1013 DEF avg(x, y): (x + y) / 2;1014 out = [FROM SCAN(%s) AS X EMIT avg(X.salary)];1015 STORE(out, OUTPUT);1016 """ % self.emp_key1017 with self.assertRaises(ReservedTokenException):1018 self.check_result(query, collections.Counter())1019 def test_duplicate_udf(self):1020 query = """1021 DEF foo(x, y): x + y;1022 DEF bar(): 7;1023 DEF foo(x): -1 * x;1024 out = [FROM SCAN(%s) AS X EMIT foo(X.salary)];1025 STORE(out, OUTPUT);1026 """ % self.emp_key1027 with self.assertRaises(DuplicateFunctionDefinitionException):1028 self.check_result(query, collections.Counter())1029 def test_invalid_argument_udf(self):1030 query = """1031 DEF Foo(x, y): cos(x) * sin(y);1032 out = [FROM SCAN(%s) AS X EMIT Foo(X.salary)];1033 STORE(out, OUTPUT);1034 """ % self.emp_key1035 with self.assertRaises(InvalidArgumentList):1036 self.check_result(query, collections.Counter())1037 def test_undefined_variable_udf(self):1038 query = """1039 DEF Foo(x, y): cos(x) * sin(z);1040 out = [FROM SCAN(%s) AS X EMIT Foo(X.salary)];1041 STORE(out, OUTPUT);1042 """ % self.emp_key1043 with self.assertRaises(UndefinedVariableException):1044 self.check_result(query, collections.Counter())1045 def test_duplicate_variable_udf(self):1046 query = """1047 DEF Foo(x, x): cos(x) * sin(x);1048 out = [FROM SCAN(%s) AS X EMIT Foo(X.salary, X.dept_id)];1049 STORE(out, OUTPUT);1050 """ % self.emp_key1051 with self.assertRaises(DuplicateVariableException):1052 self.check_result(query, collections.Counter())1053 def test_nary_udf(self):1054 query = """1055 DEF Foo(a,b): [a + b, a - b];1056 out = [FROM SCAN(%s) AS X EMIT id, Foo(salary, dept_id) as [x, y]];1057 STORE(out, OUTPUT);1058 """ % self.emp_key1059 expected = collections.Counter([(t[0], t[1] + t[3], t[3] - t[1])1060 for t in self.emp_table])1061 self.check_result(query, expected)1062 def test_nary_udf_name_count(self):1063 query = """1064 DEF Foo(a,b): [a + b, a - b];1065 out = [FROM SCAN(%s) AS X EMIT id, Foo(salary, dept_id) as [x, y, z]];1066 STORE(out, OUTPUT);1067 """ % self.emp_key1068 with self.assertRaises(IllegalColumnNamesException):1069 self.check_result(query, None)1070 def test_nary_udf_illegal_nesting(self):1071 query = """1072 DEF Foo(x): [x + 3, x - 3];1073 DEF Bar(a,b): [Foo(x), Foo(b)];1074 out = [FROM SCAN(%s) AS X EMIT id, Bar(salary, dept_id) as [x, y]];1075 STORE(out, OUTPUT);1076 """ % self.emp_key1077 with self.assertRaises(NestedTupleExpressionException):1078 self.check_result(query, None)1079 def test_nary_udf_illegal_wildcard(self):1080 query = """1081 DEF Foo(x): [x + 3, *];1082 out = [FROM SCAN(%s) AS X EMIT id, Foo(salary, dept_id) as [x, y]];1083 STORE(out, OUTPUT);1084 """ % self.emp_key1085 with self.assertRaises(IllegalWildcardException):1086 self.check_result(query, None)1087 def test_triangle_udf(self):1088 query = """1089 DEF Triangle(a,b): (a*b)//2;1090 out = [FROM SCAN(%s) AS X EMIT id, Triangle(X.salary, dept_id) AS t];1091 STORE(out, OUTPUT);1092 """ % self.emp_key1093 expected = collections.Counter([(t[0], t[1] * t[3] / 2) for t in self.emp_table]) # noqa1094 self.check_result(query, expected)1095 def test_noop_udf(self):1096 expr = "30 + 15 // 7 + -45"1097 query = """1098 DEF Noop(): %s;1099 out = [Noop() AS t];1100 STORE(out, OUTPUT);1101 """ % expr1102 val = eval(expr)1103 expected = collections.Counter([(val,)])1104 self.check_result(query, expected)1105 def test_const(self):1106 expr = "30 + 15 // 7 + -45"1107 query = """1108 CONST myconstant: %s;1109 out = [myconstant AS t];1110 STORE(out, OUTPUT);1111 """ % expr1112 val = eval(expr)1113 expected = collections.Counter([(val,)])1114 self.check_result(query, expected)1115 def test_composition_udf(self):1116 query = """1117 DEF Add7(x): x + 7;1118 DEF Add6(x): x + 6;1119 out = [FROM SCAN(%s) AS X EMIT id, Add6(Add7(Add6(X.salary)))];1120 STORE(out, OUTPUT);1121 """ % self.emp_key1122 expected = collections.Counter([(t[0], t[3] + 19)1123 for t in self.emp_table])1124 self.check_result(query, expected)1125 def test_nested_udf(self):1126 query = """1127 DEF Add7(x): x + 7;1128 DEF Add10(x): Add7(x) + 3;1129 out = [FROM SCAN(%s) AS X EMIT id, Add10(X.salary)];1130 STORE(out, OUTPUT);1131 """ % self.emp_key1132 expected = collections.Counter([(t[0], t[3] + 10)1133 for t in self.emp_table])1134 self.check_result(query, expected)1135 def test_regression_150(self):1136 """Repeated invocation of a UDF."""1137 query = """1138 DEF transform(x): pow(10, x/pow(2,16)*3.5);1139 out = [FROM SCAN(%s) AS X EMIT id, transform(salary),1140 transform(dept_id)];1141 STORE(out, OUTPUT);1142 """ % self.emp_key1143 def tx(x):1144 return pow(10, float(x) / pow(2, 16) * 3.5)1145 expected = collections.Counter([(t[0], tx(t[3]), tx(t[1]))1146 for t in self.emp_table])1147 self.check_result(query, expected)1148 def test_safediv_2_function(self):1149 query = """1150 out = [FROM SCAN(%s) AS X EMIT SafeDiv(X.salary,X.dept_id-1)];1151 STORE(out, OUTPUT);1152 """ % self.emp_key1153 expected = collections.Counter(1154 [(t[3] / (t[1] - 1) if t[1] - 1 > 0 else 0,)1155 for t in self.emp_table])1156 self.check_result(query, expected)1157 def test_safediv_3_function(self):1158 query = """1159 out = [FROM SCAN(%s) AS X EMIT SafeDiv(X.salary,X.dept_id-1,42)];1160 STORE(out, OUTPUT);1161 """ % self.emp_key1162 expected = collections.Counter(1163 [(t[3] / (t[1] - 1) if t[1] - 1 > 0 else 42,)1164 for t in self.emp_table])1165 self.check_result(query, expected)1166 def test_answer_to_everything_function(self):1167 query = """1168 out = [TheAnswerToLifeTheUniverseAndEverything()];1169 STORE(out, OUTPUT);1170 """1171 expected = collections.Counter([(42,)])1172 self.check_result(query, expected)1173 def test_least_function(self):1174 query = """1175 out = [FROM SCAN(%s) AS X EMIT least(X.id,X.dept_id,1)];1176 STORE(out, OUTPUT);1177 """ % self.emp_key1178 expected = collections.Counter(1179 [(min(t[0], t[1], 1),)1180 for t in self.emp_table])1181 self.check_result(query, expected)1182 def test_greatest_function(self):1183 query = """1184 out = [FROM SCAN(%s) AS X EMIT greatest(X.id,X.dept_id,3)];1185 STORE(out, OUTPUT);1186 """ % self.emp_key1187 expected = collections.Counter(1188 [(max(t[0], t[1], 3),)1189 for t in self.emp_table])1190 self.check_result(query, expected)1191 def test_lesser_function(self):1192 query = """1193 out = [FROM SCAN(%s) AS X EMIT lesser(X.id,X.dept_id)];1194 STORE(out, OUTPUT);1195 """ % self.emp_key1196 expected = collections.Counter(1197 [(min(t[0], t[1]),)1198 for t in self.emp_table])1199 self.check_result(query, expected)1200 def test_greater_function(self):1201 query = """1202 out = [FROM SCAN(%s) AS X EMIT greater(X.id,X.dept_id)];1203 STORE(out, OUTPUT);1204 """ % self.emp_key1205 expected = collections.Counter(1206 [(max(t[0], t[1],),)1207 for t in self.emp_table])1208 self.check_result(query, expected)1209 def test_uda_illegal_init(self):1210 query = """1211 uda Foo(x,y) {1212 [0 as A, *];1213 [A + x, A + y];1214 A;1215 };1216 out = [FROM SCAN(%s) AS X EMIT dept_id, Foo(salary, id)];1217 STORE(out, OUTPUT);1218 """ % self.emp_key1219 with self.assertRaises(IllegalWildcardException):1220 self.check_result(query, None)1221 def test_uda_illegal_update(self):1222 query = """1223 uda Foo(x,y) {1224 [0 as A, 1 as B];1225 [A + x + y, *];1226 A + B;1227 };1228 out = [FROM SCAN(%s) AS X EMIT dept_id, Foo(salary, id)];1229 STORE(out, OUTPUT);1230 """ % self.emp_key1231 with self.assertRaises(MyrialCompileException):1232 self.check_result(query, None)1233 def test_uda_nested_emitter(self):1234 query = """1235 uda Foo(x) {1236 [0 as A];1237 [A + x];1238 [A];1239 };1240 uda Bar(x) {1241 [0 as B];1242 [B + x];1243 Foo(B);1244 };1245 out = [FROM SCAN(%s) AS X EMIT dept_id, Bar(salary)];1246 STORE(out, OUTPUT);1247 """ % self.emp_key1248 with self.assertRaises(NestedAggregateException):1249 self.check_result(query, None)1250 def test_uda_nested_init(self):1251 query = """1252 uda Foo(x) {1253 [0 as A];1254 [A + x];1255 [A];1256 };1257 uda Bar(x) {1258 [Foo(0) as B];1259 [B + x];1260 B;1261 };1262 out = [FROM SCAN(%s) AS X EMIT dept_id, Bar(salary)];1263 STORE(out, OUTPUT);1264 """ % self.emp_key1265 with self.assertRaises(NestedAggregateException):1266 self.check_result(query, None)1267 def test_uda_nested_update(self):1268 query = """1269 uda Foo(x) {1270 [0 as A];1271 [A + x];1272 [A];1273 };1274 uda Bar(x) {1275 [0 as B];1276 [Foo(B)];1277 B;1278 };1279 out = [FROM SCAN(%s) AS X EMIT dept_id, Bar(salary)];1280 STORE(out, OUTPUT);1281 """ % self.emp_key1282 with self.assertRaises(NestedAggregateException):1283 self.check_result(query, None)1284 def test_uda_unary_emit_arg_list(self):1285 query = """1286 uda MyAvg(val) {1287 [0 as _sum, 0 as _count];1288 [_sum + val, _count + 1];1289 [_sum / _count];1290 };1291 out = [FROM SCAN(%s) AS X EMIT dept_id, MyAvg(salary)];1292 STORE(out, OUTPUT);1293 """ % self.emp_key1294 def agg_func(x):1295 return float(sum(x)) / len(x)1296 expected = self.__aggregate_expected_result(agg_func)1297 self.check_result(query, expected)1298 def test_second_max_uda(self):1299 """UDA to compute the second largest element in a collection."""1300 query = """1301 uda SecondMax(val) {1302 [0 as _max, 0 as second_max];1303 [case when val > _max then val else _max end,1304 case when val > _max then _max when val > second_max then val1305 else second_max end];1306 second_max;1307 };1308 out = [FROM SCAN(%s) AS X EMIT dept_id, SecondMax(salary)];1309 STORE(out, OUTPUT);1310 """ % self.emp_key1311 def agg_func(x):1312 if len(x) < 2:1313 return 01314 else:1315 return sorted(x, reverse=True)[1]1316 expected = self.__aggregate_expected_result(agg_func)1317 self.check_result(query, expected)1318 def test_multi_invocation_uda(self):1319 query = """1320 uda MaxDivMin(val) {1321 [9999999 as _min, 0 as _max];1322 [case when val < _min then val else _min end,1323 case when val > _max then val else _max end];1324 _max / _min;1325 };1326 out = [FROM SCAN(%s) AS X EMIT1327 MaxDivMin(id) + dept_id + MaxDivMin(salary), dept_id];1328 STORE(out, OUTPUT);1329 """ % self.emp_key1330 d = collections.defaultdict(list)1331 for t in self.emp_table.elements():1332 d[t[1]].append(t)1333 results = []1334 for k, tpls in d.iteritems():1335 max_salary = max(t[3] for t in tpls)1336 min_salary = min(t[3] for t in tpls)1337 max_id = max(t[0] for t in tpls)1338 min_id = min(t[0] for t in tpls)1339 results.append((k + float(max_salary) / min_salary +1340 float(max_id) / min_id, k))1341 self.check_result(query, collections.Counter(results))1342 def test_multiple_uda(self):1343 query = """1344 uda MyMax1(val) {1345 [0 as _max];1346 [case when val > _max then val else _max end];1347 _max;1348 };1349 uda MyMax2(val) {1350 [0 as _max];1351 [case when val > _max then val else _max end];1352 _max;1353 };1354 out = [FROM SCAN(%s) AS X EMIT dept_id, MyMax1(salary), MyMax2(id)];1355 STORE(out, OUTPUT);1356 """ % self.emp_key1357 d = collections.defaultdict(list)1358 for t in self.emp_table.elements():1359 d[t[1]].append(t)1360 results = []1361 for k, tpls in d.iteritems():1362 max_salary = max(t[3] for t in tpls)1363 max_id = max(t[0] for t in tpls)1364 results.append((k, max_salary, max_id))1365 self.check_result(query, collections.Counter(results))1366 def test_uda_no_emit_clause(self):1367 query = """1368 uda MyCount() {1369 [0 as _count];1370 [_count + 1];1371 };1372 out = [FROM SCAN(%s) AS X EMIT dept_id, MyCount()];1373 STORE(out, OUTPUT);1374 """ % self.emp_key1375 self.check_result(query, self.__aggregate_expected_result(len))1376 def test_uda_no_emit_clause_many_cols(self):1377 query = """1378 uda MyAggs(x) {1379 [0 as _count, 0 as _sum, 0 as _sumsq];1380 [_count + 1, _sum + x, _sumsq + x*x];1381 };1382 out = [FROM SCAN(%s) AS X EMIT MyAggs(salary) as [a, b, c]];1383 STORE(out, OUTPUT);1384 """ % self.emp_key1385 c = len(list(self.emp_table.elements()))1386 s = sum(d for a, b, c, d in self.emp_table.elements())1387 sq = sum(d * d for a, b, c, d in self.emp_table.elements())1388 expected = collections.Counter([(c, s, sq)])1389 self.check_result(query, expected)1390 # Test with two different column orders in case the undefined1391 # order used by Python is correct by chance.1392 query = """1393 uda MyAggs(x) {1394 [0 as _count, 0 as _sumsq, 0 as _sum];1395 [_count + 1, _sumsq + x*x, _sum + x];1396 };1397 out = [FROM SCAN(%s) AS X EMIT MyAggs(salary) as [a, b, c]];1398 STORE(out, OUTPUT);1399 """ % self.emp_key1400 c = len(list(self.emp_table.elements()))1401 sq = sum(d * d for a, b, c, d in self.emp_table.elements())1402 s = sum(d for a, b, c, d in self.emp_table.elements())1403 expected = collections.Counter([(c, sq, s)])1404 self.check_result(query, expected)1405 def test_uda_with_udf(self):1406 query = """1407 def foo(x, y): x + y;1408 uda max2(x, y) {1409 [0 as _max];1410 [case when foo(x, y) > _max then foo(x, y) else _max end];1411 _max;1412 };1413 out = [FROM SCAN(%s) AS X EMIT dept_id, max2(salary, id)];1414 STORE(out, OUTPUT);1415 """ % self.emp_key1416 d = collections.defaultdict(list)1417 for t in self.emp_table.elements():1418 d[t[1]].append(t)1419 results = []1420 for k, tpls in d.iteritems():1421 results.append((k, max(t[3] + t[0] for t in tpls)))1422 self.check_result(query, collections.Counter(results))1423 def test_uda_with_subsequent_project_0(self):1424 query = """1425 def foo(x, y): x + y;1426 uda max2(x, y) {1427 [0 as _max];1428 [case when foo(x, y) > _max then foo(x, y) else _max end];1429 _max;1430 };1431 inter = [FROM SCAN(%s) AS X EMIT dept_id, max2(salary, id)];1432 out = [from inter emit $0];1433 STORE(out, OUTPUT);1434 """ % self.emp_key1435 d = collections.defaultdict(list)1436 for t in self.emp_table.elements():1437 d[t[1]].append(t)1438 results = []1439 for k, tpls in d.iteritems():1440 results.append((k, max(t[3] + t[0] for t in tpls)))1441 results = [(t[0],) for t in results]1442 self.check_result(query, collections.Counter(results))1443 def test_uda_with_subsequent_project_1(self):1444 query = """1445 def foo(x, y): x + y;1446 uda max2(x, y) {1447 [0 as _max];1448 [case when foo(x, y) > _max then foo(x, y) else _max end];1449 _max;1450 };1451 inter = [FROM SCAN(%s) AS X EMIT dept_id, max2(salary, id)];1452 out = [from inter emit $1];1453 STORE(out, OUTPUT);1454 """ % self.emp_key1455 d = collections.defaultdict(list)1456 for t in self.emp_table.elements():1457 d[t[1]].append(t)1458 results = []1459 for k, tpls in d.iteritems():1460 results.append((k, max(t[3] + t[0] for t in tpls)))1461 results = [(t[1],) for t in results]1462 self.check_result(query, collections.Counter(results))1463 def test_uda_with_subsequent_project_2(self):1464 query = """1465 def foo(x, y): x + y;1466 uda max2(x, y) {1467 [0 as _max];1468 [case when foo(x, y) > _max then foo(x, y) else _max end];1469 _max;1470 };1471 inter = [FROM SCAN(%s) AS X EMIT dept_id, max2(salary, id)1472 , max2(dept_id, id)];1473 out = [from inter emit $1];1474 STORE(out, OUTPUT);1475 """ % self.emp_key1476 d = collections.defaultdict(list)1477 for t in self.emp_table.elements():1478 d[t[1]].append(t)1479 results = []1480 for k, tpls in d.iteritems():1481 results.append((k,1482 max(t[3] + t[0] for t in tpls),1483 max(t[1] + t[0] for t in tpls)))1484 results = [(t[1],) for t in results]1485 self.check_result(query, collections.Counter(results))1486 def __run_multiple_emitter_test(self, include_column_names):1487 if include_column_names:1488 names = " AS [mysum, mycount, myavg]"1489 else:1490 names = ""1491 query = """1492 uda SumCountMean(x) {1493 [0 as _sum, 0 as _count];1494 [_sum + x, _count + 1];1495 [_sum, _count, _sum/_count];1496 };1497 out = [FROM SCAN(%s) AS X EMIT dept_id, SumCountMean(salary) %s,1498 dept_id+3, max(id) as max_id];1499 STORE(out, OUTPUT);1500 """ % (self.emp_key, names)1501 d = collections.defaultdict(list)1502 for t in self.emp_table.elements():1503 d[t[1]].append(t)1504 results = []1505 for k, tpls in d.iteritems():1506 _sum = sum(x[3] for x in tpls)1507 _count = len(tpls)1508 _avg = float(_sum) / _count1509 _max_id = max(x[0] for x in tpls)1510 results.append((k, _sum, _count, _avg, k + 3, _max_id))1511 self.check_result(query, collections.Counter(results))1512 def test_uda_multiple_emitters_default_names(self):1513 self.__run_multiple_emitter_test(False)1514 def test_uda_multiple_emitters_provided_names(self):1515 self.__run_multiple_emitter_test(True)1516 scheme_actual = self.db.get_scheme('OUTPUT')1517 scheme_expected = scheme.Scheme([1518 ('dept_id', types.LONG_TYPE), ('mysum', types.LONG_TYPE),1519 ('mycount', types.LONG_TYPE), ('myavg', types.FLOAT_TYPE),1520 ('_COLUMN4_', types.LONG_TYPE), ('max_id', types.LONG_TYPE)])1521 self.assertEquals(scheme_actual, scheme_expected)1522 def test_emit_arg_bad_column_name_length(self):1523 query = """1524 out = [FROM SCAN(%s) AS X EMIT dept_id AS [dept_id1, dept_id2]];1525 STORE(out, OUTPUT);1526 """ % self.emp_key1527 with self.assertRaises(IllegalColumnNamesException):1528 self.check_result(query, None)1529 def test_uda_bad_column_name_length(self):1530 query = """1531 uda Fubar(x, y, z) {1532 [0 as Q];1533 [Q + 1];1534 [1,2,3];1535 };1536 out = [FROM SCAN(%s) AS X EMIT dept_id, Fubar(1, salary, id)1537 AS [A, B, C, D]];1538 STORE(out, OUTPUT);1539 """ % self.emp_key1540 with self.assertRaises(IllegalColumnNamesException):1541 self.check_result(query, None)1542 def test_uda_init_tuple_valued(self):1543 query = """1544 uda Foo(x) {1545 [0 as Q];1546 [Q + 1];1547 [1,2,3];1548 };1549 uda Bar(x) {1550 [Foo(0) as [A, B, C]];1551 [Q * 8];1552 [1,2,3];1553 };1554 out = [FROM SCAN(%s) AS X EMIT dept_id, Bar(salary)];1555 STORE(out, OUTPUT);1556 """ % self.emp_key1557 with self.assertRaises(NestedTupleExpressionException):1558 self.check_result(query, None)1559 def test_uda_update_tuple_valued(self):1560 query = """1561 uda Foo(x) {1562 [0 as Q];1563 [Q + 1];1564 [1,2,3];1565 };1566 uda Bar(x) {1567 [0 as Q];1568 [Foo(Q + 1)];1569 [1,2,3];1570 };1571 out = [FROM SCAN(%s) AS X EMIT dept_id, Bar(salary)];1572 STORE(out, OUTPUT);1573 """ % self.emp_key1574 with self.assertRaises(NestedTupleExpressionException):1575 self.check_result(query, None)1576 def test_uda_result_tuple_valued(self):1577 query = """1578 uda Foo(x) {1579 [0 as Q];1580 [Q + 1];1581 [1,2,3];1582 };1583 uda Bar(x) {1584 [0 as Q];1585 [Q + 2];1586 [1,2,Foo(3)];1587 };1588 out = [FROM SCAN(%s) AS X EMIT dept_id, Bar(salary)];1589 STORE(out, OUTPUT);1590 """ % self.emp_key1591 with self.assertRaises(NestedTupleExpressionException):1592 self.check_result(query, None)1593 def test_uda_multiple_emitters_nested(self):1594 """Test that we raise an Exception if a tuple-valued UDA doesn't appear1595 by itself in an emit expression."""1596 query = """1597 uda SumCountMean(x) {1598 [0 as _sum, 0 as _count];1599 [_sum + x, _count + 1];1600 [_sum, _count, _sum/_count];1601 };1602 out = [FROM SCAN(%s) AS X EMIT dept_id, SumCountMean(salary) + 5];1603 STORE(out, OUTPUT);1604 """ % self.emp_key1605 with self.assertRaises(NestedTupleExpressionException):1606 self.check_result(query, None)1607 __DECOMPOSED_UDA = """1608 uda LogicalAvg(x) {1609 [0 as _sum, 0 as _count];1610 [_sum + x, _count + 1];1611 float(_sum); -- Note bogus return value1612 };1613 uda LocalAvg(x) {1614 [0 as _sum, 0 as _count];1615 [_sum + x, _count + 1];1616 };1617 uda RemoteAvg(_local_sum, _local_count) {1618 [0 as _sum, 0 as _count];1619 [_sum + _local_sum, _count + _local_count];1620 [_sum/_count];1621 };1622 uda* LogicalAvg {LocalAvg, RemoteAvg};1623 """1624 __ARG_MAX_UDA = """1625 def pickval(id, salary, val, _id, _salary, _val):1626 case when salary > _salary then val1627 when salary = _salary and id > _id then val1628 else _val end;1629 uda ArgMax(id, dept_id, name, salary) {1630 [0 as _id, 0 as _dept_id, "" as _name, 0 as _salary];1631 [pickval(id, salary, id, _id, _salary, _id),1632 pickval(id, salary, dept_id, _id, _salary, _dept_id),1633 pickval(id, salary, name, _id, _salary, _name),1634 pickval(id, salary, salary, _id, _salary, _salary)];1635 [_id, _dept_id, _name, _salary];1636 };1637 """1638 __ARG_MAX_UDA_UNNECESSARY_EXPR = """1639 def pickval(id, salary, val, _id, _salary, _val):1640 case when salary > _salary then val1641 when salary = _salary and id > _id then val1642 else _val end;1643 uda ArgMax(id, dept_id, name, salary) {1644 [0 as _id, 0 as _dept_id, "" as _name, 0 as _salary];1645 [pickval(id, salary, greater(id, id), _id, _salary, _id),1646 pickval(id, salary, lesser(dept_id, dept_id), _id, _salary,1647 _dept_id),1648 pickval(id, salary, case when name="" then name else name end, _id,1649 _salary, _name),1650 pickval(id, salary, salary * 1, _id, _salary, _salary)];1651 [_id, _dept_id, _name, _salary];1652 };1653 """1654 def test_decomposable_average_uda(self):1655 """Test of a decomposed average UDA.1656 Note that the logical aggregate returns a broken value, so1657 this test only passes if we decompose the aggregate properly.1658 """1659 query = """%s1660 out = [FROM SCAN(%s) AS X EMIT dept_id, LogicalAvg(salary)];1661 STORE(out, OUTPUT);1662 """ % (TestQueryFunctions.__DECOMPOSED_UDA, self.emp_key)1663 result_dict = collections.defaultdict(list)1664 for t in self.emp_table.elements():1665 result_dict[t[1]].append(t[3])1666 tuples = []1667 for key, vals in result_dict.iteritems():1668 _cnt = len(vals)1669 _sum = sum(vals)1670 tuples.append((key, float(_sum) / _cnt))1671 self.check_result(query, collections.Counter(tuples))1672 def test_decomposable_nary_uda(self):1673 query = """1674 uda Sum2(x, y) {1675 [0 as sum_x, 0 as sum_y];1676 [sum_x + x, sum_y + y];1677 };1678 uda* Sum2 {Sum2, Sum2};1679 out = [FROM SCAN(%s) AS X EMIT1680 Sum2(id, salary) AS [id_sum, salary_sum]];1681 STORE(out, OUTPUT);1682 """ % self.emp_key1683 result_dict = collections.defaultdict(list)1684 for t in self.emp_table.elements():1685 result_dict[t[1]].append(t)1686 id_sum = sum(t[0] for t in self.emp_table.elements())1687 salary_sum = sum(t[3] for t in self.emp_table.elements())1688 tuples = [(id_sum, salary_sum)]1689 self.check_result(query, collections.Counter(tuples))1690 def test_arg_max_uda(self):1691 """Test of an arg_max UDA.1692 """1693 query = """1694 {arg}1695 emp = scan({emp});1696 out = [from emp emit ArgMax(id, dept_id, name, salary)];1697 store(out, OUTPUT);1698 """.format(arg=self.__ARG_MAX_UDA, emp=self.emp_key)1699 tuples = [(a, b, c, d) for (a, b, c, d) in self.emp_table1700 if all(d > d1 or d == d1 and a >= a11701 for a1, b1, c1, d1 in self.emp_table)]1702 self.check_result(query, collections.Counter(tuples))1703 def test_arg_max_uda_with_references(self):1704 """Test of an arg_max UDA with named, unnamed, and dotted1705 attribute references.1706 """1707 query = """1708 {arg}1709 emp = scan({emp});1710 out = [from emp emit ArgMax(id, emp.dept_id, $2, emp.$3)];1711 store(out, OUTPUT);1712 """.format(arg=self.__ARG_MAX_UDA, emp=self.emp_key)1713 tuples = [(a, b, c, d) for (a, b, c, d) in self.emp_table1714 if all(d > d1 or d == d1 and a >= a11715 for a1, b1, c1, d1 in self.emp_table)]1716 self.check_result(query, collections.Counter(tuples))1717 def test_arg_max_uda_with_functions(self):1718 """Test of an arg_max UDA with expressions as inputs.1719 """1720 query = """1721 {arg}1722 emp = scan({emp});1723 out = [from emp emit ArgMax(id,1724 greater(dept_id, dept_id),1725 case when id=1 then name else name end,1726 salary)];1727 store(out, OUTPUT);1728 """.format(arg=self.__ARG_MAX_UDA, emp=self.emp_key)1729 tuples = [(a, b, c, d) for (a, b, c, d) in self.emp_table1730 if all(d > d1 or d == d1 and a >= a11731 for a1, b1, c1, d1 in self.emp_table)]1732 self.check_result(query, collections.Counter(tuples))1733 def test_decomposable_arg_max_uda(self):1734 """Test of a decomposable arg_max UDA.1735 """1736 query = """1737 {arg}1738 uda* ArgMax {{ArgMax, ArgMax}};1739 emp = scan({emp});1740 out = [from emp emit ArgMax(id, dept_id, name, salary)1741 as [a, b, c, d]];1742 store(out, OUTPUT);1743 """.format(arg=self.__ARG_MAX_UDA, emp=self.emp_key)1744 tuples = [(a, b, c, d) for (a, b, c, d) in self.emp_table1745 if all(d > d1 or d == d1 and a >= a11746 for a1, b1, c1, d1 in self.emp_table)]1747 self.check_result(query, collections.Counter(tuples))1748 """Test of an arg_max UDA with named, unnamed, and dotted1749 attribute references.1750 """1751 def test_decomposable_arg_max_uda_with_references(self):1752 """Test of a decomposable arg_max UDA with named, unnamed, and dotted1753 attribute references.1754 """1755 query = """1756 {arg}1757 uda* ArgMax {{ArgMax, ArgMax}};1758 emp = scan({emp});1759 out = [from emp emit ArgMax(id, emp.dept_id, $2, emp.$3)1760 as [a, b, c, d]];1761 store(out, OUTPUT);1762 """.format(arg=self.__ARG_MAX_UDA, emp=self.emp_key)1763 tuples = [(a, b, c, d) for (a, b, c, d) in self.emp_table1764 if all(d > d1 or d == d1 and a >= a11765 for a1, b1, c1, d1 in self.emp_table)]1766 self.check_result(query, collections.Counter(tuples))1767 def test_decomposable_arg_max_uda_with_functions(self):1768 """Test of a decomposable arg_max UDA with expressions as inputs.1769 """1770 query = """1771 {arg}1772 uda* ArgMax {{ArgMax, ArgMax}};1773 emp = scan({emp});1774 out = [from emp emit ArgMax(id,1775 greater(dept_id, dept_id),1776 case when id=1 then name else name end,1777 salary)];1778 store(out, OUTPUT);1779 """.format(arg=self.__ARG_MAX_UDA, emp=self.emp_key)1780 tuples = [(a, b, c, d) for (a, b, c, d) in self.emp_table1781 if all(d > d1 or d == d1 and a >= a11782 for a1, b1, c1, d1 in self.emp_table)]1783 self.check_result(query, collections.Counter(tuples))1784 def test_arg_max_uda_internal_exprs(self):1785 """Test of an arg_max UDA.1786 """1787 query = """1788 {arg}1789 emp = scan({emp});1790 out = [from emp emit ArgMax(id, dept_id, name, salary)];1791 store(out, OUTPUT);1792 """.format(arg=self.__ARG_MAX_UDA_UNNECESSARY_EXPR, emp=self.emp_key)1793 tuples = [(a, b, c, d) for (a, b, c, d) in self.emp_table1794 if all(d > d1 or d == d1 and a >= a11795 for a1, b1, c1, d1 in self.emp_table)]1796 self.check_result(query, collections.Counter(tuples))1797 def test_arg_max_uda_internal_exprs_with_references(self):1798 """Test of an arg_max UDA with named, unnamed, and dotted1799 attribute references.1800 """1801 query = """1802 {arg}1803 emp = scan({emp});1804 out = [from emp emit ArgMax(id, emp.dept_id, $2, emp.$3)];1805 store(out, OUTPUT);1806 """.format(arg=self.__ARG_MAX_UDA_UNNECESSARY_EXPR, emp=self.emp_key)1807 tuples = [(a, b, c, d) for (a, b, c, d) in self.emp_table1808 if all(d > d1 or d == d1 and a >= a11809 for a1, b1, c1, d1 in self.emp_table)]1810 self.check_result(query, collections.Counter(tuples))1811 def test_arg_max_uda_internal_exprs_with_functions(self):1812 """Test of an arg_max UDA with expressions as inputs.1813 """1814 query = """1815 {arg}1816 emp = scan({emp});1817 out = [from emp emit ArgMax(id,1818 greater(dept_id, dept_id),1819 case when id=1 then name else name end,1820 salary)];1821 store(out, OUTPUT);1822 """.format(arg=self.__ARG_MAX_UDA_UNNECESSARY_EXPR, emp=self.emp_key)1823 tuples = [(a, b, c, d) for (a, b, c, d) in self.emp_table1824 if all(d > d1 or d == d1 and a >= a11825 for a1, b1, c1, d1 in self.emp_table)]1826 self.check_result(query, collections.Counter(tuples))1827 def test_decomposable_arg_max_uda_internal_exprs(self):1828 """Test of a decomposable arg_max UDA.1829 """1830 query = """1831 {arg}1832 uda* ArgMax {{ArgMax, ArgMax}};1833 emp = scan({emp});1834 out = [from emp emit ArgMax(id, dept_id, name, salary)1835 as [a, b, c, d]];1836 store(out, OUTPUT);1837 """.format(arg=self.__ARG_MAX_UDA_UNNECESSARY_EXPR, emp=self.emp_key)1838 tuples = [(a, b, c, d) for (a, b, c, d) in self.emp_table1839 if all(d > d1 or d == d1 and a >= a11840 for a1, b1, c1, d1 in self.emp_table)]1841 self.check_result(query, collections.Counter(tuples))1842 """Test of an arg_max UDA with named, unnamed, and dotted1843 attribute references.1844 """1845 def test_decomposable_arg_max_uda_internal_exprs_with_references(self):1846 """Test of a decomposable arg_max UDA with named, unnamed, and dotted1847 attribute references.1848 """1849 query = """1850 {arg}1851 uda* ArgMax {{ArgMax, ArgMax}};1852 emp = scan({emp});1853 out = [from emp emit ArgMax(id, emp.dept_id, $2, emp.$3)1854 as [a, b, c, d]];1855 store(out, OUTPUT);1856 """.format(arg=self.__ARG_MAX_UDA_UNNECESSARY_EXPR, emp=self.emp_key)1857 tuples = [(a, b, c, d) for (a, b, c, d) in self.emp_table1858 if all(d > d1 or d == d1 and a >= a11859 for a1, b1, c1, d1 in self.emp_table)]1860 self.check_result(query, collections.Counter(tuples))1861 def test_decomposable_arg_max_uda_internal_exprs_with_functions(self):1862 """Test of a decomposable arg_max UDA with expressions as inputs.1863 """1864 query = """1865 {arg}1866 uda* ArgMax {{ArgMax, ArgMax}};1867 emp = scan({emp});1868 out = [from emp emit ArgMax(id,1869 greater(dept_id, dept_id),1870 case when id=1 then name else name end,1871 salary)];1872 store(out, OUTPUT);1873 """.format(arg=self.__ARG_MAX_UDA_UNNECESSARY_EXPR, emp=self.emp_key)1874 tuples = [(a, b, c, d) for (a, b, c, d) in self.emp_table1875 if all(d > d1 or d == d1 and a >= a11876 for a1, b1, c1, d1 in self.emp_table)]1877 self.check_result(query, collections.Counter(tuples))1878 def test_decomposable_average_uda_repeated(self):1879 """Test of repeated invocations of decomposed UDAs."""1880 query = """%s1881 out = [FROM SCAN(%s) AS X EMIT dept_id,1882 LogicalAvg(salary) + LogicalAvg($0)];1883 STORE(out, OUTPUT);1884 """ % (TestQueryFunctions.__DECOMPOSED_UDA, self.emp_key)1885 result_dict = collections.defaultdict(list)1886 for t in self.emp_table.elements():1887 result_dict[t[1]].append(t)1888 tuples = []1889 for key, vals in result_dict.iteritems():1890 _cnt = len(vals)1891 _salary_sum = sum(t[3] for t in vals)1892 _id_sum = sum(t[0] for t in vals)1893 tuples.append((key, (float(_salary_sum) + float(_id_sum)) / _cnt))1894 self.check_result(query, collections.Counter(tuples))1895 def test_decomposable_sum_uda(self):1896 """Test of a decomposed sum UDA.1897 Note that the logical aggregate returns a broken value, so1898 this test only passes if we decompose the aggregate properly.1899 """1900 query = """1901 uda MySumBroken(x) {1902 [0 as _sum];1903 [_sum + x];1904 17; -- broken1905 };1906 uda MySum(x) {1907 [0 as _sum];1908 [_sum + x];1909 };1910 uda* MySumBroken {MySum, MySum};1911 out = [FROM SCAN(%s) AS X EMIT dept_id, MySumBroken(salary)];1912 STORE(out, OUTPUT);1913 """ % self.emp_key1914 self.check_result(query, self.__aggregate_expected_result(sum))1915 def test_decomposable_uda_with_builtin_agg(self):1916 """Test of a decomposed UDA + builtin aggregate.1917 Note that the logical aggregate returns a broken value, so1918 this test only passes if we decompose the aggregate properly.1919 """1920 query = """1921 uda MySumBroken(x) {1922 [0 as _sum];1923 [_sum + x];1924 17; -- broken1925 };1926 uda MySum(x) {1927 [0 as _sum];1928 [_sum + x];1929 };1930 uda* MySumBroken {MySum, MySum};1931 out = [FROM SCAN(%s) AS X EMIT dept_id, MySumBroken(salary), SUM(id)];1932 STORE(out, OUTPUT);1933 """ % self.emp_key1934 result_dict = collections.defaultdict(list)1935 for t in self.emp_table.elements():1936 result_dict[t[1]].append(t)1937 tuples = []1938 for key, vals in result_dict.iteritems():1939 _salary_sum = sum(t[3] for t in vals)1940 _id_sum = sum(t[0] for t in vals)1941 tuples.append((key, _salary_sum, _id_sum))1942 self.check_result(query, collections.Counter(tuples))1943 def test_duplicate_decomposable_uda(self):1944 query = """1945 uda Agg1(x) {1946 [0 as _sum];1947 [_sum + x];1948 };1949 uda* Agg1 {Agg1, Agg1};1950 uda* Agg1 {Agg1, Agg1};1951 """1952 with self.assertRaises(DuplicateFunctionDefinitionException):1953 self.check_result(query, None)1954 def test_decomposable_uda_type_check_fail1(self):1955 query = """1956 uda Logical(x) {1957 [0 as _sum];1958 [_sum + x];1959 };1960 uda Local(x, y) {1961 [0 as _sum];1962 [_sum + x];1963 };1964 uda* Logical {Local, Logical};1965 """1966 with self.assertRaises(InvalidArgumentList):1967 self.check_result(query, None)1968 def test_decomposable_uda_type_check_fail2(self):1969 query = """1970 uda Logical(x) {1971 [0 as _sum];1972 [_sum + x];1973 };1974 uda Remote(x, y) {1975 [0 as _sum];1976 [_sum + x];1977 };1978 uda* Logical {Logical, Remote};1979 """1980 with self.assertRaises(InvalidArgumentList):1981 self.check_result(query, None)1982 def test_decomposable_uda_type_check_fail3(self):1983 query = """1984 uda Logical(x) {1985 [0 as _sum];1986 [_sum + x];1987 };1988 uda Remote(x) {1989 [0 as _sum];1990 [_sum + x];1991 [1, 2, 3];1992 };1993 uda* Logical {Logical, Remote};1994 """1995 with self.assertRaises(InvalidEmitList):1996 self.check_result(query, None)1997 def test_running_mean_sapply(self):1998 query = """1999 APPLY RunningMean(value) {2000 [0 AS _count, 0 AS _sum];2001 [_count + 1, _sum + value];2002 _sum / _count;2003 };2004 out = [FROM SCAN(%s) AS X EMIT id, RunningMean(X.salary)];2005 STORE(out, OUTPUT);2006 """ % self.emp_key2007 tps = []2008 _sum = 02009 _count = 02010 for emp in self.emp_table:2011 _sum += emp[3]2012 _count += 12013 tps.append((emp[0], float(_sum) / _count))2014 self.check_result(query, collections.Counter(tps))2015 def test_sapply_multi_invocation(self):2016 query = """2017 APPLY RunningSum(x) {2018 [0 AS _sum];2019 [_sum + x];2020 _sum;2021 };2022 out = [FROM SCAN(%s) AS X2023 EMIT id, RunningSum(X.salary), RunningSum(id)];2024 STORE(out, OUTPUT);2025 """ % self.emp_key2026 tps = []2027 _sum1 = 02028 _sum2 = 02029 for emp in self.emp_table:2030 _sum1 += emp[3]2031 _sum2 += emp[0]2032 tps.append((emp[0], _sum1, _sum2))2033 self.check_result(query, collections.Counter(tps))2034 def test_118_regression(self):2035 """Regression test for https://github.com/uwescience/datalogcompiler/issues/118""" # noqa2036 query = """2037 out = [FROM SCAN(%s) AS X WHERE dept_id = 2 AND salary = 5000 EMIT id];2038 STORE(out, OUTPUT);2039 """ % self.emp_key2040 expected = collections.Counter(2041 [(x[0],) for x in self.emp_table.elements()2042 if x[1] == 2 and x[3] == 5000])2043 self.check_result(query, expected)2044 def test_scan_emp_empty_statement(self):2045 """Test with an empty statement."""2046 query = """2047 ;;;2048 emp = SCAN(%s);2049 STORE(emp, OUTPUT);;;2050 """ % self.emp_key2051 self.check_result(query, self.emp_table)2052 def test_empty_statement_parse(self):2053 """Program that contains nothing but empty statements."""2054 with self.assertRaises(MyrialCompileException):2055 self.check_result(";", None)2056 def test_case_binary(self):2057 query = """2058 emp = SCAN(%s);2059 rich = [FROM emp EMIT id, CASE WHEN salary > 150002060 THEN salary // salary2061 ELSE 0 // salary END];2062 STORE(rich, OUTPUT);2063 """ % self.emp_key2064 def func(y):2065 if y > 15000:2066 return 12067 else:2068 return 02069 expected = collections.Counter(2070 [(x[0], func(x[3])) for x in self.emp_table.elements()])2071 self.check_result(query, expected)2072 def test_case_ternary(self):2073 query = """2074 emp = SCAN(%s);2075 rich = [FROM emp EMIT id,2076 CASE WHEN salary <= 5000 THEN "poor"2077 WHEN salary <= 25000 THEN "middle class"2078 ELSE "rich"2079 END];2080 STORE(rich, OUTPUT);2081 """ % self.emp_key2082 def func(y):2083 if y <= 5000:2084 return 'poor'2085 elif y <= 25000:2086 return 'middle class'2087 else:2088 return 'rich'2089 expected = collections.Counter(2090 [(x[0], func(x[3])) for x in self.emp_table.elements()])2091 self.check_result(query, expected)2092 def test_case_aggregate(self):2093 query = """2094 emp = SCAN(%s);2095 rich = [FROM emp EMIT SUM(3 * CASE WHEN salary > 150002096 THEN 1 ELSE 0 END)];2097 STORE(rich, OUTPUT);2098 """ % self.emp_key2099 _sum = 3 * len([x for x in self.emp_table.elements()2100 if x[3] > 15000])2101 self.check_result(query, collections.Counter([(_sum,)]))2102 def test_case_unbox(self):2103 query = """2104 TH = [15000];2105 A = [1 AS one, 2 AS two, 3 AS three];2106 emp = SCAN(%s);2107 rich = [FROM emp EMIT SUM(*A.three * CASE WHEN salary > *TH2108 THEN 1 ELSE 0 END)];2109 STORE(rich, OUTPUT);2110 """ % self.emp_key2111 _sum = 3 * len([x for x in self.emp_table.elements()2112 if x[3] > 15000])2113 self.check_result(query, collections.Counter([(_sum,)]))2114 def test_default_column_names(self):2115 with open('examples/groupby1.myl') as fh:2116 query = fh.read()2117 self.execute_query(query)2118 scheme = self.db.get_scheme('OUTPUT')2119 self.assertEquals(scheme.getName(0), "_COLUMN0_")2120 self.assertEquals(scheme.getName(1), "id")2121 def test_worker_id(self):2122 query = """2123 X = [FROM SCAN(%s) AS X EMIT X.id, WORKER_ID()];2124 STORE(X, OUTPUT);2125 """ % self.emp_key2126 expected = collections.Counter([(x[0], 0) for x2127 in self.emp_table.elements()])2128 self.check_result(query, expected)2129 def test_flip_zero(self):2130 """flip(0) should always evaluate to false"""2131 query = """2132 X = [FROM SCAN(%s) AS X WHERE flip(0) EMIT *];2133 STORE(X, OUTPUT);2134 """ % self.emp_key2135 expected = collections.Counter()2136 self.check_result(query, expected)2137 def test_flip_one(self):2138 """flip(1) should always evaluate to true"""2139 query = """2140 X = [FROM SCAN(%s) AS X WHERE flip(1) EMIT *];2141 STORE(X, OUTPUT);2142 """ % self.emp_key2143 expected = collections.Counter(self.emp_table.elements())2144 self.check_result(query, expected)2145 def test_substr(self):2146 query = """2147 ZERO = [0];2148 THREE = [3];2149 out = [FROM SCAN(%s) AS X EMIT X.id, substr(X.name, *ZERO, *THREE)];2150 STORE(out, OUTPUT);2151 """ % self.emp_key2152 expected = collections.Counter(2153 [(x[0], x[2][0:3]) for x in self.emp_table.elements()])2154 self.check_result(query, expected)2155 def test_len(self):2156 query = """2157 out = [FROM SCAN(%s) AS X EMIT X.id, len(X.name)];2158 STORE(out, OUTPUT);2159 """ % self.emp_key2160 expected = collections.Counter(2161 [(x[0], len(x[2])) for x in self.emp_table.elements()])2162 self.check_result(query, expected)2163 def test_head(self):2164 query = """2165 out = [FROM SCAN(%s) AS X EMIT X.id, head(X.name, 10)];2166 STORE(out, OUTPUT);2167 """ % self.emp_key2168 expected = collections.Counter(2169 [(x[0], x[2][0:10]) for x in self.emp_table.elements()])2170 self.check_result(query, expected)2171 def test_tail(self):2172 query = """2173 ZERO = [0];2174 THREE = [3];2175 out = [FROM SCAN(%s) AS X EMIT X.id, tail(X.name, 10)];2176 STORE(out, OUTPUT);2177 """ % self.emp_key2178 expected = collections.Counter(2179 [(x[0], (lambda i: i if len(i) <= 10 else i[len(i) - 10:])(x[2]))2180 for x in self.emp_table.elements()])2181 self.check_result(query, expected)2182 def test_column_name_reserved(self):2183 query = """2184 T = EMPTY(x:INT);2185 A = [FROM T EMIT SafeDiv(x, 3) AS SafeDiv];2186 STORE (A, BadProgram);2187 """2188 with self.assertRaises(ReservedTokenException):2189 self.check_result(query, None)2190 def test_bug_226(self):2191 query = """2192 T = scan({emp});2193 A = select id, salary from T where 1=1;2194 B = select id, salary from A where salary=90000;2195 C = select A.* from B, A where A.salary < B.salary;2196 STORE (C, OUTPUT);2197 """.format(emp=self.emp_key)2198 expected = collections.Counter(2199 (i, s) for (i, d, n, s) in self.emp_table2200 for (i2, d2, n2, s2) in self.emp_table2201 if s2 == 90000 and s < s2)2202 self.assertEquals(expected, self.execute_query(query))2203 def test_column_mixed_case_reserved(self):2204 query = """2205 T = EMPTY(x:INT);2206 A = [FROM T EMIT MAX(x) AS maX];2207 STORE (A, BadProgram);2208 """2209 with self.assertRaises(ReservedTokenException):2210 self.check_result(query, None)2211 def test_variable_name_reserved(self):2212 query = """2213 T = EMPTY(x:INT);2214 avg = COUNTALL(T);2215 STORE (countall, BadProgram);2216 """2217 with self.assertRaises(ReservedTokenException):2218 self.check_result(query, None)2219 def test_empty_query(self):2220 query = """2221 T1 = empty(x:INT);2222 """2223 with self.assertRaises(MyrialCompileException):2224 self.check_result(query, None)2225 def test_sink(self):2226 query = """2227 ZERO = [0];2228 A = [from ZERO emit *];2229 SINK(A);2230 """2231 self.evaluate_sink_query(query)2232 def test_string_cast(self):2233 query = """2234 emp = SCAN(%s);2235 bc = [FROM emp EMIT STRING(emp.dept_id) AS foo];2236 STORE(bc, OUTPUT);2237 """ % self.emp_key2238 ex = collections.Counter((str(d),) for (i, d, n, s) in self.emp_table)2239 ex_scheme = scheme.Scheme([('foo', types.STRING_TYPE)])2240 self.check_result(query, ex)2241 def test_float_cast(self):2242 query = """2243 emp = SCAN(%s);2244 bc = [FROM emp EMIT float(emp.dept_id) AS foo];2245 STORE(bc, OUTPUT);2246 """ % self.emp_key2247 ex = collections.Counter((float(d),) for (i, d, n, s) in self.emp_table) # noqa2248 ex_scheme = scheme.Scheme([('foo', types.DOUBLE_TYPE)])2249 self.check_result(query, ex, ex_scheme)2250 def test_sequence(self):2251 query = """2252 T1 = scan({rel});2253 store(T1, OUTPUT);2254 T2 = scan({rel});2255 store(T2, OUTPUT2);2256 """.format(rel=self.emp_key)2257 physical_plan = self.get_physical_plan(query)2258 self.assertIsInstance(physical_plan, raco.algebra.Sequence)2259 self.check_result(query, self.emp_table, output='OUTPUT')2260 self.check_result(query, self.emp_table, output='OUTPUT2')2261 def test_238_dont_renumber_columns(self):2262 # see https://github.com/uwescience/raco/issues/2382263 query = """2264 x = [1 as a, 2 as b];2265 y = [from x as x1, x as x22266 emit x2.a, x2.b];2267 z = [from y emit a];2268 store(z, OUTPUT);"""2269 self.check_result(query, collections.Counter([(1,)]))2270 def test_implicit_column_names(self):2271 query = """2272 x = [1 as a, 2 as b];2273 y = [from x as x1, x as x22274 emit $0, $1];2275 store(y, OUTPUT);"""2276 expected_scheme = scheme.Scheme([('a', types.LONG_TYPE),2277 ('b', types.LONG_TYPE)])2278 self.check_result(query, collections.Counter([(1, 2)]),2279 scheme=expected_scheme)2280 def test_implicit_column_names2(self):2281 query = """2282 x = [1 as a, 2 as b];2283 y = [from x as x1, x as x22284 emit $2, $3];2285 store(y, OUTPUT);"""2286 expected_scheme = scheme.Scheme([('a', types.LONG_TYPE),2287 ('b', types.LONG_TYPE)])2288 self.check_result(query, collections.Counter([(1, 2)]),2289 scheme=expected_scheme)2290 def test_implicit_column_names3(self):2291 query = """2292 x = [1 as a, 2 as b];2293 y = [from x as x1, x as x22294 emit $2, $1];2295 store(y, OUTPUT);"""2296 expected_scheme = scheme.Scheme([('a', types.LONG_TYPE),2297 ('b', types.LONG_TYPE)])2298 self.check_result(query, collections.Counter([(1, 2)]),2299 scheme=expected_scheme)2300 def test_unbox_index_column_names(self):2301 query = """2302 x = [1 as a, 2 as b];2303 y = [from x as x1, x as x22304 emit x2.$0, x2.$1];2305 store(y, OUTPUT);"""2306 expected_scheme = scheme.Scheme([('a', types.LONG_TYPE),2307 ('b', types.LONG_TYPE)])2308 self.check_result(query, collections.Counter([(1, 2)]),2309 scheme=expected_scheme)2310 def test_duplicate_column_names(self):2311 query = """2312 x = [1 as a, 2 as b];2313 y = [from x as x1, x as x2 emit x1.a, x2.a];2314 store(y, OUTPUT);"""2315 expected_scheme = scheme.Scheme([('a', types.LONG_TYPE),2316 ('a1', types.LONG_TYPE)])2317 self.check_result(query, collections.Counter([(1, 1)]),2318 scheme=expected_scheme)2319 def test_distinct_aggregate_combinations(self):2320 """Test to make sure that aggregates of different columns are not2321 combined together by the optimizer."""2322 query = """2323 emp = scan(%s);2324 ans = [from emp emit sum(dept_id) as d, sum(salary) as s];2325 store(ans, OUTPUT);""" % self.emp_key2326 sum_dept_id = sum([e[1] for e in self.emp_table])2327 sum_salary = sum([e[3] for e in self.emp_table])2328 expected = collections.Counter([(sum_dept_id, sum_salary)])2329 self.check_result(query, expected)2330 def test_bug_245_dead_code_with_do_while_plan(self):2331 """Test to make sure that a dead program (no Stores) with a DoWhile2332 throws the correct parse error."""2333 with open('examples/deadcode2.myl') as fh:2334 query = fh.read()2335 with self.assertRaises(MyrialCompileException):2336 self.check_result(query, None)2337 def test_simple_do_while(self):2338 """count to 32 by powers of 2"""2339 with open('examples/iteration.myl') as fh:2340 query = fh.read()2341 expected = collections.Counter([(32, 5)])...

Full Screen

Full Screen

test_pyshell.py

Source:test_pyshell.py Github

copy

Full Screen

...52 def all_removed(self, text):53 self.assertEqual('', self.regexp.sub('', text))54 def none_removed(self, text):55 self.assertEqual(text, self.regexp.sub('', text))56 def check_result(self, text, expected):57 self.assertEqual(expected, self.regexp.sub('', text))58 def test_empty(self):59 self.all_removed('')60 def test_newline(self):61 self.all_removed('\n')62 def test_whitespace_no_newline(self):63 self.all_removed(' ')64 self.all_removed(' ')65 self.all_removed(' ')66 self.all_removed(' ' * 20)67 self.all_removed('\t')68 self.all_removed('\t\t')69 self.all_removed('\t\t\t')70 self.all_removed('\t' * 20)71 self.all_removed('\t ')72 self.all_removed(' \t')73 self.all_removed(' \t \t ')74 self.all_removed('\t \t \t')75 def test_newline_with_whitespace(self):76 self.all_removed(' \n')77 self.all_removed('\t\n')78 self.all_removed(' \t\n')79 self.all_removed('\t \n')80 self.all_removed('\n ')81 self.all_removed('\n\t')82 self.all_removed('\n \t')83 self.all_removed('\n\t ')84 self.all_removed(' \n ')85 self.all_removed('\t\n ')86 self.all_removed(' \n\t')87 self.all_removed('\t\n\t')88 self.all_removed('\t \t \t\n')89 self.all_removed(' \t \t \n')90 self.all_removed('\n\t \t \t')91 self.all_removed('\n \t \t ')92 def test_multiple_newlines(self):93 self.check_result('\n\n', '\n')94 self.check_result('\n' * 5, '\n' * 4)95 self.check_result('\n' * 5 + '\t', '\n' * 4)96 self.check_result('\n' * 20, '\n' * 19)97 self.check_result('\n' * 20 + ' ', '\n' * 19)98 self.check_result(' \n \n ', ' \n')99 self.check_result(' \n\n ', ' \n')100 self.check_result(' \n\n', ' \n')101 self.check_result('\t\n\n', '\t\n')102 self.check_result('\n\n ', '\n')103 self.check_result('\n\n\t', '\n')104 self.check_result(' \n \n ', ' \n')105 self.check_result('\t\n\t\n\t', '\t\n')106 def test_non_whitespace(self):107 self.none_removed('a')108 self.check_result('a\n', 'a')109 self.check_result('a\n ', 'a')110 self.check_result('a \n ', 'a')111 self.check_result('a \n\t', 'a')112 self.none_removed('-')113 self.check_result('-\n', '-')114 self.none_removed('.')115 self.check_result('.\n', '.')116 def test_unsupported_whitespace(self):117 self.none_removed('\v')118 self.none_removed('\n\v')119 self.check_result('\v\n', '\v')120 self.none_removed(' \n\v')121 self.check_result('\v\n ', '\v')122if __name__ == '__main__':...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run hypothesis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful