The R Project SVN R-packages

Rev

Rev 4590 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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