The R Project SVN R-packages

Rev

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

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