From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id C75FC71828; Tue, 23 Mar 2021 12:38:45 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org C75FC71828 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1616492325; bh=b0RyW3/Y7Piycrq9GBeEVKdH1SXhmYg0ViudK1agOHY=; h=To:Cc:Date:In-Reply-To:References:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=lkqctq890eTvUpkF81qS1Ftgg5ESOe27i11mkUzneJ6Cxm2ZF8hpBI7K4XCIsvTqr 8+ViIoDm5UVoHWOPwnS/39niSvWjbcqMsyJKYcv8Zo2G6U3LE6hepIVqhj6ZQ3z/ri 9FYaTEYHliugrET+RnMWddu7MKmLLf3tCP1IFoU0= Received: from smtp61.i.mail.ru (smtp61.i.mail.ru [217.69.128.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 3A1856B469 for ; Tue, 23 Mar 2021 12:35:12 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 3A1856B469 Received: by smtp61.i.mail.ru with esmtpa (envelope-from ) id 1lOdRP-0004Pi-EI; Tue, 23 Mar 2021 12:35:11 +0300 To: v.shpilevoy@tarantool.org, tsafin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Tue, 23 Mar 2021 12:35:11 +0300 Message-Id: <1c4d34e5c3272be804b896ee172809be6bab9932.1616491731.git.imeevma@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD95D6E7CC48CB1F5F10D3016C09B407F8B1E2E766A3410B623182A05F53808504001D9E3F305F0FF8AF8355EC229F8278B7C563059EF45C58A142629AA6E676543 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7DF2546A43F668C04EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637B8CD1D12629438CE8638F802B75D45FF914D58D5BE9E6BC131B5C99E7648C95C686AF563A045C75E20611378CF0512014161757716D9600AA471835C12D1D9774AD6D5ED66289B5278DA827A17800CE71AE4D56B06699BBC9FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C3E478A468B35FE767117882F4460429728AD0CFFFB425014E868A13BD56FB6657D81D268191BDAD3DC09775C1D3CA48CFF16188E26AE129E1BA3038C0950A5D36C8A9BA7A39EFB766EC990983EF5C0329BA3038C0950A5D36D5E8D9A59859A8B6DF6A890A82DFDDA276E601842F6C81A1F004C906525384307823802FF610243DF43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B3BBE47FD9DD3FB59A8DF7F3B2552694A57739F23D657EF2B13377AFFFEAFD26923F8577A6DFFEA7C054662F8F2CA352F93EC92FD9297F6715571747095F342E857739F23D657EF2BD5E8D9A59859A8B698B1B85E56A716AC089D37D7C0E48F6C5571747095F342E857739F23D657EF2B6825BDBE14D8E7024847893F9AA87235E5BFE6E7EFDEDCD789D4C264860C145E X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A24A6D60772A99906F8E1CD14B953EB46DA91DA45F8F35711A355D89D7DBCDD132 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C686AF563A045C75E20611378CF0512014161757716D9600A9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF0417BEADF48D1460699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34A150564C13026C18EBC2CB460C4A07195E100F7DF608CEDA12B9AC79884A99D863B4A13285D64FD41D7E09C32AA3244CF3B0351BCF793633982957EAE95B600881560E2432555DBBFACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojWJZv20R+6UjRZaN1pKzsDA== X-Mailru-Sender: 5C3750E245F362008BC1685FEC6306ED6999C68F2097F659F8355EC229F8278BD776CA2DFBE07C795105BD0848736F9966FEC6BF5C9C28D97E07721503EA2E00ED97202A5A4E92BF7402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: [Tarantool-patches] [PATCH v4 08/53] sql: introduce mem_str() X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Mergen Imeev via Tarantool-patches Reply-To: imeevma@tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" This patch introduces mem_str() which allows to receive value of MEM as a string. Part of #5818 --- src/box/sql/func.c | 8 ++++---- src/box/sql/mem.c | 45 ++++++++++++++++++++++++++++++----------- src/box/sql/mem.h | 16 +++++++-------- src/box/sql/vdbe.c | 28 ++++++++++++------------- src/box/sql/vdbeaux.c | 19 ++++------------- src/box/sql/vdbetrace.c | 33 ++++-------------------------- 6 files changed, 66 insertions(+), 83 deletions(-) diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 074d41260..5ece28d6d 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -849,7 +849,7 @@ roundFunc(sql_context * context, int argc, sql_value ** argv) enum mp_type mp_type = sql_value_type(argv[0]); if (mp_type_is_bloblike(mp_type)) { diag_set(ClientError, ER_SQL_TYPE_MISMATCH, - sql_value_to_diag_str(argv[0]), "numeric"); + mem_str(argv[0]), "numeric"); context->is_aborted = true; return; } @@ -991,7 +991,7 @@ randomBlob(sql_context * context, int argc, sql_value ** argv) UNUSED_PARAMETER(argc); if (mp_type_is_bloblike(sql_value_type(argv[0]))) { diag_set(ClientError, ER_SQL_TYPE_MISMATCH, - sql_value_to_diag_str(argv[0]), "numeric"); + mem_str(argv[0]), "numeric"); context->is_aborted = true; return; } @@ -1883,7 +1883,7 @@ soundexFunc(sql_context * context, int argc, sql_value ** argv) enum mp_type mp_type = sql_value_type(argv[0]); if (mp_type_is_bloblike(mp_type)) { diag_set(ClientError, ER_SQL_TYPE_MISMATCH, - sql_value_to_diag_str(argv[0]), "text"); + mem_str(argv[0]), "text"); context->is_aborted = true; return; } @@ -1956,7 +1956,7 @@ sum_step(struct sql_context *context, int argc, sql_value **argv) if (type != MP_DOUBLE && type != MP_INT && type != MP_UINT) { if (mem_apply_numeric_type(argv[0]) != 0) { diag_set(ClientError, ER_SQL_TYPE_MISMATCH, - sql_value_to_diag_str(argv[0]), "number"); + mem_str(argv[0]), "number"); context->is_aborted = true; return; } diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index 62338e1db..0ed5e38d4 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -37,6 +37,39 @@ #include "box/tuple.h" #include "mpstream/mpstream.h" +enum { + BUF_SIZE = 32, +}; + +const char * +mem_str(const struct Mem *mem) +{ + char buf[BUF_SIZE]; + switch (mem->flags & MEM_PURE_TYPE_MASK) { + case MEM_Null: + return "NULL"; + case MEM_Str: + return tt_sprintf("%.*s", mem->n, mem->z); + case MEM_Int: + return tt_sprintf("%lld", mem->u.i); + case MEM_UInt: + return tt_sprintf("%llu", mem->u.u); + case MEM_Real: + sql_snprintf(BUF_SIZE, &buf[0], "%!.15g", mem->u.r); + return tt_sprintf("%s", buf); + case MEM_Blob: + if ((mem->flags & MEM_Subtype) == 0) + return "varbinary"; + assert(mem->subtype == SQL_SUBTYPE_MSGPACK); + return mp_str(mem->z); + case MEM_Bool: + return mem->u.b ? "TRUE" : "FALSE"; + default: + break; + } + return "unknown"; +} + static inline bool mem_has_msgpack_subtype(struct Mem *mem) { @@ -1632,18 +1665,6 @@ sqlValueText(sql_value * pVal) return valueToText(pVal); } -const char * -sql_value_to_diag_str(sql_value *value) -{ - enum mp_type mp_type = sql_value_type(value); - if (mp_type_is_bloblike(mp_type)) { - if (mem_has_msgpack_subtype(value)) - return sqlValueText(value); - return "varbinary"; - } - return sqlValueText(value); -} - enum sql_subtype sql_value_subtype(sql_value * pVal) { diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index e78ebbe47..740c7dfba 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -87,6 +87,13 @@ struct Mem { */ #define MEMCELLSIZE offsetof(Mem,zMalloc) +/** + * Return a string that represent content of MEM. String is either allocated + * using static_alloc() of just a static variable. + */ +const char * +mem_str(const struct Mem *mem); + /* One or more of the following flags are set to indicate the validOK * representations of the value stored in the Mem struct. * @@ -375,15 +382,6 @@ sql_value_text(struct Mem *); const void *sqlValueText(struct Mem *); -/** - * Return pointer to a string with the data type in the case of - * binary data stored in @a value. Otherwise, return the result - * of sql_value_text(). It is used due to the fact that not all - * binary strings can be displayed correctly (e.g. contain - * unprintable symbols). - */ -const char * -sql_value_to_diag_str(struct Mem *value); #define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))]) enum sql_subtype diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c index dbbcc1013..9b392f9cb 100644 --- a/src/box/sql/vdbe.c +++ b/src/box/sql/vdbe.c @@ -1236,12 +1236,12 @@ case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ } else { if (sqlVdbeRealValue(pIn1, &rA) != 0) { diag_set(ClientError, ER_SQL_TYPE_MISMATCH, - sql_value_to_diag_str(pIn1), "numeric"); + mem_str(pIn1), "numeric"); goto abort_due_to_error; } if (sqlVdbeRealValue(pIn2, &rB) != 0) { diag_set(ClientError, ER_SQL_TYPE_MISMATCH, - sql_value_to_diag_str(pIn2), "numeric"); + mem_str(pIn2), "numeric"); goto abort_due_to_error; } assert(((type1 | type2) & MEM_Real) != 0); @@ -1517,12 +1517,12 @@ case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */ bool unused; if (sqlVdbeIntValue(pIn2, (int64_t *) &iA, &unused) != 0) { diag_set(ClientError, ER_SQL_TYPE_MISMATCH, - sql_value_to_diag_str(pIn2), "integer"); + mem_str(pIn2), "integer"); goto abort_due_to_error; } if (sqlVdbeIntValue(pIn1, (int64_t *) &iB, &unused) != 0) { diag_set(ClientError, ER_SQL_TYPE_MISMATCH, - sql_value_to_diag_str(pIn1), "integer"); + mem_str(pIn1), "integer"); goto abort_due_to_error; } op = pOp->opcode; @@ -1587,7 +1587,7 @@ case OP_MustBeInt: { /* jump, in1 */ if ((pIn1->flags & (MEM_Int | MEM_UInt)) == 0) { if (pOp->p2==0) { diag_set(ClientError, ER_SQL_TYPE_MISMATCH, - sql_value_to_diag_str(pIn1), "integer"); + mem_str(pIn1), "integer"); goto abort_due_to_error; } else { goto jump_to_p2; @@ -1644,7 +1644,7 @@ case OP_Cast: { /* in1 */ UPDATE_MAX_BLOBSIZE(pIn1); if (rc == 0) break; - diag_set(ClientError, ER_SQL_TYPE_MISMATCH, sql_value_to_diag_str(pIn1), + diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(pIn1), field_type_strs[pOp->p2]); goto abort_due_to_error; } @@ -1817,7 +1817,7 @@ case OP_Ge: { /* same as TK_GE, jump, in1, in3 */ if (mem_apply_numeric_type(pIn3) != 0) { diag_set(ClientError, ER_SQL_TYPE_MISMATCH, - sql_value_to_diag_str(pIn3), + mem_str(pIn3), "numeric"); goto abort_due_to_error; } @@ -2083,7 +2083,7 @@ case OP_Or: { /* same as TK_OR, in1, in2, out3 */ v1 = pIn1->u.b; } else { diag_set(ClientError, ER_SQL_TYPE_MISMATCH, - sql_value_to_diag_str(pIn1), "boolean"); + mem_str(pIn1), "boolean"); goto abort_due_to_error; } pIn2 = &aMem[pOp->p2]; @@ -2093,7 +2093,7 @@ case OP_Or: { /* same as TK_OR, in1, in2, out3 */ v2 = pIn2->u.b; } else { diag_set(ClientError, ER_SQL_TYPE_MISMATCH, - sql_value_to_diag_str(pIn2), "boolean"); + mem_str(pIn2), "boolean"); goto abort_due_to_error; } if (pOp->opcode==OP_And) { @@ -2123,7 +2123,7 @@ case OP_Not: { /* same as TK_NOT, in1, out2 */ if ((pIn1->flags & MEM_Null)==0) { if ((pIn1->flags & MEM_Bool) == 0) { diag_set(ClientError, ER_SQL_TYPE_MISMATCH, - sql_value_to_diag_str(pIn1), "boolean"); + mem_str(pIn1), "boolean"); goto abort_due_to_error; } mem_set_bool(pOut, ! pIn1->u.b); @@ -2148,7 +2148,7 @@ case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */ bool is_neg; if (sqlVdbeIntValue(pIn1, &i, &is_neg) != 0) { diag_set(ClientError, ER_SQL_TYPE_MISMATCH, - sql_value_to_diag_str(pIn1), "integer"); + mem_str(pIn1), "integer"); goto abort_due_to_error; } mem_set_i64(pOut, ~i); @@ -2194,7 +2194,7 @@ case OP_IfNot: { /* jump, in1 */ c = pOp->opcode == OP_IfNot ? ! pIn1->u.b : pIn1->u.b; } else { diag_set(ClientError, ER_SQL_TYPE_MISMATCH, - sql_value_to_diag_str(pIn1), "boolean"); + mem_str(pIn1), "boolean"); goto abort_due_to_error; } VdbeBranchTaken(c!=0, 2); @@ -2374,7 +2374,7 @@ case OP_ApplyType: { continue; type_mismatch: diag_set(ClientError, ER_SQL_TYPE_MISMATCH, - sql_value_to_diag_str(pIn1), field_type_strs[type]); + mem_str(pIn1), field_type_strs[type]); goto abort_due_to_error; } break; @@ -3003,7 +3003,7 @@ case OP_SeekGT: { /* jump, in3 */ is_neg = i < 0; } else { diag_set(ClientError, ER_SQL_TYPE_MISMATCH, - sql_value_to_diag_str(pIn3), "integer"); + mem_str(pIn3), "integer"); goto abort_due_to_error; } iKey = i; diff --git a/src/box/sql/vdbeaux.c b/src/box/sql/vdbeaux.c index 9153df1df..f270b0ed1 100644 --- a/src/box/sql/vdbeaux.c +++ b/src/box/sql/vdbeaux.c @@ -1108,21 +1108,10 @@ displayP4(Op * pOp, char *zTemp, int nTemp) break; } case P4_MEM:{ - Mem *pMem = pOp->p4.pMem; - if (pMem->flags & MEM_Str) { - zP4 = pMem->z; - } else if (pMem->flags & MEM_Int) { - sqlXPrintf(&x, "%lld", pMem->u.i); - } else if (pMem->flags & MEM_UInt) { - sqlXPrintf(&x, "%llu", pMem->u.u); - } else if (pMem->flags & MEM_Real) { - sqlXPrintf(&x, "%.16g", pMem->u.r); - } else if (pMem->flags & MEM_Null) { - zP4 = "NULL"; - } else { - assert(pMem->flags & MEM_Blob); - zP4 = "(binary string)"; - } + const char *value = mem_str(pOp->p4.pMem); + uint32_t size = MIN((int)strlen(value), nTemp - 1); + memcpy(zP4, value, size); + zP4[size] = '\0'; break; } case P4_INTARRAY:{ diff --git a/src/box/sql/vdbetrace.c b/src/box/sql/vdbetrace.c index e84bb3192..eceaa953b 100644 --- a/src/box/sql/vdbetrace.c +++ b/src/box/sql/vdbetrace.c @@ -97,8 +97,6 @@ sqlVdbeExpandSql(Vdbe * p, /* The prepared statement being evaluated */ int nextIndex = 1; /* Index of next ? host parameter */ int n; /* Length of a token prefix */ int nToken; /* Length of the parameter token */ - int i; /* Loop counter */ - Mem *pVar; /* Value of a host parameter */ StrAccum out; /* Accumulate the output here */ char zBase[100]; /* Initial working space */ @@ -147,33 +145,10 @@ sqlVdbeExpandSql(Vdbe * p, /* The prepared statement being evaluated */ zRawSql += nToken; nextIndex = idx + 1; assert(idx > 0 && idx <= p->nVar); - pVar = &p->aVar[idx - 1]; - if (pVar->flags & MEM_Null) { - sqlStrAccumAppend(&out, "NULL", 4); - } else if (pVar->flags & MEM_Int) { - sqlXPrintf(&out, "%lld", pVar->u.i); - } else if (pVar->flags & MEM_UInt) { - sqlXPrintf(&out, "%llu", pVar->u.u); - } else if (pVar->flags & MEM_Real) { - sqlXPrintf(&out, "%!.15g", pVar->u.r); - } else if (pVar->flags & MEM_Str) { - int nOut; /* Number of bytes of the string text to include in output */ - nOut = pVar->n; - sqlXPrintf(&out, "'%.*q'", nOut, pVar->z); - } else if (pVar->flags & MEM_Zero) { - sqlXPrintf(&out, "zeroblob(%d)", - pVar->u.nZero); - } else { - int nOut; /* Number of bytes of the blob to include in output */ - assert(pVar->flags & MEM_Blob); - sqlStrAccumAppend(&out, "x'", 2); - nOut = pVar->n; - for (i = 0; i < nOut; i++) { - sqlXPrintf(&out, "%02x", - pVar->z[i] & 0xff); - } - sqlStrAccumAppend(&out, "'", 1); - } + const char *value = mem_str(&p->aVar[idx - 1]); + uint32_t size = MIN(strlen(value), sizeof(zBase) - 1); + memcpy(zBase, value, size); + zBase[size] = '\0'; } } if (out.accError) -- 2.25.1