hDB usage in namespace

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?

This works for me:

q)a:(a:ab`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:ab`c;b:1 2 3)

q.maint)\d .

q)select from .maint.a

maint b


a     1

b     2

c     3

q).z.K

3.3

Thanks

Showvik

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.

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?

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}

Does this work?

This is my test:

q)\l D:\DATA\hdb

q)

q)tables`

s#DailyIndexMinute

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#DailyIndexMinute

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}

Put it in a script and they should all work 

the 

select from . Index

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

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

Tutorials can be found at 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#DailyIndexMinute

q)\d .ns

q.ns)show(inside;tables.)

`inside

s#DailyIndexMinute

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

Tutorials can be found at http://code.kx.com/wiki/Tutorials

To exit, type \

To remove this startup msg, edit q.q

`outside

s#DailyIndexMinute

`inside

s#DailyIndexMinute

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… 

Ah right.  I see. 

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. 

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)

 

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.

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…

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