The R Project SVN R-packages

Rev

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

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