Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call function with multiple value kdb\q

Tags:

kdb+

I have a function declaration:

func:{[id;time] select last synp from synp_t where instr_id = id, tp_time < time}

where instr_id has type i and tp_time has type v.

For example, func[8;05:00:11] works fine and gives me value 17.55.

However, if I try func[8 1;05:00:11 07:10:59], I get:

'length ERROR: incompatible lengths (different lengths of operands for synchronized operation or table columns lengths are not the same

What I want to get is, for example, 17.55 9.66.

Same error will pop up as well if I do select res:func_demo[id;time]from tab, where tab is table with two column of instr_id and tp_time.

I guess enlist could possibly resolve the problem, but I don't know exactly how to use it. How could I fix this issue?

like image 930
Chenrui Su Avatar asked Mar 11 '26 16:03

Chenrui Su


1 Answers

Both of the answers here are great but don't give much detail about why you got the 'length error. The error was all down to your where clause where instr_id = id, tp_time < time. When you passed a list of items you actually created a list of boolean lists that are being passed to the where clause. Setting up two example lists to highlight this:

q)show instr_id:-5?10
2 8 0 1 6
q)show tp_time:5?.z.t
01:05:42.696 03:42:39.320 17:44:49.101 15:01:01.470 05:47:49.777

Running your first condition instr_id = id with atoms an lists:

q)instr_id=2
10000b
q)instr_id=2 8
'length
  [0]  instr_id=2 8
               ^

Using = will only work for an atom of a list of equal length to instr_id. If you want to pass multiple items to match you can instead use in:

q)instr_id in 2
10000b
q)instr_id in 2 8
11000b

For the second condition tp_time < time again requires atoms or lists of the same length to be passed:

q)tp_time<.z.t
11111b
q)tp_time<.z.t,.z.t
'length
  [0]  tp_time<.z.t,.z.t
              ^

Can get around this by making use of adverbs, such as each-right /: to compare tp_time to multiple items and using any to cut it down to a single list:

q)tp_time</:.z.t,.z.t
11111b
11111b
q)any tp_time</:.z.t,.z.t
11111b

Hope this helps you to understand where you ran into issues above.

like image 78
Thomas Smyth - Treliant Avatar answered Mar 17 '26 12:03

Thomas Smyth - Treliant



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!