Changeset: d155fefef655 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/d155fefef655
Modified Files:
        sql/backends/monet5/sql.c
Branch: Jul2021
Log Message:

small fix for the sql_storage, don't access the deleted rows


diffs (truncated from 397 to 300 lines):

diff --git a/sql/backends/monet5/sql.c b/sql/backends/monet5/sql.c
--- a/sql/backends/monet5/sql.c
+++ b/sql/backends/monet5/sql.c
@@ -3932,33 +3932,164 @@ sql_storage(Client cntxt, MalBlkPtr mb, 
        os_iterator(&si, tr->cat->schemas, tr, NULL);
        for (sql_base *b = oi_next(&si); b; b = oi_next(&si)) {
                sql_schema *s = (sql_schema *) b;
-               if( sname && strcmp(b->name, sname) )
+               if ((sname && strcmp(b->name, sname) ) || b->name[0] == '%')
                        continue;
-               if (b->name[0] != '%')
-                       if (s->tables) {
-                               struct os_iter oi;
-
-                               os_iterator(&oi, s->tables, tr, NULL);
-                               for (sql_base *bt = oi_next(&oi); bt; bt = 
oi_next(&oi)) {
-                                       sql_table *t = (sql_table *) bt;
-                                       if( tname && strcmp(bt->name, tname) )
-                                               continue;
-                                       if (isTable(t))
-                                               if (ol_first_node(t->columns))
-                                                       for (ncol = 
ol_first_node((t)->columns); ncol; ncol = ncol->next) {
-                                                               sql_base *bc = 
ncol->data;
-                                                               sql_column *c = 
(sql_column *) ncol->data;
+               if (s->tables) {
+                       struct os_iter oi;
+
+                       os_iterator(&oi, s->tables, tr, NULL);
+                       for (sql_base *bt = oi_next(&oi); bt; bt = 
oi_next(&oi)) {
+                               sql_table *t = (sql_table *) bt;
+                               if( tname && strcmp(bt->name, tname) )
+                                       continue;
+                               if (isTable(t)) {
+                                       if (ol_first_node(t->columns)) {
+                                               for (ncol = 
ol_first_node((t)->columns); ncol; ncol = ncol->next) {
+                                                       sql_base *bc = 
ncol->data;
+                                                       sql_column *c = 
(sql_column *) ncol->data;
+                                                       lng sz;
+
+                                                       if( cname && 
strcmp(bc->name, cname) )
+                                                               continue;
+                                                       bn = 
store->storage_api.bind_col(tr, c, RDONLY); /* is slice */
+                                                       bs = 
store->storage_api.bind_col(tr, c, QUICK);
+                                                       if (bn == NULL || bs == 
NULL) {
+                                                               msg = 
createException(SQL, "sql.storage", SQLSTATE(HY005) "Cannot access column 
descriptor");
+                                                               goto bailout;
+                                                       }
+
+                                                       /*printf("schema 
%s.%s.%s" , b->name, bt->name, bc->name); */
+                                                       if (BUNappend(sch, 
b->name, false) != GDK_SUCCEED ||
+                                                           BUNappend(tab, 
bt->name, false) != GDK_SUCCEED ||
+                                                           BUNappend(col, 
bc->name, false) != GDK_SUCCEED)
+                                                               goto bailout;
+                                                       if (c->t->access == 
TABLE_WRITABLE) {
+                                                               if 
(BUNappend(mode, "writable", false) != GDK_SUCCEED)
+                                                                       goto 
bailout;
+                                                       } else if (c->t->access 
== TABLE_APPENDONLY) {
+                                                               if 
(BUNappend(mode, "appendonly", false) != GDK_SUCCEED)
+                                                                       goto 
bailout;
+                                                       } else if (c->t->access 
== TABLE_READONLY) {
+                                                               if 
(BUNappend(mode, "readonly", false) != GDK_SUCCEED)
+                                                                       goto 
bailout;
+                                                       } else {
+                                                               if 
(BUNappend(mode, str_nil, false) != GDK_SUCCEED)
+                                                                       goto 
bailout;
+                                                       }
+                                                       if (BUNappend(type, 
c->type.type->base.name, false) != GDK_SUCCEED)
+                                                               goto bailout;
+
+                                                       /*printf(" cnt "BUNFMT, 
BATcount(bs)); */
+                                                       sz = BATcount(bs);
+                                                       if (BUNappend(cnt, &sz, 
false) != GDK_SUCCEED)
+                                                               goto bailout;
+
+                                                       /*printf(" loc %s", 
BBP_physical(bs->batCacheid)); */
+                                                       if (BUNappend(loc, 
BBP_physical(bs->batCacheid), false) != GDK_SUCCEED)
+                                                               goto bailout;
+                                                       /*printf(" width %d", 
bn->twidth); */
+                                                       w = bn->twidth;
+                                                       if (bn->ttype == 
TYPE_str) {
+                                                               double sum = 0;
+                                                               BATiter bi = 
bat_iterator(bn);
+                                                               lng cnt1, cnt2 
= cnt1 = (lng) BATcount(bn);
+                                                               BAT *cands = 
store->storage_api.bind_cands(tr, t, 1, 0);
+                                                               oid hseq = 
bn->hseqbase;
+
+                                                               if (cands) {
+                                                                       BUN lo, 
hi;
+                                                                       struct 
canditer ci = {0};
+                                                                       /* just 
take a sample */
+                                                                       if 
(cnt1 > 512)
+                                                                               
cnt1 = cnt2 = 512;
+                                                                       
canditer_init(&ci, NULL, cands);
+                                                                       for(lo 
= 0, hi = ci.ncand; lo < hi; lo++) {
+                                                                               
oid o = canditer_next(&ci);
+                                                                               
str s = BUNtail(bi, o - hseq);
+                                                                               
if (!strNil(s))
+                                                                               
        sum += strlen(s);
+                                                                               
if (--cnt1 <= 0)
+                                                                               
        break;
+                                                                       }
+                                                                       
BBPunfix(cands->batCacheid);
+                                                               }
+                                                               
bat_iterator_end(&bi);
+                                                               if (cnt2)
+                                                                       w = 
(int) (sum / cnt2);
+                                                       } else if 
(ATOMvarsized(bn->ttype)) {
+                                                               sz = 
BATcount(bn);
+                                                               if (sz > 0)
+                                                                       w = 
(int) ((bn->tvheap->free + sz / 2) / sz);
+                                                               else
+                                                                       w = 0;
+                                                       }
+                                                       if (BUNappend(atom, &w, 
false) != GDK_SUCCEED)
+                                                               goto bailout;
+
+                                                       sz = BATcount(bs) << 
bn->tshift;
+                                                       if (BUNappend(size, 
&sz, false) != GDK_SUCCEED)
+                                                               goto bailout;
+
+                                                       sz = 
heapinfo(bs->tvheap, bs->batCacheid);
+                                                       if (BUNappend(heap, 
&sz, false) != GDK_SUCCEED)
+                                                               goto bailout;
+
+                                                       
MT_rwlock_rdlock(&bs->thashlock);
+                                                       sz = 
hashinfo(bs->thash, bs->batCacheid);
+                                                       
MT_rwlock_rdunlock(&bs->thashlock);
+                                                       if (BUNappend(indices, 
&sz, false) != GDK_SUCCEED)
+                                                               goto bailout;
+
+                                                       bitval = 0; /* 
HASHispersistent(bs); */
+                                                       if (BUNappend(phash, 
&bitval, false) != GDK_SUCCEED)
+                                                               goto bailout;
+
+                                                       sz = 
IMPSimprintsize(bs);
+                                                       if (BUNappend(imprints, 
&sz, false) != GDK_SUCCEED)
+                                                               goto bailout;
+                                                       /*printf(" indices 
"BUNFMT, bn->thash?bn->thash->heap.size:0); */
+                                                       /*printf("\n"); */
+                                                       bitval = 
BATtordered(bs);
+                                                       if (!bitval && 
bs->tnosorted == 0)
+                                                               bitval = 
bit_nil;
+                                                       if (BUNappend(sort, 
&bitval, false) != GDK_SUCCEED)
+                                                               goto bailout;
+
+                                                       bitval = 
BATtrevordered(bs);
+                                                       if (!bitval && 
bs->tnorevsorted == 0)
+                                                               bitval = 
bit_nil;
+                                                       if (BUNappend(revsort, 
&bitval, false) != GDK_SUCCEED)
+                                                               goto bailout;
+
+                                                       bitval = BATtkey(bs);
+                                                       if (!bitval && 
bs->tnokey[0] == 0 && bs->tnokey[1] == 0)
+                                                               bitval = 
bit_nil;
+                                                       if (BUNappend(key, 
&bitval, false) != GDK_SUCCEED)
+                                                               goto bailout;
+
+                                                       sz = bs->torderidx && 
bs->torderidx != (Heap *) 1 ? bs->torderidx->free : 0;
+                                                       if (BUNappend(oidx, 
&sz, false) != GDK_SUCCEED)
+                                                               goto bailout;
+                                                       
BBPunfix(bn->batCacheid);
+                                                       bn = NULL;
+                                               }
+                                       }
+
+                                       if (t->idxs) {
+                                               for (ncol = 
ol_first_node((t)->idxs); ncol; ncol = ncol->next) {
+                                                       sql_base *bc = 
ncol->data;
+                                                       sql_idx *c = (sql_idx 
*) ncol->data;
+                                                       if 
(idx_has_column(c->type)) {
+                                                               bn = 
store->storage_api.bind_idx(tr, c, RDONLY);
+                                                               bs = 
store->storage_api.bind_idx(tr, c, QUICK);
                                                                lng sz;
 
-                                                               if( cname && 
strcmp(bc->name, cname) )
-                                                                       
continue;
-                                                               bn = 
store->storage_api.bind_col(tr, c, RDONLY); /* is slice */
-                                                               bs = 
store->storage_api.bind_col(tr, c, QUICK);
                                                                if (bn == NULL 
|| bs == NULL) {
                                                                        msg = 
createException(SQL, "sql.storage", SQLSTATE(HY005) "Cannot access column 
descriptor");
                                                                        goto 
bailout;
                                                                }
-
+                                                               if( cname && 
strcmp(bc->name, cname) )
+                                                                       
continue;
                                                                
/*printf("schema %s.%s.%s" , b->name, bt->name, bc->name); */
                                                                if 
(BUNappend(sch, b->name, false) != GDK_SUCCEED ||
                                                                    
BUNappend(tab, bt->name, false) != GDK_SUCCEED ||
@@ -3977,7 +4108,7 @@ sql_storage(Client cntxt, MalBlkPtr mb, 
                                                                        if 
(BUNappend(mode, str_nil, false) != GDK_SUCCEED)
                                                                                
goto bailout;
                                                                }
-                                                               if 
(BUNappend(type, c->type.type->base.name, false) != GDK_SUCCEED)
+                                                               if 
(BUNappend(type, "oid", false) != GDK_SUCCEED)
                                                                        goto 
bailout;
 
                                                                /*printf(" cnt 
"BUNFMT, BATcount(bs)); */
@@ -3994,7 +4125,7 @@ sql_storage(Client cntxt, MalBlkPtr mb, 
                                                                        BUN p, 
q;
                                                                        double 
sum = 0;
                                                                        BATiter 
bi = bat_iterator(bn);
-                                                                       lng 
cnt1, cnt2 = cnt1 = (lng) BATcount(bn);
+                                                                       lng 
cnt1, cnt2 = cnt1 = BATcount(bn);
 
                                                                        /* just 
take a sample */
                                                                        if 
(cnt1 > 512)
@@ -4009,30 +4140,23 @@ sql_storage(Client cntxt, MalBlkPtr mb, 
                                                                        
bat_iterator_end(&bi);
                                                                        if 
(cnt2)
                                                                                
w = (int) (sum / cnt2);
-                                                               } else if 
(ATOMvarsized(bn->ttype)) {
-                                                                       sz = 
BATcount(bn);
-                                                                       if (sz 
> 0)
-                                                                               
w = (int) ((bn->tvheap->free + sz / 2) / sz);
-                                                                       else
-                                                                               
w = 0;
                                                                }
                                                                if 
(BUNappend(atom, &w, false) != GDK_SUCCEED)
                                                                        goto 
bailout;
-
-                                                               sz = 
BATcount(bs) << bn->tshift;
+                                                               /*printf(" size 
"BUNFMT, tailsize(bn,BATcount(bn)) + (bn->tvheap? bn->tvheap->size:0)); */
+                                                               sz = 
tailsize(bs, BATcount(bs));
                                                                if 
(BUNappend(size, &sz, false) != GDK_SUCCEED)
                                                                        goto 
bailout;
 
-                                                               sz = 
heapinfo(bs->tvheap, bs->batCacheid);
+                                                               sz = bs->tvheap 
? bs->tvheap->size : 0;
                                                                if 
(BUNappend(heap, &sz, false) != GDK_SUCCEED)
                                                                        goto 
bailout;
 
                                                                
MT_rwlock_rdlock(&bs->thashlock);
-                                                               sz = 
hashinfo(bs->thash, bs->batCacheid);
+                                                               sz = bs->thash 
&& bs->thash != (Hash *) 1 ? bs->thash->heaplink.size + 
bs->thash->heapbckt.size : 0; /* HASHsize() */
                                                                
MT_rwlock_rdunlock(&bs->thashlock);
                                                                if 
(BUNappend(indices, &sz, false) != GDK_SUCCEED)
                                                                        goto 
bailout;
-
                                                                bitval = 0; /* 
HASHispersistent(bs); */
                                                                if 
(BUNappend(phash, &bitval, false) != GDK_SUCCEED)
                                                                        goto 
bailout;
@@ -4040,151 +4164,34 @@ sql_storage(Client cntxt, MalBlkPtr mb, 
                                                                sz = 
IMPSimprintsize(bs);
                                                                if 
(BUNappend(imprints, &sz, false) != GDK_SUCCEED)
                                                                        goto 
bailout;
-                                                               /*printf(" 
indices "BUNFMT, bn->thash?bn->thash->heap.size:0); */
+                                                               /*printf(" 
indices "BUNFMT, 
bs->thash?bs->thash->heaplink.size+bs->thash->heapbckt.size:0); */
                                                                /*printf("\n"); 
*/
-
                                                                bitval = 
BATtordered(bs);
                                                                if (!bitval && 
bs->tnosorted == 0)
                                                                        bitval 
= bit_nil;
                                                                if 
(BUNappend(sort, &bitval, false) != GDK_SUCCEED)
                                                                        goto 
bailout;
-
                                                                bitval = 
BATtrevordered(bs);
                                                                if (!bitval && 
bs->tnorevsorted == 0)
                                                                        bitval 
= bit_nil;
                                                                if 
(BUNappend(revsort, &bitval, false) != GDK_SUCCEED)
                                                                        goto 
bailout;
-
                                                                bitval = 
BATtkey(bs);
                                                                if (!bitval && 
bs->tnokey[0] == 0 && bs->tnokey[1] == 0)
                                                                        bitval 
= bit_nil;
                                                                if 
(BUNappend(key, &bitval, false) != GDK_SUCCEED)
                                                                        goto 
bailout;
-
                                                                sz = 
bs->torderidx && bs->torderidx != (Heap *) 1 ? bs->torderidx->free : 0;
                                                                if 
(BUNappend(oidx, &sz, false) != GDK_SUCCEED)
                                                                        goto 
bailout;
                                                                
BBPunfix(bn->batCacheid);
                                                                bn = NULL;
                                                        }
-
-                                       if (isTable(t))
-                                               if (t->idxs)
-                                                       for (ncol = 
ol_first_node((t)->idxs); ncol; ncol = ncol->next) {
-                                                               sql_base *bc = 
ncol->data;
-                                                               sql_idx *c = 
(sql_idx *) ncol->data;
-                                                               if 
(idx_has_column(c->type)) {
-                                                                       bn = 
store->storage_api.bind_idx(tr, c, RDONLY);
-                                                                       bs = 
store->storage_api.bind_idx(tr, c, QUICK);
-                                                                       lng sz;
-
-                                                                       if (bn 
== NULL || bs == NULL) {
-                                                                               
msg = createException(SQL, "sql.storage", SQLSTATE(HY005) "Cannot access column 
descriptor");
-                                                                               
goto bailout;
-                                                                       }
-                                                                       if( 
cname && strcmp(bc->name, cname) )
-                                                                               
continue;
-                                                                       
/*printf("schema %s.%s.%s" , b->name, bt->name, bc->name); */
-                                                                       if 
(BUNappend(sch, b->name, false) != GDK_SUCCEED ||
-                                                                           
BUNappend(tab, bt->name, false) != GDK_SUCCEED ||
-                                                                           
BUNappend(col, bc->name, false) != GDK_SUCCEED)
-                                                                               
goto bailout;
-                                                                       if 
(c->t->access == TABLE_WRITABLE) {
-                                                                               
if (BUNappend(mode, "writable", false) != GDK_SUCCEED)
-                                                                               
        goto bailout;
-                                                                       } else 
if (c->t->access == TABLE_APPENDONLY) {
-                                                                               
if (BUNappend(mode, "appendonly", false) != GDK_SUCCEED)
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to