DROP FUNCTION IF EXISTS `f1`;
DELIMITER |
CREATE FUNCTION `f1`(network_id BIGINT) RETURNS BIGINT
DETERMINISTIC—for test
BEGIN
DECLARE cn BIGINT(20);
SET cn = (select count(*) from ontime);
RETURN cn;
END;|
DELIMITER ;
select f1(123) from ontime limit 1;
ERROR 5 (HY000): The query includes syntax that is not supported by the Infobright Optimizer. Either restructure the query with supported syntax, or enable the MySQL Query Path in the brighthouse.ini file to execute the query with reduced performance.
It is too difficult just to add it in a free time, and apparently it does not have a high priority in the desired features backlog… I guess you may vote to increase the priority
DROP FUNCTION IF EXISTS `f4`;
DELIMITER |
CREATE FUNCTION `f4`(network_id BIGINT) RETURNS BIGINT
DETERMINISTIC—for test
BEGIN
RETURN network_id;
END;|
DELIMITER ;
mysql> select f4(2010) from t limit 1;
+—————+
| f4(2010) |
+—————+
| 2010 |
+—————+
1 row in set (24.39 sec)
mysql> insert into t values (2004); Query OK, 1 row affected (0.01 sec)
mysql> insert into t values (2006); Query OK, 1 row affected (0.00 sec)
mysql> select f6(2003) from t; +----------+ | f6(2003) | +----------+ | 2003 | | 2003 | | 2003 | +----------+ 3 rows in set (0.04 sec)
mysql> select * from t; +------+ | Year | +------+ | 2002 | | 2004 | | 2006 | +------+ 3 rows in set (0.00 sec)
mysql> select * from t where year = f6(2002); +------+ | Year | +------+ | 2002 | +------+ 1 row in set (0.00 sec)
mysql> select * from t where year = f6(2006); +------+ | Year | +------+ | 2006 | +------+ 1 row in set (0.00 sec)
mysql> select * from t where f6(2002) = year; +------+ | Year | +------+ | 2002 | +------+ 1 row in set (0.00 sec)
However, I cannot say anything about the performance. It may be very bad because unoptimized expressions are used, or it may be acceptable because f6(constant) is a constant and therefore optimizable. No one has tried it yet.
In general, it is not.
To be deterministic, the function must not depend on e.g. current time, system calls, random number generator etc. Additionally, it must not have any side effects (e.g. setting variables).
On the other hand, a function depending only on its parameters is deterministic.