The R Project SVN R-packages

Rev

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

Rev 3808 Rev 3855
Line 1030... Line 1030...
1030
 
1030
 
1031
SEXP sdf_do_variable_summary(SEXP func, SEXP vector, SEXP na_rm) {
1031
SEXP sdf_do_variable_summary(SEXP func, SEXP vector, SEXP na_rm) {
1032
    char *iname_src, *varname_src, *funcname;
1032
    char *iname_src, *varname_src, *funcname;
1033
    int res;
1033
    int res;
1034
    sqlite3_stmt *stmt;
1034
    sqlite3_stmt *stmt;
1035
    double _ret = NA_REAL; SEXP ret;
1035
    double _ret = NA_REAL, _ret2; SEXP ret;
1036
 
1036
 
1037
    /* get data from arguments (function name and sqlite.vector stuffs) */
1037
    /* get data from arguments (function name and sqlite.vector stuffs) */
1038
    funcname = CHAR_ELT(func, 0);
1038
    funcname = CHAR_ELT(func, 0);
1039
    iname_src = SDF_INAME(vector);
1039
    iname_src = SDF_INAME(vector);
1040
    varname_src = SVEC_VARNAME(vector);
1040
    varname_src = SVEC_VARNAME(vector);
Line 1046... Line 1046...
1046
        /* special handling for range. use min then max */
1046
        /* special handling for range. use min then max */
1047
        _init_sqlite_function_accumulator();
1047
        _init_sqlite_function_accumulator();
1048
        sprintf(g_sql_buf[0], "select min_df([%s]) from [%s].sdf_data", varname_src, iname_src);
1048
        sprintf(g_sql_buf[0], "select min_df([%s]) from [%s].sdf_data", varname_src, iname_src);
1049
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
1049
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
1050
        if (_sqlite_error(res)) return R_NilValue;
1050
        if (_sqlite_error(res)) return R_NilValue;
1051
        sqlite3_step(stmt); sqlite3_finalize(stmt);
1051
        sqlite3_step(stmt); 
1052
        _ret = g_accumulator;
1052
        _ret = sqlite3_column_double(stmt, 0);
-
 
1053
        sqlite3_finalize(stmt);
1053
 
1054
 
1054
        if (R_IsNA(_ret) && !g_narm) {
1055
        if (R_IsNA(_ret) && !g_narm) {
1055
            PROTECT(ret = NEW_NUMERIC(2));
1056
            PROTECT(ret = NEW_NUMERIC(2));
1056
            REAL(ret)[0] = REAL(ret)[1] = R_NaReal;
1057
            REAL(ret)[0] = REAL(ret)[1] = R_NaReal;
1057
            goto __sdf_do_variable_summary_out;
1058
            goto __sdf_do_variable_summary_out;
Line 1059... Line 1060...
1059
        
1060
        
1060
        _init_sqlite_function_accumulator();
1061
        _init_sqlite_function_accumulator();
1061
        sprintf(g_sql_buf[0], "select max_df([%s]) from [%s].sdf_data", varname_src, iname_src);
1062
        sprintf(g_sql_buf[0], "select max_df([%s]) from [%s].sdf_data", varname_src, iname_src);
1062
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
1063
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
1063
        if (_sqlite_error(res)) return R_NilValue;
1064
        if (_sqlite_error(res)) return R_NilValue;
-
 
1065
        sqlite3_step(stmt); 
-
 
1066
        _ret2 = sqlite3_column_double(stmt, 0);
1064
        sqlite3_step(stmt); sqlite3_finalize(stmt);
1067
        sqlite3_finalize(stmt);
1065
 
1068
 
1066
        /* if there is NA, then the if above should have caught it already */
1069
        /* if there is NA, then the if above should have caught it already */
1067
        PROTECT(ret = NEW_NUMERIC(2));
1070
        PROTECT(ret = NEW_NUMERIC(2));
1068
        REAL(ret)[0] = _ret;
1071
        REAL(ret)[0] = _ret;
1069
        REAL(ret)[1] = g_accumulator;
1072
        REAL(ret)[1] = _ret2;
1070
    } else {
1073
    } else {
1071
        _init_sqlite_function_accumulator();
1074
        _init_sqlite_function_accumulator();
1072
        sprintf(g_sql_buf[0], "select %s_df([%s]) from [%s].sdf_data", funcname, varname_src, iname_src);
1075
        sprintf(g_sql_buf[0], "select %s_df([%s]) from [%s].sdf_data", funcname, varname_src, iname_src);
1073
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
1076
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
1074
        if (_sqlite_error(res)) return R_NilValue;
1077
        if (_sqlite_error(res)) return R_NilValue;
1075
        res = sqlite3_step(stmt); sqlite3_finalize(stmt);
1078
        res = sqlite3_step(stmt); 
-
 
1079
        _ret = sqlite3_column_double(stmt, 0);
-
 
1080
        sqlite3_finalize(stmt);
1076
 
1081
 
1077
        if (strcmp(funcname, "all") == 0 || strcmp(funcname, "any") == 0) {
1082
        if (strcmp(funcname, "all") == 0 || strcmp(funcname, "any") == 0) {
1078
            PROTECT(ret = NEW_LOGICAL(1));
1083
            PROTECT(ret = NEW_LOGICAL(1));
1079
            if (R_IsNA(g_accumulator)) LOGICAL(ret)[0] = NA_INTEGER;
1084
            if (R_IsNA(_ret)) LOGICAL(ret)[0] = NA_INTEGER;
1080
            else LOGICAL(ret)[0] = !(g_accumulator == 0);
1085
            else LOGICAL(ret)[0] = (_ret != 0);
1081
        } else {
1086
        } else {
1082
            PROTECT(ret = NEW_NUMERIC(1));
1087
            PROTECT(ret = NEW_NUMERIC(1));
1083
            REAL(ret)[0] = g_accumulator;
1088
            REAL(ret)[0] = _ret;
1084
        }
1089
        }
1085
    }
1090
    }
1086
 
1091
 
1087
__sdf_do_variable_summary_out:
1092
__sdf_do_variable_summary_out:
1088
    UNPROTECT(1);
1093
    UNPROTECT(1);
Line 1157... Line 1162...
1157
}
1162
}
1158
 
1163
 
1159
/****************************************************************************
1164
/****************************************************************************
1160
 * VECTOR MATH/OPS/GROUP OPERATIONS
1165
 * VECTOR MATH/OPS/GROUP OPERATIONS
1161
 ****************************************************************************/
1166
 ****************************************************************************/
-
 
1167
struct accumulator_t {
-
 
1168
    /* long double acts weird here, but if it's not long double,
-
 
1169
     * it f*cks up sum() */
-
 
1170
    long double accumulator;
-
 
1171
    int started;
-
 
1172
};
1162
 
1173
 
1163
R_INLINE int __vecmath_checkarg(sqlite3_context *ctx, sqlite3_value *arg, double *value) {
1174
R_INLINE int __vecmath_checkarg(sqlite3_context *ctx, sqlite3_value *arg, double *value) {
1164
    int ret = 1;
1175
    int ret = 1;
1165
    if (sqlite3_value_type(arg) == SQLITE_NULL) { 
1176
    if (sqlite3_value_type(arg) == SQLITE_NULL) { 
1166
        sqlite3_result_null(ctx); 
1177
        sqlite3_result_null(ctx); 
Line 1228... Line 1239...
1228
SQLITE_MATH_FUNC1(gamma, gammafn) /* in R */
1239
SQLITE_MATH_FUNC1(gamma, gammafn) /* in R */
1229
/* SQLITE_MATH_FUNC1(gammaCody, gammaCody)   * in R ?? */
1240
/* SQLITE_MATH_FUNC1(gammaCody, gammaCody)   * in R ?? */
1230
SQLITE_MATH_FUNC1(digamma, digamma) /* in R */    
1241
SQLITE_MATH_FUNC1(digamma, digamma) /* in R */    
1231
SQLITE_MATH_FUNC1(trigamma, trigamma) /* in R */
1242
SQLITE_MATH_FUNC1(trigamma, trigamma) /* in R */
1232
 
1243
 
1233
#define SUM(a, b)  (a) + (b)
1244
#define SUM(a, b)  ((long double)(a) + (b))
1234
#define PROD(a, b) (a) * (b)
1245
#define PROD(a, b) (a) * (b)
1235
#define MIN(a, b) ((a) <= (b)) ? (a) : (b)
1246
#define MIN(a, b) ((a) <= (b)) ? (a) : (b)
1236
#define MAX(a, b) ((a) >= (b)) ? (a) : (b)
1247
#define MAX(a, b) ((a) >= (b)) ? (a) : (b)
1237
#define ALL(a, b) (((a) == 0) || ((b) == 0)) ? 0 : 1
1248
#define ALL(a, b) (((a) == 0) || ((b) == 0)) ? 0 : 1
1238
#define ANY(a, b) (((a) == 0) && ((b) == 0)) ? 0 : 1
1249
#define ANY(a, b) (((a) == 0) && ((b) == 0)) ? 0 : 1
Line 1243... Line 1254...
1243
SQLITE_MATH_FUNC_CUM(cummax, MAX)
1254
SQLITE_MATH_FUNC_CUM(cummax, MAX)
1244
 
1255
 
1245
#define SQLITE_SUMMARY_FUNC(name, func) static void __vecsummary_ ## name(\
1256
#define SQLITE_SUMMARY_FUNC(name, func) static void __vecsummary_ ## name(\
1246
        sqlite3_context *ctx, int argc, sqlite3_value **argv) { \
1257
        sqlite3_context *ctx, int argc, sqlite3_value **argv) { \
1247
    double value; \
1258
    double value; \
-
 
1259
    struct accumulator_t *acc; \
-
 
1260
    acc = sqlite3_aggregate_context(ctx, sizeof(struct accumulator_t)); \
1248
    if (!g_narm && R_IsNA(g_accumulator)) return; /* NA if na.rm=F & NA found */ \
1261
    if (!g_narm && R_IsNA(acc->accumulator)) return; /* NA if na.rm=F & NA found */ \
1249
    if (sqlite3_value_type(argv[0]) != SQLITE_NULL) {  \
1262
    if (sqlite3_value_type(argv[0]) != SQLITE_NULL) {  \
1250
        if (sqlite3_value_type(argv[0]) == SQLITE_INTEGER) {  \
1263
        if (sqlite3_value_type(argv[0]) == SQLITE_INTEGER) {  \
1251
            int tmp = sqlite3_value_int(argv[0]); \
1264
            int tmp = sqlite3_value_int(argv[0]); \
1252
            value = (tmp == NA_INTEGER) ? R_NaReal : tmp; \
1265
            value = (tmp == NA_INTEGER) ? R_NaReal : tmp; \
1253
        } else value = sqlite3_value_double(argv[0]);  \
1266
        } else value = sqlite3_value_double(argv[0]);  \
1254
        if (R_IsNA(value)) { \
1267
        if (R_IsNA(value)) { \
1255
            if (!g_narm) g_accumulator = value; return; \
1268
            if (!g_narm) acc->accumulator = value; return; /* else ignore */ \
-
 
1269
        } else if (!acc->started) { \
-
 
1270
            acc->started = 1; \
1256
        } else if (g_start) { g_start = 0; g_accumulator = value; } \
1271
            acc->accumulator = value; \
1257
        else g_accumulator = func(g_accumulator, value); \
1272
        } else acc->accumulator = func(acc->accumulator, value); \
1258
    } \
1273
    } \
1259
}
1274
}
1260
 
1275
 
1261
SQLITE_SUMMARY_FUNC(all_df, ALL)
1276
SQLITE_SUMMARY_FUNC(all_df, ALL)
1262
SQLITE_SUMMARY_FUNC(any_df, ANY)
1277
SQLITE_SUMMARY_FUNC(any_df, ANY)
Line 1265... Line 1280...
1265
SQLITE_SUMMARY_FUNC(min_df, MIN)
1280
SQLITE_SUMMARY_FUNC(min_df, MIN)
1266
SQLITE_SUMMARY_FUNC(max_df, MAX)
1281
SQLITE_SUMMARY_FUNC(max_df, MAX)
1267
 
1282
 
1268
static void __vecsummary_finalize(sqlite3_context *ctx) {
1283
static void __vecsummary_finalize(sqlite3_context *ctx) {
1269
    /* g_accumulator already summarizes it. just return that */
1284
    /* g_accumulator already summarizes it. just return that */
-
 
1285
    struct accumulator_t *acc = (struct accumulator_t *)
-
 
1286
                sqlite3_aggregate_context(ctx, sizeof(struct accumulator_t)); 
1270
    sqlite3_result_double(ctx, g_accumulator);
1287
    sqlite3_result_double(ctx, acc->accumulator);
1271
}
1288
}
1272
 
1289
 
1273
static void __r_modulo(sqlite3_context *ctx, int argc, sqlite3_value **argv) {
1290
static void __r_modulo(sqlite3_context *ctx, int argc, sqlite3_value **argv) {
1274
    if (sqlite3_value_type(argv[0]) == SQLITE_NULL ||
1291
    if (sqlite3_value_type(argv[0]) == SQLITE_NULL ||
1275
        sqlite3_value_type(argv[1]) == SQLITE_NULL) { 
1292
        sqlite3_value_type(argv[1]) == SQLITE_NULL) {