# hDB usage in namespace

**URL:** <https://forum.kx.com/t/hdb-usage-in-namespace/10085>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [July 8, 2015, 2:17am UTC](https://forum.kx.com/t/hdb-usage-in-namespace/10085 "2015-07-08T02:17:00Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Flying1](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@Flying1](https://forum.kx.com/u/Flying1)\
**Post date:** [July 8, 2015, 2:17am UTC](https://forum.kx.com/t/hdb-usage-in-namespace/10085/1 "2015-07-08T02:17:00Z")

</div>

Is there a limitation to use hDB tables within a namespace?

I could try to address the hDB tables, which are in global namespace, with `.[`Table]. But the select statement returned 'type error none the less.

Is there a properly way for library code, which resides in a namespace, to query hDB tables?

---

<div class="post-metadata">

**Author:** ![showvik1](https://avatars.discourse-cdn.com/v4/letter/s/9de0a6/32.png) [@showvik1](https://forum.kx.com/u/showvik1)\
**Post date:** [July 8, 2015, 7:19am UTC](https://forum.kx.com/t/hdb-usage-in-namespace/10085/2 "2015-07-08T07:19:00Z")

</div>

This works for me:

q)a:(a:`a`b`c;b:1 2 3)

q)\d .maint

q.maint)select from `.[`a]

a b

* * *

a 1

b 2

c 3

q.maint)a:(maint:`a`b`c;b:1 2 3)

q.maint)\d .

q)select from .maint.a

maint b

* * *

a &nbsp; &nbsp; 1

b &nbsp; &nbsp; 2

c &nbsp; &nbsp; 3

q).z.K

3.3

Thanks

Showvik

---

<div class="post-metadata">

**Author:** ![Flying1](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@Flying1](https://forum.kx.com/u/Flying1)\
**Post date:** [July 10, 2015, 4:02am UTC](https://forum.kx.com/t/hdb-usage-in-namespace/10085/3 "2015-07-10T04:02:00Z")

</div>

Thanks for your reply, showvik. But you’ve misunderstood my question, I’m afraid. Note that I was having issues with hdb tables (tables that are splayed onto disk and memory mapped from there), not ordinary tables in the global namespace. It’d be great of you could make an example with that working properly.

---

<div class="post-metadata">

**Author:** ![Flying1](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@Flying1](https://forum.kx.com/u/Flying1)\
**Post date:** [July 22, 2015, 7:56am UTC](https://forum.kx.com/t/hdb-usage-in-namespace/10085/4 "2015-07-22T07:56:00Z")

</div>

It seems no one actually references hDB from code within a namespace?

Am I right to assume, in this case, that hDB cannot be used directly within a namespace?

---

<div class="post-metadata">

**Author:** ![pressjonny0](https://avatars.discourse-cdn.com/v4/letter/p/ccd318/32.png) [@pressjonny0](https://forum.kx.com/u/pressjonny0)\
**Post date:** [July 22, 2015, 8:26am UTC](https://forum.kx.com/t/hdb-usage-in-namespace/10085/5 "2015-07-22T08:26:00Z")

</div>

These are fairly common ways:

\d .myns

f1:{select count i by date from value `trades}

f2:{select count i by date from `. `trades}

f3:{select count i by date from value `..trades}

>

---

<div class="post-metadata">

**Author:** ![Flying1](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@Flying1](https://forum.kx.com/u/Flying1)\
**Post date:** [July 22, 2015, 10:58am UTC](https://forum.kx.com/t/hdb-usage-in-namespace/10085/6 "2015-07-22T10:58:00Z")

</div>

Does this work?

This is my test:

> q)\l D:\DATA\hdb
> 
> q)
> 
> q)tables`
> 
> `s#`Daily`Index`Minute
> 
> q)\d .ns
> 
> q.ns)select count i by date from value`Index
> 
> 'Index
> 
> q.ns)f:{select count i by date from value`Index}
> 
> q.ns)f`
> 
> {select count i by date from value`Index}
> 
> 'Index
> 
> .:
> 
> `Index
> 
> q.ns))\</font\>
> 
> q.ns)tables`.
> 
> `s#`Daily`Index`Minute

On Wednesday, July 22, 2015 at 3:26:39 PM UTC+8, Jonny Press wrote:

> These are fairly common ways:
> 
> \d .myns
> 
> f1:{select count i by date from value `trades}
> 
> f2:{select count i by date from `. `trades}
> 
> f3:{select count i by date from value `..trades}
> 
> >

---

<div class="post-metadata">

**Author:** ![pressjonny0](https://avatars.discourse-cdn.com/v4/letter/p/ccd318/32.png) [@pressjonny0](https://forum.kx.com/u/pressjonny0)\
**Post date:** [July 22, 2015, 12:18pm UTC](https://forum.kx.com/t/hdb-usage-in-namespace/10085/7 "2015-07-22T12:18:00Z")

</div>

Put it in a script and they should all work&nbsp;

the&nbsp;

select from `. `Index

form should work in both cases (script and command line).&nbsp;

>

---

<div class="post-metadata">

**Author:** ![Flying1](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@Flying1](https://forum.kx.com/u/Flying1)\
**Post date:** [July 23, 2015, 6:00am UTC](https://forum.kx.com/t/hdb-usage-in-namespace/10085/8 "2015-07-23T06:00:00Z")

</div>

Thanks for your follow-up, Jonny. But no, I still didn’t manage to make it work.

Following are my tests:

Console direct input:

> D:\Dev\>q
> 
> KDB+ 3.3 2015.07.09 Copyright (C) 1993-2015 Kx Systems
> 
> w32/ 4()core 4095MB Think flying-thinkpad 10.166.16.225 NONEXPIRE
> 
> Welcome to kdb+ 32bit edition
> 
> For support please see [http://groups.google.com/d/forum/personal-kdbplus](http://groups.google.com/d/forum/personal-kdbplus)
> 
> Tutorials can be found at [http://code.kx.com/wiki/Tutorials](http://code.kx.com/wiki/Tutorials)
> 
> To exit, type \
> 
> To remove this startup msg, edit q.q
> 
> q)system"l ",getenv`HDB\_DIR
> 
> q)show(`outside;tables`)
> 
> `outside
> 
> `s#`Daily`Index`Minute
> 
> q)\d .ns
> 
> q.ns)show(`inside;tables`.)
> 
> `inside
> 
> `s#`Daily`Index`Minute
> 
> q.ns)HDB:select count i by date from`. `Index
> 
> k){$[#pm;pmx;z in vt x;vp x;+(!+. x)!`/:dd[y;z],x]}
> 
> 'Index
> 
> .:
> 
> `Index
> 
> q.Q))

Test script (test.q):

> system"l ",getenv`HDB\_DIR
> 
> show(`outside;tables`)
> 
> \d .ns
> 
> show(`inside;tables`.)
> 
> /HDB:select count i by date from Index
> 
> /HDB:select count i by date from value `Index
> 
> HDB:select count i by date from`. `Index
> 
> \d .

And the result of running test.q:

> D:\Dev\>q test.q
> 
> KDB+ 3.3 2015.07.09 Copyright (C) 1993-2015 Kx Systems
> 
> w32/ 4()core 4095MB Think flying-thinkpad 10.166.16.225 NONEXPIRE
> 
> Welcome to kdb+ 32bit edition
> 
> For support please see [http://groups.google.com/d/forum/personal-kdbplus](http://groups.google.com/d/forum/personal-kdbplus)
> 
> Tutorials can be found at [http://code.kx.com/wiki/Tutorials](http://code.kx.com/wiki/Tutorials)
> 
> To exit, type \
> 
> To remove this startup msg, edit q.q
> 
> `outside
> 
> `s#`Daily`Index`Minute
> 
> `inside
> 
> `s#`Daily`Index`Minute
> 
> k){$[#pm;pmx;z in vt x;vp x;+(!+. x)!`/:dd[y;z],x]}
> 
> 'Index
> 
> .:
> 
> `Index
> 
> q.Q))

As you can see above, none of the approaches worked for me. Not sure what the diff is between our setups…&nbsp;

---

<div class="post-metadata">

**Author:** ![pressjonny0](https://avatars.discourse-cdn.com/v4/letter/p/ccd318/32.png) [@pressjonny0](https://forum.kx.com/u/pressjonny0)\
**Post date:** [July 23, 2015, 9:03am UTC](https://forum.kx.com/t/hdb-usage-in-namespace/10085/9 "2015-07-23T09:03:00Z")

</div>

Ah right. &nbsp;I see.&nbsp;

I think the way to do what you want is to create an initialising function and invoke it from the top level namespace e.g.&nbsp;

> system"l ",getenv`HDB\_DIR
> 
> show(`outside;tables`)
> 
> \d .ns
> 
> show(`inside;tables`.)
> 
> init:{HDB::select count i by date from`. `Index}
> 
> \d .
> 
> .ns.init

I \*think\* the rule is that an on-disk tables can be referenced from a function in another namespace, but the function can only be successfully invoked from the top level namespace (e.g. any client calls would be invoked from the top-level namespace)

&nbsp;

>

---

<div class="post-metadata">

**Author:** ![ikorkhov1](https://avatars.discourse-cdn.com/v4/letter/i/d2c977/32.png) [@ikorkhov1](https://forum.kx.com/u/ikorkhov1)\
**Post date:** [July 23, 2015, 11:02am UTC](https://forum.kx.com/t/hdb-usage-in-namespace/10085/10 "2015-07-23T11:02:00Z")

</div>

You can “copy” tables you need from the default context to your namespace like this:

$ q

q)\l /path/to/hdb

q)\d .ns

q.ns)Daily: `. `Daily

q.ns)date: `. `date / don’t forget to copy year, month, date or int as well!

q.ns)select count i by date from Daily

You can create a function that copies all partitioned table definitions (or mappings, I am not sure how to call it properly) from the default context into a context (namespace) of your choice I think.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![ikorkhov1](https://avatars.discourse-cdn.com/v4/letter/i/d2c977/32.png) [@ikorkhov1](https://forum.kx.com/u/ikorkhov1)\
**Post date:** [July 23, 2015, 12:06pm UTC](https://forum.kx.com/t/hdb-usage-in-namespace/10085/11 "2015-07-23T12:06:00Z")

</div>

To illustrate my point:

$ q

q)\l /path/to/hdb

q)\d .ns

q.ns)copyFromRoot: {[obj] eval (:;`$(string system "d"), ".", string obj; `. obj) }

q.ns)copyFromRoot each .Q.pt

q.ns)copyFromRoot .Q.pf

q.ns)select count i by date from Index / whatever…

---

<div class="post-metadata">

**Author:** ![Flying1](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@Flying1](https://forum.kx.com/u/Flying1)\
**Post date:** [July 24, 2015, 11:09am UTC](https://forum.kx.com/t/hdb-usage-in-namespace/10085/12 "2015-07-24T11:09:00Z")

</div>

Thanks for this useful tip, Igor. That helps a lot.
