Skip to content

Instantly share code, notes, and snippets.

@rianoc
rianoc / markdown.md
Last active September 26, 2023 06:02
Kdb+ to Markdown
q)t:([] a:1 2 3;s:`lll`h`j;longNameColumn:("ww";"ko";"ooo"))
q)t
a s   longNameColumn
--------------------
1 lll "ww"
2 h   "ko"
3 j   "ooo"
@rianoc
rianoc / dashH.md
Created April 18, 2021 08:06
Kdb+ bytes to KB,MB,GB,TB
`syms _.Q.w[]
used| 10407392
heap| 67108864
peak| 67108864
wmax| 0
mmap| 0
mphy| 8464715776
symw| 1380256
@rianoc
rianoc / isGUID.md
Created April 18, 2021 08:16
Kdb+ test if string is valid GUID

Tempting to check against spec

isGuid:{
 t:{all (8 4 4 4 12~count each "-" vs x;all x in "ABCDEFabcdef0123456789");
 $[36=c:count x;t x;38=c;all (x[0 37]~"{}";t x 1+til 36;0b]
 }

Instead faster is testing roundtrip

@rianoc
rianoc / PATH.md
Created April 18, 2021 08:17
Kdb+ PATH and LD_LIBRARY_PATH manipulation
addToBeginning:{x setenv y,":",":" sv {x where not x~\:""}":" vs getenv x}
addToEnd:{x setenv (":" sv {x where not x~\:""}":" vs getenv x),":",y}
remove:{x setenv ":" sv {x where not any x like/:("";y)}[;y]":" vs getenv x}
pushToEnd:{x setenv ":" sv {x iasc x like y}[;y] {x where not x~\:""}":" vs getenv x}
bringForward:{x setenv ":" sv {x idesc x like y}[;y] {x where not x~\:""}":" vs getenv x}

Add where needed:

@rianoc
rianoc / unpivot.md
Created December 7, 2021 09:57
Kdb+ unpivot
/tab : the table to operate on
/baseCols : the columns not to unpivot
/pivotCols : the columns which you wish to unpivot
/kCol : the key name for unpivoted data
/vCol :  the value name for unpivoted data
unpivot:{[tab;baseCols;pivotCols;kCol;vCol] 
 base:?[tab;();0b;{x!x}(),baseCols];
 newCols:{[k;v;t;p] flip (k;v)!(count[t]#p;t p)}[kCol;vCol;tab] each pivotCols;
@rianoc
rianoc / part.md
Created December 7, 2021 10:02
Kdb+ find source of 'part' error
@rianoc
rianoc / wjr.md
Created December 7, 2021 10:07
Kdb+ window join with rename for multiple aggregations

https://code.kx.com/q/ref/wj/

When permforming wj out of box multiple aggregations requires the user to perform positional renaming to tidy up the result.

Instead a helper can be created:

wjr:{[f;w;c;t;q]
 if[not 99h~type q[1];
 :f[w;c;t;q]];
@rianoc
rianoc / tmp.md
Created December 7, 2021 10:20
Kdb+ minimise use of '/tmp' during 'system' calls

In Kdb+ you cannot control the use of '/tmp' during a system call. You may need to avoid this if the drive space is small.

For familiarity use the TMPDIR environment variable:

q)setenv[`TMPDIR] "/my/chosen/path" 

Function to run system commands:

@rianoc
rianoc / EmbedPyMulti.md
Created December 7, 2021 10:26
Kdb+ EmbedPy handle function that returns more than one value.