Kdb+ Utilities: Displaying q Objects

14 Feb 2018 |
Share on:

By Simon Garland

If you are a kdb+/q developer, you will find the workspace utilities created by Kx Managing Director and Senior Solution Architect Leslie Goldsmith to be a valuable resource. The “Kdb+ Utilities” series of blog posts gives a quick introduction to the utilities, available at Leslie Goldsmith’s GitHub. The first part of the series looked at ws, an essential tool which contains routines for summarizing and searching the contents of a workspace.  In this second part of the series we look at another essential tool for q programmers at every level, dpy, for displaying q objects.

 

A common need of q programmers, especially those just learning q, is the ability to easily understand complicated data structures — to be able to visualize the data contents and structure. By default, q provides a linear representation, which while concise and correct, is not always very helpful with complex or deeply nested data.

Leslie has created a utility called dpy which displays both the data structure and the datatypes in one visually accessible diagram. This style of display has been available for many years to J and Dialog APL programmers, but until now, it hasn’t been available to q programmers.

To use it, you simply load in one script file, dpy.q, and use the function dpy to inspect the object you are working with. I suggest everyone keeps this utility lying around for the next time they need to debug an unexpected type or length error. What I appreciate about it is the way it allows me to flip the way I am looking at a problem.

q)dpy 3 4
.---.
|3 4|
'J--'

Note how the datatype (Integer – “J”)is also shown.

q)dpy enlist 3 4
.-----.
|.---.|
||3 4||
|'J--'|
'#----'

q)dpy("yellow";1011000011b)
.------------.
|.------.    |
||yellow|    |
|'C-----'    |
|.----------.|
||1011000011||
|'B---------'|
'#-----------'

q)dpy enlist enlist "abc"
.-------.
|.-----.|
||.---.||
|||abc|||
||'C--'||
|'#----'|
'#------'

q)dpy(((2 3 4;1011000011b);"howdy");2 3#3)
.----------------.
|.--------------.|
||.------------.||
|||.-----.     |||
||||2 3 4|     |||
|||'J----'     |||
|||.----------.|||
||||1011000011||||
|||'B---------'|||
||'#-----------'||
||.-----.       ||
|||howdy|       ||
||'C----'       ||
|'#-------------'|
|.-----.         |
||3 3 3|         |
||3 3 3|         |
|'J----'         |
'#---------------'

Note too how useful this representation can be for understanding parsed selection constraints.

q)dpy parse"select event from tbl where t>0"
.--------------.
|.-.           |
||?|           |
|':'           |
|              |
|`tbl          |
|s             |
|.-------.     |
||.-----.|     |
|||.---.||     |
||||.-.|||     |
|||||>||||     |
||||':'|||     |
||||   |||     |
||||`t |||     |
||||s  |||     |
||||   |||     |
||||0  |||     |
||||j  |||     |
|||'#--'||     |
||'#----'|     |
|'#------'     |
|              |
|0             |
|b             |
|.------------.|
||event| event||
|'Y-----------'|
'#-------------'

 

Leslie Goldsmith is currently a Managing Director and Senior Solution Architect at Kx, a division of First Derivatives (FD), based in Mississauga, Canada. Prior to this position, Leslie was co-founder and Managing Director at Affinity Systems, a software consulting and products firm acquired by FD in 2015. In Leslie’s long and storied career as a technologist, he has built many large, complex enterprise software systems and has set a standard of excellence in engineering for generations of developers. He is also the author of one of the world’s first commercial email systems, Mailbox, which debuted in 1973.

Simon Garland is Chief Customer Officer for Kx. He is responsible for upholding Kx’s high standards for technical excellence and customer responsiveness. Prior to joining Kx in 2002, Simon worked at a database search engine company, before that he worked at Credit Suisse in risk management. Simon began his programming career working with APL, and subsequently worked with the original k and kdb, as well as q and kdb+.