Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
MDEV-18789 Port "MDEV-7773 Aggregate stored functions" to sql_yacc_or…
…a.yy
- Loading branch information
Showing
6 changed files
with
195 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| SET sql_mode=ORACLE; | ||
| create aggregate function f1(x INT) return INT AS | ||
| begin | ||
| insert into t1(sal) values (x); | ||
| return x; | ||
| end| | ||
| ERROR HY000: Aggregate specific instruction(FETCH GROUP NEXT ROW) missing from the aggregate function | ||
| create function f1(x INT) return INT AS | ||
| begin | ||
| set x=5; | ||
| fetch group next row; | ||
| return x+1; | ||
| end | | ||
| ERROR HY000: Non-aggregate function contains aggregate specific instructions: (FETCH GROUP NEXT ROW) | ||
| CREATE TABLE marks(stud_id INT, grade_count INT); | ||
| INSERT INTO marks VALUES (1,6), (2,4), (3,7), (4,5), (5,8); | ||
| SELECT * FROM marks; | ||
| stud_id grade_count | ||
| 1 6 | ||
| 2 4 | ||
| 3 7 | ||
| 4 5 | ||
| 5 8 | ||
| # Using PL/SQL syntax: EXCEPTION WHEN NO_DATA_FOUND | ||
| CREATE AGGREGATE FUNCTION IF NOT EXISTS aggregate_count(x INT) RETURN INT AS | ||
| count_students INT DEFAULT 0; | ||
| BEGIN | ||
| LOOP | ||
| FETCH GROUP NEXT ROW; | ||
| IF x THEN | ||
| count_students:= count_students + 1; | ||
| END IF; | ||
| END LOOP; | ||
| EXCEPTION | ||
| WHEN NO_DATA_FOUND THEN | ||
| RETURN count_students; | ||
| END aggregate_count // | ||
| SELECT aggregate_count(stud_id) FROM marks; | ||
| aggregate_count(stud_id) | ||
| 5 | ||
| DROP FUNCTION IF EXISTS aggregate_count; | ||
| # Using SQL/PSM systax: CONTINUE HANDLER | ||
| CREATE AGGREGATE FUNCTION IF NOT EXISTS aggregate_count(x INT) RETURN INT AS | ||
| count_students INT DEFAULT 0; | ||
| CONTINUE HANDLER FOR NOT FOUND RETURN count_students; | ||
| BEGIN | ||
| LOOP | ||
| FETCH GROUP NEXT ROW; | ||
| IF x THEN | ||
| SET count_students= count_students + 1; | ||
| END IF; | ||
| END LOOP; | ||
| END // | ||
| SELECT aggregate_count(stud_id) FROM marks; | ||
| aggregate_count(stud_id) | ||
| 5 | ||
| DROP FUNCTION IF EXISTS aggregate_count; | ||
| DROP TABLE marks; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| SET sql_mode=ORACLE; | ||
|
|
||
| delimiter |; | ||
| --error ER_INVALID_AGGREGATE_FUNCTION | ||
| create aggregate function f1(x INT) return INT AS | ||
| begin | ||
| insert into t1(sal) values (x); | ||
| return x; | ||
| end| | ||
|
|
||
| --error ER_NOT_AGGREGATE_FUNCTION | ||
| create function f1(x INT) return INT AS | ||
| begin | ||
| set x=5; | ||
| fetch group next row; | ||
| return x+1; | ||
| end | | ||
|
|
||
| DELIMITER ;| | ||
|
|
||
|
|
||
| CREATE TABLE marks(stud_id INT, grade_count INT); | ||
| INSERT INTO marks VALUES (1,6), (2,4), (3,7), (4,5), (5,8); | ||
| SELECT * FROM marks; | ||
|
|
||
| --echo # Using PL/SQL syntax: EXCEPTION WHEN NO_DATA_FOUND | ||
|
|
||
| DELIMITER //; | ||
| CREATE AGGREGATE FUNCTION IF NOT EXISTS aggregate_count(x INT) RETURN INT AS | ||
| count_students INT DEFAULT 0; | ||
| BEGIN | ||
| LOOP | ||
| FETCH GROUP NEXT ROW; | ||
| IF x THEN | ||
| count_students:= count_students + 1; | ||
| END IF; | ||
| END LOOP; | ||
| EXCEPTION | ||
| WHEN NO_DATA_FOUND THEN | ||
| RETURN count_students; | ||
| END aggregate_count // | ||
| DELIMITER ;// | ||
| SELECT aggregate_count(stud_id) FROM marks; | ||
| DROP FUNCTION IF EXISTS aggregate_count; | ||
|
|
||
|
|
||
| --echo # Using SQL/PSM systax: CONTINUE HANDLER | ||
|
|
||
| DELIMITER //; | ||
| CREATE AGGREGATE FUNCTION IF NOT EXISTS aggregate_count(x INT) RETURN INT AS | ||
| count_students INT DEFAULT 0; | ||
| CONTINUE HANDLER FOR NOT FOUND RETURN count_students; | ||
| BEGIN | ||
| LOOP | ||
| FETCH GROUP NEXT ROW; | ||
| IF x THEN | ||
| SET count_students= count_students + 1; | ||
| END IF; | ||
| END LOOP; | ||
| END // | ||
| DELIMITER ;// | ||
| SELECT aggregate_count(stud_id) FROM marks; | ||
| DROP FUNCTION IF EXISTS aggregate_count; | ||
|
|
||
|
|
||
| DROP TABLE marks; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters