[MDEV-17246] Stored Functions need their own temporary table namespace Created: 2018-09-19  Updated: 2018-10-16

Status: Open
Project: MariaDB Server
Component/s: Data Definition - Procedure, Stored routines
Fix Version/s: None

Type: Task Priority: Major
Reporter: Chris Wilson Assignee: Sergei Golubchik
Resolution: Unresolved Votes: 0
Labels: None


 Description   

At present stored aggregate functions using temporary tables can only be used once within a particular query.

Using the medi_int example from the documentation ( @ https://mariadb.com/kb/en/library/stored-aggregate-functions/ ) one can try:

 
CREATE TABLE t1 (x int, y int);
INSERT INTO t1 values (1,1), (2,1), (3,2);
 
select medi_int(x) from t1;
# works as expected
 
select medi_int(x), medi_int(y) from t1;
# fails due to temporary table tt already existing, it also fails in such a way that the temporary table 'tt' is left, I've not checked whether one can alter the function to catch the error and clean up the temporary table.



 Comments   
Comment by Chris Wilson [ 2018-09-19 ]

In some cases I believe this problem can also be triggered by a query that only uses the Stored Aggregate Function once, but also has a 'HAVING' clause. I need to isolate an example.

Comment by Sergei Golubchik [ 2018-09-19 ]

Right. But in a way it's common to all stored routines — functions, procedures, triggers, anything.

The kind of error you've got is only possible in stored aggregate functions.

But even in a stored routine, if one would need to create a temporary table, one can never know whether it'll conflict with some existing table of the same name.

Comment by Chris Wilson [ 2018-09-19 ]

... but with those other stored routines the routine causing side effects is much more expected, isn't it? Indeed with triggers, often very much the intended use. Also with those it's always possible to work around the problem with a table naming convention.

For the stored aggregate functions it doesn't appear to be possible to work around the problem - here's an example only using the function once, but with a having clause that triggers the issue (again using the medi_int example from the documentation):

CREATE TABLE t1 (x int, y int);
INSERT INTO t1 values (1,1), (2,1), (3,2);
 
select y, medi_int(x), count(*) c from t1 group by 1 having c=1;
# fails due to "Table 'tt' already exists" error

[as an aside, interesting that if you switch x & y such that the count(*)s are all 0 then it doesn't trigger the problem].

Comment by Chris Wilson [ 2018-09-19 ]

In fact:

select y, medi_int(x) z, count(*) c from t1 group by 1 having c=1;
# triggers the problem
 
select y, z, c from (select y, medi_int(x) z, count(*) c from t1 group by 1) t where c=1;
# also triggers the problem
#
# yet:
 
select y, z, c from (select y, medi_int(x) z, count(*) c from t1 group by 1) t having c=1;
# works fine
# but leaves warnings:
# +-------+------+-----------------------------+
# | Level | Code | Message                     |
# +-------+------+-----------------------------+
# | Note  | 4094 | At line 18 in test.medi_int |
# | Note  | 4094 | At line 18 in test.medi_int |
# | Note  | 4094 | At line 18 in test.medi_int |
# +-------+------+-----------------------------+

Comment by Elena Stepanova [ 2018-10-03 ]

Assigning to serg to decide what to do about it, if anything.

Generated at Thu Feb 08 08:35:00 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.