Skip to content

Instantly share code, notes, and snippets.

@lmangani
Created July 28, 2023 07:47
Show Gist options
  • Save lmangani/e30edae31ad939e539989b8e326fb449 to your computer and use it in GitHub Desktop.
Save lmangani/e30edae31ad939e539989b8e326fb449 to your computer and use it in GitHub Desktop.
chdb R Binding (GPT)
#include <R.h>
#include <Rinternals.h>
// Function signature for the C function
char *execute_impl(char *query, char *format);
char *execute_impl(char *query, char *format) {
char *argv[] = {(char *)"clickhouse", (char *)"--multiquery", (char *)"--output-format=CSV", (char *)"--query="};
char dataFormat[100];
char *localQuery;
int argc = 4;
struct local_result *result;
snprintf(dataFormat, sizeof(dataFormat), "--format=%s", format);
argv[2] = strdup(dataFormat);
localQuery = (char *) malloc(strlen(query) + 10);
if (localQuery == NULL) {
return NULL;
}
sprintf(localQuery, "--query=%s", query);
argv[3] = strdup(localQuery);
free(localQuery);
// Assuming you have the `query_stable` function available, include its declaration here:
// struct local_result *query_stable(int argc, char *argv[]);
result = query_stable(argc, argv);
free(argv[2]);
free(argv[3]);
return result->buf;
}
static const R_CallMethodDef callMethods[] = {
{"execute_impl", (DL_FUNC)&execute_impl, 2},
{NULL, NULL, 0}
};
void R_init_myPackage(DllInfo *info) {
R_registerRoutines(info, NULL, callMethods, NULL, NULL);
R_useDynamicSymbols(info, FALSE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment