GithubHelp home page GithubHelp logo

kxsystems / rkdb Goto Github PK

View Code? Open in Web Editor NEW
39.0 14.0 29.0 467 KB

R client for kdb+

Home Page: https://code.kx.com/q/interfaces

License: Apache License 2.0

R 32.55% C 67.45%
q kdb r interface kdb-expression

rkdb's Introduction

R client for kdb+

GitHub release (latest by date) Travis (.org) branch AppVeyor branch

Execute kdb+ queries from R for advanced high-performance analytics.

See Interfacing with R on code.kx.com.

Installation

# remove old package
if('rkdb' %in% rownames(installed.packages())) remove.packages('rkdb')
# install devtools
if(! 'devtools' %in% rownames(installed.packages())) install.packages('devtools')
library(devtools)
# install rkdb
devtools::install_github('kxsystems/rkdb', quiet=TRUE,INSTALL_opts=c("--no-multiarch"))
# to install rkdb of particular release
# devtools::install_github('kxsystems/[email protected]', quiet=TRUE)
library(rkdb)

First steps

Set up a connection

Start a kdb+ process to test the installation.

q -p 5000

Open a connection to it.

h <- open_connection('localhost',5000)

Hello kdb+

You can evaluate any kdb+ expression and its result will come back to R:

execute(h, '1+1')

    ## [1] 2

Assigning a variable in the q workspace also works:

execute(h, 'x:1+1') #assign x to 2

    ## NULL

execute(h, 'x') # get back the value

    ## [1] 2

Getting data from kdb+ to R

Kdb+ uses some basic types that might not have a direct equivalent in R. Note also that this is not a bijective operation. The conversions (from kdb to R) are:

kdb/q r
boolean logical
byte raw
short integer
int integer
long integer64
real numeric
float numeric
char character
symbol character
timestamp nanotime
month integer
date Date
datetime POSIXct
timespan integer64
minute difftime
second difftime
time integer
enumeration character
table data.frame
keyed table data.frame
dictionary (mixed types) named list
dictionary (same types) named vector
function character
list (same types) vector
list (same ‘complex’ types) list
list (different types) list

Computing on kdb+

Rkdb provides a convenient way to retrieve computation done on the kdb+ side so you can have the best of both worlds:

kdb <- '
t: ([] x:1000#`a`b`c;y:1000#1f*til 10;z:1000#1f*til 4);
select sum y, dev z by x from t
'

execute(h, kdb)

    ##   x    y        z
    ## 1 a 1503 1.120709
    ## 2 b 1497 1.116689
    ## 3 c 1500 1.116689

One can for instance use R graphical capabilities:

kdb <- '
t: ([] x:1000#`a`b`c;y:1000#1f*til 10;z:1000#1f*til 4);
select y,z from t where x=`a
'

DF <- execute(h, kdb)
plot(DF$y, DF$z, main='scatter plot', xlab='y values', ylab='z values')

Getting data from R to kdb+

Evaluating kdb+ expressions using R objects

You can call kdb+ functions with R objects as arguments. They will be passed and converted to native kdb+ data types, and the kdb+ expression will be evaluated:

execute(h, "raze", list(c(1,2,3), c(4,5,6)))

    ## [1] 1 2 3 4 5 6

execute(h, "+", 2, 5)

    ## [1] 7

execute(h,'{`tmp set x}',data.frame(a=c(1,2,3),b=c("a","b","b")))

    ## [1] "tmp"

For example, here is how you can use the left-join function on two data frames:

DF1 <- data.frame(x=c('x','x','y','y'), y=1:4)
DF2 <- data.frame(x=c('x','y','z'), z=seq(10,30,10))
execute(h, "{[x;y] x lj `x xkey y}", DF1, DF2)

    ##   x y  z
    ## 1 x 1 10
    ## 2 x 2 10
    ## 3 y 3 20
    ## 4 y 4 20

rkdb's People

Contributors

5jt avatar awilson-kx avatar charlieskelton-kx avatar cmccarthy1 avatar jameslittle avatar jhanna-kx avatar lwshang avatar sshanks-kx avatar statquant avatar sv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rkdb's Issues

default timezones set on datetime

I would like to propose modifying src/common.c to remove line 380 in function from_datetime_kobject()

settimezone(result,"GMT");

and also removing or commenting the entire function settimezone() at lines 66-74 since that is the only place this function is used. Here is the reason:
This function returns an R POSIXct object. In R, such an object is always in UTC. It may optionally include a tzone attribute, indicating how it would prefer to be displayed; without that attribute it is displayed by default in the machine local timezone though any different display timezone can also be specified. Kdb+ datetime objects have no such property, so the interface should not add this information. If the Kdb+ datetime is not UTC, then the conversion will simply be incorrect, and adding the label does not fix it.
R Sys.time() returns local time with no default time zone. It would be useful if execute(db,'.z.z') also had no default time zone. At least in older versions of R, warnings were produced if arithmetic were done between POSIXct objects with different default time zones.
Finally, "GMT" is an obsolete specification; the modern usage is "UTC".
To summarize: attaching a default time zone to POSIXct objects returned by rkdb is unnecessary, is an inaccurate reflection of what is in the underlying data table, and is a potential source of confusing errors. It would be simpler to remove that code.

basic type not supported

Hello, it looks like basic type conversion are not supported from R -> q

R) con <- open_connection(port = 12345)
R) DF =  data.frame(x=1:2, y=1L:2L, z = c("11", "11"), d = Sys.Date() + 1:2)
R) execute(con, "{[x] `data set x}", DF)

q) data
x y z    d                
--------------------------
1 1 "11" (,`class)!,"Date"
2 2 "11" 18242 18243f     

Mac e.o is x86_64 only, not arm64

The binary file e.o distributed with the Mac version is only for the older x86_64 architecture, not the newer arm64 that is in M1 Macbooks. I know that Kx has a dual version -- could you include it here with the distribution?

europa: file e.o 
e.o: Mach-O 64-bit object x86_64

should be (with the newer version that Kx does have)

europa: file e.o
e.o: Mach-O universal binary with 2 architectures:
    [x86_64:Mach-O 64-bit object x86_64Mach-O 64-bit object x86_64]
    [arm64:Mach-O 64-bit object arm64Mach-O 64-bit object arm64]
e.o (for architecture x86_64):	Mach-O 64-bit object x86_64
e.o (for architecture arm64):	Mach-O 64-bit object arm64

Segfault when transferring objects >2gb

Strange issue, rkdb seems to segfault with a memory not mapped error (bringing down the entire R process) when transferring objects larger than about 2gb. This is using a 64-bit linux machine and the professional version of kdb+/q.

datetime => POSIXct conversion improvement

The datetime type in q and POSIXct in R are both double precision floating number. But their actual definition is different. datetime represents number of days since 2000.01.01 while POSIXct represents number of seconds since 1970.01.01. So rkdb has to do some floating number arithmetic to complete the conversion.

For a datetime variable y, the conversion procedure can be shown as:

secperday   = 24 * 60 * 60
offset_days = 10957
offset_sec  = secperday *  offset_days

Method A:  ( y * secperday ) + offset_sec
Method B:  ( y + offset_days ) * secperday // currently used in rkdb

The difference between Method A and Method B is only the order of multiplication and addition. Currently, rkdb is using Method B. Consider two datetime variables which are very close to each other, their corresponding R conversion can be the same because Method B do the addition first with shift the floating number to a less precise zone. For example,

> t1 <- execute(h, "`datetime$6803.5601388888890142")
> t2 <- execute(h, "`datetime$6803.5601388888917427")
> t2 == T1
[1] TRUE

If Method A is used, t2 should be greater than t1.

Mistakes in conversions of Q object `minute` and `second`

As mentioned in README file of this repository, minute and second objects should be import to R as difftime object. The current version of rkdb doesn't process it right.

Following R commands shows the mistake:

> t1 <- execute(h, "00:01") # h is a kdb handle opened by open_connection()
> print(t1)
# Time difference of 1 
# Some Error messages, trace back to attr() in print.difftime
> attr(t1, "units")
# Error in as.vector(x): cannot have attributes on a CHARSXP

If we manually create a difftime object in R, the result should be like:

> t2 <- as.difftime(1, units="mins")
> print(t2)
# Time difference of 1 mins
> attr(t2, units)
# 'mins'

Environment:

  • rkdb: v0.10.0
  • R: v3.4.4
  • KDB+: 3.5

Installation error

Today, I tried installing the rkdb package by following the instructions on https://kx.com/blog/kdb-interface-r/

The result was as follows:

* installing *source* package 'rkdb' ...
** using staged installation
** libs

*** arch - i386
c:/Rtools/mingw_32/bin/gcc  -I"C:/PROGRA~1/R/R-36~1.1/include" -DNDEBUG       -D_GNU_SOURCE   -O3 -Wall  -std=gnu99 -mtune=generic -c rkdb.c -o rkdb.o
In file included from rkdb.c:20:0:
common.c: In function 'from_list_of_kobjects':
common.c:202:3: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
   utype= length > 0 ? kK(x)[0]->t : 0;
   ^
In file included from rkdb.c:15:0:
common.c: In function 'from_guid_kobject':
k.h:31:15: warning: pointer targets in passing argument 1 of 'sprintf' differ in signedness [-Wpointer-sign]
 #define kG(x) ((x)->G0)
               ^
k.h:32:15: note: in expansion of macro 'kG'
 #define kC(x) kG(x)
               ^
common.c:253:11: note: in expansion of macro 'kC'
   sprintf(kC(y),"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",gv[ 0],gv[ 1],gv[ 2],gv[ 3],gv[ 4],gv[ 5],gv[ 6],gv[ 7],gv[ 8],gv[ 9],gv[10],gv[11],gv[12],gv[13],gv[14],gv[15]);
           ^
In file included from C:/PROGRA~1/R/R-36~1.1/include/R.h:56:0,
                 from rkdb.c:7:
C:/Rtools/mingw_32/i686-w64-mingw32/include/stdio.h:379:15: note: expected 'char * restrict' but argument is of type 'G *'
   int __cdecl sprintf(char * __restrict__ _Dest,const char * __restrict__ _Format,...) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
               ^
In file included from rkdb.c:20:0:
common.c: In function 'from_dictionary_kobject':
common.c:417:3: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
   K table, k= kK(x)[0], v= kK(x)[1];
   ^
common.c:417:3: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
common.c: In function 'from_table_kobject':
common.c:441:3: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
   PROTECT(names= from_any_kobject(kK(x->k)[0]));
   ^
common.c:442:3: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
   PROTECT(result= from_columns_kobject(kK(x->k)[1]));
   ^
c:/Rtools/mingw_32/bin/gcc -shared -s -static-libgcc -o rkdb.dll tmp.def rkdb.o -Lw32 -le_static -lws2_32 -liphlpapi -LC:/PROGRA~1/R/R-36~1.1/bin/i386 -lR
C:/Rtools/mingw_32/bin/../lib/gcc/i686-w64-mingw32/4.9.3/../../../../i686-w64-mingw32/bin/ld.exe: cannot find -le_static
collect2.exe: error: ld returned 1 exit status
no DLL was created
ERROR: compilation failed for package 'rkdb'

Installation problems

I'm trying to install this package (under R 3.5.1 / Rtools 3.5.0.4) and I get an error

"cannot find -le_static"

under the 32 bit installation (never gets to 64 bit). I've tried to find this but can't find it anywhere.

Rkdb installation not working on Windows

Describe the bug
When I run the following standard installation command: devtools::install_github('kxsystems/rkdb', quiet=FALSE,INSTALL_opts=c("--no-multiarch")), I obtain the following:
`
── R CMD build ───────────────────────────────────────────────────────────────────────────────────────────────────────────
✔ checking for file 'C:\Users\mathi\AppData\Local\Temp\RtmpIh7EBU\remotes350047eb79e9\KxSystems-rkdb-ad47cd7/DESCRIPTION' (354ms)
─ preparing 'rkdb':
✔ checking DESCRIPTION meta-information ...
─ cleaning src
─ checking for LF line-endings in source and make files and shell scripts
─ checking for empty or unneeded directories
─ building 'rkdb_0.13.0.tar.gz'

Installing package into ‘C:/Users/mathi/AppData/Local/R/win-library/4.2’
(as ‘lib’ is unspecified)

  • installing source package 'rkdb' ...
    ** using staged installation
    ** libs
    Warning in system(cmd) : 'make' not found
    ERROR: compilation failed for package 'rkdb'
  • removing 'C:/Users/mathi/AppData/Local/R/win-library/4.2/rkdb'
    Warning messages:
    1: In file.copy(savedcopy, lib, recursive = TRUE) :
    problem copying C:\Users\mathi\AppData\Local\R\win-library\4.2\00LOCK\Rcpp\libs\x64\Rcpp.dll to C:\Users\mathi\AppData\Local\R\win-library\4.2\Rcpp\libs\x64\Rcpp.dll: Permission denied
    2: In i.p(...) :
    installation of package ‘C:/Users/mathi/AppData/Local/Temp/RtmpIh7EBU/file35002b94511d/rkdb_0.13.0.tar.gz’ had non-zero exit status`.

Expected behavior
The package installation should work. Does anyone know how to solve this issue?
Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: Windows11 64 bit

Tables with column type C create data.frames with List columns

I'm unclear whether this is the expected behavior or not, but it wasn't what I expected to get back in R.

In q:

q)meta tt
c        | t f a
---------| -----
sym      | s
AvgPx    | j   s
stringSym| C
> library(rkdb)
> qcon <- rkdb::open_connection(port=6660)
> tt <- rkdb::execute(qcon, "tt")
> str(tt)
'data.frame':	5 obs. of  3 variables:
 $ sym      : chr  "A" "B" "C" "D" ...
 $ AvgPx    : num  10 20 30 40 50
 $ stringSym:List of 5
  ..$ : chr "A"
  ..$ : chr "B"
  ..$ : chr "C"
  ..$ : chr "D"
  ..$ : chr "E"

As opposed what I expected:

> tt$stringSym <- unlist(tt$stringSym)
> str(tt)
'data.frame':	5 obs. of  3 variables:
 $ sym      : chr  "A" "B" "C" "D" ...
 $ AvgPx    : num  10 20 30 40 50
 $ stringSym: chr  "A" "B" "C" "D" ...

Inconsistency of type conversions with the table in README

Some entries in the type conversion table in README are wrong:

kdb/q r (wrong) r (correct)
timespan character difftime
time difftime integer

Correct means what actually happen in rkdb.
Please check whether the README or the code should be fixed.

null conversion is incorrect

Quick question to understand if this package will be developed further and new feature added (and/or debugged)

Unable to install latest version of rkdb (on windows 7)

Hi, sometime in roughly the last 8 months some changes were made to rkdb 0.10 and I can no longer install it (with R CMD INSTALL rkdb on the downloaded source folder).

c:/Users/[...]/Documents/Rtools/mingw_32/bin/gcc -shared -s -static-libgcc -o rkdb.dll tmp.def rkdb.o -Lw32 -le_static -lws2_32 -liphlpapi -LC:/Users/[...]/Documents/R/R-3.5.2/bin/i386 -lR
C:/Users/[...]l/Documents/Rtools/mingw_32/bin/../lib/gcc/i686-w64-mingw32/4.9.3/../../../../i686-w64-mingw32/bin/ld.exe: cannot find -le_static
collect2.exe: error: ld returned 1 exit status
no DLL was created
ERROR: compilation failed for package 'rkdb'

There seem to be new package dependencies on nanotime and bit64, which were installed, but clearly the compiler is unable to find lib e_static

Tested this on windows 7 (requirement for work) with various versions of R and R tools:
R 3.5.2, R tools 3.5.
R 3.5.2, R tools 3.4.
R 3.5.1 R tools 3.5.

No success on any versions.

When I revert back to older souce for rkdb 0.10, the install is successful, thankfully. (a package version bump seems warranted for such a big change to the package).

Cannot install with R 4.3.0

Trying to install with R4.3.0 I get the following output

gcc  -I"C:/PROGRA~1/R/R-43~1.0/include" -DNDEBUG     -I"C:/rtools43/x86_64-w64-mingw32.static.posix/include"  -D_GNU_SOURCE   -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign  -c rkdb.c -o rkdb.o
In file included from rkdb.c:15:
common.c: In function 'from_guid_kobject':
k.h:29:21: warning: passing argument 1 of 'guid_2_char' from incompatible pointer type [-Wincompatible-pointer-types]
   29 | #define kG(x)   ((x)->G0)
      |                 ~~~~^~~~~
      |                     |
      |                     G * {aka unsigned char *}
k.h:29:21: note: in definition of macro 'kG'
   29 | #define kG(x)   ((x)->G0)
      |                     ^~
In file included from rkdb.c:20:
common.c:116:22: note: expected 'K' {aka 'struct k0 *'} but argument is of type 'G *' {aka 'unsigned char *'}
  116 | static K guid_2_char(K);
      |                      ^
common.c:264:20: warning: passing argument 1 of 'guid_2_char' from incompatible pointer type [-Wincompatible-pointer-types]
  264 |     y= guid_2_char((G*)(&kU(x)[i]));
      |                    ^~~~~~~~~~~~~~~
      |                    |
      |                    G * {aka unsigned char *}
common.c:116:22: note: expected 'K' {aka 'struct k0 *'} but argument is of type 'G *' {aka 'unsigned char *'}
  116 | static K guid_2_char(K);
      |                      ^
k.h:29:21: warning: pointer targets in passing argument 1 of 'kp' differ in signedness [-Wpointer-sign]
   29 | #define kG(x)   ((x)->G0)
      |                 ~~~~^~~~~
      |                     |
      |                     G * {aka unsigned char *}
k.h:29:21: note: in definition of macro 'kG'
   29 | #define kG(x)   ((x)->G0)
      |                     ^~
common.c:265:18: note: in expansion of macro 'kC'
  265 |     kK(z)[i]= kp(kC(y));
      |                  ^~
k.h:77:16: note: expected 'S' {aka 'char *'} but argument is of type 'G *' {aka 'unsigned char *'}
   77 |  knk(I,...),kp(S),ja(K*,V*),js(K*,S),jk(K*,K),jv(K*k,K),k(I,const S,...),xT(K),xD(K,K),ktd(K),r1(K),krr(const S),orr(const S),dot(K,K),b9(I,K),d9(K),sslInfo(K x),vaknk(I,va_list),vak(I,const S,va_list),vi(K,UJ),m4(I);
      |                ^
common.c: In function 'guid_2_char':
common.c:468:11: warning: initialization of 'G *' {aka 'unsigned char *'} from incompatible pointer type 'K' {aka 'struct k0 *'} [-Wincompatible-pointer-types]
  468 |     G*gv= x;
      |           ^
k.h:29:21: warning: pointer targets in passing argument 1 of 'sprintf' differ in signedness [-Wpointer-sign]
   29 | #define kG(x)   ((x)->G0)
      |                 ~~~~^~~~~
      |                     |
      |                     G * {aka unsigned char *}
k.h:29:21: note: in definition of macro 'kG'
   29 | #define kG(x)   ((x)->G0)
      |                     ^~
common.c:469:13: note: in expansion of macro 'kC'
  469 |     sprintf(kC(y),"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",gv[ 0],gv[ 1],gv[ 2],gv[ 3],gv[ 4],gv[ 5],gv[ 6],gv[ 7],gv[ 8],gv[ 9],gv[10],gv[11],gv[12],gv[13],gv[14],gv[15]);
      |             ^~
In file included from C:/PROGRA~1/R/R-43~1.0/include/R.h:44,
                 from rkdb.c:7:
c:\rtools43\x86_64-w64-mingw32.static.posix\include\stdio.h:396:20: note: expected 'char *' but argument is of type 'G *' {aka 'unsigned char *'}
  396 | int sprintf (char *__stream, const char *__format, ...)
      |              ~~~~~~^~~~~~~~
gcc -shared -s -static-libgcc -o rkdb.dll tmp.def rkdb.o -L. -lest -lws2_32 -liphlpapi -LC:/rtools43/x86_64-w64-mingw32.static.posix/lib/x64 -LC:/rtools43/x86_64-w64-mingw32.static.posix/lib -LC:/PROGRA~1/R/R-43~1.0/bin/x64 -lR
C:\rtools43\x86_64-w64-mingw32.static.posix\bin/ld.exe: internal error: aborting at ../../binutils-2.40/ld/ldlang.c:527 in compare_section
C:\rtools43\x86_64-w64-mingw32.static.posix\bin/ld.exe: please report this bug

Desktop:

  • OS: Windows 10

Months do not convert to character

Not sure if this is a code fix, or a documentation fix.

q)t:2000.01m
q)tt:(2000.01m;2001.01m;2002.02m)

Produces

> execute(h, "t")
[1] 0
> execute(h, "tt")
[1]  0 12 25

Cant install RKDB package directly from .gz file

Hi,

I tried to install rkdb directly from the zipped file and get the following error (see below) . As I am on a business environment i cant access github via the install_github function, due to blocked API.

Could you please help?

Thanks,

Stefano


install.packages(".../pckgs/rkdb_0.9.0.tar.gz", repos = NULL, type = "source")

  • installing source package 'rkdb' ...
    ** package 'rkdb' successfully unpacked and MD5 sums checked
    ** libs

*** arch - i386
Warning: running command 'make -f "Makevars" -f "C:/PROGRA1/R/R-341.0/etc/i386/Makeconf" -f "C:/PROGRA1/R/R-341.0/share/make/winshlib.mk" SHLIB="rkdb.dll" ' had status 127
ERROR: compilation failed for package 'rkdb'

  • removing 'C:/Program Files/R/R-3.4.0/library/rkdb'
    Warning in install.packages :
    running command '"C:/PROGRA1/R/R-341.0/bin/x64/R" CMD INSTALL -l "C:\Program Files\R\R-3.4.0\library" "P:/FRAIB/ZCM_FIC_Sales/CSSG/Direct Sales/BIGDAT1/360TAN1/other/pckgs/rkdb_0.9.0.tar.gz"' had status 1
    Warning in install.packages :
    installation of package ‘P:/FRAIB/ZCM_FIC_Sales/CSSG/Direct Sales/BIGDAT1/360TAN1/other/pckgs/rkdb_0.9.0.tar.gz’ had non-zero exit status

Add nanotime support for timestamp

There is a package called nanotime that would be very useful to use to convert q timestamp to some R equivalent, as current type is POSIXct that only as microsecond resolution.

Make data types bijective

right now, we have clear conversions from KDB types to R types, but it is not bijective so converting from R to KDB is not clear.
For example, KDB time converts to R difftime, but KDB minute also converts to R difftime.

If I send R difftime to KDB, I get a dictionary on the other side with the attributes as keys.

But this gives us a clue for a useful refactor of rkdb. Lets use custom attributes in R to construct types that rkdb can convert into proper KDB types. For exmaple, make a function like rkdb::minute() that sets the attribute and ultimate results in a minute type in KDB. On the way back, the same thing happens. It is still a difftime in R, but has this attribute indicating which KDB type it came from.

Installation problems on macos

I am trying to install the rkdb package on a mac:

R version 3.6.0 (2019-04-26)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.5

and I am getting this error, which doesn't give me much to go on:

Error in i.p(...) :
(converted from warning) installation of package ‘/var/folders/pf/8_rzvpr95ybbr5n31grmh4dc0000gp/T//RtmpijNNSy/file6dd2f3a8b70/rkdb_0.12.0.tar.gz’ had non-zero exit status

Does anyone have any suggestions?

Fix kdb types to R

Some atomic types might gain to be casted to character, like month, minutes, ...

kdb/q r implemented
boolean logical Y
byte integer Y
short integer Y
int integer Y
long integer N
real numeric Y
float numeric Y
char character Y
symbol character Y
timestamp POSIXct Y
month character N
date Date Y
datetime POSIXct Y
timespan character N
minute character N
second character N
time character N
enumeration factor N
table data.frame Y
keyed table data.frame Y
dictionary (mixted types) named list Y
dictionary (same types) named vector Y
function character Y
list (same types) vector Y
list (same 'complex' types) list Y
list (different types) list Y

Would you always expect R -> kdb -> R to be type-stable ?

Hello, say you have an object in R, set it to a kdb variable, and query this kdb variable again

R) h = open_connection(port=5000)
R) xR = someRObject
R) execute(h, '{[x] `xKdb set x}', xR)
R) yR = execute(h, 'xKdb')

would you expect str(xR) to be str(yR) ?
I think it should, if that's intended I'll add some more tests

By the way you could also test the same q -> R -> q stuff

Async calls from R

I see some mention of async calls.
Is it possible to query data asynchronously through R?

Thanks!

timestamp conversion not precise

I'm aware of the truth that it is not possible to convert a timestamp of q into POSIXct in R perfectly. It is still reasonable to request that: every R POSIXct converted to q timestamp and then converted back to R POSIXct should give the same value as original R POSIXct.

However, current rkdb has a problem with it. For example:

> Qtorigin <- ISOdatetime(2000,1,1,0,0,0,tz='UTC')
> x <- as.POSIXct(1534458275.511368513107,origin=ISOdatetime(1970,1,1,0,0,0,tz='UTC'))
> format(x,'%Y-%m-%d %H:%M:%OS6 %Z',tz='UTC')
[1] "2018-08-16 22:24:35.511368 UTC"
# xs is a string that should generate the closest Q timestamp
> xs <- sprintf('12h$%.0fj',1e9*as.numeric(x-Qtorigin,units='secs'))
> xs
[1] "12h$587773475511368576j"

In a q session,

q)12h$587773475511368576j
2018.08.16D22:24:35.511368576
> y <- execute(db,xs)                                   
#  It is the same to the maximum 6 decimal places that R shows
> format(y,'%Y-%m-%d %H:%M:%OS6 %Z',tz='UTC')
[1] "2018-08-16 22:24:35.511368 UTC"

#  It is close but it is not the same
> x == y
[1] FALSE
> y - x

This issue is caused by floating-point arithmetic in

rkdb/src/common.c

Lines 521 to 534 in cd36c3e

static SEXP from_timestamp_kobject(K x) {
SEXP result;
int i, length= x->n;
if(scalar(x)) {
PROTECT(result= NEW_NUMERIC(1));
NUMERIC_POINTER(result)[0]= x->j==nj?R_NaN:(946684800 + x->j / 1e9);
} else {
PROTECT(result= NEW_NUMERIC(length));
for(i= 0; i < length; i++)
NUMERIC_POINTER(result)[i]= kJ(x)[i]==nj?R_NaN:(946684800 + kJ(x)[i] / 1e9);
}
setdatetimeclass(result);
return result;
}

The problem is that the computation 946684800 + x->j / 1e9; "promotes" the long integer x->j to double precision in order to divide by 1e9. But a double precision mantissa has only 53 bits, and x->j is so large that it requires 59 bits (as of year 2018). So precision is lost.

I've come up with a fix by converting the integer number of seconds and nanoseconds separately. I would like to implement it in my fork of this project and send a pull request shortly.

R Date type not handled

Looks like R Date type is not recognised?

h <- open_connection('localhost', 5000)
execute(h,'{`tmp set x}',data.frame(a=c(1,2,3),b=c("a","b","b"))) # works as expected
execute(h,'{`tmp set x}',data.frame(a=c(1,2,3),b=Sys.Date()+0:2)) # fails

Unable to connect with TLS - something about an incompatible ssl version

If I try to open a connection I get OpenSSL initialisation error.

If I run sslinfo() I get

Error in sslinfo() : 
  kdb+ : Could not initialize openssl. Error was incompatible ssl version
.

I am using the latest version of openssl, according to the website linked from the KX ssl instructions.
OpenSSL 1.1.1l 24 Aug 2021

This same website tells me to punch myself in the face if I want to use an older version of openssl ;)

Windows install issue from file

When attempting to install on Windows from a downloaded version of this repository the use of INSTALL_opts=c("--no-multiarch") does not appear to be valid option.

This seems to limit the ability to install the file in this way in systems that do not have access to git.

How can I use rkdb in other packages ?

Hello I'd like to use rkdb in another package, actually what I am after is the R <-> kdb conversion functions.
Would you be kind enough to let me know how I can use the C functions from_any_kobject in a C++ code from another package ?
I found description in http://adv-r.had.co.nz/C-interface.html but that's a bit blurry to me.

Kind regards if you can help

Unable to install rkdb on windows 10

Hi, I tried to install rkdb in my windows 10 system. And found error as shown below. The same error has been reported for windows 7 before. I think I have made my R to run 64-bit already.

c:/Rtools/mingw_32/bin/gcc -shared -s -static-libgcc -o rkdb.dll tmp.def rkdb.o -Lw32 -le_static -lws2_32 -liphlpapi -LC:/PROGRA1/R/R-351.3/bin/i386 -lR
C:/Rtools/mingw_32/bin/../lib/gcc/i686-w64-mingw32/4.9.3/../../../../i686-w64-mingw32/bin/ld.exe: cannot find -le_static
collect2.exe: error: ld returned 1 exit status
no DLL was created
ERROR: compilation failed for package 'rkdb'

My R versions are below and I use 3.5 Rtools:

version
platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status
major 3
minor 5.3

Thank you so much for your help!

bit64 should be auto loaded with rkdb

R package bit64 provide support for 64 bit integer which is used in current version of rkdb. However, it is not auto loaded when we library(rkdb). It will be loaded when an execute call involve q timestamp because nanotime is loaded then.

This behavior can cause some problem.

> library(rkdb)
> h <- open_connection()
> execute(h, "1")
[1] 0
attr(,"class")
[1] "integer64"

The q long integer 1 was not transformed to R correctly.

We can explicitly load bit64.

> library(bit64)
> execute(h, "1")
integer64
[1] 1

Adding examples

I've been doing some tests:
You might find useful to add them in the README

Installation

# remove old package
if('qserver' %in% rownames(installed.packages())) remove.packages('qserver')
# install with devtools
require(devtools)
devtools::install_github('kxsystems/rkdb')
# load
library(rkdb)

The test function only works on port 5000, maybe allow to specify something else (this was taken for me)

test.rkdb()

First steps

Open a q server and connect to it

Open a qserver to test the installation

q -p 1234

Open a connection to it

h <- open_connection('localhost',1234) #this open a connection

First test:

execute(h, '1+1')
## [1] 2

Assigning a variable in q workspace:

execute(h, 'x:1+1') #assign x hopefully to 2
## [1] 0
execute(h, 'x') # get back the value
## [1] 2

Checks

Atoms from kdb to R

test <- function(h, x){
    cat(sprintf('From kdb %s comes back as :',x))
    str(execute(h, x))
}
test(h, '1b')
## From kdb 1b comes back as : logi TRUE
test(h, '1i')
## From kdb 1i comes back as : int 1
test(h, '1.5')
## From kdb 1.5 comes back as : num 1.5
test(h, '"a"')
## From kdb "a" comes back as : chr "a"
test(h, '`a')
## From kdb `a comes back as : chr "a"
test(h, '.z.d')
## From kdb .z.d comes back as : Date[1:1], format: "2017-06-15"
test(h, '.z.z')
## From kdb .z.z comes back as : POSIXt[1:1], format: "2017-06-15 09:54:35.65002"
test(h, '`a`b!(10 12)')
## From kdb `a`b!(10 12) comes back as : Named num [1:2] 10 12
##  - attr(*, "names")= chr [1:2] "a" "b"
test(h, '([] x:`a`b`c;y:3?1.)')
## From kdb ([] x:`a`b`c;y:3?1.) comes back as :'data.frame':   3 obs. of  2 variables:
##  $ x: chr  "a" "b" "c"
##  $ y: num  0.3915 0.0812 0.9368
test(h, '([x:`a`b`c]y:3?1.)')
## From kdb ([x:`a`b`c]y:3?1.) comes back as :'data.frame': 3 obs. of  2 variables:
##  $ x: chr  "a" "b" "c"
##  $ y: num  0.278 0.239 0.151

Bug when passing trivial data.frame frm R -> kdb

Hello I do not understand why the basic example bellow is failing

R> dput(DF2)
structure(list(date = structure(c(17897L, 17897L), class = "Date"), 
    sym = c("AAAA.L", "AAL.L"), delta_usd = c(-144930.612769693, 
    -152637.728273474), weight = c(31472.1659906769, 1e+06)), .Names = c("date",
"sym", "delta_usd", "weight"), row.names = 1:2, class = "data.frame")
R> dput(DF)
structure(list(x = c(1, 1), y = c(2, 2), sym = c("AAA.L", "AAA.L"
), date = structure(c(17897, 17897), class = "Date")), .Names = c("x", 
"y", "sym", "date"), row.names = c("1", "1.1"), class = "data.frame")
R>  execute(con, "meta", DF)
     c t f a
1    x f    
2    y f    
3  sym C    
4 date d    
R>  execute(con, "meta", DF2)
Error in execute(con, "meta", DF2) : 
  REAL() can only be applied to a 'numeric', not a 'integer'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.