The R Project SVN R-packages

Rev

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

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