John Dempster - John Dempster

Forum Replies Created

Viewing 15 posts - 1 through 15 (of 21 total)
  • Author
    Posts
  • in reply to: Get todays date time in kdb #111186

    For a full list of available date time types see here:
    http://www.timestored.com/b/forums/topic/kdb-date-time-types/

    in reply to: kdb Date Time Types? #111185

    The date time types available in kdb are:

    char size num literal null name sql java .net
    p 8 12 dateDtimespan 0Np timestamp Timestamp DateTime (r/w)
    m 4 13 2000.01m 0Nm month
    d 4 14 2000.01.01 0Nd date date Date
    z 8 15 dateTtime 0Nz datetime timestamp Timestamp DateTime*(read only)
    n 8 16 00:00:00.000000000 0Nn timespan Timespan TimeSpan
    u 4 17 00:00 0Nu minute
    v 4 18 00:00:00 0Nv second
    t 4 19 00:00:00.000 0Nt time time Time TimeSpan

    Our cheat sheet includes the full data types table:
    http://www.timestored.com/kdb-guides/kdb-reference-card

    in reply to: Import kdb+ data into Excel? #40288

    In qStudio right click on the result panel and choose an export option.

    qStudio csv excel export kdb

    in reply to: What companies use kdb+? Just finance / banks? #40287

    From official customer list:

    Goldman Sachs
    Morgan Stanley
    Merrill Lynch
    J.P. Morgan
    Deutsche Bank
    Oppenheimer Capital Markets
    UniCredit Bank AG
    DekaBank
    ConvergEx
    Commerzbank AG
    RBC Capital Markets
    Connor, Clark & Lunn Financial Group
    GSA Capital
    Total Gas & Power UK

    Other ones include:
    Numerous hedge funds
    Nomura
    MUSI
    UBS
    Barclays Bank
    Citi Bank

    in reply to: kdb+ scheduler #40286

    Many people use cron.tab or autosys for scheduled tasks. From a shell script they can then run q scripts and return exit codes to log success.

    in reply to: kdb+ select columns names as variables #40284

    Or you can mangle strings together and use eval:

    q)f:{value “select “,(“,” sv string x),” from t”}
    q)t:([] q:til 9; w:til 9; e:9?9)
    q)t
    q w e
    —–
    0 0 4
    1 1 2
    2 2 7
    3 3 0
    4 4 1
    5 5 2
    6 6 1
    7 7 8
    8 8 8
    q)f:{value “select “,(“,” sv string x),” from t”}
    q)f[`q`e]
    q e

    0 4
    1 2
    2 7
    3 0
    4 1
    5 2
    6 1
    7 8
    8 8
    q)

    • This reply was modified 6 years, 4 months ago by  John Dempster.
    in reply to: string functions? like search replace regex? #40282

    Someone has actually written up a guide to importing a regular expression library:
    http://code.kx.com/wiki/Cookbook/regex

    kdb+ has some builtin regex features, for use with like and ssr.

    For those who need something more flexible, it’s possible to leverage regex libs such as re2, described below.

    The home for re2 can be found at [1] The code below was compiled for kdb+v3.1 with this release [2] The k.h file can be downloaded from [3] For 64bit linux, this can be compiled as

    g++ -m64 -O2 re2.cc -o re2.so -I . re2/obj/libre2.a -DKXVER=3 -shared -static

    and the resulting re2.so should be copied into $QHOME/l64 subdirectory.

    It can then be loaded and called in kdb+ via

    q)f:`re2 2:(`FullMatch;2) / bind FullMatch to f
    q)f[“hello world”;”hello ..rld”]

    #include
    #include

    #include //malloc
    #include
    #include”k.h”

    using namespace re2;

    extern “C” {
    Z S makeErrStr(S s1,S s2){Z __thread char b[256];snprintf(b,256,”%s – %s”,s1,s2);R b;}
    Z __inline S c2s(S s,J n){S r=(S)malloc(n+1);R r?memcpy(r,s,n),r[n]=0,r:(S)krr((S)”wsfull (re2)”);}
    K FullMatch(K x,K y){
    S s,sy;K r;
    P(x->t&&x->t!=KC&&x->t!=KS&&x->t!=-KS||y->t!=KC,krr((S)”type”))
    U(sy=c2s((S)kC(y),y->n))
    RE2 pattern(sy,RE2::Quiet);
    free(sy);
    P(!pattern.ok(),krr(makeErrStr((S)”bad regex”,(S)pattern.error().c_str())))
    if(!x->t||x->t==KS){
    J i=0;
    K r=ktn(KB,x->n);
    for(;in;i++){
    K z=0;
    P(!x->t&&(z=kK(x)[i])->t!=KC,(r0(r),krr((S)”type”)))
    s=z?c2s((S)kC(z),z->n):kS(x)[i];P(!s,(r0(r),(K)0))
    kG(r)[i]=RE2::FullMatch(s,pattern);
    if(z)free(s);
    }
    R r;
    }
    s=x->t==-KS?x->s:c2s((S)kC(x),x->n);
    r=kb(RE2::FullMatch(s,pattern));
    if(s!=x->s)free(s);
    R r;
    }
    }

    in reply to: Get todays date time in kdb #40252

    The uppercase letters return local date time e.g. .z.Z

    While the lowercase letters return GMT time

    The offset is based on the OS settings.

    in reply to: kdb+ scheduler #614

    kdb+ does not have a scheduler, it provides a timer which can be used to schedule simple regular tasks.

    The \t timer is specified in milliseconds, saying how long the delay between firings are.
    The event handler .z.ts can then be defined to perform an action every X milliseconds.

    e.g.

    \t can be used to either find the current timer setting (0 is off) or set the between
    `
    q)\t
    0i
    q) / Set our timer handler to show the current time
    q).z.ts:{show .z.t}
    q)\t 11000
    q)15:40:58.353
    15:41:09.353
    15:41:20.352

    q)\t 5000
    q)15:41:23.724
    15:41:28.723
    15:41:33.723`

    Turn it off, then set timer to increment a variable, restart timer to trigger every 100 milliseconds.
    `
    q)\t 0
    q)a:1
    q).z.ts:{a::a+1}
    q)\t 100
    q)a
    10
    q)a
    17
    q)a
    25
    q)a
    32`

    in reply to: rank error during select x y z from table #527

    Most programming languages follow the substitution principle that assigning a value to a variable, means anywhere that variable is used the user can consider it as if the value was directly entered in place. Breaking this common rule to allow simplification of the parser at the expense of user friendliness is not a choice many other languages have made.

     

    Because this is such uncommon and non-intuitive behavior for what is a common piece of code, I would call it a bug.

    in reply to: string functions? like search replace regex? #520

    When selecting based on the condition of a string or char array column, “like” is normally used.

    The verb like takes two arguments
    1. The list of strings to search
    2. The pattern to be searched for
    The pattern has certain meta characters like *,? and [] that allow pattern matching.
    However full regular expressions are not supported.

    Here are some examples:

    q)t:([] a:til 6; name:(“John Smith”;”John Doe”;”Kate Beck”;”George Bush”;”Isaac Asimov”; “Dave Beck”))
    q)t
    a name
    —————-
    0 “John Smith”
    1 “John Doe”
    2 “Kate Beck”
    3 “George Bush”
    4 “Isaac Asimov”
    5 “Dave Beck”

    / perfect matches, rely on correct case
    q)select from t where name like “John Smith”
    a name
    ————–
    0 “John Smith”
    q)select from t where name like “John smith”
    a name
    ——

    / * – matches any number of characters
    q)select from t where name like “John*”
    a name
    ————–
    0 “John Smith”
    1 “John Doe”

    / ? – Represents any single character
    q)select from t where name like “???? Beck”
    a name
    ————-
    2 “Kate Beck”
    5 “Dave Beck”

    / [] – square brackets contain allowed character sets
    q)select from t where name like “John [S]*”
    a name
    ————–
    0 “John Smith”

    q)select from t where name like “John [SD]*”
    a name
    ————–
    0 “John Smith”
    1 “John Doe”

    Here we used it to match a table with a string column, you can use it on a plain list of strings or symbols as well.

    Regards,
    John

    in reply to: Are there seminars and conferences for kdb+ ? #444

    KX regularly hold workshops internationally: http://www.kx.com/events-kx.php
    We provide free kdb+ tutorials for learning kdb: http://www.timestored.com/kdb-guides
    We can come deliver a training course at your company: http://www.timestored.com/kdb-training
    There are two mailing lists, a few linkedin groups

    Best way to learn is to go through q for mortals, our free tutorials then try and use it to solve an actual problem you have or make up one.

    Best of luck.

    -JD

    in reply to: curly brackets mean what in kdb+q? #443

    Curly brackets contain a function. e.g.
    {x+1}
    would be a function that adds one to it’s argument.

    {[a] b:a*10; b}
    In this function we have named the argument a
    Created a variable b which is ten times a
    and returned that vlue (Since it’s the last statement)

    {} is only used to contain function bodies there is no other use in kdb.

    in reply to: show tables in kdb+ #442

    \a or system “a” will list all the tables that exist on the kdb+ server.

    q)t:([] a:1 2 3)
    q)\a
    ,`t
    q)system “a”
    ,`t
    q)t2:([] c:4 5)
    q)\a
    `s#`t`t2
    q)system “a”
    `s#`t`t2

    in reply to: rank error during select x y z from table #260

    It is a bug. q has issues with default parameters and qsql queries inside function. You can see this from the code below:

    `q)t:([] a:1 2 3 4 5; b:6 7 8 9 0)
    q)t
    a b

    1 6
    2 7
    3 8
    4 9
    5 0`

    One parameter works
    `
    q){select from t where a in x}[2 3]
    a b

    2 7
    3 8`

    Two parameters fail.
    `
    q){select from t where a in x,b in y}[2 3;6 7]
    ‘rank
    `

    Notice when I name the parameters the function executes successfully.
    `
    q){[x;y] select from t where a in x,b in y}[2 3;6 7]
    a b

    2 7`

    I recommend specifying sensible names (not x/y) as much as possible.

Viewing 15 posts - 1 through 15 (of 21 total)