Changeset: 6b53a5405943 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=6b53a5405943
Modified Files:
sql/server/rel_prop.c
sql/server/rel_statistics.c
sql/server/rel_statistics.h
sql/server/rel_statistics_functions.c
sql/server/sql_atom.c
sql/server/sql_atom.h
Branch: properties
Log Message:
Use sql atoms instead of gdk values in order to deal with sql types such as
decimals easier
diffs (truncated from 710 to 300 lines):
diff --git a/sql/server/rel_prop.c b/sql/server/rel_prop.c
--- a/sql/server/rel_prop.c
+++ b/sql/server/rel_prop.c
@@ -9,6 +9,7 @@
#include "monetdb_config.h"
#include "sql_relation.h"
#include "rel_prop.h"
+#include "sql_atom.h"
prop *
prop_create( sql_allocator *sa, rel_prop kind, prop *pre )
@@ -68,6 +69,14 @@ find_prop_and_get(prop *p, rel_prop kind
return found ? found->value : NULL;
}
+#ifdef MIN
+#undef MIN
+#endif
+
+#ifdef MAX
+#undef MAX
+#endif
+
const char *
propkind2string( prop *p)
{
@@ -83,8 +92,8 @@ propkind2string( prop *p)
PT(USED);
PT(DISTRIBUTE);
PT(GROUPINGS);
- case PROP_MIN: return "MIN";
- case PROP_MAX: return "MAX";
+ PT(MIN);
+ PT(MAX);
}
return "UNKNOWN";
}
@@ -109,24 +118,17 @@ propvalue2string(sql_allocator *sa, prop
}
case PROP_MIN:
case PROP_MAX: {
- ValPtr ptr = p->value;
-
- if (VALisnil(ptr)) {
- return sa_strdup(sa, "NULL");
- } else {
- char *s = ATOMformat(ptr->vtype, VALptr(ptr)),
*res = NULL;
+ atom *a = p->value;
+ char *s = atom2sql(sa, a, 0), *res = NULL;
- if (s && *s == '"') {
- res = sa_strdup(sa, s);
- } else if (s) {
- res = sa_alloc(sa, strlen(s) + 3);
- stpcpy(stpcpy(stpcpy(res, "\""), s),
"\"");
- } else { /* shouldn't happen */
- res = sa_strdup(sa, "");
- }
- GDKfree(s);
- return res;
+ assert(s);
+ if (*s == '"') {
+ res = sa_strdup(sa, s);
+ } else {
+ res = sa_alloc(sa, strlen(s) + 3);
+ stpcpy(stpcpy(stpcpy(res, "\""), s), "\"");
}
+ return res;
}
default:
break;
diff --git a/sql/server/rel_statistics.c b/sql/server/rel_statistics.c
--- a/sql/server/rel_statistics.c
+++ b/sql/server/rel_statistics.c
@@ -42,7 +42,7 @@ comparison_find_column(sql_exp *input, s
static sql_exp *
rel_propagate_column_ref_statistics(mvc *sql, sql_rel *rel, sql_exp *e)
{
- ValPtr lval, rval;
+ atom *lval, *rval;
sql_exp *ne = NULL;
assert(e->type == e_column);
@@ -220,11 +220,11 @@ rel_basetable_get_statistics(visitor *v,
if ((EC_NUMBER(c->type.type->eclass) ||
EC_TEMP_NOFRAC(c->type.type->eclass) || c->type.type->eclass == EC_DATE) &&
(max = mvc_has_max_value(sql, c))) {
prop *p = e->p = prop_create(sql->sa, PROP_MAX, e->p);
- p->value = max;
+ p->value = atom_general_ptr(sql->sa, &c->type, max);
}
if ((EC_NUMBER(c->type.type->eclass) ||
EC_TEMP_NOFRAC(c->type.type->eclass) || c->type.type->eclass == EC_DATE) &&
(min = mvc_has_min_value(sql, c))) {
prop *p = e->p = prop_create(sql->sa, PROP_MIN, e->p);
- p->value = min;
+ p->value = atom_general_ptr(sql->sa, &c->type, min);
}
}
return e;
@@ -235,7 +235,7 @@ rel_set_get_statistics(mvc *sql, sql_rel
{
sql_exp *le = list_fetch(((sql_rel*)(rel->l))->exps, i);
sql_exp *re = list_fetch(((sql_rel*)(rel->r))->exps, i);
- ValPtr lval, rval;
+ atom *lval, *rval;
assert(le && e);
if ((lval = find_prop_and_get(le->p, PROP_MAX)) && (rval =
find_prop_and_get(re->p, PROP_MAX))) {
@@ -260,7 +260,7 @@ static sql_exp *
rel_propagate_statistics(visitor *v, sql_rel *rel, sql_exp *e, int depth)
{
mvc *sql = v->sql;
- ValPtr lval;
+ atom *lval;
sql_subtype *tp = exp_subtype(e);
(void) depth;
@@ -298,9 +298,8 @@ rel_propagate_statistics(visitor *v, sql
sql_exp *l = e->l;
if ((lval = find_prop_and_get(l->p, PROP_MAX)))
{
if (EC_NUMBER(from->type->eclass)) {
- ValPtr res = (ValPtr)
sa_zalloc(sql->sa, sizeof(ValRecord));
- VALcopy(res, lval);
- if
(VALconvert(to->type->localtype, res))
+ atom *res = atom_dup(sql->sa,
lval);
+ if (atom_cast(sql->sa, res, to))
set_property(sql, e,
PROP_MAX, res);
} else {
set_property(sql, e, PROP_MAX,
lval);
@@ -308,9 +307,8 @@ rel_propagate_statistics(visitor *v, sql
}
if ((lval = find_prop_and_get(l->p, PROP_MIN)))
{
if (EC_NUMBER(from->type->eclass)) {
- ValPtr res = (ValPtr)
sa_zalloc(sql->sa, sizeof(ValRecord));
- VALcopy(res, lval);
- if
(VALconvert(to->type->localtype, res))
+ atom *res = atom_dup(sql->sa,
lval);
+ if (atom_cast(sql->sa, res, to))
set_property(sql, e,
PROP_MIN, res);
} else {
set_property(sql, e, PROP_MIN,
lval);
@@ -341,35 +339,32 @@ rel_propagate_statistics(visitor *v, sql
if (e->l) {
atom *a = (atom*) e->l;
if (!a->isnull) {
- set_property(sql, e, PROP_MAX,
&a->data);
- set_property(sql, e, PROP_MIN,
&a->data);
+ set_property(sql, e, PROP_MAX, a);
+ set_property(sql, e, PROP_MIN, a);
}
} else if (e->f) {
list *vals = (list *) e->f;
sql_exp *first = vals->h ? vals->h->data : NULL;
- ValPtr max = NULL, min = NULL; /* all child
values must have a valid min/max */
+ atom *max = NULL, *min = NULL; /* all child
values must have a valid min/max */
if (first) {
max = ((lval =
find_prop_and_get(first->p, PROP_MAX))) ? lval : NULL;
min = ((lval =
find_prop_and_get(first->p, PROP_MIN))) ? lval : NULL;
}
- for (node *n = vals->h ? vals->h->next : NULL ;
n && min && max; n = n->next) {
+ for (node *n = vals->h ? vals->h->next : NULL ;
n ; n = n->next) {
sql_exp *ee = n->data;
- ValRecord res;
if (max) {
if ((lval =
find_prop_and_get(ee->p, PROP_MAX))) {
- VARcalcgt(&res, lval,
max);
- max = res.val.btval ==
1 ? lval : max;
+ max = atom_cmp(lval,
max) > 0 ? lval : max;
} else {
max = NULL;
}
}
if (min) {
if ((lval =
find_prop_and_get(ee->p, PROP_MIN))) {
- VARcalcgt(&res, min,
lval);
- min = res.val.btval ==
1 ? lval : min;
+ min = atom_cmp(min,
lval) > 0 ? lval : min;
} else {
min = NULL;
}
diff --git a/sql/server/rel_statistics.h b/sql/server/rel_statistics.h
--- a/sql/server/rel_statistics.h
+++ b/sql/server/rel_statistics.h
@@ -23,29 +23,21 @@ struct function_properties {
};
static inline void
-set_max_of_values(mvc *sql, sql_exp *e, rel_prop kind, ValPtr lval, ValPtr
rval)
+set_max_of_values(mvc *sql, sql_exp *e, rel_prop kind, atom *lval, atom *rval)
{
- prop *p;
- ValRecord res;
-
- VARcalcgt(&res, lval, rval);
- p = e->p = prop_create(sql->sa, kind, e->p);
- p->value = res.val.btval == 1 ? lval : rval;
+ prop *p = e->p = prop_create(sql->sa, kind, e->p);
+ p->value = atom_cmp(lval, rval) > 0 ? lval : rval;
}
static inline void
-set_min_of_values(mvc *sql, sql_exp *e, rel_prop kind, ValPtr lval, ValPtr
rval)
+set_min_of_values(mvc *sql, sql_exp *e, rel_prop kind, atom *lval, atom *rval)
{
- prop *p;
- ValRecord res;
-
- VARcalcgt(&res, lval, rval);
- p = e->p = prop_create(sql->sa, kind, e->p);
- p->value = res.val.btval == 1 ? rval : lval;
+ prop *p = e->p = prop_create(sql->sa, kind, e->p);
+ p->value = atom_cmp(lval, rval) > 0 ? rval : lval;
}
static inline void
-set_property(mvc *sql, sql_exp *e, rel_prop kind, ValPtr val)
+set_property(mvc *sql, sql_exp *e, rel_prop kind, atom *val)
{
prop *p = e->p = prop_create(sql->sa, kind, e->p);
p->value = val;
diff --git a/sql/server/rel_statistics_functions.c
b/sql/server/rel_statistics_functions.c
--- a/sql/server/rel_statistics_functions.c
+++ b/sql/server/rel_statistics_functions.c
@@ -23,25 +23,17 @@ sql_add_propagate_statistics(mvc *sql, s
{
list *l = e->l;
sql_exp *first = l->h->data, *second = l->h->next->data;
- ValPtr lval, rval;
+ atom *lval, *rval;
if ((lval = find_prop_and_get(first->p, PROP_MAX)) && (rval =
find_prop_and_get(second->p, PROP_MAX))) {
- ValPtr res = (ValPtr) sa_zalloc(sql->sa, sizeof(ValRecord));
- res->vtype = lval->vtype > rval->vtype ? lval->vtype :
rval->vtype;
- if (VARcalcadd(res, lval, rval, true) == GDK_SUCCEED) {
+ atom *res = atom_add(atom_dup(sql->sa, lval), rval);
+ if (res)
set_property(sql, e, PROP_MAX, res);
- } else {
- GDKclrerr(); /* if the min/max pair overflows, then
don't propagate */
- }
}
if ((lval = find_prop_and_get(first->p, PROP_MIN)) && (rval =
find_prop_and_get(second->p, PROP_MIN))) {
- ValPtr res = (ValPtr) sa_zalloc(sql->sa, sizeof(ValRecord));
- res->vtype = lval->vtype > rval->vtype ? lval->vtype :
rval->vtype;
- if (VARcalcadd(res, lval, rval, true) == GDK_SUCCEED) {
+ atom *res = atom_add(atom_dup(sql->sa, lval), rval);
+ if (res)
set_property(sql, e, PROP_MIN, res);
- } else {
- GDKclrerr();
- }
}
}
@@ -51,32 +43,21 @@ sql_##FUNC##_propagate_statistics(mvc *s
{ \
list *l = e->l; \
sql_exp *first = l->h->data, *second = l->h->next->data; \
- ValPtr lmax, rmax, lmin, rmin; \
+ atom *lmax, *rmax, *lmin, *rmin; \
\
if ((lmax = find_prop_and_get(first->p, PROP_MAX)) && (rmax =
find_prop_and_get(second->p, PROP_MAX)) && \
(lmin = find_prop_and_get(first->p, PROP_MIN)) && (rmin =
find_prop_and_get(second->p, PROP_MIN))) { \
- ValPtr res1 = (ValPtr) sa_zalloc(sql->sa, sizeof(ValRecord)),
res2 = (ValPtr) sa_zalloc(sql->sa, sizeof(ValRecord)); \
- gdk_return code1, code2; \
- \
- res1->vtype = res2->vtype = lmax->vtype > rmax->vtype ?
lmax->vtype : rmax->vtype; \
- code1 = VARcalc##FUNC(res1, lmax, rmax, true); \
- GDKclrerr(); \
- code2 = VARcalc##FUNC(res2, lmin, rmin, true); \
- GDKclrerr(); \
+ atom *res1 = atom_##FUNC(atom_dup(sql->sa, lmax), rmax); \
+ atom *res2 = atom_##FUNC(atom_dup(sql->sa, lmin), rmin); \
\
- if (code1 == GDK_SUCCEED && code2 == GDK_SUCCEED) { /* if the
min/max pair overflows, then don't propagate */ \
- ValRecord zero1 = (ValRecord) {.vtype = lmax->vtype,},
zero2 = (ValRecord) {.vtype = rmax->vtype,}, zero3 = (ValRecord) {.vtype =
lmin->vtype,}, \
- zero4 = (ValRecord) {.vtype =
rmin->vtype,}, verify1, verify2, verify3, verify4; \
+ if (res1 && res2) { /* if the min/max pair overflows, then
don't propagate */ \
+ atom *zero1 = atom_zero_value(sql->sa, &(lmax->tpe)),
*zero2 = atom_zero_value(sql->sa, &(rmax->tpe)); \
+ int cmp1 = atom_cmp(lmax, zero1), cmp2 = atom_cmp(lmin,
zero1), cmp3 = atom_cmp(rmin, zero2), cmp4 = atom_cmp(rmax, zero2); \
\
- VARcalcge(&verify1, lmax, &zero1); \
- VARcalcge(&verify2, rmax, &zero2); \
- VARcalcge(&verify3, lmin, &zero3); \
- VARcalcge(&verify4, rmin, &zero4); \
- \
- if (verify1.val.btval == 1 && verify2.val.btval == 1 &&
verify3.val.btval == 1 && verify4.val.btval == 1) { /* if all positive then
propagate */ \
+ if (cmp1 >= 0 && cmp2 >= 0 && cmp3 >= 0 && cmp4 >= 0) {
/* if all positive then propagate */ \
set_property(sql, e, PROP_MAX, res1); \
set_property(sql, e, PROP_MIN, res2); \
- } else if (verify1.val.btval == 0 && verify2.val.btval
== 0 && verify3.val.btval == 0 && verify4.val.btval == 0) { /* if all negative
propagate by swapping min and max */ \
+ } else if (cmp1 < 0 && cmp2 < 0 && cmp3 < 0 && cmp4 <
0) { /* if all negative propagate by swapping min and max */ \
set_property(sql, e, PROP_MAX, res2); \
set_property(sql, e, PROP_MIN, res1); \
} \
@@ -92,32 +73,21 @@ sql_div_propagate_statistics(mvc *sql, s
{
list *l = e->l;
sql_exp *first = l->h->data, *second = l->h->next->data;
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list