The R Project SVN R-packages

Rev

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

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