-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolumnar.c
More file actions
2799 lines (2597 loc) · 91.1 KB
/
columnar.c
File metadata and controls
2799 lines (2597 loc) · 91.1 KB
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
**
** Column-oriented storage for SQLite, implemented as a virtual table module.
** This is intentionally isolated from the core pager/btree code: each virtual
** table owns one shadow rowid table plus one shadow table per declared column.
**
** Usage:
**
** .load ./columnar
** CREATE VIRTUAL TABLE c USING columnar(a INTEGER, b TEXT, c REAL);
** INSERT INTO c VALUES(1,'one',1.5);
** SELECT sum(a) FROM c;
**
** The table is rowid-addressed. Full scans stream only the columns marked
** used by sqlite3_index_info.colUsed, so analytical queries over a subset of
** columns avoid reading the other column btrees.
**
** Created by Marco Bambini on 08/05/26.
**
*/
#if defined(BUILD_sqlite) && !defined(SQLITE_CORE)
# define SQLITE_CORE 1
#endif
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef SQLITE_PRIVATE
# define SQLITE_PRIVATE
#endif
#ifndef COLUMNAR_VERSION
# define COLUMNAR_VERSION "1.0.0"
#endif
#ifndef SQLITE_OMIT_VIRTUALTABLE
typedef struct ColumnarVtab ColumnarVtab;
typedef struct ColumnarCursor ColumnarCursor;
typedef struct ColumnarAnalytic ColumnarAnalytic;
typedef struct ColumnarRangeAnalytic ColumnarRangeAnalytic;
typedef struct ColumnarGroupAgg ColumnarGroupAgg;
typedef struct ColumnarGroupVtab ColumnarGroupVtab;
typedef struct ColumnarGroupCursor ColumnarGroupCursor;
typedef struct ColumnarGroupEntry ColumnarGroupEntry;
typedef struct ColumnarTableRef ColumnarTableRef;
static sqlite3_module columnarModule;
static sqlite3_module columnarGroupModule;
static sqlite3_module columnarGroupWhereModule;
// PRAGMA MARK: - Types -
struct ColumnarVtab {
sqlite3_vtab base;
sqlite3 *db;
char *zDb;
char *zName;
char *zRowTab;
char *zStatTab;
char *zChunkTab;
char *zDirtyTab;
char *zMetaTab;
char **azColTab;
int nCol;
};
struct ColumnarCursor {
sqlite3_vtab_cursor base;
sqlite3_int64 iRowid;
sqlite3_uint64 colUsed;
int bEof;
int bPoint;
int iDriver;
sqlite3_stmt *pRowStmt;
sqlite3_stmt **apStmt;
};
struct ColumnarTableRef {
char *zDb;
char *zTab;
};
struct ColumnarAnalytic {
int eOp;
};
struct ColumnarRangeAnalytic {
int eOp;
};
struct ColumnarGroupAgg {
int eOp;
int nOut;
int nValArg;
int nValHidden;
const char *zDecl;
const char *zErrName;
};
struct ColumnarGroupVtab {
sqlite3_vtab base;
sqlite3 *db;
const ColumnarGroupAgg *pAgg;
};
struct ColumnarGroupEntry {
int eKeyType;
int nKey;
unsigned int uKeyHash;
sqlite3_int64 iKey;
double rKey;
void *pKey;
sqlite3_int64 nCount;
double rSum;
double rMin;
double rMax;
unsigned char bUsed;
unsigned char bMin;
unsigned char bMax;
};
struct ColumnarGroupCursor {
sqlite3_vtab_cursor base;
ColumnarGroupEntry *aEntry;
int nAlloc;
int nEntry;
int iEntry;
sqlite3_int64 iRowid;
};
#define COLUMNAR_ANALYTIC_SUM 1
#define COLUMNAR_ANALYTIC_AVG 2
#define COLUMNAR_ANALYTIC_COUNT 3
#define COLUMNAR_CHUNK_SIZE 65536
#define COLUMNAR_GROUP_SUM 1
#define COLUMNAR_GROUP_AVG 2
#define COLUMNAR_GROUP_COUNT 3
#define COLUMNAR_GROUP_SUM_AVG_COUNT 4
#define COLUMNAR_GROUP_MIN 5
#define COLUMNAR_GROUP_MAX 6
#define COLUMNAR_GROUP_MIN_MAX_COUNT 7
#define COLUMNAR_GROUP_RANGE 8
#define COLUMNAR_GROUP_IDX_VAL1 0x01
#define COLUMNAR_GROUP_IDX_VAL2 0x02
// PRAGMA MARK: - Operation Descriptors -
static const ColumnarAnalytic columnarSumOp = { COLUMNAR_ANALYTIC_SUM };
static const ColumnarAnalytic columnarAvgOp = { COLUMNAR_ANALYTIC_AVG };
static const ColumnarAnalytic columnarCountOp = { COLUMNAR_ANALYTIC_COUNT };
static const ColumnarRangeAnalytic columnarRangeSumOp = { COLUMNAR_ANALYTIC_SUM };
static const ColumnarRangeAnalytic columnarRangeAvgOp = { COLUMNAR_ANALYTIC_AVG };
static const ColumnarRangeAnalytic columnarRangeCountOp = { COLUMNAR_ANALYTIC_COUNT };
static const ColumnarGroupAgg columnarGroupSumAgg = {
COLUMNAR_GROUP_SUM, 2, 1, 1,
"CREATE TABLE x(k,sum,tab HIDDEN,keycol HIDDEN,valcol HIDDEN)",
"columnar_group_sum"
};
static const ColumnarGroupAgg columnarGroupAvgAgg = {
COLUMNAR_GROUP_AVG, 2, 1, 1,
"CREATE TABLE x(k,avg,tab HIDDEN,keycol HIDDEN,valcol HIDDEN)",
"columnar_group_avg"
};
static const ColumnarGroupAgg columnarGroupCountAgg = {
COLUMNAR_GROUP_COUNT, 2, 0, 1,
"CREATE TABLE x(k,count,tab HIDDEN,keycol HIDDEN,valcol HIDDEN)",
"columnar_group_count"
};
static const ColumnarGroupAgg columnarGroupSumAvgCountAgg = {
COLUMNAR_GROUP_SUM_AVG_COUNT, 4, 1, 1,
"CREATE TABLE x(k,sum,avg,count,tab HIDDEN,keycol HIDDEN,valcol HIDDEN)",
"columnar_group_sum_avg_count"
};
static const ColumnarGroupAgg columnarGroupMinAgg = {
COLUMNAR_GROUP_MIN, 2, 1, 1,
"CREATE TABLE x(k,min,tab HIDDEN,keycol HIDDEN,valcol HIDDEN)",
"columnar_group_min"
};
static const ColumnarGroupAgg columnarGroupMaxAgg = {
COLUMNAR_GROUP_MAX, 2, 1, 1,
"CREATE TABLE x(k,max,tab HIDDEN,keycol HIDDEN,valcol HIDDEN)",
"columnar_group_max"
};
static const ColumnarGroupAgg columnarGroupMinMaxCountAgg = {
COLUMNAR_GROUP_MIN_MAX_COUNT, 4, 1, 1,
"CREATE TABLE x(k,min,max,count,tab HIDDEN,keycol HIDDEN,valcol HIDDEN)",
"columnar_group_min_max_count"
};
static const ColumnarGroupAgg columnarGroupRangeAgg = {
COLUMNAR_GROUP_RANGE, 5, 2, 2,
"CREATE TABLE x(k,range,max,min,count,tab HIDDEN,keycol HIDDEN,maxcol HIDDEN,mincol HIDDEN)",
"columnar_group_range"
};
static const ColumnarGroupAgg columnarGroupSumWhereAgg = {
COLUMNAR_GROUP_SUM, 2, 1, 1,
"CREATE TABLE x(k,sum,tab HIDDEN,keycol HIDDEN,valcol HIDDEN,filtercol HIDDEN,lo HIDDEN,hi HIDDEN)",
"columnar_group_sum_where_range"
};
static const ColumnarGroupAgg columnarGroupSumAvgCountWhereAgg = {
COLUMNAR_GROUP_SUM_AVG_COUNT, 4, 1, 1,
"CREATE TABLE x(k,sum,avg,count,tab HIDDEN,keycol HIDDEN,valcol HIDDEN,filtercol HIDDEN,lo HIDDEN,hi HIDDEN)",
"columnar_group_sum_avg_count_where_range"
};
// PRAGMA MARK: - Virtual Table Setup -
static void columnarFreeVtab(ColumnarVtab *p){
int i;
if( p==0 ) return;
sqlite3_free(p->zDb);
sqlite3_free(p->zName);
sqlite3_free(p->zRowTab);
sqlite3_free(p->zStatTab);
sqlite3_free(p->zChunkTab);
sqlite3_free(p->zDirtyTab);
sqlite3_free(p->zMetaTab);
if( p->azColTab ){
for(i=0; i<p->nCol; i++) sqlite3_free(p->azColTab[i]);
sqlite3_free(p->azColTab);
}
sqlite3_free(p);
}
static int columnarIsIdentChar(char c){
return ((c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9') || c=='_');
}
static char *columnarColumnName(sqlite3 *db, const char *zDef, int iCol){
const char *z = zDef;
char *zOut = 0;
if( z==0 || z[0]==0 ){
return sqlite3_mprintf("c%d", iCol);
}
while( z[0]==' ' || z[0]=='\t' || z[0]=='\n' || z[0]=='\r' ) z++;
if( z[0]=='"' || z[0]=='`' || z[0]=='[' ){
char q = z[0]=='[' ? ']' : z[0];
const char *zStart = ++z;
while( z[0] && z[0]!=q ) z++;
zOut = sqlite3_mprintf("%.*s", (int)(z-zStart), zStart);
}else{
const char *zStart = z;
while( columnarIsIdentChar(z[0]) ) z++;
if( z>zStart ) zOut = sqlite3_mprintf("%.*s", (int)(z-zStart), zStart);
}
if( zOut==0 ) zOut = sqlite3_mprintf("c%d", iCol);
(void)db;
return zOut;
}
static char *columnarDeclaration(sqlite3 *db, int argc, const char *const *argv){
sqlite3_str *pStr = sqlite3_str_new(db);
int i;
sqlite3_str_appendall(pStr, "CREATE TABLE x(");
for(i=3; i<argc; i++){
if( i>3 ) sqlite3_str_appendall(pStr, ",");
sqlite3_str_appendall(pStr, argv[i]);
}
sqlite3_str_appendall(pStr, ")");
return sqlite3_str_finish(pStr);
}
static int columnarExec(sqlite3 *db, char **pzErr, const char *zSql){
char *zErr = 0;
int rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
if( rc!=SQLITE_OK ){
if( pzErr ){
*pzErr = sqlite3_mprintf("%s", zErr ? zErr : sqlite3_errmsg(db));
}
sqlite3_free(zErr);
}
return rc;
}
static int columnarPrepare(sqlite3 *db, sqlite3_stmt **ppStmt, char **pzErr, const char *zFmt, ...){
va_list ap;
char *zSql;
int rc;
va_start(ap, zFmt);
zSql = sqlite3_vmprintf(zFmt, ap);
va_end(ap);
if( zSql==0 ) return SQLITE_NOMEM;
rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
if( rc!=SQLITE_OK && pzErr ){
*pzErr = sqlite3_mprintf("%s: %s", sqlite3_errmsg(db), zSql);
}
sqlite3_free(zSql);
return rc;
}
static int columnarInit(sqlite3 *db, int bCreate, int argc, const char *const *argv, sqlite3_vtab **ppVtab, char **pzErr){
ColumnarVtab *p = 0;
char *zDecl = 0;
int rc;
int i;
if( argc<4 ){
*pzErr = sqlite3_mprintf("columnar requires at least one column");
return SQLITE_ERROR;
}
zDecl = columnarDeclaration(db, argc, argv);
if( zDecl==0 ) return SQLITE_NOMEM;
rc = sqlite3_declare_vtab(db, zDecl);
sqlite3_free(zDecl);
if( rc!=SQLITE_OK ) return rc;
p = sqlite3_malloc64(sizeof(*p));
if( p==0 ) return SQLITE_NOMEM;
memset(p, 0, sizeof(*p));
p->db = db;
p->nCol = argc - 3;
p->zDb = sqlite3_mprintf("%s", argv[1]);
p->zName = sqlite3_mprintf("%s", argv[2]);
p->zRowTab = sqlite3_mprintf("%s__columnar_rowid", argv[2]);
p->zStatTab = sqlite3_mprintf("%s__columnar_stat", argv[2]);
p->zChunkTab = sqlite3_mprintf("%s__columnar_chunk", argv[2]);
p->zDirtyTab = sqlite3_mprintf("%s__columnar_dirty", argv[2]);
p->zMetaTab = sqlite3_mprintf("%s__columnar_meta", argv[2]);
p->azColTab = sqlite3_malloc64(sizeof(char*) * p->nCol);
if( p->zDb==0 || p->zName==0 || p->zRowTab==0
|| p->zStatTab==0 || p->zChunkTab==0 || p->zDirtyTab==0
|| p->zMetaTab==0
|| p->azColTab==0
){
columnarFreeVtab(p);
return SQLITE_NOMEM;
}
memset(p->azColTab, 0, sizeof(char*) * p->nCol);
for(i=0; i<p->nCol; i++){
p->azColTab[i] = sqlite3_mprintf("%s__columnar_c%d", argv[2], i);
if( p->azColTab[i]==0 ){
columnarFreeVtab(p);
return SQLITE_NOMEM;
}
}
if( bCreate ){
char *zSql;
zSql = sqlite3_mprintf("CREATE TABLE %Q.%Q(rid INTEGER PRIMARY KEY)", p->zDb, p->zRowTab);
if( zSql==0 ){
columnarFreeVtab(p);
return SQLITE_NOMEM;
}
rc = columnarExec(db, pzErr, zSql);
sqlite3_free(zSql);
if( rc!=SQLITE_OK ){
columnarFreeVtab(p);
return rc;
}
zSql = sqlite3_mprintf("CREATE TABLE %Q.%Q(" "col INTEGER PRIMARY KEY," "row_count INTEGER NOT NULL," "nonnull_count INTEGER NOT NULL," "sum REAL," "min," "max" ")", p->zDb, p->zStatTab);
if( zSql==0 ){
columnarFreeVtab(p);
return SQLITE_NOMEM;
}
rc = columnarExec(db, pzErr, zSql);
sqlite3_free(zSql);
if( rc!=SQLITE_OK ){
columnarFreeVtab(p);
return rc;
}
zSql = sqlite3_mprintf("CREATE TABLE %Q.%Q(" "col INTEGER NOT NULL," "chunk INTEGER NOT NULL," "rid_min INTEGER NOT NULL," "rid_max INTEGER NOT NULL," "row_count INTEGER NOT NULL," "nonnull_count INTEGER NOT NULL," "sum REAL," "min," "max," "PRIMARY KEY(col,chunk)" ")", p->zDb, p->zChunkTab);
if( zSql==0 ){
columnarFreeVtab(p);
return SQLITE_NOMEM;
}
rc = columnarExec(db, pzErr, zSql);
sqlite3_free(zSql);
if( rc!=SQLITE_OK ){
columnarFreeVtab(p);
return rc;
}
zSql = sqlite3_mprintf("CREATE TABLE %Q.%Q(" "col INTEGER NOT NULL," "chunk INTEGER NOT NULL," "PRIMARY KEY(col,chunk)" ")", p->zDb, p->zDirtyTab);
if( zSql==0 ){
columnarFreeVtab(p);
return SQLITE_NOMEM;
}
rc = columnarExec(db, pzErr, zSql);
sqlite3_free(zSql);
if( rc!=SQLITE_OK ){
columnarFreeVtab(p);
return rc;
}
zSql = sqlite3_mprintf("CREATE TABLE %Q.%Q(" "k TEXT PRIMARY KEY," "v INTEGER NOT NULL" ")", p->zDb, p->zMetaTab);
if( zSql==0 ){
columnarFreeVtab(p);
return SQLITE_NOMEM;
}
rc = columnarExec(db, pzErr, zSql);
sqlite3_free(zSql);
if( rc!=SQLITE_OK ){
columnarFreeVtab(p);
return rc;
}
zSql = sqlite3_mprintf("INSERT INTO %Q.%Q(k,v) VALUES" "('format_version',1)," "('chunk_size',%d)," "('ncol',%d)," "('row_count',0)," "('chunk_count',0)," "('dirty_count',0)," "('stats_valid',0)", p->zDb, p->zMetaTab, COLUMNAR_CHUNK_SIZE, p->nCol);
if( zSql==0 ){
columnarFreeVtab(p);
return SQLITE_NOMEM;
}
rc = columnarExec(db, pzErr, zSql);
sqlite3_free(zSql);
if( rc!=SQLITE_OK ){
columnarFreeVtab(p);
return rc;
}
for(i=0; i<p->nCol; i++){
char *zCol = columnarColumnName(db, argv[3+i], i);
if( zCol==0 ){
columnarFreeVtab(p);
return SQLITE_NOMEM;
}
zSql = sqlite3_mprintf("CREATE TABLE %Q.%Q(rid INTEGER PRIMARY KEY, v)", p->zDb, p->azColTab[i]);
sqlite3_free(zCol);
if( zSql==0 ){
columnarFreeVtab(p);
return SQLITE_NOMEM;
}
rc = columnarExec(db, pzErr, zSql);
sqlite3_free(zSql);
if( rc!=SQLITE_OK ){
columnarFreeVtab(p);
return rc;
}
}
}
*ppVtab = (sqlite3_vtab*)p;
return SQLITE_OK;
}
static int columnarCreate(sqlite3 *db, void *pAux, int argc, const char *const *argv, sqlite3_vtab **ppVtab, char **pzErr){
(void)pAux;
return columnarInit(db, 1, argc, argv, ppVtab, pzErr);
}
static int columnarConnect(sqlite3 *db, void *pAux, int argc, const char *const *argv, sqlite3_vtab **ppVtab, char **pzErr){
(void)pAux;
return columnarInit(db, 0, argc, argv, ppVtab, pzErr);
}
static int columnarDisconnect(sqlite3_vtab *pVtab){
columnarFreeVtab((ColumnarVtab*)pVtab);
return SQLITE_OK;
}
static int columnarDestroy(sqlite3_vtab *pVtab){
ColumnarVtab *p = (ColumnarVtab*)pVtab;
sqlite3 *db = p->db;
char *zErr = 0;
int rc = SQLITE_OK;
int i;
for(i=0; rc==SQLITE_OK && i<p->nCol; i++){
char *zSql = sqlite3_mprintf("DROP TABLE IF EXISTS %Q.%Q", p->zDb, p->azColTab[i]);
if( zSql==0 ){
rc = SQLITE_NOMEM;
}else{
rc = columnarExec(db, &zErr, zSql);
sqlite3_free(zSql);
}
}
if( rc==SQLITE_OK ){
char *zSql = sqlite3_mprintf("DROP TABLE IF EXISTS %Q.%Q", p->zDb, p->zStatTab);
if( zSql==0 ){
rc = SQLITE_NOMEM;
}else{
rc = columnarExec(db, &zErr, zSql);
sqlite3_free(zSql);
}
}
if( rc==SQLITE_OK ){
char *zSql = sqlite3_mprintf("DROP TABLE IF EXISTS %Q.%Q", p->zDb, p->zChunkTab);
if( zSql==0 ){
rc = SQLITE_NOMEM;
}else{
rc = columnarExec(db, &zErr, zSql);
sqlite3_free(zSql);
}
}
if( rc==SQLITE_OK ){
char *zSql = sqlite3_mprintf("DROP TABLE IF EXISTS %Q.%Q", p->zDb, p->zDirtyTab);
if( zSql==0 ){
rc = SQLITE_NOMEM;
}else{
rc = columnarExec(db, &zErr, zSql);
sqlite3_free(zSql);
}
}
if( rc==SQLITE_OK ){
char *zSql = sqlite3_mprintf("DROP TABLE IF EXISTS %Q.%Q", p->zDb, p->zMetaTab);
if( zSql==0 ){
rc = SQLITE_NOMEM;
}else{
rc = columnarExec(db, &zErr, zSql);
sqlite3_free(zSql);
}
}
if( rc==SQLITE_OK ){
char *zSql = sqlite3_mprintf("DROP TABLE IF EXISTS %Q.%Q", p->zDb, p->zRowTab);
if( zSql==0 ){
rc = SQLITE_NOMEM;
}else{
rc = columnarExec(db, &zErr, zSql);
sqlite3_free(zSql);
}
}
if( rc!=SQLITE_OK ){
sqlite3_free(p->base.zErrMsg);
p->base.zErrMsg = zErr;
}else{
sqlite3_free(zErr);
}
columnarDisconnect(pVtab);
return rc;
}
static int columnarOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
ColumnarVtab *p = (ColumnarVtab*)pVtab;
ColumnarCursor *pCur;
pCur = sqlite3_malloc64(sizeof(*pCur));
if( pCur==0 ) return SQLITE_NOMEM;
memset(pCur, 0, sizeof(*pCur));
pCur->iDriver = -1;
pCur->apStmt = sqlite3_malloc64(sizeof(sqlite3_stmt*) * p->nCol);
if( pCur->apStmt==0 ){
sqlite3_free(pCur);
return SQLITE_NOMEM;
}
memset(pCur->apStmt, 0, sizeof(sqlite3_stmt*) * p->nCol);
*ppCursor = &pCur->base;
return SQLITE_OK;
}
static void columnarCursorClear(ColumnarCursor *pCur, int nCol){
int i;
sqlite3_finalize(pCur->pRowStmt);
pCur->pRowStmt = 0;
if( pCur->apStmt ){
for(i=0; i<nCol; i++){
sqlite3_finalize(pCur->apStmt[i]);
pCur->apStmt[i] = 0;
}
}
pCur->bEof = 1;
pCur->bPoint = 0;
pCur->iDriver = -1;
}
static int columnarClose(sqlite3_vtab_cursor *cur){
ColumnarCursor *pCur = (ColumnarCursor*)cur;
ColumnarVtab *p = (ColumnarVtab*)cur->pVtab;
columnarCursorClear(pCur, p->nCol);
sqlite3_free(pCur->apStmt);
sqlite3_free(pCur);
return SQLITE_OK;
}
static int columnarUsed(sqlite3_uint64 m, int iCol){
if( iCol>=63 ) return (m & (((sqlite3_uint64)1)<<63))!=0;
return (m & (((sqlite3_uint64)1)<<iCol))!=0;
}
static int columnarParseU64(const char *z, sqlite3_uint64 *pVal){
sqlite3_uint64 v = 0;
if( z==0 || z[0]==0 ){
*pVal = 0;
return SQLITE_OK;
}
while( z[0]>='0' && z[0]<='9' ){
v = v*10 + (sqlite3_uint64)(z[0]-'0');
z++;
}
*pVal = v;
return SQLITE_OK;
}
static int columnarAdvance(ColumnarCursor *pCur){
ColumnarVtab *p = (ColumnarVtab*)pCur->base.pVtab;
int rc;
int i;
if( pCur->bPoint ){
pCur->bEof = 1;
return SQLITE_OK;
}
if( pCur->iDriver<0 ){
rc = sqlite3_step(pCur->pRowStmt);
if( rc==SQLITE_ROW ){
pCur->iRowid = sqlite3_column_int64(pCur->pRowStmt, 0);
pCur->bEof = 0;
return SQLITE_OK;
}
pCur->bEof = 1;
return rc==SQLITE_DONE ? SQLITE_OK : rc;
}
rc = sqlite3_step(pCur->apStmt[pCur->iDriver]);
if( rc==SQLITE_DONE ){
pCur->bEof = 1;
return SQLITE_OK;
}
if( rc!=SQLITE_ROW ) return rc;
pCur->iRowid = sqlite3_column_int64(pCur->apStmt[pCur->iDriver], 0);
for(i=0; i<p->nCol; i++){
if( i==pCur->iDriver || pCur->apStmt[i]==0 ) continue;
rc = sqlite3_step(pCur->apStmt[i]);
if( rc!=SQLITE_ROW ) return rc==SQLITE_DONE ? SQLITE_CORRUPT : rc;
if( sqlite3_column_int64(pCur->apStmt[i], 0)!=pCur->iRowid ){
return SQLITE_CORRUPT;
}
}
pCur->bEof = 0;
return SQLITE_OK;
}
static int columnarFilter(sqlite3_vtab_cursor *cur, int idxNum, const char *idxStr, int argc, sqlite3_value **argv){
ColumnarCursor *pCur = (ColumnarCursor*)cur;
ColumnarVtab *p = (ColumnarVtab*)cur->pVtab;
char *zErr = 0;
int rc;
int i;
sqlite3_int64 iRowid = 0;
columnarCursorClear(pCur, p->nCol);
columnarParseU64(idxStr, &pCur->colUsed);
pCur->bPoint = (idxNum & 1)!=0;
if( pCur->bPoint ){
assert( argc==1 );
iRowid = sqlite3_value_int64(argv[0]);
rc = columnarPrepare(p->db, &pCur->pRowStmt, &zErr, "SELECT rid FROM %Q.%Q WHERE rid=?1", p->zDb, p->zRowTab);
if( rc==SQLITE_OK ){
sqlite3_bind_int64(pCur->pRowStmt, 1, iRowid);
rc = sqlite3_step(pCur->pRowStmt);
if( rc==SQLITE_ROW ){
pCur->iRowid = iRowid;
pCur->bEof = 0;
rc = SQLITE_OK;
}else{
pCur->bEof = 1;
rc = rc==SQLITE_DONE ? SQLITE_OK : rc;
}
}
sqlite3_free(zErr);
return rc;
}
for(i=0; i<p->nCol; i++){
if( columnarUsed(pCur->colUsed, i) ){
pCur->iDriver = i;
break;
}
}
if( pCur->iDriver<0 ){
rc = columnarPrepare(p->db, &pCur->pRowStmt, &zErr, "SELECT rid FROM %Q.%Q ORDER BY rid", p->zDb, p->zRowTab);
}else{
rc = SQLITE_OK;
for(i=0; rc==SQLITE_OK && i<p->nCol; i++){
if( !columnarUsed(pCur->colUsed, i) ) continue;
rc = columnarPrepare(p->db, &pCur->apStmt[i], &zErr, "SELECT rid, v FROM %Q.%Q ORDER BY rid", p->zDb, p->azColTab[i]);
}
}
if( rc!=SQLITE_OK ){
sqlite3_free(cur->pVtab->zErrMsg);
cur->pVtab->zErrMsg = zErr;
return rc;
}
sqlite3_free(zErr);
return columnarAdvance(pCur);
}
// PRAGMA MARK: - Cursor Output -
static int columnarNext(sqlite3_vtab_cursor *cur){
return columnarAdvance((ColumnarCursor*)cur);
}
static int columnarEof(sqlite3_vtab_cursor *cur){
return ((ColumnarCursor*)cur)->bEof;
}
static int columnarLookupValue(ColumnarVtab *p, sqlite3_int64 iRowid, int iCol, sqlite3_context *ctx){
sqlite3_stmt *pStmt = 0;
char *zErr = 0;
int rc = columnarPrepare(p->db, &pStmt, &zErr, "SELECT v FROM %Q.%Q WHERE rid=?1", p->zDb, p->azColTab[iCol]);
if( rc==SQLITE_OK ){
sqlite3_bind_int64(pStmt, 1, iRowid);
rc = sqlite3_step(pStmt);
if( rc==SQLITE_ROW ){
sqlite3_result_value(ctx, sqlite3_column_value(pStmt, 0));
rc = SQLITE_OK;
}else if( rc==SQLITE_DONE ){
sqlite3_result_null(ctx);
rc = SQLITE_OK;
}
}
sqlite3_finalize(pStmt);
sqlite3_free(zErr);
return rc;
}
static int columnarColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
ColumnarCursor *pCur = (ColumnarCursor*)cur;
ColumnarVtab *p = (ColumnarVtab*)cur->pVtab;
if( i<0 || i>=p->nCol ){
sqlite3_result_null(ctx);
return SQLITE_OK;
}
if( !pCur->bPoint && pCur->apStmt[i] ){
sqlite3_result_value(ctx, sqlite3_column_value(pCur->apStmt[i], 1));
return SQLITE_OK;
}
return columnarLookupValue(p, pCur->iRowid, i, ctx);
}
static int columnarRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
*pRowid = ((ColumnarCursor*)cur)->iRowid;
return SQLITE_OK;
}
static int columnarBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
int i;
int iRowidCons = -1;
(void)tab;
for(i=0; i<pIdxInfo->nConstraint; i++){
const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
if( pCons->usable && pCons->iColumn<0
&& pCons->op==SQLITE_INDEX_CONSTRAINT_EQ
){
iRowidCons = i;
break;
}
}
if( iRowidCons>=0 ){
pIdxInfo->aConstraintUsage[iRowidCons].argvIndex = 1;
pIdxInfo->aConstraintUsage[iRowidCons].omit = 1;
pIdxInfo->idxNum = 1;
pIdxInfo->estimatedRows = 1;
pIdxInfo->estimatedCost = 5.0;
}else{
pIdxInfo->idxNum = 0;
pIdxInfo->estimatedRows = 1000000;
pIdxInfo->estimatedCost = 1000000.0;
}
pIdxInfo->idxStr = sqlite3_mprintf("%llu", (unsigned long long)pIdxInfo->colUsed);
if( pIdxInfo->idxStr==0 ) return SQLITE_NOMEM;
pIdxInfo->needToFreeIdxStr = 1;
return SQLITE_OK;
}
static int columnarStepDone(sqlite3_stmt *pStmt){
int rc = sqlite3_step(pStmt);
return rc==SQLITE_DONE ? SQLITE_OK : rc;
}
// PRAGMA MARK: - Metadata -
static int columnarEnsureMetaTable(sqlite3 *db, const ColumnarTableRef *pRef, char **pzErr){
char *zSql = sqlite3_mprintf("CREATE TABLE IF NOT EXISTS \"%w\".\"%w__columnar_meta\"(" "k TEXT PRIMARY KEY," "v INTEGER NOT NULL" ")", pRef->zDb, pRef->zTab);
int rc;
if( zSql==0 ) return SQLITE_NOMEM;
rc = columnarExec(db, pzErr, zSql);
sqlite3_free(zSql);
return rc;
}
static int columnarMetaGetInt(sqlite3 *db, const ColumnarTableRef *pRef, const char *zKey, sqlite3_int64 *pVal, int *pbFound){
sqlite3_stmt *pStmt = 0;
int rc = columnarPrepare(db, &pStmt, 0, "SELECT v FROM \"%w\".\"%w__columnar_meta\" WHERE k=?1", pRef->zDb, pRef->zTab);
*pVal = 0;
*pbFound = 0;
if( rc!=SQLITE_OK ) return rc;
sqlite3_bind_text(pStmt, 1, zKey, -1, SQLITE_STATIC);
rc = sqlite3_step(pStmt);
if( rc==SQLITE_ROW ){
*pVal = sqlite3_column_int64(pStmt, 0);
*pbFound = 1;
rc = SQLITE_OK;
}else if( rc==SQLITE_DONE ){
rc = SQLITE_OK;
}
sqlite3_finalize(pStmt);
return rc;
}
static int columnarMetaSetInt(sqlite3 *db, const ColumnarTableRef *pRef, const char *zKey, sqlite3_int64 iVal){
sqlite3_stmt *pStmt = 0;
int rc = columnarPrepare(db, &pStmt, 0, "REPLACE INTO \"%w\".\"%w__columnar_meta\"(k,v) VALUES(?1,?2)", pRef->zDb, pRef->zTab);
if( rc!=SQLITE_OK ) return rc;
sqlite3_bind_text(pStmt, 1, zKey, -1, SQLITE_STATIC);
sqlite3_bind_int64(pStmt, 2, iVal);
rc = columnarStepDone(pStmt);
sqlite3_finalize(pStmt);
return rc;
}
static int columnarMetaIncrInt(sqlite3 *db, const ColumnarTableRef *pRef, const char *zKey, sqlite3_int64 iDelta){
sqlite3_stmt *pStmt = 0;
int rc = columnarPrepare(db, &pStmt, 0, "INSERT OR IGNORE INTO \"%w\".\"%w__columnar_meta\"(k,v)" " VALUES(?1,0)", pRef->zDb, pRef->zTab);
if( rc!=SQLITE_OK ) return rc;
sqlite3_bind_text(pStmt, 1, zKey, -1, SQLITE_STATIC);
rc = columnarStepDone(pStmt);
sqlite3_finalize(pStmt);
if( rc!=SQLITE_OK ) return rc;
rc = columnarPrepare(db, &pStmt, 0, "UPDATE \"%w\".\"%w__columnar_meta\" SET v=v+?1 WHERE k=?2", pRef->zDb, pRef->zTab);
if( rc!=SQLITE_OK ) return rc;
sqlite3_bind_int64(pStmt, 1, iDelta);
sqlite3_bind_text(pStmt, 2, zKey, -1, SQLITE_STATIC);
rc = columnarStepDone(pStmt);
sqlite3_finalize(pStmt);
return rc;
}
static int columnarMetaEnsureBase(sqlite3 *db, const ColumnarTableRef *pRef, int nCol){
int rc;
rc = columnarMetaSetInt(db, pRef, "format_version", 1);
if( rc==SQLITE_OK ){
rc = columnarMetaSetInt(db, pRef, "chunk_size", COLUMNAR_CHUNK_SIZE);
}
if( rc==SQLITE_OK && nCol>=0 ){
rc = columnarMetaSetInt(db, pRef, "ncol", nCol);
}
return rc;
}
static sqlite3_int64 columnarChunkOfRowid(sqlite3_int64 iRowid){
return (iRowid - 1) / COLUMNAR_CHUNK_SIZE;
}
static int columnarEnsureDirtyTable(sqlite3 *db, const ColumnarTableRef *pRef, char **pzErr){
char *zSql = sqlite3_mprintf("CREATE TABLE IF NOT EXISTS \"%w\".\"%w__columnar_dirty\"(" "col INTEGER NOT NULL," "chunk INTEGER NOT NULL," "PRIMARY KEY(col,chunk)" ")", pRef->zDb, pRef->zTab);
int rc;
if( zSql==0 ) return SQLITE_NOMEM;
rc = columnarExec(db, pzErr, zSql);
sqlite3_free(zSql);
return rc;
}
static int columnarMarkDirtyRowid(ColumnarVtab *p, sqlite3_int64 iRowid){
ColumnarTableRef sRef = {p->zDb, p->zName};
sqlite3_stmt *pStmt = 0;
sqlite3_int64 iChunk = columnarChunkOfRowid(iRowid);
sqlite3_int64 nNewDirty = 0;
int rc;
int i;
rc = columnarEnsureMetaTable(p->db, &sRef, 0);
if( rc!=SQLITE_OK ) return rc;
rc = columnarMetaEnsureBase(p->db, &sRef, p->nCol);
if( rc!=SQLITE_OK ) return rc;
rc = columnarMetaSetInt(p->db, &sRef, "stats_valid", 0);
if( rc!=SQLITE_OK ) return rc;
rc = columnarEnsureDirtyTable(p->db, &sRef, 0);
if( rc!=SQLITE_OK ) return rc;
rc = columnarPrepare(p->db, &pStmt, 0, "DELETE FROM %Q.%Q", p->zDb, p->zStatTab);
if( rc==SQLITE_OK ) rc = columnarStepDone(pStmt);
sqlite3_finalize(pStmt);
pStmt = 0;
if( rc!=SQLITE_OK ) return rc;
rc = columnarPrepare(p->db, &pStmt, 0, "INSERT OR IGNORE INTO %Q.%Q(col,chunk) VALUES(?1,?2)", p->zDb, p->zDirtyTab);
if( rc!=SQLITE_OK ) return rc;
for(i=0; rc==SQLITE_OK && i<p->nCol; i++){
sqlite3_bind_int(pStmt, 1, i);
sqlite3_bind_int64(pStmt, 2, iChunk);
rc = columnarStepDone(pStmt);
if( rc==SQLITE_OK ) nNewDirty += sqlite3_changes(p->db);
sqlite3_reset(pStmt);
sqlite3_clear_bindings(pStmt);
}
sqlite3_finalize(pStmt);
if( rc==SQLITE_OK && nNewDirty>0 ){
rc = columnarMetaIncrInt(p->db, &sRef, "dirty_count", nNewDirty);
}
return rc;
}
// PRAGMA MARK: - Row Mutation -
static int columnarDeleteRow(ColumnarVtab *p, sqlite3_int64 iRowid){
sqlite3_stmt *pStmt = 0;
int rc;
int i;
rc = columnarPrepare(p->db, &pStmt, 0, "DELETE FROM %Q.%Q WHERE rid=?1", p->zDb, p->zRowTab);
if( rc==SQLITE_OK ){
sqlite3_bind_int64(pStmt, 1, iRowid);
rc = columnarStepDone(pStmt);
}
sqlite3_finalize(pStmt);
for(i=0; rc==SQLITE_OK && i<p->nCol; i++){
rc = columnarPrepare(p->db, &pStmt, 0, "DELETE FROM %Q.%Q WHERE rid=?1", p->zDb, p->azColTab[i]);
if( rc==SQLITE_OK ){
sqlite3_bind_int64(pStmt, 1, iRowid);
rc = columnarStepDone(pStmt);
}
sqlite3_finalize(pStmt);
}
return rc;
}
static int columnarNextRowid(ColumnarVtab *p, sqlite3_int64 *pRowid){
sqlite3_stmt *pStmt = 0;
int rc = columnarPrepare(p->db, &pStmt, 0, "SELECT coalesce(max(rid),0)+1 FROM %Q.%Q", p->zDb, p->zRowTab);
if( rc==SQLITE_OK ){
rc = sqlite3_step(pStmt);
if( rc==SQLITE_ROW ){
*pRowid = sqlite3_column_int64(pStmt, 0);
rc = SQLITE_OK;
}
}
sqlite3_finalize(pStmt);
return rc;
}
static int columnarRowidExists(ColumnarVtab *p, sqlite3_int64 iRowid, int *pbExists){
sqlite3_stmt *pStmt = 0;
int rc = columnarPrepare(p->db, &pStmt, 0, "SELECT 1 FROM %Q.%Q WHERE rid=?1", p->zDb, p->zRowTab);
*pbExists = 0;
if( rc==SQLITE_OK ){
sqlite3_bind_int64(pStmt, 1, iRowid);
rc = sqlite3_step(pStmt);
if( rc==SQLITE_ROW ){
*pbExists = 1;
rc = SQLITE_OK;
}else if( rc==SQLITE_DONE ){
rc = SQLITE_OK;
}
}
sqlite3_finalize(pStmt);
return rc;
}
static int columnarInsertRow(ColumnarVtab *p, sqlite3_int64 iRowid, int argc, sqlite3_value **argv){
sqlite3_stmt *pStmt = 0;
int rc;
int i;
const char *zConflict = "";
int eConflict = sqlite3_vtab_on_conflict(p->db);
if( eConflict==SQLITE_REPLACE ) zConflict = "OR REPLACE ";
else if( eConflict==SQLITE_IGNORE ) zConflict = "OR IGNORE ";
else if( eConflict==SQLITE_FAIL ) zConflict = "OR FAIL ";
else if( eConflict==SQLITE_ROLLBACK ) zConflict = "OR ROLLBACK ";
rc = columnarPrepare(p->db, &pStmt, 0, "INSERT %sINTO %Q.%Q(rid) VALUES(?1)", zConflict, p->zDb, p->zRowTab);
if( rc==SQLITE_OK ){
sqlite3_bind_int64(pStmt, 1, iRowid);
rc = columnarStepDone(pStmt);
}
sqlite3_finalize(pStmt);
for(i=0; rc==SQLITE_OK && i<p->nCol; i++){
rc = columnarPrepare(p->db, &pStmt, 0, "INSERT %sINTO %Q.%Q(rid,v) VALUES(?1,?2)", zConflict, p->zDb, p->azColTab[i]);
if( rc==SQLITE_OK ){
sqlite3_bind_int64(pStmt, 1, iRowid);
sqlite3_bind_value(pStmt, 2, argv[2+i]);
rc = columnarStepDone(pStmt);
}
sqlite3_finalize(pStmt);
}
(void)argc;
return rc;
}
static int columnarUpdate(sqlite3_vtab *pVtab, int argc, sqlite3_value **argv, sqlite_int64 *pRowid){
ColumnarVtab *p = (ColumnarVtab*)pVtab;
ColumnarTableRef sRef = {p->zDb, p->zName};
sqlite3_int64 oldRowid = 0;
sqlite3_int64 newRowid = 0;
int bExists = 0;
int bInsert = 0;
int bReplaceExisting = 0;
int rc;
if( argc==1 ){
oldRowid = sqlite3_value_int64(argv[0]);
rc = columnarRowidExists(p, oldRowid, &bExists);
if( rc!=SQLITE_OK ) return rc;
rc = columnarMarkDirtyRowid(p, oldRowid);
if( rc!=SQLITE_OK ) return rc;
rc = columnarDeleteRow(p, oldRowid);
if( rc==SQLITE_OK && bExists ){
rc = columnarMetaIncrInt(p->db, &sRef, "row_count", -1);
}
return rc;
}
if( sqlite3_value_type(argv[1])==SQLITE_NULL ){
rc = columnarNextRowid(p, &newRowid);
if( rc!=SQLITE_OK ) return rc;
}else{
newRowid = sqlite3_value_int64(argv[1]);
}