Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in function behavior when called standalone or inside a query in q

Tags:

q-lang

kdb+

I have found a strange issue in q, a possible bug I suppose. I have defined a simple function that returns a float, given a date as input:

give_dummy:{[the_date] 
   /// give_dummy[2013.05.10]  // <- if u wanna test
   :$[ the_date > 2013.01.01 ; 0.001 ; 0.002] ;
 }

It works without problems if called stand-alone:

q)give_dummy[2013.05.10]
0.001

Nevertheless, if I try to call it in a query I get an error:

q)select  give_dummy[date] from tab where sym = sec, i >= first_i  , i < 4000
'type

If I simplify the function to just return the input date (identity function), it works in the query. If I simplify the function to just return a float, without comparing the dates, it works in the query. The problem arises when I USE the input date to compare it in the if-statement: $[ the_date > 2013.01.01 ; 0.001 ; 0.002]

The same happens if I re-define the function taking a float as input, instead than a date, and then I try to give the price as input in the query:

 give_dummy:{[the_price] 
    /// give_dummy[12]  // <- if u wanna test
   :$[ the_price > 20 ; 0.001 ; 0.002] ;
 }
 q) give_dummy[12]
 0.002
 q)select  give_dummy[price] from tab where sym = sec, i >= first_i  , i < 4000
 'type

Do you have any idea of why this happens? I tried everything. Thanks Marco

like image 334
Marco Mene Avatar asked Jan 26 '26 04:01

Marco Mene


1 Answers

You need to either:

select give_dummy each date from tab where ...

Or:

give_dummy:{[the_date] :?[ the_date > 2013.01.01 ; 0.001 ; 0.002]; }
select give_dummy[date] from tab where ...

? is the vector conditional. See here for more details: http://code.kx.com/q4m3/10_Execution_Control/

like image 134
mnestor Avatar answered Jan 29 '26 13:01

mnestor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!