The R Project SVN R-packages

Rev

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

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