Changeset: bcfdf89c5133 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/bcfdf89c5133
Modified Files:
        sql/backends/monet5/sql_statistics.c
        sql/storage/bat/bat_storage.c
        sql/storage/sql_storage.h
        sql/storage/store.c
Branch: analyze-fix
Log Message:

Use column lock to avoid redundant work when caching min/max values in the sql 
layer. It also avoids a possible leak while setting the min/max values. Small 
cleanup


diffs (134 lines):

diff --git a/sql/backends/monet5/sql_statistics.c 
b/sql/backends/monet5/sql_statistics.c
--- a/sql/backends/monet5/sql_statistics.c
+++ b/sql/backends/monet5/sql_statistics.c
@@ -320,14 +320,6 @@ sql_statistics(Client cntxt, MalBlkPtr m
                                                                msg = 
createException(SQL, "sql.statistics", SQLSTATE(HY005) "Cannot access column 
descriptor");
                                                                goto bailout;
                                                        }
-                                                       if (isVIEW(fb)) { /* If 
it is a view get the parent BAT, but maybe we can remove this here */
-                                                               BAT *nb = 
BBP_cache(VIEWtparent(fb));
-                                                               
BBPunfix(fb->batCacheid);
-                                                               if (!(fb = 
BATdescriptor(nb->batCacheid))) {
-                                                                       msg = 
createException(SQL, "sql.statistics", SQLSTATE(HY005) "Cannot access column 
descriptor");
-                                                                       goto 
bailout;
-                                                               }
-                                                       }
 
                                                        BATiter bi = 
bat_iterator(fb);
                                                        if (fb->tminpos != 
BUN_NONE || fb->tmaxpos != BUN_NONE) {
diff --git a/sql/storage/bat/bat_storage.c b/sql/storage/bat/bat_storage.c
--- a/sql/storage/bat/bat_storage.c
+++ b/sql/storage/bat/bat_storage.c
@@ -2321,42 +2321,44 @@ dcount_col(sql_trans *tr, sql_column *c)
 }
 
 static int
-min_max_col(sql_trans *tr, sql_column *col, size_t *minlen, void **min, size_t 
*maxlen, void **max)
+min_max_col(sql_trans *tr, sql_column *c)
 {
        int ok = 0;
+       BAT *b = NULL;
+       sql_delta *d = NULL;
 
        assert(tr->active);
-       *min = NULL;
-       *max = NULL;
-       if (!isTable(col->t) || !col->t->s)
-               return ok;
-
-       if (col && ATOMIC_PTR_GET(&col->data)) {
-               BAT *b = bind_col(tr, col, QUICK), *fb = NULL;
-               if (b && b->tminpos != BUN_NONE && b->tmaxpos != BUN_NONE && 
(fb = bind_col(tr, col, RDONLY))) {
-                       BATiter bi = bat_iterator(fb);
-                       if (fb->tminpos != BUN_NONE && fb->tmaxpos != BUN_NONE) 
{
-                               void *nmin = BUNtail(bi, fb->tminpos), *nmax = 
BUNtail(bi, fb->tmaxpos);
-
-                               *minlen = ATOMlen(fb->ttype, nmin);
-                               *maxlen = ATOMlen(fb->ttype, nmax);
-                               if (!(*min = GDKmalloc(*minlen)) || !(*max = 
GDKmalloc(*maxlen))) {
-                                       GDKfree(*min);
-                                       GDKfree(*max);
-                                       *min = NULL;
-                                       *max = NULL;
-                                       *minlen = 0;
-                                       *maxlen = 0;
-                               } else {
-                                       memcpy(*min, nmin, *minlen);
-                                       memcpy(*max, nmax, *maxlen);
-                                       ok = 1;
-                               }
+       if (!c || !ATOMIC_PTR_GET(&c->data) || !isTable(c->t) || !c->t->s)
+               return 0;
+       if (c->min && c->max)
+               return 1;
+
+       lock_column(tr->store, c->base.id);
+       if (c->min && c->max) {
+               unlock_column(tr->store, c->base.id);
+               return 1;
+       }
+       _DELETE(c->min);
+       _DELETE(c->max);
+       if ((d = ATOMIC_PTR_GET(&c->data)) && (b = temp_descriptor(d->cs.bid))) 
{
+               BATiter bi = bat_iterator(b);
+               if (b->tminpos != BUN_NONE && b->tmaxpos != BUN_NONE) {
+                       void *nmin = BUNtail(bi, b->tminpos), *nmax = 
BUNtail(bi, b->tmaxpos);
+                       size_t minlen = ATOMlen(b->ttype, nmin), maxlen = 
ATOMlen(b->ttype, nmax);
+
+                       if (!(c->min = GDKmalloc(minlen)) || !(c->max = 
GDKmalloc(maxlen))) {
+                               _DELETE(c->min);
+                               _DELETE(c->max);
+                       } else {
+                               memcpy(c->min, nmin, minlen);
+                               memcpy(c->max, nmax, maxlen);
+                               ok = 1;
                        }
-                       bat_iterator_end(&bi);
-                       BBPunfix(fb->batCacheid);
                }
+               bat_iterator_end(&bi);
+               bat_destroy(b);
        }
+       unlock_column(tr->store, c->base.id);
        return ok;
 }
 
diff --git a/sql/storage/sql_storage.h b/sql/storage/sql_storage.h
--- a/sql/storage/sql_storage.h
+++ b/sql/storage/sql_storage.h
@@ -152,7 +152,7 @@ typedef size_t (*count_del_fptr) (sql_tr
 typedef size_t (*count_col_fptr) (sql_trans *tr, sql_column *c, int access);
 typedef size_t (*count_idx_fptr) (sql_trans *tr, sql_idx *i, int access);
 typedef size_t (*dcount_col_fptr) (sql_trans *tr, sql_column *c);
-typedef int (*min_max_col_fptr) (sql_trans *tr, sql_column *c, size_t *minlen, 
void **min, size_t *maxlen, void **max);
+typedef int (*min_max_col_fptr) (sql_trans *tr, sql_column *c);
 typedef int (*prop_col_fptr) (sql_trans *tr, sql_column *c);
 
 /*
diff --git a/sql/storage/store.c b/sql/storage/store.c
--- a/sql/storage/store.c
+++ b/sql/storage/store.c
@@ -6181,19 +6181,10 @@ sql_trans_ranges( sql_trans *tr, sql_col
        *min = NULL;
        *max = NULL;
        if (col && isTable(col->t)) {
-               if (col->min && col->max) {
-                       *min = col->min;
-                       *max = col->max;
-               } else {
-                       void *smin = NULL, *smax = NULL;
-                       size_t minlen = 0, maxlen = 0;
-                       if (store->storage_api.min_max_col(tr, col, &minlen, 
&smin, &maxlen, &smax)) {
-                               _DELETE(col->min);
-                               _DELETE(col->max);
-                               *min = col->min = smin;
-                               *max = col->max = smax;
-                       }
-               }
+               if (!col->min || !col->max)
+                       (void) store->storage_api.min_max_col(tr, col);
+               *min = col->min;
+               *max = col->max;
        }
        return *min != NULL && *max != NULL;
 }
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to