The R Project SVN R-packages

Rev

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

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