-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhistogram.cpp
417 lines (367 loc) · 12.4 KB
/
histogram.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
histogram.cpp, Robert Oeffner 2018
The MIT License (MIT)
Copyright (c) 2017 Robert Oeffner
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <iostream>
#include <vector>
#include <cstdlib>
#include "RegistExt.h"
#include "helpers.h"
#include <assert.h>
#include <memory.h>
#ifndef SQLITE_OMIT_VIRTUALTABLE
#ifdef __cplusplus
extern "C" {
#endif
/* histo_cursor is a subclass of sqlite3_vtab_cursor which will
** serve as the underlying representation of a cursor that scans
** over rows of the result
*/
typedef struct histo_cursor histo_cursor;
struct histo_cursor {
sqlite3_vtab_cursor base; /* Base class - must be first */
int isDesc; /* True to count down rather than up */
sqlite3_int64 iRowid; /* The rowid */
double bin;
sqlite3_int64 count1;
sqlite3_int64 count2;
double ratio;
sqlite3_int64 totalcount;
std::string tblname;
std::string colid;
int nbins;
double minbin;
double maxbin;
std::vector<histobin> histogram;
};
enum ColNum
{ /* Column numbers. The order determines the order of columns in the table output
and must match the order of columns in the CREATE TABLE statement below
*/
HISTO_BIN = 0,
HISTO_COUNT1,
HISTO_COUNT2,
HISTO_TBLNAME,
HISTO_COLID,
HISTO_NBINS,
HISTO_MINBIN,
HISTO_MAXBIN
};
/*
** The histoConnect() method is invoked to create a new
** histo_vtab that describes the generate_histo virtual table.
** As the histoCreate method is set to NULL this virtual table is
** an Eponymous-only virtual table, i.e. useful as a table-valued function.
** The hidden columns are the arguments to the function and won't show up
** in the SQL tables.
** Think of this routine as the constructor for histo_vtab objects.
**
** All this routine needs to do is:
**
** (1) Allocate the histo_vtab object and initialize all fields.
**
** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
** result set of queries against generate_histo will look like.
*/
int histoConnect(
sqlite3 *db,
void *pAux,
int argc, const char *const*argv,
sqlite3_vtab **ppVtab,
char **pzErr
)
{
sqlite3_vtab *pNew;
int rc;
// The hidden columns serves as arguments to the HISTO function as in:
// SELECT * FROM HISTO('tblname', 'colid', nbins, minbin, maxbin, 'discrcolid', discrval);
// They won't show up in the SQL tables.
rc = sqlite3_declare_vtab(db,
// Order of columns MUST match the order of the above enum ColNum
"CREATE TABLE x(bin REAL, bincount INTEGER, accumcount INTEGER, " \
"tblname hidden, colid hidden, nbins hidden, minbin hidden, maxbin hidden)");
if( rc==SQLITE_OK )
{
pNew = *ppVtab = (sqlite3_vtab *)sqlite3_malloc( sizeof(*pNew) );
if( pNew==0 ) return SQLITE_NOMEM;
memset(pNew, 0, sizeof(*pNew));
}
thisdb = db;
return rc;
}
/*
** This method is the destructor for histo_cursor objects.
*/
int histoDisconnect(sqlite3_vtab *pVtab){
sqlite3_free(pVtab);
return SQLITE_OK;
}
/*
** Constructor for a new histo_cursor object.
*/
int histoOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
histo_cursor *pCur;
// allocate c++ object with new rather than sqlite3_malloc which doesn't call constructors
pCur = new histo_cursor;
if( pCur==NULL ) return SQLITE_NOMEM;
*ppCursor = &pCur->base;
return SQLITE_OK;
}
/*
** Destructor for a histo_cursor.
*/
int histoClose(sqlite3_vtab_cursor *cur){
delete cur;
return SQLITE_OK;
}
/*
** Advance a histo_cursor to its next row of output.
*/
int histoNext(sqlite3_vtab_cursor *cur){
histo_cursor *pCur = (histo_cursor*)cur;
pCur->iRowid++;
int i = pCur->iRowid - 1;
pCur->bin = pCur->histogram[i].binval;
pCur->count1 = pCur->histogram[i].count;
pCur->count2 = pCur->histogram[i].accumcount;
return SQLITE_OK;
}
/*
** Return values of columns for the row at which the histo_cursor
** is currently pointing.
*/
int histoColumn(
sqlite3_vtab_cursor *cur, /* The cursor */
sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
int i /* Which column to return */
){
histo_cursor *pCur = (histo_cursor*)cur;
sqlite3_int64 x = 123456;
std::string c = "waffle";
double d = -42.24;
switch( i ){
case HISTO_BIN: d = pCur->bin; sqlite3_result_double(ctx, d); break;
case HISTO_COUNT1: x = pCur->count1; sqlite3_result_int64(ctx, x); break;
case HISTO_COUNT2: x = pCur->count2; sqlite3_result_int64(ctx, x); break;
case HISTO_TBLNAME: c = pCur->tblname; sqlite3_result_text(ctx, c.c_str(), -1, NULL); break;
case HISTO_COLID: c = pCur->colid; sqlite3_result_text(ctx, c.c_str(), -1, NULL); break;
case HISTO_NBINS: x = pCur->nbins; sqlite3_result_double(ctx, x); break;
case HISTO_MINBIN: d = pCur->minbin; sqlite3_result_double(ctx, d); break;
case HISTO_MAXBIN: d = pCur->maxbin; sqlite3_result_double(ctx, d); break;
default: x = pCur->count1; sqlite3_result_int64(ctx, x); break;
}
return SQLITE_OK;
}
/*
** Return the rowid for the current row. In this implementation, the
** rowid is the same as the output value.
*/
int histoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
histo_cursor *pCur = (histo_cursor*)cur;
*pRowid = pCur->iRowid;
return SQLITE_OK;
}
/*
** Return TRUE if the cursor has been moved off of the last
** row of output.
*/
int histoEof(sqlite3_vtab_cursor *cur) {
histo_cursor *pCur = (histo_cursor*)cur;
if (pCur->isDesc) {
return pCur->iRowid < 1;
}
else {
return pCur->iRowid > pCur->histogram.size();
}
}
/*
** This method is called to "rewind" the histo_cursor object back
** to the first row of output. This method is always called at least
** once prior to any call to histoColumn() or histoRowid() or
** histoEof().
**
** The query plan selected by histoBestIndex is passed in the idxNum
** parameter. (idxStr is not used in this implementation.) idxNum
** is one of the enum ColNum values above.
** This routine should initialize the cursor and position it so that it
** is pointing at the first row, or pointing off the end of the table
** (so that histoEof() will return true) if the table is empty.
*/
int histoFilter(
sqlite3_vtab_cursor *pVtabCursor,
int idxNum, const char *idxStr,
int argc, sqlite3_value **argv
){
histo_cursor *pCur = (histo_cursor *)pVtabCursor;
int i = 0, rc = SQLITE_OK;
pCur->tblname = "";
pCur->colid = "";
pCur->nbins = 1.0;
pCur->minbin = 1.0;
pCur->maxbin = 1.0;
if( idxNum >= HISTO_MAXBIN)
{
pCur->tblname = (const char*)sqlite3_value_text(argv[i++]);
pCur->colid = (const char*)sqlite3_value_text(argv[i++]);
pCur->nbins = sqlite3_value_double(argv[i++]);
pCur->minbin = sqlite3_value_double(argv[i++]);
pCur->maxbin = sqlite3_value_double(argv[i++]);
}
else
{
const char *zText = "Incorrect arguments for function HISTO which must be called as:\n" \
" HISTO('tablename', 'columnname', nbins, minbin, maxbin)\n";
pCur->base.pVtab->zErrMsg = sqlite3_mprintf( zText);
return SQLITE_ERROR;
}
std::vector< std::vector<double> > mybins;
std::string s_exe("SELECT ");
s_exe += pCur->colid + " FROM " + pCur->tblname;
mybins.clear();
mybins = GetColumns(thisdb, s_exe, &rc);
if (rc != SQLITE_OK)
{
pCur->base.pVtab->zErrMsg = sqlite3_mprintf(sqlite3_errmsg(thisdb));
return rc;
}
pCur->histogram = CalcHistogram(mybins, pCur->nbins, pCur->minbin, pCur->maxbin, &rc);
if (rc != SQLITE_OK)
return rc;
pCur->bin = pCur->histogram[0].binval;
pCur->count1 = pCur->histogram[0].count;
pCur->count2 = pCur->histogram[0].accumcount;
pCur->isDesc = 0;
pCur->iRowid = 1;
return rc;
}
/*
** SQLite will invoke this method one or more times while planning a query
** that uses the generate_histo virtual table. This routine needs to create
** a query plan for each invocation and compute an estimated cost for that
** plan.
**
** In this implementation idxNum is used to represent the
** query plan. idxStr is unused.
**
** The query plan is represented by bits in idxNum:
**
** (1) start = $value -- constraint exists
** (2) stop = $value -- constraint exists
** (4) step = $value -- constraint exists
** (8) output in descending order
*/
int histoBestIndex(
sqlite3_vtab *tab,
sqlite3_index_info *pIdxInfo
){
int i; /* Loop over constraints */
int idxNum = 0; /* The query plan bitmask */
int tblnameidx = -1; /* Index of the start= constraint, or -1 if none */
int colididx = -1; /* Index of the stop= constraint, or -1 if none */
int binsidx = -1; /* Index of the step= constraint, or -1 if none */
int minbinidx = -1;
int maxbinidx = -1;
int nArg = 0; /* Number of arguments that histoFilter() expects */
sqlite3_index_info::sqlite3_index_constraint *pConstraint;
pConstraint = pIdxInfo->aConstraint;
for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
if( pConstraint->usable==0 ) continue;
if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
switch( pConstraint->iColumn ){
case HISTO_TBLNAME:
tblnameidx = i;
idxNum = HISTO_TBLNAME;
break;
case HISTO_COLID:
colididx = i;
idxNum = HISTO_COLID;
break;
case HISTO_NBINS:
binsidx = i;
idxNum = HISTO_NBINS;
break;
case HISTO_MINBIN:
minbinidx = i;
idxNum = HISTO_MINBIN;
break;
case HISTO_MAXBIN:
maxbinidx = i;
idxNum = HISTO_MAXBIN;
break;
}
}
if(tblnameidx >=0 ){
pIdxInfo->aConstraintUsage[tblnameidx].argvIndex = ++nArg;
pIdxInfo->aConstraintUsage[tblnameidx].omit= 1;
}
if(colididx >=0 ){
pIdxInfo->aConstraintUsage[colididx].argvIndex = ++nArg;
pIdxInfo->aConstraintUsage[colididx].omit = 1;
}
if (binsidx >= 0) {
pIdxInfo->aConstraintUsage[binsidx].argvIndex = ++nArg;
pIdxInfo->aConstraintUsage[binsidx].omit = 1;
}
if (minbinidx >= 0) {
pIdxInfo->aConstraintUsage[minbinidx].argvIndex = ++nArg;
pIdxInfo->aConstraintUsage[minbinidx].omit = 1;
}
if (maxbinidx >= 0) {
pIdxInfo->aConstraintUsage[maxbinidx].argvIndex = ++nArg;
pIdxInfo->aConstraintUsage[maxbinidx].omit = 1;
}
pIdxInfo->estimatedCost = 2.0;
pIdxInfo->estimatedRows = 500;
if( pIdxInfo->nOrderBy==1 )
{
pIdxInfo->orderByConsumed = 1;
}
pIdxInfo->idxNum = idxNum;
return SQLITE_OK;
}
/*
** This following structure defines all the methods for the
** generate_histo virtual table.
*/
sqlite3_module histoModule = {
0, /* iVersion */
0, /* xCreate */
histoConnect, /* xConnect */
histoBestIndex, /* xBestIndex */
histoDisconnect, /* xDisconnect */
0, /* xDestroy */
histoOpen, /* xOpen - open a cursor */
histoClose, /* xClose - close a cursor */
histoFilter, /* xFilter - configure scan constraints */
histoNext, /* xNext - advance a cursor */
histoEof, /* xEof - check for end of scan */
histoColumn, /* xColumn - read data */
histoRowid, /* xRowid - read data */
0, /* xUpdate */
0, /* xBegin */
0, /* xSync */
0, /* xCommit */
0, /* xRollback */
0, /* xFindMethod */
0, /* xRename */
};
#endif /* SQLITE_OMIT_VIRTUALTABLE */
#ifdef __cplusplus
}
#endif