Joinutility seperatorLogin utility separator Infobright.com
   
1 of 2
1
Why Stored Function Can’t With Parameters
Posted: 20 January 2011 08:19 AM   Ignore ]  
Newbie
Rank
Total Posts:  31
Joined  2011-01-20

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.

Profile
 
Posted: 20 January 2011 10:13 AM   Ignore ]   [ # 1 ]  
Super Duper Member
Avatar
RankRankRankRankRank
Total Posts:  964
Joined  2008-08-18

Hi!
Well, it is not implemented.

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

Profile
 
Posted: 20 January 2011 11:39 AM   Ignore ]   [ # 2 ]  
Newbie
Rank
Total Posts:  31
Joined  2011-01-20

Here is the code come from ICE 3.5 beta
bool MysqlExpression::SanityAggregationCheck(...)
{
  ...
  if(dynamic_cast<Item_func_sp*>(item) != NULL) {
      Item_func_sp* ifunc = dynamic_cast<Item_func_sp*>(item);
      if(ifunc->argument_count() != 0)
        return false;
      return true;
  }
  ....
}

I comment the argument count check and re-build , do some simple test and could get expected result .

I want to know why need check the parameters count , and if there are some other impact if I comment these code.

Thanks.

Profile
 
Posted: 20 January 2011 12:06 PM   Ignore ]   [ # 3 ]  
Super Duper Member
Avatar
RankRankRankRankRank
Total Posts:  964
Joined  2008-08-18

Hi !

AFAIK the problem is when the function is used inside a query and it gets arguments from database tables e.g.

DESC t;
 > 
a INTEGER

SELECT storfunc
(afrom t

Getting values from table t and inserting them as actual function parameters is the problem.

Profile
 
Posted: 20 January 2011 12:32 PM   Ignore ]   [ # 4 ]  
Newbie
Rank
Total Posts:  31
Joined  2011-01-20

CREATE TABLE `t` (
  `Year` year(4) DEFAULT NULL
) ENGINE=BRIGHTHOUSE DEFAULT CHARSET=latin1;

mysql> select * from t;
+———+
| Year |
+———+
| 2001 |
| 2002 |
| 2003 |
| 2004 |
| 2005 |
| 2006 |
| 2007 |
| 2008 |
| 2009 |
+———+
9 rows in set (0.00 sec)

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> select f4(2010) from t;
+—————+
| f4(2010) |
+—————+
|    2010 |
|    2010 |
|    2010 |
|    2010 |
|    2010 |
|    2010 |
|    2010 |
|    2010 |
|    2010 |
+—————+
9 rows in set (26.66 sec)

It’s seem to have a little discrepancy with your replay, why?

Profile
 
Posted: 21 January 2011 12:31 AM   Ignore ]   [ # 5 ]  
Newbie
Rank
Total Posts:  31
Joined  2011-01-20

Sorry ,wrong understood .
I means , if there are any problem if i use stored function with constant parameters.
thanks.

Profile
 
Posted: 21 January 2011 12:47 PM   Ignore ]   [ # 6 ]  
Administrator
RankRankRankRank
Total Posts:  448
Joined  2010-09-22

Hi Ares,

You should not see any issue with using constant parameters in stored functions.  Let us know if this isn’t the case.

Cheers,

Jeff

Signature 

jeff kibler

Profile
 
Posted: 22 January 2011 12:51 AM   Ignore ]   [ # 7 ]  
Newbie
Rank
Total Posts:  31
Joined  2011-01-20

In this case it maybe some thing wrong:

select * from t where f6(2001)=Year


But ,In this case it will work well

select f6(2001) from t ;

Why?

Profile
 
Posted: 24 January 2011 05:27 AM   Ignore ]   [ # 8 ]  
Super Duper Member
Avatar
RankRankRankRankRank
Total Posts:  964
Joined  2008-08-18

Hi !

Please note, that stored function with parameters were never tested as they were not enabled.

What is the f6 definition and Year column definition?

Profile
 
Posted: 25 January 2011 01:54 AM   Ignore ]   [ # 9 ]  
Newbie
Rank
Total Posts:  31
Joined  2011-01-20

DROP FUNCTION IF EXISTS `f6`;
DELIMITER |
CREATE FUNCTION `f6`(y Year) RETURNS Year
DETERMINISTIC—for test
BEGIN
      RETURN y;
END;|
DELIMITER ;

CREATE TABLE `t` (
  `Year` year(4) DEFAULT NULL
) ENGINE=BRIGHTHOUSE DEFAULT CHARSET=latin1

thx.

Profile
 
Posted: 25 January 2011 05:50 AM   Ignore ]   [ # 10 ]  
Super Duper Member
Avatar
RankRankRankRankRank
Total Posts:  964
Joined  2008-08-18

Hi !

In this case it maybe some thing wrong:
select * from t where f6(2001)=Year

What do you mean? With you modifications allowing for stored functions I run the examples successfully:

mysqlCREATE FUNCTION `f6`(y YearRETURNS Year
    
-> DETERMINISTIC
    
-> BEGIN
    
->       RETURN y;
    -> 
END;|
Query OK0 rows affected (0.03 sec)

mysqlDELIMITER ;
mysql
mysqlCREATE TABLE `t` (
    ->   `
Yearyear(4) DEFAULT NULL
    
-> ) ENGINE=BRIGHTHOUSE DEFAULT CHARSET=latin1;
Query OK0 rows affected (0.00 sec)
mysqlinsert into t values (2002);
Query OK1 row affected (0.03 sec)

mysqlinsert into t values (2004);
Query OK1 row affected (0.01 sec)

mysqlinsert into t values (2006);
Query OK1 row affected (0.00 sec)

mysqlselect f6(2003from t;
+----------+
f6(2003) |
+----------+
|     
2003 |
|     
2003 |
|     
2003 |
+----------+
3 rows in set (0.04 sec)

mysqlselect from t;
+------+
Year |
+------+
2002 |
2004 |
2006 |
+------+
3 rows in set (0.00 sec)

mysqlselect from t where year f6(2002);
+------+
Year |
+------+
2002 |
+------+
1 row in set (0.00 sec)

mysqlselect from t where year f6(2006);
+------+
Year |
+------+
2006 |
+------+
1 row in set (0.00 sec)

mysqlselect 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.

[ Edited: 25 January 2011 06:12 AM by Janusz Borkowski]
Profile
 
Posted: 26 January 2011 12:03 AM   Ignore ]   [ # 11 ]  
Newbie
Rank
Total Posts:  31
Joined  2011-01-20

Thanks.

Yes. I also notice the performance depend on the data distribute .

Bc the f6(2001) is passed by constant parameter , i think it should be optimized
It should have the same performance with constant expression.

I’m digging into source code and find how to resolve and Can you give me some advice ?

Profile
 
Posted: 26 January 2011 12:51 AM   Ignore ]   [ # 12 ]  
Newbie
Rank
Total Posts:  31
Joined  2011-01-20

I think MysqlExpression::IsDeterministic is the key function .

So ,I think i can test if the stored function could be evaluate, If it can been evaluated so the stored function should
tread as constant ,is right?

Profile
 
Posted: 26 January 2011 01:30 AM   Ignore ]   [ # 13 ]  
Newbie
Rank
Total Posts:  31
Joined  2011-01-20

bool IsDeterministic() {return deterministic;}

Change to


bool IsDeterministic() {return deterministic ||  vars.empty();}

is right?

Profile
 
Posted: 26 January 2011 02:18 AM   Ignore ]   [ # 14 ]  
Newbie
Rank
Total Posts:  31
Joined  2011-01-20

Performance comparsion
Before change
mysql> select event_date,network_id from q_inventory_daily where f6(‘2010-09-01’)=event_date limit 1;
+——————+——————+
| event_date | network_id |
+——————+——————+
| 2010-09-01 |  96313 |
+——————+——————+
1 row in set (0.86 sec)

mysql> select event_date,network_id from q_inventory_daily where f6(‘2010-12-31’)=event_date limit 1;
+——————+——————+
| event_date | network_id |
+——————+——————+
| 2010-12-31 |  40185 |
+——————+——————+
1 row in set (28 min 22.01 sec)

After change
mysql> select event_date,network_id from q_inventory_daily where event_date=f6(‘2010-09-01’) limit 1;
+——————+——————+
| event_date | network_id |
+——————+——————+
| 2010-09-01 |    96313 |
+——————+——————+
1 row in set (0.01 sec)

mysql> select event_date,network_id from q_inventory_daily where event_date=f6(‘2010-12-31’) limit 1;
+——————+——————+
| event_date | network_id |
+——————+——————+
| 2010-12-31 |    40185 |
+——————+——————+
1 row in set (0.01 sec)

Profile
 
Posted: 26 January 2011 02:48 AM   Ignore ]   [ # 15 ]  
Sr. Member
Avatar
RankRankRankRank
Total Posts:  763
Joined  2008-08-18

Hi,

You’re right - potential nondeterminism is a reason of slower execution of stored functions (even constant). But:

Ares - 26 January 2011 01:30 AM

bool IsDeterministic() {return deterministic;}

Change to

bool IsDeterministic() {return deterministic ||  vars.empty();}

is right?

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.

Regards,

Signature 
Profile
 
   
1 of 2
1