Changeset: 397a0731b952 for MonetDB URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=397a0731b952 Modified Files: sql/backends/monet5/rel_weld.c Branch: rel-weld Log Message:
rel_weld.c code cleanup - new function to get the column name and replace non alpha-num characters - create a sql_allocator to handle our allocations - save the eventual error in the weld_state struct diffs (truncated from 601 to 300 lines): diff --git a/sql/backends/monet5/rel_weld.c b/sql/backends/monet5/rel_weld.c --- a/sql/backends/monet5/rel_weld.c +++ b/sql/backends/monet5/rel_weld.c @@ -45,6 +45,9 @@ #define STR_BUF_SIZE 4096 +#define REL 0 +#define ALIAS 1 + /* From sql_statement.c */ #define meta(Id, Tpe) \ q = newStmt(mb, batRef, newRef); \ @@ -67,21 +70,25 @@ typedef struct { unsigned long program_max_len; char str_cols[STR_BUF_SIZE * 3]; /* names the vheaps take */ list *stmt_list; + sql_allocator *sa; + int error; } weld_state; -typedef int (*produce_func)(backend*, sql_rel*, weld_state*); +typedef void (*produce_func)(backend*, sql_rel*, weld_state*); static produce_func getproduce_func(sql_rel *rel); -static int exps_to_weld(backend*, str, int*, list*, list*, str); -static int exp_to_weld(backend*, str, int*, sql_exp*, list*); +static void exps_to_weld(backend*, weld_state*, str, int*, list*, list*, str); +static void exp_to_weld(backend*, weld_state*, str, int*, sql_exp*, list*); -static void dump_program(weld_state *wstate) { +static void +dump_program(weld_state *wstate) { FILE *f = fopen(tmpnam(NULL), "w"); dumpWeldProgram(wstate->program, f); fclose(f); } -static void prepend_weld_stmt(weld_state *wstate, str weld_stmt) { +static void +prepend_weld_stmt(weld_state *wstate, str weld_stmt) { if (strlen(wstate->program) + strlen(weld_stmt) >= wstate->program_max_len) { wstate->program_max_len = strlen(wstate->program) + strlen(weld_stmt) + 1; wstate->program = realloc(wstate->program, wstate->program_max_len * sizeof(char)); @@ -90,7 +97,8 @@ static void prepend_weld_stmt(weld_state memcpy(wstate->program, weld_stmt, strlen(weld_stmt)); } -static void append_weld_stmt(weld_state *wstate, str weld_stmt) { +static void +append_weld_stmt(weld_state *wstate, str weld_stmt) { if (strlen(wstate->program) + strlen(weld_stmt) >= wstate->program_max_len) { wstate->program_max_len = strlen(wstate->program) + strlen(weld_stmt) + 1; wstate->program = realloc(wstate->program, wstate->program_max_len * sizeof(char)); @@ -98,20 +106,20 @@ static void append_weld_stmt(weld_state wstate->program = strcat(wstate->program, weld_stmt); } -static int exps_to_weld(backend *be, str weld_stmt, int *len, list *exps, list *stmt_list, str delim) { +static void +exps_to_weld(backend *be, weld_state *wstate, str weld_stmt, int *len, list *exps, + list *stmt_list, str delim) { node *en; for (en = exps->h; en; en = en->next) { - if (exp_to_weld(be, weld_stmt, len, en->data, stmt_list) != 0) { - return -1; - } - if (en->next != NULL) { + exp_to_weld(be, wstate, weld_stmt, len, en->data, stmt_list); + if (en->next != NULL && delim != NULL) { *len += sprintf(weld_stmt + *len, "%s", delim); } } - return 0; } -static str get_weld_cmp(int cmp) { +static str +get_weld_cmp(int cmp) { switch(cmp) { case cmp_gt: return ">"; case cmp_gte: return ">="; @@ -123,7 +131,8 @@ static str get_weld_cmp(int cmp) { } } -static str get_weld_func(sql_subfunc *f) { +static str +get_weld_func(sql_subfunc *f) { if (strcmp(f->func->imp, "+") == 0 || strcmp(f->func->imp, "sum") == 0 || strcmp(f->func->imp, "count") == 0) return "+"; @@ -137,8 +146,26 @@ static str get_weld_func(sql_subfunc *f) return NULL; } +static str +get_col_name(sql_allocator *sa, sql_exp *exp, int name_type) { + char col_name[256]; + size_t i; + if (name_type == REL) { + sprintf(col_name, "%s_%s", exp->l ? (str)exp->l : (str)exp->r, (str)exp->r); + } else if (name_type == ALIAS) { + sprintf(col_name, "%s_%s", exp->rname ? exp->rname : exp->name, exp->name); + } + for (i = 0; i < strlen(col_name); i++) { + if (!isalnum(col_name[i])) { + col_name[i] = '_'; + } + } + return sa_strdup(sa, col_name); +} + /* Check wether the relation return a BAT as opposed to a single value */ -static int rel_returns_bat(sql_rel *rel) { +static int +rel_returns_bat(sql_rel *rel) { switch (rel->op) { case op_project: case op_select: @@ -154,7 +181,8 @@ static int rel_returns_bat(sql_rel *rel) } } -static int exp_has_column(sql_exp *exp) { +static int +exp_has_column(sql_exp *exp) { node *en; int ret = 0; switch (exp->type) { @@ -186,17 +214,18 @@ static int exp_has_column(sql_exp *exp) /* Produce Weld code from an expression. If the expression doesn't involve a column, call `exp_bin` on it * to produce a stmt and then use the result variable in the Weld program. This way we let MonetDB evaluate * complex conversions or atom expansions. */ -static int exp_to_weld(backend *be, str weld_stmt, int *len, sql_exp *exp, list *stmt_list) { +static void +exp_to_weld(backend *be, weld_state *wstate, str weld_stmt, int *len, sql_exp *exp, list *stmt_list) { if (!exp_has_column(exp)) { stmt* sub = exp_bin(be, exp, NULL, NULL, NULL, NULL, NULL, NULL); *len += sprintf(weld_stmt + *len, "in%d", sub->nr); list_append(stmt_list, sub); - return 0; + return; } switch (exp->type) { case e_convert: { *len += sprintf(weld_stmt + *len, "%s(", getWeldType(exp->tpe.type->localtype)); - if (exp_to_weld(be, weld_stmt, len, exp->l, stmt_list) < 0) return -1; + exp_to_weld(be, wstate, weld_stmt, len, exp->l, stmt_list); *len += sprintf(weld_stmt + *len, ")"); break; } @@ -205,19 +234,25 @@ static int exp_to_weld(backend *be, str *len += sprintf(weld_stmt + *len, "("); } if (exp->f) { - if (get_weld_cmp(swap_compare(range2lcompare(exp->flag))) == NULL) return -1; - if (exp_to_weld(be, weld_stmt, len, exp->r, stmt_list) != 0) return -1; + if (get_weld_cmp(swap_compare(range2lcompare(exp->flag))) == NULL) { + wstate->error = 1; + return; + } + exp_to_weld(be, wstate, weld_stmt, len, exp->r, stmt_list); *len += sprintf(weld_stmt + *len, " %s ", get_weld_cmp(swap_compare(range2lcompare(exp->flag)))); - if (exp_to_weld(be, weld_stmt, len, exp->l, stmt_list) != 0) return -1; + exp_to_weld(be, wstate, weld_stmt, len, exp->l, stmt_list); *len += sprintf(weld_stmt + *len, " && "); - if (exp_to_weld(be, weld_stmt, len, exp->f, stmt_list) != 0) return -1; + exp_to_weld(be, wstate, weld_stmt, len, exp->f, stmt_list); *len += sprintf(weld_stmt + *len, " %s ", get_weld_cmp(range2lcompare(exp->flag))); - if (exp_to_weld(be, weld_stmt, len, exp->l, stmt_list) != 0) return -1; + exp_to_weld(be, wstate, weld_stmt, len, exp->l, stmt_list); } else { - if (get_weld_cmp(get_cmp(exp)) == NULL) return -1; - if (exp_to_weld(be, weld_stmt, len, exp->l, stmt_list) != 0) return -1; + if (get_weld_cmp(get_cmp(exp)) == NULL) { + wstate->error = 1; + return; + } + exp_to_weld(be, wstate, weld_stmt, len, exp->l, stmt_list); *len += sprintf(weld_stmt + *len, " %s ", get_weld_cmp(get_cmp(exp))); - if (exp_to_weld(be, weld_stmt, len, exp->r, stmt_list) != 0) return -1; + exp_to_weld(be, wstate, weld_stmt, len, exp->r, stmt_list); } if (is_anti(exp)) { *len += sprintf(weld_stmt + *len, ") == false "); @@ -225,9 +260,7 @@ static int exp_to_weld(backend *be, str break; } case e_column: { - char col_name[256]; - sprintf(col_name, "%s_%s", exp->l ? (str)exp->l : (str)exp->r, (str)exp->r); - *len += sprintf(weld_stmt + *len, "%s", col_name); + *len += sprintf(weld_stmt + *len, "%s", get_col_name(wstate->sa, exp, REL)); break; } case e_func: { @@ -246,7 +279,7 @@ static int exp_to_weld(backend *be, str /* Left operand */ if (left_type < right_type) *len += sprintf(weld_stmt + *len, "%s(", getWeldType(right_type)); - if (exp_to_weld(be, weld_stmt, len, left, stmt_list) != 0) return -1; + exp_to_weld(be, wstate, weld_stmt, len, left, stmt_list); if (left_type < right_type) *len += sprintf(weld_stmt + *len, ")"); /* Operator */ @@ -254,40 +287,40 @@ static int exp_to_weld(backend *be, str /* Right operand */ if (right_type < left_type) *len += sprintf(weld_stmt + *len, "%s(", getWeldType(left_type)); - if (exp_to_weld(be, weld_stmt, len, right, stmt_list) != 0) return -1; + exp_to_weld(be, wstate, weld_stmt, len, right, stmt_list); if (right_type < left_type) *len += sprintf(weld_stmt + *len, ")"); } else { *len += sprintf(weld_stmt + *len, "%s(", weld_func); - if (exps_to_weld(be, weld_stmt, len, exp->l, stmt_list, ", ") != 0) return -1; + exps_to_weld(be, wstate, weld_stmt, len, exp->l, stmt_list, ", "); *len += sprintf(weld_stmt + *len, ")"); } break; } case e_aggr: { if (exp->l) { - if (exps_to_weld(be, weld_stmt, len, exp->l, stmt_list, NULL) != 0) return -1; + exps_to_weld(be, wstate, weld_stmt, len, exp->l, stmt_list, NULL); } else { if (strcmp(((sql_subfunc*)exp->f)->func->imp, "count") == 0) { *len += sprintf(weld_stmt + *len, "1L"); } else { - return -1; + wstate->error = 1; + return; } } break; } - default: return -1; + default: wstate->error = 1; } - return 0; } -static int +static void base_table_produce(backend *be, sql_rel *rel, weld_state *wstate) { stmt *sub = subrel_bin(be, rel, NULL); node *en; sql_exp *exp; - char weld_stmt[STR_BUF_SIZE], col_name[256], iter_idx[64]; + char weld_stmt[STR_BUF_SIZE], iter_idx[64]; int count; int len = sprintf(weld_stmt, "for(zip("); for (en = rel->exps->h; en; en = en->next) { @@ -299,11 +332,10 @@ base_table_produce(backend *be, sql_rel } if (exp_subtype(exp)->type->localtype == TYPE_str) { /* Save the vheap and stroffset names */ - sprintf(col_name, "%s_%s", (str)exp->l, (str)exp->r); sprintf(wstate->str_cols + strlen(wstate->str_cols), "let %s_strcol = in%dstr;", - col_name, col->nr); + get_col_name(wstate->sa, exp, REL), col->nr); sprintf(wstate->str_cols + strlen(wstate->str_cols), "let %s_stroffset = in%dstroffset;", - col_name, col->nr); + get_col_name(wstate->sa, exp, REL), col->nr); } } /* builder and function header */ @@ -313,7 +345,7 @@ base_table_produce(backend *be, sql_rel /* extract named values from the tuple */ for (en = rel->exps->h, count = 0; en; en = en->next, count++) { exp = en->data; - sprintf(col_name, "%s_%s", (str)exp->l, (str)exp->r); + str col_name = get_col_name(wstate->sa, exp, REL); if (rel->exps->h->next == NULL) { /* just a single column so n.$0 doesn't work */ sprintf(iter_idx, "n%d", wstate->num_loops); @@ -331,16 +363,18 @@ base_table_produce(backend *be, sql_rel ++wstate->num_parens; append_weld_stmt(wstate, weld_stmt); list_append(wstate->stmt_list, sub); - return 0; } -static int +static void select_produce(backend *be, sql_rel *rel, weld_state *wstate) { /* === Produce === */ produce_func input_produce = getproduce_func(rel->l); - if (input_produce == NULL) return -1; - if (input_produce(be, rel->l, wstate) != 0) return -1; + if (input_produce == NULL) { + wstate->error = 1; + return; + } + input_produce(be, rel->l, wstate); /* === Consume === */ _______________________________________________ checkin-list mailing list checkin-list@monetdb.org https://www.monetdb.org/mailman/listinfo/checkin-list