diff --git a/src/lang/nfo.c b/src/lang/nfo.c index 7148b79c0..2aea01b13 100644 --- a/src/lang/nfo.c +++ b/src/lang/nfo.c @@ -26,23 +26,21 @@ ray_t* ray_nfo_create(const char* filename, size_t fname_len, const char* source, size_t src_len) { + /* A part that fails to allocate: no nfo (NULL), the parts made freed. + * An error object here would be kept by callers, which take the nfo as + * optional and release it (a no-op on an error), and read as a list. */ ray_t* fname = ray_str(filename, fname_len); - if (RAY_IS_ERR(fname)) return fname; - - ray_t* src = ray_str(source, src_len); - if (RAY_IS_ERR(src)) { ray_release(fname); return src; } - - ray_t* keys = ray_vec_new(RAY_I64, 0); - if (RAY_IS_ERR(keys)) { ray_release(fname); ray_release(src); return keys; } - - ray_t* vals = ray_vec_new(RAY_I64, 0); - if (RAY_IS_ERR(vals)) { ray_release(fname); ray_release(src); ray_release(keys); return vals; } - - /* Build the nfo list: alloc 4-slot list, set elements directly. */ - ray_t* nfo = ray_alloc(4 * sizeof(ray_t*)); + ray_t* src = fname && !RAY_IS_ERR(fname) ? ray_str(source, src_len) : NULL; + ray_t* keys = src && !RAY_IS_ERR(src) ? ray_vec_new(RAY_I64, 0) : NULL; + ray_t* vals = keys && !RAY_IS_ERR(keys) ? ray_vec_new(RAY_I64, 0) : NULL; + ray_t* nfo = vals && !RAY_IS_ERR(vals) ? ray_alloc(4 * sizeof(ray_t*)) : NULL; if (!nfo || RAY_IS_ERR(nfo)) { - ray_release(fname); ray_release(src); ray_release(keys); ray_release(vals); - return ray_error("oom", NULL); + ray_t* parts[5] = { fname, src, keys, vals, nfo }; + for (int i = 0; i < 5; i++) { + if (parts[i] && RAY_IS_ERR(parts[i])) ray_error_free(parts[i]); + else if (parts[i]) ray_release(parts[i]); + } + return NULL; } nfo->type = RAY_LIST; nfo->len = 4; @@ -63,15 +61,19 @@ void ray_nfo_insert(ray_t* nfo, ray_t* node, ray_span_t span) { ray_t* vals = NFO_VALS(nfo); if (!keys || !vals) return; - ray_t* new_keys = ray_vec_append(keys, &key); - ray_t* new_vals = ray_vec_append(vals, &val); - /* If vec_append reallocated, update the nfo list slots directly. * ray_vec_append already transferred ownership (the old pointer is * invalid), so we just swap the slot pointer; no retain/release needed - * since the list already owns one ref from the original append. */ + * since the list already owns one ref from the original append. An + * append that fails to grow leaves its vector as it was: the span goes + * unrecorded, its key taken back if only the value failed, and the + * error is freed, never kept in a slot. */ ray_t** slots = (ray_t**)ray_data(nfo); + ray_t* new_keys = ray_vec_append(keys, &key); + if (!new_keys || RAY_IS_ERR(new_keys)) { ray_error_free(new_keys); return; } if (new_keys != keys) slots[2] = new_keys; + ray_t* new_vals = ray_vec_append(vals, &val); + if (!new_vals || RAY_IS_ERR(new_vals)) { ray_error_free(new_vals); new_keys->len--; return; } if (new_vals != vals) slots[3] = new_vals; } diff --git a/src/lang/nfo.h b/src/lang/nfo.h index 10f29e8ac..db8b965a4 100644 --- a/src/lang/nfo.h +++ b/src/lang/nfo.h @@ -59,7 +59,8 @@ typedef union ray_span_t { #define NFO_VALS(nfo) ray_list_get((nfo), 3) /* Create a new nfo object for the given source file. - * Returns a RAY_LIST or ray_error() on failure. */ + * Returns a RAY_LIST, or NULL (no nfo: the source goes without spans) when + * a part fails to allocate. */ ray_t* ray_nfo_create(const char* filename, size_t fname_len, const char* source, size_t src_len); diff --git a/src/ops/agg_engine.c b/src/ops/agg_engine.c index 169aef898..2e6d032f8 100644 --- a/src/ops/agg_engine.c +++ b/src/ops/agg_engine.c @@ -13633,7 +13633,7 @@ static ray_t* exec_group_v2_run_keys(ray_graph_t* g, ray_op_t* op, ray_t* tbl, if (grp_rc != 0) { agg_dense_plan_free(&dp); return agg_keys_error(grp_rc); } ray_t* result = ray_table_new(ext->n_keys + ext->n_aggs); - if (!result || RAY_IS_ERR(result)) { agg_dense_plan_free(&dp); agg_groups_free(&groups); return ray_error("oom", NULL); } + if (!result || RAY_IS_ERR(result)) { agg_dense_plan_free(&dp); agg_groups_free(&groups); return result ? result : ray_error("oom", NULL); } for (uint32_t k = 0; k < ext->n_keys; k++) { ray_t* kc = ray_group_gather(key_cols[k], groups.first_row, groups.ngroups); @@ -13882,7 +13882,7 @@ ray_t* agg_run_one(const agg_vtable_t* vt, ray_t* val_col, if (vt->destroy) for (int64_t gi = 0; gi < ngroups; gi++) vt->destroy(states + (size_t)gi * vt->state_size); - ray_free_raw(states); return ray_error("oom", NULL); + ray_free_raw(states); return out ? out : ray_error("oom", NULL); } out->len = ngroups; for (int64_t gi = 0; gi < ngroups; gi++) { @@ -13923,7 +13923,7 @@ ray_t* agg_run_one_bin(const agg_vtable_t* vt, ray_t* x_col, ray_t* y_col, if (vt->destroy) for (int64_t gi = 0; gi < ngroups; gi++) vt->destroy(states + (size_t)gi * vt->state_size); - ray_free_raw(states); return ray_error("oom", NULL); + ray_free_raw(states); return out ? out : ray_error("oom", NULL); } out->len = ngroups; for (int64_t gi = 0; gi < ngroups; gi++) { diff --git a/src/ops/exec.c b/src/ops/exec.c index 79efe155f..e1704f023 100644 --- a/src/ops/exec.c +++ b/src/ops/exec.c @@ -7742,7 +7742,16 @@ ray_t* exec_flatten_parted_table(ray_t* tbl) { static ray_t* ray_execute_inner(ray_graph_t* g, ray_op_t* root, bool reset_cancel) { ray_enc_note_exec_start(); - if (!g || !root) return ray_error("nyi", NULL); + if (!g || !root) { + /* A node that failed to allocate leaves a NULL root and its failure + * (oom) in compile_err: answer that, not "not implemented". */ + if (g && g->compile_err) { + ray_t* e = g->compile_err; + g->compile_err = NULL; + return e; + } + return ray_error("nyi", NULL); + } /* Lazy-init the global thread pool on first call */ ray_pool_t* pool = ray_pool_get(); diff --git a/src/ops/fused_topk.c b/src/ops/fused_topk.c index d7c7424f5..218f6322a 100644 --- a/src/ops/fused_topk.c +++ b/src/ops/fused_topk.c @@ -1341,7 +1341,9 @@ ray_t* ray_fused_topk_select(ray_t* tbl, if (gerr) { build_ok = 0; break; } col = gather_by_idx(src, global_idx, global_n); } - if (!col || RAY_IS_ERR(col)) { build_ok = 0; break; } + /* a gather that failed to allocate: declined, the unfused path + * runs; its error goes, it is not refcounted */ + if (!col || RAY_IS_ERR(col)) { ray_error_free(col); build_ok = 0; break; } result = ray_table_add_col(result, alias, col); ray_release(col); } diff --git a/src/ops/graph.c b/src/ops/graph.c index 238d385fb..50ab044bb 100644 --- a/src/ops/graph.c +++ b/src/ops/graph.c @@ -46,6 +46,18 @@ static void graph_compile_failure(ray_graph_t* g, ray_t* err) { ray_error_free(err); } +/* Every builder refuses an input node it was handed NULL: a node that + * failed to build (its allocation failure recorded in g->compile_err, or a + * shape its builder declines) has no id to read. The builder answers NULL + * in turn, so a plan built under a failure ends in a NULL root, which + * ray_execute answers with the recorded error (#1817). An array of n + * inputs is checked element by element; a NULL array holds none. */ +static inline bool graph_ins_ok(ray_op_t* const* ins, uint32_t n) { + if (!ins) return n == 0; + for (uint32_t i = 0; i < n; i++) if (!ins[i]) return false; + return true; +} + /* Graph edges are stable uint32 node ids (indices into g->nodes), so a realloc of g->nodes needs no pointer fixups — the former graph_fix_ptr / graph_fixup_ptrs / graph_fixup_ext_ptrs machinery is gone entirely. */ @@ -413,6 +425,7 @@ ray_op_t* ray_const_table(ray_graph_t* g, ray_t* tbl) { * -------------------------------------------------------------------------- */ static ray_op_t* make_unary(ray_graph_t* g, uint16_t opcode, ray_op_t* a, int8_t out_type) { + if (!a) return NULL; /* an input that failed to build */ /* Save ID before alloc — realloc may invalidate the pointer */ uint32_t a_id = a->id; uint32_t est = a->est_rows; @@ -430,6 +443,7 @@ static ray_op_t* make_unary(ray_graph_t* g, uint16_t opcode, ray_op_t* a, int8_t static ray_op_t* make_unary_param(ray_graph_t* g, uint16_t opcode, ray_op_t* a, int8_t out_type, int64_t param) { + if (!a) return NULL; /* an input that failed to build */ uint32_t a_id = a->id; uint32_t est = a->est_rows; ray_op_ext_t* ext = graph_alloc_ext_node(g); @@ -448,6 +462,7 @@ static ray_op_t* make_unary_param(ray_graph_t* g, uint16_t opcode, ray_op_t* a, } static ray_op_t* make_binary(ray_graph_t* g, uint16_t opcode, ray_op_t* a, ray_op_t* b, int8_t out_type) { + if (!a || !b) return NULL; /* an input that failed to build */ /* Save IDs before alloc — realloc may invalidate the pointers */ uint32_t a_id = a->id; uint32_t b_id = b->id; @@ -529,8 +544,8 @@ static int8_t promote_if_type(int8_t a, int8_t b) { * Unary element-wise ops * -------------------------------------------------------------------------- */ -ray_op_t* ray_neg(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_NEG, a, a->out_type); } -ray_op_t* ray_abs(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_ABS, a, a->out_type); } +ray_op_t* ray_neg(ray_graph_t* g, ray_op_t* a) { return a ? make_unary(g, OP_NEG, a, a->out_type) : NULL; } +ray_op_t* ray_abs(ray_graph_t* g, ray_op_t* a) { return a ? make_unary(g, OP_ABS, a, a->out_type) : NULL; } ray_op_t* ray_not(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_NOT, a, RAY_BOOL); } ray_op_t* ray_sqrt_op(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_SQRT, a, RAY_F64); } ray_op_t* ray_log_op(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_LOG, a, RAY_F64); } @@ -543,9 +558,9 @@ ray_op_t* ray_tan_op(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_TAN ray_op_t* ray_atan_op(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_ATAN, a, RAY_F64); } ray_op_t* ray_reciprocal_op(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_RECIPROCAL, a, RAY_F64); } ray_op_t* ray_signum_op(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_SIGNUM, a, RAY_I64); } -ray_op_t* ray_ceil_op(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_CEIL, a, a->out_type); } -ray_op_t* ray_floor_op(ray_graph_t* g, ray_op_t* a){ return make_unary(g, OP_FLOOR, a, a->out_type); } -ray_op_t* ray_round_op(ray_graph_t* g, ray_op_t* a){ return make_unary(g, OP_ROUND, a, a->out_type); } +ray_op_t* ray_ceil_op(ray_graph_t* g, ray_op_t* a) { return a ? make_unary(g, OP_CEIL, a, a->out_type) : NULL; } +ray_op_t* ray_floor_op(ray_graph_t* g, ray_op_t* a){ return a ? make_unary(g, OP_FLOOR, a, a->out_type) : NULL; } +ray_op_t* ray_round_op(ray_graph_t* g, ray_op_t* a){ return a ? make_unary(g, OP_ROUND, a, a->out_type) : NULL; } ray_op_t* ray_isnull(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_ISNULL, a, RAY_BOOL); } ray_op_t* ray_cast(ray_graph_t* g, ray_op_t* a, int8_t target_type) { @@ -648,6 +663,7 @@ static int cmp_is_symbol_value(ray_graph_t* g, ray_op_t* x) { } static ray_op_t* make_cmp(ray_graph_t* g, uint16_t opcode, ray_op_t* a, ray_op_t* b) { + if (!a || !b) return NULL; /* an input that failed to build */ int8_t a_type = a->out_type, b_type = b->out_type; if (RAY_IS_PARTED(a_type)) a_type = (int8_t)RAY_PARTED_BASETYPE(a_type); if (RAY_IS_PARTED(b_type)) b_type = (int8_t)RAY_PARTED_BASETYPE(b_type); @@ -678,6 +694,7 @@ static ray_op_t* make_cmp(ray_graph_t* g, uint16_t opcode, ray_op_t* a, ray_op_t } ray_op_t* ray_binop(ray_graph_t* g, uint16_t opcode, ray_op_t* a, ray_op_t* b) { + if (!a || !b) return NULL; /* an input that failed to build */ int8_t out; switch (opcode) { case OP_EQ: case OP_NE: case OP_LT: case OP_LE: @@ -699,18 +716,19 @@ ray_op_t* ray_binop(ray_graph_t* g, uint16_t opcode, ray_op_t* a, ray_op_t* b) { return make_binary(g, opcode, a, b, out); } -ray_op_t* ray_add(ray_graph_t* g, ray_op_t* a, ray_op_t* b) { return make_binary(g, OP_ADD, a, b, arith_promote(a->out_type, b->out_type)); } -ray_op_t* ray_sub(ray_graph_t* g, ray_op_t* a, ray_op_t* b) { return make_binary(g, OP_SUB, a, b, arith_promote(a->out_type, b->out_type)); } -ray_op_t* ray_mul(ray_graph_t* g, ray_op_t* a, ray_op_t* b) { return make_binary(g, OP_MUL, a, b, arith_promote(a->out_type, b->out_type)); } +ray_op_t* ray_add(ray_graph_t* g, ray_op_t* a, ray_op_t* b) { return a && b ? make_binary(g, OP_ADD, a, b, arith_promote(a->out_type, b->out_type)) : NULL; } +ray_op_t* ray_sub(ray_graph_t* g, ray_op_t* a, ray_op_t* b) { return a && b ? make_binary(g, OP_SUB, a, b, arith_promote(a->out_type, b->out_type)) : NULL; } +ray_op_t* ray_mul(ray_graph_t* g, ray_op_t* a, ray_op_t* b) { return a && b ? make_binary(g, OP_MUL, a, b, arith_promote(a->out_type, b->out_type)) : NULL; } ray_op_t* ray_div(ray_graph_t* g, ray_op_t* a, ray_op_t* b) { return make_binary(g, OP_DIV, a, b, RAY_F64); } /* div takes the promotion table's type over numeric operands (#972); * anything else (a temporal the projection hands to the builtin) keeps I64. */ ray_op_t* ray_idiv(ray_graph_t* g, ray_op_t* a, ray_op_t* b) { + if (!a || !b) return NULL; /* an input that failed to build */ int8_t out = ray_num_type(a->out_type, b->out_type) ? arith_promote(a->out_type, b->out_type) : RAY_I64; return make_binary(g, OP_IDIV, a, b, out); } -ray_op_t* ray_mod(ray_graph_t* g, ray_op_t* a, ray_op_t* b) { return make_binary(g, OP_MOD, a, b, arith_promote(a->out_type, b->out_type)); } +ray_op_t* ray_mod(ray_graph_t* g, ray_op_t* a, ray_op_t* b) { return a && b ? make_binary(g, OP_MOD, a, b, arith_promote(a->out_type, b->out_type)) : NULL; } ray_op_t* ray_pow_op(ray_graph_t* g, ray_op_t* a, ray_op_t* b) { return make_binary(g, OP_POW, a, b, RAY_F64); } ray_op_t* ray_eq(ray_graph_t* g, ray_op_t* a, ray_op_t* b) { return make_cmp(g, OP_EQ, a, b); } @@ -721,8 +739,8 @@ ray_op_t* ray_gt(ray_graph_t* g, ray_op_t* a, ray_op_t* b) { return make_cmp(g, ray_op_t* ray_ge(ray_graph_t* g, ray_op_t* a, ray_op_t* b) { return make_cmp(g, OP_GE, a, b); } ray_op_t* ray_and(ray_graph_t* g, ray_op_t* a, ray_op_t* b){ return make_binary(g, OP_AND, a, b, RAY_BOOL); } ray_op_t* ray_or(ray_graph_t* g, ray_op_t* a, ray_op_t* b) { return make_binary(g, OP_OR, a, b, RAY_BOOL); } -ray_op_t* ray_min2(ray_graph_t* g, ray_op_t* a, ray_op_t* b){ return make_binary(g, OP_MIN2, a, b, promote(a->out_type, b->out_type)); } -ray_op_t* ray_max2(ray_graph_t* g, ray_op_t* a, ray_op_t* b){ return make_binary(g, OP_MAX2, a, b, promote(a->out_type, b->out_type)); } +ray_op_t* ray_min2(ray_graph_t* g, ray_op_t* a, ray_op_t* b){ return a && b ? make_binary(g, OP_MIN2, a, b, promote(a->out_type, b->out_type)) : NULL; } +ray_op_t* ray_max2(ray_graph_t* g, ray_op_t* a, ray_op_t* b){ return a && b ? make_binary(g, OP_MAX2, a, b, promote(a->out_type, b->out_type)) : NULL; } ray_op_t* ray_in(ray_graph_t* g, ray_op_t* col, ray_op_t* set){ return make_binary(g, OP_IN, col, set, RAY_BOOL); } ray_op_t* ray_not_in(ray_graph_t* g, ray_op_t* col, ray_op_t* set){ return make_binary(g, OP_NOT_IN, col, set, RAY_BOOL); } @@ -754,6 +772,7 @@ static bool if_const_str_atom(ray_graph_t* g, uint32_t id) { } ray_op_t* ray_if(ray_graph_t* g, ray_op_t* cond, ray_op_t* then_val, ray_op_t* else_val) { + if (!cond || !then_val || !else_val) return NULL; /* an input that failed to build */ /* 3-input node: cond, then, else — needs ext node */ uint32_t cond_id = cond->id; uint32_t then_id = then_val->id; @@ -856,15 +875,16 @@ ray_op_t* ray_ilike(ray_graph_t* g, ray_op_t* input, ray_op_t* pattern) { } /* String ops */ -ray_op_t* ray_upper(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_UPPER, a, a->out_type == RAY_STR ? RAY_STR : RAY_SYM); } -ray_op_t* ray_lower(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_LOWER, a, a->out_type == RAY_STR ? RAY_STR : RAY_SYM); } +ray_op_t* ray_upper(ray_graph_t* g, ray_op_t* a) { return a ? make_unary(g, OP_UPPER, a, a->out_type == RAY_STR ? RAY_STR : RAY_SYM) : NULL; } +ray_op_t* ray_lower(ray_graph_t* g, ray_op_t* a) { return a ? make_unary(g, OP_LOWER, a, a->out_type == RAY_STR ? RAY_STR : RAY_SYM) : NULL; } ray_op_t* ray_strlen(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_STRLEN, a, RAY_I64); } -ray_op_t* ray_trim_op(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_TRIM, a, a->out_type == RAY_STR ? RAY_STR : RAY_SYM); } +ray_op_t* ray_trim_op(ray_graph_t* g, ray_op_t* a) { return a ? make_unary(g, OP_TRIM, a, a->out_type == RAY_STR ? RAY_STR : RAY_SYM) : NULL; } ray_op_t* ray_str_find_op(ray_graph_t* g, ray_op_t* input, ray_op_t* pattern) { return make_binary(g, OP_STR_FIND, input, pattern, RAY_I64); } ray_op_t* ray_substr(ray_graph_t* g, ray_op_t* str, ray_op_t* start, ray_op_t* len) { + if (!str || !start || !len) return NULL; /* an input that failed to build */ /* 3-input: str=inputs[0], start=inputs[1], len stored via literal field */ uint32_t s_id = str->id; uint32_t st_id = start->id; @@ -889,6 +909,7 @@ ray_op_t* ray_substr(ray_graph_t* g, ray_op_t* str, ray_op_t* start, ray_op_t* l } ray_op_t* ray_replace(ray_graph_t* g, ray_op_t* str, ray_op_t* from, ray_op_t* to) { + if (!str || !from || !to) return NULL; /* an input that failed to build */ /* 3-input: str=inputs[0], from=inputs[1], to stored via literal field */ uint32_t s_id = str->id; uint32_t f_id = from->id; @@ -913,6 +934,7 @@ ray_op_t* ray_replace(ray_graph_t* g, ray_op_t* str, ray_op_t* from, ray_op_t* t } ray_op_t* ray_concat(ray_graph_t* g, ray_op_t** args, int n) { + if (!graph_ins_ok(args, (uint32_t)n)) return NULL; /* Variadic: first 2 in inputs[], rest in trailing IDs */ if (!args || n < 2) return NULL; /* M4: Guard VLA upper bound */ @@ -955,64 +977,71 @@ ray_op_t* ray_concat(ray_graph_t* g, ray_op_t** args, int n) { * -------------------------------------------------------------------------- */ ray_op_t* ray_sum(ray_graph_t* g, ray_op_t* a) { + if (!a) return NULL; /* an input that failed to build */ int8_t type = a->out_type; if (RAY_IS_PARTED(type)) type = (int8_t)RAY_PARTED_BASETYPE(type); int8_t out = type == RAY_F64 || type == RAY_F32 ? RAY_F64 : type == RAY_TIMESPAN ? RAY_TIMESPAN : RAY_I64; return make_unary(g, OP_SUM, a, out); } -ray_op_t* ray_prod(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_PROD, a, (a->out_type == RAY_F64 || a->out_type == RAY_F32) ? RAY_F64 : RAY_I64); } +ray_op_t* ray_prod(ray_graph_t* g, ray_op_t* a) { return a ? make_unary(g, OP_PROD, a, (a->out_type == RAY_F64 || a->out_type == RAY_F32) ? RAY_F64 : RAY_I64) : NULL; } ray_op_t* ray_all(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_ALL, a, RAY_BOOL); } ray_op_t* ray_any(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_ANY, a, RAY_BOOL); } -ray_op_t* ray_min_op(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_MIN, a, a->out_type); } -ray_op_t* ray_max_op(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_MAX, a, a->out_type); } +ray_op_t* ray_min_op(ray_graph_t* g, ray_op_t* a) { return a ? make_unary(g, OP_MIN, a, a->out_type) : NULL; } +ray_op_t* ray_max_op(ray_graph_t* g, ray_op_t* a) { return a ? make_unary(g, OP_MAX, a, a->out_type) : NULL; } ray_op_t* ray_count(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_COUNT, a, RAY_I64); } -ray_op_t* ray_avg(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_AVG, a, mean_out_type(a->out_type)); } -ray_op_t* ray_first(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_FIRST, a, a->out_type); } -ray_op_t* ray_last(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_LAST, a, a->out_type); } +ray_op_t* ray_avg(ray_graph_t* g, ray_op_t* a) { return a ? make_unary(g, OP_AVG, a, mean_out_type(a->out_type)) : NULL; } +ray_op_t* ray_first(ray_graph_t* g, ray_op_t* a) { return a ? make_unary(g, OP_FIRST, a, a->out_type) : NULL; } +ray_op_t* ray_last(ray_graph_t* g, ray_op_t* a) { return a ? make_unary(g, OP_LAST, a, a->out_type) : NULL; } ray_op_t* ray_count_distinct(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_COUNT_DISTINCT, a, RAY_I64); } -ray_op_t* ray_distinct_op(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_DISTINCT, a, a->out_type); } -ray_op_t* ray_asc_op(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_ASC, a, a->out_type); } -ray_op_t* ray_desc_op(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_DESC, a, a->out_type); } -ray_op_t* ray_reverse_op(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_REVERSE, a, a->out_type); } -ray_op_t* ray_lag_op(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_LAG, a, a->out_type); } -ray_op_t* ray_lead_op(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_LEAD, a, a->out_type); } +ray_op_t* ray_distinct_op(ray_graph_t* g, ray_op_t* a) { return a ? make_unary(g, OP_DISTINCT, a, a->out_type) : NULL; } +ray_op_t* ray_asc_op(ray_graph_t* g, ray_op_t* a) { return a ? make_unary(g, OP_ASC, a, a->out_type) : NULL; } +ray_op_t* ray_desc_op(ray_graph_t* g, ray_op_t* a) { return a ? make_unary(g, OP_DESC, a, a->out_type) : NULL; } +ray_op_t* ray_reverse_op(ray_graph_t* g, ray_op_t* a) { return a ? make_unary(g, OP_REVERSE, a, a->out_type) : NULL; } +ray_op_t* ray_lag_op(ray_graph_t* g, ray_op_t* a) { return a ? make_unary(g, OP_LAG, a, a->out_type) : NULL; } +ray_op_t* ray_lead_op(ray_graph_t* g, ray_op_t* a) { return a ? make_unary(g, OP_LEAD, a, a->out_type) : NULL; } ray_op_t* ray_deltas_op(ray_graph_t* g, ray_op_t* a) { + if (!a) return NULL; /* an input that failed to build */ int8_t t = a->out_type; return make_unary(g, OP_DELTAS, a, deltas_out_type(t)); } ray_op_t* ray_ratios_op(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_RATIOS, a, RAY_F64); } -ray_op_t* ray_fills_op(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_FILLS, a, a->out_type); } +ray_op_t* ray_fills_op(ray_graph_t* g, ray_op_t* a) { return a ? make_unary(g, OP_FILLS, a, a->out_type) : NULL; } ray_op_t* ray_sums_op(ray_graph_t* g, ray_op_t* a) { + if (!a) return NULL; /* an input that failed to build */ int8_t t = a->out_type; return make_unary(g, OP_SUMS, a, (t == RAY_F64 || t == RAY_F32) ? RAY_F64 : scan_int_out_type(t)); } -ray_op_t* ray_avgs_op(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_AVGS, a, mean_out_type(a->out_type)); } +ray_op_t* ray_avgs_op(ray_graph_t* g, ray_op_t* a) { return a ? make_unary(g, OP_AVGS, a, mean_out_type(a->out_type)) : NULL; } ray_op_t* ray_mins_op(ray_graph_t* g, ray_op_t* a) { + if (!a) return NULL; /* an input that failed to build */ int8_t t = a->out_type; return make_unary(g, OP_MINS, a, t); } ray_op_t* ray_maxs_op(ray_graph_t* g, ray_op_t* a) { + if (!a) return NULL; /* an input that failed to build */ int8_t t = a->out_type; return make_unary(g, OP_MAXS, a, t); } ray_op_t* ray_prds_op(ray_graph_t* g, ray_op_t* a) { + if (!a) return NULL; /* an input that failed to build */ int8_t t = a->out_type; return make_unary(g, OP_PRDS, a, (t == RAY_F64 || t == RAY_F32) ? RAY_F64 : RAY_I64); } ray_op_t* ray_differ_op(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_DIFFER, a, RAY_BOOL); } ray_op_t* ray_msum_op(ray_graph_t* g, ray_op_t* a, int64_t window) { + if (!a) return NULL; /* an input that failed to build */ int8_t t = a->out_type; return make_unary_param(g, OP_MSUM, a, (t == RAY_F64 || t == RAY_F32) ? RAY_F64 : scan_int_out_type(t), window); } -ray_op_t* ray_mavg_op(ray_graph_t* g, ray_op_t* a, int64_t window) { - return make_unary_param(g, OP_MAVG, a, mean_out_type(a->out_type), window); -} +ray_op_t* ray_mavg_op(ray_graph_t* g, ray_op_t* a, int64_t window) { return a ? make_unary_param(g, OP_MAVG, a, mean_out_type(a->out_type), window) : NULL; } ray_op_t* ray_mmin_op(ray_graph_t* g, ray_op_t* a, int64_t window) { + if (!a) return NULL; /* an input that failed to build */ int8_t t = a->out_type; return make_unary_param(g, OP_MMIN, a, t, window); } ray_op_t* ray_mmax_op(ray_graph_t* g, ray_op_t* a, int64_t window) { + if (!a) return NULL; /* an input that failed to build */ int8_t t = a->out_type; return make_unary_param(g, OP_MMAX, a, t, window); } @@ -1022,11 +1051,9 @@ ray_op_t* ray_mcount_op(ray_graph_t* g, ray_op_t* a, int64_t window) { ray_op_t* ray_mvar_op(ray_graph_t* g, ray_op_t* a, int64_t window) { return make_unary_param(g, OP_MVAR, a, RAY_F64, window); } -ray_op_t* ray_mdev_op(ray_graph_t* g, ray_op_t* a, int64_t window) { - return make_unary_param(g, OP_MDEV, a, mean_out_type(a->out_type), window); -} -ray_op_t* ray_stddev(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_STDDEV, a, mean_out_type(a->out_type)); } -ray_op_t* ray_stddev_pop(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_STDDEV_POP, a, mean_out_type(a->out_type)); } +ray_op_t* ray_mdev_op(ray_graph_t* g, ray_op_t* a, int64_t window) { return a ? make_unary_param(g, OP_MDEV, a, mean_out_type(a->out_type), window) : NULL; } +ray_op_t* ray_stddev(ray_graph_t* g, ray_op_t* a) { return a ? make_unary(g, OP_STDDEV, a, mean_out_type(a->out_type)) : NULL; } +ray_op_t* ray_stddev_pop(ray_graph_t* g, ray_op_t* a) { return a ? make_unary(g, OP_STDDEV_POP, a, mean_out_type(a->out_type)) : NULL; } ray_op_t* ray_var(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_VAR, a, RAY_F64); } ray_op_t* ray_var_pop(ray_graph_t* g, ray_op_t* a) { return make_unary(g, OP_VAR_POP, a, RAY_F64); } @@ -1042,6 +1069,7 @@ ray_op_t* ray_median(ray_graph_t* g, ray_op_t* a) { * -------------------------------------------------------------------------- */ ray_op_t* ray_filter(ray_graph_t* g, ray_op_t* input, ray_op_t* predicate) { + if (!input || !predicate) return NULL; /* an input that failed to build */ uint32_t input_id = input->id; uint32_t pred_id = predicate->id; uint32_t est = input->est_rows / 2; /* estimate: 50% selectivity */ @@ -1064,11 +1092,13 @@ ray_op_t* ray_filter(ray_graph_t* g, ray_op_t* input, ray_op_t* predicate) { ray_op_t* ray_sort_op(ray_graph_t* g, ray_op_t* table_node, ray_op_t** keys, uint8_t* descs, uint8_t* nulls_first, uint32_t n_cols) { + if (!table_node) return NULL; /* an input that failed to build */ + if (!graph_ins_ok(keys, (uint32_t)n_cols)) return NULL; uint32_t table_id = table_node->id; ray_t* ids_hdr = NULL; uint32_t* key_ids = (uint32_t*)scratch_alloc(&ids_hdr, (size_t)(n_cols ? n_cols : 1) * sizeof(uint32_t)); - if (!key_ids) return NULL; + if (!key_ids) { graph_compile_failure(g, NULL); return NULL; } for (uint32_t i = 0; i < n_cols; i++) key_ids[i] = keys[i]->id; size_t keys_sz = (size_t)n_cols * sizeof(ray_op_t*); @@ -1113,13 +1143,17 @@ ray_op_t* ray_group_build(ray_graph_t* g, ray_op_t** keys, uint32_t n_keys, uint16_t* agg_ops, ray_op_t** agg_ins, ray_op_t** agg_ins2, const int64_t* agg_k, uint32_t n_aggs) { + /* An input node that failed to allocate (a NULL scan or expression) + * fails the group node too: the caller answers that failure. */ + for (uint32_t i = 0; i < n_keys; i++) if (!keys[i]) return NULL; + for (uint32_t i = 0; i < n_aggs; i++) if (!agg_ins[i]) return NULL; /* One carve for all three id arrays (key_ids/agg_ids/agg_ids2 are all * uint32_t, so a single flat buffer sliced by offset suffices). */ ray_t* ids_hdr = NULL; size_t ids_elems = (size_t)n_keys + (size_t)n_aggs * 2; uint32_t* ids_buf = (uint32_t*)scratch_alloc(&ids_hdr, (ids_elems ? ids_elems : 1) * sizeof(uint32_t)); - if (!ids_buf) return NULL; + if (!ids_buf) { graph_compile_failure(g, NULL); return NULL; } uint32_t* key_ids = ids_buf; uint32_t* agg_ids = ids_buf + n_keys; uint32_t* agg_ids2 = ids_buf + n_keys + n_aggs; /* parallel to agg_ids; 0 when no second input */ @@ -1205,10 +1239,12 @@ ray_op_t* ray_pivot_op(ray_graph_t* g, ray_op_t* pivot_col, ray_op_t* value_col, uint16_t agg_op) { + if (!pivot_col || !value_col) return NULL; /* an input that failed to build */ + if (!graph_ins_ok(index_cols, (uint32_t)n_index)) return NULL; ray_t* idx_hdr = NULL; uint32_t* idx_ids = (uint32_t*)scratch_alloc(&idx_hdr, (size_t)(n_index ? n_index : 1) * sizeof(uint32_t)); - if (!idx_ids) return NULL; + if (!idx_ids) { graph_compile_failure(g, NULL); return NULL; } for (uint32_t i = 0; i < n_index; i++) idx_ids[i] = index_cols[i]->id; uint32_t pcol_id = pivot_col->id; uint32_t vcol_id = value_col->id; @@ -1240,13 +1276,16 @@ ray_op_t* ray_join(ray_graph_t* g, ray_op_t* left_table, ray_op_t** left_keys, ray_op_t* right_table, ray_op_t** right_keys, uint32_t n_keys, uint8_t join_type) { + if (!left_table || !right_table) return NULL; /* an input that failed to build */ + if (!graph_ins_ok(left_keys, (uint32_t)n_keys)) return NULL; + if (!graph_ins_ok(right_keys, (uint32_t)n_keys)) return NULL; uint32_t left_table_id = left_table->id; uint32_t right_table_id = right_table->id; ray_t* keyids_hdr = NULL; size_t keyids_elems = (size_t)n_keys * 2; uint32_t* keyids_buf = (uint32_t*)scratch_alloc(&keyids_hdr, (keyids_elems ? keyids_elems : 1) * sizeof(uint32_t)); - if (!keyids_buf) return NULL; + if (!keyids_buf) { graph_compile_failure(g, NULL); return NULL; } uint32_t* lkey_ids = keyids_buf; uint32_t* rkey_ids = keyids_buf + n_keys; for (uint32_t i = 0; i < n_keys; i++) { @@ -1288,13 +1327,16 @@ ray_op_t* ray_antijoin(ray_graph_t* g, ray_op_t* left_table, ray_op_t** left_keys, ray_op_t* right_table, ray_op_t** right_keys, uint32_t n_keys) { + if (!left_table || !right_table) return NULL; /* an input that failed to build */ + if (!graph_ins_ok(left_keys, (uint32_t)n_keys)) return NULL; + if (!graph_ins_ok(right_keys, (uint32_t)n_keys)) return NULL; uint32_t left_table_id = left_table->id; uint32_t right_table_id = right_table->id; ray_t* keyids_hdr = NULL; size_t keyids_elems = (size_t)n_keys * 2; uint32_t* keyids_buf = (uint32_t*)scratch_alloc(&keyids_hdr, (keyids_elems ? keyids_elems : 1) * sizeof(uint32_t)); - if (!keyids_buf) return NULL; + if (!keyids_buf) { graph_compile_failure(g, NULL); return NULL; } uint32_t* lkey_ids = keyids_buf; uint32_t* rkey_ids = keyids_buf + n_keys; for (uint32_t i = 0; i < n_keys; i++) { @@ -1336,13 +1378,15 @@ ray_op_t* ray_asof_join(ray_graph_t* g, ray_op_t* time_key, ray_op_t** eq_keys, uint32_t n_eq_keys, uint8_t join_type) { + if (!left_table || !right_table || !time_key) return NULL; /* an input that failed to build */ + if (!graph_ins_ok(eq_keys, (uint32_t)n_eq_keys)) return NULL; uint32_t left_id = left_table->id; uint32_t right_id = right_table->id; uint32_t time_id = time_key->id; ray_t* eq_hdr = NULL; uint32_t* eq_ids = (uint32_t*)scratch_alloc(&eq_hdr, (size_t)(n_eq_keys ? n_eq_keys : 1) * sizeof(uint32_t)); - if (!eq_ids) return NULL; + if (!eq_ids) { graph_compile_failure(g, NULL); return NULL; } for (uint32_t i = 0; i < n_eq_keys; i++) eq_ids[i] = eq_keys[i]->id; /* Trailing: [eq_keys: n_eq_keys * ptr] */ @@ -1379,12 +1423,16 @@ ray_op_t* ray_window_op(ray_graph_t* g, ray_op_t* table_node, int64_t* func_params, uint32_t n_funcs, uint8_t frame_type, uint8_t frame_start, uint8_t frame_end, int64_t frame_start_n, int64_t frame_end_n) { + if (!table_node) return NULL; /* an input that failed to build */ + if (!graph_ins_ok(part_keys, (uint32_t)n_part)) return NULL; + if (!graph_ins_ok(order_keys, (uint32_t)n_order)) return NULL; + if (!graph_ins_ok(func_inputs, (uint32_t)n_funcs)) return NULL; /* One carve for all three id arrays (all uint32_t; sliced by offset). */ ray_t* ids_hdr = NULL; size_t ids_elems = (size_t)n_part + (size_t)n_order + (size_t)n_funcs; uint32_t* ids_buf = (uint32_t*)scratch_alloc(&ids_hdr, (ids_elems ? ids_elems : 1) * sizeof(uint32_t)); - if (!ids_buf) return NULL; + if (!ids_buf) { graph_compile_failure(g, NULL); return NULL; } uint32_t* part_ids = ids_buf; uint32_t* order_ids = ids_buf + n_part; uint32_t* func_ids = ids_buf + n_part + n_order; @@ -1469,11 +1517,13 @@ ray_op_t* ray_window_op(ray_graph_t* g, ray_op_t* table_node, ray_op_t* ray_select_op(ray_graph_t* g, ray_op_t* input, ray_op_t** cols, uint32_t n_cols) { + if (!input) return NULL; /* an input that failed to build */ + if (!graph_ins_ok(cols, (uint32_t)n_cols)) return NULL; uint32_t input_id = input->id; ray_t* ids_hdr = NULL; uint32_t* col_ids = (uint32_t*)scratch_alloc(&ids_hdr, (size_t)(n_cols ? n_cols : 1) * sizeof(uint32_t)); - if (!col_ids) return NULL; + if (!col_ids) { graph_compile_failure(g, NULL); return NULL; } for (uint32_t i = 0; i < n_cols; i++) col_ids[i] = cols[i]->id; size_t cols_sz = (size_t)n_cols * sizeof(ray_op_t*); @@ -1502,6 +1552,7 @@ ray_op_t* ray_select_op(ray_graph_t* g, ray_op_t* input, /* L6: When n (stored as ext->sym) is 0, HEAD produces an empty result with the same schema as the input. */ ray_op_t* ray_head(ray_graph_t* g, ray_op_t* input, int64_t n) { + if (!input) return NULL; /* an input that failed to build */ uint32_t input_id = input->id; ray_op_ext_t* ext = graph_alloc_ext_node(g); if (!ext) return NULL; @@ -1520,6 +1571,7 @@ ray_op_t* ray_head(ray_graph_t* g, ray_op_t* input, int64_t n) { } ray_op_t* ray_tail(ray_graph_t* g, ray_op_t* input, int64_t n) { + if (!input) return NULL; /* an input that failed to build */ uint32_t input_id = input->id; ray_op_ext_t* ext = graph_alloc_ext_node(g); if (!ext) return NULL; @@ -1538,6 +1590,7 @@ ray_op_t* ray_tail(ray_graph_t* g, ray_op_t* input, int64_t n) { } ray_op_t* ray_extract(ray_graph_t* g, ray_op_t* col, int64_t field) { + if (!col) return NULL; /* an input that failed to build */ uint32_t col_id = col->id; uint32_t est = col->est_rows; @@ -1558,6 +1611,7 @@ ray_op_t* ray_extract(ray_graph_t* g, ray_op_t* col, int64_t field) { } ray_op_t* ray_date_trunc(ray_graph_t* g, ray_op_t* col, int64_t field) { + if (!col) return NULL; /* an input that failed to build */ /* Not a truncation unit (a TIMESTAMP takes every real one): no op. */ if (!ray_temporal_trunc_type(RAY_TIMESTAMP, (int)field)) return NULL; /* The result keeps the column's type. A pairing the type does not allow @@ -1641,6 +1695,7 @@ ray_op_t* ray_scan_table(ray_graph_t* g, uint16_t table_id, const char* col_name ray_op_t* ray_expand(ray_graph_t* g, ray_op_t* src_nodes, ray_rel_t* rel, uint8_t direction) { + if (!src_nodes) return NULL; /* an input that failed to build */ uint32_t src_id = src_nodes->id; uint32_t est = src_nodes->est_rows; @@ -1667,6 +1722,7 @@ ray_op_t* ray_var_expand(ray_graph_t* g, ray_op_t* start_nodes, ray_rel_t* rel, uint8_t direction, uint8_t min_depth, uint8_t max_depth, bool track_path) { + if (!start_nodes) return NULL; /* an input that failed to build */ uint32_t src_id = start_nodes->id; uint32_t est = start_nodes->est_rows; @@ -1691,6 +1747,7 @@ ray_op_t* ray_var_expand(ray_graph_t* g, ray_op_t* start_nodes, ray_op_t* ray_shortest_path(ray_graph_t* g, ray_op_t* src, ray_op_t* dst, ray_rel_t* rel, uint8_t max_depth) { + if (!src || !dst) return NULL; /* an input that failed to build */ uint32_t src_id = src->id; uint32_t dst_id = dst->id; diff --git a/src/ops/group.c b/src/ops/group.c index be6a9b750..26d72ec64 100644 --- a/src/ops/group.c +++ b/src/ops/group.c @@ -16542,6 +16542,9 @@ static ray_t* exec_group_v2_exprs_run(ray_graph_t* g, ray_op_t* op, ray_t* tbl, ray_sym_intern(knames[k], strlen(knames[k])), ksynth[k]); if (!sub || RAY_IS_ERR(sub)) ok = false; } + /* a failed table or add_col: its error goes (add_col consumed the + * table), and the legacy path runs */ + if (sub && RAY_IS_ERR(sub)) { ray_error_free(sub); sub = NULL; } } ray_op_t* op2 = NULL; if (ok) { @@ -17736,14 +17739,12 @@ static ray_t* exec_group_run(ray_graph_t* g, ray_op_t* op, ray_t* tbl, int64_t out_ncols = out_nkeys + n_aggs; ray_t* result = ray_table_new((int64_t)out_ncols); if (!result || RAY_IS_ERR(result)) - return ray_error("oom", NULL); + return result ? result : ray_error("oom", NULL); ray_retain(src_col); ray_t* tmp_r = ray_table_add_col(result, src_sym, src_col); ray_release(src_col); - if (!tmp_r || RAY_IS_ERR(tmp_r)) { - ray_release(result); - return ray_error("oom", NULL); - } + if (!tmp_r || RAY_IS_ERR(tmp_r)) /* add_col consumed result */ + return tmp_r ? tmp_r : ray_error("oom", NULL); result = tmp_r; for (uint32_t a = 0; a < n_aggs; a++) { ray_retain(cnt_col); @@ -17755,10 +17756,8 @@ static ray_t* exec_group_run(ray_graph_t* g, ray_op_t* op, ray_t* tbl, } tmp_r = ray_table_add_col(result, agg_name, cnt_col); ray_release(cnt_col); - if (!tmp_r || RAY_IS_ERR(tmp_r)) { - ray_release(result); - return ray_error("oom", NULL); - } + if (!tmp_r || RAY_IS_ERR(tmp_r)) /* add_col consumed result */ + return tmp_r ? tmp_r : ray_error("oom", NULL); result = tmp_r; } return result; @@ -24440,7 +24439,7 @@ exec_group_per_partition(ray_graph_t* g, ray_t* parted_tbl, ray_op_ext_t* ext, * borrow_syms, which is already deduplicated.) */ (void)unique_agg; ray_t* sub = ray_table_new((int64_t)(n_borrow + n_keys + n_aggs + 1)); - if (!sub || RAY_IS_ERR(sub)) goto batch_fail; + if (!sub || RAY_IS_ERR(sub)) { ray_error_free(sub); goto batch_fail; } int64_t part_nrows = -1; int64_t fill_budget = GRP_NULL_FILL_BUDGET; /* one sub-table's fills live at a time */ @@ -24703,7 +24702,7 @@ exec_group_per_partition(ray_graph_t* g, ray_t* parted_tbl, ray_op_ext_t* ext, if (merge_tbl) { ray_release(merge_tbl); merge_tbl = NULL; } merge_tbl = ray_table_new((int64_t)(n_keys + part_n_aggs)); if (!merge_tbl || RAY_IS_ERR(merge_tbl)) { - merge_tbl = NULL; goto batch_fail; + ray_error_free(merge_tbl); merge_tbl = NULL; goto batch_fail; } /* Key columns. Read by position: running keeps key k at column k, @@ -24985,6 +24984,9 @@ exec_group_per_partition(ray_graph_t* g, ray_t* parted_tbl, ray_op_ext_t* ext, running = new_running; if (!running || RAY_IS_ERR(running)) { + /* the concat fallback runs; the merge's error goes with it */ + ray_error_free(running); + running = NULL; ray_release(merge_tbl); goto pp_cleanup; } @@ -25076,6 +25078,7 @@ exec_group_per_partition(ray_graph_t* g, ray_t* parted_tbl, ray_op_ext_t* ext, if (has_avg || has_stddev || frow_sym >= 0) { /* drops the decomposed and hidden slots */ ray_t* trimmed = ray_table_new((int64_t)(n_keys + n_aggs)); if (!trimmed || RAY_IS_ERR(trimmed)) { + ray_error_free(trimmed); ray_release(result); if (merge_tbl) ray_release(merge_tbl); goto pp_cleanup; diff --git a/src/ops/query.c b/src/ops/query.c index eaf51992c..75f59042c 100644 --- a/src/ops/query.c +++ b/src/ops/query.c @@ -29109,6 +29109,11 @@ static ray_t* ray_select_body(ray_t** args, int64_t n, unsigned plan, } ray_op_t* root = ray_const_table(g, tbl); + if (!root) { /* its node failed to allocate */ + ray_graph_free(g); + scratch_free(dep_src_hdr); + ray_release(tbl); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL); + } /* Non-agg expression tracking for post-DAG scatter (used in GROUP BY). * Arith-of-aggs decomposition (see try_decompose_agg_arith): hidden agg @@ -33477,7 +33482,7 @@ static ray_t* ray_select_body(ray_t** args, int64_t n, unsigned plan, * pushed onto, as the predicate pushdown wires a filter (opt.c), or run * now as the fused path runs it. */ if (firstn_where) { - ray_op_t* grp = group_head_pushed && root->opcode == OP_HEAD ? op_child(g, root, 0) : NULL; + ray_op_t* grp = group_head_pushed && root && root->opcode == OP_HEAD ? op_child(g, root, 0) : NULL; ray_op_ext_t* gext = grp && grp->opcode == OP_GROUP ? find_ext(g, grp->id) : NULL; /* outputs past the group (a constant, a scatter) read the * selection afterwards (saved_selection): those run it here */ diff --git a/test/test_aware.c b/test/test_aware.c index d4c6d37ff..b9b4cf365 100644 --- a/test/test_aware.c +++ b/test/test_aware.c @@ -31,7 +31,6 @@ #include "table/sym.h" #include "table/table.h" #include "core/runtime.h" -#include "lang/format.h" #include "store/col.h" #include "ops/idxop.h" /* RAY_IDX_DICT */ #include "store/enc.h" @@ -43,6 +42,8 @@ #include "lang/env.h" #include "store/splay.h" #include "store/part.h" +#include "lang/parse.h" +#include "lang/eval.h" #include "ops/colgather.h" #include "ops/agg_engine.h" /* agg_route_stats: the first-n route */ #include "core/pool.h" /* ray_cancel */ @@ -3786,6 +3787,104 @@ static test_result_t test_group_fallback_order(void) { PASS(); } +/* A parted table's group runs per partition (exec_group_per_partition): + * each partition's graph, its scans and group node, and the merge graph + * are built under the failure; the nth allocation of the group fails, + * every n it reaches. A failure the route cannot answer sends the group + * to the flattened table, whose variance may differ in its last bits. */ +static test_result_t test_group_parted_alloc_fail(void) { + for (int p = 0; p < 3; p++) { + char q[600]; + snprintf(q, sizeof q, "(.db.splayed.set \"" AW_TMP "/gp/2024.01.0%d/t/\" " + "(table [j v w] (list (%% (til 30000) 7) (+ (til 30000) %d) (as 'F64 (%% (til 30000) 13)))) {compress: false})", + p + 1, p * 30000); + aw_eval_ok(q); + } + aw_eval_ok("(set P (.db.parted.get \"" AW_TMP "/gp\" 't))"); + static const char* const pq[] = { + "(select {s: (sum v) c: (count v) from: P by: j})", + "(select {a: (avg w) m: (max v) from: P by: j})", + "(select {d: (dev w) from: P by: j})", + }; + static const char* const pcmp[] = { + NULL, NULL, + "(and (all (== (at Q 'j) (at R0 'j))) (all (< (abs (- (at Q 'd) (at R0 'd))) 1e-9)))", + }; + for (size_t q = 0; q < sizeof pq / sizeof *pq; q++) { + int64_t runs0 = ray_group_perpart_runs(); + char setq[200]; + snprintf(setq, sizeof setq, "(set R0 %s)", pq[q]); + aw_eval_ok(setq); + TEST_ASSERT_FMT(ray_group_perpart_runs() > runs0, "%s: not grouped per partition", pq[q]); + int failed = 0; + for (int64_t n = 1; n <= 4000; n++) { + ray_heap_gc(); + bool traced = ray_mem_trace_begin(); + ray_group_alloc_fail_for_test(n); + ray_t* got = aw_eval(pq[q]); + bool fired = ray_group_alloc_fail_fired_for_test(); + ray_group_alloc_fail_for_test(0); + if (got && RAY_IS_ERR(got)) { + char code[32]; + snprintf(code, sizeof code, "%s", ray_err_code(got)); + ray_error_free(got); + ray_heap_gc(); + ray_mem_trace_t tr = {0}; + if (traced) ray_mem_trace_end(&tr); + TEST_ASSERT_FMT(strcmp(code, "oom") == 0, "%s, allocation %lld fails: %s, not oom", pq[q], (long long)n, code); + TEST_ASSERT_FMT(tr.net_bytes <= 0, "%s, allocation %lld fails: %lld bytes kept", pq[q], (long long)n, (long long)tr.net_bytes); + failed++; + continue; + } + if (traced) { ray_mem_trace_t t0; ray_mem_trace_end(&t0); } + TEST_ASSERT_NOT_NULL(got); + ray_env_set(ray_sym_intern("Q", 1), got); + ray_release(got); + TEST_ASSERT_FMT(aw_q_same(pcmp[q]), "%s, allocation %lld fails: an answer, not the reference", pq[q], (long long)n); + if (!fired) break; + } + TEST_ASSERT_FMT(failed > 0, "%s: no allocation failure answered oom", pq[q]); + } + aw_eval_ok("(set P 0)"); aw_eval_ok("(set R0 0)"); aw_eval_ok("(set Q 0)"); + PASS(); +} + +static int aw_sweep_eval(const char* const* qs, const char* const* cmps, size_t nq); + +/* Building the select's graph: the nth allocation of the whole evaluation + * (parse, plan, the graph's nodes, the group) fails, every n it reaches. + * A node that failed to allocate answers `oom` keeping nothing, never + * "not implemented" or a crash (aw_sweep_eval). */ +static test_result_t test_group_build_alloc_fail(void) { + aw_eval_ok("(set F (table [k j v w] (list (% (* (til 70000) 7919) 1000) (% (til 70000) 7) (til 70000) (as 'F64 (% (til 70000) 13)))))"); + for (int p = 0; p < 3; p++) { + char q[600]; + snprintf(q, sizeof q, "(.db.splayed.set \"" AW_TMP "/gb/2024.01.0%d/t/\" " + "(table [j v w] (list (%% (til 30000) 7) (+ (til 30000) %d) (as 'F64 (%% (til 30000) 13)))) {compress: false})", + p + 1, p * 30000); + aw_eval_ok(q); + } + aw_eval_ok("(set P (.db.parted.get \"" AW_TMP "/gb\" 't))"); + static const char* const bq[] = { + "(select {from: F by: k})", + "(select {s: (sum v) from: F by: k})", + "(select {s: (sum v) c: (count v) from: F by: [k j]})", + "(select {q: (med v) from: F by: j})", + "(select {c: (pearson_corr v w) from: F by: j})", + "(select {s: (sum v) a: (avg w) from: F})", + "(select {c: (wavg w v) from: F})", + "(select {t: (sum (* v w)) from: F by: j})", + "(select {s: (sum v) c: (count v) from: P by: j})", + "(select {s: (sum v) from: P})", + "(select {s: (sum (* v w)) from: P by: j})", + }; + int failed = aw_sweep_eval(bq, NULL, sizeof bq / sizeof *bq); + TEST_ASSERT_FMT(failed >= 0, "a failed allocation while building the select answered other than oom keeping nothing"); + TEST_ASSERT_FMT(failed > 0, "no allocation failure answered oom"); + aw_eval_ok("(set F 0)"); aw_eval_ok("(set P 0)"); aw_eval_ok("(set R0 0)"); aw_eval_ok("(set Q 0)"); + PASS(); +} + static test_result_t test_group_alloc_fail(void) { ray_pool_destroy(); TEST_ASSERT_EQ_I(ray_pool_init_total(4), RAY_OK); @@ -3922,6 +4021,65 @@ static test_result_t test_group_alloc_fail(void) { TEST_ASSERT_FMT(ray_group_test_radix_bufs_forced() > forced0, "%s: never reached the radix buffers", fq[q]); TEST_ASSERT_FMT(failed > 0, "%s: no second failure answered oom", fq[q]); } + /* two more emit_agg_columns callers: the slice-group route (an indexed + * key under `where: (in s ...)`, exec_group_slices) and the keyless + * whole-table route, whose row-gathered GUID max is recomputed after + * it; v2 on and off. A failed slice route groups the selection on + * another route, whose SYM key column may carry the has-nulls flag: + * the slice answers compare by value. */ + aw_eval_ok("(set KS (as 'SYM (at ['aa 'bb 'cc 'dd 'ee 'ff 'gg 'hh] (% (til 40000) 8))))"); + aw_eval_ok("(set TI (table [s v f] (list (.attr.set 'grouped KS) (til 40000) (as 'F64 (% (til 40000) 64)))))"); + aw_eval_ok("(set H (table [k v g] (list (% (til 70000) 7) (til 70000) (take (guid 3) 70000))))"); + static const char* const eq[] = { + "(select {sv: (sum f) n: (count v) a: (avg v) from: TI by: s where: (in s ['aa 'cc])})", + "(select {s: (sum v) m: (max v) a: (avg v) from: H})", + "(select {s: (sum v) g: (max g) from: H})", + }; + static const char* const ecmp[] = { + "(and (== (count Q) (count R0)) (all (== (at Q 's) (at R0 's))) (all (== (at Q 'sv) (at R0 'sv))) (all (== (at Q 'n) (at R0 'n))) (all (== (at Q 'a) (at R0 'a))))", + NULL, NULL, + }; + for (int e = 0; e < 2; e++) { + ray_agg_engine_v2 = e == 0; + for (size_t q = 0; q < sizeof eq / sizeof *eq; q++) { + uint64_t sl0 = agg_route_stats().routes[AGG_ROUTE_SLICES]; + char setq[200]; + snprintf(setq, sizeof setq, "(set R0 %s)", eq[q]); + aw_eval_ok(setq); + if (q == 0) + TEST_ASSERT_FMT(agg_route_stats().routes[AGG_ROUTE_SLICES] > sl0, "%s: not on the slice route", eq[q]); + int failed = 0; + for (int64_t n = 1; n <= 4000; n++) { + ray_heap_gc(); + bool traced = ray_mem_trace_begin(); + ray_group_alloc_fail_for_test(n); + ray_t* got = aw_eval(eq[q]); + bool fired = ray_group_alloc_fail_fired_for_test(); + ray_group_alloc_fail_for_test(0); + if (got && RAY_IS_ERR(got)) { + char code[32]; + snprintf(code, sizeof code, "%s", ray_err_code(got)); + ray_error_free(got); + ray_heap_gc(); + ray_mem_trace_t tr = {0}; + if (traced) ray_mem_trace_end(&tr); + TEST_ASSERT_FMT(strcmp(code, "oom") == 0, "%s (v2 %d), allocation %lld fails: %s, not oom", eq[q], e == 0, (long long)n, code); + TEST_ASSERT_FMT(tr.net_bytes <= 0, "%s (v2 %d), allocation %lld fails: %lld bytes kept", eq[q], e == 0, (long long)n, (long long)tr.net_bytes); + failed++; + continue; + } + if (traced) { ray_mem_trace_t t0; ray_mem_trace_end(&t0); } + TEST_ASSERT_NOT_NULL(got); + ray_env_set(ray_sym_intern("Q", 1), got); + ray_release(got); + TEST_ASSERT_FMT(aw_q_same(ecmp[q]), "%s (v2 %d), allocation %lld fails: an answer, not the reference", eq[q], e == 0, (long long)n); + if (!fired) break; + } + TEST_ASSERT_FMT(failed > 0, "%s (v2 %d): no allocation failure answered oom", eq[q], e == 0); + } + } + ray_agg_engine_v2 = true; + aw_eval_ok("(set KS 0)"); aw_eval_ok("(set TI 0)"); aw_eval_ok("(set H 0)"); /* a keys-only `select by:` builds its result after the group (first * rows, the table, the gathered columns): the nth allocation of that * build fails, every n it reaches (#1811) */ @@ -7034,6 +7192,120 @@ static test_result_t test_temporal_accessor_dag(void) { PASS(); } +/* `src` parsed once, evaluated as ray_eval_str does: a sweep then counts + * the evaluation's allocations, not the parser's. */ +static ray_t* aw_eval_parsed(ray_t* parsed) { + ray_clear_error_trace(); + ray_raise_clear(); + ray_t* r = ray_eval(parsed); + ray_eval_request_end(); + if (r && !RAY_IS_ERR(r) && ray_is_lazy(r)) r = ray_lazy_materialize(r); + return r; +} + +/* Sweep each query in qs: the nth allocation of its evaluation fails, + * every n it reaches: of the parsed query on the calling thread's own + * code, then on its pool tasks too, then of the text, parse included. + * Each run answers R0 (by `cmp` when given, else byte for byte) or `oom` + * keeping nothing; the error's trace, which the next request clears, is + * cleared first. Returns the oom count, or -1 after a failure (reported). */ +static int aw_sweep_eval(const char* const* qs, const char* const* cmps, size_t nq) { + int failed = 0; + aw_eval_ok("(set Q 0)"); /* bound before the trace: an answer rebinds it */ + for (int skip = 2; skip >= 0; skip--) { + ray_alloc_fail_skip_tasks_for_test(skip == 1); + for (size_t q = 0; q < nq; q++) { + char setq[600]; + snprintf(setq, sizeof setq, "(set R0 %s)", qs[q]); + aw_eval_ok(setq); + ray_t* parsed = ray_parse(qs[q]); + if (!parsed || RAY_IS_ERR(parsed)) { fprintf(stderr, " %s: does not parse\n", qs[q]); return -1; } + for (int64_t n = 1; n <= 40000; n++) { + ray_heap_gc(); + bool traced = ray_mem_trace_begin(); + ray_alloc_fail_set_for_test(n, 1); + ray_t* got = skip == 2 ? ray_eval_str(qs[q]) : aw_eval_parsed(parsed); + bool fired = ray_alloc_fail_armed_for_test() == 0; + ray_alloc_fail_set_for_test(0, 0); + ray_clear_error_trace(); + const char* what = NULL; + char msg[96]; + if (got && RAY_IS_ERR(got)) { + bool oom = strcmp(ray_err_code(got), "oom") == 0; + snprintf(msg, sizeof msg, "%s, not oom", ray_err_code(got)); + ray_error_free(got); + ray_heap_gc(); + ray_mem_trace_t tr = {0}; + if (traced) ray_mem_trace_end(&tr); + if (!oom) what = msg; + else if (tr.net_bytes > 0) { snprintf(msg, sizeof msg, "%lld bytes kept", (long long)tr.net_bytes); what = msg; } + else failed++; + } else if (!got) { + if (traced) { ray_mem_trace_t t0; ray_mem_trace_end(&t0); } + what = "no answer"; + } else { + /* compared, then released: an answer keeps nothing either */ + ray_env_set(ray_sym_intern("Q", 1), got); + ray_release(got); + bool same = aw_q_same(cmps ? cmps[q] : NULL); + ray_t* zero = ray_i64(0); + ray_env_set(ray_sym_intern("Q", 1), zero); + ray_release(zero); + ray_heap_gc(); + ray_mem_trace_t tr = {0}; + if (traced) ray_mem_trace_end(&tr); + if (!same) what = "an answer, not the reference"; + else if (tr.net_bytes > 0) { snprintf(msg, sizeof msg, "an answer, %lld bytes kept", (long long)tr.net_bytes); what = msg; } + } + if (what) { + fprintf(stderr, " %s (%s), allocation %lld fails: %s\n", qs[q], skip == 2 ? "parse included" : skip ? "tasks skipped" : "tasks too", (long long)n, what); + ray_release(parsed); + ray_alloc_fail_skip_tasks_for_test(false); + return -1; + } + if (!fired) break; + } + ray_release(parsed); + } + } + ray_alloc_fail_skip_tasks_for_test(false); + return failed; +} + +/* Planning under a failure: every select shape builds its graph's nodes + * (scans, constants, the filter, group, join, sort, window and select + * nodes) while the nth allocation of the evaluation fails. A node that + * failed to build is refused by every builder it is handed to, and the + * plan answers the recorded `oom`: never a crash, "not implemented", or + * memory kept (#1817). (The where: shapes are swept with the filter's + * own failures, aware/filter_alloc_fail.) */ +static test_result_t test_plan_alloc_fail(void) { + aw_eval_ok("(set F (table [k j v w] (list (% (* (til 70000) 7919) 1000) (% (til 70000) 7) (til 70000) (as 'F64 (% (til 70000) 13)))))"); + aw_eval_ok("(set L (table [k a] (list (% (til 3000) 700) (til 3000))))"); + aw_eval_ok("(set R (table [k b] (list (til 500) (* 2 (til 500)))))"); + static const char* const pq[] = { + "(select {s: (sum v) from: F by: k})", + "(select {s: (sum v) c: (count w) from: F by: [k j]})", + "(select {v: v from: F asc: [j v] take: 9})", + "(select {m: (max v) a: (avg w) from: F})", + "(select {r: (sums v) m: (mavg 3 w) from: F})", + "(inner-join [k] L R)", + "(left-join [k] L R)", + "(select {s: (sum a) n: (count b) from: (inner-join [k] L R) by: k})", + }; + static const char* const pcmp[] = { + "(all (== (ser (select {from: Q asc: k})) (ser (select {from: R0 asc: k}))))", + "(all (== (ser (select {from: Q asc: [k j]})) (ser (select {from: R0 asc: [k j]}))))", + NULL, NULL, NULL, NULL, NULL, + "(all (== (ser (select {from: Q asc: k})) (ser (select {from: R0 asc: k}))))", + }; + int failed = aw_sweep_eval(pq, pcmp, sizeof pq / sizeof *pq); + TEST_ASSERT_FMT(failed >= 0, "a failed allocation while planning answered other than oom keeping nothing"); + TEST_ASSERT_FMT(failed > 0, "no allocation failure answered oom"); + aw_eval_ok("(set F 0)"); aw_eval_ok("(set L 0)"); aw_eval_ok("(set R 0)"); aw_eval_ok("(set R0 0)"); aw_eval_ok("(set Q 0)"); + PASS(); +} + const test_entry_t aware_entries[] = { { "aware/vview_encodings", test_vview_encodings, aw_setup, aw_teardown }, { "aware/vview_near_top", test_vview_near_top, aw_setup, aw_teardown }, @@ -7090,6 +7362,8 @@ const test_entry_t aware_entries[] = { { "aware/group_sym_key_nulls_flag", test_group_sym_key_nulls_flag, aw_setup, aw_teardown }, { "aware/group_fallback_order", test_group_fallback_order, aw_setup, aw_teardown }, { "aware/group_alloc_fail", test_group_alloc_fail, aw_setup, aw_teardown }, + { "aware/group_parted_alloc_fail", test_group_parted_alloc_fail, aw_setup, aw_teardown }, + { "aware/group_build_alloc_fail", test_group_build_alloc_fail, aw_setup, aw_teardown }, { "aware/holistic_oom", test_holistic_oom, aw_setup, aw_teardown }, { "aware/radix_cancel_keeps_nothing", test_radix_cancel_keeps_nothing, aw_setup, aw_teardown }, { "aware/sel_compact_cancel_at_dispatch", test_sel_compact_cancel_at_dispatch, aw_setup, aw_teardown }, @@ -7148,5 +7422,6 @@ const test_entry_t aware_entries[] = { { "aware/filter_only_columns", test_filter_only_columns, aw_setup, aw_teardown }, { "aware/firstn_where_deferred", test_firstn_where_deferred, aw_setup, aw_teardown }, { "aware/grouped_grade_routes", test_grouped_grade_routes, aw_setup, aw_teardown }, + { "aware/plan_alloc_fail", test_plan_alloc_fail, aw_setup, aw_teardown }, { NULL, NULL, NULL, NULL }, };