The R Project SVN R-packages

Rev

Rev 4160 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3255 mrmanese 1
#include "sqlite_dataframe.h"
3307 mrmanese 2
#include <math.h>
3
#include "Rmath.h"
3255 mrmanese 4
 
3358 mrmanese 5
/****************************************************************************
6
 * UTILITY FUNCTIONS
7
 ****************************************************************************/
3419 mrmanese 8
char *_create_svector1(SEXP name, const char *type, int * _namelen, int protect) {
3358 mrmanese 9
    int namelen, res;
3419 mrmanese 10
    char *iname = _create_sdf_skeleton1(name, &namelen, protect);
3255 mrmanese 11
 
3358 mrmanese 12
    if (iname == NULL) return NULL;
3255 mrmanese 13
 
3358 mrmanese 14
    sprintf(g_sql_buf[2], "create table [%s].sdf_data ([row name] text, "
15
            "V1 %s, primary key ([row name]))", iname, type);
16
    res = _sqlite_exec(g_sql_buf[2]);
17
    _sqlite_error(res);
3308 mrmanese 18
 
3358 mrmanese 19
    if (_namelen != NULL) *_namelen = namelen;
20
    return iname;
21
}
3255 mrmanese 22
 
3684 mrmanese 23
SEXP _create_svector_sexp(const char *iname, const char *tblname,
24
        const char *varname, const char *type) {
3358 mrmanese 25
    SEXP ret, value; int nprotected = 0;
3684 mrmanese 26
    PROTECT(ret = NEW_LIST(3)); nprotected++;
3255 mrmanese 27
 
28
    /* set list names */
3684 mrmanese 29
    PROTECT(value = NEW_CHARACTER(3)); nprotected++;
3255 mrmanese 30
    SET_STRING_ELT(value, 0, mkChar("iname"));
3684 mrmanese 31
    SET_STRING_ELT(value, 1, mkChar("tblname"));
32
    SET_STRING_ELT(value, 2, mkChar("varname"));
3255 mrmanese 33
    SET_NAMES(ret, value);
34
 
35
    /* set list values */
36
    SET_VECTOR_ELT(ret, 0, mkString(iname));
3684 mrmanese 37
    SET_VECTOR_ELT(ret, 1, mkString(tblname));
38
    SET_VECTOR_ELT(ret, 2, mkString(varname));
3255 mrmanese 39
 
3358 mrmanese 40
    /* set sexp class */
3456 mrmanese 41
    SET_CLASS(ret, mkString("sqlite.vector"));
3255 mrmanese 42
 
3456 mrmanese 43
    /* set sdf.vector.type */
44
    SET_SDFVECTORTYPE(ret, mkString(type));
45
 
3255 mrmanese 46
    UNPROTECT(nprotected);
3358 mrmanese 47
 
3255 mrmanese 48
    return ret;
49
}
50
 
4160 mrmanese 51
/* if ret == NULL, 3rd arg is the length of the vector to be created. otherwise
3358 mrmanese 52
 * it is the index in the vector ret where we will put the result extracted
53
 * from ret */
4160 mrmanese 54
int _get_vector_index_typed_result(sqlite3_stmt *stmt, SEXP *ret, int colidx,
55
        int idx_or_len, int *coltype) {
56
    int added = 1, ctype;
3255 mrmanese 57
    if (*ret == NULL || *ret == R_NilValue) {
4160 mrmanese 58
        if ((ctype = sqlite3_column_type(stmt, colidx)) == SQLITE_NULL) {
3255 mrmanese 59
            added = 0;
60
        }
61
 
4160 mrmanese 62
        if (ctype == SQLITE_TEXT) {
3255 mrmanese 63
            PROTECT(*ret = NEW_CHARACTER(idx_or_len));
64
            if (added) 
4160 mrmanese 65
                SET_STRING_ELT(*ret, 0, mkChar((char *)sqlite3_column_text(stmt, colidx)));
66
        } else if (ctype == SQLITE_FLOAT) {
3255 mrmanese 67
            PROTECT(*ret = NEW_NUMERIC(idx_or_len));
4160 mrmanese 68
            if (added) REAL(*ret)[0] = sqlite3_column_double(stmt, colidx);
69
        } else if (ctype == SQLITE_INTEGER) {
70
            /* sqlite does not differentiate b/w ints & bits, so we have to
71
             * do it ourselves since R distinguishes b/w ints & logicals */
72
            if (strcmp(sqlite3_column_decltype(stmt, colidx), "bit") == 0) {
73
                ctype = SQLITEDF_BIT; 
74
                PROTECT(*ret = NEW_LOGICAL(idx_or_len));
75
                if (added) LOGICAL(*ret)[0] = sqlite3_column_int(stmt, colidx);
76
            } else {
77
                PROTECT(*ret = NEW_INTEGER(idx_or_len));
78
                if (added) INTEGER(*ret)[0] = sqlite3_column_int(stmt, colidx);
79
            }
3255 mrmanese 80
        } else added = 0;
81
 
4160 mrmanese 82
        if (added) UNPROTECT(1);
83
        *coltype = ctype;
3684 mrmanese 84
    } else if (stmt != NULL) {
4160 mrmanese 85
        int ctype = *coltype;
86
 
4590 mrmanese 87
        /* first row/previous rows must have been NULL so that *coltype 
88
         * was not set */
4160 mrmanese 89
        if (ctype == SQLITE_NULL) {
90
            ctype = *coltype = sqlite3_column_type(stmt, colidx);
91
            /* distinguish int or logical */
92
            if (ctype == SQLITE_INTEGER) 
93
                ctype = (strcmp(sqlite3_column_decltype(stmt, colidx), "bit") == 0) ?
94
                    SQLITEDF_BIT : SQLITE_INTEGER;
95
        }
96
 
97
        if (sqlite3_column_type(stmt, colidx) == SQLITE_NULL) {
3255 mrmanese 98
            added = 0;
4160 mrmanese 99
        } else if (ctype == SQLITE_TEXT) {
3255 mrmanese 100
            SET_STRING_ELT(*ret, idx_or_len, 
4160 mrmanese 101
                    mkChar((char *)sqlite3_column_text(stmt, colidx)));
102
        } else if (ctype == SQLITE_FLOAT) {
103
            REAL(*ret)[idx_or_len] = sqlite3_column_double(stmt, colidx);
104
        } else if (ctype == SQLITE_INTEGER) {
105
            INTEGER(*ret)[idx_or_len] = sqlite3_column_int(stmt, colidx);
106
        } else if (ctype == SQLITEDF_BIT) {
107
            LOGICAL(*ret)[idx_or_len] = sqlite3_column_int(stmt, colidx);
3255 mrmanese 108
        } else added = 0;
3684 mrmanese 109
    } else if (stmt == NULL) {
4160 mrmanese 110
        /* used when there is no row returned (sqlite3_step(stmt)!= QLITE_ROW),
111
         * and we just insert NAs in the result */
112
        ctype = *coltype;
113
        if (ctype == SQLITE_NULL) { 
114
            /* since stmt == NULL, we can't check its column type */
3684 mrmanese 115
            added = 0;
4160 mrmanese 116
        } else if (ctype == SQLITE_TEXT) {
3684 mrmanese 117
            SET_STRING_ELT(*ret, idx_or_len, NA_STRING);
4160 mrmanese 118
        } else if (ctype == SQLITE_FLOAT) {
3684 mrmanese 119
            REAL(*ret)[idx_or_len] = NA_REAL;
4160 mrmanese 120
        } else if (ctype == SQLITE_INTEGER) {
3684 mrmanese 121
            INTEGER(*ret)[idx_or_len] = NA_INTEGER;
4160 mrmanese 122
        } else if (ctype == SQLITEDF_BIT) {
123
            LOGICAL(*ret)[idx_or_len] = NA_LOGICAL;
3684 mrmanese 124
        } else added = 0;
3255 mrmanese 125
    }
126
 
127
    return added;
128
}
129
 
3358 mrmanese 130
/****************************************************************************
131
 * SVEC FUNCTIONS
132
 ****************************************************************************/
133
SEXP sdf_get_variable(SEXP sdf, SEXP name) {
3456 mrmanese 134
    char *iname, *varname, *svec_type = NULL;
135
    const char *coltype;
136
    int type = -1, res, nprotected = 0;
137
    SEXP ret, value;
138
 
3358 mrmanese 139
    if (!IS_CHARACTER(name)) {
3471 mrmanese 140
        error("argument is not a string.\n");
3358 mrmanese 141
    }
142
 
3456 mrmanese 143
    iname = SDF_INAME(sdf);
144
    varname = CHAR_ELT(name, 0);
3358 mrmanese 145
 
3419 mrmanese 146
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
3358 mrmanese 147
 
148
    /* check if sdf & varname w/in that sdf exists */
149
    sqlite3_stmt *stmt;
150
    sprintf(g_sql_buf[0], "select [%s] from [%s].sdf_data", varname, iname);
151
 
3456 mrmanese 152
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
3358 mrmanese 153
 
154
    if (_sqlite_error(res)) return R_NilValue;
155
 
3456 mrmanese 156
    coltype = sqlite3_column_decltype(stmt, 0);
3358 mrmanese 157
    sqlite3_finalize(stmt);
3456 mrmanese 158
 
3684 mrmanese 159
    PROTECT(ret = NEW_LIST(3)); nprotected++;
3358 mrmanese 160
 
161
    /* set list names */
3684 mrmanese 162
    PROTECT(value = NEW_CHARACTER(3)); nprotected++;
3358 mrmanese 163
    SET_STRING_ELT(value, 0, mkChar("iname"));
3684 mrmanese 164
    SET_STRING_ELT(value, 1, mkChar("tblname"));
165
    SET_STRING_ELT(value, 2, mkChar("varname"));
3358 mrmanese 166
    SET_NAMES(ret, value);
167
 
168
    /* set list values */
169
    SET_VECTOR_ELT(ret, 0, mkString(iname));
3684 mrmanese 170
    SET_VECTOR_ELT(ret, 1, mkString("sdf_data"));
171
    SET_VECTOR_ELT(ret, 2, mkString(varname));
3358 mrmanese 172
 
173
    /* set class */
3456 mrmanese 174
    if (strcmp(coltype, "text") == 0) svec_type = "character";
175
    else if (strcmp(coltype, "double") == 0) svec_type = "numeric";
176
    else if (strcmp(coltype, "bit") == 0) svec_type = "logical";
3358 mrmanese 177
    else if (strcmp(coltype, "integer") == 0 || strcmp(coltype, "int") == 0) {
178
        /* determine if int, factor or ordered */
4160 mrmanese 179
        type = _get_factor_levels1(iname, varname, ret, FALSE);
3358 mrmanese 180
        switch(type) {
3456 mrmanese 181
            case VAR_INTEGER: svec_type = "integer"; break;
182
            case VAR_FACTOR: svec_type = "factor"; break;
183
            case VAR_ORDERED: svec_type = "ordered";
3358 mrmanese 184
        }
185
    }
186
 
3456 mrmanese 187
    SET_CLASS(ret, mkString("sqlite.vector"));
188
    SET_SDFVECTORTYPE(ret, mkString(svec_type));
3358 mrmanese 189
 
190
    UNPROTECT(nprotected);
191
    return ret;
192
 
193
}
194
 
195
 
3255 mrmanese 196
SEXP sdf_get_variable_length(SEXP svec) {
197
    char *iname = SDF_INAME(svec);
3419 mrmanese 198
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
3255 mrmanese 199
 
3446 mrmanese 200
    return ScalarInteger(_get_row_count2(iname, 1));
3255 mrmanese 201
}
202
 
203
 
204
SEXP sdf_get_variable_index(SEXP svec, SEXP idx) {
205
    SEXP ret = R_NilValue, tmp;
3684 mrmanese 206
    char *iname = SDF_INAME(svec), *tblname = SVEC_TBLNAME(svec),
207
         *varname = SVEC_VARNAME(svec);
4590 mrmanese 208
    int *index, _idx, nrows, i, init=FALSE, retlen=0, res, coltype;
3358 mrmanese 209
    sqlite3_stmt *stmt;
3255 mrmanese 210
 
3419 mrmanese 211
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
3308 mrmanese 212
 
4590 mrmanese 213
    /* get # of rows in svec */
214
    nrows = _get_row_count2(iname, TRUE);
215
    if (nrows < 1) return ret;
3255 mrmanese 216
 
4160 mrmanese 217
    sprintf(g_sql_buf[0], "select [%s] from [%s].[%s] where rowid=?",
3684 mrmanese 218
            varname, iname, tblname);
3255 mrmanese 219
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
3684 mrmanese 220
    if (_sqlite_error(res)) error("cannot complete request");
3255 mrmanese 221
 
4590 mrmanese 222
    index = _make_row_index(idx, &nrows);
3255 mrmanese 223
 
4590 mrmanese 224
    for (i = 0; i < nrows; i++) {
225
        _idx = index[i];
226
        if (_idx < 1 || _idx == NA_INTEGER) continue;
227
        sqlite3_reset(stmt);
228
        sqlite3_bind_int(stmt, 1, _idx);
229
        res = sqlite3_step(stmt);
230
        if (!init) {
3255 mrmanese 231
            if (res == SQLITE_ROW) { 
4590 mrmanese 232
                /* valid 1st row. initialize the returned vector with length
233
                 * nrows. retlen holds the length of vector ret. 
234
                 */ 
235
                retlen = _get_vector_index_typed_result(stmt, &ret, 0, nrows, &coltype);
236
            } else {
237
                /* invalid 1st row, initalize anyway but now with length 
238
                 * nrows-1. we put dummy results in 1st element of vector which
239
                 * should be overwritten in succeeding calls
4160 mrmanese 240
                 */
3255 mrmanese 241
                sqlite3_reset(stmt);
4590 mrmanese 242
                sqlite3_bind_int(stmt, 1, 0);  /* get 1st row */
3255 mrmanese 243
                res = sqlite3_step(stmt);
4590 mrmanese 244
                _get_vector_index_typed_result(stmt, &ret, 0, nrows - 1, &coltype);
245
                retlen = 0;
3255 mrmanese 246
            }
4590 mrmanese 247
            init = TRUE;
248
        } else {
249
            retlen += _get_vector_index_typed_result((res == SQLITE_ROW) ? stmt : NULL,
250
                            &ret, 0, retlen, &coltype);
3255 mrmanese 251
        }
252
    }
253
 
254
    sqlite3_finalize(stmt);
255
 
256
    if (ret != R_NilValue) {
257
        ret = _shrink_vector(ret, retlen);
258
        tmp = GET_LEVELS(svec);
259
        if (tmp != R_NilValue) {
260
            SET_LEVELS(ret, duplicate(tmp));
3456 mrmanese 261
            if (TEST_SDFVECTORTYPE(svec, "factor")) {
3255 mrmanese 262
                SET_CLASS(ret, mkString("factor"));
263
            } else {
264
                PROTECT(tmp = NEW_CHARACTER(2));
265
                SET_STRING_ELT(tmp, 0, mkChar("ordered"));
266
                SET_STRING_ELT(tmp, 1, mkChar("factor"));
267
                UNPROTECT(1);
268
            }
269
        }
270
    }
271
 
272
    return ret;
273
}
274
 
3358 mrmanese 275
/* sqlite.vector.[<- */
276
SEXP sdf_set_variable_index(SEXP svec, SEXP idx, SEXP value) {
4590 mrmanese 277
    int *index;
278
    int coltype, valtype, idx_len, val_len, svec_len, i, i2, res;
4160 mrmanese 279
    char *iname = SDF_INAME(svec), *tblname = SVEC_TBLNAME(svec),
280
         *varname = SVEC_VARNAME(svec);
4590 mrmanese 281
    const char *decltype;
4160 mrmanese 282
    sqlite3_stmt *stmt;
3358 mrmanese 283
 
284
 
4590 mrmanese 285
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
286
 
287
    /* find the type of the svec */
288
    sprintf(g_sql_buf[0], "select [%s] from [%s].[%s] where not [%s] is null limit 1", 
289
                varname, iname, tblname, varname);
290
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
291
    if (_sqlite_error(res)) error("cannot complete request");
292
    sqlite3_step(stmt);
293
    decltype = sqlite3_column_decltype(stmt, 0);
294
    Rprintf("decltype: %s\n", decltype); 
295
    coltype = _get_r_type(decltype);
296
    sqlite3_finalize(stmt);
297
 
298
    /* get index */
3358 mrmanese 299
    val_len = LENGTH(value);
4590 mrmanese 300
    idx_len = svec_len = _get_row_count2(iname, TRUE);
301
    /*Rprintf("idx_len: %d\n", idx_len);*/
302
    index = _make_row_index(idx, &idx_len);
4160 mrmanese 303
    if (idx_len % val_len != 0) 
304
        warning("number of items to replace is not a multiple of replacement length");
305
 
4590 mrmanese 306
    /* find the type of value to be assigned */
307
    valtype = TYPEOF(value);
308
 
309
    /* we will use the style similar to R's */
310
    coltype = coltype * 100 + valtype;
311
 
4160 mrmanese 312
    sprintf(g_sql_buf[0], "update [%s].[%s] set [%s]=? where rowid=?",
313
            iname, tblname, varname);
314
    _sqlite_begin;
315
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
4590 mrmanese 316
    if (_sqlite_error(res)) error("cannot complete request");
317
    /*Rprintf("idx_len: %d\n", idx_len);*/
4160 mrmanese 318
 
4590 mrmanese 319
    Rprintf("coltype: %d\n", coltype);
320
    switch (coltype) {
321
        case 1010: /* logical <- logical */
322
        case 1310: /* int <- logical */
323
        case 1313: /* int <- int */
324
            /* TODO: check if factor */
325
            for (i = 0; i < idx_len; i++) { 
326
                sqlite3_reset(stmt);
327
                i2 = i % val_len;
328
                sqlite3_bind_int(stmt, 1, INTEGER(value)[i2]);
329
                sqlite3_bind_int(stmt, 2, index[i]);
330
                sqlite3_step(stmt);
331
            }
332
            break;
333
        case 1410: /* real <- logical */
334
        case 1413: /* real <- int */
335
            for (i = 0; i < idx_len; i++) { 
336
                sqlite3_reset(stmt);
337
                i2 = i % val_len;
338
                sqlite3_bind_double(stmt, 1, (double) INTEGER(value)[i2]);
339
                sqlite3_bind_int(stmt, 2, index[i]);
340
                sqlite3_step(stmt);
341
            }
342
            break;
343
        case 1414: /* real <- real */
344
            for (i = 0; i < idx_len; i++) { 
345
                sqlite3_reset(stmt);
346
                i2 = i % val_len;
347
                sqlite3_bind_double(stmt, 1, REAL(value)[i2]);
348
                sqlite3_bind_int(stmt, 2, index[i]);
349
                res = sqlite3_step(stmt);
350
            }
351
            break;
352
        case 1610: /* character <- logical */
353
            for (i = 0; i < idx_len; i++) { 
354
                sqlite3_reset(stmt);
355
                i2 = i % val_len;
356
                sqlite3_bind_text(stmt, 1, 
357
                        (INTEGER(value)[i2]) ? "TRUE" : "FALSE", -1,
358
                        SQLITE_STATIC);
359
                sqlite3_bind_int(stmt, 2, index[i]);
360
                sqlite3_step(stmt);
361
            }
362
            break;
363
        case 1613: /* character <- integer */
364
            /* value could be a factor! */
365
            for (i = 0; i < idx_len; i++) { 
366
                sqlite3_reset(stmt);
367
                i2 = i % val_len;
368
                res = sprintf(g_sql_buf[1], "%d", INTEGER(value)[i2], 
369
                        SQLITE_STATIC);
370
                sqlite3_bind_text(stmt, 1, g_sql_buf[1], res, SQLITE_STATIC);
371
                sqlite3_bind_int(stmt, 2, index[i]);
372
                sqlite3_step(stmt);
373
            }
374
            break;
375
        case 1614: { /* character <- real */
376
            SEXP chvalue;
377
            PROTECT(chvalue = AS_CHARACTER(value));
378
            for (i = 0; i < idx_len; i++) { 
379
                sqlite3_reset(stmt);
380
                i2 = i % val_len;
381
                sqlite3_bind_text(stmt, 1, CHAR(STRING_ELT(chvalue, i2)), 
382
                        -1, SQLITE_STATIC);
383
                sqlite3_bind_int(stmt, 2, index[i]);
384
                sqlite3_step(stmt);
385
            }
386
            UNPROTECT(1);
387
          }
388
            break;
389
        case 1616: /* character <- character */
390
            for (i = 0; i < idx_len; i++) { 
391
                sqlite3_reset(stmt);
392
                i2 = i % val_len;
393
                sqlite3_bind_text(stmt, 1, CHAR(STRING_ELT(value, i2)), 
394
                        -1, SQLITE_STATIC);
395
                sqlite3_bind_int(stmt, 2, index[i]);
396
                sqlite3_step(stmt);
397
            }
398
            break;
399
        case 1314: /* int <- real */
400
            _sqlite_commit;
401
            error("cannot promote vector from integer to real");
402
        case 1014: /* logical <- real */
403
            _sqlite_commit;
404
            error("cannot promote vector from logical to real");
405
        case 1013: /* logical <- int */
406
            _sqlite_commit;
407
            error("cannot promote vector from logical to integer");
408
        case 1016: /* logical <- character */
409
            _sqlite_commit;
410
            error("cannot promote vector from logical to character");
411
        case 1316: /* integer <- character */
412
            /* TODO: this could be factors */
413
            _sqlite_commit;
414
            error("cannot promote vector from integer to character");
415
        case 1416: /* real <- character */
416
            _sqlite_commit;
417
            error("cannot promote vector from real to character");
418
        default:
419
            _sqlite_commit;
420
            error("don't know what to do with coltype=%d", coltype);
3358 mrmanese 421
    }
4590 mrmanese 422
    _sqlite_commit;
3358 mrmanese 423
 
4590 mrmanese 424
    return svec;
3358 mrmanese 425
}
426
 
3419 mrmanese 427
SEXP sdf_variable_summary(SEXP svec, SEXP maxsum) {
3684 mrmanese 428
    char *iname, *tblname, *varname, *type;
3419 mrmanese 429
    sqlite3_stmt *stmt;
430
    int nprotected = 0;
431
    SEXP ret, names;
432
 
433
    iname = SDF_INAME(svec);
3684 mrmanese 434
    tblname = SVEC_TBLNAME(svec);
3419 mrmanese 435
    varname = SVEC_VARNAME(svec);
436
    USE_SDF1(iname, TRUE, FALSE);
437
 
3456 mrmanese 438
    if ((TEST_SDFVECTORTYPE(svec, "ordered") && ((type = "ordered"))) ||
439
            (TEST_SDFVECTORTYPE(svec, "factor") && ((type = "factor")))) {
3419 mrmanese 440
        int nrows, i, max_rows = INTEGER(maxsum)[0];
441
 
442
 
443
        sprintf(g_sql_buf[0], "[%s].[%s %s]", iname, type, varname);
444
        nrows = _get_row_count2(g_sql_buf[0], FALSE);
445
        if (nrows <= max_rows) max_rows = nrows;
446
        else { nrows = max_rows; max_rows--; }
447
 
448
        PROTECT(ret = NEW_INTEGER(nrows)); nprotected = 1;
449
        PROTECT(names = NEW_CHARACTER(nrows)); nprotected++;
450
 
451
        sprintf(g_sql_buf[0], "select [%s].[%s %s].label, count(*) from "
3684 mrmanese 452
                "[%s].[%s] join [%s].[%s %s] on [%s].[%s].[%s]=[%s].[%s %s].level "
453
                "group by [%s].[%s].[%s], [%s].[%s %s].level order by count(*) desc",
454
                iname, type, varname, iname, tblname, iname, type, varname, iname, tblname, varname,
455
                iname, type, varname, iname, tblname, varname, iname, type, varname);
3419 mrmanese 456
        sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
457
 
458
        for (i = 0; i < max_rows; i++) {
459
            sqlite3_step(stmt);
460
            SET_STRING_ELT(names, i, mkChar((char *)sqlite3_column_text(stmt, 0)));
461
            INTEGER(ret)[i] = sqlite3_column_int(stmt, 1);
462
        }
463
 
464
        if (nrows > max_rows) {
465
            int others_sum = 0;
466
            SET_STRING_ELT(names, nrows-1, mkChar("(Others)"));
467
            while (sqlite3_step(stmt) == SQLITE_ROW) {
468
                others_sum += sqlite3_column_int(stmt, 1);
469
            }
470
            INTEGER(ret)[nrows-1] = others_sum;
471
        }
3456 mrmanese 472
    } else if (TEST_SDFVECTORTYPE(svec, "logical")) {
3419 mrmanese 473
        sprintf(g_sql_buf[0], "select count(*) from "
3684 mrmanese 474
                "[%s].[%s] group by [%s] order by [%s]", iname, tblname, varname, varname);
3419 mrmanese 475
 
476
        PROTECT(names = NEW_CHARACTER(3)); nprotected = 1;
477
        PROTECT(ret = NEW_CHARACTER(3)); nprotected++;
478
 
479
        SET_STRING_ELT(names, 0, mkChar("Mode"));
480
        SET_STRING_ELT(names, 1, mkChar("FALSE"));
481
        SET_STRING_ELT(names, 2, mkChar("TRUE"));
482
 
483
        sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
484
        SET_STRING_ELT(ret, 0, mkChar("logical"));
485
        sqlite3_step(stmt);
486
        SET_STRING_ELT(ret, 1, mkChar((const char *)sqlite3_column_text(stmt, 0)));
487
        sqlite3_step(stmt);
488
        SET_STRING_ELT(ret, 2, mkChar((const char *)sqlite3_column_text(stmt, 0)));
489
 
490
    } else return R_NilValue;
491
 
492
    sqlite3_finalize(stmt);
493
    SET_NAMES(ret, names);
494
    SET_CLASS(ret, mkString("table"));
495
    UNPROTECT(nprotected);
496
    return ret;
497
}
498
 
499
 
3324 mrmanese 500
/* the global accumulator should be safe if we only do 1 cummulative or
501
 * aggregate at any time. it won't work for stuffs like "select max(col)-min(col)
502
 * from sdf_data". */
3432 mrmanese 503
static long double g_accumulator = 0.0; /* accumulator var for cumsum, cumprod, etc. */
3324 mrmanese 504
static int g_start = 0;            /* flag for start of cummulation */
505
static int g_narm = 0;             /* for Summary group */
3255 mrmanese 506
 
3473 mrmanese 507
void _init_sqlite_function_accumulator() {
508
    g_accumulator = 0.0;   /* initialize accumulator */
509
    g_start = 1;           /* flag that we are at start of accumulating */
510
}
511
 
3434 mrmanese 512
SEXP sdf_do_variable_math(SEXP func, SEXP vector, SEXP other_args) {
3307 mrmanese 513
    char *iname, *iname_src, *varname_src, *funcname;
3434 mrmanese 514
    int namelen, res;
3307 mrmanese 515
    sqlite3_stmt *stmt;
3255 mrmanese 516
 
3307 mrmanese 517
    /* get data from arguments (function name and sqlite.vector stuffs) */
518
    funcname = CHAR_ELT(func, 0);
519
    iname_src = SDF_INAME(vector);
520
    varname_src = SVEC_VARNAME(vector);
3255 mrmanese 521
 
3419 mrmanese 522
    if (!USE_SDF1(iname_src, TRUE, TRUE)) return R_NilValue;
3308 mrmanese 523
 
3307 mrmanese 524
    /* create a new sdf, with 1 column named V1 */
3419 mrmanese 525
    iname = _create_svector1(R_NilValue, "double", &namelen, TRUE);
3307 mrmanese 526
 
527
    /* insert into <newsdf>.col, row.names select func(col), rownames */
3434 mrmanese 528
    if (strcmp(funcname, "round") == 0 || strcmp(funcname, "signif") == 0) {
529
        double digits = REAL(_getListElement(other_args, "digits"))[0];
530
        sprintf(g_sql_buf[0], "insert into [%s].sdf_data([row name], V1) "
531
                "select [row name], %s([%s],?) from [%s].sdf_data", iname, funcname,
532
                varname_src, iname_src);
533
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0); 
534
        if (_sqlite_error(res)) goto vecmath_prepare_error;
535
        res = sqlite3_bind_double(stmt, 1, digits);
536
    } else if (strcmp(funcname, "log") == 0) {
537
        double base = REAL(_getListElement(other_args, "base"))[0];
538
        sprintf(g_sql_buf[0], "insert into [%s].sdf_data([row name], V1) "
539
                "select [row name], %s([%s],?) from [%s].sdf_data", iname, funcname,
540
                varname_src, iname_src);
541
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0); 
542
        if (_sqlite_error(res)) goto vecmath_prepare_error;
543
        res = sqlite3_bind_double(stmt, 1, base);
544
    } else {
545
        sprintf(g_sql_buf[0], "insert into [%s].sdf_data([row name], V1) "
546
                "select [row name], %s([%s]) from [%s].sdf_data", iname, funcname,
547
                varname_src, iname_src);
548
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0); 
549
    }
3307 mrmanese 550
 
551
    if (_sqlite_error(res)) {
3434 mrmanese 552
vecmath_prepare_error:
3307 mrmanese 553
        sprintf(g_sql_buf[0], "detach %s", iname);
554
        _sqlite_exec(g_sql_buf[0]);
555
 
556
        /* we will return a string with the file name, and do file.remove
557
         * at R */
558
        iname[namelen] = '.';
559
        return mkString(iname);
560
    }
561
 
3473 mrmanese 562
    _init_sqlite_function_accumulator();
3307 mrmanese 563
    sqlite3_step(stmt);
564
    sqlite3_finalize(stmt);
565
 
3419 mrmanese 566
    UNUSE_SDF2(iname);
567
    UNUSE_SDF2(iname_src);
568
 
3684 mrmanese 569
    return _create_svector_sexp(iname, "sdf_data", "V1", "numeric");
3358 mrmanese 570
}
3307 mrmanese 571
 
3434 mrmanese 572
SEXP sdf_do_variable_op(SEXP func, SEXP vector, SEXP op2, SEXP arg_reversed) {
3358 mrmanese 573
    char *iname = NULL, *iname_src, *varname_src, *funcname;
3434 mrmanese 574
    int res, functype = -1, op2_len, svec_len, i, reversed;
3358 mrmanese 575
    sqlite3_stmt *stmt, *stmt2;
3307 mrmanese 576
 
3358 mrmanese 577
    /* get data from arguments (function name and sqlite.vector stuffs) */
578
    funcname = CHAR_ELT(func, 0);
579
    iname_src = SDF_INAME(vector);
580
    varname_src = SVEC_VARNAME(vector);
3434 mrmanese 581
    reversed = LOGICAL(arg_reversed)[0];
3307 mrmanese 582
 
3419 mrmanese 583
    if (!USE_SDF1(iname_src, TRUE, TRUE)) return R_NilValue;
3358 mrmanese 584
    switch(funcname[0]) {
585
        case '+' :
586
        case '-' :
587
        case '*' :
588
        case '/' :
589
        case '^' :
590
        case '%' : /* %% and %/% */ 
591
            functype = 0; break;  /* output is REAL */
592
        case '&' :
593
        case '|' :
594
        case '!' :  /* ! and != */
595
            if (funcname[1] == 0) { functype = 1; break; }
596
        case '=' :  /* == */
597
        case '<' :  /* < and <= */
598
        case '>' :  /* > and >= */
599
            functype = 2; 
600
    }
3307 mrmanese 601
 
3358 mrmanese 602
    svec_len = _get_row_count2(iname_src, 1);
603
    if (functype == 0 || functype == 2 || (functype == 1 && funcname[0] != '!')) {
604
        char *insert_fmt_string1c, *insert_fmt_string1s;
605
        char *insert_fmt_string2c, *insert_fmt_string2s;
3434 mrmanese 606
        int vec_idx, sdf_idx;
607
 
3419 mrmanese 608
        iname = _create_svector1(R_NilValue, (functype == 0) ? "double" : "bit", NULL, TRUE);
3307 mrmanese 609
 
3434 mrmanese 610
        /* insert_fmt_string prefixes:
611
         * 1 - op2 is an ordinary vector, op2_len = 1, straightforward insert-select
612
         * 2 - op2 is an ordinary vector, op2_len > 1, may need recycling, loop both sides
613
         * s - operator will be printed as string
614
         * c - operator is either INTDIV or RMOD (int division, R modulo). initially, I
615
         *     thought I could cast both op to int then use / and % of sqlite, which is just
616
         *     the 2nd character of the funcname. however, R's INTDIV and casts to int 
617
         *     after division (e.g. 3.5 %/% 1.5 == 2). RMOD operates on doubles too,
618
         *     which is the remainder of the largest int multiple of "divisor"
619
         *     (e.g. 3.5 %% 1.5 = 0.5)
620
         */ 
3358 mrmanese 621
        if (functype == 1) { /* boolean binary operators */
3434 mrmanese 622
            if (!reversed) {
623
                insert_fmt_string1s = "insert into [%s].sdf_data "
624
                            "select [row name], ([%s] != 0) %s (? != 0) from [%s].sdf_data";
625
            } else {
626
                insert_fmt_string1s = "insert into [%s].sdf_data "
627
                            "select [row name], (? != 0) %s ([%s] != 0) from [%s].sdf_data";
628
            }
3358 mrmanese 629
            insert_fmt_string2s = "insert into [%s].sdf_data values(?, (? != 0) %s (? != 0))";
630
            /* no need for char version of fmt_string2, since %% only occurs for functype==0 */
631
            insert_fmt_string1c = insert_fmt_string1s;
632
            insert_fmt_string2c = insert_fmt_string2s;
633
        } else {
3434 mrmanese 634
            if (!reversed) {
635
                insert_fmt_string1s = "insert into [%s].sdf_data "
636
                            "select [row name], [%s] %s ? from [%s].sdf_data";
637
                insert_fmt_string1c = "insert into [%s].sdf_data "
638
                            "select [row name], %s([%s],?) from [%s].sdf_data";
639
            } else {
640
                insert_fmt_string1s = "insert into [%s].sdf_data "
641
                            "select [row name], ? %s [%s] from [%s].sdf_data";
642
                insert_fmt_string1c = "insert into [%s].sdf_data "
643
                            "select [row name], %s(?,[%s]) from [%s].sdf_data";
644
            }
3358 mrmanese 645
            /* fmt_string2c format operator from a char, used for %% and %/% */
3434 mrmanese 646
            insert_fmt_string2c = "insert into [%s].sdf_data values(?, %s(?,?))";
3358 mrmanese 647
            insert_fmt_string2s = "insert into [%s].sdf_data values(?, ? %s ?)";
648
        }
3307 mrmanese 649
 
3434 mrmanese 650
        /* for 2* insert statements, the values below specifies the index of bind().
651
         * if !reversed, then e1 is svec, e2 is anything. otherwise, e2 is the svec */
652
        if (reversed) { sdf_idx = 3; vec_idx = 2; }
653
        else { sdf_idx = 2; vec_idx = 3; }
3358 mrmanese 654
 
655
        if (IS_NUMERIC(op2)) {
656
            op2_len = LENGTH(op2);
657
 
658
            if (op2_len == 1) {
659
                if (funcname[0] == '%') {
660
                    sprintf(g_sql_buf[2], insert_fmt_string1c, iname, 
3434 mrmanese 661
                            (funcname[1] == '%') ? "r_mod" : "r_intdiv", varname_src, iname_src);
3358 mrmanese 662
                } else {
663
                    sprintf(g_sql_buf[2], insert_fmt_string1s, iname,
664
                            varname_src, funcname, iname_src);
665
                }
666
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
667
                _sqlite_error(res);
668
                sqlite3_bind_double(stmt, 1, REAL(op2)[0]);
669
                sqlite3_step(stmt);
3419 mrmanese 670
                sqlite3_finalize(stmt);
3358 mrmanese 671
            } else if (op2_len <= svec_len) {  /* recycle op2 */
672
                sprintf(g_sql_buf[2], "select [row name], [%s] from [%s].sdf_data",
673
                        varname_src, iname_src);
674
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
675
                _sqlite_error(res);
676
 
677
                if (funcname[0] == '%') {
3434 mrmanese 678
                    sprintf(g_sql_buf[2], insert_fmt_string2c, iname, 
679
                            (funcname[1] == '%') ? "r_mod" : "r_intdiv");  
3419 mrmanese 680
                    _sqlite_begin;
3358 mrmanese 681
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
682
                    _sqlite_error(res);
683
 
684
                    for (i = 0; i < svec_len; i++) {
685
                        sqlite3_step(stmt2);
686
 
687
                        sqlite3_reset(stmt);
688
                        sqlite3_bind_text(stmt, 1, (char *)sqlite3_column_text(stmt2, 0), -1, SQLITE_STATIC);
3434 mrmanese 689
                        sqlite3_bind_int(stmt, sdf_idx, (int)sqlite3_column_double(stmt2, 1));
690
                        sqlite3_bind_int(stmt, vec_idx, (int)REAL(op2)[i % op2_len]);
3358 mrmanese 691
                        sqlite3_step(stmt);
692
                    }
3419 mrmanese 693
                    sqlite3_finalize(stmt);
694
                    sqlite3_finalize(stmt2);
695
                    _sqlite_commit;
3358 mrmanese 696
                } else { /* non-integer binary operation */
697
                    sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
3419 mrmanese 698
                    _sqlite_begin;
3358 mrmanese 699
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
700
                    _sqlite_error(res);
701
 
702
                    for (i = 0; i < svec_len; i++) {
703
                        sqlite3_step(stmt2);
704
 
705
                        sqlite3_reset(stmt);
706
                        sqlite3_bind_text(stmt, 1, (char *)sqlite3_column_text(stmt2, 0), -1, SQLITE_STATIC);
3434 mrmanese 707
                        sqlite3_bind_double(stmt, sdf_idx, sqlite3_column_double(stmt2, 1));
708
                        sqlite3_bind_double(stmt, vec_idx, REAL(op2)[i % op2_len]);
3358 mrmanese 709
                        sqlite3_step(stmt);
710
                    }
3419 mrmanese 711
                    sqlite3_finalize(stmt);
712
                    sqlite3_finalize(stmt2);
713
                    _sqlite_commit;
3358 mrmanese 714
                }
715
            } else { /* op2_len > svec_len, recycle svec */
716
                sprintf(g_sql_buf[1], "select [%s] from [%s].sdf_data",
717
                        varname_src, iname_src);
718
 
719
                if (funcname[0] == '%') {
3434 mrmanese 720
                    sprintf(g_sql_buf[2], insert_fmt_string2c, iname, 
721
                            (funcname[1] == '%') ? "r_mod" : "r_intdiv");
3358 mrmanese 722
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
723
                    _sqlite_error(res);
724
                } else {
725
                    sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
726
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
727
                    _sqlite_error(res);
728
                }
3434 mrmanese 729
 
3419 mrmanese 730
                i = 0; _sqlite_begin;
3358 mrmanese 731
                while (i < op2_len) {
732
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
733
                    _sqlite_error(res);
734
 
735
                    if (funcname[0] == '%') {
736
                        for ( ; i < op2_len && sqlite3_step(stmt) == SQLITE_ROW ; i++) {
737
                            sqlite3_step(stmt2);
738
 
739
                            sqlite3_reset(stmt);
740
                            /* simplify my life, just use ints for row names so that we
741
                             * don't have to worry about duplicates */
742
                            sqlite3_bind_int(stmt, 1, i); 
3434 mrmanese 743
                            sqlite3_bind_int(stmt, sdf_idx, sqlite3_column_int(stmt2, 1));
744
                            sqlite3_bind_int(stmt, vec_idx, (int)REAL(op2)[i % op2_len]);
3358 mrmanese 745
                            sqlite3_step(stmt);
746
                        }
747
                    } else {
748
                        for ( ; i < op2_len && sqlite3_step(stmt) == SQLITE_ROW ; i++) {
749
                            sqlite3_step(stmt2);
750
 
751
                            sqlite3_reset(stmt);
752
                            sqlite3_bind_int(stmt, 1, i);
3434 mrmanese 753
                            sqlite3_bind_double(stmt, sdf_idx, sqlite3_column_double(stmt2, 1));
754
                            sqlite3_bind_double(stmt, vec_idx, REAL(op2)[i % op2_len]);
3358 mrmanese 755
                            sqlite3_step(stmt);
756
                        }
757
                    }
758
 
759
                    /* recycle on svec if we loop again */
760
                    sqlite3_finalize(stmt2);
761
                }
3397 mrmanese 762
 
3419 mrmanese 763
                sqlite3_finalize(stmt);
764
                _sqlite_commit;
3358 mrmanese 765
            }
766
 
767
        } else if (IS_INTEGER(op2) || IS_LOGICAL(op2)) { /* I N T E G E R */
768
            /* we are taking advantage of the fact that logicals are stored as int.
769
             * if this becomes untrue in the future, then this is a bug */
3458 mrmanese 770
            /* we are binding double because ___ (?) */
3358 mrmanese 771
            op2_len = LENGTH(op2);
772
 
773
            if (op2_len == 1) {
774
                if (funcname[0] == '%') {
775
                    sprintf(g_sql_buf[2], insert_fmt_string1c, iname, 
3434 mrmanese 776
                            varname_src, (funcname[1] == '%') ? "r_mod" : "r_intdiv", iname_src);
3358 mrmanese 777
                } else {
778
                    sprintf(g_sql_buf[2], insert_fmt_string1s, iname,
779
                            varname_src, funcname, iname_src);
780
                }
781
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
782
                _sqlite_error(res);
783
                sqlite3_bind_double(stmt, 1, (double)INTEGER(op2)[0]);
784
                sqlite3_step(stmt);
785
            } else if (op2_len <= svec_len) {
786
                sprintf(g_sql_buf[2], "select [row name], [%s] from [%s].sdf_data",
787
                        varname_src, iname_src);
788
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
789
                _sqlite_error(res);
790
 
791
                if (funcname[0] == '%') {
3434 mrmanese 792
                    sprintf(g_sql_buf[2], insert_fmt_string2c, iname, 
793
                            (funcname[1] == '%') ? "r_mod" : "r_intdiv");
3419 mrmanese 794
                    _sqlite_begin;
3358 mrmanese 795
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
796
                    _sqlite_error(res);
797
 
798
                    for (i = 0; i < svec_len; i++) {
799
                        sqlite3_step(stmt2);
800
 
801
                        sqlite3_reset(stmt);
802
                        sqlite3_bind_text(stmt, 1, (char *)sqlite3_column_text(stmt2, 0), -1, SQLITE_STATIC);
3434 mrmanese 803
                        sqlite3_bind_int(stmt, sdf_idx, sqlite3_column_int(stmt2, 1));
804
                        sqlite3_bind_int(stmt, vec_idx, INTEGER(op2)[i % op2_len]);
3358 mrmanese 805
                        sqlite3_step(stmt);
806
                    }
3458 mrmanese 807
                    sqlite3_finalize(stmt);
808
                    sqlite3_finalize(stmt2);
3419 mrmanese 809
                    _sqlite_commit;
3358 mrmanese 810
                } else {
811
                    sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
3419 mrmanese 812
                    _sqlite_begin;
3358 mrmanese 813
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
814
                    _sqlite_error(res);
815
 
816
                    for (i = 0; i < svec_len; i++) {
817
                        sqlite3_step(stmt2);
818
 
819
                        sqlite3_reset(stmt);
820
                        sqlite3_bind_text(stmt, 1, (char *)sqlite3_column_text(stmt2, 0), -1, SQLITE_STATIC);
3434 mrmanese 821
                        sqlite3_bind_double(stmt, sdf_idx, sqlite3_column_double(stmt2, 1));
822
                        sqlite3_bind_double(stmt, vec_idx, (double)INTEGER(op2)[i % op2_len]);
3358 mrmanese 823
                        sqlite3_step(stmt);
824
                    }
3458 mrmanese 825
                    sqlite3_finalize(stmt);
826
                    sqlite3_finalize(stmt2);
3419 mrmanese 827
                    _sqlite_commit;
3358 mrmanese 828
                }
829
            } else {
830
                sprintf(g_sql_buf[1], "select [%s] from [%s].sdf_data",
831
                        varname_src, iname_src);
832
 
833
                if (funcname[0] == '%') {
3434 mrmanese 834
                    sprintf(g_sql_buf[2], insert_fmt_string2c, iname,
835
                            (funcname[1] == '%') ? "r_mod" : "r_intdiv");
3358 mrmanese 836
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
837
                    _sqlite_error(res);
838
                } else {
839
                    sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
840
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
841
                    _sqlite_error(res);
842
                }
843
 
3419 mrmanese 844
                i = 0; _sqlite_begin; 
3358 mrmanese 845
                while (i < op2_len) {
846
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
847
                    _sqlite_error(res);
848
 
849
                    if (funcname[0] == '%') {
850
                        for ( ; i < op2_len && sqlite3_step(stmt) == SQLITE_ROW ; i++) {
851
                            sqlite3_step(stmt2);
852
 
853
                            sqlite3_reset(stmt);
854
                            /* simplify my life, just use ints for row names so that we
855
                             * don't have to worry about duplicates */
856
                            sqlite3_bind_int(stmt, 1, i); 
3434 mrmanese 857
                            sqlite3_bind_int(stmt, sdf_idx, sqlite3_column_int(stmt2, 1));
858
                            sqlite3_bind_int(stmt, vec_idx, INTEGER(op2)[i % op2_len]);
3358 mrmanese 859
                            sqlite3_step(stmt);
860
                        }
861
                    } else {
862
                        for ( ; i < op2_len && sqlite3_step(stmt) == SQLITE_ROW ; i++) {
863
                            sqlite3_step(stmt2);
864
 
865
                            sqlite3_reset(stmt);
866
                            sqlite3_bind_int(stmt, 1, i);
3434 mrmanese 867
                            sqlite3_bind_double(stmt, sdf_idx, sqlite3_column_double(stmt2, 1));
868
                            sqlite3_bind_double(stmt, vec_idx, (double)INTEGER(op2)[i % op2_len]);
3358 mrmanese 869
                            sqlite3_step(stmt);
870
                        }
871
                    }
872
 
873
                    /* recycle on svec if we loop again */
874
                    sqlite3_finalize(stmt2);
875
                }
3458 mrmanese 876
                sqlite3_finalize(stmt);
3419 mrmanese 877
                _sqlite_commit;
3358 mrmanese 878
            }
879
 
3458 mrmanese 880
        } else if (IS_CHARACTER(op2)) {
881
            if (functype != 2) error("not supported");
882
 
883
            op2_len = LENGTH(op2);
884
 
885
            if (op2_len == 1) {
886
                sprintf(g_sql_buf[2], insert_fmt_string1s, iname,
887
                            varname_src, funcname, iname_src);
888
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
889
                _sqlite_error(res);
890
                sqlite3_bind_text(stmt, 1, CHAR_ELT(op2, 0), -1, SQLITE_STATIC);
891
                sqlite3_step(stmt);
892
            } else if (op2_len <= svec_len) {
893
                sprintf(g_sql_buf[2], "select [row name], [%s] from [%s].sdf_data",
894
                        varname_src, iname_src);
895
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
896
                _sqlite_error(res);
897
 
898
                sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
899
                _sqlite_begin;
900
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
901
                _sqlite_error(res);
902
 
903
                for (i = 0; i < svec_len; i++) {
904
                    sqlite3_step(stmt2);
905
 
906
                    sqlite3_reset(stmt);
907
                    sqlite3_bind_text(stmt, 1, (char *)sqlite3_column_text(stmt2, 0), -1, SQLITE_STATIC);
908
                    sqlite3_bind_text(stmt, sdf_idx, (char *)sqlite3_column_text(stmt2, 1), -1, SQLITE_STATIC);
909
                    sqlite3_bind_text(stmt, vec_idx, CHAR_ELT(op2, i % op2_len), -1 , SQLITE_STATIC);
910
                    sqlite3_step(stmt);
911
                }
912
                sqlite3_finalize(stmt2);
913
                _sqlite_commit;
914
            } else {
915
                sprintf(g_sql_buf[1], "select [%s] from [%s].sdf_data",
916
                        varname_src, iname_src);
917
 
918
                sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
919
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
920
                _sqlite_error(res);
921
 
922
                i = 0; _sqlite_begin; 
923
                while (i < op2_len) {
924
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
925
                    _sqlite_error(res);
926
 
927
                    for ( ; i < op2_len && sqlite3_step(stmt) == SQLITE_ROW ; i++) {
928
                        sqlite3_step(stmt2);
929
 
930
                        sqlite3_reset(stmt);
931
                        sqlite3_bind_int(stmt, 1, i);
932
                        sqlite3_bind_text(stmt, sdf_idx, (char *)sqlite3_column_text(stmt2, 1), -1, SQLITE_STATIC);
933
                        sqlite3_bind_text(stmt, vec_idx, CHAR_ELT(op2, i % op2_len), -1 , SQLITE_STATIC);
934
                        sqlite3_step(stmt);
935
                    }
936
 
937
                    /* recycle on svec if we loop again */
938
                    sqlite3_finalize(stmt2);
939
                }
940
                sqlite3_finalize(stmt);
941
                _sqlite_commit;
942
            }
943
 
3358 mrmanese 944
        } else if (inherits(op2, "sqlite.vector")) { 
945
            /* op2 is surely not a factor, as handled by the R wrapper */
3434 mrmanese 946
            /* even though it is impossible for reversed to be FALSE, still use
947
             * sdf_idx and vec_idx so that code would be less confusing */
3358 mrmanese 948
            char *iname_op2, *varname_op2;
949
            sqlite3_stmt *stmt3;
950
            iname_op2 = SDF_INAME(op2);
951
            varname_op2 = SVEC_VARNAME(op2);
952
 
3419 mrmanese 953
            if (!USE_SDF1(iname_op2, TRUE, TRUE)) {
3358 mrmanese 954
                /* delete created sqlite.vector */
3458 mrmanese 955
                warning("detaching created result SDF %s\n", iname);
3358 mrmanese 956
                sdf_detach_sdf(mkString(iname));
957
                return R_NilValue;
958
            }
3419 mrmanese 959
            _sqlite_begin;
3358 mrmanese 960
 
961
            op2_len = _get_row_count2(iname_op2, 1);
962
 
963
            sprintf(g_sql_buf[2], "select [%s] from [%s].sdf_data", varname_src, iname_src);
964
            res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
965
            _sqlite_error(res);
966
 
967
            sprintf(g_sql_buf[2], "select [%s] from [%s].sdf_data", varname_op2, iname_op2);
968
            res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt3, 0);
969
            _sqlite_error(res);
970
 
971
            if (funcname[0] == '%') {
3434 mrmanese 972
                sprintf(g_sql_buf[2], insert_fmt_string2c, iname,
973
                            (funcname[1] == '%') ? "r_mod" : "r_intdiv");
3358 mrmanese 974
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
975
                _sqlite_error(res);
976
 
977
                if (svec_len == op2_len) {
978
                    for (i = 0; i < svec_len; i++) {
979
                        sqlite3_step(stmt2); sqlite3_step(stmt3);
980
 
981
                        sqlite3_reset(stmt);
982
                        sqlite3_bind_int(stmt, 1, i);
3434 mrmanese 983
                        sqlite3_bind_int(stmt, sdf_idx, sqlite3_column_int(stmt2, 0));
984
                        sqlite3_bind_int(stmt, vec_idx, sqlite3_column_int(stmt3, 0));
3358 mrmanese 985
                        sqlite3_step(stmt);
986
                    }
987
                } else if (svec_len < op2_len) {
988
                    i = 0;
989
                    while (i < op2_len) {
990
                        for ( ; sqlite3_step(stmt2) == SQLITE_ROW && i < op2_len; i++) {
991
                            sqlite3_step(stmt3);
992
 
993
                            sqlite3_reset(stmt);
994
                            sqlite3_bind_int(stmt, 1, i);
3434 mrmanese 995
                            sqlite3_bind_int(stmt, sdf_idx, sqlite3_column_int(stmt2, 0));
996
                            sqlite3_bind_int(stmt, vec_idx, sqlite3_column_int(stmt3, 0));
3358 mrmanese 997
                            sqlite3_step(stmt);
998
                        }
999
                        sqlite3_reset(stmt2);
1000
                    }
1001
                } else {
1002
                    i = 0;
1003
                    while (i < svec_len) {
1004
                        for ( ; sqlite3_step(stmt3) == SQLITE_ROW && i < svec_len; i++) {
1005
                            sqlite3_step(stmt2);
1006
 
1007
                            sqlite3_reset(stmt);
1008
                            sqlite3_bind_int(stmt, 1, i);
3434 mrmanese 1009
                            sqlite3_bind_int(stmt, sdf_idx, sqlite3_column_int(stmt2, 0));
1010
                            sqlite3_bind_int(stmt, vec_idx, sqlite3_column_int(stmt3, 0));
3358 mrmanese 1011
                            sqlite3_step(stmt);
1012
                        }
1013
                        sqlite3_reset(stmt3);
1014
                    }
1015
                }
3419 mrmanese 1016
                _sqlite_commit;
3358 mrmanese 1017
            } else { /* not an integer op %% or %/% */
1018
                sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
1019
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
1020
                _sqlite_error(res);
1021
 
1022
                if (svec_len == op2_len) {
1023
                    for (i = 0; i < svec_len; i++) {
1024
                        sqlite3_step(stmt2); sqlite3_step(stmt3);
1025
 
1026
                        sqlite3_reset(stmt);
1027
                        sqlite3_bind_int(stmt, 1, i);
3434 mrmanese 1028
                        sqlite3_bind_double(stmt, sdf_idx, sqlite3_column_double(stmt2, 0));
1029
                        sqlite3_bind_double(stmt, vec_idx, sqlite3_column_double(stmt3, 0));
3358 mrmanese 1030
                        sqlite3_step(stmt);
1031
                    }
1032
                } else if (svec_len < op2_len) { /* recycle svec */
1033
                    i = 0;
1034
                    while (i < op2_len) {
1035
                        for ( ; sqlite3_step(stmt2) == SQLITE_ROW && i < op2_len; i++) {
1036
                            sqlite3_step(stmt3);
1037
 
1038
                            sqlite3_reset(stmt);
1039
                            sqlite3_bind_int(stmt, 1, i);
3434 mrmanese 1040
                            sqlite3_bind_double(stmt, sdf_idx, sqlite3_column_double(stmt2, 0));
1041
                            sqlite3_bind_double(stmt, vec_idx, sqlite3_column_double(stmt3, 0));
3358 mrmanese 1042
                            sqlite3_step(stmt);
1043
                        }
1044
                        sqlite3_reset(stmt2);
1045
                    }
1046
                } else { /* svec_len > op2_len, recycle op2 */
1047
                    i = 0;
1048
                    while (i < svec_len) {
1049
                        for ( ; sqlite3_step(stmt3) == SQLITE_ROW && i < svec_len; i++) {
1050
                            sqlite3_step(stmt2);
1051
 
1052
                            sqlite3_reset(stmt);
1053
                            sqlite3_bind_int(stmt, 1, i);
3434 mrmanese 1054
                            sqlite3_bind_double(stmt, sdf_idx, sqlite3_column_double(stmt2, 0));
1055
                            sqlite3_bind_double(stmt, vec_idx, sqlite3_column_double(stmt3, 0));
3358 mrmanese 1056
                            sqlite3_step(stmt);
1057
                        }
1058
                        sqlite3_reset(stmt3);
1059
                    }
1060
                }
1061
            }
1062
 
1063
            sqlite3_finalize(stmt);
1064
            sqlite3_finalize(stmt2);
1065
            sqlite3_finalize(stmt3);
3419 mrmanese 1066
            _sqlite_commit;
1067
 
1068
            UNUSE_SDF2(iname_op2);
3358 mrmanese 1069
        }
1070
    } else if (functype == 1 && funcname[0] != '!') { /* unary not operator */
3419 mrmanese 1071
        iname = _create_svector1(R_NilValue, "bit", NULL, TRUE);
3358 mrmanese 1072
        sprintf(g_sql_buf[2], "insert into [%s].sdf_data "
1073
                    "select [row name], [%s] == 0 from [%s].sdf_data", 
1074
                    iname, varname_src, iname_src);
1075
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
1076
        _sqlite_error(res);
1077
        sqlite3_step(stmt);
1078
        sqlite3_finalize(stmt);
1079
    }
1080
 
3419 mrmanese 1081
    if (iname != NULL) {
3684 mrmanese 1082
        return _create_svector_sexp(iname, "sdf_data", "V1", 
3358 mrmanese 1083
                (functype == 0) ? "numeric" : "logical");
3419 mrmanese 1084
        UNUSE_SDF2(iname);
1085
    }
3358 mrmanese 1086
 
3419 mrmanese 1087
    UNUSE_SDF2(iname_src);
1088
 
3358 mrmanese 1089
    return R_NilValue;
1090
 
3307 mrmanese 1091
}
1092
 
3324 mrmanese 1093
SEXP sdf_do_variable_summary(SEXP func, SEXP vector, SEXP na_rm) {
1094
    char *iname_src, *varname_src, *funcname;
1095
    int res;
1096
    sqlite3_stmt *stmt;
3855 mrmanese 1097
    double _ret = NA_REAL, _ret2; SEXP ret;
3307 mrmanese 1098
 
3324 mrmanese 1099
    /* get data from arguments (function name and sqlite.vector stuffs) */
1100
    funcname = CHAR_ELT(func, 0);
1101
    iname_src = SDF_INAME(vector);
1102
    varname_src = SVEC_VARNAME(vector);
3307 mrmanese 1103
 
3419 mrmanese 1104
    if (!USE_SDF1(iname_src, TRUE, FALSE)) return R_NilValue;
3324 mrmanese 1105
 
1106
    g_narm = LOGICAL(na_rm)[0];
1107
    if (strcmp(funcname, "range") == 0) {
1108
        /* special handling for range. use min then max */
3473 mrmanese 1109
        _init_sqlite_function_accumulator();
3324 mrmanese 1110
        sprintf(g_sql_buf[0], "select min_df([%s]) from [%s].sdf_data", varname_src, iname_src);
1111
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
1112
        if (_sqlite_error(res)) return R_NilValue;
3855 mrmanese 1113
        sqlite3_step(stmt); 
1114
        _ret = sqlite3_column_double(stmt, 0);
1115
        sqlite3_finalize(stmt);
3324 mrmanese 1116
 
1117
        if (R_IsNA(_ret) && !g_narm) {
1118
            PROTECT(ret = NEW_NUMERIC(2));
1119
            REAL(ret)[0] = REAL(ret)[1] = R_NaReal;
1120
            goto __sdf_do_variable_summary_out;
1121
        }
1122
 
3473 mrmanese 1123
        _init_sqlite_function_accumulator();
3324 mrmanese 1124
        sprintf(g_sql_buf[0], "select max_df([%s]) from [%s].sdf_data", varname_src, iname_src);
1125
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
1126
        if (_sqlite_error(res)) return R_NilValue;
3855 mrmanese 1127
        sqlite3_step(stmt); 
1128
        _ret2 = sqlite3_column_double(stmt, 0);
1129
        sqlite3_finalize(stmt);
3324 mrmanese 1130
 
1131
        /* if there is NA, then the if above should have caught it already */
1132
        PROTECT(ret = NEW_NUMERIC(2));
1133
        REAL(ret)[0] = _ret;
3855 mrmanese 1134
        REAL(ret)[1] = _ret2;
3324 mrmanese 1135
    } else {
3473 mrmanese 1136
        _init_sqlite_function_accumulator();
3324 mrmanese 1137
        sprintf(g_sql_buf[0], "select %s_df([%s]) from [%s].sdf_data", funcname, varname_src, iname_src);
1138
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
1139
        if (_sqlite_error(res)) return R_NilValue;
3855 mrmanese 1140
        res = sqlite3_step(stmt); 
1141
        _ret = sqlite3_column_double(stmt, 0);
1142
        sqlite3_finalize(stmt);
3324 mrmanese 1143
 
1144
        if (strcmp(funcname, "all") == 0 || strcmp(funcname, "any") == 0) {
1145
            PROTECT(ret = NEW_LOGICAL(1));
3855 mrmanese 1146
            if (R_IsNA(_ret)) LOGICAL(ret)[0] = NA_INTEGER;
1147
            else LOGICAL(ret)[0] = (_ret != 0);
3324 mrmanese 1148
        } else {
1149
            PROTECT(ret = NEW_NUMERIC(1));
3855 mrmanese 1150
            REAL(ret)[0] = _ret;
3324 mrmanese 1151
        }
1152
    }
1153
 
1154
__sdf_do_variable_summary_out:
1155
    UNPROTECT(1);
1156
    return ret;
1157
}
1158
 
1159
 
3358 mrmanese 1160
SEXP sdf_sort_variable(SEXP svec, SEXP decreasing) {
4160 mrmanese 1161
    char *iname, *tblname, *varname, *type, *sort_type;
3358 mrmanese 1162
    sqlite3_stmt *stmt;
1163
    int res;
1164
 
3808 mrmanese 1165
    iname = SDF_INAME(svec);
1166
    tblname = SVEC_TBLNAME(svec);
1167
    varname = SVEC_VARNAME(svec);
3358 mrmanese 1168
 
3808 mrmanese 1169
    if (!USE_SDF1(iname, TRUE, TRUE)) return R_NilValue;
3358 mrmanese 1170
 
1171
    /* determine type of svec */
3808 mrmanese 1172
    sprintf(g_sql_buf[0], "select [%s] from [%s].[%s] limit 1", varname, iname, tblname);
3358 mrmanese 1173
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
1174
    _sqlite_error(res);
1175
    sqlite3_step(stmt);
1176
    strcpy(g_sql_buf[0], sqlite3_column_decltype(stmt, 0));
1177
    sqlite3_finalize(stmt);
1178
 
3456 mrmanese 1179
    if (TEST_SDFVECTORTYPE(svec, "factor")) { /* copy factor table to iname */
1180
        if (TEST_SDFVECTORTYPE(svec, "ordered")) type = "ordered";
3358 mrmanese 1181
        else type = "factor";
3808 mrmanese 1182
        _copy_factor_levels2(type, iname, varname, iname, "V1");
3471 mrmanese 1183
    } else type = CHAR_ELT(GET_SDFVECTORTYPE(svec), 0);
3358 mrmanese 1184
 
3808 mrmanese 1185
    if (strcmp(tblname, "sdf_data") == 0) {
1186
        /* cache sorted data if column to be sorted comes from main sdf_data */
3419 mrmanese 1187
 
3808 mrmanese 1188
        /* test if there is already a sort_varname table */
1189
        sort_type = (LOGICAL(decreasing)[0]) ? "desc" : "asc";
1190
        sprintf(g_sql_buf[1], "select count(*) from [%s].sqlite_master "
1191
                "where type='table' and name='sort_%s_%s'", iname, sort_type, varname);
1192
        sqlite3_prepare(g_workspace, g_sql_buf[1], -1, &stmt, NULL);
1193
        sqlite3_step(stmt);
1194
        res = sqlite3_column_int(stmt, 0);
1195
        sqlite3_finalize(stmt);
1196
 
1197
        if (res == 0) {
1198
            sprintf(g_sql_buf[1], "create table [%s].[sort_%s_%s] ([%s] %s)", 
1199
                    iname, sort_type, varname, varname, g_sql_buf[0]);
1200
            if (_sqlite_error(_sqlite_exec(g_sql_buf[1]))) 
1201
                    error("Can't create table: %s", g_sql_buf[1]);
1202
 
1203
            /* insert to new sdf ordered */
1204
            sprintf(g_sql_buf[0], "insert into [%s].[sort_%s_%s] "
1205
                    "select [%s] from [%s].[%s] order by [%s] %s", 
1206
                    iname, sort_type, varname, varname, iname, tblname, varname, sort_type);
1207
 
1208
            res = _sqlite_exec(g_sql_buf[0]);
1209
            if (_sqlite_error(res)) error("Can't insert: %s", g_sql_buf[0]);
1210
        }
1211
 
1212
        UNUSE_SDF2(iname);
1213
        sprintf(g_sql_buf[0], "sort_%s_%s", sort_type, varname);
1214
        return _create_svector_sexp(iname, g_sql_buf[0], varname, type);
1215
    } else {
1216
        /* create a new sdf table, copy data to that */
1217
 
1218
        /* create a sorted column */
1219
 
1220
        error("Not yet supported.");
1221
    }
1222
 
1223
    return R_NilValue;
3358 mrmanese 1224
}
1225
 
3307 mrmanese 1226
/****************************************************************************
1227
 * VECTOR MATH/OPS/GROUP OPERATIONS
1228
 ****************************************************************************/
3855 mrmanese 1229
struct accumulator_t {
1230
    /* long double acts weird here, but if it's not long double,
1231
     * it f*cks up sum() */
1232
    long double accumulator;
1233
    int started;
1234
};
3324 mrmanese 1235
 
3434 mrmanese 1236
R_INLINE int __vecmath_checkarg(sqlite3_context *ctx, sqlite3_value *arg, double *value) {
3307 mrmanese 1237
    int ret = 1;
1238
    if (sqlite3_value_type(arg) == SQLITE_NULL) { 
1239
        sqlite3_result_null(ctx); 
1240
        ret = 0;
1241
    } else {
1242
        if (sqlite3_value_type(arg) == SQLITE_INTEGER) 
1243
            *value = sqlite3_value_int(arg); 
1244
        else *value = sqlite3_value_double(arg); 
1245
    }
1246
    return ret;
1247
}
1248
 
1249
#define SQLITE_MATH_FUNC1(name, func) static void __vecmath_ ## name(\
1250
        sqlite3_context *ctx, int argc, sqlite3_value **argv) { \
1251
    double value; \
1252
    if (__vecmath_checkarg(ctx, argv[0], &value)) { \
1253
        sqlite3_result_double(ctx, func(value)); \
1254
    }  \
1255
}
1256
 
3434 mrmanese 1257
#define SQLITE_MATH_FUNC2(name, func) static void __vecmath_ ## name(\
1258
        sqlite3_context *ctx, int argc, sqlite3_value **argv) { \
1259
    double value1, value2; \
1260
    if (__vecmath_checkarg(ctx, argv[0], &value1) && \
1261
        __vecmath_checkarg(ctx, argv[1], &value2)) { \
1262
        sqlite3_result_double(ctx, func((long double)value1, (long double)value2)); \
1263
    }  \
1264
}
1265
 
3324 mrmanese 1266
#define SQLITE_MATH_FUNC_CUM(name, func) static void __vecmath_ ## name(\
1267
        sqlite3_context *ctx, int argc, sqlite3_value **argv) { \
1268
    double value; \
1269
    if (__vecmath_checkarg(ctx, argv[0], &value)) { \
1270
        if (g_start) { g_start = 0; g_accumulator = value; } \
1271
        else g_accumulator = func(g_accumulator, value); \
1272
        sqlite3_result_double(ctx, g_accumulator); \
1273
    }  \
1274
}
1275
 
3434 mrmanese 1276
#define LOGBASE(a, b) log(a)/log(b)
1277
 
3307 mrmanese 1278
/* SQLITE_MATH_FUNC1(abs, abs)   in SQLite */
1279
SQLITE_MATH_FUNC1(sign, sign)   /* in R */
1280
SQLITE_MATH_FUNC1(sqrt, sqrt)
1281
SQLITE_MATH_FUNC1(floor, floor)
1282
SQLITE_MATH_FUNC1(ceiling, ceil)
1283
SQLITE_MATH_FUNC1(trunc, ftrunc) /* in R */
3434 mrmanese 1284
/*SQLITE_MATH_FUNC2(round, fprec)  2 arg, in SQLite, but override with R's version */
1285
SQLITE_MATH_FUNC2(signif, fround) /* 2 arg, in R */
3307 mrmanese 1286
SQLITE_MATH_FUNC1(exp, exp)
3434 mrmanese 1287
SQLITE_MATH_FUNC2(log, LOGBASE) /* 2 arg */
3307 mrmanese 1288
SQLITE_MATH_FUNC1(cos, cos)
1289
SQLITE_MATH_FUNC1(sin, sin)
1290
SQLITE_MATH_FUNC1(tan, tan)
1291
SQLITE_MATH_FUNC1(acos, acos)
1292
SQLITE_MATH_FUNC1(asin, asin)
1293
SQLITE_MATH_FUNC1(atan, atan)
1294
SQLITE_MATH_FUNC1(cosh, cosh)
1295
SQLITE_MATH_FUNC1(sinh, sinh)
1296
SQLITE_MATH_FUNC1(tanh, tanh)
1297
SQLITE_MATH_FUNC1(acosh, acosh)  /* nowhere in include?? */
1298
SQLITE_MATH_FUNC1(asinh, asinh)  /* nowhere in include?? */
1299
SQLITE_MATH_FUNC1(atanh, atanh)  /* nowhere in include?? */
1300
SQLITE_MATH_FUNC1(lgamma, lgammafn) /* in R */
1301
SQLITE_MATH_FUNC1(gamma, gammafn) /* in R */
1302
/* SQLITE_MATH_FUNC1(gammaCody, gammaCody)   * in R ?? */
1303
SQLITE_MATH_FUNC1(digamma, digamma) /* in R */    
1304
SQLITE_MATH_FUNC1(trigamma, trigamma) /* in R */
1305
 
3855 mrmanese 1306
#define SUM(a, b)  ((long double)(a) + (b))
3324 mrmanese 1307
#define PROD(a, b) (a) * (b)
1308
#define MIN(a, b) ((a) <= (b)) ? (a) : (b)
1309
#define MAX(a, b) ((a) >= (b)) ? (a) : (b)
1310
#define ALL(a, b) (((a) == 0) || ((b) == 0)) ? 0 : 1
1311
#define ANY(a, b) (((a) == 0) && ((b) == 0)) ? 0 : 1
1312
 
1313
SQLITE_MATH_FUNC_CUM(cumsum, SUM)
1314
SQLITE_MATH_FUNC_CUM(cumprod, PROD)
1315
SQLITE_MATH_FUNC_CUM(cummin, MIN)
1316
SQLITE_MATH_FUNC_CUM(cummax, MAX)
1317
 
1318
#define SQLITE_SUMMARY_FUNC(name, func) static void __vecsummary_ ## name(\
1319
        sqlite3_context *ctx, int argc, sqlite3_value **argv) { \
1320
    double value; \
3855 mrmanese 1321
    struct accumulator_t *acc; \
1322
    acc = sqlite3_aggregate_context(ctx, sizeof(struct accumulator_t)); \
1323
    if (!g_narm && R_IsNA(acc->accumulator)) return; /* NA if na.rm=F & NA found */ \
3324 mrmanese 1324
    if (sqlite3_value_type(argv[0]) != SQLITE_NULL) {  \
1325
        if (sqlite3_value_type(argv[0]) == SQLITE_INTEGER) {  \
1326
            int tmp = sqlite3_value_int(argv[0]); \
1327
            value = (tmp == NA_INTEGER) ? R_NaReal : tmp; \
1328
        } else value = sqlite3_value_double(argv[0]);  \
1329
        if (R_IsNA(value)) { \
3855 mrmanese 1330
            if (!g_narm) acc->accumulator = value; return; /* else ignore */ \
1331
        } else if (!acc->started) { \
1332
            acc->started = 1; \
1333
            acc->accumulator = value; \
1334
        } else acc->accumulator = func(acc->accumulator, value); \
3324 mrmanese 1335
    } \
1336
}
1337
 
1338
SQLITE_SUMMARY_FUNC(all_df, ALL)
1339
SQLITE_SUMMARY_FUNC(any_df, ANY)
1340
SQLITE_SUMMARY_FUNC(sum_df, SUM)
1341
SQLITE_SUMMARY_FUNC(prod_df, PROD)
1342
SQLITE_SUMMARY_FUNC(min_df, MIN)
1343
SQLITE_SUMMARY_FUNC(max_df, MAX)
1344
 
1345
static void __vecsummary_finalize(sqlite3_context *ctx) {
1346
    /* g_accumulator already summarizes it. just return that */
3855 mrmanese 1347
    struct accumulator_t *acc = (struct accumulator_t *)
1348
                sqlite3_aggregate_context(ctx, sizeof(struct accumulator_t)); 
1349
    sqlite3_result_double(ctx, acc->accumulator);
3324 mrmanese 1350
}
1351
 
3434 mrmanese 1352
static void __r_modulo(sqlite3_context *ctx, int argc, sqlite3_value **argv) {
1353
    if (sqlite3_value_type(argv[0]) == SQLITE_NULL ||
1354
        sqlite3_value_type(argv[1]) == SQLITE_NULL) { 
1355
        sqlite3_result_null(ctx); 
1356
    } else {
1357
        double v1, v2, q, tmp;
1358
        if (sqlite3_value_type(argv[0]) == SQLITE_INTEGER && 
1359
            sqlite3_value_type(argv[1]) == SQLITE_INTEGER) {
1360
            int i1, i2;
1361
            i1 = sqlite3_value_int(argv[0]);
1362
            i2 = sqlite3_value_int(argv[1]);
1363
            if (i1 > 0 && i2 > 0) { 
1364
                sqlite3_result_int(ctx, i1 % i2); return;
1365
            }
1366
            v1 = i1; v2 = i2;
1367
        } else {
1368
            v1 = sqlite3_value_double(argv[0]);
1369
            v2 = sqlite3_value_double(argv[1]);
1370
        }
1371
        /* copied from myfmod() in src/main/arithmetic.c */
1372
        q = v1 / v2; 
1373
        if (v2 == 0) sqlite3_result_double(ctx, R_NaN);
1374
        tmp = v1 - floor(q) * v2;
1375
        /* checking omitted */
1376
        q = floor(tmp/v2);
1377
        sqlite3_result_double(ctx, tmp - q*v2);
1378
 
1379
    }
1380
}
1381
 
1382
static void __r_intdiv(sqlite3_context *ctx, int argc, sqlite3_value **argv) {
1383
    if (sqlite3_value_type(argv[0]) == SQLITE_NULL ||
1384
        sqlite3_value_type(argv[1]) == SQLITE_NULL) { 
1385
        sqlite3_result_null(ctx); 
1386
    } else {
1387
        double v1, v2;
1388
        if (sqlite3_value_type(argv[0]) == SQLITE_INTEGER && 
1389
            sqlite3_value_type(argv[1]) == SQLITE_INTEGER) {
1390
            int i1, i2;
1391
            i1 = sqlite3_value_int(argv[0]);
1392
            i2 = sqlite3_value_int(argv[1]);
1393
            if (i1 == NA_INTEGER || i2 == NA_INTEGER) {
1394
                sqlite3_result_int(ctx, NA_INTEGER); return;
1395
            } else if (i2 == 0) {
1396
                sqlite3_result_int(ctx, 0); return;
1397
            }
1398
            v1 = i1; v2 = i2;
1399
        } else {
1400
            v1 = sqlite3_value_double(argv[0]);
1401
            v2 = sqlite3_value_double(argv[1]);
1402
        }
1403
        /* copied from IDIVOP cases in src/main/arithmetic.c */
1404
        sqlite3_result_double(ctx, floor(v1/v2));
1405
    }
1406
}
3307 mrmanese 1407
#define VMENTRY1(func)  {#func, __vecmath_ ## func}
3324 mrmanese 1408
#define VSENTRY1(func)  {#func, __vecsummary_ ## func}
3307 mrmanese 1409
void __register_vector_math() {
1410
    int i, res;
1411
    static const struct {
1412
        char *name;
1413
        void (*func)(sqlite3_context*, int, sqlite3_value**);
1414
    } arr_func1[] = {
1415
        VMENTRY1(sign),
1416
        VMENTRY1(sqrt),
1417
        VMENTRY1(floor),
1418
        VMENTRY1(ceiling),
1419
        VMENTRY1(trunc),
1420
        VMENTRY1(exp),
1421
        VMENTRY1(cos),
1422
        VMENTRY1(sin),
1423
        VMENTRY1(tan),
1424
        VMENTRY1(acos),
1425
        VMENTRY1(asin),
1426
        VMENTRY1(atan),
1427
        VMENTRY1(cosh),
1428
        VMENTRY1(sinh),
1429
        VMENTRY1(tanh),
1430
        VMENTRY1(acosh),
1431
        VMENTRY1(asinh),
1432
        VMENTRY1(atanh),
1433
        VMENTRY1(lgamma),
1434
        VMENTRY1(gamma),
1435
        VMENTRY1(digamma),
3324 mrmanese 1436
        VMENTRY1(trigamma),
1437
        VMENTRY1(cumsum),
1438
        VMENTRY1(cumprod),
1439
        VMENTRY1(cummin),
1440
        VMENTRY1(cummax)
3434 mrmanese 1441
    }, arr_func2[] =  {
1442
        {"r_mod", __r_modulo},
1443
        {"r_intdiv", __r_intdiv},
1444
        /*VMENTRY1(round),*/
1445
        VMENTRY1(signif),
1446
        VMENTRY1(log)
3324 mrmanese 1447
    }, arr_sum1[] = {
1448
        VSENTRY1(all_df),  /* can't override sum, min, max */
1449
        VSENTRY1(any_df),
1450
        VSENTRY1(sum_df),
1451
        VSENTRY1(prod_df),
1452
        VSENTRY1(min_df),
1453
        VSENTRY1(max_df)
3307 mrmanese 1454
    };
1455
 
3324 mrmanese 1456
    int len = sizeof(arr_func1) / sizeof(arr_func1[0]);
3307 mrmanese 1457
 
3324 mrmanese 1458
    for (i = 0; i < len; i++) {
3307 mrmanese 1459
        res = sqlite3_create_function(g_workspace, arr_func1[i].name, 1, 
1460
                SQLITE_ANY, NULL, arr_func1[i].func, NULL, NULL);
1461
        _sqlite_error(res);
1462
    }
3324 mrmanese 1463
 
3434 mrmanese 1464
    len = sizeof(arr_func2) / sizeof(arr_func2[0]);
1465
    for (i = 0; i < len; i++) {
1466
        res = sqlite3_create_function(g_workspace, arr_func2[i].name, 2, 
1467
                SQLITE_ANY, NULL, arr_func2[i].func, NULL, NULL);
1468
        _sqlite_error(res);
1469
    }
1470
 
3324 mrmanese 1471
    len = sizeof(arr_sum1) / sizeof(arr_sum1[0]);
1472
    for (i = 0; i < len; i++) {
1473
        res = sqlite3_create_function(g_workspace, arr_sum1[i].name, 1, 
1474
                SQLITE_ANY, NULL, NULL, arr_sum1[i].func, __vecsummary_finalize);
1475
        _sqlite_error(res);
1476
    }
3307 mrmanese 1477
}