KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ Column accepting multiple types

  • Column accepting multiple types

    Posted by Jonathan on November 28, 2024 at 4:14 pm

    Is it possible for a column to accept multiple types such as a long and float? I have a column that takes both float and long values but I’m not sure how to adjust the table definition to be able to accept both.

    • This discussion was modified 1 month ago by  Jonathan.
    unterrainer_ale replied 1 month ago 2 Members · 1 Reply
  • 1 Reply
  • unterrainer_ale

    Member
    November 28, 2024 at 4:51 pm

    You can create an untyped column by using below syntax:

    t:([] a:(); b:(); c:()) 

    However, the moment you get the first insert the types will be inferred from the first row you insert. Every successive insert that doesn’t align to the types of the first row will throw a type error. Apart from that, it’s pretty much one of the worst design decisions you can make. The power and speed comes from having typed columns and knowing how much memory needs to be assigned and knowing how big each memory block needs to be. Why not simply define the column as float? You can read about it here https://code.kx.com/q4m3/8_Tables/#82-empty-tables-and-schema

    q)meta t

    c| t f a

    -| -----

    a|

    b|

    c|

    q)`t insert (1;2;4.0)</p><p><span style="font-size: 1rem;">,0</span><br></p><p>q)t</p><p><span style="font-size: 1rem;">a b c</span><br></p><p>-----</p><p>1 2 4</p><p>q)meta t</p><p><span style="font-size: 1rem;">c| t f a</span><br></p><p>-| -----</p><p>a| j</p><p>b| j</p><p>c| f</p><p>q)`t insert (1;2;3)

    'type

    [0] `t insert (1;2;3)

    ^




Log in to reply.