The R Project SVN R-packages

Rev

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

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