Search code examples
pythonkdbqpython

qPython qpython.qtype.QException: b'length'


So I'm trying to fire the following query (synced) to kdb+:

q.sync('{select from trade where date>x,date<y, sym=z}', 'instr81', '2014.04.14', '2017.04.14')

When executed it gives me raise QException(self._read_symbol()) qpython.qtype.QException: b'length' and I do not get where it is coming from. I've tried other types such as numpy.string_ or and regular bytes (b'instr81') but no luck there.

Am I missing something?


Solution

  • Your order of arguments looks off: from your q.sync call, it looks like x is 'instr81', y is '2017.04.14', and z is '2017.04.14'.

    Your length error is because you're comparing a list of n dates with a string of length m.

    I'm pretty sure your query will also fail because of your date types - you need to send actual dates (as numpy.datetime64 D per the documentation) instead of strings.

    You are also correct in needing to convert your string - as it is, q will be receiving the "symbol" as a string type because it has not been sent as a numpy.string_ type.

    Better syntax might look like the below - also observe the more-performant q code using within rather than date>x,date<y - this will save you reading in all partitions after x and then subsetting for date<y. This is if you wish to make your select inclusive - I noticed that your query is exclusive, so in the query in your example, you should never get any data back (your start == your end so 0 results).

    > import numpy as np
    > q.sync(
        '{[x;y;z]select from trade where date within(x;y),sym=z}', 
        np.datetime64('2014-04-14','D'), #start date
        np.datetime64('2014-04-14','D'), #end date
        np.string_('instr81')
     )