The R Project SVN R-packages

Rev

Rev 3284 | Rev 3308 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3284 Rev 3307
1
#include "sqlite_dataframe.h"
1
#include "sqlite_dataframe.h"
-
 
2
#include <math.h>
-
 
3
#include "Rmath.h"
2
 
4
 
3
SEXP sdf_get_variable(SEXP sdf, SEXP name) {
5
SEXP sdf_get_variable(SEXP sdf, SEXP name) {
4
    if (!IS_CHARACTER(name)) {
6
    if (!IS_CHARACTER(name)) {
5
        Rprintf("ERROR: argument is not a string.\n");
7
        Rprintf("ERROR: argument is not a string.\n");
6
        return R_NilValue;
8
        return R_NilValue;
7
    }
9
    }
8
 
10
 
9
    char *iname = SDF_INAME(sdf);
11
    char *iname = SDF_INAME(sdf);
10
    char *varname = CHAR_ELT(name, 0);
12
    char *varname = CHAR_ELT(name, 0);
11
 
13
 
12
    /* check if sdf & varname w/in that sdf exists */
14
    /* check if sdf & varname w/in that sdf exists */
13
    sqlite3_stmt *stmt;
15
    sqlite3_stmt *stmt;
14
    sprintf(g_sql_buf[0], "select [%s] from [%s].sdf_data", varname, iname);
16
    sprintf(g_sql_buf[0], "select [%s] from [%s].sdf_data", varname, iname);
15
 
17
 
16
    int res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
18
    int res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
17
 
19
 
18
    if (_sqlite_error(res)) return R_NilValue;
20
    if (_sqlite_error(res)) return R_NilValue;
19
 
21
 
20
    const char *coltype = sqlite3_column_decltype(stmt, 0);
22
    const char *coltype = sqlite3_column_decltype(stmt, 0);
21
    sqlite3_finalize(stmt);
23
    sqlite3_finalize(stmt);
22
 
24
 
23
 
25
 
24
    SEXP ret, value, class = R_NilValue; int nprotected = 0;
26
    SEXP ret, value, class = R_NilValue; int nprotected = 0;
25
    PROTECT(ret = NEW_LIST(2)); nprotected++;
27
    PROTECT(ret = NEW_LIST(2)); nprotected++;
26
 
28
 
27
    /* set list names */
29
    /* set list names */
28
    PROTECT(value = NEW_CHARACTER(2)); nprotected++;
30
    PROTECT(value = NEW_CHARACTER(2)); nprotected++;
29
    SET_STRING_ELT(value, 0, mkChar("iname"));
31
    SET_STRING_ELT(value, 0, mkChar("iname"));
30
    SET_STRING_ELT(value, 1, mkChar("varname"));
32
    SET_STRING_ELT(value, 1, mkChar("varname"));
31
    SET_NAMES(ret, value);
33
    SET_NAMES(ret, value);
32
 
34
 
33
    /* set list values */
35
    /* set list values */
34
    SET_VECTOR_ELT(ret, 0, mkString(iname));
36
    SET_VECTOR_ELT(ret, 0, mkString(iname));
35
    SET_VECTOR_ELT(ret, 1, mkString(varname));
37
    SET_VECTOR_ELT(ret, 1, mkString(varname));
36
 
38
 
37
    /* set class */
39
    /* set class */
38
    int type = -1;
40
    int type = -1;
39
    if (strcmp(coltype, "text") == 0) class = mkChar("character");
41
    if (strcmp(coltype, "text") == 0) class = mkChar("character");
40
    else if (strcmp(coltype, "double") == 0) class = mkChar("numeric");
42
    else if (strcmp(coltype, "double") == 0) class = mkChar("numeric");
41
    else if (strcmp(coltype, "bit") == 0) class = mkChar("logical");
43
    else if (strcmp(coltype, "bit") == 0) class = mkChar("logical");
42
    else if (strcmp(coltype, "integer") == 0 || strcmp(coltype, "int") == 0) {
44
    else if (strcmp(coltype, "integer") == 0 || strcmp(coltype, "int") == 0) {
43
        /* determine if int, factor or ordered */
45
        /* determine if int, factor or ordered */
44
        type = _get_factor_levels1(iname, varname, ret);
46
        type = _get_factor_levels1(iname, varname, ret);
45
        switch(type) {
47
        switch(type) {
46
            case VAR_INTEGER: class = mkChar("numeric"); break;
48
            case VAR_INTEGER: class = mkChar("integer"); break;
47
            case VAR_FACTOR: class = mkChar("factor"); break;
49
            case VAR_FACTOR: class = mkChar("factor"); break;
48
            case VAR_ORDERED: class = mkChar("ordered");
50
            case VAR_ORDERED: class = mkChar("ordered");
49
        }
51
        }
50
 
52
 
51
    }
53
    }
52
 
54
 
53
    if (type != VAR_ORDERED) {
55
    if (type != VAR_ORDERED) {
54
        PROTECT(value = NEW_CHARACTER(2)); nprotected++;
56
        PROTECT(value = NEW_CHARACTER(2)); nprotected++;
55
        SET_STRING_ELT(value, 0, mkChar("sqlite.vector"));
57
        SET_STRING_ELT(value, 0, mkChar("sqlite.vector"));
56
        SET_STRING_ELT(value, 1, class);
58
        SET_STRING_ELT(value, 1, class);
57
    } else {
59
    } else {
58
        PROTECT(value = NEW_CHARACTER(3)); nprotected++;
60
        PROTECT(value = NEW_CHARACTER(3)); nprotected++;
59
        SET_STRING_ELT(value, 0, mkChar("sqlite.vector"));
61
        SET_STRING_ELT(value, 0, mkChar("sqlite.vector"));
60
        SET_STRING_ELT(value, 1, class);
62
        SET_STRING_ELT(value, 1, class);
61
        SET_STRING_ELT(value, 2, mkChar("factor"));
63
        SET_STRING_ELT(value, 2, mkChar("factor"));
62
    }
64
    }
63
    SET_CLASS(ret, value);
65
    SET_CLASS(ret, value);
64
 
66
 
65
    UNPROTECT(nprotected);
67
    UNPROTECT(nprotected);
66
    return ret;
68
    return ret;
67
 
69
 
68
}
70
}
69
 
71
 
70
int _get_vector_index_typed_result(sqlite3_stmt *stmt, SEXP *ret, int idx_or_len) {
72
int _get_vector_index_typed_result(sqlite3_stmt *stmt, SEXP *ret, int idx_or_len) {
71
    int added = 1;
73
    int added = 1;
72
    if (*ret == NULL || *ret == R_NilValue) {
74
    if (*ret == NULL || *ret == R_NilValue) {
73
        const char *coltype = sqlite3_column_decltype(stmt, 0);
75
        const char *coltype = sqlite3_column_decltype(stmt, 0);
74
        if (sqlite3_column_type(stmt, 0) == SQLITE_NULL) {
76
        if (sqlite3_column_type(stmt, 0) == SQLITE_NULL) {
75
            added = 0;
77
            added = 0;
76
        }
78
        }
77
        
79
        
78
        if (strcmp(coltype, "text") == 0) {
80
        if (strcmp(coltype, "text") == 0) {
79
            PROTECT(*ret = NEW_CHARACTER(idx_or_len));
81
            PROTECT(*ret = NEW_CHARACTER(idx_or_len));
80
            if (added) 
82
            if (added) 
81
                SET_STRING_ELT(*ret, 0, mkChar((char *)sqlite3_column_text(stmt, 0)));
83
                SET_STRING_ELT(*ret, 0, mkChar((char *)sqlite3_column_text(stmt, 0)));
82
        } else if (strcmp(coltype, "double") == 0) {
84
        } else if (strcmp(coltype, "double") == 0) {
83
            PROTECT(*ret = NEW_NUMERIC(idx_or_len));
85
            PROTECT(*ret = NEW_NUMERIC(idx_or_len));
84
            if (added) REAL(*ret)[0] = sqlite3_column_double(stmt, 0);
86
            if (added) REAL(*ret)[0] = sqlite3_column_double(stmt, 0);
85
        } else if (strcmp(coltype, "bit") == 0) {
87
        } else if (strcmp(coltype, "bit") == 0) {
86
            PROTECT(*ret = NEW_LOGICAL(idx_or_len));
88
            PROTECT(*ret = NEW_LOGICAL(idx_or_len));
87
            if (added) INTEGER(*ret)[0] = sqlite3_column_int(stmt, 0);
89
            if (added) INTEGER(*ret)[0] = sqlite3_column_int(stmt, 0);
88
        } else if (strcmp(coltype, "integer") == 0 || 
90
        } else if (strcmp(coltype, "integer") == 0 || 
89
                   strcmp(coltype, "int") == 0) {
91
                   strcmp(coltype, "int") == 0) {
90
            /* caller should just copy off the vars level attr for factors */
92
            /* caller should just copy off the vars level attr for factors */
91
            PROTECT(*ret = NEW_INTEGER(idx_or_len));
93
            PROTECT(*ret = NEW_INTEGER(idx_or_len));
92
            if (added) INTEGER(*ret)[0] = sqlite3_column_int(stmt, 0);
94
            if (added) INTEGER(*ret)[0] = sqlite3_column_int(stmt, 0);
93
        } else added = 0;
95
        } else added = 0;
94
 
96
 
95
        UNPROTECT(1);
97
        UNPROTECT(1);
96
    } else {
98
    } else {
97
        const char *coltype = sqlite3_column_decltype(stmt, 0);
99
        const char *coltype = sqlite3_column_decltype(stmt, 0);
98
        if (sqlite3_column_type(stmt, 0) == SQLITE_NULL) {
100
        if (sqlite3_column_type(stmt, 0) == SQLITE_NULL) {
99
            added = 0;
101
            added = 0;
100
        } else if (strcmp(coltype, "text") == 0) {
102
        } else if (strcmp(coltype, "text") == 0) {
101
            SET_STRING_ELT(*ret, idx_or_len, 
103
            SET_STRING_ELT(*ret, idx_or_len, 
102
                    mkChar((char *)sqlite3_column_text(stmt, 0)));
104
                    mkChar((char *)sqlite3_column_text(stmt, 0)));
103
        } else if (strcmp(coltype, "double") == 0) {
105
        } else if (strcmp(coltype, "double") == 0) {
104
            REAL(*ret)[idx_or_len] = sqlite3_column_double(stmt, 0);
106
            REAL(*ret)[idx_or_len] = sqlite3_column_double(stmt, 0);
105
        } else if (strcmp(coltype, "bit") == 0) {
107
        } else if (strcmp(coltype, "bit") == 0) {
106
            INTEGER(*ret)[idx_or_len] = sqlite3_column_int(stmt, 0);
108
            INTEGER(*ret)[idx_or_len] = sqlite3_column_int(stmt, 0);
107
        } else if (strcmp(coltype, "integer") == 0 ||
109
        } else if (strcmp(coltype, "integer") == 0 ||
108
                   strcmp(coltype, "int") == 0) {
110
                   strcmp(coltype, "int") == 0) {
109
            /* caller should just copy off the vars level attr for factors */
111
            /* caller should just copy off the vars level attr for factors */
110
            INTEGER(*ret)[idx_or_len] = sqlite3_column_int(stmt, 0);
112
            INTEGER(*ret)[idx_or_len] = sqlite3_column_int(stmt, 0);
111
        } else added = 0;
113
        } else added = 0;
112
    }
114
    }
113
        
115
        
114
    return added;
116
    return added;
115
}
117
}
116
 
118
 
117
SEXP sdf_get_variable_length(SEXP svec) {
119
SEXP sdf_get_variable_length(SEXP svec) {
118
    char *iname = SDF_INAME(svec);
120
    char *iname = SDF_INAME(svec);
119
    sprintf(g_sql_buf[0], "[%s].sdf_data", iname);
121
    sprintf(g_sql_buf[0], "[%s].sdf_data", iname);
120
 
122
 
121
    SEXP ret;
123
    SEXP ret;
122
    PROTECT(ret = NEW_INTEGER(1));
124
    PROTECT(ret = NEW_INTEGER(1));
123
    INTEGER(ret)[0] = _get_row_count2(g_sql_buf[0]);
125
    INTEGER(ret)[0] = _get_row_count2(g_sql_buf[0]);
124
    UNPROTECT(1);
126
    UNPROTECT(1);
125
    return ret;
127
    return ret;
126
}
128
}
127
 
129
 
128
    
130
    
129
SEXP sdf_get_variable_index(SEXP svec, SEXP idx) {
131
SEXP sdf_get_variable_index(SEXP svec, SEXP idx) {
130
    SEXP ret = R_NilValue, tmp;
132
    SEXP ret = R_NilValue, tmp;
131
    char *iname = SDF_INAME(svec), *varname = SVEC_VARNAME(svec);
133
    char *iname = SDF_INAME(svec), *varname = SVEC_VARNAME(svec);
132
    int index, idxlen, i, retlen=0, res;
134
    int index, idxlen, i, retlen=0, res;
133
 
135
 
134
    /* check if sdf exists */
136
    /* check if sdf exists */
135
    sqlite3_stmt *stmt;
137
    sqlite3_stmt *stmt;
136
    sprintf(g_sql_buf[0], "select [%s] from [%s].sdf_data", varname, iname);
138
    sprintf(g_sql_buf[0], "select [%s] from [%s].sdf_data", varname, iname);
137
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
139
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
138
    sqlite3_finalize(stmt);
140
    sqlite3_finalize(stmt);
139
    if (_sqlite_error(res)) { return ret; }
141
    if (_sqlite_error(res)) { return ret; }
140
 
142
 
141
    idxlen = LENGTH(idx);
143
    idxlen = LENGTH(idx);
142
    if (idxlen < 1) return ret;
144
    if (idxlen < 1) return ret;
143
 
145
 
144
    sprintf(g_sql_buf[0], "select [%s] from [%s].sdf_data limit ?,1",
146
    sprintf(g_sql_buf[0], "select [%s] from [%s].sdf_data limit ?,1",
145
            varname, iname);
147
            varname, iname);
146
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
148
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
147
 
149
 
148
    /* get data based on index */
150
    /* get data based on index */
149
    if (IS_NUMERIC(idx)) {
151
    if (IS_NUMERIC(idx)) {
150
        index = ((int) REAL(idx)[0]) - 1;
152
        index = ((int) REAL(idx)[0]) - 1;
151
        if (index < 0 && idxlen == 1) return ret;
153
        if (index < 0 && idxlen == 1) return ret;
152
 
154
 
153
        if (index >= 0) {
155
        if (index >= 0) {
154
            sqlite3_bind_int(stmt, 1, index);
156
            sqlite3_bind_int(stmt, 1, index);
155
            res = sqlite3_step(stmt);
157
            res = sqlite3_step(stmt);
156
            if (res == SQLITE_ROW) { 
158
            if (res == SQLITE_ROW) { 
157
                retlen = _get_vector_index_typed_result(stmt, &ret, idxlen);
159
                retlen = _get_vector_index_typed_result(stmt, &ret, idxlen);
158
            } 
160
            } 
159
        } 
161
        } 
160
        
162
        
161
        if (index < 0 || res != SQLITE_ROW) {
163
        if (index < 0 || res != SQLITE_ROW) {
162
            /* something wrong w/ 1st idx, and it is quietly ignored. we make 
164
            /* something wrong w/ 1st idx, and it is quietly ignored. we make 
163
             * a "dummy" call to setup the SEXP */
165
             * a "dummy" call to setup the SEXP */
164
            sqlite3_bind_int(stmt, 1, 0);
166
            sqlite3_bind_int(stmt, 1, 0);
165
            _get_vector_index_typed_result(stmt, &ret, idxlen - 1);
167
            _get_vector_index_typed_result(stmt, &ret, idxlen - 1);
166
            retlen = 0;
168
            retlen = 0;
167
        }
169
        }
168
 
170
 
169
        if (idxlen > 1) {
171
        if (idxlen > 1) {
170
            for (i = 1; i < idxlen; i++) {
172
            for (i = 1; i < idxlen; i++) {
171
                index = ((int) REAL(idx)[i]) - 1;
173
                index = ((int) REAL(idx)[i]) - 1;
172
                if (index < 0) continue;
174
                if (index < 0) continue;
173
                sqlite3_reset(stmt);
175
                sqlite3_reset(stmt);
174
                sqlite3_bind_int(stmt, 1, index);
176
                sqlite3_bind_int(stmt, 1, index);
175
                res = sqlite3_step(stmt);
177
                res = sqlite3_step(stmt);
176
                if (res == SQLITE_ROW)
178
                if (res == SQLITE_ROW)
177
                    retlen += _get_vector_index_typed_result(stmt, &ret, retlen); 
179
                    retlen += _get_vector_index_typed_result(stmt, &ret, retlen); 
178
            }
180
            }
179
        }
181
        }
180
    } else if (IS_INTEGER(idx)) {
182
    } else if (IS_INTEGER(idx)) {
181
        /* similar to REAL (IS_NUMERIC) above, except that we don't have
183
        /* similar to REAL (IS_NUMERIC) above, except that we don't have
182
         * to cast idx to int. can't refactor this out, sucks. */
184
         * to cast idx to int. can't refactor this out, sucks. */
183
        index = INTEGER(idx)[0] - 1;
185
        index = INTEGER(idx)[0] - 1;
184
        if (index < 0 && idxlen == 1) return ret;
186
        if (index < 0 && idxlen == 1) return ret;
185
 
187
 
186
        if (index >= 0) {
188
        if (index >= 0) {
187
            sqlite3_bind_int(stmt, 1, index);
189
            sqlite3_bind_int(stmt, 1, index);
188
            res = sqlite3_step(stmt);
190
            res = sqlite3_step(stmt);
189
            if (res == SQLITE_ROW) { 
191
            if (res == SQLITE_ROW) { 
190
                retlen = _get_vector_index_typed_result(stmt, &ret, idxlen);
192
                retlen = _get_vector_index_typed_result(stmt, &ret, idxlen);
191
            } 
193
            } 
192
        } 
194
        } 
193
        
195
        
194
        if (index < 0 || res != SQLITE_ROW) {
196
        if (index < 0 || res != SQLITE_ROW) {
195
            /* something wrong w/ 1st idx, and it is quietly ignored. we make 
197
            /* something wrong w/ 1st idx, and it is quietly ignored. we make 
196
             * a "dummy" call to setup the SEXP */
198
             * a "dummy" call to setup the SEXP */
197
            sqlite3_bind_int(stmt, 1, 0);
199
            sqlite3_bind_int(stmt, 1, 0);
198
            _get_vector_index_typed_result(stmt, &ret, idxlen - 1);
200
            _get_vector_index_typed_result(stmt, &ret, idxlen - 1);
199
            retlen = 0;
201
            retlen = 0;
200
        }
202
        }
201
 
203
 
202
        if (idxlen > 1) {
204
        if (idxlen > 1) {
203
            for (i = 1; i < idxlen; i++) {
205
            for (i = 1; i < idxlen; i++) {
204
                index = INTEGER(idx)[i] - 1;
206
                index = INTEGER(idx)[i] - 1;
205
                if (index < 0) continue;
207
                if (index < 0) continue;
206
                sqlite3_reset(stmt);
208
                sqlite3_reset(stmt);
207
                sqlite3_bind_int(stmt, 1, index);
209
                sqlite3_bind_int(stmt, 1, index);
208
                sqlite3_step(stmt);
210
                sqlite3_step(stmt);
209
                retlen += _get_vector_index_typed_result(stmt, &ret, retlen); 
211
                retlen += _get_vector_index_typed_result(stmt, &ret, retlen); 
210
            }
212
            }
211
        }
213
        }
212
 
214
 
213
    } else if (IS_LOGICAL(idx)) {
215
    } else if (IS_LOGICAL(idx)) {
214
        /* have to deal with recycling */
216
        /* have to deal with recycling */
215
        sprintf(g_sql_buf[0], "[%s].sdf_data", iname);
217
        sprintf(g_sql_buf[0], "[%s].sdf_data", iname);
216
        int veclen = _get_row_count2(g_sql_buf[0]);
218
        int veclen = _get_row_count2(g_sql_buf[0]);
217
 
219
 
218
        /* find if there is any TRUE element in the vector */
220
        /* find if there is any TRUE element in the vector */
219
        for (i = 0; i < idxlen && i < veclen; i++) {
221
        for (i = 0; i < idxlen && i < veclen; i++) {
220
            if (LOGICAL(idx)[i]) {
222
            if (LOGICAL(idx)[i]) {
221
                sqlite3_bind_int(stmt, 1, i);
223
                sqlite3_bind_int(stmt, 1, i);
222
                sqlite3_step(stmt);
224
                sqlite3_step(stmt);
223
                /* there are at least (idxlen-i) TRUE per cycle of the LOGICAL
225
                /* there are at least (idxlen-i) TRUE per cycle of the LOGICAL
224
                 * index. there are at least (veclen/idxlen) cycles (int div).
226
                 * index. there are at least (veclen/idxlen) cycles (int div).
225
                 * at the last cycle, if (veclen%idxlen > 0), there will be
227
                 * at the last cycle, if (veclen%idxlen > 0), there will be
226
                 * at least (veclen%idxlen - i) if veclen%idxlen > i */
228
                 * at least (veclen%idxlen - i) if veclen%idxlen > i */
227
                retlen = (idxlen-i) * (veclen/idxlen);
229
                retlen = (idxlen-i) * (veclen/idxlen);
228
                if (veclen%idxlen > i) retlen += (veclen%idxlen - i);
230
                if (veclen%idxlen > i) retlen += (veclen%idxlen - i);
229
 
231
 
230
                /* create the vector */
232
                /* create the vector */
231
                retlen = _get_vector_index_typed_result(stmt, &ret, retlen);
233
                retlen = _get_vector_index_typed_result(stmt, &ret, retlen);
232
                break;
234
                break;
233
            }
235
            }
234
        }
236
        }
235
 
237
 
236
        if (i < idxlen && i < veclen) {
238
        if (i < idxlen && i < veclen) {
237
            for (i++; i < veclen; i++) {
239
            for (i++; i < veclen; i++) {
238
                if (LOGICAL(idx)[i%idxlen]) {
240
                if (LOGICAL(idx)[i%idxlen]) {
239
                    sqlite3_reset(stmt);
241
                    sqlite3_reset(stmt);
240
                    sqlite3_bind_int(stmt, 1, i);
242
                    sqlite3_bind_int(stmt, 1, i);
241
                    sqlite3_step(stmt);
243
                    sqlite3_step(stmt);
242
                    retlen += _get_vector_index_typed_result(stmt, &ret, retlen);
244
                    retlen += _get_vector_index_typed_result(stmt, &ret, retlen);
243
                }
245
                }
244
            }
246
            }
245
        }
247
        }
246
    }
248
    }
247
 
249
 
248
    sqlite3_finalize(stmt);
250
    sqlite3_finalize(stmt);
249
 
251
 
250
    if (ret != R_NilValue) {
252
    if (ret != R_NilValue) {
251
        ret = _shrink_vector(ret, retlen);
253
        ret = _shrink_vector(ret, retlen);
252
        tmp = GET_LEVELS(svec);
254
        tmp = GET_LEVELS(svec);
253
        if (tmp != R_NilValue) {
255
        if (tmp != R_NilValue) {
254
            SET_LEVELS(ret, duplicate(tmp));
256
            SET_LEVELS(ret, duplicate(tmp));
255
            if (LENGTH(GET_CLASS(svec)) == 2) {
257
            if (LENGTH(GET_CLASS(svec)) == 2) {
256
                SET_CLASS(ret, mkString("factor"));
258
                SET_CLASS(ret, mkString("factor"));
257
            } else {
259
            } else {
258
                PROTECT(tmp = NEW_CHARACTER(2));
260
                PROTECT(tmp = NEW_CHARACTER(2));
259
                SET_STRING_ELT(tmp, 0, mkChar("ordered"));
261
                SET_STRING_ELT(tmp, 0, mkChar("ordered"));
260
                SET_STRING_ELT(tmp, 1, mkChar("factor"));
262
                SET_STRING_ELT(tmp, 1, mkChar("factor"));
261
                UNPROTECT(1);
263
                UNPROTECT(1);
262
            }
264
            }
263
        }
265
        }
264
    }
266
    }
265
 
267
 
266
    return ret;
268
    return ret;
267
}
269
}
268
 
270
 
269
 
271
 
-
 
272
SEXP sdf_do_variable_math(SEXP func, SEXP vector, SEXP extra_args, SEXP _nargs) {
-
 
273
    char *iname, *iname_src, *varname_src, *funcname;
-
 
274
    int namelen, res, nargs;
-
 
275
    sqlite3_stmt *stmt;
-
 
276
 
-
 
277
    /* get data from arguments (function name and sqlite.vector stuffs) */
-
 
278
    funcname = CHAR_ELT(func, 0);
-
 
279
    iname_src = SDF_INAME(vector);
-
 
280
    varname_src = SVEC_VARNAME(vector);
-
 
281
    nargs = INTEGER(_nargs)[0];
-
 
282
 
-
 
283
    /* check nargs */
-
 
284
    if (nargs > 2) {
-
 
285
        Rprintf("Error: Don't know how to handle Math functions w/ more than 2 args\n");
-
 
286
        return R_NilValue;
-
 
287
    }
-
 
288
 
-
 
289
    /* create a new sdf, with 1 column named V1 */
-
 
290
    iname = _create_sdf_skeleton2(R_NilValue, &namelen);
-
 
291
    if (iname == NULL) return R_NilValue;
-
 
292
 
-
 
293
    sprintf(g_sql_buf[0], "create table [%s].sdf_data ([row name] text, "
-
 
294
            "V1 double)", iname);
-
 
295
    res = _sqlite_exec(g_sql_buf[0]);
-
 
296
    _sqlite_error(res);
-
 
297
 
-
 
298
    /* insert into <newsdf>.col, row.names select func(col), rownames */
-
 
299
    sprintf(g_sql_buf[0], "insert into [%s].sdf_data([row name], V1) "
-
 
300
            "select [row name], %s([%s]) from [%s].sdf_data", iname, funcname,
-
 
301
            varname_src, iname_src);
-
 
302
 
-
 
303
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0); 
-
 
304
    if (_sqlite_error(res)) {
-
 
305
        sprintf(g_sql_buf[0], "detach %s", iname);
-
 
306
        _sqlite_exec(g_sql_buf[0]);
-
 
307
 
-
 
308
        /* we will return a string with the file name, and do file.remove
-
 
309
         * at R */
-
 
310
        iname[namelen] = '.';
-
 
311
        return mkString(iname);
-
 
312
    }
-
 
313
 
-
 
314
    sqlite3_step(stmt);
-
 
315
    sqlite3_finalize(stmt);
-
 
316
 
-
 
317
    /* add to workspace */
-
 
318
    strcpy(g_sql_buf[0], iname);
-
 
319
    iname[namelen] = '.';
-
 
320
    _add_sdf1(iname,  g_sql_buf[0]);
-
 
321
    iname[namelen] = 0;
-
 
322
 
-
 
323
    /* create sqlite.vector sexp */
-
 
324
    SEXP ret, value; int nprotected = 0;
-
 
325
    PROTECT(ret = NEW_LIST(2)); nprotected++;
-
 
326
 
-
 
327
    /* set list names */
-
 
328
    PROTECT(value = NEW_CHARACTER(2)); nprotected++;
-
 
329
    SET_STRING_ELT(value, 0, mkChar("iname"));
-
 
330
    SET_STRING_ELT(value, 1, mkChar("varname"));
-
 
331
    SET_NAMES(ret, value);
270
 
332
 
-
 
333
    /* set list values */
-
 
334
    SET_VECTOR_ELT(ret, 0, mkString(iname));
-
 
335
    SET_VECTOR_ELT(ret, 1, mkString("V1"));
271
 
336
 
-
 
337
    /* set sexp class */
-
 
338
    PROTECT(value = NEW_CHARACTER(2)); nprotected++;
-
 
339
    SET_VECTOR_ELT(value, 0, mkChar("sqlite.vector"));
-
 
340
    SET_VECTOR_ELT(value, 1, mkChar("numeric"));
-
 
341
    SET_CLASS(ret, value);
-
 
342
 
-
 
343
    UNPROTECT(nprotected);
-
 
344
 
-
 
345
    return ret;
-
 
346
 
-
 
347
}
-
 
348
 
-
 
349
 
-
 
350
 
-
 
351
/****************************************************************************
-
 
352
 * VECTOR MATH/OPS/GROUP OPERATIONS
-
 
353
 ****************************************************************************/
-
 
354
int __vecmath_checkarg(sqlite3_context *ctx, sqlite3_value *arg, double *value) {
-
 
355
    int ret = 1;
-
 
356
    if (sqlite3_value_type(arg) == SQLITE_NULL) { 
-
 
357
        sqlite3_result_null(ctx); 
-
 
358
        ret = 0;
-
 
359
    } else {
-
 
360
        if (sqlite3_value_type(arg) == SQLITE_INTEGER) 
-
 
361
            *value = sqlite3_value_int(arg); 
-
 
362
        else *value = sqlite3_value_double(arg); 
-
 
363
    }
-
 
364
    return ret;
-
 
365
}
-
 
366
 
-
 
367
#define SQLITE_MATH_FUNC1(name, func) static void __vecmath_ ## name(\
-
 
368
        sqlite3_context *ctx, int argc, sqlite3_value **argv) { \
-
 
369
    double value; \
-
 
370
    if (__vecmath_checkarg(ctx, argv[0], &value)) { \
-
 
371
        sqlite3_result_double(ctx, func(value)); \
-
 
372
    }  \
-
 
373
}
-
 
374
 
-
 
375
/* SQLITE_MATH_FUNC1(abs, abs)   in SQLite */
-
 
376
SQLITE_MATH_FUNC1(sign, sign)   /* in R */
-
 
377
SQLITE_MATH_FUNC1(sqrt, sqrt)
-
 
378
SQLITE_MATH_FUNC1(floor, floor)
-
 
379
SQLITE_MATH_FUNC1(ceiling, ceil)
-
 
380
SQLITE_MATH_FUNC1(trunc, ftrunc) /* in R */
-
 
381
/* SQLITE_MATH_FUNC1(round, )   in SQLite */
-
 
382
/* SQLITE_MATH_FUNC1(signif, ) 2 arg */
-
 
383
SQLITE_MATH_FUNC1(exp, exp)
-
 
384
/* SQLITE_MATH_FUNC1(log, ) 2 arg */
-
 
385
SQLITE_MATH_FUNC1(cos, cos)
-
 
386
SQLITE_MATH_FUNC1(sin, sin)
-
 
387
SQLITE_MATH_FUNC1(tan, tan)
-
 
388
SQLITE_MATH_FUNC1(acos, acos)
-
 
389
SQLITE_MATH_FUNC1(asin, asin)
-
 
390
SQLITE_MATH_FUNC1(atan, atan)
-
 
391
SQLITE_MATH_FUNC1(cosh, cosh)
-
 
392
SQLITE_MATH_FUNC1(sinh, sinh)
-
 
393
SQLITE_MATH_FUNC1(tanh, tanh)
-
 
394
SQLITE_MATH_FUNC1(acosh, acosh)  /* nowhere in include?? */
-
 
395
SQLITE_MATH_FUNC1(asinh, asinh)  /* nowhere in include?? */
-
 
396
SQLITE_MATH_FUNC1(atanh, atanh)  /* nowhere in include?? */
-
 
397
SQLITE_MATH_FUNC1(lgamma, lgammafn) /* in R */
-
 
398
SQLITE_MATH_FUNC1(gamma, gammafn) /* in R */
-
 
399
/* SQLITE_MATH_FUNC1(gammaCody, gammaCody)   * in R ?? */
-
 
400
SQLITE_MATH_FUNC1(digamma, digamma) /* in R */    
-
 
401
SQLITE_MATH_FUNC1(trigamma, trigamma) /* in R */
-
 
402
 
-
 
403
#define VMENTRY1(func)  {#func, __vecmath_ ## func}
-
 
404
void __register_vector_math() {
-
 
405
    int i, res;
-
 
406
    static const struct {
-
 
407
        char *name;
-
 
408
        void (*func)(sqlite3_context*, int, sqlite3_value**);
-
 
409
    } arr_func1[] = {
-
 
410
        VMENTRY1(sign),
-
 
411
        VMENTRY1(sqrt),
-
 
412
        VMENTRY1(floor),
-
 
413
        VMENTRY1(ceiling),
-
 
414
        VMENTRY1(trunc),
-
 
415
        VMENTRY1(exp),
-
 
416
        VMENTRY1(cos),
-
 
417
        VMENTRY1(sin),
-
 
418
        VMENTRY1(tan),
-
 
419
        VMENTRY1(acos),
-
 
420
        VMENTRY1(asin),
-
 
421
        VMENTRY1(atan),
-
 
422
        VMENTRY1(cosh),
-
 
423
        VMENTRY1(sinh),
-
 
424
        VMENTRY1(tanh),
-
 
425
        VMENTRY1(acosh),
-
 
426
        VMENTRY1(asinh),
-
 
427
        VMENTRY1(atanh),
-
 
428
        VMENTRY1(lgamma),
-
 
429
        VMENTRY1(gamma),
-
 
430
        VMENTRY1(digamma),
-
 
431
        VMENTRY1(trigamma)
-
 
432
    };
-
 
433
 
-
 
434
    int func1_len = sizeof(arr_func1) / sizeof(arr_func1[0]);
-
 
435
 
-
 
436
    for (i = 0; i < func1_len; i++) {
-
 
437
        res = sqlite3_create_function(g_workspace, arr_func1[i].name, 1, 
-
 
438
                SQLITE_ANY, NULL, arr_func1[i].func, NULL, NULL);
-
 
439
        _sqlite_error(res);
-
 
440
    }
-
 
441
}