Changeset: f05f3ecc962f for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/f05f3ecc962f
Modified Files:
sql/server/rel_exp.c
Branch: default
Log Message:
added range selectivity estimation
diffs (179 lines):
diff --git a/sql/server/rel_exp.c b/sql/server/rel_exp.c
--- a/sql/server/rel_exp.c
+++ b/sql/server/rel_exp.c
@@ -4332,11 +4332,156 @@ free_exp(allocator *sa, sql_exp *e)
_free_exp_internal(sa, e);
}
+static atom *
+range_stat_atom(prop *p)
+{
+ return p ? (atom *) p->value.pval : NULL;
+}
+
+static sql_exp *
+range_stat_exp(sql_exp *e)
+{
+ while (e && e->type == e_convert) {
+ if (find_prop(e->p, PROP_MIN) && find_prop(e->p, PROP_MAX))
+ return e;
+ e = (sql_exp *) e->l;
+ }
+ return e;
+}
+
+static atom *
+range_bound_atom(mvc *sql, sql_exp *e)
+{
+ sql_exp *outer = e;
+ atom *a;
+
+ while (e && e->type == e_convert)
+ e = (sql_exp *) e->l;
+ if (!e || e->type != e_atom || !(a = (atom *) e->l))
+ return NULL;
+ if (outer->type == e_convert) {
+ a = atom_copy(sql->sa, a);
+ if (!a || !(a = atom_cast(sql->sa, a, &outer->tpe)))
+ return NULL;
+ }
+ return a;
+}
+
+static bool
+range_atom_to_dbl(mvc *sql, atom *a, dbl *v)
+{
+ sql_subtype *dbltype;
+ atom *d;
+
+ if (!a || a->isnull)
+ return false;
+ if (a->tpe.type->eclass == EC_DEC) {
+ /* decimals are integer-encoded with a scale, let atom_cast
+ * apply the scale before converting to a double */
+ dbltype = sql_fetch_localtype(TYPE_dbl);
+ if (!dbltype)
+ return false;
+ d = atom_cast(sql->sa, atom_copy(sql->sa, a), dbltype);
+ if (!d || d->isnull)
+ return false;
+ *v = d->data.val.dval;
+ return true;
+ }
+ switch (ATOMstorage(a->data.vtype)) {
+ case TYPE_bte:
+ *v = (dbl) a->data.val.btval;
+ return true;
+ case TYPE_sht:
+ *v = (dbl) a->data.val.shval;
+ return true;
+ case TYPE_int: /* includes TYPE_date */
+ *v = (dbl) a->data.val.ival;
+ return true;
+ case TYPE_oid:
+ *v = (dbl) a->data.val.oval;
+ return true;
+ case TYPE_lng: /* includes TYPE_daytime and TYPE_timestamp */
+ *v = (dbl) a->data.val.lval;
+ return true;
+#ifdef HAVE_HGE
+ case TYPE_hge:
+ *v = (dbl) a->data.val.hval;
+ return true;
+#endif
+ case TYPE_flt:
+ *v = (dbl) a->data.val.fval;
+ return true;
+ case TYPE_dbl:
+ *v = (dbl) a->data.val.dval;
+ return true;
+ default:
+ return false;
+ }
+}
+
+static dbl
+exp_range_selectivity(mvc *sql, sql_exp *l, sql_exp *r, comp_type flag)
+{
+ sql_exp *col = l, *bound = r;
+ comp_type cmp = flag;
+ prop *pn;
+ atom *min, *max, *batom;
+ dbl minv, maxv, bv, span, sel;
+
+ if (exp_is_atom(l) && !exp_is_atom(r)) {
+ col = r;
+ bound = l;
+ cmp = swap_compare(flag);
+ } else if (!exp_is_atom(r) || exp_is_atom(l)) {
+ /* only column/atom comparisons can use column min/max */
+ return -1.0;
+ }
+ col = range_stat_exp(col);
+ if (!col || exp_is_atom(col))
+ return -1.0;
+ pn = find_prop(col->p, PROP_NUNIQUES);
+ if (pn && pn->value.dval < 20)
+ return -1.0;
+ min = range_stat_atom(find_prop(col->p, PROP_MIN));
+ max = range_stat_atom(find_prop(col->p, PROP_MAX));
+ batom = range_bound_atom(sql, bound);
+ if (!min || !max || !batom)
+ return -1.0;
+ if (!range_atom_to_dbl(sql, min, &minv) ||
+ !range_atom_to_dbl(sql, max, &maxv) ||
+ !range_atom_to_dbl(sql, batom, &bv))
+ return -1.0;
+ if (!isfinite(minv) || !isfinite(maxv) || !isfinite(bv) || !(maxv >
minv))
+ return -1.0;
+ span = maxv - minv;
+ if (cmp == cmp_gt || cmp == cmp_gte) {
+ if (bv <= minv)
+ sel = 1.0;
+ else if (bv >= maxv)
+ sel = 0.0;
+ else
+ sel = (maxv - bv) / span;
+ } else {
+ if (bv <= minv)
+ sel = 0.0;
+ else if (bv >= maxv)
+ sel = 1.0;
+ else
+ sel = (bv - minv) / span;
+ }
+ /* never under-estimate an uncertain range to zero, and never go below
+ * a small floor for skew we cannot see from min/max alone */
+ if (sel < 0.05)
+ sel = 0.05;
+ if (sel > 1.0)
+ sel = 1.0;
+ return sel;
+}
+
dbl
exp_estimate_selectivity(mvc *sql, sql_exp *e, sql_rel *rel)
{
dbl sel = 0.1;
- (void)sql;
if (!e)
return 1.0;
switch (e->type) {
@@ -4417,13 +4562,11 @@ exp_estimate_selectivity(mvc *sql, sql_e
case cmp_gt:
case cmp_gte:
case cmp_lt:
- case cmp_lte:
- sel = 0.50;
- /*
- if (e->f)
- sel = 0.10;
- */
+ case cmp_lte: {
+ dbl rsel = exp_range_selectivity(sql, l, r,
e->flag);
+ sel = rsel >= 0.0 ? rsel : 0.50;
break;
+ }
case cmp_in:
case cmp_notin: {
list *vals = (list *) e->r;
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]