The R Project SVN R-packages

Rev

Rev 3471 | Rev 3684 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3471 Rev 3473
Line 410... Line 410...
410
 * from sdf_data". */
410
 * from sdf_data". */
411
static long double g_accumulator = 0.0; /* accumulator var for cumsum, cumprod, etc. */
411
static long double g_accumulator = 0.0; /* accumulator var for cumsum, cumprod, etc. */
412
static int g_start = 0;            /* flag for start of cummulation */
412
static int g_start = 0;            /* flag for start of cummulation */
413
static int g_narm = 0;             /* for Summary group */
413
static int g_narm = 0;             /* for Summary group */
414
 
414
 
-
 
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
 
415
SEXP sdf_do_variable_math(SEXP func, SEXP vector, SEXP other_args) {
420
SEXP sdf_do_variable_math(SEXP func, SEXP vector, SEXP other_args) {
416
    char *iname, *iname_src, *varname_src, *funcname;
421
    char *iname, *iname_src, *varname_src, *funcname;
417
    int namelen, res;
422
    int namelen, res;
418
    sqlite3_stmt *stmt;
423
    sqlite3_stmt *stmt;
419
 
424
 
Line 460... Line 465...
460
         * at R */
465
         * at R */
461
        iname[namelen] = '.';
466
        iname[namelen] = '.';
462
        return mkString(iname);
467
        return mkString(iname);
463
    }
468
    }
464
 
469
 
465
    g_accumulator = 0.0;   /* initialize accumulator */
470
    _init_sqlite_function_accumulator();
466
    g_start = 1;           /* flag that we are at start of accumulating */
-
 
467
    sqlite3_step(stmt);
471
    sqlite3_step(stmt);
468
    sqlite3_finalize(stmt);
472
    sqlite3_finalize(stmt);
469
 
473
 
470
    UNUSE_SDF2(iname);
474
    UNUSE_SDF2(iname);
471
    UNUSE_SDF2(iname_src);
475
    UNUSE_SDF2(iname_src);
Line 1008... Line 1012...
1008
    if (!USE_SDF1(iname_src, TRUE, FALSE)) return R_NilValue;
1012
    if (!USE_SDF1(iname_src, TRUE, FALSE)) return R_NilValue;
1009
 
1013
 
1010
    g_narm = LOGICAL(na_rm)[0];
1014
    g_narm = LOGICAL(na_rm)[0];
1011
    if (strcmp(funcname, "range") == 0) {
1015
    if (strcmp(funcname, "range") == 0) {
1012
        /* special handling for range. use min then max */
1016
        /* special handling for range. use min then max */
1013
        g_start = 1;
-
 
1014
        g_accumulator = 0.0;
1017
        _init_sqlite_function_accumulator();
1015
        sprintf(g_sql_buf[0], "select min_df([%s]) from [%s].sdf_data", varname_src, iname_src);
1018
        sprintf(g_sql_buf[0], "select min_df([%s]) from [%s].sdf_data", varname_src, iname_src);
1016
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
1019
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
1017
        if (_sqlite_error(res)) return R_NilValue;
1020
        if (_sqlite_error(res)) return R_NilValue;
1018
        sqlite3_step(stmt); sqlite3_finalize(stmt);
1021
        sqlite3_step(stmt); sqlite3_finalize(stmt);
1019
        _ret = g_accumulator;
1022
        _ret = g_accumulator;
Line 1022... Line 1025...
1022
            PROTECT(ret = NEW_NUMERIC(2));
1025
            PROTECT(ret = NEW_NUMERIC(2));
1023
            REAL(ret)[0] = REAL(ret)[1] = R_NaReal;
1026
            REAL(ret)[0] = REAL(ret)[1] = R_NaReal;
1024
            goto __sdf_do_variable_summary_out;
1027
            goto __sdf_do_variable_summary_out;
1025
        }
1028
        }
1026
        
1029
        
1027
        g_start = 1;
-
 
1028
        g_accumulator = 0.0;
1030
        _init_sqlite_function_accumulator();
1029
        sprintf(g_sql_buf[0], "select max_df([%s]) from [%s].sdf_data", varname_src, iname_src);
1031
        sprintf(g_sql_buf[0], "select max_df([%s]) from [%s].sdf_data", varname_src, iname_src);
1030
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
1032
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
1031
        if (_sqlite_error(res)) return R_NilValue;
1033
        if (_sqlite_error(res)) return R_NilValue;
1032
        sqlite3_step(stmt); sqlite3_finalize(stmt);
1034
        sqlite3_step(stmt); sqlite3_finalize(stmt);
1033
 
1035
 
1034
        /* if there is NA, then the if above should have caught it already */
1036
        /* if there is NA, then the if above should have caught it already */
1035
        PROTECT(ret = NEW_NUMERIC(2));
1037
        PROTECT(ret = NEW_NUMERIC(2));
1036
        REAL(ret)[0] = _ret;
1038
        REAL(ret)[0] = _ret;
1037
        REAL(ret)[1] = g_accumulator;
1039
        REAL(ret)[1] = g_accumulator;
1038
    } else {
1040
    } else {
1039
        g_start = 1;  /* we'll use these instead of sqlite3_aggregate_context */
-
 
1040
        g_accumulator = 0.0;
1041
        _init_sqlite_function_accumulator();
1041
        sprintf(g_sql_buf[0], "select %s_df([%s]) from [%s].sdf_data", funcname, varname_src, iname_src);
1042
        sprintf(g_sql_buf[0], "select %s_df([%s]) from [%s].sdf_data", funcname, varname_src, iname_src);
1042
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
1043
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
1043
        if (_sqlite_error(res)) return R_NilValue;
1044
        if (_sqlite_error(res)) return R_NilValue;
1044
        res = sqlite3_step(stmt); sqlite3_finalize(stmt);
1045
        res = sqlite3_step(stmt); sqlite3_finalize(stmt);
1045
 
1046