The R Project SVN R-packages

Rev

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

Rev 3473 Rev 3684
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
char *_create_svector1(SEXP name, const char *type, int * _namelen, int protect) {
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, protect);
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
SEXP _create_svector_sexp(const char *iname, const char *tblname,
24
        const char *type) {
24
        const char *varname, 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(3)); nprotected++;
27
 
27
 
28
    /* set list names */
28
    /* set list names */
29
    PROTECT(value = NEW_CHARACTER(2)); nprotected++;
29
    PROTECT(value = NEW_CHARACTER(3)); nprotected++;
30
    SET_STRING_ELT(value, 0, mkChar("iname"));
30
    SET_STRING_ELT(value, 0, mkChar("iname"));
-
 
31
    SET_STRING_ELT(value, 1, mkChar("tblname"));
31
    SET_STRING_ELT(value, 1, mkChar("varname"));
32
    SET_STRING_ELT(value, 2, mkChar("varname"));
32
    SET_NAMES(ret, value);
33
    SET_NAMES(ret, value);
33
 
34
 
34
    /* set list values */
35
    /* set list values */
35
    SET_VECTOR_ELT(ret, 0, mkString(iname));
36
    SET_VECTOR_ELT(ret, 0, mkString(iname));
-
 
37
    SET_VECTOR_ELT(ret, 1, mkString(tblname));
36
    SET_VECTOR_ELT(ret, 1, mkString(varname));
38
    SET_VECTOR_ELT(ret, 2, mkString(varname));
37
 
39
 
38
    /* set sexp class */
40
    /* set sexp class */
39
    SET_CLASS(ret, mkString("sqlite.vector"));
41
    SET_CLASS(ret, mkString("sqlite.vector"));
40
 
42
 
41
    /* set sdf.vector.type */
43
    /* set sdf.vector.type */
42
    SET_SDFVECTORTYPE(ret, mkString(type));
44
    SET_SDFVECTORTYPE(ret, mkString(type));
43
 
45
 
44
    UNPROTECT(nprotected);
46
    UNPROTECT(nprotected);
45
 
47
 
46
    return ret;
48
    return ret;
47
}
49
}
48
 
50
 
49
/* if ret == NULL, 3rd arg is the length of the vector created. otherwise
51
/* if ret == NULL, 3rd arg is the length of the vector created. otherwise
50
 * it is the index in the vector ret where we will put the result extracted
52
 * it is the index in the vector ret where we will put the result extracted
51
 * from ret */
53
 * from ret */
52
int _get_vector_index_typed_result(sqlite3_stmt *stmt, SEXP *ret, int idx_or_len) {
54
int _get_vector_index_typed_result(sqlite3_stmt *stmt, SEXP *ret, int idx_or_len) {
53
    int added = 1;
55
    int added = 1;
54
    if (*ret == NULL || *ret == R_NilValue) {
56
    if (*ret == NULL || *ret == R_NilValue) {
55
        const char *coltype = sqlite3_column_decltype(stmt, 0);
57
        const char *coltype = sqlite3_column_decltype(stmt, 0);
56
        if (sqlite3_column_type(stmt, 0) == SQLITE_NULL) {
58
        if (sqlite3_column_type(stmt, 0) == SQLITE_NULL) {
57
            added = 0;
59
            added = 0;
58
        }
60
        }
59
        
61
        
60
        if (strcmp(coltype, "text") == 0) {
62
        if (strcmp(coltype, "text") == 0) {
61
            PROTECT(*ret = NEW_CHARACTER(idx_or_len));
63
            PROTECT(*ret = NEW_CHARACTER(idx_or_len));
62
            if (added) 
64
            if (added) 
63
                SET_STRING_ELT(*ret, 0, mkChar((char *)sqlite3_column_text(stmt, 0)));
65
                SET_STRING_ELT(*ret, 0, mkChar((char *)sqlite3_column_text(stmt, 0)));
64
        } else if (strcmp(coltype, "double") == 0) {
66
        } else if (strcmp(coltype, "double") == 0) {
65
            PROTECT(*ret = NEW_NUMERIC(idx_or_len));
67
            PROTECT(*ret = NEW_NUMERIC(idx_or_len));
66
            if (added) REAL(*ret)[0] = sqlite3_column_double(stmt, 0);
68
            if (added) REAL(*ret)[0] = sqlite3_column_double(stmt, 0);
67
        } else if (strcmp(coltype, "bit") == 0) {
69
        } else if (strcmp(coltype, "bit") == 0) {
68
            PROTECT(*ret = NEW_LOGICAL(idx_or_len));
70
            PROTECT(*ret = NEW_LOGICAL(idx_or_len));
69
            if (added) INTEGER(*ret)[0] = sqlite3_column_int(stmt, 0);
71
            if (added) INTEGER(*ret)[0] = sqlite3_column_int(stmt, 0);
70
        } else if (strcmp(coltype, "integer") == 0 || 
72
        } else if (strcmp(coltype, "integer") == 0 || 
71
                   strcmp(coltype, "int") == 0) {
73
                   strcmp(coltype, "int") == 0) {
72
            /* caller should just copy off the vars level attr for factors */
74
            /* caller should just copy off the vars level attr for factors */
73
            PROTECT(*ret = NEW_INTEGER(idx_or_len));
75
            PROTECT(*ret = NEW_INTEGER(idx_or_len));
74
            if (added) INTEGER(*ret)[0] = sqlite3_column_int(stmt, 0);
76
            if (added) INTEGER(*ret)[0] = sqlite3_column_int(stmt, 0);
75
        } else added = 0;
77
        } else added = 0;
76
 
78
 
77
        UNPROTECT(1);
79
        UNPROTECT(1);
78
    } else {
80
    } else if (stmt != NULL) {
79
        const char *coltype = sqlite3_column_decltype(stmt, 0);
81
        const char *coltype = sqlite3_column_decltype(stmt, 0);
80
        if (sqlite3_column_type(stmt, 0) == SQLITE_NULL) {
82
        if (sqlite3_column_type(stmt, 0) == SQLITE_NULL) {
81
            added = 0;
83
            added = 0;
82
        } else if (strcmp(coltype, "text") == 0) {
84
        } else if (strcmp(coltype, "text") == 0) {
83
            SET_STRING_ELT(*ret, idx_or_len, 
85
            SET_STRING_ELT(*ret, idx_or_len, 
84
                    mkChar((char *)sqlite3_column_text(stmt, 0)));
86
                    mkChar((char *)sqlite3_column_text(stmt, 0)));
85
        } else if (strcmp(coltype, "double") == 0) {
87
        } else if (strcmp(coltype, "double") == 0) {
86
            REAL(*ret)[idx_or_len] = sqlite3_column_double(stmt, 0);
88
            REAL(*ret)[idx_or_len] = sqlite3_column_double(stmt, 0);
87
        } else if (strcmp(coltype, "bit") == 0) {
89
        } else if (strcmp(coltype, "bit") == 0) {
88
            INTEGER(*ret)[idx_or_len] = sqlite3_column_int(stmt, 0);
90
            INTEGER(*ret)[idx_or_len] = sqlite3_column_int(stmt, 0);
89
        } else if (strcmp(coltype, "integer") == 0 ||
91
        } else if (strcmp(coltype, "integer") == 0 ||
90
                   strcmp(coltype, "int") == 0) {
92
                   strcmp(coltype, "int") == 0) {
91
            /* caller should just copy off the vars level attr for factors */
93
            /* caller should just copy off the vars level attr for factors */
92
            INTEGER(*ret)[idx_or_len] = sqlite3_column_int(stmt, 0);
94
            INTEGER(*ret)[idx_or_len] = sqlite3_column_int(stmt, 0);
93
        } else added = 0;
95
        } else added = 0;
-
 
96
    } else if (stmt == NULL) {
-
 
97
        const char *coltype = sqlite3_column_decltype(stmt, 0);
-
 
98
        if (sqlite3_column_type(stmt, 0) == SQLITE_NULL) {
-
 
99
            added = 0;
-
 
100
        } else if (strcmp(coltype, "text") == 0) {
-
 
101
            SET_STRING_ELT(*ret, idx_or_len, NA_STRING);
-
 
102
        } else if (strcmp(coltype, "double") == 0) {
-
 
103
            REAL(*ret)[idx_or_len] = NA_REAL;
-
 
104
        } else if (strcmp(coltype, "bit") == 0 || strcmp(coltype, "integer") == 0 ||
-
 
105
                   strcmp(coltype, "int") == 0) {
-
 
106
            INTEGER(*ret)[idx_or_len] = NA_INTEGER;
-
 
107
        } else added = 0;
94
    }
108
    }
95
        
109
        
96
    return added;
110
    return added;
97
}
111
}
98
 
112
 
99
/****************************************************************************
113
/****************************************************************************
100
 * SVEC FUNCTIONS
114
 * SVEC FUNCTIONS
101
 ****************************************************************************/
115
 ****************************************************************************/
102
SEXP sdf_get_variable(SEXP sdf, SEXP name) {
116
SEXP sdf_get_variable(SEXP sdf, SEXP name) {
103
    char *iname, *varname, *svec_type = NULL;
117
    char *iname, *varname, *svec_type = NULL;
104
    const char *coltype;
118
    const char *coltype;
105
    int type = -1, res, nprotected = 0;
119
    int type = -1, res, nprotected = 0;
106
    SEXP ret, value;
120
    SEXP ret, value;
107
 
121
 
108
    if (!IS_CHARACTER(name)) {
122
    if (!IS_CHARACTER(name)) {
109
        error("argument is not a string.\n");
123
        error("argument is not a string.\n");
110
    }
124
    }
111
 
125
 
112
    iname = SDF_INAME(sdf);
126
    iname = SDF_INAME(sdf);
113
    varname = CHAR_ELT(name, 0);
127
    varname = CHAR_ELT(name, 0);
114
 
128
 
115
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
129
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
116
 
130
 
117
    /* check if sdf & varname w/in that sdf exists */
131
    /* check if sdf & varname w/in that sdf exists */
118
    sqlite3_stmt *stmt;
132
    sqlite3_stmt *stmt;
119
    sprintf(g_sql_buf[0], "select [%s] from [%s].sdf_data", varname, iname);
133
    sprintf(g_sql_buf[0], "select [%s] from [%s].sdf_data", varname, iname);
120
 
134
 
121
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
135
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
122
 
136
 
123
    if (_sqlite_error(res)) return R_NilValue;
137
    if (_sqlite_error(res)) return R_NilValue;
124
 
138
 
125
    coltype = sqlite3_column_decltype(stmt, 0);
139
    coltype = sqlite3_column_decltype(stmt, 0);
126
    sqlite3_finalize(stmt);
140
    sqlite3_finalize(stmt);
127
    
141
    
128
    PROTECT(ret = NEW_LIST(2)); nprotected++;
142
    PROTECT(ret = NEW_LIST(3)); nprotected++;
129
 
143
 
130
    /* set list names */
144
    /* set list names */
131
    PROTECT(value = NEW_CHARACTER(2)); nprotected++;
145
    PROTECT(value = NEW_CHARACTER(3)); nprotected++;
132
    SET_STRING_ELT(value, 0, mkChar("iname"));
146
    SET_STRING_ELT(value, 0, mkChar("iname"));
-
 
147
    SET_STRING_ELT(value, 1, mkChar("tblname"));
133
    SET_STRING_ELT(value, 1, mkChar("varname"));
148
    SET_STRING_ELT(value, 2, mkChar("varname"));
134
    SET_NAMES(ret, value);
149
    SET_NAMES(ret, value);
135
 
150
 
136
    /* set list values */
151
    /* set list values */
137
    SET_VECTOR_ELT(ret, 0, mkString(iname));
152
    SET_VECTOR_ELT(ret, 0, mkString(iname));
-
 
153
    SET_VECTOR_ELT(ret, 1, mkString("sdf_data"));
138
    SET_VECTOR_ELT(ret, 1, mkString(varname));
154
    SET_VECTOR_ELT(ret, 2, mkString(varname));
139
 
155
 
140
    /* set class */
156
    /* set class */
141
    if (strcmp(coltype, "text") == 0) svec_type = "character";
157
    if (strcmp(coltype, "text") == 0) svec_type = "character";
142
    else if (strcmp(coltype, "double") == 0) svec_type = "numeric";
158
    else if (strcmp(coltype, "double") == 0) svec_type = "numeric";
143
    else if (strcmp(coltype, "bit") == 0) svec_type = "logical";
159
    else if (strcmp(coltype, "bit") == 0) svec_type = "logical";
144
    else if (strcmp(coltype, "integer") == 0 || strcmp(coltype, "int") == 0) {
160
    else if (strcmp(coltype, "integer") == 0 || strcmp(coltype, "int") == 0) {
145
        /* determine if int, factor or ordered */
161
        /* determine if int, factor or ordered */
146
        type = _get_factor_levels1(iname, varname, ret);
162
        type = _get_factor_levels1(iname, varname, ret);
147
        switch(type) {
163
        switch(type) {
148
            case VAR_INTEGER: svec_type = "integer"; break;
164
            case VAR_INTEGER: svec_type = "integer"; break;
149
            case VAR_FACTOR: svec_type = "factor"; break;
165
            case VAR_FACTOR: svec_type = "factor"; break;
150
            case VAR_ORDERED: svec_type = "ordered";
166
            case VAR_ORDERED: svec_type = "ordered";
151
        }
167
        }
152
 
168
 
153
    }
169
    }
154
 
170
 
155
    SET_CLASS(ret, mkString("sqlite.vector"));
171
    SET_CLASS(ret, mkString("sqlite.vector"));
156
    SET_SDFVECTORTYPE(ret, mkString(svec_type));
172
    SET_SDFVECTORTYPE(ret, mkString(svec_type));
157
 
173
 
158
    UNPROTECT(nprotected);
174
    UNPROTECT(nprotected);
159
    return ret;
175
    return ret;
160
 
176
 
161
}
177
}
162
 
178
 
163
 
179
 
164
SEXP sdf_get_variable_length(SEXP svec) {
180
SEXP sdf_get_variable_length(SEXP svec) {
165
    char *iname = SDF_INAME(svec);
181
    char *iname = SDF_INAME(svec);
166
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
182
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
167
 
183
 
168
    return ScalarInteger(_get_row_count2(iname, 1));
184
    return ScalarInteger(_get_row_count2(iname, 1));
169
}
185
}
170
 
186
 
171
    
187
    
172
SEXP sdf_get_variable_index(SEXP svec, SEXP idx) {
188
SEXP sdf_get_variable_index(SEXP svec, SEXP idx) {
173
    SEXP ret = R_NilValue, tmp;
189
    SEXP ret = R_NilValue, tmp;
174
    char *iname = SDF_INAME(svec), *varname = SVEC_VARNAME(svec);
190
    char *iname = SDF_INAME(svec), *tblname = SVEC_TBLNAME(svec),
-
 
191
         *varname = SVEC_VARNAME(svec);
175
    int index, idxlen, i, retlen=0, res;
192
    int index, idxlen, i, retlen=0, res;
176
    sqlite3_stmt *stmt;
193
    sqlite3_stmt *stmt;
177
 
194
 
178
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
195
    if (!USE_SDF1(iname, TRUE, FALSE)) return R_NilValue;
179
 
196
 
180
    idxlen = LENGTH(idx);
197
    idxlen = LENGTH(idx);
181
    if (idxlen < 1) return ret;
198
    if (idxlen < 1) return ret;
182
 
199
 
183
    sprintf(g_sql_buf[0], "select [%s] from [%s].sdf_data limit ?,1",
200
    sprintf(g_sql_buf[0], "select [%s] from [%s].[%s] limit ?,1",
184
            varname, iname);
201
            varname, iname, tblname);
185
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
202
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
-
 
203
    if (_sqlite_error(res)) error("cannot complete request");
186
 
204
 
187
    /* get data based on index */
205
    /* get data based on index */
188
    if (IS_NUMERIC(idx)) {
206
    if (IS_NUMERIC(idx)) {
189
        index = ((int) REAL(idx)[0]) - 1;
207
        index = ((int) REAL(idx)[0]) - 1;
190
        if (index < 0 && idxlen == 1) return ret;
208
        if (index < 0 && idxlen == 1) return ret;
191
 
209
 
192
        if (index >= 0) {
210
        if (index >= 0) {
193
            sqlite3_bind_int(stmt, 1, index);
211
            sqlite3_bind_int(stmt, 1, index);
194
            res = sqlite3_step(stmt);
212
            res = sqlite3_step(stmt);
195
            if (res == SQLITE_ROW) { 
213
            if (res == SQLITE_ROW) { 
196
                retlen = _get_vector_index_typed_result(stmt, &ret, idxlen);
214
                retlen = _get_vector_index_typed_result(stmt, &ret, idxlen);
197
            } 
215
            } 
198
        } 
216
        } 
199
        
217
        
200
        if (index < 0 || res != SQLITE_ROW) {
218
        if (index < 0 || res != SQLITE_ROW) {
201
            /* something wrong w/ 1st idx, and it is quietly ignored. we make 
219
            /* something wrong w/ 1st idx, and it is quietly ignored. we make 
202
             * a "dummy" call to setup the SEXP */
220
             * a "dummy" call to setup the SEXP */
203
            sqlite3_reset(stmt);
221
            sqlite3_reset(stmt);
204
            sqlite3_bind_int(stmt, 1, 0);
222
            sqlite3_bind_int(stmt, 1, 0);
205
            res = sqlite3_step(stmt);
223
            res = sqlite3_step(stmt);
206
            if (res == SQLITE_ROW) {
224
            if (res == SQLITE_ROW) {
207
                _get_vector_index_typed_result(stmt, &ret, idxlen - 1);
225
                _get_vector_index_typed_result(stmt, &ret, idxlen - 1);
208
            }
226
            }
209
            retlen = 0;
227
            retlen = 0;
210
        }
228
        }
211
 
229
 
212
 
230
 
213
        if (idxlen > 1) {
231
        if (idxlen > 1) {
214
            for (i = 1; i < idxlen; i++) {
232
            for (i = 1; i < idxlen; i++) {
215
                index = ((int) REAL(idx)[i]) - 1;
233
                index = ((int) REAL(idx)[i]) - 1;
216
                if (index < 0) continue;
234
                if (index < 0) continue;
217
                sqlite3_reset(stmt);
235
                sqlite3_reset(stmt);
218
                sqlite3_bind_int(stmt, 1, index);
236
                sqlite3_bind_int(stmt, 1, index);
219
                res = sqlite3_step(stmt);
237
                res = sqlite3_step(stmt);
220
                if (res == SQLITE_ROW)
238
                if (res == SQLITE_ROW) {
221
                    retlen += _get_vector_index_typed_result(stmt, &ret, retlen); 
239
                    retlen += _get_vector_index_typed_result(stmt, &ret, retlen); 
-
 
240
                } else {
-
 
241
                    retlen += _get_vector_index_typed_result(NULL, &ret, retlen); 
-
 
242
                }
222
            }
243
            }
223
        }
244
        }
224
    } else if (IS_INTEGER(idx)) {
245
    } else if (IS_INTEGER(idx)) {
225
        /* similar to REAL (IS_NUMERIC) above, except that we don't have
246
        /* similar to REAL (IS_NUMERIC) above, except that we don't have
226
         * to cast idx to int. can't refactor this out, sucks. */
247
         * to cast idx to int. can't refactor this out, sucks. */
227
        index = INTEGER(idx)[0] - 1;
248
        index = INTEGER(idx)[0] - 1;
228
        if (index < 0 && idxlen == 1) return ret;
249
        if (index < 0 && idxlen == 1) return ret;
229
 
250
 
230
        if (index >= 0) {
251
        if (index >= 0) {
231
            sqlite3_bind_int(stmt, 1, index);
252
            sqlite3_bind_int(stmt, 1, index);
232
            res = sqlite3_step(stmt);
253
            res = sqlite3_step(stmt);
233
            if (res == SQLITE_ROW) { 
254
            if (res == SQLITE_ROW) { 
234
                retlen = _get_vector_index_typed_result(stmt, &ret, idxlen);
255
                retlen = _get_vector_index_typed_result(stmt, &ret, idxlen);
235
            } 
256
            } 
236
        } 
257
        } 
237
        
258
        
238
        if (index < 0 || res != SQLITE_ROW) {
259
        if (index < 0 || res != SQLITE_ROW) {
239
            /* something wrong w/ 1st idx, and it is quietly ignored. we make 
260
            /* something wrong w/ 1st idx, and it is quietly ignored. we make 
240
             * a "dummy" call to setup the SEXP */
261
             * a "dummy" call to setup the SEXP */
241
            sqlite3_reset(stmt);
262
            sqlite3_reset(stmt);
242
            sqlite3_bind_int(stmt, 1, 0);
263
            sqlite3_bind_int(stmt, 1, 0);
243
            res = sqlite3_step(stmt);
264
            res = sqlite3_step(stmt);
244
            if (res == SQLITE_ROW) {
265
            if (res == SQLITE_ROW) {
245
                _get_vector_index_typed_result(stmt, &ret, idxlen - 1);
266
                _get_vector_index_typed_result(stmt, &ret, idxlen - 1);
246
            }
267
            }
247
            retlen = 0;
268
            retlen = 0;
248
        }
269
        }
249
 
270
 
250
        if (idxlen > 1) {
271
        if (idxlen > 1) {
251
            for (i = 1; i < idxlen; i++) {
272
            for (i = 1; i < idxlen; i++) {
252
                index = INTEGER(idx)[i] - 1;
273
                index = INTEGER(idx)[i] - 1;
253
                if (index < 0) continue;
274
                if (index < 0) continue;
254
                sqlite3_reset(stmt);
275
                sqlite3_reset(stmt);
255
                sqlite3_bind_int(stmt, 1, index);
276
                sqlite3_bind_int(stmt, 1, index);
256
                sqlite3_step(stmt);
277
                res = sqlite3_step(stmt);
-
 
278
                if (res == SQLITE_ROW) {
257
                retlen += _get_vector_index_typed_result(stmt, &ret, retlen); 
279
                    retlen += _get_vector_index_typed_result(stmt, &ret, retlen); 
-
 
280
                } else {
-
 
281
                    retlen += _get_vector_index_typed_result(NULL, &ret, retlen);
-
 
282
                }
258
            }
283
            }
259
        }
284
        }
260
 
285
 
261
    } else if (IS_LOGICAL(idx)) {
286
    } else if (IS_LOGICAL(idx)) {
262
        /* have to deal with recycling */
287
        /* have to deal with recycling */
263
        int veclen = _get_row_count2(iname, 1);
288
        int veclen = _get_row_count2(iname, 1);
264
 
289
 
265
        /* find if there is any TRUE element in the vector */
290
        /* find if there is any TRUE element in the vector */
266
        for (i = 0; i < idxlen && i < veclen; i++) {
291
        for (i = 0; i < idxlen && i < veclen; i++) {
267
            if (LOGICAL(idx)[i]) {
292
            if (LOGICAL(idx)[i]) {
268
                sqlite3_bind_int(stmt, 1, i);
293
                sqlite3_bind_int(stmt, 1, i);
269
                sqlite3_step(stmt);
294
                sqlite3_step(stmt);
270
                /* there are at least (idxlen-i) TRUE per cycle of the LOGICAL
295
                /* there are at least (idxlen-i) TRUE per cycle of the LOGICAL
271
                 * index. there are at least (veclen/idxlen) cycles (int div).
296
                 * index. there are at least (veclen/idxlen) cycles (int div).
272
                 * at the last cycle, if (veclen%idxlen > 0), there will be
297
                 * at the last cycle, if (veclen%idxlen > 0), there will be
273
                 * at least (veclen%idxlen - i) if veclen%idxlen > i */
298
                 * at least (veclen%idxlen - i) if veclen%idxlen > i */
274
                retlen = (idxlen-i) * (veclen/idxlen);
299
                retlen = (idxlen-i) * (veclen/idxlen);
275
                if (veclen%idxlen > i) retlen += (veclen%idxlen - i);
300
                if (veclen%idxlen > i) retlen += (veclen%idxlen - i);
276
 
301
 
277
                /* create the vector */
302
                /* create the vector */
278
                retlen = _get_vector_index_typed_result(stmt, &ret, retlen);
303
                retlen = _get_vector_index_typed_result(stmt, &ret, retlen);
279
                break;
304
                break;
280
            }
305
            }
281
        }
306
        }
282
 
307
 
283
        if (i < idxlen && i < veclen) {
308
        if (i < idxlen && i < veclen) {
284
            for (i++; i < veclen; i++) {
309
            for (i++; i < veclen; i++) {
285
                if (LOGICAL(idx)[i%idxlen]) {
310
                if (LOGICAL(idx)[i%idxlen]) {
286
                    sqlite3_reset(stmt);
311
                    sqlite3_reset(stmt);
287
                    sqlite3_bind_int(stmt, 1, i);
312
                    sqlite3_bind_int(stmt, 1, i);
288
                    sqlite3_step(stmt);
313
                    res = sqlite3_step(stmt);
-
 
314
                    if (res == SQLITE_ROW) {
289
                    retlen += _get_vector_index_typed_result(stmt, &ret, retlen);
315
                        retlen += _get_vector_index_typed_result(stmt, &ret, retlen);
-
 
316
                    } else {
-
 
317
                        retlen += _get_vector_index_typed_result(NULL, &ret, retlen);
-
 
318
                    }
290
                }
319
                }
291
            }
320
            }
292
        }
321
        }
293
    }
322
    }
294
 
323
 
295
    sqlite3_finalize(stmt);
324
    sqlite3_finalize(stmt);
296
 
325
 
297
    if (ret != R_NilValue) {
326
    if (ret != R_NilValue) {
298
        ret = _shrink_vector(ret, retlen);
327
        ret = _shrink_vector(ret, retlen);
299
        tmp = GET_LEVELS(svec);
328
        tmp = GET_LEVELS(svec);
300
        if (tmp != R_NilValue) {
329
        if (tmp != R_NilValue) {
301
            SET_LEVELS(ret, duplicate(tmp));
330
            SET_LEVELS(ret, duplicate(tmp));
302
            if (TEST_SDFVECTORTYPE(svec, "factor")) {
331
            if (TEST_SDFVECTORTYPE(svec, "factor")) {
303
                SET_CLASS(ret, mkString("factor"));
332
                SET_CLASS(ret, mkString("factor"));
304
            } else {
333
            } else {
305
                PROTECT(tmp = NEW_CHARACTER(2));
334
                PROTECT(tmp = NEW_CHARACTER(2));
306
                SET_STRING_ELT(tmp, 0, mkChar("ordered"));
335
                SET_STRING_ELT(tmp, 0, mkChar("ordered"));
307
                SET_STRING_ELT(tmp, 1, mkChar("factor"));
336
                SET_STRING_ELT(tmp, 1, mkChar("factor"));
308
                UNPROTECT(1);
337
                UNPROTECT(1);
309
            }
338
            }
310
        }
339
        }
311
    }
340
    }
312
 
341
 
313
    return ret;
342
    return ret;
314
}
343
}
315
 
344
 
316
/* sqlite.vector.[<- */
345
/* sqlite.vector.[<- */
317
SEXP sdf_set_variable_index(SEXP svec, SEXP idx, SEXP value) {
346
SEXP sdf_set_variable_index(SEXP svec, SEXP idx, SEXP value) {
318
    int idx_len, val_len;
347
    int idx_len, val_len;
319
 
348
 
320
    /* match levels if factor or ordered */
349
    /* match levels if factor or ordered */
321
    if (inherits(value, "ordered")) {
350
    if (inherits(value, "ordered")) {
322
    } else if (inherits(value, "factor")) {
351
    } else if (inherits(value, "factor")) {
323
    }
352
    }
324
 
353
 
325
    idx_len = LENGTH(idx);
354
    idx_len = LENGTH(idx);
326
    val_len = LENGTH(value);
355
    val_len = LENGTH(value);
327
 
356
 
328
    if (IS_NUMERIC(idx)) {
357
    if (IS_NUMERIC(idx)) {
329
    } else if (IS_INTEGER(idx)) {
358
    } else if (IS_INTEGER(idx)) {
330
    } else if (IS_LOGICAL(idx)) {
359
    } else if (IS_LOGICAL(idx)) {
331
    }
360
    }
332
 
361
 
333
    return R_NilValue;
362
    return R_NilValue;
334
}
363
}
335
 
364
 
336
SEXP sdf_variable_summary(SEXP svec, SEXP maxsum) {
365
SEXP sdf_variable_summary(SEXP svec, SEXP maxsum) {
337
    char *iname, *varname, *type;
366
    char *iname, *tblname, *varname, *type;
338
    sqlite3_stmt *stmt;
367
    sqlite3_stmt *stmt;
339
    int nprotected = 0;
368
    int nprotected = 0;
340
    SEXP ret, names;
369
    SEXP ret, names;
341
 
370
 
342
    iname = SDF_INAME(svec);
371
    iname = SDF_INAME(svec);
-
 
372
    tblname = SVEC_TBLNAME(svec);
343
    varname = SVEC_VARNAME(svec);
373
    varname = SVEC_VARNAME(svec);
344
    USE_SDF1(iname, TRUE, FALSE);
374
    USE_SDF1(iname, TRUE, FALSE);
345
 
375
 
346
    if ((TEST_SDFVECTORTYPE(svec, "ordered") && ((type = "ordered"))) ||
376
    if ((TEST_SDFVECTORTYPE(svec, "ordered") && ((type = "ordered"))) ||
347
            (TEST_SDFVECTORTYPE(svec, "factor") && ((type = "factor")))) {
377
            (TEST_SDFVECTORTYPE(svec, "factor") && ((type = "factor")))) {
348
        int nrows, i, max_rows = INTEGER(maxsum)[0];
378
        int nrows, i, max_rows = INTEGER(maxsum)[0];
349
 
379
 
350
 
380
 
351
        sprintf(g_sql_buf[0], "[%s].[%s %s]", iname, type, varname);
381
        sprintf(g_sql_buf[0], "[%s].[%s %s]", iname, type, varname);
352
        nrows = _get_row_count2(g_sql_buf[0], FALSE);
382
        nrows = _get_row_count2(g_sql_buf[0], FALSE);
353
        if (nrows <= max_rows) max_rows = nrows;
383
        if (nrows <= max_rows) max_rows = nrows;
354
        else { nrows = max_rows; max_rows--; }
384
        else { nrows = max_rows; max_rows--; }
355
 
385
 
356
        PROTECT(ret = NEW_INTEGER(nrows)); nprotected = 1;
386
        PROTECT(ret = NEW_INTEGER(nrows)); nprotected = 1;
357
        PROTECT(names = NEW_CHARACTER(nrows)); nprotected++;
387
        PROTECT(names = NEW_CHARACTER(nrows)); nprotected++;
358
 
388
 
359
        sprintf(g_sql_buf[0], "select [%s].[%s %s].label, count(*) from "
389
        sprintf(g_sql_buf[0], "select [%s].[%s %s].label, count(*) from "
360
                "[%s].sdf_data join [%s].[%s %s] on [%s].sdf_data.[%s]=[%s].[%s %s].level "
390
                "[%s].[%s] join [%s].[%s %s] on [%s].[%s].[%s]=[%s].[%s %s].level "
361
                "group by [%s].sdf_data.[%s], [%s].[%s %s].level order by count(*) desc",
391
                "group by [%s].[%s].[%s], [%s].[%s %s].level order by count(*) desc",
362
                iname, type, varname, iname, iname, type, varname, iname, varname,
392
                iname, type, varname, iname, tblname, iname, type, varname, iname, tblname, varname,
363
                iname, type, varname, iname, varname, iname, type, varname);
393
                iname, type, varname, iname, tblname, varname, iname, type, varname);
364
        sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
394
        sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
365
 
395
 
366
        for (i = 0; i < max_rows; i++) {
396
        for (i = 0; i < max_rows; i++) {
367
            sqlite3_step(stmt);
397
            sqlite3_step(stmt);
368
            SET_STRING_ELT(names, i, mkChar((char *)sqlite3_column_text(stmt, 0)));
398
            SET_STRING_ELT(names, i, mkChar((char *)sqlite3_column_text(stmt, 0)));
369
            INTEGER(ret)[i] = sqlite3_column_int(stmt, 1);
399
            INTEGER(ret)[i] = sqlite3_column_int(stmt, 1);
370
        }
400
        }
371
 
401
 
372
        if (nrows > max_rows) {
402
        if (nrows > max_rows) {
373
            int others_sum = 0;
403
            int others_sum = 0;
374
            SET_STRING_ELT(names, nrows-1, mkChar("(Others)"));
404
            SET_STRING_ELT(names, nrows-1, mkChar("(Others)"));
375
            while (sqlite3_step(stmt) == SQLITE_ROW) {
405
            while (sqlite3_step(stmt) == SQLITE_ROW) {
376
                others_sum += sqlite3_column_int(stmt, 1);
406
                others_sum += sqlite3_column_int(stmt, 1);
377
            }
407
            }
378
            INTEGER(ret)[nrows-1] = others_sum;
408
            INTEGER(ret)[nrows-1] = others_sum;
379
        }
409
        }
380
    } else if (TEST_SDFVECTORTYPE(svec, "logical")) {
410
    } else if (TEST_SDFVECTORTYPE(svec, "logical")) {
381
        sprintf(g_sql_buf[0], "select count(*) from "
411
        sprintf(g_sql_buf[0], "select count(*) from "
382
                "[%s].sdf_data group by [%s] order by [%s]", iname, varname, varname);
412
                "[%s].[%s] group by [%s] order by [%s]", iname, tblname, varname, varname);
383
 
413
 
384
        PROTECT(names = NEW_CHARACTER(3)); nprotected = 1;
414
        PROTECT(names = NEW_CHARACTER(3)); nprotected = 1;
385
        PROTECT(ret = NEW_CHARACTER(3)); nprotected++;
415
        PROTECT(ret = NEW_CHARACTER(3)); nprotected++;
386
 
416
 
387
        SET_STRING_ELT(names, 0, mkChar("Mode"));
417
        SET_STRING_ELT(names, 0, mkChar("Mode"));
388
        SET_STRING_ELT(names, 1, mkChar("FALSE"));
418
        SET_STRING_ELT(names, 1, mkChar("FALSE"));
389
        SET_STRING_ELT(names, 2, mkChar("TRUE"));
419
        SET_STRING_ELT(names, 2, mkChar("TRUE"));
390
 
420
 
391
        sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
421
        sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
392
        SET_STRING_ELT(ret, 0, mkChar("logical"));
422
        SET_STRING_ELT(ret, 0, mkChar("logical"));
393
        sqlite3_step(stmt);
423
        sqlite3_step(stmt);
394
        SET_STRING_ELT(ret, 1, mkChar((const char *)sqlite3_column_text(stmt, 0)));
424
        SET_STRING_ELT(ret, 1, mkChar((const char *)sqlite3_column_text(stmt, 0)));
395
        sqlite3_step(stmt);
425
        sqlite3_step(stmt);
396
        SET_STRING_ELT(ret, 2, mkChar((const char *)sqlite3_column_text(stmt, 0)));
426
        SET_STRING_ELT(ret, 2, mkChar((const char *)sqlite3_column_text(stmt, 0)));
397
 
427
 
398
    } else return R_NilValue;
428
    } else return R_NilValue;
399
 
429
 
400
    sqlite3_finalize(stmt);
430
    sqlite3_finalize(stmt);
401
    SET_NAMES(ret, names);
431
    SET_NAMES(ret, names);
402
    SET_CLASS(ret, mkString("table"));
432
    SET_CLASS(ret, mkString("table"));
403
    UNPROTECT(nprotected);
433
    UNPROTECT(nprotected);
404
    return ret;
434
    return ret;
405
}
435
}
406
            
436
            
407
 
437
 
408
/* the global accumulator should be safe if we only do 1 cummulative or
438
/* the global accumulator should be safe if we only do 1 cummulative or
409
 * aggregate at any time. it won't work for stuffs like "select max(col)-min(col)
439
 * aggregate at any time. it won't work for stuffs like "select max(col)-min(col)
410
 * from sdf_data". */
440
 * from sdf_data". */
411
static long double g_accumulator = 0.0; /* accumulator var for cumsum, cumprod, etc. */
441
static long double g_accumulator = 0.0; /* accumulator var for cumsum, cumprod, etc. */
412
static int g_start = 0;            /* flag for start of cummulation */
442
static int g_start = 0;            /* flag for start of cummulation */
413
static int g_narm = 0;             /* for Summary group */
443
static int g_narm = 0;             /* for Summary group */
414
 
444
 
415
void _init_sqlite_function_accumulator() {
445
void _init_sqlite_function_accumulator() {
416
    g_accumulator = 0.0;   /* initialize accumulator */
446
    g_accumulator = 0.0;   /* initialize accumulator */
417
    g_start = 1;           /* flag that we are at start of accumulating */
447
    g_start = 1;           /* flag that we are at start of accumulating */
418
}
448
}
419
 
449
 
420
SEXP sdf_do_variable_math(SEXP func, SEXP vector, SEXP other_args) {
450
SEXP sdf_do_variable_math(SEXP func, SEXP vector, SEXP other_args) {
421
    char *iname, *iname_src, *varname_src, *funcname;
451
    char *iname, *iname_src, *varname_src, *funcname;
422
    int namelen, res;
452
    int namelen, res;
423
    sqlite3_stmt *stmt;
453
    sqlite3_stmt *stmt;
424
 
454
 
425
    /* get data from arguments (function name and sqlite.vector stuffs) */
455
    /* get data from arguments (function name and sqlite.vector stuffs) */
426
    funcname = CHAR_ELT(func, 0);
456
    funcname = CHAR_ELT(func, 0);
427
    iname_src = SDF_INAME(vector);
457
    iname_src = SDF_INAME(vector);
428
    varname_src = SVEC_VARNAME(vector);
458
    varname_src = SVEC_VARNAME(vector);
429
 
459
 
430
    if (!USE_SDF1(iname_src, TRUE, TRUE)) return R_NilValue;
460
    if (!USE_SDF1(iname_src, TRUE, TRUE)) return R_NilValue;
431
 
461
 
432
    /* create a new sdf, with 1 column named V1 */
462
    /* create a new sdf, with 1 column named V1 */
433
    iname = _create_svector1(R_NilValue, "double", &namelen, TRUE);
463
    iname = _create_svector1(R_NilValue, "double", &namelen, TRUE);
434
 
464
 
435
    /* insert into <newsdf>.col, row.names select func(col), rownames */
465
    /* insert into <newsdf>.col, row.names select func(col), rownames */
436
    if (strcmp(funcname, "round") == 0 || strcmp(funcname, "signif") == 0) {
466
    if (strcmp(funcname, "round") == 0 || strcmp(funcname, "signif") == 0) {
437
        double digits = REAL(_getListElement(other_args, "digits"))[0];
467
        double digits = REAL(_getListElement(other_args, "digits"))[0];
438
        sprintf(g_sql_buf[0], "insert into [%s].sdf_data([row name], V1) "
468
        sprintf(g_sql_buf[0], "insert into [%s].sdf_data([row name], V1) "
439
                "select [row name], %s([%s],?) from [%s].sdf_data", iname, funcname,
469
                "select [row name], %s([%s],?) from [%s].sdf_data", iname, funcname,
440
                varname_src, iname_src);
470
                varname_src, iname_src);
441
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0); 
471
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0); 
442
        if (_sqlite_error(res)) goto vecmath_prepare_error;
472
        if (_sqlite_error(res)) goto vecmath_prepare_error;
443
        res = sqlite3_bind_double(stmt, 1, digits);
473
        res = sqlite3_bind_double(stmt, 1, digits);
444
    } else if (strcmp(funcname, "log") == 0) {
474
    } else if (strcmp(funcname, "log") == 0) {
445
        double base = REAL(_getListElement(other_args, "base"))[0];
475
        double base = REAL(_getListElement(other_args, "base"))[0];
446
        sprintf(g_sql_buf[0], "insert into [%s].sdf_data([row name], V1) "
476
        sprintf(g_sql_buf[0], "insert into [%s].sdf_data([row name], V1) "
447
                "select [row name], %s([%s],?) from [%s].sdf_data", iname, funcname,
477
                "select [row name], %s([%s],?) from [%s].sdf_data", iname, funcname,
448
                varname_src, iname_src);
478
                varname_src, iname_src);
449
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0); 
479
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0); 
450
        if (_sqlite_error(res)) goto vecmath_prepare_error;
480
        if (_sqlite_error(res)) goto vecmath_prepare_error;
451
        res = sqlite3_bind_double(stmt, 1, base);
481
        res = sqlite3_bind_double(stmt, 1, base);
452
    } else {
482
    } else {
453
        sprintf(g_sql_buf[0], "insert into [%s].sdf_data([row name], V1) "
483
        sprintf(g_sql_buf[0], "insert into [%s].sdf_data([row name], V1) "
454
                "select [row name], %s([%s]) from [%s].sdf_data", iname, funcname,
484
                "select [row name], %s([%s]) from [%s].sdf_data", iname, funcname,
455
                varname_src, iname_src);
485
                varname_src, iname_src);
456
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0); 
486
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0); 
457
    }
487
    }
458
 
488
 
459
    if (_sqlite_error(res)) {
489
    if (_sqlite_error(res)) {
460
vecmath_prepare_error:
490
vecmath_prepare_error:
461
        sprintf(g_sql_buf[0], "detach %s", iname);
491
        sprintf(g_sql_buf[0], "detach %s", iname);
462
        _sqlite_exec(g_sql_buf[0]);
492
        _sqlite_exec(g_sql_buf[0]);
463
 
493
 
464
        /* we will return a string with the file name, and do file.remove
494
        /* we will return a string with the file name, and do file.remove
465
         * at R */
495
         * at R */
466
        iname[namelen] = '.';
496
        iname[namelen] = '.';
467
        return mkString(iname);
497
        return mkString(iname);
468
    }
498
    }
469
 
499
 
470
    _init_sqlite_function_accumulator();
500
    _init_sqlite_function_accumulator();
471
    sqlite3_step(stmt);
501
    sqlite3_step(stmt);
472
    sqlite3_finalize(stmt);
502
    sqlite3_finalize(stmt);
473
 
503
 
474
    UNUSE_SDF2(iname);
504
    UNUSE_SDF2(iname);
475
    UNUSE_SDF2(iname_src);
505
    UNUSE_SDF2(iname_src);
476
 
506
 
477
    return _create_svector_sexp(iname, "V1", "numeric");
507
    return _create_svector_sexp(iname, "sdf_data", "V1", "numeric");
478
}
508
}
479
 
509
 
480
SEXP sdf_do_variable_op(SEXP func, SEXP vector, SEXP op2, SEXP arg_reversed) {
510
SEXP sdf_do_variable_op(SEXP func, SEXP vector, SEXP op2, SEXP arg_reversed) {
481
    char *iname = NULL, *iname_src, *varname_src, *funcname;
511
    char *iname = NULL, *iname_src, *varname_src, *funcname;
482
    int res, functype = -1, op2_len, svec_len, i, reversed;
512
    int res, functype = -1, op2_len, svec_len, i, reversed;
483
    sqlite3_stmt *stmt, *stmt2;
513
    sqlite3_stmt *stmt, *stmt2;
484
 
514
 
485
    /* get data from arguments (function name and sqlite.vector stuffs) */
515
    /* get data from arguments (function name and sqlite.vector stuffs) */
486
    funcname = CHAR_ELT(func, 0);
516
    funcname = CHAR_ELT(func, 0);
487
    iname_src = SDF_INAME(vector);
517
    iname_src = SDF_INAME(vector);
488
    varname_src = SVEC_VARNAME(vector);
518
    varname_src = SVEC_VARNAME(vector);
489
    reversed = LOGICAL(arg_reversed)[0];
519
    reversed = LOGICAL(arg_reversed)[0];
490
 
520
 
491
    if (!USE_SDF1(iname_src, TRUE, TRUE)) return R_NilValue;
521
    if (!USE_SDF1(iname_src, TRUE, TRUE)) return R_NilValue;
492
    switch(funcname[0]) {
522
    switch(funcname[0]) {
493
        case '+' :
523
        case '+' :
494
        case '-' :
524
        case '-' :
495
        case '*' :
525
        case '*' :
496
        case '/' :
526
        case '/' :
497
        case '^' :
527
        case '^' :
498
        case '%' : /* %% and %/% */ 
528
        case '%' : /* %% and %/% */ 
499
            functype = 0; break;  /* output is REAL */
529
            functype = 0; break;  /* output is REAL */
500
        case '&' :
530
        case '&' :
501
        case '|' :
531
        case '|' :
502
        case '!' :  /* ! and != */
532
        case '!' :  /* ! and != */
503
            if (funcname[1] == 0) { functype = 1; break; }
533
            if (funcname[1] == 0) { functype = 1; break; }
504
        case '=' :  /* == */
534
        case '=' :  /* == */
505
        case '<' :  /* < and <= */
535
        case '<' :  /* < and <= */
506
        case '>' :  /* > and >= */
536
        case '>' :  /* > and >= */
507
            functype = 2; 
537
            functype = 2; 
508
    }
538
    }
509
 
539
 
510
    svec_len = _get_row_count2(iname_src, 1);
540
    svec_len = _get_row_count2(iname_src, 1);
511
    if (functype == 0 || functype == 2 || (functype == 1 && funcname[0] != '!')) {
541
    if (functype == 0 || functype == 2 || (functype == 1 && funcname[0] != '!')) {
512
        char *insert_fmt_string1c, *insert_fmt_string1s;
542
        char *insert_fmt_string1c, *insert_fmt_string1s;
513
        char *insert_fmt_string2c, *insert_fmt_string2s;
543
        char *insert_fmt_string2c, *insert_fmt_string2s;
514
        int vec_idx, sdf_idx;
544
        int vec_idx, sdf_idx;
515
 
545
 
516
        iname = _create_svector1(R_NilValue, (functype == 0) ? "double" : "bit", NULL, TRUE);
546
        iname = _create_svector1(R_NilValue, (functype == 0) ? "double" : "bit", NULL, TRUE);
517
 
547
 
518
        /* insert_fmt_string prefixes:
548
        /* insert_fmt_string prefixes:
519
         * 1 - op2 is an ordinary vector, op2_len = 1, straightforward insert-select
549
         * 1 - op2 is an ordinary vector, op2_len = 1, straightforward insert-select
520
         * 2 - op2 is an ordinary vector, op2_len > 1, may need recycling, loop both sides
550
         * 2 - op2 is an ordinary vector, op2_len > 1, may need recycling, loop both sides
521
         * s - operator will be printed as string
551
         * s - operator will be printed as string
522
         * c - operator is either INTDIV or RMOD (int division, R modulo). initially, I
552
         * c - operator is either INTDIV or RMOD (int division, R modulo). initially, I
523
         *     thought I could cast both op to int then use / and % of sqlite, which is just
553
         *     thought I could cast both op to int then use / and % of sqlite, which is just
524
         *     the 2nd character of the funcname. however, R's INTDIV and casts to int 
554
         *     the 2nd character of the funcname. however, R's INTDIV and casts to int 
525
         *     after division (e.g. 3.5 %/% 1.5 == 2). RMOD operates on doubles too,
555
         *     after division (e.g. 3.5 %/% 1.5 == 2). RMOD operates on doubles too,
526
         *     which is the remainder of the largest int multiple of "divisor"
556
         *     which is the remainder of the largest int multiple of "divisor"
527
         *     (e.g. 3.5 %% 1.5 = 0.5)
557
         *     (e.g. 3.5 %% 1.5 = 0.5)
528
         */ 
558
         */ 
529
        if (functype == 1) { /* boolean binary operators */
559
        if (functype == 1) { /* boolean binary operators */
530
            if (!reversed) {
560
            if (!reversed) {
531
                insert_fmt_string1s = "insert into [%s].sdf_data "
561
                insert_fmt_string1s = "insert into [%s].sdf_data "
532
                            "select [row name], ([%s] != 0) %s (? != 0) from [%s].sdf_data";
562
                            "select [row name], ([%s] != 0) %s (? != 0) from [%s].sdf_data";
533
            } else {
563
            } else {
534
                insert_fmt_string1s = "insert into [%s].sdf_data "
564
                insert_fmt_string1s = "insert into [%s].sdf_data "
535
                            "select [row name], (? != 0) %s ([%s] != 0) from [%s].sdf_data";
565
                            "select [row name], (? != 0) %s ([%s] != 0) from [%s].sdf_data";
536
            }
566
            }
537
            insert_fmt_string2s = "insert into [%s].sdf_data values(?, (? != 0) %s (? != 0))";
567
            insert_fmt_string2s = "insert into [%s].sdf_data values(?, (? != 0) %s (? != 0))";
538
            /* no need for char version of fmt_string2, since %% only occurs for functype==0 */
568
            /* no need for char version of fmt_string2, since %% only occurs for functype==0 */
539
            insert_fmt_string1c = insert_fmt_string1s;
569
            insert_fmt_string1c = insert_fmt_string1s;
540
            insert_fmt_string2c = insert_fmt_string2s;
570
            insert_fmt_string2c = insert_fmt_string2s;
541
        } else {
571
        } else {
542
            if (!reversed) {
572
            if (!reversed) {
543
                insert_fmt_string1s = "insert into [%s].sdf_data "
573
                insert_fmt_string1s = "insert into [%s].sdf_data "
544
                            "select [row name], [%s] %s ? from [%s].sdf_data";
574
                            "select [row name], [%s] %s ? from [%s].sdf_data";
545
                insert_fmt_string1c = "insert into [%s].sdf_data "
575
                insert_fmt_string1c = "insert into [%s].sdf_data "
546
                            "select [row name], %s([%s],?) from [%s].sdf_data";
576
                            "select [row name], %s([%s],?) from [%s].sdf_data";
547
            } else {
577
            } else {
548
                insert_fmt_string1s = "insert into [%s].sdf_data "
578
                insert_fmt_string1s = "insert into [%s].sdf_data "
549
                            "select [row name], ? %s [%s] from [%s].sdf_data";
579
                            "select [row name], ? %s [%s] from [%s].sdf_data";
550
                insert_fmt_string1c = "insert into [%s].sdf_data "
580
                insert_fmt_string1c = "insert into [%s].sdf_data "
551
                            "select [row name], %s(?,[%s]) from [%s].sdf_data";
581
                            "select [row name], %s(?,[%s]) from [%s].sdf_data";
552
            }
582
            }
553
            /* fmt_string2c format operator from a char, used for %% and %/% */
583
            /* fmt_string2c format operator from a char, used for %% and %/% */
554
            insert_fmt_string2c = "insert into [%s].sdf_data values(?, %s(?,?))";
584
            insert_fmt_string2c = "insert into [%s].sdf_data values(?, %s(?,?))";
555
            insert_fmt_string2s = "insert into [%s].sdf_data values(?, ? %s ?)";
585
            insert_fmt_string2s = "insert into [%s].sdf_data values(?, ? %s ?)";
556
        }
586
        }
557
 
587
 
558
        /* for 2* insert statements, the values below specifies the index of bind().
588
        /* for 2* insert statements, the values below specifies the index of bind().
559
         * if !reversed, then e1 is svec, e2 is anything. otherwise, e2 is the svec */
589
         * if !reversed, then e1 is svec, e2 is anything. otherwise, e2 is the svec */
560
        if (reversed) { sdf_idx = 3; vec_idx = 2; }
590
        if (reversed) { sdf_idx = 3; vec_idx = 2; }
561
        else { sdf_idx = 2; vec_idx = 3; }
591
        else { sdf_idx = 2; vec_idx = 3; }
562
 
592
 
563
        if (IS_NUMERIC(op2)) {
593
        if (IS_NUMERIC(op2)) {
564
            op2_len = LENGTH(op2);
594
            op2_len = LENGTH(op2);
565
 
595
 
566
            if (op2_len == 1) {
596
            if (op2_len == 1) {
567
                if (funcname[0] == '%') {
597
                if (funcname[0] == '%') {
568
                    sprintf(g_sql_buf[2], insert_fmt_string1c, iname, 
598
                    sprintf(g_sql_buf[2], insert_fmt_string1c, iname, 
569
                            (funcname[1] == '%') ? "r_mod" : "r_intdiv", varname_src, iname_src);
599
                            (funcname[1] == '%') ? "r_mod" : "r_intdiv", varname_src, iname_src);
570
                } else {
600
                } else {
571
                    sprintf(g_sql_buf[2], insert_fmt_string1s, iname,
601
                    sprintf(g_sql_buf[2], insert_fmt_string1s, iname,
572
                            varname_src, funcname, iname_src);
602
                            varname_src, funcname, iname_src);
573
                }
603
                }
574
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
604
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
575
                _sqlite_error(res);
605
                _sqlite_error(res);
576
                sqlite3_bind_double(stmt, 1, REAL(op2)[0]);
606
                sqlite3_bind_double(stmt, 1, REAL(op2)[0]);
577
                sqlite3_step(stmt);
607
                sqlite3_step(stmt);
578
                sqlite3_finalize(stmt);
608
                sqlite3_finalize(stmt);
579
            } else if (op2_len <= svec_len) {  /* recycle op2 */
609
            } else if (op2_len <= svec_len) {  /* recycle op2 */
580
                sprintf(g_sql_buf[2], "select [row name], [%s] from [%s].sdf_data",
610
                sprintf(g_sql_buf[2], "select [row name], [%s] from [%s].sdf_data",
581
                        varname_src, iname_src);
611
                        varname_src, iname_src);
582
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
612
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
583
                _sqlite_error(res);
613
                _sqlite_error(res);
584
 
614
 
585
                if (funcname[0] == '%') {
615
                if (funcname[0] == '%') {
586
                    sprintf(g_sql_buf[2], insert_fmt_string2c, iname, 
616
                    sprintf(g_sql_buf[2], insert_fmt_string2c, iname, 
587
                            (funcname[1] == '%') ? "r_mod" : "r_intdiv");  
617
                            (funcname[1] == '%') ? "r_mod" : "r_intdiv");  
588
                    _sqlite_begin;
618
                    _sqlite_begin;
589
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
619
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
590
                    _sqlite_error(res);
620
                    _sqlite_error(res);
591
 
621
 
592
                    for (i = 0; i < svec_len; i++) {
622
                    for (i = 0; i < svec_len; i++) {
593
                        sqlite3_step(stmt2);
623
                        sqlite3_step(stmt2);
594
 
624
 
595
                        sqlite3_reset(stmt);
625
                        sqlite3_reset(stmt);
596
                        sqlite3_bind_text(stmt, 1, (char *)sqlite3_column_text(stmt2, 0), -1, SQLITE_STATIC);
626
                        sqlite3_bind_text(stmt, 1, (char *)sqlite3_column_text(stmt2, 0), -1, SQLITE_STATIC);
597
                        sqlite3_bind_int(stmt, sdf_idx, (int)sqlite3_column_double(stmt2, 1));
627
                        sqlite3_bind_int(stmt, sdf_idx, (int)sqlite3_column_double(stmt2, 1));
598
                        sqlite3_bind_int(stmt, vec_idx, (int)REAL(op2)[i % op2_len]);
628
                        sqlite3_bind_int(stmt, vec_idx, (int)REAL(op2)[i % op2_len]);
599
                        sqlite3_step(stmt);
629
                        sqlite3_step(stmt);
600
                    }
630
                    }
601
                    sqlite3_finalize(stmt);
631
                    sqlite3_finalize(stmt);
602
                    sqlite3_finalize(stmt2);
632
                    sqlite3_finalize(stmt2);
603
                    _sqlite_commit;
633
                    _sqlite_commit;
604
                } else { /* non-integer binary operation */
634
                } else { /* non-integer binary operation */
605
                    sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
635
                    sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
606
                    _sqlite_begin;
636
                    _sqlite_begin;
607
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
637
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
608
                    _sqlite_error(res);
638
                    _sqlite_error(res);
609
 
639
 
610
                    for (i = 0; i < svec_len; i++) {
640
                    for (i = 0; i < svec_len; i++) {
611
                        sqlite3_step(stmt2);
641
                        sqlite3_step(stmt2);
612
 
642
 
613
                        sqlite3_reset(stmt);
643
                        sqlite3_reset(stmt);
614
                        sqlite3_bind_text(stmt, 1, (char *)sqlite3_column_text(stmt2, 0), -1, SQLITE_STATIC);
644
                        sqlite3_bind_text(stmt, 1, (char *)sqlite3_column_text(stmt2, 0), -1, SQLITE_STATIC);
615
                        sqlite3_bind_double(stmt, sdf_idx, sqlite3_column_double(stmt2, 1));
645
                        sqlite3_bind_double(stmt, sdf_idx, sqlite3_column_double(stmt2, 1));
616
                        sqlite3_bind_double(stmt, vec_idx, REAL(op2)[i % op2_len]);
646
                        sqlite3_bind_double(stmt, vec_idx, REAL(op2)[i % op2_len]);
617
                        sqlite3_step(stmt);
647
                        sqlite3_step(stmt);
618
                    }
648
                    }
619
                    sqlite3_finalize(stmt);
649
                    sqlite3_finalize(stmt);
620
                    sqlite3_finalize(stmt2);
650
                    sqlite3_finalize(stmt2);
621
                    _sqlite_commit;
651
                    _sqlite_commit;
622
                }
652
                }
623
            } else { /* op2_len > svec_len, recycle svec */
653
            } else { /* op2_len > svec_len, recycle svec */
624
                sprintf(g_sql_buf[1], "select [%s] from [%s].sdf_data",
654
                sprintf(g_sql_buf[1], "select [%s] from [%s].sdf_data",
625
                        varname_src, iname_src);
655
                        varname_src, iname_src);
626
 
656
 
627
                if (funcname[0] == '%') {
657
                if (funcname[0] == '%') {
628
                    sprintf(g_sql_buf[2], insert_fmt_string2c, iname, 
658
                    sprintf(g_sql_buf[2], insert_fmt_string2c, iname, 
629
                            (funcname[1] == '%') ? "r_mod" : "r_intdiv");
659
                            (funcname[1] == '%') ? "r_mod" : "r_intdiv");
630
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
660
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
631
                    _sqlite_error(res);
661
                    _sqlite_error(res);
632
                } else {
662
                } else {
633
                    sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
663
                    sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
634
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
664
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
635
                    _sqlite_error(res);
665
                    _sqlite_error(res);
636
                }
666
                }
637
 
667
 
638
                i = 0; _sqlite_begin;
668
                i = 0; _sqlite_begin;
639
                while (i < op2_len) {
669
                while (i < op2_len) {
640
                    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);
641
                    _sqlite_error(res);
671
                    _sqlite_error(res);
642
 
672
 
643
                    if (funcname[0] == '%') {
673
                    if (funcname[0] == '%') {
644
                        for ( ; i < op2_len && sqlite3_step(stmt) == SQLITE_ROW ; i++) {
674
                        for ( ; i < op2_len && sqlite3_step(stmt) == SQLITE_ROW ; i++) {
645
                            sqlite3_step(stmt2);
675
                            sqlite3_step(stmt2);
646
 
676
 
647
                            sqlite3_reset(stmt);
677
                            sqlite3_reset(stmt);
648
                            /* simplify my life, just use ints for row names so that we
678
                            /* simplify my life, just use ints for row names so that we
649
                             * don't have to worry about duplicates */
679
                             * don't have to worry about duplicates */
650
                            sqlite3_bind_int(stmt, 1, i); 
680
                            sqlite3_bind_int(stmt, 1, i); 
651
                            sqlite3_bind_int(stmt, sdf_idx, sqlite3_column_int(stmt2, 1));
681
                            sqlite3_bind_int(stmt, sdf_idx, sqlite3_column_int(stmt2, 1));
652
                            sqlite3_bind_int(stmt, vec_idx, (int)REAL(op2)[i % op2_len]);
682
                            sqlite3_bind_int(stmt, vec_idx, (int)REAL(op2)[i % op2_len]);
653
                            sqlite3_step(stmt);
683
                            sqlite3_step(stmt);
654
                        }
684
                        }
655
                    } else {
685
                    } else {
656
                        for ( ; i < op2_len && sqlite3_step(stmt) == SQLITE_ROW ; i++) {
686
                        for ( ; i < op2_len && sqlite3_step(stmt) == SQLITE_ROW ; i++) {
657
                            sqlite3_step(stmt2);
687
                            sqlite3_step(stmt2);
658
 
688
 
659
                            sqlite3_reset(stmt);
689
                            sqlite3_reset(stmt);
660
                            sqlite3_bind_int(stmt, 1, i);
690
                            sqlite3_bind_int(stmt, 1, i);
661
                            sqlite3_bind_double(stmt, sdf_idx, sqlite3_column_double(stmt2, 1));
691
                            sqlite3_bind_double(stmt, sdf_idx, sqlite3_column_double(stmt2, 1));
662
                            sqlite3_bind_double(stmt, vec_idx, REAL(op2)[i % op2_len]);
692
                            sqlite3_bind_double(stmt, vec_idx, REAL(op2)[i % op2_len]);
663
                            sqlite3_step(stmt);
693
                            sqlite3_step(stmt);
664
                        }
694
                        }
665
                    }
695
                    }
666
 
696
 
667
                    /* recycle on svec if we loop again */
697
                    /* recycle on svec if we loop again */
668
                    sqlite3_finalize(stmt2);
698
                    sqlite3_finalize(stmt2);
669
                }
699
                }
670
 
700
 
671
                sqlite3_finalize(stmt);
701
                sqlite3_finalize(stmt);
672
                _sqlite_commit;
702
                _sqlite_commit;
673
            }
703
            }
674
 
704
 
675
        } else if (IS_INTEGER(op2) || IS_LOGICAL(op2)) { /* I N T E G E R */
705
        } else if (IS_INTEGER(op2) || IS_LOGICAL(op2)) { /* I N T E G E R */
676
            /* we are taking advantage of the fact that logicals are stored as int.
706
            /* we are taking advantage of the fact that logicals are stored as int.
677
             * if this becomes untrue in the future, then this is a bug */
707
             * if this becomes untrue in the future, then this is a bug */
678
            /* we are binding double because ___ (?) */
708
            /* we are binding double because ___ (?) */
679
            op2_len = LENGTH(op2);
709
            op2_len = LENGTH(op2);
680
 
710
 
681
            if (op2_len == 1) {
711
            if (op2_len == 1) {
682
                if (funcname[0] == '%') {
712
                if (funcname[0] == '%') {
683
                    sprintf(g_sql_buf[2], insert_fmt_string1c, iname, 
713
                    sprintf(g_sql_buf[2], insert_fmt_string1c, iname, 
684
                            varname_src, (funcname[1] == '%') ? "r_mod" : "r_intdiv", iname_src);
714
                            varname_src, (funcname[1] == '%') ? "r_mod" : "r_intdiv", iname_src);
685
                } else {
715
                } else {
686
                    sprintf(g_sql_buf[2], insert_fmt_string1s, iname,
716
                    sprintf(g_sql_buf[2], insert_fmt_string1s, iname,
687
                            varname_src, funcname, iname_src);
717
                            varname_src, funcname, iname_src);
688
                }
718
                }
689
                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);
690
                _sqlite_error(res);
720
                _sqlite_error(res);
691
                sqlite3_bind_double(stmt, 1, (double)INTEGER(op2)[0]);
721
                sqlite3_bind_double(stmt, 1, (double)INTEGER(op2)[0]);
692
                sqlite3_step(stmt);
722
                sqlite3_step(stmt);
693
            } else if (op2_len <= svec_len) {
723
            } else if (op2_len <= svec_len) {
694
                sprintf(g_sql_buf[2], "select [row name], [%s] from [%s].sdf_data",
724
                sprintf(g_sql_buf[2], "select [row name], [%s] from [%s].sdf_data",
695
                        varname_src, iname_src);
725
                        varname_src, iname_src);
696
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
726
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
697
                _sqlite_error(res);
727
                _sqlite_error(res);
698
 
728
 
699
                if (funcname[0] == '%') {
729
                if (funcname[0] == '%') {
700
                    sprintf(g_sql_buf[2], insert_fmt_string2c, iname, 
730
                    sprintf(g_sql_buf[2], insert_fmt_string2c, iname, 
701
                            (funcname[1] == '%') ? "r_mod" : "r_intdiv");
731
                            (funcname[1] == '%') ? "r_mod" : "r_intdiv");
702
                    _sqlite_begin;
732
                    _sqlite_begin;
703
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
733
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
704
                    _sqlite_error(res);
734
                    _sqlite_error(res);
705
 
735
 
706
                    for (i = 0; i < svec_len; i++) {
736
                    for (i = 0; i < svec_len; i++) {
707
                        sqlite3_step(stmt2);
737
                        sqlite3_step(stmt2);
708
 
738
 
709
                        sqlite3_reset(stmt);
739
                        sqlite3_reset(stmt);
710
                        sqlite3_bind_text(stmt, 1, (char *)sqlite3_column_text(stmt2, 0), -1, SQLITE_STATIC);
740
                        sqlite3_bind_text(stmt, 1, (char *)sqlite3_column_text(stmt2, 0), -1, SQLITE_STATIC);
711
                        sqlite3_bind_int(stmt, sdf_idx, sqlite3_column_int(stmt2, 1));
741
                        sqlite3_bind_int(stmt, sdf_idx, sqlite3_column_int(stmt2, 1));
712
                        sqlite3_bind_int(stmt, vec_idx, INTEGER(op2)[i % op2_len]);
742
                        sqlite3_bind_int(stmt, vec_idx, INTEGER(op2)[i % op2_len]);
713
                        sqlite3_step(stmt);
743
                        sqlite3_step(stmt);
714
                    }
744
                    }
715
                    sqlite3_finalize(stmt);
745
                    sqlite3_finalize(stmt);
716
                    sqlite3_finalize(stmt2);
746
                    sqlite3_finalize(stmt2);
717
                    _sqlite_commit;
747
                    _sqlite_commit;
718
                } else {
748
                } else {
719
                    sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
749
                    sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
720
                    _sqlite_begin;
750
                    _sqlite_begin;
721
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
751
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
722
                    _sqlite_error(res);
752
                    _sqlite_error(res);
723
 
753
 
724
                    for (i = 0; i < svec_len; i++) {
754
                    for (i = 0; i < svec_len; i++) {
725
                        sqlite3_step(stmt2);
755
                        sqlite3_step(stmt2);
726
 
756
 
727
                        sqlite3_reset(stmt);
757
                        sqlite3_reset(stmt);
728
                        sqlite3_bind_text(stmt, 1, (char *)sqlite3_column_text(stmt2, 0), -1, SQLITE_STATIC);
758
                        sqlite3_bind_text(stmt, 1, (char *)sqlite3_column_text(stmt2, 0), -1, SQLITE_STATIC);
729
                        sqlite3_bind_double(stmt, sdf_idx, sqlite3_column_double(stmt2, 1));
759
                        sqlite3_bind_double(stmt, sdf_idx, sqlite3_column_double(stmt2, 1));
730
                        sqlite3_bind_double(stmt, vec_idx, (double)INTEGER(op2)[i % op2_len]);
760
                        sqlite3_bind_double(stmt, vec_idx, (double)INTEGER(op2)[i % op2_len]);
731
                        sqlite3_step(stmt);
761
                        sqlite3_step(stmt);
732
                    }
762
                    }
733
                    sqlite3_finalize(stmt);
763
                    sqlite3_finalize(stmt);
734
                    sqlite3_finalize(stmt2);
764
                    sqlite3_finalize(stmt2);
735
                    _sqlite_commit;
765
                    _sqlite_commit;
736
                }
766
                }
737
            } else {
767
            } else {
738
                sprintf(g_sql_buf[1], "select [%s] from [%s].sdf_data",
768
                sprintf(g_sql_buf[1], "select [%s] from [%s].sdf_data",
739
                        varname_src, iname_src);
769
                        varname_src, iname_src);
740
 
770
 
741
                if (funcname[0] == '%') {
771
                if (funcname[0] == '%') {
742
                    sprintf(g_sql_buf[2], insert_fmt_string2c, iname,
772
                    sprintf(g_sql_buf[2], insert_fmt_string2c, iname,
743
                            (funcname[1] == '%') ? "r_mod" : "r_intdiv");
773
                            (funcname[1] == '%') ? "r_mod" : "r_intdiv");
744
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
774
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
745
                    _sqlite_error(res);
775
                    _sqlite_error(res);
746
                } else {
776
                } else {
747
                    sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
777
                    sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
748
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
778
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
749
                    _sqlite_error(res);
779
                    _sqlite_error(res);
750
                }
780
                }
751
               
781
               
752
                i = 0; _sqlite_begin; 
782
                i = 0; _sqlite_begin; 
753
                while (i < op2_len) {
783
                while (i < op2_len) {
754
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
784
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
755
                    _sqlite_error(res);
785
                    _sqlite_error(res);
756
 
786
 
757
                    if (funcname[0] == '%') {
787
                    if (funcname[0] == '%') {
758
                        for ( ; i < op2_len && sqlite3_step(stmt) == SQLITE_ROW ; i++) {
788
                        for ( ; i < op2_len && sqlite3_step(stmt) == SQLITE_ROW ; i++) {
759
                            sqlite3_step(stmt2);
789
                            sqlite3_step(stmt2);
760
 
790
 
761
                            sqlite3_reset(stmt);
791
                            sqlite3_reset(stmt);
762
                            /* simplify my life, just use ints for row names so that we
792
                            /* simplify my life, just use ints for row names so that we
763
                             * don't have to worry about duplicates */
793
                             * don't have to worry about duplicates */
764
                            sqlite3_bind_int(stmt, 1, i); 
794
                            sqlite3_bind_int(stmt, 1, i); 
765
                            sqlite3_bind_int(stmt, sdf_idx, sqlite3_column_int(stmt2, 1));
795
                            sqlite3_bind_int(stmt, sdf_idx, sqlite3_column_int(stmt2, 1));
766
                            sqlite3_bind_int(stmt, vec_idx, INTEGER(op2)[i % op2_len]);
796
                            sqlite3_bind_int(stmt, vec_idx, INTEGER(op2)[i % op2_len]);
767
                            sqlite3_step(stmt);
797
                            sqlite3_step(stmt);
768
                        }
798
                        }
769
                    } else {
799
                    } else {
770
                        for ( ; i < op2_len && sqlite3_step(stmt) == SQLITE_ROW ; i++) {
800
                        for ( ; i < op2_len && sqlite3_step(stmt) == SQLITE_ROW ; i++) {
771
                            sqlite3_step(stmt2);
801
                            sqlite3_step(stmt2);
772
 
802
 
773
                            sqlite3_reset(stmt);
803
                            sqlite3_reset(stmt);
774
                            sqlite3_bind_int(stmt, 1, i);
804
                            sqlite3_bind_int(stmt, 1, i);
775
                            sqlite3_bind_double(stmt, sdf_idx, sqlite3_column_double(stmt2, 1));
805
                            sqlite3_bind_double(stmt, sdf_idx, sqlite3_column_double(stmt2, 1));
776
                            sqlite3_bind_double(stmt, vec_idx, (double)INTEGER(op2)[i % op2_len]);
806
                            sqlite3_bind_double(stmt, vec_idx, (double)INTEGER(op2)[i % op2_len]);
777
                            sqlite3_step(stmt);
807
                            sqlite3_step(stmt);
778
                        }
808
                        }
779
                    }
809
                    }
780
 
810
 
781
                    /* recycle on svec if we loop again */
811
                    /* recycle on svec if we loop again */
782
                    sqlite3_finalize(stmt2);
812
                    sqlite3_finalize(stmt2);
783
                }
813
                }
784
                sqlite3_finalize(stmt);
814
                sqlite3_finalize(stmt);
785
                _sqlite_commit;
815
                _sqlite_commit;
786
            }
816
            }
787
 
817
 
788
        } else if (IS_CHARACTER(op2)) {
818
        } else if (IS_CHARACTER(op2)) {
789
            if (functype != 2) error("not supported");
819
            if (functype != 2) error("not supported");
790
 
820
 
791
            op2_len = LENGTH(op2);
821
            op2_len = LENGTH(op2);
792
 
822
 
793
            if (op2_len == 1) {
823
            if (op2_len == 1) {
794
                sprintf(g_sql_buf[2], insert_fmt_string1s, iname,
824
                sprintf(g_sql_buf[2], insert_fmt_string1s, iname,
795
                            varname_src, funcname, iname_src);
825
                            varname_src, funcname, iname_src);
796
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
826
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
797
                _sqlite_error(res);
827
                _sqlite_error(res);
798
                sqlite3_bind_text(stmt, 1, CHAR_ELT(op2, 0), -1, SQLITE_STATIC);
828
                sqlite3_bind_text(stmt, 1, CHAR_ELT(op2, 0), -1, SQLITE_STATIC);
799
                sqlite3_step(stmt);
829
                sqlite3_step(stmt);
800
            } else if (op2_len <= svec_len) {
830
            } else if (op2_len <= svec_len) {
801
                sprintf(g_sql_buf[2], "select [row name], [%s] from [%s].sdf_data",
831
                sprintf(g_sql_buf[2], "select [row name], [%s] from [%s].sdf_data",
802
                        varname_src, iname_src);
832
                        varname_src, iname_src);
803
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
833
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
804
                _sqlite_error(res);
834
                _sqlite_error(res);
805
 
835
 
806
                sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
836
                sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
807
                _sqlite_begin;
837
                _sqlite_begin;
808
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
838
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
809
                _sqlite_error(res);
839
                _sqlite_error(res);
810
 
840
 
811
                for (i = 0; i < svec_len; i++) {
841
                for (i = 0; i < svec_len; i++) {
812
                    sqlite3_step(stmt2);
842
                    sqlite3_step(stmt2);
813
 
843
 
814
                    sqlite3_reset(stmt);
844
                    sqlite3_reset(stmt);
815
                    sqlite3_bind_text(stmt, 1, (char *)sqlite3_column_text(stmt2, 0), -1, SQLITE_STATIC);
845
                    sqlite3_bind_text(stmt, 1, (char *)sqlite3_column_text(stmt2, 0), -1, SQLITE_STATIC);
816
                    sqlite3_bind_text(stmt, sdf_idx, (char *)sqlite3_column_text(stmt2, 1), -1, SQLITE_STATIC);
846
                    sqlite3_bind_text(stmt, sdf_idx, (char *)sqlite3_column_text(stmt2, 1), -1, SQLITE_STATIC);
817
                    sqlite3_bind_text(stmt, vec_idx, CHAR_ELT(op2, i % op2_len), -1 , SQLITE_STATIC);
847
                    sqlite3_bind_text(stmt, vec_idx, CHAR_ELT(op2, i % op2_len), -1 , SQLITE_STATIC);
818
                    sqlite3_step(stmt);
848
                    sqlite3_step(stmt);
819
                }
849
                }
820
                sqlite3_finalize(stmt2);
850
                sqlite3_finalize(stmt2);
821
                _sqlite_commit;
851
                _sqlite_commit;
822
            } else {
852
            } else {
823
                sprintf(g_sql_buf[1], "select [%s] from [%s].sdf_data",
853
                sprintf(g_sql_buf[1], "select [%s] from [%s].sdf_data",
824
                        varname_src, iname_src);
854
                        varname_src, iname_src);
825
 
855
 
826
                sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
856
                sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
827
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
857
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
828
                _sqlite_error(res);
858
                _sqlite_error(res);
829
               
859
               
830
                i = 0; _sqlite_begin; 
860
                i = 0; _sqlite_begin; 
831
                while (i < op2_len) {
861
                while (i < op2_len) {
832
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
862
                    res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
833
                    _sqlite_error(res);
863
                    _sqlite_error(res);
834
 
864
 
835
                    for ( ; i < op2_len && sqlite3_step(stmt) == SQLITE_ROW ; i++) {
865
                    for ( ; i < op2_len && sqlite3_step(stmt) == SQLITE_ROW ; i++) {
836
                        sqlite3_step(stmt2);
866
                        sqlite3_step(stmt2);
837
 
867
 
838
                        sqlite3_reset(stmt);
868
                        sqlite3_reset(stmt);
839
                        sqlite3_bind_int(stmt, 1, i);
869
                        sqlite3_bind_int(stmt, 1, i);
840
                        sqlite3_bind_text(stmt, sdf_idx, (char *)sqlite3_column_text(stmt2, 1), -1, SQLITE_STATIC);
870
                        sqlite3_bind_text(stmt, sdf_idx, (char *)sqlite3_column_text(stmt2, 1), -1, SQLITE_STATIC);
841
                        sqlite3_bind_text(stmt, vec_idx, CHAR_ELT(op2, i % op2_len), -1 , SQLITE_STATIC);
871
                        sqlite3_bind_text(stmt, vec_idx, CHAR_ELT(op2, i % op2_len), -1 , SQLITE_STATIC);
842
                        sqlite3_step(stmt);
872
                        sqlite3_step(stmt);
843
                    }
873
                    }
844
 
874
 
845
                    /* recycle on svec if we loop again */
875
                    /* recycle on svec if we loop again */
846
                    sqlite3_finalize(stmt2);
876
                    sqlite3_finalize(stmt2);
847
                }
877
                }
848
                sqlite3_finalize(stmt);
878
                sqlite3_finalize(stmt);
849
                _sqlite_commit;
879
                _sqlite_commit;
850
            }
880
            }
851
        
881
        
852
        } else if (inherits(op2, "sqlite.vector")) { 
882
        } else if (inherits(op2, "sqlite.vector")) { 
853
            /* op2 is surely not a factor, as handled by the R wrapper */
883
            /* op2 is surely not a factor, as handled by the R wrapper */
854
            /* even though it is impossible for reversed to be FALSE, still use
884
            /* even though it is impossible for reversed to be FALSE, still use
855
             * sdf_idx and vec_idx so that code would be less confusing */
885
             * sdf_idx and vec_idx so that code would be less confusing */
856
            char *iname_op2, *varname_op2;
886
            char *iname_op2, *varname_op2;
857
            sqlite3_stmt *stmt3;
887
            sqlite3_stmt *stmt3;
858
            iname_op2 = SDF_INAME(op2);
888
            iname_op2 = SDF_INAME(op2);
859
            varname_op2 = SVEC_VARNAME(op2);
889
            varname_op2 = SVEC_VARNAME(op2);
860
 
890
 
861
            if (!USE_SDF1(iname_op2, TRUE, TRUE)) {
891
            if (!USE_SDF1(iname_op2, TRUE, TRUE)) {
862
                /* delete created sqlite.vector */
892
                /* delete created sqlite.vector */
863
                warning("detaching created result SDF %s\n", iname);
893
                warning("detaching created result SDF %s\n", iname);
864
                sdf_detach_sdf(mkString(iname));
894
                sdf_detach_sdf(mkString(iname));
865
                return R_NilValue;
895
                return R_NilValue;
866
            }
896
            }
867
            _sqlite_begin;
897
            _sqlite_begin;
868
 
898
 
869
            op2_len = _get_row_count2(iname_op2, 1);
899
            op2_len = _get_row_count2(iname_op2, 1);
870
            
900
            
871
            sprintf(g_sql_buf[2], "select [%s] from [%s].sdf_data", varname_src, iname_src);
901
            sprintf(g_sql_buf[2], "select [%s] from [%s].sdf_data", varname_src, iname_src);
872
            res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
902
            res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt2, 0);
873
            _sqlite_error(res);
903
            _sqlite_error(res);
874
 
904
 
875
            sprintf(g_sql_buf[2], "select [%s] from [%s].sdf_data", varname_op2, iname_op2);
905
            sprintf(g_sql_buf[2], "select [%s] from [%s].sdf_data", varname_op2, iname_op2);
876
            res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt3, 0);
906
            res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt3, 0);
877
            _sqlite_error(res);
907
            _sqlite_error(res);
878
 
908
 
879
            if (funcname[0] == '%') {
909
            if (funcname[0] == '%') {
880
                sprintf(g_sql_buf[2], insert_fmt_string2c, iname,
910
                sprintf(g_sql_buf[2], insert_fmt_string2c, iname,
881
                            (funcname[1] == '%') ? "r_mod" : "r_intdiv");
911
                            (funcname[1] == '%') ? "r_mod" : "r_intdiv");
882
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
912
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
883
                _sqlite_error(res);
913
                _sqlite_error(res);
884
 
914
 
885
                if (svec_len == op2_len) {
915
                if (svec_len == op2_len) {
886
                    for (i = 0; i < svec_len; i++) {
916
                    for (i = 0; i < svec_len; i++) {
887
                        sqlite3_step(stmt2); sqlite3_step(stmt3);
917
                        sqlite3_step(stmt2); sqlite3_step(stmt3);
888
 
918
 
889
                        sqlite3_reset(stmt);
919
                        sqlite3_reset(stmt);
890
                        sqlite3_bind_int(stmt, 1, i);
920
                        sqlite3_bind_int(stmt, 1, i);
891
                        sqlite3_bind_int(stmt, sdf_idx, sqlite3_column_int(stmt2, 0));
921
                        sqlite3_bind_int(stmt, sdf_idx, sqlite3_column_int(stmt2, 0));
892
                        sqlite3_bind_int(stmt, vec_idx, sqlite3_column_int(stmt3, 0));
922
                        sqlite3_bind_int(stmt, vec_idx, sqlite3_column_int(stmt3, 0));
893
                        sqlite3_step(stmt);
923
                        sqlite3_step(stmt);
894
                    }
924
                    }
895
                } else if (svec_len < op2_len) {
925
                } else if (svec_len < op2_len) {
896
                    i = 0;
926
                    i = 0;
897
                    while (i < op2_len) {
927
                    while (i < op2_len) {
898
                        for ( ; sqlite3_step(stmt2) == SQLITE_ROW && i < op2_len; i++) {
928
                        for ( ; sqlite3_step(stmt2) == SQLITE_ROW && i < op2_len; i++) {
899
                            sqlite3_step(stmt3);
929
                            sqlite3_step(stmt3);
900
 
930
 
901
                            sqlite3_reset(stmt);
931
                            sqlite3_reset(stmt);
902
                            sqlite3_bind_int(stmt, 1, i);
932
                            sqlite3_bind_int(stmt, 1, i);
903
                            sqlite3_bind_int(stmt, sdf_idx, sqlite3_column_int(stmt2, 0));
933
                            sqlite3_bind_int(stmt, sdf_idx, sqlite3_column_int(stmt2, 0));
904
                            sqlite3_bind_int(stmt, vec_idx, sqlite3_column_int(stmt3, 0));
934
                            sqlite3_bind_int(stmt, vec_idx, sqlite3_column_int(stmt3, 0));
905
                            sqlite3_step(stmt);
935
                            sqlite3_step(stmt);
906
                        }
936
                        }
907
                        sqlite3_reset(stmt2);
937
                        sqlite3_reset(stmt2);
908
                    }
938
                    }
909
                } else {
939
                } else {
910
                    i = 0;
940
                    i = 0;
911
                    while (i < svec_len) {
941
                    while (i < svec_len) {
912
                        for ( ; sqlite3_step(stmt3) == SQLITE_ROW && i < svec_len; i++) {
942
                        for ( ; sqlite3_step(stmt3) == SQLITE_ROW && i < svec_len; i++) {
913
                            sqlite3_step(stmt2);
943
                            sqlite3_step(stmt2);
914
 
944
 
915
                            sqlite3_reset(stmt);
945
                            sqlite3_reset(stmt);
916
                            sqlite3_bind_int(stmt, 1, i);
946
                            sqlite3_bind_int(stmt, 1, i);
917
                            sqlite3_bind_int(stmt, sdf_idx, sqlite3_column_int(stmt2, 0));
947
                            sqlite3_bind_int(stmt, sdf_idx, sqlite3_column_int(stmt2, 0));
918
                            sqlite3_bind_int(stmt, vec_idx, sqlite3_column_int(stmt3, 0));
948
                            sqlite3_bind_int(stmt, vec_idx, sqlite3_column_int(stmt3, 0));
919
                            sqlite3_step(stmt);
949
                            sqlite3_step(stmt);
920
                        }
950
                        }
921
                        sqlite3_reset(stmt3);
951
                        sqlite3_reset(stmt3);
922
                    }
952
                    }
923
                }
953
                }
924
                _sqlite_commit;
954
                _sqlite_commit;
925
            } else { /* not an integer op %% or %/% */
955
            } else { /* not an integer op %% or %/% */
926
                sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
956
                sprintf(g_sql_buf[2], insert_fmt_string2s, iname, funcname);
927
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
957
                res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
928
                _sqlite_error(res);
958
                _sqlite_error(res);
929
 
959
 
930
                if (svec_len == op2_len) {
960
                if (svec_len == op2_len) {
931
                    for (i = 0; i < svec_len; i++) {
961
                    for (i = 0; i < svec_len; i++) {
932
                        sqlite3_step(stmt2); sqlite3_step(stmt3);
962
                        sqlite3_step(stmt2); sqlite3_step(stmt3);
933
 
963
 
934
                        sqlite3_reset(stmt);
964
                        sqlite3_reset(stmt);
935
                        sqlite3_bind_int(stmt, 1, i);
965
                        sqlite3_bind_int(stmt, 1, i);
936
                        sqlite3_bind_double(stmt, sdf_idx, sqlite3_column_double(stmt2, 0));
966
                        sqlite3_bind_double(stmt, sdf_idx, sqlite3_column_double(stmt2, 0));
937
                        sqlite3_bind_double(stmt, vec_idx, sqlite3_column_double(stmt3, 0));
967
                        sqlite3_bind_double(stmt, vec_idx, sqlite3_column_double(stmt3, 0));
938
                        sqlite3_step(stmt);
968
                        sqlite3_step(stmt);
939
                    }
969
                    }
940
                } else if (svec_len < op2_len) { /* recycle svec */
970
                } else if (svec_len < op2_len) { /* recycle svec */
941
                    i = 0;
971
                    i = 0;
942
                    while (i < op2_len) {
972
                    while (i < op2_len) {
943
                        for ( ; sqlite3_step(stmt2) == SQLITE_ROW && i < op2_len; i++) {
973
                        for ( ; sqlite3_step(stmt2) == SQLITE_ROW && i < op2_len; i++) {
944
                            sqlite3_step(stmt3);
974
                            sqlite3_step(stmt3);
945
 
975
 
946
                            sqlite3_reset(stmt);
976
                            sqlite3_reset(stmt);
947
                            sqlite3_bind_int(stmt, 1, i);
977
                            sqlite3_bind_int(stmt, 1, i);
948
                            sqlite3_bind_double(stmt, sdf_idx, sqlite3_column_double(stmt2, 0));
978
                            sqlite3_bind_double(stmt, sdf_idx, sqlite3_column_double(stmt2, 0));
949
                            sqlite3_bind_double(stmt, vec_idx, sqlite3_column_double(stmt3, 0));
979
                            sqlite3_bind_double(stmt, vec_idx, sqlite3_column_double(stmt3, 0));
950
                            sqlite3_step(stmt);
980
                            sqlite3_step(stmt);
951
                        }
981
                        }
952
                        sqlite3_reset(stmt2);
982
                        sqlite3_reset(stmt2);
953
                    }
983
                    }
954
                } else { /* svec_len > op2_len, recycle op2 */
984
                } else { /* svec_len > op2_len, recycle op2 */
955
                    i = 0;
985
                    i = 0;
956
                    while (i < svec_len) {
986
                    while (i < svec_len) {
957
                        for ( ; sqlite3_step(stmt3) == SQLITE_ROW && i < svec_len; i++) {
987
                        for ( ; sqlite3_step(stmt3) == SQLITE_ROW && i < svec_len; i++) {
958
                            sqlite3_step(stmt2);
988
                            sqlite3_step(stmt2);
959
 
989
 
960
                            sqlite3_reset(stmt);
990
                            sqlite3_reset(stmt);
961
                            sqlite3_bind_int(stmt, 1, i);
991
                            sqlite3_bind_int(stmt, 1, i);
962
                            sqlite3_bind_double(stmt, sdf_idx, sqlite3_column_double(stmt2, 0));
992
                            sqlite3_bind_double(stmt, sdf_idx, sqlite3_column_double(stmt2, 0));
963
                            sqlite3_bind_double(stmt, vec_idx, sqlite3_column_double(stmt3, 0));
993
                            sqlite3_bind_double(stmt, vec_idx, sqlite3_column_double(stmt3, 0));
964
                            sqlite3_step(stmt);
994
                            sqlite3_step(stmt);
965
                        }
995
                        }
966
                        sqlite3_reset(stmt3);
996
                        sqlite3_reset(stmt3);
967
                    }
997
                    }
968
                }
998
                }
969
            }
999
            }
970
 
1000
 
971
            sqlite3_finalize(stmt);
1001
            sqlite3_finalize(stmt);
972
            sqlite3_finalize(stmt2);
1002
            sqlite3_finalize(stmt2);
973
            sqlite3_finalize(stmt3);
1003
            sqlite3_finalize(stmt3);
974
            _sqlite_commit;
1004
            _sqlite_commit;
975
 
1005
 
976
            UNUSE_SDF2(iname_op2);
1006
            UNUSE_SDF2(iname_op2);
977
        }
1007
        }
978
    } else if (functype == 1 && funcname[0] != '!') { /* unary not operator */
1008
    } else if (functype == 1 && funcname[0] != '!') { /* unary not operator */
979
        iname = _create_svector1(R_NilValue, "bit", NULL, TRUE);
1009
        iname = _create_svector1(R_NilValue, "bit", NULL, TRUE);
980
        sprintf(g_sql_buf[2], "insert into [%s].sdf_data "
1010
        sprintf(g_sql_buf[2], "insert into [%s].sdf_data "
981
                    "select [row name], [%s] == 0 from [%s].sdf_data", 
1011
                    "select [row name], [%s] == 0 from [%s].sdf_data", 
982
                    iname, varname_src, iname_src);
1012
                    iname, varname_src, iname_src);
983
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
1013
        res = sqlite3_prepare(g_workspace, g_sql_buf[2], -1, &stmt, 0);
984
        _sqlite_error(res);
1014
        _sqlite_error(res);
985
        sqlite3_step(stmt);
1015
        sqlite3_step(stmt);
986
        sqlite3_finalize(stmt);
1016
        sqlite3_finalize(stmt);
987
    }
1017
    }
988
 
1018
 
989
    if (iname != NULL) {
1019
    if (iname != NULL) {
990
        return _create_svector_sexp(iname, "V1", 
1020
        return _create_svector_sexp(iname, "sdf_data", "V1", 
991
                (functype == 0) ? "numeric" : "logical");
1021
                (functype == 0) ? "numeric" : "logical");
992
        UNUSE_SDF2(iname);
1022
        UNUSE_SDF2(iname);
993
    }
1023
    }
994
 
1024
 
995
    UNUSE_SDF2(iname_src);
1025
    UNUSE_SDF2(iname_src);
996
 
1026
 
997
    return R_NilValue;
1027
    return R_NilValue;
998
 
1028
 
999
}
1029
}
1000
 
1030
 
1001
SEXP sdf_do_variable_summary(SEXP func, SEXP vector, SEXP na_rm) {
1031
SEXP sdf_do_variable_summary(SEXP func, SEXP vector, SEXP na_rm) {
1002
    char *iname_src, *varname_src, *funcname;
1032
    char *iname_src, *varname_src, *funcname;
1003
    int res;
1033
    int res;
1004
    sqlite3_stmt *stmt;
1034
    sqlite3_stmt *stmt;
1005
    double _ret = NA_REAL; SEXP ret;
1035
    double _ret = NA_REAL; SEXP ret;
1006
 
1036
 
1007
    /* get data from arguments (function name and sqlite.vector stuffs) */
1037
    /* get data from arguments (function name and sqlite.vector stuffs) */
1008
    funcname = CHAR_ELT(func, 0);
1038
    funcname = CHAR_ELT(func, 0);
1009
    iname_src = SDF_INAME(vector);
1039
    iname_src = SDF_INAME(vector);
1010
    varname_src = SVEC_VARNAME(vector);
1040
    varname_src = SVEC_VARNAME(vector);
1011
 
1041
 
1012
    if (!USE_SDF1(iname_src, TRUE, FALSE)) return R_NilValue;
1042
    if (!USE_SDF1(iname_src, TRUE, FALSE)) return R_NilValue;
1013
 
1043
 
1014
    g_narm = LOGICAL(na_rm)[0];
1044
    g_narm = LOGICAL(na_rm)[0];
1015
    if (strcmp(funcname, "range") == 0) {
1045
    if (strcmp(funcname, "range") == 0) {
1016
        /* special handling for range. use min then max */
1046
        /* special handling for range. use min then max */
1017
        _init_sqlite_function_accumulator();
1047
        _init_sqlite_function_accumulator();
1018
        sprintf(g_sql_buf[0], "select min_df([%s]) from [%s].sdf_data", varname_src, iname_src);
1048
        sprintf(g_sql_buf[0], "select min_df([%s]) from [%s].sdf_data", varname_src, iname_src);
1019
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
1049
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
1020
        if (_sqlite_error(res)) return R_NilValue;
1050
        if (_sqlite_error(res)) return R_NilValue;
1021
        sqlite3_step(stmt); sqlite3_finalize(stmt);
1051
        sqlite3_step(stmt); sqlite3_finalize(stmt);
1022
        _ret = g_accumulator;
1052
        _ret = g_accumulator;
1023
 
1053
 
1024
        if (R_IsNA(_ret) && !g_narm) {
1054
        if (R_IsNA(_ret) && !g_narm) {
1025
            PROTECT(ret = NEW_NUMERIC(2));
1055
            PROTECT(ret = NEW_NUMERIC(2));
1026
            REAL(ret)[0] = REAL(ret)[1] = R_NaReal;
1056
            REAL(ret)[0] = REAL(ret)[1] = R_NaReal;
1027
            goto __sdf_do_variable_summary_out;
1057
            goto __sdf_do_variable_summary_out;
1028
        }
1058
        }
1029
        
1059
        
1030
        _init_sqlite_function_accumulator();
1060
        _init_sqlite_function_accumulator();
1031
        sprintf(g_sql_buf[0], "select max_df([%s]) from [%s].sdf_data", varname_src, iname_src);
1061
        sprintf(g_sql_buf[0], "select max_df([%s]) from [%s].sdf_data", varname_src, iname_src);
1032
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
1062
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
1033
        if (_sqlite_error(res)) return R_NilValue;
1063
        if (_sqlite_error(res)) return R_NilValue;
1034
        sqlite3_step(stmt); sqlite3_finalize(stmt);
1064
        sqlite3_step(stmt); sqlite3_finalize(stmt);
1035
 
1065
 
1036
        /* if there is NA, then the if above should have caught it already */
1066
        /* if there is NA, then the if above should have caught it already */
1037
        PROTECT(ret = NEW_NUMERIC(2));
1067
        PROTECT(ret = NEW_NUMERIC(2));
1038
        REAL(ret)[0] = _ret;
1068
        REAL(ret)[0] = _ret;
1039
        REAL(ret)[1] = g_accumulator;
1069
        REAL(ret)[1] = g_accumulator;
1040
    } else {
1070
    } else {
1041
        _init_sqlite_function_accumulator();
1071
        _init_sqlite_function_accumulator();
1042
        sprintf(g_sql_buf[0], "select %s_df([%s]) from [%s].sdf_data", funcname, varname_src, iname_src);
1072
        sprintf(g_sql_buf[0], "select %s_df([%s]) from [%s].sdf_data", funcname, varname_src, iname_src);
1043
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
1073
        res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, NULL);
1044
        if (_sqlite_error(res)) return R_NilValue;
1074
        if (_sqlite_error(res)) return R_NilValue;
1045
        res = sqlite3_step(stmt); sqlite3_finalize(stmt);
1075
        res = sqlite3_step(stmt); sqlite3_finalize(stmt);
1046
 
1076
 
1047
        if (strcmp(funcname, "all") == 0 || strcmp(funcname, "any") == 0) {
1077
        if (strcmp(funcname, "all") == 0 || strcmp(funcname, "any") == 0) {
1048
            PROTECT(ret = NEW_LOGICAL(1));
1078
            PROTECT(ret = NEW_LOGICAL(1));
1049
            if (R_IsNA(g_accumulator)) LOGICAL(ret)[0] = NA_INTEGER;
1079
            if (R_IsNA(g_accumulator)) LOGICAL(ret)[0] = NA_INTEGER;
1050
            else LOGICAL(ret)[0] = !(g_accumulator == 0);
1080
            else LOGICAL(ret)[0] = !(g_accumulator == 0);
1051
        } else {
1081
        } else {
1052
            PROTECT(ret = NEW_NUMERIC(1));
1082
            PROTECT(ret = NEW_NUMERIC(1));
1053
            REAL(ret)[0] = g_accumulator;
1083
            REAL(ret)[0] = g_accumulator;
1054
        }
1084
        }
1055
    }
1085
    }
1056
 
1086
 
1057
__sdf_do_variable_summary_out:
1087
__sdf_do_variable_summary_out:
1058
    UNPROTECT(1);
1088
    UNPROTECT(1);
1059
    return ret;
1089
    return ret;
1060
}
1090
}
1061
 
1091
 
1062
 
1092
 
1063
SEXP sdf_sort_variable(SEXP svec, SEXP decreasing) {
1093
SEXP sdf_sort_variable(SEXP svec, SEXP decreasing) {
1064
    char *iname, *iname_src, *varname_src, *type;
1094
    char *iname, *iname_src, *varname_src, *type;
1065
    sqlite3_stmt *stmt;
1095
    sqlite3_stmt *stmt;
1066
    int res;
1096
    int res;
1067
 
1097
 
1068
    iname_src = SDF_INAME(svec);
1098
    iname_src = SDF_INAME(svec);
1069
    varname_src = SVEC_VARNAME(svec);
1099
    varname_src = SVEC_VARNAME(svec);
1070
 
1100
 
1071
    if (!USE_SDF1(iname_src, TRUE, TRUE)) return R_NilValue;
1101
    if (!USE_SDF1(iname_src, TRUE, TRUE)) return R_NilValue;
1072
 
1102
 
1073
    /* determine type of svec */
1103
    /* determine type of svec */
1074
    sprintf(g_sql_buf[0], "select [%s] from [%s].sdf_data limit 1", varname_src, iname_src);
1104
    sprintf(g_sql_buf[0], "select [%s] from [%s].sdf_data limit 1", varname_src, iname_src);
1075
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
1105
    res = sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt, 0);
1076
    _sqlite_error(res);
1106
    _sqlite_error(res);
1077
    sqlite3_step(stmt);
1107
    sqlite3_step(stmt);
1078
    strcpy(g_sql_buf[0], sqlite3_column_decltype(stmt, 0));
1108
    strcpy(g_sql_buf[0], sqlite3_column_decltype(stmt, 0));
1079
    sqlite3_finalize(stmt);
1109
    sqlite3_finalize(stmt);
1080
 
1110
 
1081
    /* create a new vector of that type */
1111
    /* create a new vector of that type */
1082
    iname = _create_svector1(mkString("tmp_sort"), g_sql_buf[0], NULL, TRUE);
1112
    iname = _create_svector1(mkString("tmp_sort"), g_sql_buf[0], NULL, TRUE);
1083
 
1113
 
1084
    /* insert to new sdf ordered */
1114
    /* insert to new sdf ordered */
1085
    sprintf(g_sql_buf[0], "insert into [%s].sdf_data "
1115
    sprintf(g_sql_buf[0], "insert into [%s].sdf_data "
1086
            "select [row name], [%s] from [%s].sdf_data "
1116
            "select [row name], [%s] from [%s].sdf_data "
1087
            "order by [%s] %s", iname, varname_src, iname_src, varname_src,
1117
            "order by [%s] %s", iname, varname_src, iname_src, varname_src,
1088
            (LOGICAL(decreasing)[0]) ? "desc" : "asc");
1118
            (LOGICAL(decreasing)[0]) ? "desc" : "asc");
1089
    res = _sqlite_exec(g_sql_buf[0]);
1119
    res = _sqlite_exec(g_sql_buf[0]);
1090
    _sqlite_error(res);
1120
    _sqlite_error(res);
1091
 
1121
 
1092
    if (TEST_SDFVECTORTYPE(svec, "factor")) { /* copy factor table to iname */
1122
    if (TEST_SDFVECTORTYPE(svec, "factor")) { /* copy factor table to iname */
1093
        if (TEST_SDFVECTORTYPE(svec, "ordered")) type = "ordered";
1123
        if (TEST_SDFVECTORTYPE(svec, "ordered")) type = "ordered";
1094
        else type = "factor";
1124
        else type = "factor";
1095
        _copy_factor_levels2(type, iname_src, varname_src, iname, "V1");
1125
        _copy_factor_levels2(type, iname_src, varname_src, iname, "V1");
1096
    } else type = CHAR_ELT(GET_SDFVECTORTYPE(svec), 0);
1126
    } else type = CHAR_ELT(GET_SDFVECTORTYPE(svec), 0);
1097
 
1127
 
1098
    UNUSE_SDF2(iname_src);
1128
    UNUSE_SDF2(iname_src);
1099
    UNUSE_SDF2(iname);
1129
    UNUSE_SDF2(iname);
1100
 
1130
 
1101
    return _create_svector_sexp(iname, "V1", type);
1131
    return _create_svector_sexp(iname, "sdf_data", "V1", type);
1102
}
1132
}
1103
 
1133
 
1104
/****************************************************************************
1134
/****************************************************************************
1105
 * VECTOR MATH/OPS/GROUP OPERATIONS
1135
 * VECTOR MATH/OPS/GROUP OPERATIONS
1106
 ****************************************************************************/
1136
 ****************************************************************************/
1107
 
1137
 
1108
R_INLINE int __vecmath_checkarg(sqlite3_context *ctx, sqlite3_value *arg, double *value) {
1138
R_INLINE int __vecmath_checkarg(sqlite3_context *ctx, sqlite3_value *arg, double *value) {
1109
    int ret = 1;
1139
    int ret = 1;
1110
    if (sqlite3_value_type(arg) == SQLITE_NULL) { 
1140
    if (sqlite3_value_type(arg) == SQLITE_NULL) { 
1111
        sqlite3_result_null(ctx); 
1141
        sqlite3_result_null(ctx); 
1112
        ret = 0;
1142
        ret = 0;
1113
    } else {
1143
    } else {
1114
        if (sqlite3_value_type(arg) == SQLITE_INTEGER) 
1144
        if (sqlite3_value_type(arg) == SQLITE_INTEGER) 
1115
            *value = sqlite3_value_int(arg); 
1145
            *value = sqlite3_value_int(arg); 
1116
        else *value = sqlite3_value_double(arg); 
1146
        else *value = sqlite3_value_double(arg); 
1117
    }
1147
    }
1118
    return ret;
1148
    return ret;
1119
}
1149
}
1120
 
1150
 
1121
#define SQLITE_MATH_FUNC1(name, func) static void __vecmath_ ## name(\
1151
#define SQLITE_MATH_FUNC1(name, func) static void __vecmath_ ## name(\
1122
        sqlite3_context *ctx, int argc, sqlite3_value **argv) { \
1152
        sqlite3_context *ctx, int argc, sqlite3_value **argv) { \
1123
    double value; \
1153
    double value; \
1124
    if (__vecmath_checkarg(ctx, argv[0], &value)) { \
1154
    if (__vecmath_checkarg(ctx, argv[0], &value)) { \
1125
        sqlite3_result_double(ctx, func(value)); \
1155
        sqlite3_result_double(ctx, func(value)); \
1126
    }  \
1156
    }  \
1127
}
1157
}
1128
 
1158
 
1129
#define SQLITE_MATH_FUNC2(name, func) static void __vecmath_ ## name(\
1159
#define SQLITE_MATH_FUNC2(name, func) static void __vecmath_ ## name(\
1130
        sqlite3_context *ctx, int argc, sqlite3_value **argv) { \
1160
        sqlite3_context *ctx, int argc, sqlite3_value **argv) { \
1131
    double value1, value2; \
1161
    double value1, value2; \
1132
    if (__vecmath_checkarg(ctx, argv[0], &value1) && \
1162
    if (__vecmath_checkarg(ctx, argv[0], &value1) && \
1133
        __vecmath_checkarg(ctx, argv[1], &value2)) { \
1163
        __vecmath_checkarg(ctx, argv[1], &value2)) { \
1134
        sqlite3_result_double(ctx, func((long double)value1, (long double)value2)); \
1164
        sqlite3_result_double(ctx, func((long double)value1, (long double)value2)); \
1135
    }  \
1165
    }  \
1136
}
1166
}
1137
 
1167
 
1138
#define SQLITE_MATH_FUNC_CUM(name, func) static void __vecmath_ ## name(\
1168
#define SQLITE_MATH_FUNC_CUM(name, func) static void __vecmath_ ## name(\
1139
        sqlite3_context *ctx, int argc, sqlite3_value **argv) { \
1169
        sqlite3_context *ctx, int argc, sqlite3_value **argv) { \
1140
    double value; \
1170
    double value; \
1141
    if (__vecmath_checkarg(ctx, argv[0], &value)) { \
1171
    if (__vecmath_checkarg(ctx, argv[0], &value)) { \
1142
        if (g_start) { g_start = 0; g_accumulator = value; } \
1172
        if (g_start) { g_start = 0; g_accumulator = value; } \
1143
        else g_accumulator = func(g_accumulator, value); \
1173
        else g_accumulator = func(g_accumulator, value); \
1144
        sqlite3_result_double(ctx, g_accumulator); \
1174
        sqlite3_result_double(ctx, g_accumulator); \
1145
    }  \
1175
    }  \
1146
}
1176
}
1147
 
1177
 
1148
#define LOGBASE(a, b) log(a)/log(b)
1178
#define LOGBASE(a, b) log(a)/log(b)
1149
 
1179
 
1150
/* SQLITE_MATH_FUNC1(abs, abs)   in SQLite */
1180
/* SQLITE_MATH_FUNC1(abs, abs)   in SQLite */
1151
SQLITE_MATH_FUNC1(sign, sign)   /* in R */
1181
SQLITE_MATH_FUNC1(sign, sign)   /* in R */
1152
SQLITE_MATH_FUNC1(sqrt, sqrt)
1182
SQLITE_MATH_FUNC1(sqrt, sqrt)
1153
SQLITE_MATH_FUNC1(floor, floor)
1183
SQLITE_MATH_FUNC1(floor, floor)
1154
SQLITE_MATH_FUNC1(ceiling, ceil)
1184
SQLITE_MATH_FUNC1(ceiling, ceil)
1155
SQLITE_MATH_FUNC1(trunc, ftrunc) /* in R */
1185
SQLITE_MATH_FUNC1(trunc, ftrunc) /* in R */
1156
/*SQLITE_MATH_FUNC2(round, fprec)  2 arg, in SQLite, but override with R's version */
1186
/*SQLITE_MATH_FUNC2(round, fprec)  2 arg, in SQLite, but override with R's version */
1157
SQLITE_MATH_FUNC2(signif, fround) /* 2 arg, in R */
1187
SQLITE_MATH_FUNC2(signif, fround) /* 2 arg, in R */
1158
SQLITE_MATH_FUNC1(exp, exp)
1188
SQLITE_MATH_FUNC1(exp, exp)
1159
SQLITE_MATH_FUNC2(log, LOGBASE) /* 2 arg */
1189
SQLITE_MATH_FUNC2(log, LOGBASE) /* 2 arg */
1160
SQLITE_MATH_FUNC1(cos, cos)
1190
SQLITE_MATH_FUNC1(cos, cos)
1161
SQLITE_MATH_FUNC1(sin, sin)
1191
SQLITE_MATH_FUNC1(sin, sin)
1162
SQLITE_MATH_FUNC1(tan, tan)
1192
SQLITE_MATH_FUNC1(tan, tan)
1163
SQLITE_MATH_FUNC1(acos, acos)
1193
SQLITE_MATH_FUNC1(acos, acos)
1164
SQLITE_MATH_FUNC1(asin, asin)
1194
SQLITE_MATH_FUNC1(asin, asin)
1165
SQLITE_MATH_FUNC1(atan, atan)
1195
SQLITE_MATH_FUNC1(atan, atan)
1166
SQLITE_MATH_FUNC1(cosh, cosh)
1196
SQLITE_MATH_FUNC1(cosh, cosh)
1167
SQLITE_MATH_FUNC1(sinh, sinh)
1197
SQLITE_MATH_FUNC1(sinh, sinh)
1168
SQLITE_MATH_FUNC1(tanh, tanh)
1198
SQLITE_MATH_FUNC1(tanh, tanh)
1169
SQLITE_MATH_FUNC1(acosh, acosh)  /* nowhere in include?? */
1199
SQLITE_MATH_FUNC1(acosh, acosh)  /* nowhere in include?? */
1170
SQLITE_MATH_FUNC1(asinh, asinh)  /* nowhere in include?? */
1200
SQLITE_MATH_FUNC1(asinh, asinh)  /* nowhere in include?? */
1171
SQLITE_MATH_FUNC1(atanh, atanh)  /* nowhere in include?? */
1201
SQLITE_MATH_FUNC1(atanh, atanh)  /* nowhere in include?? */
1172
SQLITE_MATH_FUNC1(lgamma, lgammafn) /* in R */
1202
SQLITE_MATH_FUNC1(lgamma, lgammafn) /* in R */
1173
SQLITE_MATH_FUNC1(gamma, gammafn) /* in R */
1203
SQLITE_MATH_FUNC1(gamma, gammafn) /* in R */
1174
/* SQLITE_MATH_FUNC1(gammaCody, gammaCody)   * in R ?? */
1204
/* SQLITE_MATH_FUNC1(gammaCody, gammaCody)   * in R ?? */
1175
SQLITE_MATH_FUNC1(digamma, digamma) /* in R */    
1205
SQLITE_MATH_FUNC1(digamma, digamma) /* in R */    
1176
SQLITE_MATH_FUNC1(trigamma, trigamma) /* in R */
1206
SQLITE_MATH_FUNC1(trigamma, trigamma) /* in R */
1177
 
1207
 
1178
#define SUM(a, b)  (a) + (b)
1208
#define SUM(a, b)  (a) + (b)
1179
#define PROD(a, b) (a) * (b)
1209
#define PROD(a, b) (a) * (b)
1180
#define MIN(a, b) ((a) <= (b)) ? (a) : (b)
1210
#define MIN(a, b) ((a) <= (b)) ? (a) : (b)
1181
#define MAX(a, b) ((a) >= (b)) ? (a) : (b)
1211
#define MAX(a, b) ((a) >= (b)) ? (a) : (b)
1182
#define ALL(a, b) (((a) == 0) || ((b) == 0)) ? 0 : 1
1212
#define ALL(a, b) (((a) == 0) || ((b) == 0)) ? 0 : 1
1183
#define ANY(a, b) (((a) == 0) && ((b) == 0)) ? 0 : 1
1213
#define ANY(a, b) (((a) == 0) && ((b) == 0)) ? 0 : 1
1184
 
1214
 
1185
SQLITE_MATH_FUNC_CUM(cumsum, SUM)
1215
SQLITE_MATH_FUNC_CUM(cumsum, SUM)
1186
SQLITE_MATH_FUNC_CUM(cumprod, PROD)
1216
SQLITE_MATH_FUNC_CUM(cumprod, PROD)
1187
SQLITE_MATH_FUNC_CUM(cummin, MIN)
1217
SQLITE_MATH_FUNC_CUM(cummin, MIN)
1188
SQLITE_MATH_FUNC_CUM(cummax, MAX)
1218
SQLITE_MATH_FUNC_CUM(cummax, MAX)
1189
 
1219
 
1190
#define SQLITE_SUMMARY_FUNC(name, func) static void __vecsummary_ ## name(\
1220
#define SQLITE_SUMMARY_FUNC(name, func) static void __vecsummary_ ## name(\
1191
        sqlite3_context *ctx, int argc, sqlite3_value **argv) { \
1221
        sqlite3_context *ctx, int argc, sqlite3_value **argv) { \
1192
    double value; \
1222
    double value; \
1193
    if (!g_narm && R_IsNA(g_accumulator)) return; /* NA if na.rm=F & NA found */ \
1223
    if (!g_narm && R_IsNA(g_accumulator)) return; /* NA if na.rm=F & NA found */ \
1194
    if (sqlite3_value_type(argv[0]) != SQLITE_NULL) {  \
1224
    if (sqlite3_value_type(argv[0]) != SQLITE_NULL) {  \
1195
        if (sqlite3_value_type(argv[0]) == SQLITE_INTEGER) {  \
1225
        if (sqlite3_value_type(argv[0]) == SQLITE_INTEGER) {  \
1196
            int tmp = sqlite3_value_int(argv[0]); \
1226
            int tmp = sqlite3_value_int(argv[0]); \
1197
            value = (tmp == NA_INTEGER) ? R_NaReal : tmp; \
1227
            value = (tmp == NA_INTEGER) ? R_NaReal : tmp; \
1198
        } else value = sqlite3_value_double(argv[0]);  \
1228
        } else value = sqlite3_value_double(argv[0]);  \
1199
        if (R_IsNA(value)) { \
1229
        if (R_IsNA(value)) { \
1200
            if (!g_narm) g_accumulator = value; return; \
1230
            if (!g_narm) g_accumulator = value; return; \
1201
        } else if (g_start) { g_start = 0; g_accumulator = value; } \
1231
        } else if (g_start) { g_start = 0; g_accumulator = value; } \
1202
        else g_accumulator = func(g_accumulator, value); \
1232
        else g_accumulator = func(g_accumulator, value); \
1203
    } \
1233
    } \
1204
}
1234
}
1205
 
1235
 
1206
SQLITE_SUMMARY_FUNC(all_df, ALL)
1236
SQLITE_SUMMARY_FUNC(all_df, ALL)
1207
SQLITE_SUMMARY_FUNC(any_df, ANY)
1237
SQLITE_SUMMARY_FUNC(any_df, ANY)
1208
SQLITE_SUMMARY_FUNC(sum_df, SUM)
1238
SQLITE_SUMMARY_FUNC(sum_df, SUM)
1209
SQLITE_SUMMARY_FUNC(prod_df, PROD)
1239
SQLITE_SUMMARY_FUNC(prod_df, PROD)
1210
SQLITE_SUMMARY_FUNC(min_df, MIN)
1240
SQLITE_SUMMARY_FUNC(min_df, MIN)
1211
SQLITE_SUMMARY_FUNC(max_df, MAX)
1241
SQLITE_SUMMARY_FUNC(max_df, MAX)
1212
 
1242
 
1213
static void __vecsummary_finalize(sqlite3_context *ctx) {
1243
static void __vecsummary_finalize(sqlite3_context *ctx) {
1214
    /* g_accumulator already summarizes it. just return that */
1244
    /* g_accumulator already summarizes it. just return that */
1215
    sqlite3_result_double(ctx, g_accumulator);
1245
    sqlite3_result_double(ctx, g_accumulator);
1216
}
1246
}
1217
 
1247
 
1218
static void __r_modulo(sqlite3_context *ctx, int argc, sqlite3_value **argv) {
1248
static void __r_modulo(sqlite3_context *ctx, int argc, sqlite3_value **argv) {
1219
    if (sqlite3_value_type(argv[0]) == SQLITE_NULL ||
1249
    if (sqlite3_value_type(argv[0]) == SQLITE_NULL ||
1220
        sqlite3_value_type(argv[1]) == SQLITE_NULL) { 
1250
        sqlite3_value_type(argv[1]) == SQLITE_NULL) { 
1221
        sqlite3_result_null(ctx); 
1251
        sqlite3_result_null(ctx); 
1222
    } else {
1252
    } else {
1223
        double v1, v2, q, tmp;
1253
        double v1, v2, q, tmp;
1224
        if (sqlite3_value_type(argv[0]) == SQLITE_INTEGER && 
1254
        if (sqlite3_value_type(argv[0]) == SQLITE_INTEGER && 
1225
            sqlite3_value_type(argv[1]) == SQLITE_INTEGER) {
1255
            sqlite3_value_type(argv[1]) == SQLITE_INTEGER) {
1226
            int i1, i2;
1256
            int i1, i2;
1227
            i1 = sqlite3_value_int(argv[0]);
1257
            i1 = sqlite3_value_int(argv[0]);
1228
            i2 = sqlite3_value_int(argv[1]);
1258
            i2 = sqlite3_value_int(argv[1]);
1229
            if (i1 > 0 && i2 > 0) { 
1259
            if (i1 > 0 && i2 > 0) { 
1230
                sqlite3_result_int(ctx, i1 % i2); return;
1260
                sqlite3_result_int(ctx, i1 % i2); return;
1231
            }
1261
            }
1232
            v1 = i1; v2 = i2;
1262
            v1 = i1; v2 = i2;
1233
        } else {
1263
        } else {
1234
            v1 = sqlite3_value_double(argv[0]);
1264
            v1 = sqlite3_value_double(argv[0]);
1235
            v2 = sqlite3_value_double(argv[1]);
1265
            v2 = sqlite3_value_double(argv[1]);
1236
        }
1266
        }
1237
        /* copied from myfmod() in src/main/arithmetic.c */
1267
        /* copied from myfmod() in src/main/arithmetic.c */
1238
        q = v1 / v2; 
1268
        q = v1 / v2; 
1239
        if (v2 == 0) sqlite3_result_double(ctx, R_NaN);
1269
        if (v2 == 0) sqlite3_result_double(ctx, R_NaN);
1240
        tmp = v1 - floor(q) * v2;
1270
        tmp = v1 - floor(q) * v2;
1241
        /* checking omitted */
1271
        /* checking omitted */
1242
        q = floor(tmp/v2);
1272
        q = floor(tmp/v2);
1243
        sqlite3_result_double(ctx, tmp - q*v2);
1273
        sqlite3_result_double(ctx, tmp - q*v2);
1244
 
1274
 
1245
    }
1275
    }
1246
}
1276
}
1247
 
1277
 
1248
static void __r_intdiv(sqlite3_context *ctx, int argc, sqlite3_value **argv) {
1278
static void __r_intdiv(sqlite3_context *ctx, int argc, sqlite3_value **argv) {
1249
    if (sqlite3_value_type(argv[0]) == SQLITE_NULL ||
1279
    if (sqlite3_value_type(argv[0]) == SQLITE_NULL ||
1250
        sqlite3_value_type(argv[1]) == SQLITE_NULL) { 
1280
        sqlite3_value_type(argv[1]) == SQLITE_NULL) { 
1251
        sqlite3_result_null(ctx); 
1281
        sqlite3_result_null(ctx); 
1252
    } else {
1282
    } else {
1253
        double v1, v2;
1283
        double v1, v2;
1254
        if (sqlite3_value_type(argv[0]) == SQLITE_INTEGER && 
1284
        if (sqlite3_value_type(argv[0]) == SQLITE_INTEGER && 
1255
            sqlite3_value_type(argv[1]) == SQLITE_INTEGER) {
1285
            sqlite3_value_type(argv[1]) == SQLITE_INTEGER) {
1256
            int i1, i2;
1286
            int i1, i2;
1257
            i1 = sqlite3_value_int(argv[0]);
1287
            i1 = sqlite3_value_int(argv[0]);
1258
            i2 = sqlite3_value_int(argv[1]);
1288
            i2 = sqlite3_value_int(argv[1]);
1259
            if (i1 == NA_INTEGER || i2 == NA_INTEGER) {
1289
            if (i1 == NA_INTEGER || i2 == NA_INTEGER) {
1260
                sqlite3_result_int(ctx, NA_INTEGER); return;
1290
                sqlite3_result_int(ctx, NA_INTEGER); return;
1261
            } else if (i2 == 0) {
1291
            } else if (i2 == 0) {
1262
                sqlite3_result_int(ctx, 0); return;
1292
                sqlite3_result_int(ctx, 0); return;
1263
            }
1293
            }
1264
            v1 = i1; v2 = i2;
1294
            v1 = i1; v2 = i2;
1265
        } else {
1295
        } else {
1266
            v1 = sqlite3_value_double(argv[0]);
1296
            v1 = sqlite3_value_double(argv[0]);
1267
            v2 = sqlite3_value_double(argv[1]);
1297
            v2 = sqlite3_value_double(argv[1]);
1268
        }
1298
        }
1269
        /* copied from IDIVOP cases in src/main/arithmetic.c */
1299
        /* copied from IDIVOP cases in src/main/arithmetic.c */
1270
        sqlite3_result_double(ctx, floor(v1/v2));
1300
        sqlite3_result_double(ctx, floor(v1/v2));
1271
    }
1301
    }
1272
}
1302
}
1273
#define VMENTRY1(func)  {#func, __vecmath_ ## func}
1303
#define VMENTRY1(func)  {#func, __vecmath_ ## func}
1274
#define VSENTRY1(func)  {#func, __vecsummary_ ## func}
1304
#define VSENTRY1(func)  {#func, __vecsummary_ ## func}
1275
void __register_vector_math() {
1305
void __register_vector_math() {
1276
    int i, res;
1306
    int i, res;
1277
    static const struct {
1307
    static const struct {
1278
        char *name;
1308
        char *name;
1279
        void (*func)(sqlite3_context*, int, sqlite3_value**);
1309
        void (*func)(sqlite3_context*, int, sqlite3_value**);
1280
    } arr_func1[] = {
1310
    } arr_func1[] = {
1281
        VMENTRY1(sign),
1311
        VMENTRY1(sign),
1282
        VMENTRY1(sqrt),
1312
        VMENTRY1(sqrt),
1283
        VMENTRY1(floor),
1313
        VMENTRY1(floor),
1284
        VMENTRY1(ceiling),
1314
        VMENTRY1(ceiling),
1285
        VMENTRY1(trunc),
1315
        VMENTRY1(trunc),
1286
        VMENTRY1(exp),
1316
        VMENTRY1(exp),
1287
        VMENTRY1(cos),
1317
        VMENTRY1(cos),
1288
        VMENTRY1(sin),
1318
        VMENTRY1(sin),
1289
        VMENTRY1(tan),
1319
        VMENTRY1(tan),
1290
        VMENTRY1(acos),
1320
        VMENTRY1(acos),
1291
        VMENTRY1(asin),
1321
        VMENTRY1(asin),
1292
        VMENTRY1(atan),
1322
        VMENTRY1(atan),
1293
        VMENTRY1(cosh),
1323
        VMENTRY1(cosh),
1294
        VMENTRY1(sinh),
1324
        VMENTRY1(sinh),
1295
        VMENTRY1(tanh),
1325
        VMENTRY1(tanh),
1296
        VMENTRY1(acosh),
1326
        VMENTRY1(acosh),
1297
        VMENTRY1(asinh),
1327
        VMENTRY1(asinh),
1298
        VMENTRY1(atanh),
1328
        VMENTRY1(atanh),
1299
        VMENTRY1(lgamma),
1329
        VMENTRY1(lgamma),
1300
        VMENTRY1(gamma),
1330
        VMENTRY1(gamma),
1301
        VMENTRY1(digamma),
1331
        VMENTRY1(digamma),
1302
        VMENTRY1(trigamma),
1332
        VMENTRY1(trigamma),
1303
        VMENTRY1(cumsum),
1333
        VMENTRY1(cumsum),
1304
        VMENTRY1(cumprod),
1334
        VMENTRY1(cumprod),
1305
        VMENTRY1(cummin),
1335
        VMENTRY1(cummin),
1306
        VMENTRY1(cummax)
1336
        VMENTRY1(cummax)
1307
    }, arr_func2[] =  {
1337
    }, arr_func2[] =  {
1308
        {"r_mod", __r_modulo},
1338
        {"r_mod", __r_modulo},
1309
        {"r_intdiv", __r_intdiv},
1339
        {"r_intdiv", __r_intdiv},
1310
        /*VMENTRY1(round),*/
1340
        /*VMENTRY1(round),*/
1311
        VMENTRY1(signif),
1341
        VMENTRY1(signif),
1312
        VMENTRY1(log)
1342
        VMENTRY1(log)
1313
    }, arr_sum1[] = {
1343
    }, arr_sum1[] = {
1314
        VSENTRY1(all_df),  /* can't override sum, min, max */
1344
        VSENTRY1(all_df),  /* can't override sum, min, max */
1315
        VSENTRY1(any_df),
1345
        VSENTRY1(any_df),
1316
        VSENTRY1(sum_df),
1346
        VSENTRY1(sum_df),
1317
        VSENTRY1(prod_df),
1347
        VSENTRY1(prod_df),
1318
        VSENTRY1(min_df),
1348
        VSENTRY1(min_df),
1319
        VSENTRY1(max_df)
1349
        VSENTRY1(max_df)
1320
    };
1350
    };
1321
 
1351
 
1322
    int len = sizeof(arr_func1) / sizeof(arr_func1[0]);
1352
    int len = sizeof(arr_func1) / sizeof(arr_func1[0]);
1323
 
1353
 
1324
    for (i = 0; i < len; i++) {
1354
    for (i = 0; i < len; i++) {
1325
        res = sqlite3_create_function(g_workspace, arr_func1[i].name, 1, 
1355
        res = sqlite3_create_function(g_workspace, arr_func1[i].name, 1, 
1326
                SQLITE_ANY, NULL, arr_func1[i].func, NULL, NULL);
1356
                SQLITE_ANY, NULL, arr_func1[i].func, NULL, NULL);
1327
        _sqlite_error(res);
1357
        _sqlite_error(res);
1328
    }
1358
    }
1329
 
1359
 
1330
    len = sizeof(arr_func2) / sizeof(arr_func2[0]);
1360
    len = sizeof(arr_func2) / sizeof(arr_func2[0]);
1331
    for (i = 0; i < len; i++) {
1361
    for (i = 0; i < len; i++) {
1332
        res = sqlite3_create_function(g_workspace, arr_func2[i].name, 2, 
1362
        res = sqlite3_create_function(g_workspace, arr_func2[i].name, 2, 
1333
                SQLITE_ANY, NULL, arr_func2[i].func, NULL, NULL);
1363
                SQLITE_ANY, NULL, arr_func2[i].func, NULL, NULL);
1334
        _sqlite_error(res);
1364
        _sqlite_error(res);
1335
    }
1365
    }
1336
 
1366
 
1337
    len = sizeof(arr_sum1) / sizeof(arr_sum1[0]);
1367
    len = sizeof(arr_sum1) / sizeof(arr_sum1[0]);
1338
    for (i = 0; i < len; i++) {
1368
    for (i = 0; i < len; i++) {
1339
        res = sqlite3_create_function(g_workspace, arr_sum1[i].name, 1, 
1369
        res = sqlite3_create_function(g_workspace, arr_sum1[i].name, 1, 
1340
                SQLITE_ANY, NULL, NULL, arr_sum1[i].func, __vecsummary_finalize);
1370
                SQLITE_ANY, NULL, NULL, arr_sum1[i].func, __vecsummary_finalize);
1341
        _sqlite_error(res);
1371
        _sqlite_error(res);
1342
    }
1372
    }
1343
}
1373
}