Search code examples
kdb+k

Define a view into non-default(current) namespace


According to How is a view defined

Views and their dependencies can be defined only in the default namespace.

Also q has a command \b:

Syntax: \b [namespace]

Lists dependencies (views) in namespace. Defaults to current namespace.

According to this I guess that it is possible to create a view not only into the default namespace:

$ q
KDB+ 3.6 2019.04.02 Copyright (C) 1993-2019 Kx Systems
m32/ ...
q)\d .jar
q.jar)v::x+1
q.jar)\d .
q)`. `v
x+1

but the view was created in . namespace.

So is it possible to create a view in a non-default(current) namespace for somehow? If no, why is there an argument for command \b [namespace]?


Solution

  • The answer to your question depends on what you call a namespace. The official q documentation on this topic is vague if not misleading. For example, a page describing the system command \d reads:

    \d (directory)

    Syntax: \d [namespace]

    Sets the current namespace (also known as directory or context). The namespace can be empty, and a new namespace is created when an object is defined in it. The prompt indicates the current namespace.

    As you can see, the optional argument is called a directory on the first line but it becomes a namespace on the second. Which, as we learn from the third line, is "known as context."

    However, the three words -- namespace, directory and context -- can be used interchangeably in some, but not all, cases. Defining a view is one such case where the distinction between directories and namespaces is important.

    Due to the lack of clarity in the official terminology let me refer you to a great book "Q Tips: Fast, Scalable and Maintainable Kdb+" by Nick Psaris. Nick distinguishes a subset of namespaces that begin with a "." and calls them and only them directories. In his terminology all directories are namespaces, but not all namespaces are directories.

    It turns out that directories have limitations; in particular, they can't contain views. But a less known fact is that namespaces that are not directories can:

    q).my.dir.v::x+1 / a (failed) attempt to create a view v in a directory
    'x
      [0]  .my.dir.v::x+1
    q)my.ns.v1::x+1   / v1 is defined in a namespace
    q)your.ns.v2::x-1 / so is v2
    q)\b
    `symbol$()
    q)\b my.ns
    ,`v1
    q)\b your.ns
    ,`v2
    q)x:41
    q)my.ns.v1
    42
    q)your.ns.v2
    40