Search code examples
symbolskdb+

Update field in table defined as a list of symbols


I have the following table:

q)t:([]col:`a`b`c; vals:((`abc`def);(enlist `abc);(`$())))

q)t
col vals       
---------------
a   `abc`def   
b   ,`abc      
c   `symbol$()

I want to update the entry where col=c, that is the empty list of symbols, but I get a `length error when trying the following query:

update vals:(enlist `unknown) from t where 0=count each vals

The goal is then to do an ungroup t and don't lose the data where vals=`symbol$(), as currently the ungroup is making me losing some rows.


Solution

  • I guess you got the 'length error when you updated some other table, not the one you gave as an example. Your sample code works just fine. The reason why it works on t which has only one row where 0=count each vals but fails on tables which have more than one row satisfying the where condition is the update needs the size of a list of updates to match the target. If a table has more than one row with empty vals an update will fail because you provide a list with only one new value.

    By the way, if a column were a vector (i.e. a simple list of atoms) then providing an atom as an update would work because atoms are extended to match vectors. But vectors don't.

    One way to deal with that is provide a conforming list as Rob suggested in his answer or, as an alternative, use join with each-left:

    q)update vals:(vals,\:`unknown) from 6#t where 0=count each vals
    col vals
    -------------
    a   `abc`def
    b   ,`abc
    c   ,`unknown
    a   `abc`def
    b   ,`abc
    c   ,`unknown