The R Project SVN R-packages

Rev

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

Rev 3397 Rev 3419
1
#include "sqlite_dataframe.h"
1
#include "sqlite_dataframe.h"
2
#include <math.h>
2
#include <math.h>
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);
16
    res = _sqlite_exec(g_sql_buf[2]);
16
    res = _sqlite_exec(g_sql_buf[2]);
17
    _sqlite_error(res);
17
    _sqlite_error(res);
18
 
18
 
19
    if (_namelen != NULL) *_namelen = namelen;
19
    if (_namelen != NULL) *_namelen = namelen;
20
    return iname;
20
    return iname;
21
}
21
}
22
 
22
 
23
static SEXP _create_svector_sexp(const char *iname, const char *varname, 
23
static SEXP _create_svector_sexp(const char *iname, const char *varname, 
24
        const char *type) {
24
        const char *type) {
25
    SEXP ret, value; int nprotected = 0;
25
    SEXP ret, value; int nprotected = 0;
26
    PROTECT(ret = NEW_LIST(2)); nprotected++;
26
    PROTECT(ret = NEW_LIST(2)); nprotected++;
27
 
27
 
28
    /* set list names */
28
    /* set list names */
29
    PROTECT(value = NEW_CHARACTER(2)); nprotected++;
29
    PROTECT(value = NEW_CHARACTER(2)); nprotected++;
30
    SET_STRING_ELT(value, 0, mkChar("iname"));
30
    SET_STRING_ELT(value, 0, mkChar("iname"));
31
    SET_STRING_ELT(value, 1, mkChar("varname"));
31
    SET_STRING_ELT(value, 1, mkChar("varname"));
32
    SET_NAMES(ret, value);
32
    SET_NAMES(ret, value);
33
 
33
 
34
    /* set list values */
34
    /* set list values */
35
    SET_VECTOR_ELT(ret, 0, mkString(iname));
35
    SET_VECTOR_ELT(ret, 0, mkString(iname));
36
    SET_VECTOR_ELT(ret, 1, mkString(varname));
36
    SET_VECTOR_ELT(ret, 1, mkString(varname));
37
 
37
 
38
    /* set sexp class */
38
    /* set sexp class */
39
    if (strcmp(type, "ordered") == 0) {
39
    if (strcmp(type, "ordered") == 0) {
40
        PROTECT(value = NEW_CHARACTER(3)); nprotected++;
40
        PROTECT(value = NEW_CHARACTER(3)); nprotected++;
41
        SET_VECTOR_ELT(value, 0, mkChar("sqlite.vector"));
41
        SET_VECTOR_ELT(value, 0, mkChar("sqlite.vector"));
42
        SET_VECTOR_ELT(value, 1, mkChar(type));
42
        SET_VECTOR_ELT(value, 1, mkChar(type));
43
        SET_VECTOR_ELT(value, 2, mkChar("factor"));
43
        SET_VECTOR_ELT(value, 2, mkChar("factor"));
44
    } else {
44
    } else {
45
        PROTECT(value = NEW_CHARACTER(2)); nprotected++;
45
        PROTECT(value = NEW_CHARACTER(2)); nprotected++;
46
        SET_VECTOR_ELT(value, 0, mkChar("sqlite.vector"));
46
        SET_VECTOR_ELT(value, 0, mkChar("sqlite.vector"));
47
        SET_VECTOR_ELT(value, 1, mkChar(type));
47
        SET_VECTOR_ELT(value, 1, mkChar(type));
48
        SET_CLASS(ret, value);
48
        SET_CLASS(ret, value);
49
    }
49
    }
50
 
50
 
51
    UNPROTECT(nprotected);
51
    UNPROTECT(nprotected);
52
 
52
 
53
    return ret;
53
    return ret;
54
}
54
}
55
 
55
 
56
/* if ret == NULL, 3rd arg is the length of the vector created. otherwise
56
/* if ret == NULL, 3rd arg is the length of the vector created. otherwise
57
 * it is the index in the vector ret where we will put the result extracted
57
 * it is the index in the vector ret where we will put the result extracted
58
 * from ret */
58
 * from ret */
59
int _get_vector_index_typed_result(sqlite3_stmt *stmt, SEXP *ret, int idx_or_len) {
59
int _get_vector_index_typed_result(sqlite3_stmt *stmt, SEXP *ret, int idx_or_len) {
60
    int added = 1;
60
    int added = 1;
61
    if (*ret == NULL || *ret == R_NilValue) {
61
    if (*ret == NULL || *ret == R_NilValue) {
62
        const char *coltype = sqlite3_column_decltype(stmt, 0);
62
        const char *coltype = sqlite3_column_decltype(stmt, 0);
63
        if (sqlite3_column_type(stmt, 0) == SQLITE_NULL) {
63
        if (sqlite3_column_type(stmt, 0) == SQLITE_NULL) {
64
            added = 0;
64
            added = 0;
65
        }
65
        }
66
        
66
        
67
        if (strcmp(coltype, "text") == 0) {
67
        if (strcmp(coltype, "text") == 0) {
68
            PROTECT(*ret = NEW_CHARACTER(idx_or_len));
68
            PROTECT(*ret = NEW_CHARACTER(idx_or_len));
69
            if (added) 
69
            if (added) 
70
                SET_STRING_ELT(*ret, 0, mkChar((char *)sqlite3_column_text(stmt, 0)));
70
                SET_STRING_ELT(*ret, 0, mkChar((char *)sqlite3_column_text(stmt, 0)));
71
        } else if (strcmp(coltype, "double") == 0) {
71
        } else if (strcmp(coltype, "double") == 0) {
72
            PROTECT(*ret = NEW_NUMERIC(idx_or_len));
72
            PROTECT(*ret = NEW_NUMERIC(idx_or_len));
73
            if (added) REAL(*ret)[0] = sqlite3_column_double(stmt, 0);
73
            if (added) REAL(*ret)[0] = sqlite3_column_double(stmt, 0);
74
        } else if (strcmp(coltype, "bit") == 0) {
74
        } else if (strcmp(coltype, "bit") == 0) {
75
            PROTECT(*ret = NEW_LOGICAL(idx_or_len));
75
            PROTECT(*ret = NEW_LOGICAL(idx_or_len));
76
            if (added) INTEGER(*ret)[0] = sqlite3_column_int(stmt, 0);
76
            if (added) INTEGER(*ret)[0] = sqlite3_column_int(stmt, 0);
77
        } else if (strcmp(coltype, "integer") == 0 || 
77
        } else if (strcmp(coltype, "integer") == 0 || 
78
                   strcmp(coltype, "int") == 0) {
78
                   strcmp(coltype, "int") == 0) {
79
            /* caller should just copy off the vars level attr for factors */
79
            /* caller should just copy off the vars level attr for factors */
80
            PROTECT(*ret = NEW_INTEGER(idx_or_len));
80
            PROTECT(*ret = NEW_INTEGER(idx_or_len));
81
            if (added) INTEGER(*ret)[0] = sqlite3_column_int(stmt, 0);
81
            if (added) INTEGER(*ret)[0] = sqlite3_column_int(stmt, 0);
82
        } else added = 0;
82
        } else added = 0;
83
 
83
 
84
        UNPROTECT(1);
84
        UNPROTECT(1);
85
    } else {
85
    } else {
86
        const char *coltype = sqlite3_column_decltype(stmt, 0);
86
        const char *coltype = sqlite3_column_decltype(stmt, 0);
87
        if (sqlite3_column_type(stmt, 0) == SQLITE_NULL) {
87
        if (sqlite3_column_type(stmt, 0) == SQLITE_NULL) {
88
            added = 0;
88
            added = 0;
89
        } else if (strcmp(coltype, "text") == 0) {
89
        } else if (strcmp(coltype, "text") == 0) {
90
            SET_STRING_ELT(*ret, idx_or_len, 
90
            SET_STRING_ELT(*ret, idx_or_len, 
91
                    mkChar((char *)sqlite3_column_text(stmt, 0)));
91
                    mkChar((char *)sqlite3_column_text(stmt, 0)));
92
        } else if (strcmp(coltype, "double") == 0) {
92
        } else if (strcmp(coltype, "double") == 0) {
93
            REAL(*ret)[idx_or_len] = sqlite3_column_double(stmt, 0);
93
            REAL(*ret)[idx_or_len] = sqlite3_column_double(stmt, 0);
94
        } else if (strcmp(coltype, "bit") == 0) {
94
        } else if (strcmp(coltype, "bit") == 0) {
95
            INTEGER(*ret)[idx_or_len] = sqlite3_column_int(stmt, 0);
95
            INTEGER(*ret)[idx_or_len] = sqlite3_column_int(stmt, 0);
96
        } else if (strcmp(coltype, "integer") == 0 ||
96
        } else if (strcmp(coltype, "integer") == 0 ||
97
                   strcmp(coltype, "int") == 0) {
97
                   strcmp(coltype, "int") == 0) {
98
            /* caller should just copy off the vars level attr for factors */
98
            /* caller should just copy off the vars level attr for factors */
99
            INTEGER(*ret)[idx_or_len] = sqlite3_column_int(stmt, 0);
99
            INTEGER(*ret)[idx_or_len] = sqlite3_column_int(stmt, 0);
100
        } else added = 0;
100
        } else added = 0;
101
    }
101
    }
102
        
102
        
103
    return added;
103
    return added;
104
}
104
}
105
 
105
 
106
/****************************************************************************
106
/****************************************************************************
107
 * SVEC FUNCTIONS
107
 * SVEC FUNCTIONS
108
 ****************************************************************************/
108
 ****************************************************************************/
109
SEXP sdf_get_variable(SEXP sdf, SEXP name) {
109
SEXP sdf_get_variable(SEXP sdf, SEXP name) {
110
    if (!IS_CHARACTER(name)) {
110
    if (!IS_CHARACTER(name)) {
111
        Rprintf("ERROR: argument is not a string.\n");
111
        Rprintf("ERROR: argument is not a string.\n");
112
        return R_NilValue;
112
        return R_NilValue;
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
 
124
    int res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
124
    int res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
125
 
125
 
126
    if (_sqlite_error(res)) return R_NilValue;
126
    if (_sqlite_error(res)) return R_NilValue;
127
 
127
 
128
    const char *coltype = sqlite3_column_decltype(stmt, 0);
128
    const char *coltype = sqlite3_column_decltype(stmt, 0);
129
    sqlite3_finalize(stmt);
129
    sqlite3_finalize(stmt);
130
 
130
 
131
 
131
 
132
    SEXP ret, value, class = R_NilValue; int nprotected = 0;
132
    SEXP ret, value, class = R_NilValue; int nprotected = 0;
133
    PROTECT(ret = NEW_LIST(2)); nprotected++;
133
    PROTECT(ret = NEW_LIST(2)); nprotected++;
134
 
134
 
135
    /* set list names */
135
    /* set list names */
136
    PROTECT(value = NEW_CHARACTER(2)); nprotected++;
136
    PROTECT(value = NEW_CHARACTER(2)); nprotected++;
137
    SET_STRING_ELT(value, 0, mkChar("iname"));
137
    SET_STRING_ELT(value, 0, mkChar("iname"));
138
    SET_STRING_ELT(value, 1, mkChar("varname"));
138
    SET_STRING_ELT(value, 1, mkChar("varname"));
139
    SET_NAMES(ret, value);
139
    SET_NAMES(ret, value);
140
 
140
 
141
    /* set list values */
141
    /* set list values */
142
    SET_VECTOR_ELT(ret, 0, mkString(iname));
142
    SET_VECTOR_ELT(ret, 0, mkString(iname));
143
    SET_VECTOR_ELT(ret, 1, mkString(varname));
143
    SET_VECTOR_ELT(ret, 1, mkString(varname));
144
 
144
 
145
    /* set class */
145
    /* set class */
146
    int type = -1;
146
    int type = -1;
147
    if (strcmp(coltype, "text") == 0) class = mkChar("character");
147
    if (strcmp(coltype, "text") == 0) class = mkChar("character");
148
    else if (strcmp(coltype, "double") == 0) class = mkChar("numeric");
148
    else if (strcmp(coltype, "double") == 0) class = mkChar("numeric");
149
    else if (strcmp(coltype, "bit") == 0) class = mkChar("logical");
149
    else if (strcmp(coltype, "bit") == 0) class = mkChar("logical");
150
    else if (strcmp(coltype, "integer") == 0 || strcmp(coltype, "int") == 0) {
150
    else if (strcmp(coltype, "integer") == 0 || strcmp(coltype, "int") == 0) {
151
        /* determine if int, factor or ordered */
151
        /* determine if int, factor or ordered */
152
        type = _get_factor_levels1(iname, varname, ret);
152
        type = _get_factor_levels1(iname, varname, ret);
153
        switch(type) {
153
        switch(type) {
154
            case VAR_INTEGER: class = mkChar("integer"); break;
154
            case VAR_INTEGER: class = mkChar("integer"); break;
155
            case VAR_FACTOR: class = mkChar("factor"); break;
155
            case VAR_FACTOR: class = mkChar("factor"); break;
156
            case VAR_ORDERED: class = mkChar("ordered");
156
            case VAR_ORDERED: class = mkChar("ordered");
157
        }
157
        }
158
 
158
 
159
    }
159
    }
160
 
160
 
161
    if (type != VAR_ORDERED) {
161
    if (type != VAR_ORDERED) {
162
        PROTECT(value = NEW_CHARACTER(2)); nprotected++;
162
        PROTECT(value = NEW_CHARACTER(2)); nprotected++;
163
        SET_STRING_ELT(value, 0, mkChar("sqlite.vector"));
163
        SET_STRING_ELT(value, 0, mkChar("sqlite.vector"));
164
        SET_STRING_ELT(value, 1, class);
164
        SET_STRING_ELT(value, 1, class);
165
    } else {
165
    } else {
166
        PROTECT(value = NEW_CHARACTER(3)); nprotected++;
166
        PROTECT(value = NEW_CHARACTER(3)); nprotected++;
167
        SET_STRING_ELT(value, 0, mkChar("sqlite.vector"));
167
        SET_STRING_ELT(value, 0, mkChar("sqlite.vector"));
168
        SET_STRING_ELT(value, 1, class);
168
        SET_STRING_ELT(value, 1, class);
169
        SET_STRING_ELT(value, 2, mkChar("factor"));
169
        SET_STRING_ELT(value, 2, mkChar("factor"));
170
    }
170
    }
171
    SET_CLASS(ret, value);
171
    SET_CLASS(ret, value);
172
 
172
 
173
    UNPROTECT(nprotected);
173
    UNPROTECT(nprotected);
174
    return ret;
174
    return ret;
175
 
175
 
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);
187
    return ret;
187
    return ret;
188
}
188
}
189
 
189
 
190
    
190
    
191
SEXP sdf_get_variable_index(SEXP svec, SEXP idx) {
191
SEXP sdf_get_variable_index(SEXP svec, SEXP idx) {
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",
203
            varname, iname);
203
            varname, iname);
204
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
204
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
205
 
205
 
206
    /* get data based on index */
206
    /* get data based on index */
207
    if (IS_NUMERIC(idx)) {
207
    if (IS_NUMERIC(idx)) {
208
        index = ((int) REAL(idx)[0]) - 1;
208
        index = ((int) REAL(idx)[0]) - 1;
209
        if (index < 0 && idxlen == 1) return ret;
209
        if (index < 0 && idxlen == 1) return ret;
210
 
210
 
211
        if (index >= 0) {
211
        if (index >= 0) {
212
            sqlite3_bind_int(stmt, 1, index);
212
            sqlite3_bind_int(stmt, 1, index);
213
            res = sqlite3_step(stmt);
213
            res = sqlite3_step(stmt);
214
            if (res == SQLITE_ROW) { 
214
            if (res == SQLITE_ROW) { 
215
                retlen = _get_vector_index_typed_result(stmt, &ret, idxlen);
215
                retlen = _get_vector_index_typed_result(stmt, &ret, idxlen);
216
            } 
216
            } 
217
        } 
217
        } 
218
        
218
        
219
        if (index < 0 || res != SQLITE_ROW) {
219
        if (index < 0 || res != SQLITE_ROW) {
220
            /* something wrong w/ 1st idx, and it is quietly ignored. we make 
220
            /* something wrong w/ 1st idx, and it is quietly ignored. we make 
221
             * a "dummy" call to setup the SEXP */
221
             * a "dummy" call to setup the SEXP */
222
            sqlite3_reset(stmt);
222
            sqlite3_reset(stmt);
223
            sqlite3_bind_int(stmt, 1, 0);
223
            sqlite3_bind_int(stmt, 1, 0);
224
            res = sqlite3_step(stmt);
224
            res = sqlite3_step(stmt);
225
            if (res == SQLITE_ROW) {
225
            if (res == SQLITE_ROW) {
226
                _get_vector_index_typed_result(stmt, &ret, idxlen - 1);
226
                _get_vector_index_typed_result(stmt, &ret, idxlen - 1);
227
            }
227
            }
228
            retlen = 0;
228
            retlen = 0;
229
        }
229
        }
230
 
230
 
231
 
231
 
232
        if (idxlen > 1) {
232
        if (idxlen > 1) {
233
            for (i = 1; i < idxlen; i++) {
233
            for (i = 1; i < idxlen; i++) {
234
                index = ((int) REAL(idx)[i]) - 1;
234
                index = ((int) REAL(idx)[i]) - 1;
235
                if (index < 0) continue;
235
                if (index < 0) continue;
236
                sqlite3_reset(stmt);
236
                sqlite3_reset(stmt);
237
                sqlite3_bind_int(stmt, 1, index);
237
                sqlite3_bind_int(stmt, 1, index);
238
                res = sqlite3_step(stmt);
238
                res = sqlite3_step(stmt);
239
                if (res == SQLITE_ROW)
239
                if (res == SQLITE_ROW)
240
                    retlen += _get_vector_index_typed_result(stmt, &ret, retlen); 
240
                    retlen += _get_vector_index_typed_result(stmt, &ret, retlen); 
241
            }
241
            }
242
        }
242
        }
243
    } else if (IS_INTEGER(idx)) {
243
    } else if (IS_INTEGER(idx)) {
244
        /* similar to REAL (IS_NUMERIC) above, except that we don't have
244
        /* similar to REAL (IS_NUMERIC) above, except that we don't have
245
         * to cast idx to int. can't refactor this out, sucks. */
245
         * to cast idx to int. can't refactor this out, sucks. */
246
        index = INTEGER(idx)[0] - 1;
246
        index = INTEGER(idx)[0] - 1;
247
        if (index < 0 && idxlen == 1) return ret;
247
        if (index < 0 && idxlen == 1) return ret;
248
 
248
 
249
        if (index >= 0) {
249
        if (index >= 0) {
250
            sqlite3_bind_int(stmt, 1, index);
250
            sqlite3_bind_int(stmt, 1, index);
251
            res = sqlite3_step(stmt);
251
            res = sqlite3_step(stmt);
252
            if (res == SQLITE_ROW) { 
252
            if (res == SQLITE_ROW) { 
253
                retlen = _get_vector_index_typed_result(stmt, &ret, idxlen);
253
                retlen = _get_vector_index_typed_result(stmt, &ret, idxlen);
254
            } 
254
            } 
255
        } 
255
        } 
256
        
256
        
257
        if (index < 0 || res != SQLITE_ROW) {
257
        if (index < 0 || res != SQLITE_ROW) {
258
            /* something wrong w/ 1st idx, and it is quietly ignored. we make 
258
            /* something wrong w/ 1st idx, and it is quietly ignored. we make 
259
             * a "dummy" call to setup the SEXP */
259
             * a "dummy" call to setup the SEXP */
260
            sqlite3_reset(stmt);
260
            sqlite3_reset(stmt);
261
            sqlite3_bind_int(stmt, 1, 0);
261
            sqlite3_bind_int(stmt, 1, 0);
262
            res = sqlite3_step(stmt);
262
            res = sqlite3_step(stmt);
263
            if (res == SQLITE_ROW) {
263
            if (res == SQLITE_ROW) {
264
                _get_vector_index_typed_result(stmt, &ret, idxlen - 1);
264
                _get_vector_index_typed_result(stmt, &ret, idxlen - 1);
265
            }
265
            }
266
            retlen = 0;
266
            retlen = 0;
267
        }
267
        }
268
 
268
 
269
        if (idxlen > 1) {
269
        if (idxlen > 1) {
270
            for (i = 1; i < idxlen; i++) {
270
            for (i = 1; i < idxlen; i++) {
271
                index = INTEGER(idx)[i] - 1;
271
                index = INTEGER(idx)[i] - 1;
272
                if (index < 0) continue;
272
                if (index < 0) continue;
273
                sqlite3_reset(stmt);
273
                sqlite3_reset(stmt);
274
                sqlite3_bind_int(stmt, 1, index);
274
                sqlite3_bind_int(stmt, 1, index);
275
                sqlite3_step(stmt);
275
                sqlite3_step(stmt);
276
                retlen += _get_vector_index_typed_result(stmt, &ret, retlen); 
276
                retlen += _get_vector_index_typed_result(stmt, &ret, retlen); 
277
            }
277
            }
278
        }
278
        }
279
 
279
 
280
    } else if (IS_LOGICAL(idx)) {
280
    } else if (IS_LOGICAL(idx)) {
281
        /* have to deal with recycling */
281
        /* have to deal with recycling */
282
        int veclen = _get_row_count2(iname, 1);
282
        int veclen = _get_row_count2(iname, 1);
283
 
283
 
284
        /* find if there is any TRUE element in the vector */
284
        /* find if there is any TRUE element in the vector */
285
        for (i = 0; i < idxlen && i < veclen; i++) {
285
        for (i = 0; i < idxlen && i < veclen; i++) {
286
            if (LOGICAL(idx)[i]) {
286
            if (LOGICAL(idx)[i]) {
287
                sqlite3_bind_int(stmt, 1, i);
287
                sqlite3_bind_int(stmt, 1, i);
288
                sqlite3_step(stmt);
288
                sqlite3_step(stmt);
289
                /* there are at least (idxlen-i) TRUE per cycle of the LOGICAL
289
                /* there are at least (idxlen-i) TRUE per cycle of the LOGICAL
290
                 * index. there are at least (veclen/idxlen) cycles (int div).
290
                 * index. there are at least (veclen/idxlen) cycles (int div).
291
                 * at the last cycle, if (veclen%idxlen > 0), there will be
291
                 * at the last cycle, if (veclen%idxlen > 0), there will be
292
                 * at least (veclen%idxlen - i) if veclen%idxlen > i */
292
                 * at least (veclen%idxlen - i) if veclen%idxlen > i */
293
                retlen = (idxlen-i) * (veclen/idxlen);
293
                retlen = (idxlen-i) * (veclen/idxlen);
294
                if (veclen%idxlen > i) retlen += (veclen%idxlen - i);
294
                if (veclen%idxlen > i) retlen += (veclen%idxlen - i);
295
 
295
 
296
                /* create the vector */
296
                /* create the vector */
297
                retlen = _get_vector_index_typed_result(stmt, &ret, retlen);
297
                retlen = _get_vector_index_typed_result(stmt, &ret, retlen);
298
                break;
298
                break;
299
            }
299
            }
300
        }
300
        }
301
 
301
 
302
        if (i < idxlen && i < veclen) {
302
        if (i < idxlen && i < veclen) {
303
            for (i++; i < veclen; i++) {
303
            for (i++; i < veclen; i++) {
304
                if (LOGICAL(idx)[i%idxlen]) {
304
                if (LOGICAL(idx)[i%idxlen]) {
305
                    sqlite3_reset(stmt);
305
                    sqlite3_reset(stmt);
306
                    sqlite3_bind_int(stmt, 1, i);
306
                    sqlite3_bind_int(stmt, 1, i);
307
                    sqlite3_step(stmt);
307
                    sqlite3_step(stmt);
308
                    retlen += _get_vector_index_typed_result(stmt, &ret, retlen);
308
                    retlen += _get_vector_index_typed_result(stmt, &ret, retlen);
309
                }
309
                }
310
            }
310
            }
311
        }
311
        }
312
    }
312
    }
313
 
313
 
314
    sqlite3_finalize(stmt);
314
    sqlite3_finalize(stmt);
315
 
315
 
316
    if (ret != R_NilValue) {
316
    if (ret != R_NilValue) {
317
        ret = _shrink_vector(ret, retlen);
317
        ret = _shrink_vector(ret, retlen);
318
        tmp = GET_LEVELS(svec);
318
        tmp = GET_LEVELS(svec);
319
        if (tmp != R_NilValue) {
319
        if (tmp != R_NilValue) {
320
            SET_LEVELS(ret, duplicate(tmp));
320
            SET_LEVELS(ret, duplicate(tmp));
321
            if (LENGTH(GET_CLASS(svec)) == 2) {
321
            if (LENGTH(GET_CLASS(svec)) == 2) {
322
                SET_CLASS(ret, mkString("factor"));
322
                SET_CLASS(ret, mkString("factor"));
323
            } else {
323
            } else {
324
                PROTECT(tmp = NEW_CHARACTER(2));
324
                PROTECT(tmp = NEW_CHARACTER(2));
325
                SET_STRING_ELT(tmp, 0, mkChar("ordered"));
325
                SET_STRING_ELT(tmp, 0, mkChar("ordered"));
326
                SET_STRING_ELT(tmp, 1, mkChar("factor"));
326
                SET_STRING_ELT(tmp, 1, mkChar("factor"));
327
                UNPROTECT(1);
327
                UNPROTECT(1);
328
            }
328
            }
329
        }
329
        }
330
    }
330
    }
331
 
331
 
332
    return ret;
332
    return ret;
333
}
333
}
334
 
334
 
335
/* sqlite.vector.[<- */
335
/* sqlite.vector.[<- */
336
SEXP sdf_set_variable_index(SEXP svec, SEXP idx, SEXP value) {
336
SEXP sdf_set_variable_index(SEXP svec, SEXP idx, SEXP value) {
337
    int idx_len, val_len;
337
    int idx_len, val_len;
338
 
338
 
339
    /* match levels if factor or ordered */
339
    /* match levels if factor or ordered */
340
    if (inherits(value, "ordered")) {
340
    if (inherits(value, "ordered")) {
341
    } else if (inherits(value, "factor")) {
341
    } else if (inherits(value, "factor")) {
342
    }
342
    }
343
 
343
 
344
    idx_len = LENGTH(idx);
344
    idx_len = LENGTH(idx);
345
    val_len = LENGTH(value);
345
    val_len = LENGTH(value);
346
 
346
 
347
    if (IS_NUMERIC(idx)) {
347
    if (IS_NUMERIC(idx)) {
348
    } else if (IS_INTEGER(idx)) {
348
    } else if (IS_INTEGER(idx)) {
349
    } else if (IS_LOGICAL(idx)) {
349
    } else if (IS_LOGICAL(idx)) {
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 */
360
static int g_narm = 0;             /* for Summary group */
432
static int g_narm = 0;             /* for Summary group */
361
 
433
 
362
SEXP sdf_do_variable_math(SEXP func, SEXP vector, SEXP extra_args, SEXP _nargs) {
434
SEXP sdf_do_variable_math(SEXP func, SEXP vector, SEXP extra_args, SEXP _nargs) {
363
    char *iname, *iname_src, *varname_src, *funcname;
435
    char *iname, *iname_src, *varname_src, *funcname;
364
    int namelen, res, nargs;
436
    int namelen, res, nargs;
365
    sqlite3_stmt *stmt;
437
    sqlite3_stmt *stmt;
366
 
438
 
367
    /* get data from arguments (function name and sqlite.vector stuffs) */
439
    /* get data from arguments (function name and sqlite.vector stuffs) */
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);
388
 
460
 
389
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0); 
461
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0); 
390
    if (_sqlite_error(res)) {
462
    if (_sqlite_error(res)) {
391
        sprintf(g_sql_buf[0], "detach %s", iname);
463
        sprintf(g_sql_buf[0], "detach %s", iname);
392
        _sqlite_exec(g_sql_buf[0]);
464
        _sqlite_exec(g_sql_buf[0]);
393
 
465
 
394
        /* we will return a string with the file name, and do file.remove
466
        /* we will return a string with the file name, and do file.remove
395
         * at R */
467
         * at R */
396
        iname[namelen] = '.';
468
        iname[namelen] = '.';
397
        return mkString(iname);
469
        return mkString(iname);
398
    }
470
    }
399
 
471
 
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;
410
    int res, functype = -1, op2_len, svec_len, i;
485
    int res, functype = -1, op2_len, svec_len, i;
411
    sqlite3_stmt *stmt, *stmt2;
486
    sqlite3_stmt *stmt, *stmt2;
412
 
487
 
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 '/' :
424
        case '^' :
499
        case '^' :
425
        case '%' : /* %% and %/% */ 
500
        case '%' : /* %% and %/% */ 
426
            functype = 0; break;  /* output is REAL */
501
            functype = 0; break;  /* output is REAL */
427
        case '&' :
502
        case '&' :
428
        case '|' :
503
        case '|' :
429
        case '!' :  /* ! and != */
504
        case '!' :  /* ! and != */
430
            if (funcname[1] == 0) { functype = 1; break; }
505
            if (funcname[1] == 0) { functype = 1; break; }
431
        case '=' :  /* == */
506
        case '=' :  /* == */
432
        case '<' :  /* < and <= */
507
        case '<' :  /* < and <= */
433
        case '>' :  /* > and >= */
508
        case '>' :  /* > and >= */
434
            functype = 2; 
509
            functype = 2; 
435
    }
510
    }
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))";
447
            /* no need for char version of fmt_string2, since %% only occurs for functype==0 */
522
            /* no need for char version of fmt_string2, since %% only occurs for functype==0 */
448
            insert_fmt_string1c = insert_fmt_string1s;
523
            insert_fmt_string1c = insert_fmt_string1s;
449
            insert_fmt_string2c = insert_fmt_string2s;
524
            insert_fmt_string2c = insert_fmt_string2s;
450
        } else {
525
        } else {
451
            insert_fmt_string1s = "insert into [%s].sdf_data "
526
            insert_fmt_string1s = "insert into [%s].sdf_data "
452
                        "select [row name], [%s] %s ? from [%s].sdf_data";
527
                        "select [row name], [%s] %s ? from [%s].sdf_data";
453
            insert_fmt_string1c = "insert into [%s].sdf_data "
528
            insert_fmt_string1c = "insert into [%s].sdf_data "
454
                        "select [row name], [%s] %c ? from [%s].sdf_data";
529
                        "select [row name], [%s] %c ? from [%s].sdf_data";
455
            /* fmt_string2c format operator from a char, used for %% and %/% */
530
            /* fmt_string2c format operator from a char, used for %% and %/% */
456
            /* BUG: i'm casting to int before applying / & %, w/c is different
531
            /* BUG: i'm casting to int before applying / & %, w/c is different
457
             * from R. added trunc to pass package test */
532
             * from R. added trunc to pass package test */
458
            insert_fmt_string2c = "insert into [%s].sdf_data values(?, trunc(? %c ?))";
533
            insert_fmt_string2c = "insert into [%s].sdf_data values(?, trunc(? %c ?))";
459
            insert_fmt_string2s = "insert into [%s].sdf_data values(?, ? %s ?)";
534
            insert_fmt_string2s = "insert into [%s].sdf_data values(?, ? %s ?)";
460
        }
535
        }
461
 
536
 
462
 
537
 
463
        if (IS_NUMERIC(op2)) {
538
        if (IS_NUMERIC(op2)) {
464
            op2_len = LENGTH(op2);
539
            op2_len = LENGTH(op2);
465
 
540
 
466
            if (op2_len == 1) {
541
            if (op2_len == 1) {
467
                if (funcname[0] == '%') {
542
                if (funcname[0] == '%') {
468
                    sprintf(g_sql_buf[2], insert_fmt_string1c, iname, 
543
                    sprintf(g_sql_buf[2], insert_fmt_string1c, iname, 
469
                            varname_src, funcname[1], iname_src);
544
                            varname_src, funcname[1], iname_src);
470
                } else {
545
                } else {
471
                    sprintf(g_sql_buf[2], insert_fmt_string1s, iname,
546
                    sprintf(g_sql_buf[2], insert_fmt_string1s, iname,
472
                            varname_src, funcname, iname_src);
547
                            varname_src, funcname, iname_src);
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);
494
 
570
 
495
                        sqlite3_reset(stmt);
571
                        sqlite3_reset(stmt);
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);
510
 
588
 
511
                        sqlite3_reset(stmt);
589
                        sqlite3_reset(stmt);
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] == '%') {
525
                    sprintf(g_sql_buf[2], insert_fmt_string2c, iname, funcname[1]);  
604
                    sprintf(g_sql_buf[2], insert_fmt_string2c, iname, funcname[1]);  
526
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
605
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
527
                    _sqlite_error(res);
606
                    _sqlite_error(res);
528
                } else {
607
                } else {
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] == '%') {
540
                        for ( ; i < op2_len && sqlite3_step(stmt) == SQLITE_ROW ; i++) {
619
                        for ( ; i < op2_len && sqlite3_step(stmt) == SQLITE_ROW ; i++) {
541
                            sqlite3_step(stmt2);
620
                            sqlite3_step(stmt2);
542
 
621
 
543
                            sqlite3_reset(stmt);
622
                            sqlite3_reset(stmt);
544
                            /* simplify my life, just use ints for row names so that we
623
                            /* simplify my life, just use ints for row names so that we
545
                             * don't have to worry about duplicates */
624
                             * don't have to worry about duplicates */
546
                            sqlite3_bind_int(stmt, 1, i); 
625
                            sqlite3_bind_int(stmt, 1, i); 
547
                            sqlite3_bind_int(stmt, 2, sqlite3_column_int(stmt2, 1));
626
                            sqlite3_bind_int(stmt, 2, sqlite3_column_int(stmt2, 1));
548
                            sqlite3_bind_int(stmt, 3, (int)REAL(op2)[i % op2_len]);
627
                            sqlite3_bind_int(stmt, 3, (int)REAL(op2)[i % op2_len]);
549
                            sqlite3_step(stmt);
628
                            sqlite3_step(stmt);
550
                        }
629
                        }
551
                    } else {
630
                    } else {
552
                        for ( ; i < op2_len && sqlite3_step(stmt) == SQLITE_ROW ; i++) {
631
                        for ( ; i < op2_len && sqlite3_step(stmt) == SQLITE_ROW ; i++) {
553
                            sqlite3_step(stmt2);
632
                            sqlite3_step(stmt2);
554
 
633
 
555
                            sqlite3_reset(stmt);
634
                            sqlite3_reset(stmt);
556
                            sqlite3_bind_int(stmt, 1, i);
635
                            sqlite3_bind_int(stmt, 1, i);
557
                            sqlite3_bind_double(stmt, 2, sqlite3_column_double(stmt2, 1));
636
                            sqlite3_bind_double(stmt, 2, sqlite3_column_double(stmt2, 1));
558
                            sqlite3_bind_double(stmt, 3, REAL(op2)[i % op2_len]);
637
                            sqlite3_bind_double(stmt, 3, REAL(op2)[i % op2_len]);
559
                            sqlite3_step(stmt);
638
                            sqlite3_step(stmt);
560
                        }
639
                        }
561
                    }
640
                    }
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
 
576
            if (op2_len == 1) {
655
            if (op2_len == 1) {
577
                if (funcname[0] == '%') {
656
                if (funcname[0] == '%') {
578
                    sprintf(g_sql_buf[2], insert_fmt_string1c, iname, 
657
                    sprintf(g_sql_buf[2], insert_fmt_string1c, iname, 
579
                            varname_src, funcname[1], iname_src);
658
                            varname_src, funcname[1], iname_src);
580
                } else {
659
                } else {
581
                    sprintf(g_sql_buf[2], insert_fmt_string1s, iname,
660
                    sprintf(g_sql_buf[2], insert_fmt_string1s, iname,
582
                            varname_src, funcname, iname_src);
661
                            varname_src, funcname, iname_src);
583
                }
662
                }
584
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
663
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
585
                _sqlite_error(res);
664
                _sqlite_error(res);
586
                sqlite3_bind_double(stmt, 1, (double)INTEGER(op2)[0]);
665
                sqlite3_bind_double(stmt, 1, (double)INTEGER(op2)[0]);
587
                sqlite3_step(stmt);
666
                sqlite3_step(stmt);
588
            } else if (op2_len <= svec_len) {
667
            } else if (op2_len <= svec_len) {
589
                sprintf(g_sql_buf[2], "select [row name], [%s] from [%s].sdf_data",
668
                sprintf(g_sql_buf[2], "select [row name], [%s] from [%s].sdf_data",
590
                        varname_src, iname_src);
669
                        varname_src, iname_src);
591
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
670
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
592
                _sqlite_error(res);
671
                _sqlite_error(res);
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);
604
 
683
 
605
                        sqlite3_reset(stmt);
684
                        sqlite3_reset(stmt);
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);
620
 
699
 
621
                        sqlite3_reset(stmt);
700
                        sqlite3_reset(stmt);
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);
633
 
712
 
634
                if (funcname[0] == '%') {
713
                if (funcname[0] == '%') {
635
                    sprintf(g_sql_buf[2], insert_fmt_string2c, iname, funcname[1]);  
714
                    sprintf(g_sql_buf[2], insert_fmt_string2c, iname, funcname[1]);  
636
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
715
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
637
                    _sqlite_error(res);
716
                    _sqlite_error(res);
638
                } else {
717
                } else {
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] == '%') {
650
                        for ( ; i < op2_len && sqlite3_step(stmt) == SQLITE_ROW ; i++) {
729
                        for ( ; i < op2_len && sqlite3_step(stmt) == SQLITE_ROW ; i++) {
651
                            sqlite3_step(stmt2);
730
                            sqlite3_step(stmt2);
652
 
731
 
653
                            sqlite3_reset(stmt);
732
                            sqlite3_reset(stmt);
654
                            /* simplify my life, just use ints for row names so that we
733
                            /* simplify my life, just use ints for row names so that we
655
                             * don't have to worry about duplicates */
734
                             * don't have to worry about duplicates */
656
                            sqlite3_bind_int(stmt, 1, i); 
735
                            sqlite3_bind_int(stmt, 1, i); 
657
                            sqlite3_bind_int(stmt, 2, sqlite3_column_int(stmt2, 1));
736
                            sqlite3_bind_int(stmt, 2, sqlite3_column_int(stmt2, 1));
658
                            sqlite3_bind_int(stmt, 3, INTEGER(op2)[i % op2_len]);
737
                            sqlite3_bind_int(stmt, 3, INTEGER(op2)[i % op2_len]);
659
                            sqlite3_step(stmt);
738
                            sqlite3_step(stmt);
660
                        }
739
                        }
661
                    } else {
740
                    } else {
662
                        for ( ; i < op2_len && sqlite3_step(stmt) == SQLITE_ROW ; i++) {
741
                        for ( ; i < op2_len && sqlite3_step(stmt) == SQLITE_ROW ; i++) {
663
                            sqlite3_step(stmt2);
742
                            sqlite3_step(stmt2);
664
 
743
 
665
                            sqlite3_reset(stmt);
744
                            sqlite3_reset(stmt);
666
                            sqlite3_bind_int(stmt, 1, i);
745
                            sqlite3_bind_int(stmt, 1, i);
667
                            sqlite3_bind_double(stmt, 2, sqlite3_column_double(stmt2, 1));
746
                            sqlite3_bind_double(stmt, 2, sqlite3_column_double(stmt2, 1));
668
                            sqlite3_bind_double(stmt, 3, (double)INTEGER(op2)[i % op2_len]);
747
                            sqlite3_bind_double(stmt, 3, (double)INTEGER(op2)[i % op2_len]);
669
                            sqlite3_step(stmt);
748
                            sqlite3_step(stmt);
670
                        }
749
                        }
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);
698
            _sqlite_error(res);
778
            _sqlite_error(res);
699
 
779
 
700
            sprintf(g_sql_buf[2], "select [%s] from [%s].sdf_data", varname_op2, iname_op2);
780
            sprintf(g_sql_buf[2], "select [%s] from [%s].sdf_data", varname_op2, iname_op2);
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++) {
712
                        sqlite3_step(stmt2); sqlite3_step(stmt3);
791
                        sqlite3_step(stmt2); sqlite3_step(stmt3);
713
 
792
 
714
                        sqlite3_reset(stmt);
793
                        sqlite3_reset(stmt);
715
                        sqlite3_bind_int(stmt, 1, i);
794
                        sqlite3_bind_int(stmt, 1, i);
716
                        sqlite3_bind_int(stmt, 2, sqlite3_column_int(stmt2, 0));
795
                        sqlite3_bind_int(stmt, 2, sqlite3_column_int(stmt2, 0));
717
                        sqlite3_bind_int(stmt, 3, sqlite3_column_int(stmt3, 0));
796
                        sqlite3_bind_int(stmt, 3, sqlite3_column_int(stmt3, 0));
718
                        sqlite3_step(stmt);
797
                        sqlite3_step(stmt);
719
                    }
798
                    }
720
                } else if (svec_len < op2_len) {
799
                } else if (svec_len < op2_len) {
721
                    i = 0;
800
                    i = 0;
722
                    while (i < op2_len) {
801
                    while (i < op2_len) {
723
                        for ( ; sqlite3_step(stmt2) == SQLITE_ROW && i < op2_len; i++) {
802
                        for ( ; sqlite3_step(stmt2) == SQLITE_ROW && i < op2_len; i++) {
724
                            sqlite3_step(stmt3);
803
                            sqlite3_step(stmt3);
725
 
804
 
726
                            sqlite3_reset(stmt);
805
                            sqlite3_reset(stmt);
727
                            sqlite3_bind_int(stmt, 1, i);
806
                            sqlite3_bind_int(stmt, 1, i);
728
                            sqlite3_bind_int(stmt, 2, sqlite3_column_int(stmt2, 0));
807
                            sqlite3_bind_int(stmt, 2, sqlite3_column_int(stmt2, 0));
729
                            sqlite3_bind_int(stmt, 3, sqlite3_column_int(stmt3, 0));
808
                            sqlite3_bind_int(stmt, 3, sqlite3_column_int(stmt3, 0));
730
                            sqlite3_step(stmt);
809
                            sqlite3_step(stmt);
731
                        }
810
                        }
732
                        sqlite3_reset(stmt2);
811
                        sqlite3_reset(stmt2);
733
                    }
812
                    }
734
                } else {
813
                } else {
735
                    i = 0;
814
                    i = 0;
736
                    while (i < svec_len) {
815
                    while (i < svec_len) {
737
                        for ( ; sqlite3_step(stmt3) == SQLITE_ROW && i < svec_len; i++) {
816
                        for ( ; sqlite3_step(stmt3) == SQLITE_ROW && i < svec_len; i++) {
738
                            sqlite3_step(stmt2);
817
                            sqlite3_step(stmt2);
739
 
818
 
740
                            sqlite3_reset(stmt);
819
                            sqlite3_reset(stmt);
741
                            sqlite3_bind_int(stmt, 1, i);
820
                            sqlite3_bind_int(stmt, 1, i);
742
                            sqlite3_bind_int(stmt, 2, sqlite3_column_int(stmt2, 0));
821
                            sqlite3_bind_int(stmt, 2, sqlite3_column_int(stmt2, 0));
743
                            sqlite3_bind_int(stmt, 3, sqlite3_column_int(stmt3, 0));
822
                            sqlite3_bind_int(stmt, 3, sqlite3_column_int(stmt3, 0));
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++) {
758
                        sqlite3_step(stmt2); sqlite3_step(stmt3);
836
                        sqlite3_step(stmt2); sqlite3_step(stmt3);
759
 
837
 
760
                        sqlite3_reset(stmt);
838
                        sqlite3_reset(stmt);
761
                        sqlite3_bind_int(stmt, 1, i);
839
                        sqlite3_bind_int(stmt, 1, i);
762
                        sqlite3_bind_double(stmt, 2, sqlite3_column_double(stmt2, 0));
840
                        sqlite3_bind_double(stmt, 2, sqlite3_column_double(stmt2, 0));
763
                        sqlite3_bind_double(stmt, 3, sqlite3_column_double(stmt3, 0));
841
                        sqlite3_bind_double(stmt, 3, sqlite3_column_double(stmt3, 0));
764
                        sqlite3_step(stmt);
842
                        sqlite3_step(stmt);
765
                    }
843
                    }
766
                } else if (svec_len < op2_len) { /* recycle svec */
844
                } else if (svec_len < op2_len) { /* recycle svec */
767
                    i = 0;
845
                    i = 0;
768
                    while (i < op2_len) {
846
                    while (i < op2_len) {
769
                        for ( ; sqlite3_step(stmt2) == SQLITE_ROW && i < op2_len; i++) {
847
                        for ( ; sqlite3_step(stmt2) == SQLITE_ROW && i < op2_len; i++) {
770
                            sqlite3_step(stmt3);
848
                            sqlite3_step(stmt3);
771
 
849
 
772
                            sqlite3_reset(stmt);
850
                            sqlite3_reset(stmt);
773
                            sqlite3_bind_int(stmt, 1, i);
851
                            sqlite3_bind_int(stmt, 1, i);
774
                            sqlite3_bind_double(stmt, 2, sqlite3_column_double(stmt2, 0));
852
                            sqlite3_bind_double(stmt, 2, sqlite3_column_double(stmt2, 0));
775
                            sqlite3_bind_double(stmt, 3, sqlite3_column_double(stmt3, 0));
853
                            sqlite3_bind_double(stmt, 3, sqlite3_column_double(stmt3, 0));
776
                            sqlite3_step(stmt);
854
                            sqlite3_step(stmt);
777
                        }
855
                        }
778
                        sqlite3_reset(stmt2);
856
                        sqlite3_reset(stmt2);
779
                    }
857
                    }
780
                } else { /* svec_len > op2_len, recycle op2 */
858
                } else { /* svec_len > op2_len, recycle op2 */
781
                    i = 0;
859
                    i = 0;
782
                    while (i < svec_len) {
860
                    while (i < svec_len) {
783
                        for ( ; sqlite3_step(stmt3) == SQLITE_ROW && i < svec_len; i++) {
861
                        for ( ; sqlite3_step(stmt3) == SQLITE_ROW && i < svec_len; i++) {
784
                            sqlite3_step(stmt2);
862
                            sqlite3_step(stmt2);
785
 
863
 
786
                            sqlite3_reset(stmt);
864
                            sqlite3_reset(stmt);
787
                            sqlite3_bind_int(stmt, 1, i);
865
                            sqlite3_bind_int(stmt, 1, i);
788
                            sqlite3_bind_double(stmt, 2, sqlite3_column_double(stmt2, 0));
866
                            sqlite3_bind_double(stmt, 2, sqlite3_column_double(stmt2, 0));
789
                            sqlite3_bind_double(stmt, 3, sqlite3_column_double(stmt3, 0));
867
                            sqlite3_bind_double(stmt, 3, sqlite3_column_double(stmt3, 0));
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
 
821
SEXP sdf_do_variable_summary(SEXP func, SEXP vector, SEXP na_rm) {
905
SEXP sdf_do_variable_summary(SEXP func, SEXP vector, SEXP na_rm) {
822
    char *iname_src, *varname_src, *funcname;
906
    char *iname_src, *varname_src, *funcname;
823
    int res;
907
    int res;
824
    sqlite3_stmt *stmt;
908
    sqlite3_stmt *stmt;
825
    double _ret = NA_REAL; SEXP ret;
909
    double _ret = NA_REAL; SEXP ret;
826
 
910
 
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;
838
        g_accumulator = 0.0;
922
        g_accumulator = 0.0;
839
        sprintf(g_sql_buf[0], "select min_df([%s]) from [%s].sdf_data", varname_src, iname_src);
923
        sprintf(g_sql_buf[0], "select min_df([%s]) from [%s].sdf_data", varname_src, iname_src);
840
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
924
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
841
        if (_sqlite_error(res)) return R_NilValue;
925
        if (_sqlite_error(res)) return R_NilValue;
842
        sqlite3_step(stmt); sqlite3_finalize(stmt);
926
        sqlite3_step(stmt); sqlite3_finalize(stmt);
843
        _ret = g_accumulator;
927
        _ret = g_accumulator;
844
 
928
 
845
        if (R_IsNA(_ret) && !g_narm) {
929
        if (R_IsNA(_ret) && !g_narm) {
846
            PROTECT(ret = NEW_NUMERIC(2));
930
            PROTECT(ret = NEW_NUMERIC(2));
847
            REAL(ret)[0] = REAL(ret)[1] = R_NaReal;
931
            REAL(ret)[0] = REAL(ret)[1] = R_NaReal;
848
            goto __sdf_do_variable_summary_out;
932
            goto __sdf_do_variable_summary_out;
849
        }
933
        }
850
        
934
        
851
        g_start = 1;
935
        g_start = 1;
852
        g_accumulator = 0.0;
936
        g_accumulator = 0.0;
853
        sprintf(g_sql_buf[0], "select max_df([%s]) from [%s].sdf_data", varname_src, iname_src);
937
        sprintf(g_sql_buf[0], "select max_df([%s]) from [%s].sdf_data", varname_src, iname_src);
854
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
938
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
855
        if (_sqlite_error(res)) return R_NilValue;
939
        if (_sqlite_error(res)) return R_NilValue;
856
        sqlite3_step(stmt); sqlite3_finalize(stmt);
940
        sqlite3_step(stmt); sqlite3_finalize(stmt);
857
 
941
 
858
        /* if there is NA, then the if above should have caught it already */
942
        /* if there is NA, then the if above should have caught it already */
859
        PROTECT(ret = NEW_NUMERIC(2));
943
        PROTECT(ret = NEW_NUMERIC(2));
860
        REAL(ret)[0] = _ret;
944
        REAL(ret)[0] = _ret;
861
        REAL(ret)[1] = g_accumulator;
945
        REAL(ret)[1] = g_accumulator;
862
    } else {
946
    } else {
863
        g_start = 1;  /* we'll use these instead of sqlite3_aggregate_context */
947
        g_start = 1;  /* we'll use these instead of sqlite3_aggregate_context */
864
        g_accumulator = 0.0;
948
        g_accumulator = 0.0;
865
        sprintf(g_sql_buf[0], "select %s_df([%s]) from [%s].sdf_data", funcname, varname_src, iname_src);
949
        sprintf(g_sql_buf[0], "select %s_df([%s]) from [%s].sdf_data", funcname, varname_src, iname_src);
866
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
950
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
867
        if (_sqlite_error(res)) return R_NilValue;
951
        if (_sqlite_error(res)) return R_NilValue;
868
        res = sqlite3_step(stmt); sqlite3_finalize(stmt);
952
        res = sqlite3_step(stmt); sqlite3_finalize(stmt);
869
 
953
 
870
        if (strcmp(funcname, "all") == 0 || strcmp(funcname, "any") == 0) {
954
        if (strcmp(funcname, "all") == 0 || strcmp(funcname, "any") == 0) {
871
            PROTECT(ret = NEW_LOGICAL(1));
955
            PROTECT(ret = NEW_LOGICAL(1));
872
            if (R_IsNA(g_accumulator)) LOGICAL(ret)[0] = NA_INTEGER;
956
            if (R_IsNA(g_accumulator)) LOGICAL(ret)[0] = NA_INTEGER;
873
            else LOGICAL(ret)[0] = !(g_accumulator == 0);
957
            else LOGICAL(ret)[0] = !(g_accumulator == 0);
874
        } else {
958
        } else {
875
            PROTECT(ret = NEW_NUMERIC(1));
959
            PROTECT(ret = NEW_NUMERIC(1));
876
            REAL(ret)[0] = g_accumulator;
960
            REAL(ret)[0] = g_accumulator;
877
        }
961
        }
878
    }
962
    }
879
 
963
 
880
__sdf_do_variable_summary_out:
964
__sdf_do_variable_summary_out:
881
    UNPROTECT(1);
965
    UNPROTECT(1);
882
    return ret;
966
    return ret;
883
}
967
}
884
 
968
 
885
 
969
 
886
SEXP sdf_sort_variable(SEXP svec, SEXP decreasing) {
970
SEXP sdf_sort_variable(SEXP svec, SEXP decreasing) {
887
    char *iname, *iname_src, *varname_src, *type;
971
    char *iname, *iname_src, *varname_src, *type;
888
    sqlite3_stmt *stmt;
972
    sqlite3_stmt *stmt;
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,
911
            (LOGICAL(decreasing)[0]) ? "desc" : "asc");
995
            (LOGICAL(decreasing)[0]) ? "desc" : "asc");
912
    res = _sqlite_exec(g_sql_buf[0]);
996
    res = _sqlite_exec(g_sql_buf[0]);
913
    _sqlite_error(res);
997
    _sqlite_error(res);
914
 
998
 
915
    if (inherits(svec, "factor")) { /* copy factor table to iname */
999
    if (inherits(svec, "factor")) { /* copy factor table to iname */
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
926
 ****************************************************************************/
1013
 ****************************************************************************/
927
 
1014
 
928
int __vecmath_checkarg(sqlite3_context *ctx, sqlite3_value *arg, double *value) {
1015
int __vecmath_checkarg(sqlite3_context *ctx, sqlite3_value *arg, double *value) {
929
    int ret = 1;
1016
    int ret = 1;
930
    if (sqlite3_value_type(arg) == SQLITE_NULL) { 
1017
    if (sqlite3_value_type(arg) == SQLITE_NULL) { 
931
        sqlite3_result_null(ctx); 
1018
        sqlite3_result_null(ctx); 
932
        ret = 0;
1019
        ret = 0;
933
    } else {
1020
    } else {
934
        if (sqlite3_value_type(arg) == SQLITE_INTEGER) 
1021
        if (sqlite3_value_type(arg) == SQLITE_INTEGER) 
935
            *value = sqlite3_value_int(arg); 
1022
            *value = sqlite3_value_int(arg); 
936
        else *value = sqlite3_value_double(arg); 
1023
        else *value = sqlite3_value_double(arg); 
937
    }
1024
    }
938
    return ret;
1025
    return ret;
939
}
1026
}
940
 
1027
 
941
#define SQLITE_MATH_FUNC1(name, func) static void __vecmath_ ## name(\
1028
#define SQLITE_MATH_FUNC1(name, func) static void __vecmath_ ## name(\
942
        sqlite3_context *ctx, int argc, sqlite3_value **argv) { \
1029
        sqlite3_context *ctx, int argc, sqlite3_value **argv) { \
943
    double value; \
1030
    double value; \
944
    if (__vecmath_checkarg(ctx, argv[0], &value)) { \
1031
    if (__vecmath_checkarg(ctx, argv[0], &value)) { \
945
        sqlite3_result_double(ctx, func(value)); \
1032
        sqlite3_result_double(ctx, func(value)); \
946
    }  \
1033
    }  \
947
}
1034
}
948
 
1035
 
949
#define SQLITE_MATH_FUNC_CUM(name, func) static void __vecmath_ ## name(\
1036
#define SQLITE_MATH_FUNC_CUM(name, func) static void __vecmath_ ## name(\
950
        sqlite3_context *ctx, int argc, sqlite3_value **argv) { \
1037
        sqlite3_context *ctx, int argc, sqlite3_value **argv) { \
951
    double value; \
1038
    double value; \
952
    if (__vecmath_checkarg(ctx, argv[0], &value)) { \
1039
    if (__vecmath_checkarg(ctx, argv[0], &value)) { \
953
        if (g_start) { g_start = 0; g_accumulator = value; } \
1040
        if (g_start) { g_start = 0; g_accumulator = value; } \
954
        else g_accumulator = func(g_accumulator, value); \
1041
        else g_accumulator = func(g_accumulator, value); \
955
        sqlite3_result_double(ctx, g_accumulator); \
1042
        sqlite3_result_double(ctx, g_accumulator); \
956
    }  \
1043
    }  \
957
}
1044
}
958
 
1045
 
959
/* SQLITE_MATH_FUNC1(abs, abs)   in SQLite */
1046
/* SQLITE_MATH_FUNC1(abs, abs)   in SQLite */
960
SQLITE_MATH_FUNC1(sign, sign)   /* in R */
1047
SQLITE_MATH_FUNC1(sign, sign)   /* in R */
961
SQLITE_MATH_FUNC1(sqrt, sqrt)
1048
SQLITE_MATH_FUNC1(sqrt, sqrt)
962
SQLITE_MATH_FUNC1(floor, floor)
1049
SQLITE_MATH_FUNC1(floor, floor)
963
SQLITE_MATH_FUNC1(ceiling, ceil)
1050
SQLITE_MATH_FUNC1(ceiling, ceil)
964
SQLITE_MATH_FUNC1(trunc, ftrunc) /* in R */
1051
SQLITE_MATH_FUNC1(trunc, ftrunc) /* in R */
965
/* SQLITE_MATH_FUNC1(round, )   in SQLite */
1052
/* SQLITE_MATH_FUNC1(round, )   in SQLite */
966
/* SQLITE_MATH_FUNC1(signif, ) 2 arg */
1053
/* SQLITE_MATH_FUNC1(signif, ) 2 arg */
967
SQLITE_MATH_FUNC1(exp, exp)
1054
SQLITE_MATH_FUNC1(exp, exp)
968
/* SQLITE_MATH_FUNC1(log, ) 2 arg */
1055
/* SQLITE_MATH_FUNC1(log, ) 2 arg */
969
SQLITE_MATH_FUNC1(cos, cos)
1056
SQLITE_MATH_FUNC1(cos, cos)
970
SQLITE_MATH_FUNC1(sin, sin)
1057
SQLITE_MATH_FUNC1(sin, sin)
971
SQLITE_MATH_FUNC1(tan, tan)
1058
SQLITE_MATH_FUNC1(tan, tan)
972
SQLITE_MATH_FUNC1(acos, acos)
1059
SQLITE_MATH_FUNC1(acos, acos)
973
SQLITE_MATH_FUNC1(asin, asin)
1060
SQLITE_MATH_FUNC1(asin, asin)
974
SQLITE_MATH_FUNC1(atan, atan)
1061
SQLITE_MATH_FUNC1(atan, atan)
975
SQLITE_MATH_FUNC1(cosh, cosh)
1062
SQLITE_MATH_FUNC1(cosh, cosh)
976
SQLITE_MATH_FUNC1(sinh, sinh)
1063
SQLITE_MATH_FUNC1(sinh, sinh)
977
SQLITE_MATH_FUNC1(tanh, tanh)
1064
SQLITE_MATH_FUNC1(tanh, tanh)
978
SQLITE_MATH_FUNC1(acosh, acosh)  /* nowhere in include?? */
1065
SQLITE_MATH_FUNC1(acosh, acosh)  /* nowhere in include?? */
979
SQLITE_MATH_FUNC1(asinh, asinh)  /* nowhere in include?? */
1066
SQLITE_MATH_FUNC1(asinh, asinh)  /* nowhere in include?? */
980
SQLITE_MATH_FUNC1(atanh, atanh)  /* nowhere in include?? */
1067
SQLITE_MATH_FUNC1(atanh, atanh)  /* nowhere in include?? */
981
SQLITE_MATH_FUNC1(lgamma, lgammafn) /* in R */
1068
SQLITE_MATH_FUNC1(lgamma, lgammafn) /* in R */
982
SQLITE_MATH_FUNC1(gamma, gammafn) /* in R */
1069
SQLITE_MATH_FUNC1(gamma, gammafn) /* in R */
983
/* SQLITE_MATH_FUNC1(gammaCody, gammaCody)   * in R ?? */
1070
/* SQLITE_MATH_FUNC1(gammaCody, gammaCody)   * in R ?? */
984
SQLITE_MATH_FUNC1(digamma, digamma) /* in R */    
1071
SQLITE_MATH_FUNC1(digamma, digamma) /* in R */    
985
SQLITE_MATH_FUNC1(trigamma, trigamma) /* in R */
1072
SQLITE_MATH_FUNC1(trigamma, trigamma) /* in R */
986
 
1073
 
987
#define SUM(a, b)  (a) + (b)
1074
#define SUM(a, b)  (a) + (b)
988
#define PROD(a, b) (a) * (b)
1075
#define PROD(a, b) (a) * (b)
989
#define MIN(a, b) ((a) <= (b)) ? (a) : (b)
1076
#define MIN(a, b) ((a) <= (b)) ? (a) : (b)
990
#define MAX(a, b) ((a) >= (b)) ? (a) : (b)
1077
#define MAX(a, b) ((a) >= (b)) ? (a) : (b)
991
#define ALL(a, b) (((a) == 0) || ((b) == 0)) ? 0 : 1
1078
#define ALL(a, b) (((a) == 0) || ((b) == 0)) ? 0 : 1
992
#define ANY(a, b) (((a) == 0) && ((b) == 0)) ? 0 : 1
1079
#define ANY(a, b) (((a) == 0) && ((b) == 0)) ? 0 : 1
993
 
1080
 
994
SQLITE_MATH_FUNC_CUM(cumsum, SUM)
1081
SQLITE_MATH_FUNC_CUM(cumsum, SUM)
995
SQLITE_MATH_FUNC_CUM(cumprod, PROD)
1082
SQLITE_MATH_FUNC_CUM(cumprod, PROD)
996
SQLITE_MATH_FUNC_CUM(cummin, MIN)
1083
SQLITE_MATH_FUNC_CUM(cummin, MIN)
997
SQLITE_MATH_FUNC_CUM(cummax, MAX)
1084
SQLITE_MATH_FUNC_CUM(cummax, MAX)
998
 
1085
 
999
#define SQLITE_SUMMARY_FUNC(name, func) static void __vecsummary_ ## name(\
1086
#define SQLITE_SUMMARY_FUNC(name, func) static void __vecsummary_ ## name(\
1000
        sqlite3_context *ctx, int argc, sqlite3_value **argv) { \
1087
        sqlite3_context *ctx, int argc, sqlite3_value **argv) { \
1001
    double value; \
1088
    double value; \
1002
    if (!g_narm && R_IsNA(g_accumulator)) return; /* NA if na.rm=F & NA found */ \
1089
    if (!g_narm && R_IsNA(g_accumulator)) return; /* NA if na.rm=F & NA found */ \
1003
    if (sqlite3_value_type(argv[0]) != SQLITE_NULL) {  \
1090
    if (sqlite3_value_type(argv[0]) != SQLITE_NULL) {  \
1004
        if (sqlite3_value_type(argv[0]) == SQLITE_INTEGER) {  \
1091
        if (sqlite3_value_type(argv[0]) == SQLITE_INTEGER) {  \
1005
            int tmp = sqlite3_value_int(argv[0]); \
1092
            int tmp = sqlite3_value_int(argv[0]); \
1006
            value = (tmp == NA_INTEGER) ? R_NaReal : tmp; \
1093
            value = (tmp == NA_INTEGER) ? R_NaReal : tmp; \
1007
        } else value = sqlite3_value_double(argv[0]);  \
1094
        } else value = sqlite3_value_double(argv[0]);  \
1008
        if (R_IsNA(value)) { \
1095
        if (R_IsNA(value)) { \
1009
            if (!g_narm) g_accumulator = value; return; \
1096
            if (!g_narm) g_accumulator = value; return; \
1010
        } else if (g_start) { g_start = 0; g_accumulator = value; } \
1097
        } else if (g_start) { g_start = 0; g_accumulator = value; } \
1011
        else g_accumulator = func(g_accumulator, value); \
1098
        else g_accumulator = func(g_accumulator, value); \
1012
    } \
1099
    } \
1013
}
1100
}
1014
 
1101
 
1015
SQLITE_SUMMARY_FUNC(all_df, ALL)
1102
SQLITE_SUMMARY_FUNC(all_df, ALL)
1016
SQLITE_SUMMARY_FUNC(any_df, ANY)
1103
SQLITE_SUMMARY_FUNC(any_df, ANY)
1017
SQLITE_SUMMARY_FUNC(sum_df, SUM)
1104
SQLITE_SUMMARY_FUNC(sum_df, SUM)
1018
SQLITE_SUMMARY_FUNC(prod_df, PROD)
1105
SQLITE_SUMMARY_FUNC(prod_df, PROD)
1019
SQLITE_SUMMARY_FUNC(min_df, MIN)
1106
SQLITE_SUMMARY_FUNC(min_df, MIN)
1020
SQLITE_SUMMARY_FUNC(max_df, MAX)
1107
SQLITE_SUMMARY_FUNC(max_df, MAX)
1021
 
1108
 
1022
static void __vecsummary_finalize(sqlite3_context *ctx) {
1109
static void __vecsummary_finalize(sqlite3_context *ctx) {
1023
    /* g_accumulator already summarizes it. just return that */
1110
    /* g_accumulator already summarizes it. just return that */
1024
    sqlite3_result_double(ctx, g_accumulator);
1111
    sqlite3_result_double(ctx, g_accumulator);
1025
}
1112
}
1026
 
1113
 
1027
#define VMENTRY1(func)  {#func, __vecmath_ ## func}
1114
#define VMENTRY1(func)  {#func, __vecmath_ ## func}
1028
#define VSENTRY1(func)  {#func, __vecsummary_ ## func}
1115
#define VSENTRY1(func)  {#func, __vecsummary_ ## func}
1029
void __register_vector_math() {
1116
void __register_vector_math() {
1030
    int i, res;
1117
    int i, res;
1031
    static const struct {
1118
    static const struct {
1032
        char *name;
1119
        char *name;
1033
        void (*func)(sqlite3_context*, int, sqlite3_value**);
1120
        void (*func)(sqlite3_context*, int, sqlite3_value**);
1034
    } arr_func1[] = {
1121
    } arr_func1[] = {
1035
        VMENTRY1(sign),
1122
        VMENTRY1(sign),
1036
        VMENTRY1(sqrt),
1123
        VMENTRY1(sqrt),
1037
        VMENTRY1(floor),
1124
        VMENTRY1(floor),
1038
        VMENTRY1(ceiling),
1125
        VMENTRY1(ceiling),
1039
        VMENTRY1(trunc),
1126
        VMENTRY1(trunc),
1040
        VMENTRY1(exp),
1127
        VMENTRY1(exp),
1041
        VMENTRY1(cos),
1128
        VMENTRY1(cos),
1042
        VMENTRY1(sin),
1129
        VMENTRY1(sin),
1043
        VMENTRY1(tan),
1130
        VMENTRY1(tan),
1044
        VMENTRY1(acos),
1131
        VMENTRY1(acos),
1045
        VMENTRY1(asin),
1132
        VMENTRY1(asin),
1046
        VMENTRY1(atan),
1133
        VMENTRY1(atan),
1047
        VMENTRY1(cosh),
1134
        VMENTRY1(cosh),
1048
        VMENTRY1(sinh),
1135
        VMENTRY1(sinh),
1049
        VMENTRY1(tanh),
1136
        VMENTRY1(tanh),
1050
        VMENTRY1(acosh),
1137
        VMENTRY1(acosh),
1051
        VMENTRY1(asinh),
1138
        VMENTRY1(asinh),
1052
        VMENTRY1(atanh),
1139
        VMENTRY1(atanh),
1053
        VMENTRY1(lgamma),
1140
        VMENTRY1(lgamma),
1054
        VMENTRY1(gamma),
1141
        VMENTRY1(gamma),
1055
        VMENTRY1(digamma),
1142
        VMENTRY1(digamma),
1056
        VMENTRY1(trigamma),
1143
        VMENTRY1(trigamma),
1057
        VMENTRY1(cumsum),
1144
        VMENTRY1(cumsum),
1058
        VMENTRY1(cumprod),
1145
        VMENTRY1(cumprod),
1059
        VMENTRY1(cummin),
1146
        VMENTRY1(cummin),
1060
        VMENTRY1(cummax)
1147
        VMENTRY1(cummax)
1061
    }, arr_sum1[] = {
1148
    }, arr_sum1[] = {
1062
        VSENTRY1(all_df),  /* can't override sum, min, max */
1149
        VSENTRY1(all_df),  /* can't override sum, min, max */
1063
        VSENTRY1(any_df),
1150
        VSENTRY1(any_df),
1064
        VSENTRY1(sum_df),
1151
        VSENTRY1(sum_df),
1065
        VSENTRY1(prod_df),
1152
        VSENTRY1(prod_df),
1066
        VSENTRY1(min_df),
1153
        VSENTRY1(min_df),
1067
        VSENTRY1(max_df)
1154
        VSENTRY1(max_df)
1068
    };
1155
    };
1069
 
1156
 
1070
    int len = sizeof(arr_func1) / sizeof(arr_func1[0]);
1157
    int len = sizeof(arr_func1) / sizeof(arr_func1[0]);
1071
 
1158
 
1072
    for (i = 0; i < len; i++) {
1159
    for (i = 0; i < len; i++) {
1073
        res = sqlite3_create_function(g_workspace, arr_func1[i].name, 1, 
1160
        res = sqlite3_create_function(g_workspace, arr_func1[i].name, 1, 
1074
                SQLITE_ANY, NULL, arr_func1[i].func, NULL, NULL);
1161
                SQLITE_ANY, NULL, arr_func1[i].func, NULL, NULL);
1075
        _sqlite_error(res);
1162
        _sqlite_error(res);
1076
    }
1163
    }
1077
 
1164
 
1078
    len = sizeof(arr_sum1) / sizeof(arr_sum1[0]);
1165
    len = sizeof(arr_sum1) / sizeof(arr_sum1[0]);
1079
    for (i = 0; i < len; i++) {
1166
    for (i = 0; i < len; i++) {
1080
        res = sqlite3_create_function(g_workspace, arr_sum1[i].name, 1, 
1167
        res = sqlite3_create_function(g_workspace, arr_sum1[i].name, 1, 
1081
                SQLITE_ANY, NULL, NULL, arr_sum1[i].func, __vecsummary_finalize);
1168
                SQLITE_ANY, NULL, NULL, arr_sum1[i].func, __vecsummary_finalize);
1082
        _sqlite_error(res);
1169
        _sqlite_error(res);
1083
    }
1170
    }
1084
}
1171
}