Changeset: b481118dcbf2 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/b481118dcbf2
Modified Files:
        sql/backends/monet5/sql_cat.c
        sql/backends/monet5/sql_statistics.c
        sql/backends/monet5/sql_statistics.h
        sql/include/sql_catalog.h
        sql/scripts/80_statistics.sql
        sql/server/rel_optimizer.c
        sql/server/rel_planner.c
        sql/storage/bat/bat_storage.c
        sql/storage/sql_storage.h
        sql/storage/store.c
Branch: analyze-fix
Log Message:

Removed 'statistics' table. With concurrent inserts/deletes is not worth to 
keep it. Retrieve min/max values from bat storage


diffs (truncated from 418 to 300 lines):

diff --git a/sql/backends/monet5/sql_cat.c b/sql/backends/monet5/sql_cat.c
--- a/sql/backends/monet5/sql_cat.c
+++ b/sql/backends/monet5/sql_cat.c
@@ -491,7 +491,6 @@ alter_table_set_access(mvc *sql, char *s
 {
        sql_schema *s = NULL;
        sql_table *t = NULL;
-       str msg = MAL_SUCCEED;
 
        if (!(s = mvc_bind_schema(sql, sname)))
                throw(SQL,"sql.alter_table_set_access",SQLSTATE(3F000) "ALTER 
TABLE: no such schema '%s'", sname);
@@ -514,8 +513,6 @@ alter_table_set_access(mvc *sql, char *s
                        default:
                                break;
                }
-               if (access == 0 && (msg = sql_drop_statistics(sql, t)))
-                       return msg;
        }
        return MAL_SUCCEED;
 }
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
@@ -22,57 +22,6 @@ analysis by optimizers.
 #include "sql_execute.h"
 
 str
-sql_drop_statistics(mvc *m, sql_table *t)
-{
-       node *ncol;
-       sql_trans *tr;
-       sql_schema *sys;
-       sql_table *sysstats;
-       sql_column *statsid;
-       oid rid;
-       int log_res = LOG_OK;
-
-       tr = m->session->tr;
-       sys = mvc_bind_schema(m, "sys");
-       if (sys == NULL)
-               throw(SQL, "sql_drop_statistics", SQLSTATE(3F000) "Internal 
error: No schema sys");
-       sysstats = mvc_bind_table(m, sys, "statistics");
-       if (sysstats == NULL)
-               throw(SQL, "sql_drop_statistics", SQLSTATE(3F000) "No table 
sys.statistics");
-       statsid = mvc_bind_column(m, sysstats, "column_id");
-       if (statsid == NULL)
-               throw(SQL, "sql_drop_statistics", SQLSTATE(3F000) "No table 
sys.statistics");
-
-       /* Do all the validations before any drop */
-       if (!isTable(t))
-               throw(SQL, "sql_drop_statistics", SQLSTATE(42S02) "DROP 
STATISTICS: %s '%s' is not persistent", TABLE_TYPE_DESCRIPTION(t->type, 
t->properties), t->base.name);
-       if (!table_privs(m, t, PRIV_SELECT))
-               throw(SQL, "sql_drop_statistics", SQLSTATE(42000) "DROP 
STATISTICS: access denied for %s to table '%s.%s'",
-                         get_string_global_var(m, "current_user"), 
t->s->base.name, t->base.name);
-       if (isTable(t) && ol_first_node(t->columns)) {
-               for (ncol = ol_first_node((t)->columns); ncol; ncol = 
ncol->next) {
-                       sql_column *c = (sql_column *) ncol->data;
-
-                       if (!column_privs(m, c, PRIV_SELECT))
-                               throw(SQL, "sql_drop_statistics", 
SQLSTATE(42000) "DROP STATISTICS: access denied for %s to column '%s' on table 
'%s.%s'",
-                                         get_string_global_var(m, 
"current_user"), c->base.name, t->s->base.name, t->base.name);
-               }
-       }
-
-       sqlstore *store = tr->store;
-       if (isTable(t) && ol_first_node(t->columns)) {
-               for (ncol = ol_first_node((t)->columns); ncol; ncol = 
ncol->next) {
-                       sql_column *c = ncol->data;
-
-                       rid = store->table_api.column_find_row(tr, statsid, 
&c->base.id, NULL);
-                       if (!is_oid_nil(rid) && (log_res = 
store->table_api.table_delete(tr, sysstats, rid)) != LOG_OK)
-                               throw(SQL, "sql.sql_drop_statistics", 
SQLSTATE(42000) "DROP STATISTICS: failed%s", log_res == LOG_CONFLICT ? " due to 
conflict with another transaction" : "");
-               }
-       }
-       return MAL_SUCCEED;
-}
-
-str
 sql_analyze(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
 {
        mvc *m = NULL;
diff --git a/sql/backends/monet5/sql_statistics.h 
b/sql/backends/monet5/sql_statistics.h
--- a/sql/backends/monet5/sql_statistics.h
+++ b/sql/backends/monet5/sql_statistics.h
@@ -13,6 +13,5 @@
 #include "sql.h"
 
 extern str sql_analyze(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr 
pci);
-extern str sql_drop_statistics(mvc *m, sql_table *t);
 
 #endif /* _SQL_STATISTICS_DEF */
diff --git a/sql/include/sql_catalog.h b/sql/include/sql_catalog.h
--- a/sql/include/sql_catalog.h
+++ b/sql/include/sql_catalog.h
@@ -619,8 +619,10 @@ typedef struct sql_column {
        char *storage_type;
        int sorted;             /* for DECLARED (dupped tables) we keep order 
info */
        size_t dcount;
-       char *min;
-       char *max;
+       void *min;
+       size_t minlen;
+       void *max;
+       size_t maxlen;
 
        struct sql_table *t;
        ATOMIC_PTR_TYPE data;
diff --git a/sql/scripts/80_statistics.sql b/sql/scripts/80_statistics.sql
--- a/sql/scripts/80_statistics.sql
+++ b/sql/scripts/80_statistics.sql
@@ -8,21 +8,6 @@
 -- This script gives the database administrator insight in the actual
 -- value distribution over all tables in the database.
 
-
-CREATE TABLE sys.statistics(
-       "column_id" integer,
-       "type" string,
-       width integer,
-       stamp timestamp,
-       "sample" bigint,
-       "count" bigint,
-       "unique" bigint,
-       "nils" bigint,
-       minval string,
-       maxval string,
-       sorted boolean,
-       revsorted boolean);
-
 create procedure sys.analyze(minmax int, "sample" bigint)
 external name sql.analyze;
 
diff --git a/sql/server/rel_optimizer.c b/sql/server/rel_optimizer.c
--- a/sql/server/rel_optimizer.c
+++ b/sql/server/rel_optimizer.c
@@ -8698,10 +8698,10 @@ merge_table_prune_and_unionize(visitor *
                                                if (!skip && pt->access == 
TABLE_READONLY) {
                                                        /* check if the part 
falls within the bounds of the select expression else skip this (keep at least 
on part-table) */
                                                        if (!cmin && !cmax && 
first_attempt) {
-                                                               char *min = 
NULL, *max = NULL;
+                                                               void *min = 
NULL, *max = NULL;
                                                                if 
(sql_trans_ranges(v->sql->session->tr, col, &min, &max) && min && max) {
-                                                                       cmin = 
atom_general(v->sql->sa, &col->type, min);
-                                                                       cmax = 
atom_general(v->sql->sa, &col->type, max);
+                                                                       cmin = 
atom_general_ptr(v->sql->sa, &col->type, min);
+                                                                       cmax = 
atom_general_ptr(v->sql->sa, &col->type, max);
                                                                }
                                                                first_attempt = 
false; /* no more attempts to read from storage */
                                                        }
@@ -9267,7 +9267,7 @@ exp_is_zero_rows(visitor *v, sql_rel *re
                                        if (lval && hval) {
                                                sql_rel *bt;
                                                sql_column *col = 
name_find_column(sel, exp_relname(c), exp_name(c), -2, &bt);
-                                               char *min = NULL, *max = NULL;
+                                               void *min = NULL, *max = NULL;
                                                atom *amin, *amax;
                                                sql_subtype *ct = 
exp_subtype(c);
 
@@ -9275,7 +9275,7 @@ exp_is_zero_rows(visitor *v, sql_rel *re
                                                        && col->t == t
                                                        && 
sql_trans_ranges(v->sql->session->tr, col, &min, &max)
                                                        && min && max
-                                                       && (amin = 
atom_general(v->sql->sa, ct, min)) && (amax = atom_general(v->sql->sa, ct, max))
+                                                       && (amin = 
atom_general_ptr(v->sql->sa, ct, min)) && (amax = atom_general_ptr(v->sql->sa, 
ct, max))
                                                        && 
!exp_range_overlap(amin, amax, lval, hval, false, false)) {
                                                        return 1;
                                                }
diff --git a/sql/server/rel_planner.c b/sql/server/rel_planner.c
--- a/sql/server/rel_planner.c
+++ b/sql/server/rel_planner.c
@@ -195,7 +195,7 @@ exp_getdcount( mvc *sql, sql_rel *r , sq
 }
 
 static int
-exp_getranges( mvc *sql, sql_rel *r , sql_exp *e, char **min, char **max)
+exp_getranges( mvc *sql, sql_rel *r , sql_exp *e, void **min, void **max)
 {
        switch(e->type) {
        case e_column: {
@@ -247,15 +247,15 @@ exp_getatom( mvc *sql, sql_exp *e, atom 
 }
 
 static dbl
-exp_getrange_sel( mvc *sql, sql_rel *r, sql_exp *e, char *min, char *max)
+exp_getrange_sel( mvc *sql, sql_rel *r, sql_exp *e, void *min, void *max)
 {
        atom *amin, *amax, *emin, *emax;
        dbl sel = 1.0;
        sql_subtype *t = exp_subtype(e->l);
 
        (void)r;
-       emin = amin = atom_general(sql->sa, t, min);
-       emax = amax = atom_general(sql->sa, t, max);
+       emin = amin = atom_general_ptr(sql->sa, t, min);
+       emax = amax = atom_general_ptr(sql->sa, t, max);
 
        if (e->f || e->flag == cmp_gt || e->flag == cmp_gte)
                emin = exp_getatom(sql, e->r, amin);
@@ -304,7 +304,7 @@ rel_exp_selectivity(mvc *sql, sql_rel *r
                case cmp_gte:
                case cmp_lt:
                case cmp_lte: {
-                       char *min, *max;
+                       void *min, *max;
                        if (exp_getranges( sql, r, e->l, &min, &max )) {
                                sel = (dbl)exp_getrange_sel( sql, r, e, min, 
max);
                        } else {
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
@@ -2314,6 +2314,47 @@ dcount_col(sql_trans *tr, sql_column *c)
        return cnt;
 }
 
+static int
+min_max_col(sql_trans *tr, sql_column *col, size_t *minlen, void **min, size_t 
*maxlen, void **max)
+{
+       int ok = 0;
+
+       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) {
+                       MT_lock_set(&b->theaplock);
+                       if (b->tminpos != BUN_NONE && b->tmaxpos != BUN_NONE && 
(fb = bind_col(tr, col, RDONLY))) {
+                               BATiter bi = bat_iterator_nolock(fb);
+                               void *nmin = BUNtail(bi, b->tminpos), *nmax = 
BUNtail(bi, b->tmaxpos);
+
+                               *minlen = ATOMlen(b->ttype, nmin);
+                               *maxlen = ATOMlen(b->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;
+                               }
+                               BBPunfix(fb->batCacheid);
+                       }
+                       MT_lock_unset(&b->theaplock);
+               }
+       }
+       return ok;
+}
+
 static size_t
 count_segs(segment *s)
 {
@@ -4440,6 +4481,7 @@ bat_storage_init( store_functions *sf)
        sf->count_col = &count_col;
        sf->count_idx = &count_idx;
        sf->dcount_col = &dcount_col;
+       sf->min_max_col = &min_max_col;
        sf->sorted_col = &sorted_col;
        sf->unique_col = &unique_col;
        sf->double_elim_col = &double_elim_col;
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,6 +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 (*prop_col_fptr) (sql_trans *tr, sql_column *c);
 
 /*
@@ -234,6 +235,7 @@ typedef struct store_functions {
        count_col_fptr count_col;
        count_idx_fptr count_idx;
        dcount_col_fptr dcount_col;
+       min_max_col_fptr min_max_col;
        prop_col_fptr sorted_col;
        prop_col_fptr unique_col;
        prop_col_fptr double_elim_col; /* varsize col with double elimination */
@@ -393,7 +395,7 @@ extern int sql_trans_is_sorted(sql_trans
 extern int sql_trans_is_unique(sql_trans *tr, sql_column *col);
 extern int sql_trans_is_duplicate_eliminated(sql_trans *tr, sql_column *col);
 extern size_t sql_trans_dist_count(sql_trans *tr, sql_column *col);
-extern int sql_trans_ranges(sql_trans *tr, sql_column *col, char **min, char 
**max);
+extern int sql_trans_ranges(sql_trans *tr, sql_column *col, void **min, void 
**max);
 
 extern void column_destroy(struct sqlstore *store, sql_column *c);
 extern void idx_destroy(struct sqlstore *store, sql_idx * i);
diff --git a/sql/storage/store.c b/sql/storage/store.c
--- a/sql/storage/store.c
+++ b/sql/storage/store.c
@@ -4311,27 +4311,6 @@ sys_drop_sequence(sql_trans *tr, sql_seq
 }
 
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to