The R Project SVN R-packages

Rev

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