1

I am getting error 'cast when I try to union join (uj) two very simple, five row tables in KDB+.

What is the cause of the error in the below code?

q)t1b
Symbol   | RIC
---------| ---------
000001.SZ| 000001.SZ
000001.ZK| 000001.ZK
000002.SZ| 000002.SZ
000002.ZK| 000002.ZK
000004.SZ| 000004.SZ
q)t2b
Symbol| RIC
------| ------
1301  | 1301.T
1332  | 1332.T
1333  | 1333.T
1334  | 1334.T
1352  | 1352.T
q)meta t1b
c     | t f a
------| -----
Symbol| s
RIC   | s
q)meta t2b
c     | t f a
------| -----
Symbol| s
RIC   | s
q)keys t1b
,`Symbol
q)keys t2b
,`Symbol
q)t1b uj t2b
k){+.[x;(!+y;i);:;.+y z i:&z<#y]}
'cast
q))

If I strip the primary key, join, then re-apply primary key, it works. But why? Surely, I am missing something fundamental about KDB+ here.

q)tuj: `Symbol xkey (() xkey t1b) uj (() xkey t2b)
q)tuj
Symbol   | RIC
---------| ---------
000001.SZ| 000001.SZ
000001.ZK| 000001.ZK
000002.SZ| 000002.SZ
000002.ZK| 000002.ZK
000004.SZ| 000004.SZ
1301     | 1301.T
1332     | 1332.T
1333     | 1333.T
1334     | 1334.T
1352     | 1352.T
q)meta tuj
c     | t f a
------| -----
Symbol| s
RIC   | s
q)keys tuj
,`Symbol
kevinarpe
  • 20,319
  • 26
  • 127
  • 154
  • whats' the type of columns for each table? are they enumerated columns (20h+)? Cast error can come while joining on enumerated cols if value that's getting added is not in its domain. – Rahul Jan 27 '17 at 14:50
  • @Rahul: Does the `meta` command output above not provide type of columns sufficiently for you? (Apologies -- I am not trolling; I am not a KDB+ expert, so I ask honestly.) – kevinarpe Jan 27 '17 at 14:53
  • 1
    @kevinarpe no, meta will tell you it's a symbol but not whether it's enumerated. Try doing `type exec Symbol from t1b` - if you get `11h` it's a symbol type (unenumerated). `20` and over means enumerated – Manish Patel Jan 27 '17 at 14:56
  • @user2393012: I will try when I return to the office after Chinese New Year! Your suggestion sounds very good. This is, perhaps, the "metadata" I was missing to debug my error. – kevinarpe Jan 27 '17 at 15:00

3 Answers3

3

Most likely because some of your symbols are enumerated and some aren't

terrylynch
  • 11,844
  • 13
  • 21
  • Can you elaborate? Deeper: I am first selecting from splayed tables on disk into memory, then selecting distinct Symbols, then... my sample code. – kevinarpe Jan 27 '17 at 14:52
  • 2
    Yes, you're selecting from a splay which means that the symbols are enumerated. Are you then generating another table in memory which has non-enumerated symbols (in which case the join can't work)? Or perhaps symbols enumerated to a different sym file? As a test, try "valuing" all of the symbols before the join and then try the join. i.e. `update value symCol1,value symCol2 from table` – terrylynch Jan 27 '17 at 19:25
  • 2
    Using meta to compare the tables doesn't tell you all the info you need because enumerated symbols and non-enumerated symbols appear the same, when in fact they have different types. Check the types of each of your columns (assuming they're not too big): `type each flip t1b` – terrylynch Jan 27 '17 at 19:28
0

Can you please review your sample code above: You have declared two tables t1b and t2b, but you then get the cast error when trying to join t1 and t2? What are the values of these tables?

t1b and t2b will join:

q)t1b:([Symbol:`000001.SZ`000001.ZK`000002.SZ`000002.ZK`000004.SZ]RIC:`000001.SZ`000001.ZK`000002.SZ`000002.ZK`000004.SZ)
q)t2b:([Symbol:`1301`1332`1333`1334`1352]RIC:`1301.T`1332.T`1333.T`1334.T`1352.T)
q)meta t1b
c     | t f a
------| -----
Symbol| s
RIC   | s
q)meta t2b
c     | t f a
------| -----
Symbol| s
RIC   | s
q)keys t1b
,`Symbol
q)keys t2b
,`Symbol
q)t1b uj t2b
Symbol   | RIC
---------| ---------
000001.SZ| 000001.SZ
000001.ZK| 000001.ZK
000002.SZ| 000002.SZ
000002.ZK| 000002.ZK
000004.SZ| 000004.SZ
1301     | 1301.T
1332     | 1332.T
1333     | 1333.T
1334     | 1334.T
1352     | 1352.T
q)t1b
Symbol   | RIC
---------| ---------
000001.SZ| 000001.SZ
000001.ZK| 000001.ZK
000002.SZ| 000002.SZ
000002.ZK| 000002.ZK
000004.SZ| 000004.SZ
q)t2b
Symbol| RIC
------| ------
1301  | 1301.T
1332  | 1332.T
1333  | 1333.T
1334  | 1334.T
1352  | 1352.T
  • That was a typo -- fixed in my Q, but same error occurs. – kevinarpe Jan 27 '17 at 11:25
  • Is the code snippet I provided above an exact mirror of you code? If so can you try and copy my code above to see if works within a fresh q session? – Daryl Hughes Jan 27 '17 at 11:31
  • Yes, I did a similar test myself before I posted this question. If I create the tables from literal data, the union join works. So what else can cause thie error? Usually it is different primary keys or strange table column attributes. Bizarre. – kevinarpe Jan 27 '17 at 11:51
0

Looks like you are trying to union join tables t1 and t2 rather than t1b and t2b. Joining the example tables as shown should work as expected.

dstrachan
  • 216
  • 1
  • 7