Acessing date and varbinary in kdb+ from C api.

Hello all,
I have trouble accessing to 2 data fields from table
date and varbinary.

Sample query is :
select date,bindata from test where date=‘1993-01-04’

KI(KK(kData)[0])[i];//date  produces result -15337
KDB+ epoch is after the date, how do I -15337 convert it to 1993-01-04 ?

What function do I use for varbinary access?

Hi Vladimir,

I am not sure what type of input you are expecting to the function as kData. If you are passing the result of
your query directly into the function then you will get a table type K object in C. I believe this may be why
you are getting an integer value of -15337 from your date instead of the correct -2553 (you can verify this by
casting the date to an integer type in q). I think code in your post will return a garbage value if kData is a table.
If you can post an example of the inputs you are passing to the C api from q I may be able to help some more.

In order to access binary data that is passed from kdb+ to C you can just use kG to access the the data.
The example below will take a list of bytes and iterate through each using the kG accessor. Note that the
type of the binary data is just a list of bytes (4).

K print_bytes(K bytes)
{
if (bytes->t != 4) {
return (K) 0; // return if the input is not list of bytes.
}

printf(“bytes size: %d\n”, bytes->n);
printf(“bytes type: %d\n”, bytes->t);

for (int i = 0; i < bytes->n; i++) {
printf(“byte: 0x%02x\n”, kG(bytes)[i]);
}

return (K) 0;
}

Running this function from q should produce output similar to this:

q) print_bytes[0x123456]
bytes size: 3
bytes type: 4
byte: 0x12
byte: 0x34
byte: 0x56

In order to process the date you should get an integer using the kI accessor function. For 1993.01.04
it should be -2553 – this represents the number of days from the kdb epoch. This can be used to calculate
the correct date in C with some arithmetic, but I don’t think there is a cross platform way to do this (different time
resolutions + different starting epochs can cause issues). The conversion functions listed here should give you an
idea of what will be involved:

http://code.kx.com/wiki/Cookbook/InterfacingWithC#Strings_and_datetimes

You can also find some more information on interfacing kdb+ with C at http://www.aquaq.co.uk/resources/ or http://code.kx.com/svn/contrib/aquaqanalytics/InterfacingKDBtoC/

Thanks


Mark Rooney
Financial Software Developer
AQUAQ Analytics