The R Project SVN R-packages

Rev

Rev 3397 | Rev 3434 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3397 Rev 3419
Line 3... Line 3...
3
#include "Rmath.h"
3
#include "Rmath.h"
4
 
4
 
5
/****************************************************************************
5
/****************************************************************************
6
 * UTILITY FUNCTIONS
6
 * UTILITY FUNCTIONS
7
 ****************************************************************************/
7
 ****************************************************************************/
8
static char *_create_svector1(SEXP name, const char *type, int * _namelen) {
8
char *_create_svector1(SEXP name, const char *type, int * _namelen, int protect) {
9
    int namelen, res;
9
    int namelen, res;
10
    char *iname = _create_sdf_skeleton1(name, &namelen);
10
    char *iname = _create_sdf_skeleton1(name, &namelen, protect);
11
 
11
 
12
    if (iname == NULL) return NULL;
12
    if (iname == NULL) return NULL;
13
 
13
 
14
    sprintf(g_sql_buf[2], "create table [%s].sdf_data ([row name] text, "
14
    sprintf(g_sql_buf[2], "create table [%s].sdf_data ([row name] text, "
15
            "V1 %s, primary key ([row name]))", iname, type);
15
            "V1 %s, primary key ([row name]))", iname, type);
Line 113... Line 113...
113
    }
113
    }
114
 
114
 
115
    char *iname = SDF_INAME(sdf);
115
    char *iname = SDF_INAME(sdf);
116
    char *varname = CHAR_ELT(name, 0);
116
    char *varname = CHAR_ELT(name, 0);
117
 
117
 
118
    if (!USE_SDF1(iname, TRUE)) return R_NilValue;
118
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
119
 
119
 
120
    /* check if sdf & varname w/in that sdf exists */
120
    /* check if sdf & varname w/in that sdf exists */
121
    sqlite3_stmt *stmt;
121
    sqlite3_stmt *stmt;
122
    sprintf(g_sql_buf[0], "select [%s] from [%s].sdf_data", varname, iname);
122
    sprintf(g_sql_buf[0], "select [%s] from [%s].sdf_data", varname, iname);
123
 
123
 
Line 176... Line 176...
176
}
176
}
177
 
177
 
178
 
178
 
179
SEXP sdf_get_variable_length(SEXP svec) {
179
SEXP sdf_get_variable_length(SEXP svec) {
180
    char *iname = SDF_INAME(svec);
180
    char *iname = SDF_INAME(svec);
181
    if (!USE_SDF1(iname, TRUE)) return R_NilValue;
181
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
182
 
182
 
183
    SEXP ret;
183
    SEXP ret;
184
    PROTECT(ret = NEW_INTEGER(1));
184
    PROTECT(ret = NEW_INTEGER(1));
185
    INTEGER(ret)[0] = _get_row_count2(iname, 1);
185
    INTEGER(ret)[0] = _get_row_count2(iname, 1);
186
    UNPROTECT(1);
186
    UNPROTECT(1);
Line 192... Line 192...
192
    SEXP ret = R_NilValue, tmp;
192
    SEXP ret = R_NilValue, tmp;
193
    char *iname = SDF_INAME(svec), *varname = SVEC_VARNAME(svec);
193
    char *iname = SDF_INAME(svec), *varname = SVEC_VARNAME(svec);
194
    int index, idxlen, i, retlen=0, res;
194
    int index, idxlen, i, retlen=0, res;
195
    sqlite3_stmt *stmt;
195
    sqlite3_stmt *stmt;
196
 
196
 
197
    if (!USE_SDF1(iname, TRUE)) return R_NilValue;
197
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
198
 
198
 
199
    idxlen = LENGTH(idx);
199
    idxlen = LENGTH(idx);
200
    if (idxlen < 1) return ret;
200
    if (idxlen < 1) return ret;
201
 
201
 
202
    sprintf(g_sql_buf[0], "select [%s] from [%s].sdf_data limit ?,1",
202
    sprintf(g_sql_buf[0], "select [%s] from [%s].sdf_data limit ?,1",
Line 350... Line 350...
350
    }
350
    }
351
 
351
 
352
    return R_NilValue;
352
    return R_NilValue;
353
}
353
}
354
 
354
 
-
 
355
SEXP sdf_variable_summary(SEXP svec, SEXP maxsum) {
-
 
356
    char *iname, *varname, *type;
-
 
357
    sqlite3_stmt *stmt;
-
 
358
    int nprotected = 0;
-
 
359
    SEXP ret, names;
-
 
360
 
-
 
361
    iname = SDF_INAME(svec);
-
 
362
    varname = SVEC_VARNAME(svec);
-
 
363
    USE_SDF1(iname, TRUE, FALSE);
-
 
364
 
-
 
365
    if ((inherits(svec, "ordered") && ((type = "ordered"))) ||
-
 
366
            (inherits(svec, "factor") && ((type = "factor")))) {
-
 
367
        int nrows, i, max_rows = INTEGER(maxsum)[0];
-
 
368
 
-
 
369
 
-
 
370
        sprintf(g_sql_buf[0], "[%s].[%s %s]", iname, type, varname);
-
 
371
        nrows = _get_row_count2(g_sql_buf[0], FALSE);
-
 
372
        if (nrows <= max_rows) max_rows = nrows;
-
 
373
        else { nrows = max_rows; max_rows--; }
-
 
374
 
-
 
375
        PROTECT(ret = NEW_INTEGER(nrows)); nprotected = 1;
-
 
376
        PROTECT(names = NEW_CHARACTER(nrows)); nprotected++;
-
 
377
 
-
 
378
        sprintf(g_sql_buf[0], "select [%s].[%s %s].label, count(*) from "
-
 
379
                "[%s].sdf_data join [%s].[%s %s] on [%s].sdf_data.[%s]=[%s].[%s %s].level "
-
 
380
                "group by [%s].sdf_data.[%s], [%s].[%s %s].level order by count(*) desc",
-
 
381
                iname, type, varname, iname, iname, type, varname, iname, varname,
-
 
382
                iname, type, varname, iname, varname, iname, type, varname);
-
 
383
        sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
-
 
384
 
-
 
385
        for (i = 0; i < max_rows; i++) {
-
 
386
            sqlite3_step(stmt);
-
 
387
            SET_STRING_ELT(names, i, mkChar((char *)sqlite3_column_text(stmt, 0)));
-
 
388
            INTEGER(ret)[i] = sqlite3_column_int(stmt, 1);
-
 
389
        }
-
 
390
 
-
 
391
        if (nrows > max_rows) {
-
 
392
            int others_sum = 0;
-
 
393
            SET_STRING_ELT(names, nrows-1, mkChar("(Others)"));
-
 
394
            while (sqlite3_step(stmt) == SQLITE_ROW) {
-
 
395
                others_sum += sqlite3_column_int(stmt, 1);
-
 
396
            }
-
 
397
            INTEGER(ret)[nrows-1] = others_sum;
-
 
398
        }
-
 
399
    } else if (inherits(svec, "logical")) {
-
 
400
        sprintf(g_sql_buf[0], "select count(*) from "
-
 
401
                "[%s].sdf_data group by [%s] order by [%s]", iname, varname, varname);
-
 
402
 
-
 
403
        PROTECT(names = NEW_CHARACTER(3)); nprotected = 1;
-
 
404
        PROTECT(ret = NEW_CHARACTER(3)); nprotected++;
-
 
405
 
-
 
406
        SET_STRING_ELT(names, 0, mkChar("Mode"));
-
 
407
        SET_STRING_ELT(names, 1, mkChar("FALSE"));
-
 
408
        SET_STRING_ELT(names, 2, mkChar("TRUE"));
-
 
409
 
-
 
410
        sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
-
 
411
        SET_STRING_ELT(ret, 0, mkChar("logical"));
-
 
412
        sqlite3_step(stmt);
-
 
413
        SET_STRING_ELT(ret, 1, mkChar((const char *)sqlite3_column_text(stmt, 0)));
-
 
414
        sqlite3_step(stmt);
-
 
415
        SET_STRING_ELT(ret, 2, mkChar((const char *)sqlite3_column_text(stmt, 0)));
-
 
416
 
-
 
417
    } else return R_NilValue;
-
 
418
 
-
 
419
    sqlite3_finalize(stmt);
-
 
420
    SET_NAMES(ret, names);
-
 
421
    SET_CLASS(ret, mkString("table"));
-
 
422
    UNPROTECT(nprotected);
-
 
423
    return ret;
-
 
424
}
-
 
425
            
-
 
426
 
355
/* the global accumulator should be safe if we only do 1 cummulative or
427
/* the global accumulator should be safe if we only do 1 cummulative or
356
 * aggregate at any time. it won't work for stuffs like "select max(col)-min(col)
428
 * aggregate at any time. it won't work for stuffs like "select max(col)-min(col)
357
 * from sdf_data". */
429
 * from sdf_data". */
358
static double g_accumulator = 0.0; /* accumulator var for cumsum, cumprod, etc. */
430
static double g_accumulator = 0.0; /* accumulator var for cumsum, cumprod, etc. */
359
static int g_start = 0;            /* flag for start of cummulation */
431
static int g_start = 0;            /* flag for start of cummulation */
Line 368... Line 440...
368
    funcname = CHAR_ELT(func, 0);
440
    funcname = CHAR_ELT(func, 0);
369
    iname_src = SDF_INAME(vector);
441
    iname_src = SDF_INAME(vector);
370
    varname_src = SVEC_VARNAME(vector);
442
    varname_src = SVEC_VARNAME(vector);
371
    nargs = INTEGER(_nargs)[0];
443
    nargs = INTEGER(_nargs)[0];
372
 
444
 
373
    if (!USE_SDF1(iname_src, TRUE)) return R_NilValue;
445
    if (!USE_SDF1(iname_src, TRUE, TRUE)) return R_NilValue;
374
 
446
 
375
    /* check nargs */
447
    /* check nargs */
376
    if (nargs > 2) {
448
    if (nargs > 2) {
377
        Rprintf("Error: Don't know how to handle Math functions w/ more than 2 args\n");
449
        Rprintf("Error: Don't know how to handle Math functions w/ more than 2 args\n");
378
        return R_NilValue;
450
        return R_NilValue;
379
    }
451
    }
380
 
452
 
381
    /* create a new sdf, with 1 column named V1 */
453
    /* create a new sdf, with 1 column named V1 */
382
    iname = _create_svector1(R_NilValue, "double", &namelen);
454
    iname = _create_svector1(R_NilValue, "double", &namelen, TRUE);
383
 
455
 
384
    /* insert into <newsdf>.col, row.names select func(col), rownames */
456
    /* insert into <newsdf>.col, row.names select func(col), rownames */
385
    sprintf(g_sql_buf[0], "insert into [%s].sdf_data([row name], V1) "
457
    sprintf(g_sql_buf[0], "insert into [%s].sdf_data([row name], V1) "
386
            "select [row name], %s([%s]) from [%s].sdf_data", iname, funcname,
458
            "select [row name], %s([%s]) from [%s].sdf_data", iname, funcname,
387
            varname_src, iname_src);
459
            varname_src, iname_src);
Line 400... Line 472...
400
    g_accumulator = 0.0;   /* initialize accumulator */
472
    g_accumulator = 0.0;   /* initialize accumulator */
401
    g_start = 1;           /* flag that we are at start of accumulating */
473
    g_start = 1;           /* flag that we are at start of accumulating */
402
    sqlite3_step(stmt);
474
    sqlite3_step(stmt);
403
    sqlite3_finalize(stmt);
475
    sqlite3_finalize(stmt);
404
 
476
 
-
 
477
    UNUSE_SDF2(iname);
-
 
478
    UNUSE_SDF2(iname_src);
-
 
479
 
405
    return _create_svector_sexp(iname, "V1", "numeric");
480
    return _create_svector_sexp(iname, "V1", "numeric");
406
}
481
}
407
 
482
 
408
SEXP sdf_do_variable_op(SEXP func, SEXP vector, SEXP op2) {
483
SEXP sdf_do_variable_op(SEXP func, SEXP vector, SEXP op2) {
409
    char *iname = NULL, *iname_src, *varname_src, *funcname;
484
    char *iname = NULL, *iname_src, *varname_src, *funcname;
Line 413... Line 488...
413
    /* get data from arguments (function name and sqlite.vector stuffs) */
488
    /* get data from arguments (function name and sqlite.vector stuffs) */
414
    funcname = CHAR_ELT(func, 0);
489
    funcname = CHAR_ELT(func, 0);
415
    iname_src = SDF_INAME(vector);
490
    iname_src = SDF_INAME(vector);
416
    varname_src = SVEC_VARNAME(vector);
491
    varname_src = SVEC_VARNAME(vector);
417
 
492
 
418
    if (!USE_SDF1(iname_src, TRUE)) return R_NilValue;
493
    if (!USE_SDF1(iname_src, TRUE, TRUE)) return R_NilValue;
419
    switch(funcname[0]) {
494
    switch(funcname[0]) {
420
        case '+' :
495
        case '+' :
421
        case '-' :
496
        case '-' :
422
        case '*' :
497
        case '*' :
423
        case '/' :
498
        case '/' :
Line 436... Line 511...
436
 
511
 
437
    svec_len = _get_row_count2(iname_src, 1);
512
    svec_len = _get_row_count2(iname_src, 1);
438
    if (functype == 0 || functype == 2 || (functype == 1 && funcname[0] != '!')) {
513
    if (functype == 0 || functype == 2 || (functype == 1 && funcname[0] != '!')) {
439
        char *insert_fmt_string1c, *insert_fmt_string1s;
514
        char *insert_fmt_string1c, *insert_fmt_string1s;
440
        char *insert_fmt_string2c, *insert_fmt_string2s;
515
        char *insert_fmt_string2c, *insert_fmt_string2s;
441
        iname = _create_svector1(R_NilValue, (functype == 0) ? "double" : "bit", NULL);
516
        iname = _create_svector1(R_NilValue, (functype == 0) ? "double" : "bit", NULL, TRUE);
442
 
517
 
443
        if (functype == 1) { /* boolean binary operators */
518
        if (functype == 1) { /* boolean binary operators */
444
            insert_fmt_string1s = "insert into [%s].sdf_data "
519
            insert_fmt_string1s = "insert into [%s].sdf_data "
445
                        "select [row name], ([%s] != 0) %s (? != 0) from [%s].sdf_data";
520
                        "select [row name], ([%s] != 0) %s (? != 0) from [%s].sdf_data";
446
            insert_fmt_string2s = "insert into [%s].sdf_data values(?, (? != 0) %s (? != 0))";
521
            insert_fmt_string2s = "insert into [%s].sdf_data values(?, (? != 0) %s (? != 0))";
Line 473... Line 548...
473
                }
548
                }
474
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
549
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
475
                _sqlite_error(res);
550
                _sqlite_error(res);
476
                sqlite3_bind_double(stmt, 1, REAL(op2)[0]);
551
                sqlite3_bind_double(stmt, 1, REAL(op2)[0]);
477
                sqlite3_step(stmt);
552
                sqlite3_step(stmt);
-
 
553
                sqlite3_finalize(stmt);
478
            } else if (op2_len <= svec_len) {  /* recycle op2 */
554
            } else if (op2_len <= svec_len) {  /* recycle op2 */
479
                sprintf(g_sql_buf[2], "select [row name], [%s] from [%s].sdf_data",
555
                sprintf(g_sql_buf[2], "select [row name], [%s] from [%s].sdf_data",
480
                        varname_src, iname_src);
556
                        varname_src, iname_src);
481
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
557
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
482
                _sqlite_error(res);
558
                _sqlite_error(res);
483
 
559
 
484
                if (funcname[0] == '%') {
560
                if (funcname[0] == '%') {
485
                    /* sqlite does int div with "/" if both args are int. mod is % also.
561
                    /* sqlite does int div with "/" if both args are int. mod is % also.
486
                     * fortunately, in both cases the sqlite op is 2nd char of funcname */
562
                     * fortunately, in both cases the sqlite op is 2nd char of funcname */
487
                    sprintf(g_sql_buf[2], insert_fmt_string2c, iname, funcname[1]);  
563
                    sprintf(g_sql_buf[2], insert_fmt_string2c, iname, funcname[1]);  
488
                    _sqlite_exec("begin");
564
                    _sqlite_begin;
489
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
565
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
490
                    _sqlite_error(res);
566
                    _sqlite_error(res);
491
 
567
 
492
                    for (i = 0; i < svec_len; i++) {
568
                    for (i = 0; i < svec_len; i++) {
493
                        sqlite3_step(stmt2);
569
                        sqlite3_step(stmt2);
Line 496... Line 572...
496
                        sqlite3_bind_text(stmt, 1, (char *)sqlite3_column_text(stmt2, 0), -1, SQLITE_STATIC);
572
                        sqlite3_bind_text(stmt, 1, (char *)sqlite3_column_text(stmt2, 0), -1, SQLITE_STATIC);
497
                        sqlite3_bind_int(stmt, 2, (int)sqlite3_column_double(stmt2, 1));
573
                        sqlite3_bind_int(stmt, 2, (int)sqlite3_column_double(stmt2, 1));
498
                        sqlite3_bind_int(stmt, 3, (int)REAL(op2)[i % op2_len]);
574
                        sqlite3_bind_int(stmt, 3, (int)REAL(op2)[i % op2_len]);
499
                        sqlite3_step(stmt);
575
                        sqlite3_step(stmt);
500
                    }
576
                    }
-
 
577
                    sqlite3_finalize(stmt);
-
 
578
                    sqlite3_finalize(stmt2);
501
                    _sqlite_exec("commit");
579
                    _sqlite_commit;
502
                } else { /* non-integer binary operation */
580
                } else { /* non-integer binary operation */
503
                    sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
581
                    sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
504
                    _sqlite_exec("begin");
582
                    _sqlite_begin;
505
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
583
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
506
                    _sqlite_error(res);
584
                    _sqlite_error(res);
507
 
585
 
508
                    for (i = 0; i < svec_len; i++) {
586
                    for (i = 0; i < svec_len; i++) {
509
                        sqlite3_step(stmt2);
587
                        sqlite3_step(stmt2);
Line 512... Line 590...
512
                        sqlite3_bind_text(stmt, 1, (char *)sqlite3_column_text(stmt2, 0), -1, SQLITE_STATIC);
590
                        sqlite3_bind_text(stmt, 1, (char *)sqlite3_column_text(stmt2, 0), -1, SQLITE_STATIC);
513
                        sqlite3_bind_double(stmt, 2, sqlite3_column_double(stmt2, 1));
591
                        sqlite3_bind_double(stmt, 2, sqlite3_column_double(stmt2, 1));
514
                        sqlite3_bind_double(stmt, 3, REAL(op2)[i % op2_len]);
592
                        sqlite3_bind_double(stmt, 3, REAL(op2)[i % op2_len]);
515
                        sqlite3_step(stmt);
593
                        sqlite3_step(stmt);
516
                    }
594
                    }
-
 
595
                    sqlite3_finalize(stmt);
-
 
596
                    sqlite3_finalize(stmt2);
517
                    _sqlite_exec("commit");
597
                    _sqlite_commit;
518
                }
598
                }
519
                sqlite3_finalize(stmt2);
-
 
520
            } else { /* op2_len > svec_len, recycle svec */
599
            } else { /* op2_len > svec_len, recycle svec */
521
                sprintf(g_sql_buf[1], "select [%s] from [%s].sdf_data",
600
                sprintf(g_sql_buf[1], "select [%s] from [%s].sdf_data",
522
                        varname_src, iname_src);
601
                        varname_src, iname_src);
523
 
602
 
524
                if (funcname[0] == '%') {
603
                if (funcname[0] == '%') {
Line 529... Line 608...
529
                    sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
608
                    sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
530
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
609
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
531
                    _sqlite_error(res);
610
                    _sqlite_error(res);
532
                }
611
                }
533
               
612
               
534
                i = 0; _sqlite_exec("begin");
613
                i = 0; _sqlite_begin;
535
                while (i < op2_len) {
614
                while (i < op2_len) {
536
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
615
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
537
                    _sqlite_error(res);
616
                    _sqlite_error(res);
538
 
617
 
539
                    if (funcname[0] == '%') {
618
                    if (funcname[0] == '%') {
Line 562... Line 641...
562
 
641
 
563
                    /* recycle on svec if we loop again */
642
                    /* recycle on svec if we loop again */
564
                    sqlite3_finalize(stmt2);
643
                    sqlite3_finalize(stmt2);
565
                }
644
                }
566
 
645
 
-
 
646
                sqlite3_finalize(stmt);
567
                _sqlite_exec("commit");
647
                _sqlite_commit;
568
            }
648
            }
569
 
649
 
570
            sqlite3_finalize(stmt);
-
 
571
        } else if (IS_INTEGER(op2) || IS_LOGICAL(op2)) { /* I N T E G E R */
650
        } else if (IS_INTEGER(op2) || IS_LOGICAL(op2)) { /* I N T E G E R */
572
            /* we are taking advantage of the fact that logicals are stored as int.
651
            /* we are taking advantage of the fact that logicals are stored as int.
573
             * if this becomes untrue in the future, then this is a bug */
652
             * if this becomes untrue in the future, then this is a bug */
574
            op2_len = LENGTH(op2);
653
            op2_len = LENGTH(op2);
575
 
654
 
Line 593... Line 672...
593
 
672
 
594
                if (funcname[0] == '%') {
673
                if (funcname[0] == '%') {
595
                    /* sqlite does int div with "/" if both args are int. mod is % also.
674
                    /* sqlite does int div with "/" if both args are int. mod is % also.
596
                     * fortunately, in both cases the sqlite op is 2nd char of funcname */
675
                     * fortunately, in both cases the sqlite op is 2nd char of funcname */
597
                    sprintf(g_sql_buf[2], insert_fmt_string2c, iname, funcname[1]);  
676
                    sprintf(g_sql_buf[2], insert_fmt_string2c, iname, funcname[1]);  
598
                    _sqlite_exec("begin");
677
                    _sqlite_begin;
599
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
678
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
600
                    _sqlite_error(res);
679
                    _sqlite_error(res);
601
 
680
 
602
                    for (i = 0; i < svec_len; i++) {
681
                    for (i = 0; i < svec_len; i++) {
603
                        sqlite3_step(stmt2);
682
                        sqlite3_step(stmt2);
Line 606... Line 685...
606
                        sqlite3_bind_text(stmt, 1, (char *)sqlite3_column_text(stmt2, 0), -1, SQLITE_STATIC);
685
                        sqlite3_bind_text(stmt, 1, (char *)sqlite3_column_text(stmt2, 0), -1, SQLITE_STATIC);
607
                        sqlite3_bind_int(stmt, 2, sqlite3_column_int(stmt2, 1));
686
                        sqlite3_bind_int(stmt, 2, sqlite3_column_int(stmt2, 1));
608
                        sqlite3_bind_int(stmt, 3, INTEGER(op2)[i % op2_len]);
687
                        sqlite3_bind_int(stmt, 3, INTEGER(op2)[i % op2_len]);
609
                        sqlite3_step(stmt);
688
                        sqlite3_step(stmt);
610
                    }
689
                    }
611
                    _sqlite_exec("commit");
690
                    _sqlite_commit;
612
                } else {
691
                } else {
613
                    sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
692
                    sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
614
                    _sqlite_exec("begin");
693
                    _sqlite_begin;
615
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
694
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
616
                    _sqlite_error(res);
695
                    _sqlite_error(res);
617
 
696
 
618
                    for (i = 0; i < svec_len; i++) {
697
                    for (i = 0; i < svec_len; i++) {
619
                        sqlite3_step(stmt2);
698
                        sqlite3_step(stmt2);
Line 622... Line 701...
622
                        sqlite3_bind_text(stmt, 1, (char *)sqlite3_column_text(stmt2, 0), -1, SQLITE_STATIC);
701
                        sqlite3_bind_text(stmt, 1, (char *)sqlite3_column_text(stmt2, 0), -1, SQLITE_STATIC);
623
                        sqlite3_bind_double(stmt, 2, sqlite3_column_double(stmt2, 1));
702
                        sqlite3_bind_double(stmt, 2, sqlite3_column_double(stmt2, 1));
624
                        sqlite3_bind_double(stmt, 3, (double)INTEGER(op2)[i % op2_len]);
703
                        sqlite3_bind_double(stmt, 3, (double)INTEGER(op2)[i % op2_len]);
625
                        sqlite3_step(stmt);
704
                        sqlite3_step(stmt);
626
                    }
705
                    }
627
                    _sqlite_exec("commit");
706
                    _sqlite_commit;
628
                }
707
                }
629
                sqlite3_finalize(stmt2);
708
                sqlite3_finalize(stmt2);
630
            } else {
709
            } else {
631
                sprintf(g_sql_buf[1], "select [%s] from [%s].sdf_data",
710
                sprintf(g_sql_buf[1], "select [%s] from [%s].sdf_data",
632
                        varname_src, iname_src);
711
                        varname_src, iname_src);
Line 639... Line 718...
639
                    sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
718
                    sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
640
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
719
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
641
                    _sqlite_error(res);
720
                    _sqlite_error(res);
642
                }
721
                }
643
               
722
               
644
                i = 0; _sqlite_exec("begin");
723
                i = 0; _sqlite_begin; 
645
                while (i < op2_len) {
724
                while (i < op2_len) {
646
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
725
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
647
                    _sqlite_error(res);
726
                    _sqlite_error(res);
648
 
727
 
649
                    if (funcname[0] == '%') {
728
                    if (funcname[0] == '%') {
Line 671... Line 750...
671
                    }
750
                    }
672
 
751
 
673
                    /* recycle on svec if we loop again */
752
                    /* recycle on svec if we loop again */
674
                    sqlite3_finalize(stmt2);
753
                    sqlite3_finalize(stmt2);
675
                }
754
                }
676
                _sqlite_exec("commit");
755
                _sqlite_commit;
677
            }
756
            }
678
 
757
 
679
            sqlite3_finalize(stmt);
758
            sqlite3_finalize(stmt);
680
        } else if (inherits(op2, "sqlite.vector")) { 
759
        } else if (inherits(op2, "sqlite.vector")) { 
681
            /* op2 is surely not a factor, as handled by the R wrapper */
760
            /* op2 is surely not a factor, as handled by the R wrapper */
682
            char *iname_op2, *varname_op2;
761
            char *iname_op2, *varname_op2;
683
            sqlite3_stmt *stmt3;
762
            sqlite3_stmt *stmt3;
684
            iname_op2 = SDF_INAME(op2);
763
            iname_op2 = SDF_INAME(op2);
685
            varname_op2 = SVEC_VARNAME(op2);
764
            varname_op2 = SVEC_VARNAME(op2);
686
 
765
 
687
            if (!USE_SDF1(iname_op2, TRUE)) {
766
            if (!USE_SDF1(iname_op2, TRUE, TRUE)) {
688
                /* delete created sqlite.vector */
767
                /* delete created sqlite.vector */
689
                Rprintf("Warning: detaching created result SDF %s\n", iname);
768
                Rprintf("Warning: detaching created result SDF %s\n", iname);
690
                sdf_detach_sdf(mkString(iname));
769
                sdf_detach_sdf(mkString(iname));
691
                return R_NilValue;
770
                return R_NilValue;
692
            }
771
            }
-
 
772
            _sqlite_begin;
693
 
773
 
694
            op2_len = _get_row_count2(iname_op2, 1);
774
            op2_len = _get_row_count2(iname_op2, 1);
695
            
775
            
696
            sprintf(g_sql_buf[2], "select [%s] from [%s].sdf_data", varname_src, iname_src);
776
            sprintf(g_sql_buf[2], "select [%s] from [%s].sdf_data", varname_src, iname_src);
697
            res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
777
            res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
Line 701... Line 781...
701
            res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt3, 0);
781
            res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt3, 0);
702
            _sqlite_error(res);
782
            _sqlite_error(res);
703
 
783
 
704
            if (funcname[0] == '%') {
784
            if (funcname[0] == '%') {
705
                sprintf(g_sql_buf[2], insert_fmt_string2c, iname, funcname[1]);
785
                sprintf(g_sql_buf[2], insert_fmt_string2c, iname, funcname[1]);
706
                _sqlite_exec("begin");
-
 
707
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
786
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
708
                _sqlite_error(res);
787
                _sqlite_error(res);
709
 
788
 
710
                if (svec_len == op2_len) {
789
                if (svec_len == op2_len) {
711
                    for (i = 0; i < svec_len; i++) {
790
                    for (i = 0; i < svec_len; i++) {
Line 744... Line 823...
744
                            sqlite3_step(stmt);
823
                            sqlite3_step(stmt);
745
                        }
824
                        }
746
                        sqlite3_reset(stmt3);
825
                        sqlite3_reset(stmt3);
747
                    }
826
                    }
748
                }
827
                }
749
                _sqlite_exec("commit");
828
                _sqlite_commit;
750
            } else { /* not an integer op %% or %/% */
829
            } else { /* not an integer op %% or %/% */
751
                sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
830
                sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
752
                _sqlite_exec("begin");
-
 
753
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
831
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
754
                _sqlite_error(res);
832
                _sqlite_error(res);
755
 
833
 
756
                if (svec_len == op2_len) {
834
                if (svec_len == op2_len) {
757
                    for (i = 0; i < svec_len; i++) {
835
                    for (i = 0; i < svec_len; i++) {
Line 790... Line 868...
790
                            sqlite3_step(stmt);
868
                            sqlite3_step(stmt);
791
                        }
869
                        }
792
                        sqlite3_reset(stmt3);
870
                        sqlite3_reset(stmt3);
793
                    }
871
                    }
794
                }
872
                }
795
                _sqlite_exec("commit");
-
 
796
            }
873
            }
797
 
874
 
798
            sqlite3_finalize(stmt);
875
            sqlite3_finalize(stmt);
799
            sqlite3_finalize(stmt2);
876
            sqlite3_finalize(stmt2);
800
            sqlite3_finalize(stmt3);
877
            sqlite3_finalize(stmt3);
-
 
878
            _sqlite_commit;
-
 
879
 
-
 
880
            UNUSE_SDF2(iname_op2);
801
        }
881
        }
802
    } else if (functype == 1 && funcname[0] != '!') { /* unary not operator */
882
    } else if (functype == 1 && funcname[0] != '!') { /* unary not operator */
803
        iname = _create_svector1(R_NilValue, "bit", NULL);
883
        iname = _create_svector1(R_NilValue, "bit", NULL, TRUE);
804
        sprintf(g_sql_buf[2], "insert into [%s].sdf_data "
884
        sprintf(g_sql_buf[2], "insert into [%s].sdf_data "
805
                    "select [row name], [%s] == 0 from [%s].sdf_data", 
885
                    "select [row name], [%s] == 0 from [%s].sdf_data", 
806
                    iname, varname_src, iname_src);
886
                    iname, varname_src, iname_src);
807
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
887
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
808
        _sqlite_error(res);
888
        _sqlite_error(res);
809
        sqlite3_step(stmt);
889
        sqlite3_step(stmt);
810
        sqlite3_finalize(stmt);
890
        sqlite3_finalize(stmt);
811
    }
891
    }
812
 
892
 
813
    if (iname != NULL)
893
    if (iname != NULL) {
814
        return _create_svector_sexp(iname, "V1", 
894
        return _create_svector_sexp(iname, "V1", 
815
                (functype == 0) ? "numeric" : "logical");
895
                (functype == 0) ? "numeric" : "logical");
-
 
896
        UNUSE_SDF2(iname);
-
 
897
    }
-
 
898
 
-
 
899
    UNUSE_SDF2(iname_src);
816
 
900
 
817
    return R_NilValue;
901
    return R_NilValue;
818
 
902
 
819
}
903
}
820
 
904
 
Line 827... Line 911...
827
    /* get data from arguments (function name and sqlite.vector stuffs) */
911
    /* get data from arguments (function name and sqlite.vector stuffs) */
828
    funcname = CHAR_ELT(func, 0);
912
    funcname = CHAR_ELT(func, 0);
829
    iname_src = SDF_INAME(vector);
913
    iname_src = SDF_INAME(vector);
830
    varname_src = SVEC_VARNAME(vector);
914
    varname_src = SVEC_VARNAME(vector);
831
 
915
 
832
    if (!USE_SDF1(iname_src, TRUE)) return R_NilValue;
916
    if (!USE_SDF1(iname_src, TRUE, FALSE)) return R_NilValue;
833
 
917
 
834
    g_narm = LOGICAL(na_rm)[0];
918
    g_narm = LOGICAL(na_rm)[0];
835
    if (strcmp(funcname, "range") == 0) {
919
    if (strcmp(funcname, "range") == 0) {
836
        /* special handling for range. use min then max */
920
        /* special handling for range. use min then max */
837
        g_start = 1;
921
        g_start = 1;
Line 889... Line 973...
889
    int res;
973
    int res;
890
 
974
 
891
    iname_src = SDF_INAME(svec);
975
    iname_src = SDF_INAME(svec);
892
    varname_src = SVEC_VARNAME(svec);
976
    varname_src = SVEC_VARNAME(svec);
893
 
977
 
894
    if (!USE_SDF1(iname_src, TRUE)) return R_NilValue;
978
    if (!USE_SDF1(iname_src, TRUE, TRUE)) return R_NilValue;
895
 
979
 
896
    /* determine type of svec */
980
    /* determine type of svec */
897
    sprintf(g_sql_buf[0], "select [%s] from [%s].sdf_data limit 1", varname_src, iname_src);
981
    sprintf(g_sql_buf[0], "select [%s] from [%s].sdf_data limit 1", varname_src, iname_src);
898
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
982
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
899
    _sqlite_error(res);
983
    _sqlite_error(res);
900
    sqlite3_step(stmt);
984
    sqlite3_step(stmt);
901
    strcpy(g_sql_buf[0], sqlite3_column_decltype(stmt, 0));
985
    strcpy(g_sql_buf[0], sqlite3_column_decltype(stmt, 0));
902
    sqlite3_finalize(stmt);
986
    sqlite3_finalize(stmt);
903
 
987
 
904
    /* create a new vector of that type */
988
    /* create a new vector of that type */
905
    iname = _create_svector1(R_NilValue, g_sql_buf[0], NULL);
989
    iname = _create_svector1(mkString("tmp-sort"), g_sql_buf[0], NULL, TRUE);
906
 
990
 
907
    /* insert to new sdf ordered */
991
    /* insert to new sdf ordered */
908
    sprintf(g_sql_buf[0], "insert into [%s].sdf_data "
992
    sprintf(g_sql_buf[0], "insert into [%s].sdf_data "
909
            "select [row name], [%s] from [%s].sdf_data "
993
            "select [row name], [%s] from [%s].sdf_data "
910
            "order by [%s] %s", iname, varname_src, iname_src, varname_src,
994
            "order by [%s] %s", iname, varname_src, iname_src, varname_src,
Line 916... Line 1000...
916
        if (inherits(svec, "ordered")) type = "ordered";
1000
        if (inherits(svec, "ordered")) type = "ordered";
917
        else type = "factor";
1001
        else type = "factor";
918
        _copy_factor_levels2(type, iname_src, varname_src, iname, "V1");
1002
        _copy_factor_levels2(type, iname_src, varname_src, iname, "V1");
919
    } else type = CHAR_ELT(GET_CLASS(svec), 1);
1003
    } else type = CHAR_ELT(GET_CLASS(svec), 1);
920
 
1004
 
-
 
1005
    UNUSE_SDF2(iname_src);
-
 
1006
    UNUSE_SDF2(iname);
-
 
1007
 
921
    return _create_svector_sexp(iname, "V1", type);
1008
    return _create_svector_sexp(iname, "V1", type);
922
}
1009
}
923
 
1010
 
924
/****************************************************************************
1011
/****************************************************************************
925
 * VECTOR MATH/OPS/GROUP OPERATIONS
1012
 * VECTOR MATH/OPS/GROUP OPERATIONS