mmv456
mmv456

Reputation: 23

Kdb/q: Function output returning 'length error when querying table

I have a table called DATA that looks like this:

StartDate EndDate NAME TYPE
2023.05.05 2023.09.18 Ash A
2023.01.01 2023.05.04 Ash B
2023.07.03 2023.09.18 May C
2023.03.21 2023.07.02 May D

I have another table with more data, but only has the Name, and I need to get the type based on the date.

For example:

fGetType[`Ash, 2023.09.01] --I expect--> A

fGetType[`May, 2023.23.03] --I expect--> D

I have a function that looks like this:

fGetType:{[Name; Date]
    output: select STARTDATE, ENDDATE, NAME, TYPE from DATA where NAME = Name;
    output: update ValidDate: `Valid from output where Date within (STARTDATE; ENDDATE);
    output: select TYPE from output where ValidDate = `Valid;
    output[0]`TYPE}

I call this function like below, so that I can pass in both items in a row at the same time:

fGetType'[Name; Date]

However, I get the following error message:

'length
  [3]  P:\git\home\utils\myqfile.q:106: fGetType:{[Name; Date]
    output: select STARTDATE, ENDDATE, NAME, TYPE from DATA where NAME = Name;
                   ^
    output: update ValidDate: `Valid from output where Date within (STARTDATE; ENDDATE);

I am struggling to make sense of this message, as I've tried manually using this function and it seems to work fine. Can someone please help to shed some light on this? Thanks in advance.

Upvotes: 0

Views: 424

Answers (3)

Thomas Smyth
Thomas Smyth

Reputation: 5644

Based on the answer you have supplied and the fact you are using fGetType'[Name; Date] for each row it is possible that you are passing Name as list rather than an atom. Have a look at this example using the modified table Alex provided:

data:([] startDate:2023.05.05 2023.01.01 2023.07.03 2023.03.21; endDate:2023.09.18 2023.05.04 2023.09.18 2023.07.02; name:`Ash`Ash`May`May; category:`A`B`C`D)

/ length error when using a list
{select from data where name=x}[(),`Ash]
'length
  [1]  {select from data where name=x}
                                    ^

/ no error when in replaces =
{select from data where name in x}[(),`Ash]
startDate  endDate    name category
-----------------------------------
2023.05.05 2023.09.18 Ash  A
2023.01.01 2023.05.04 Ash  B

You can verify by checking the meta of your table to see if the names are enlisted.

Upvotes: 1

Just a few wholehearted comments in general

Now regarding your initial question f{}'[Name;Date] the ' operator is each-both, do the variables Name and Date have the same length?

Below code works for me. Does it solve what you are looking for?

q)data:([] startDate:2023.05.05 2023.01.01 2023.07.03 2023.03.21; endDate:2023.09.18 2023.05.04 2023.09.18 2023.07.02; name:`Ash`Ash`May`May; category:`A`B`C`D)
q)data
startDate  endDate    name category
-----------------------------------
2023.05.05 2023.09.18 Ash  A
2023.01.01 2023.05.04 Ash  B
2023.07.03 2023.09.18 May  C
2023.03.21 2023.07.02 May  D

q){[x;y] first select from data where x within (startDate;endDate),name=y}'[2023.01.02 2023.07.04;`Ash`May]
startDate  endDate    name category
-----------------------------------
2023.01.01 2023.05.04 Ash  B
2023.07.03 2023.09.18 May  C

q){[x;y] exec first category from data where x within (startDate;endDate),name=y}'[2023.01.02 2023.07.04;`Ash`May]
`B`C

Upvotes: 1

mmv456
mmv456

Reputation: 23

So after a few hours of debugging and trying out different things, it looks like this worked:

fGetType:{[Name; Date]
    output: select STARTDATE, ENDDATE, NAME, TYPE from DATA where NAME in (Name);
    output: update ValidDate: `Valid from output where Date within (STARTDATE; ENDDATE);
    output: select TYPE from output where ValidDate = `Valid;
    output[0]`TYPE}

I don't understand why, as they're both symbols. If anyone has any insight, please let me know.

Upvotes: -1

Related Questions