2

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?

1
  • please check the aj wiki before writing the queries around prevailing prices code.kx.com/wiki/Reference/aj Commented Jun 29, 2018 at 18:38

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much! Learnt a lots from you!
4

func'[8 1;05:00:11 07:10:59]should work

It uses the each both adverb which will apply the function between corresponding elements of the two input lists

More information about adverbs can be found here http://code.kx.com/q/ref/adverbs/. They are very powerful.

A lot of the inbuilt functions are able to take both atoms and lists as inputs and work accordingly, but with user defined functions you will most likely need to make use of adverbs to use them in a variety of situations

2 Comments

Thanks so much! If my result looks like, for example, flip (enlist `synp)!enlist enlist 17.55e, how can I convert it to 17.55..?
To extract values from a column you can use exec: exec synp from <table> or run <table>`synp. Both of these return lists so if you only want an atom you will need to add first.
3

I'll recommend using aj to get the results, looking at your query it looks like you want the last price for the given id and smaller than the input time.

q)func:{[id;time] aj[`instr_id`tp_time; ([] instr_id:id;tp_time: time);synp_t]}
q)func[1 1 2;05:00:11 07:10:59 10:10:00]

with each-both you are calling the function n times and selecting the data n times. aj is one of the powerful features Kdb provides for the asof prices.

Actually, you don't need func function, you can directly get the results using aj.

aj[`instr_id`tp_time; update instr_id:id, tp_time: time from tab;synp_t]

1 Comment

Hi, I really appreciate your helps! aj works for me in this case! Thank you for your patients and answers! It is way faster than the repetitive function call!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.