ssize_t bitToStr(char **dst, size_t *len, const bit *src, bool external) { atommem(6); if (is_bit_nil(*src)) { if (external) { strcpy(*dst, "nil"); return 3; } strcpy(*dst, str_nil); return 1; } if (*src) { strcpy(*dst, "true"); return 4; } strcpy(*dst, "false"); return 5; }
str CMDifthen(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) { BAT *b = NULL, *b1 = NULL, *b2 = NULL, *bn; int tp0, tp1, tp2; bat *ret; BUN cnt = BUN_NONE; (void) cntxt; (void) mb; if (pci->argc != 4) throw(MAL, "batcalc.ifthen", "Operation not supported."); ret = getArgReference_bat(stk, pci, 0); tp0 = stk->stk[getArg(pci, 1)].vtype; tp1 = stk->stk[getArg(pci, 2)].vtype; tp2 = stk->stk[getArg(pci, 3)].vtype; if (tp0 == TYPE_bat || isaBatType(tp0)) { b = BATdescriptor(* getArgReference_bat(stk, pci, 1)); if (b == NULL) throw(MAL, "batcalc.ifthenelse", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING); cnt = BATcount(b); } if (tp1 == TYPE_bat || isaBatType(tp1)) { b1 = BATdescriptor(* getArgReference_bat(stk, pci, 2)); if (b1 == NULL) { if (b) BBPunfix(b->batCacheid); throw(MAL, "batcalc.ifthenelse", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING); } if (cnt == BUN_NONE) cnt = BATcount(b1); else if (BATcount(b1) != cnt) { BBPunfix(b->batCacheid); throw(MAL, "batcalc.ifthenelse", ILLEGAL_ARGUMENT); } } if (tp2 == TYPE_bat || isaBatType(tp2)) { b2 = BATdescriptor(* getArgReference_bat(stk, pci, 3)); if (b2 == NULL) { if (b) BBPunfix(b->batCacheid); if (b1) BBPunfix(b1->batCacheid); throw(MAL, "batcalc.ifthenelse", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING); } if (cnt == BUN_NONE) cnt = BATcount(b2); else if (BATcount(b2) != cnt) { if (b) BBPunfix(b->batCacheid); if (b1) BBPunfix(b1->batCacheid); throw(MAL, "batcalc.ifthenelse", ILLEGAL_ARGUMENT); } } if (b == NULL && b1 == NULL && b2 == NULL) { /* at least one BAT required */ throw(MAL, "batcalc.ifthenelse", ILLEGAL_ARGUMENT); } if (b != NULL) { if (b1 != NULL) { if (b2 != NULL) { bn = BATcalcifthenelse(b, b1, b2); } else { bn = BATcalcifthenelsecst(b, b1, &stk->stk[getArg(pci, 3)]); } } else { if (b2 != NULL) { bn = BATcalcifthencstelse(b, &stk->stk[getArg(pci, 2)], b2); } else { bn = BATcalcifthencstelsecst(b, &stk->stk[getArg(pci, 2)], &stk->stk[getArg(pci, 3)]); } } } else { bit v = *getArgReference_bit(stk, pci, 1); if (is_bit_nil(v)) { if (b1 != NULL) bn = BATconstant(b1->hseqbase, b1->ttype, ATOMnilptr(b1->ttype), BATcount(b1), TRANSIENT); else bn = BATconstant(b2->hseqbase, b2->ttype, ATOMnilptr(b2->ttype), BATcount(b2), TRANSIENT); } else if (v) { if (b1 != NULL) bn = COLcopy(b1, b1->ttype, 0, TRANSIENT); else bn = BATconstant(b2->hseqbase, b2->ttype, VALptr(&stk->stk[getArg(pci, 2)]), BATcount(b2), TRANSIENT); } else { if (b2 != NULL) bn = COLcopy(b2, b2->ttype, 0, TRANSIENT); else bn = BATconstant(b1->hseqbase, b1->ttype, VALptr(&stk->stk[getArg(pci, 3)]), BATcount(b1), TRANSIENT); } } if (b) BBPunfix(b->batCacheid); if (b1) BBPunfix(b1->batCacheid); if (b2) BBPunfix(b2->batCacheid); if (bn == NULL) { return mythrow(MAL, "batcalc.ifthenelse", OPERATION_FAILED); } BBPkeepref(*ret = bn->batCacheid); return MAL_SUCCEED; }