From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id C0BC423E0F for ; Tue, 13 Aug 2019 16:40:13 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id KKiVdBfXcqxr for ; Tue, 13 Aug 2019 16:40:13 -0400 (EDT) Received: from smtpng1.m.smailru.net (smtpng1.m.smailru.net [94.100.181.251]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 58612230EA for ; Tue, 13 Aug 2019 16:40:13 -0400 (EDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Subject: [tarantool-patches] Re: [PATCH v2 7/8] sql: get rid of FuncDef function hash From: "n.pettik" In-Reply-To: <29c44d7790720584498ca1763cb28ff98e35c71d.1565275470.git.kshcherbatov@tarantool.org> Date: Tue, 13 Aug 2019 23:40:11 +0300 Content-Transfer-Encoding: quoted-printable Message-Id: References: <29c44d7790720584498ca1763cb28ff98e35c71d.1565275470.git.kshcherbatov@tarantool.org> Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-Help: List-Unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-Subscribe: List-Owner: List-post: List-Archive: To: tarantool-patches@freelists.org Cc: Kirill Shcherbatov > On 8 Aug 2019, at 17:50, Kirill Shcherbatov = wrote: >=20 > Now it is possible to move all SQL builtin functions to > Tarantool's function hash. An existent FuncDef function > representation was replaced with func_sql_builtin class. > It has a sql-specific method :call and :finalize typica while Nit: typic? Please, consider using spell-checker. > port API call is not supported and protected with stub. >=20 > This patch removes FuncDef hash and sql_function_create endpoint, > but doesn't introduce something instead. Therefore few affected > tests are disabled. A required functionality would be fixed in > the next patch. >=20 > Following tests using sql_create_function are broken now. > They would be fixed in the following commit: Nit: are going to be fixed in the next commit. > sql-tap/alias.test.lua sql-tap/check.test.lua > sql-tap/func5.test.lua sql-tap/lua_sql.test.lua > sql-tap/subquery.test.lua sql-tap/trigger9.test.lua > sql/errinj.result sql/errinj.test.lua > sql/func-recreate.test.lua >=20 > Part of #2200, #4113, #2233 > --- >=20 > diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h > index 114ac0e4b..6ef1a1d4a 100644 > --- a/src/box/sql/sqlInt.h > +++ b/src/box/sql/sqlInt.h > @@ -1244,78 +1211,12 @@ struct type_def { > }; >=20 > @@ -4516,9 +4337,62 @@ Expr *sqlExprForVectorField(Parse *, Expr *, = int); > */ > extern int sqlSubProgramsRemaining; >=20 > -/** Register built-in functions to work with ANALYZE data. */ > -void > -sql_register_analyze_builtins(void); > +struct func_sql_builtin { > + /** Function object base class. */ > + struct func base; > + /** > + * A bitmask representing all supported function > + * overloads. The function supports argc =3D=3D n iff this > + * bitmask has bit n set 1. In particular case, a bitmask > + * (~0) means this function works with any possible > + * argument. > + * > + * The count of arguments for function is limited with > + * (CHAR_BITS*sizeof(uint64_t) - 1). When the highest bit > + * of the mask is set, this means that greater values > + * are supported. E.g. greatest function works correctly > + * with any number of input arguments. > + */ > + uint64_t signature_mask; > + /** A bitmask of SQL flags. */ > + uint16_t flags; > + /** User data to pass in call method. */ > + void *user_data; > + /** A call method for a function. */ Add explanation why we don=E2=80=99t use basic (i.e. call method of base class) =E2=80=9Ccall=E2=80=9D method. Furthermore, built-ins are called bypassing func_access_check() function. This should be either documented or fixed. > + void (*call)(sql_context *ctx, int argc, sql_value **argv); > + /** Finalize method (only for aggregate function). */ > + void (*finalize)(sql_context *ctx); We hold aggregate attribute in func_def; I guess lua/C aggregate are not going to be implemented soon (or someday at all); so, considering these points, why not to move this method to the base vtab? > +}; > + > +/** > + * A SQL method to find a function in a hash by it's name and Nit: it=E2=80=99s (it is) -> its. > + * count of arguments. Only functions that have 'SQL' engine > + * export field set true and have exactly the same signature > + * are returned. > + * > + * Returns not NULL function pointer when a valid and exported > + * to SQL engine function was found and NULL otherwise. Nit: was found -> is found. > + */ > +struct func * > +sql_func_by_signature(const char *name, uint32_t argc); > + > +/** > + * Test whether SQL-specific flag is set for given function. > + * Currently only SQL Builtin Functions have such hint flags, > + * so function returns false for other functions. Such approach > + * decreases code complexity and allows do not distinguish > + * functions by implementation details where it is unnecessary. > + * > + * Returns true when given flag is set for a given function and > + * false otherwise. > + */ > +static inline bool > +sql_func_flag_test(struct func *func, uint16_t flag) At least I would rename it to sql_func_test_flag() or sql_builtin_flag_is_set() or smth like that. > +{ > + if (func->def->language !=3D FUNC_LANGUAGE_SQL_BUILTIN) > + return false; > + return (((struct func_sql_builtin *)func)->flags & flag) !=3D 0; > +} >=20 > diff --git a/src/box/sql/vdbeInt.h b/src/box/sql/vdbeInt.h > index f77c019fb..f085477c1 100644 > --- a/src/box/sql/vdbeInt.h > +++ b/src/box/sql/vdbeInt.h >=20 > @@ -516,7 +523,17 @@ int sqlVdbeMemNumerify(Mem *); > int sqlVdbeMemCast(Mem *, enum field_type type); > int sqlVdbeMemFromBtree(BtCursor *, u32, u32, Mem *); > void sqlVdbeMemRelease(Mem * p); > -int sqlVdbeMemFinalize(Mem *, FuncDef *); > + > +/** > + * Memory cell pMem contains the context of an aggregate function. Nit: pMem -> mem. > + * This routine calls the finalize method for that function. The > + * result of the aggregate is stored back into pMem. > + * > + * Returns -1 if the finalizer reports an error. 0 otherwise. > + */ > +int > +sql_vdbemem_finalize(struct Mem *mem, struct func *func); > + > const char *sqlOpcodeName(int); > int sqlVdbeMemGrow(Mem * pMem, int n, int preserve); > int sqlVdbeMemClearAndResize(Mem * pMem, int n); >=20 > diff --git a/src/box/sql/analyze.c b/src/box/sql/analyze.c > index bd52d12df..b68d86dfe 100644 > --- a/src/box/sql/analyze.c > +++ b/src/box/sql/analyze.c > @@ -263,7 +263,7 @@ stat4Destructor(void *pOld) > * return value is BLOB, but it is really just a pointer to the = Stat4Accum > * object. > */ > -static void > +MAYBE_UNUSED static void Is this change really related to the patch? Same for all other added MAYBE_UNUSED modifiers. > statInit(sql_context * context, int argc, sql_value ** argv) > { > Stat4Accum *p; > @@ -535,7 +535,7 @@ samplePushPrevious(Stat4Accum * p, int iChng) > * > * The R parameter is only used for STAT4 > */ > -static void > +MAYBE_UNUSED static void > statPush(sql_context * context, int argc, sql_value ** argv) > { > int i; > @@ -608,7 +608,7 @@ statPush(sql_context * context, int argc, = sql_value ** argv) > * The content to returned is determined by the parameter J > * which is one of the STAT_GET_xxxx values defined above. > */ > -static void > +MAYBE_UNUSED static void > statGet(sql_context * context, int argc, sql_value ** argv) > { > Stat4Accum *p =3D (Stat4Accum *) sql_value_blob(argv[0]); > @@ -715,11 +715,11 @@ callStatGet(Vdbe * v, int regStat4, int iParam, = int regOut) > { > assert(regOut !=3D regStat4 && regOut !=3D regStat4 + 1); > sqlVdbeAddOp2(v, OP_Integer, iParam, regStat4 + 1); > - struct FuncDef *func =3D > - sqlFindFunction(sql_get(), "_sql_stat_get", 2, 0); > + struct func *func =3D > + sql_func_by_signature("_sql_stat_get", 2); > assert(func !=3D NULL); Instead of asserting func existence let=E2=80=99s raise an error: now user can remove built-in function from hash - there=E2=80=99s no protection to avoid such cases. Note that such opportunity (removing built-ins from cache) has been introduced by moving built-ins to the global func cache. In this case, assertion will fail in debug mode and lead to unpredictable consequences in release mode. Another option is to introduce mentioned protection, i.e. disallow user to delete functions which are declared with FUNC_LANGUAGE_SQL_BUILTIN flag. > diff --git a/src/box/sql/expr.c b/src/box/sql/expr.c > index 1f9d91705..64b3bc835 100644 > --- a/src/box/sql/expr.c > +++ b/src/box/sql/expr.c >=20 > @@ -3991,8 +3988,9 @@ sqlExprCodeTarget(Parse * pParse, Expr * pExpr, = int target) > nFarg =3D pFarg ? pFarg->nExpr : 0; > assert(!ExprHasProperty(pExpr, EP_IntValue)); > zId =3D pExpr->u.zToken; > - pDef =3D sqlFindFunction(db, zId, nFarg, 0); > - if (pDef =3D=3D 0 || pDef->xFinalize !=3D 0) { > + struct func *func =3D sql_func_by_signature(zId, = nFarg); > + if (func =3D=3D NULL || > + func->def->aggregate =3D=3D = FUNC_AGGREGATE_GROUP) { Why do we need check on aggregation type? > @@ -4128,12 +4118,15 @@ sqlExprCodeTarget(Parse * pParse, Expr * = pExpr, int target) > } else { > r1 =3D 0; > } > - if (pDef->funcFlags & SQL_FUNC_NEEDCOLL) { > + if (sql_func_flag_test(func, SQL_FUNC_NEEDCOLL)) = { > sqlVdbeAddOp4(v, OP_CollSeq, 0, 0, 0, > (char *)coll, = P4_COLLSEQ); > } > - sqlVdbeAddOp4(v, OP_BuiltinFunction0, constMask, = r1, > - target, (char *)pDef, = P4_FUNCDEF); > + assert(func->def->language =3D=3D > + FUNC_LANGUAGE_SQL_BUILTIN); > + int op =3D OP_BuiltinFunction0; > + sqlVdbeAddOp4(v, op, constMask, r1, target, > + (char *)func, P4_FUNC); > sqlVdbeChangeP5(v, (u8) nFarg); > if (nFarg && constMask =3D=3D 0) { > sqlReleaseTempRange(pParse, r1, nFarg); >=20 > diff --git a/src/box/sql/func.c b/src/box/sql/func.c > index 8e07ce892..f07c52b95 100644 > --- a/src/box/sql/func.c > +++ b/src/box/sql/func.c > @@ -1825,114 +1825,231 @@ groupConcatFinalize(sql_context * context) > } > + > +#define REG_FUNC(name, signature_mask, returns, flags, \ > + call, user_data, is_deterministic) \ > + {name, signature_mask, returns, flags, call, NULL, \ > + user_data, FUNC_AGGREGATE_NONE, is_deterministic} > + > +#define AGG_FUNC(name, signature_mask, returns, flags, \ > + call, finalize, user_data) \ > + {name, signature_mask, returns, flags, call, \ > + finalize, user_data, FUNC_AGGREGATE_GROUP, false} > +static struct func_vtab func_sql_builtin_vtab; > + > +struct func * > +func_sql_builtin_new(struct func_def *def) > { > + assert(def->language =3D=3D FUNC_LANGUAGE_SQL_BUILTIN); > + if (def->body !=3D NULL || def->is_sandboxed) { > + diag_set(ClientError, ER_CREATE_FUNCTION, def->name, > + "body and is_sandboxed options are not = compatible " > + "with SQL language=E2=80=9D); Why not move this check to func_def_new_from_tuple() ? > + return NULL; > + } > + func->flags =3D sql_builtins[idx].flags; > + func->user_data =3D sql_builtins[idx].user_data; > + func->call =3D sql_builtins[idx].call; > + func->finalize =3D sql_builtins[idx].finalize; > + func->signature_mask =3D sql_builtins[idx].signature_mask; > + func->base.vtab =3D &func_sql_builtin_vtab; > + func->base.def =3D def; > + Why not memcpy? > + def->is_deterministic =3D sql_builtins[idx].is_deterministic; > + def->returns =3D sql_builtins[idx].returns; > + def->aggregate =3D sql_builtins[idx].aggregate; > + def->exports.sql =3D true; > + return &func->base; > +} > + > +static struct func_vtab func_sql_builtin_vtab =3D { > + .call =3D sql_builtin_call_stub, > + .destroy =3D func_sql_builtin_destroy, > +}; >=20 > diff --git a/src/box/sql/resolve.c b/src/box/sql/resolve.c > index 0b90edd06..0c54afef6 100644 > --- a/src/box/sql/resolve.c > +++ b/src/box/sql/resolve.c > @@ -38,6 +38,9 @@ > #include "sqlInt.h" > #include > #include > +#include "box/func.h" > +#include "box/func_def.h" > +#include "box/schema.h" >=20 > /* > * Walk the expression tree pExpr and increase the aggregate function > @@ -596,27 +599,30 @@ resolveExprStep(Walker * pWalker, Expr * pExpr) > int is_agg =3D 0; /* True if is an = aggregate function */ > int nId; /* Number of characters in = function name */ > const char *zId; /* The function name. */ > - FuncDef *pDef; /* Information about the = function */ >=20 > assert(!ExprHasProperty(pExpr, EP_xIsSelect)); > zId =3D pExpr->u.zToken; > nId =3D sqlStrlen30(zId); > - pDef =3D sqlFindFunction(pParse->db, zId, n, 0); > - if (pDef =3D=3D 0) { > - pDef =3D > - sqlFindFunction(pParse->db, zId, = -2,0); > - if (pDef =3D=3D 0) { > + struct func *func =3D sql_func_by_signature(zId, = n); > + if (func =3D=3D NULL) { > + func =3D func_by_name(zId, strlen(zId)); > + if (func =3D=3D NULL || = !func->def->exports.sql) { > + func =3D NULL; > no_such_func =3D 1; > } else { > wrong_num_args =3D 1; > } > } else { > - is_agg =3D pDef->xFinalize !=3D 0; > - pExpr->type =3D pDef->ret_type; > + is_agg =3D func->def->language =3D=3D > + FUNC_LANGUAGE_SQL_BUILTIN && > + func->def->aggregate =3D=3D > + FUNC_AGGREGATE_GROUP;; Nit: double semicolon at the end of string. Can aggregate function be non-builtin? > + pExpr->type =3D func->def->returns; > const char *err =3D > "second argument to likelihood() = must "\ > "be a constant between 0.0 and = 1.0"; > - if (pDef->funcFlags & SQL_FUNC_UNLIKELY) = { > + if (sql_func_flag_test(func, > + = SQL_FUNC_UNLIKELY)) { > ExprSetProperty(pExpr, > EP_Unlikely | = EP_Skip); > if (n =3D=3D 2) { > @@ -643,21 +649,19 @@ resolveExprStep(Walker * pWalker, Expr * pExpr) > */ > /* TUNING: unlikely() = probability is 0.0625. likely() is 0.9375 */ > pExpr->iTable =3D > - pDef->zName[0] =3D=3D > - 'u' ? 8388608 : = 125829120; > + func->def->name[0] = =3D=3D 'u' ? > + 8388608 : 125829120; > } > } > - if (pDef-> > - funcFlags & (SQL_FUNC_CONSTANT | > - SQL_FUNC_SLOCHNG)) { > + if (func->def->is_deterministic || > + sql_func_flag_test(func, = SQL_FUNC_SLOCHNG)) { SQL_FUNC_SLOCHNG seems to be unused. So, you can simply remove it. > @@ -700,18 +704,14 @@ resolveExprStep(Walker * pWalker, Expr * pExpr) > pExpr->op2++; > pNC2 =3D pNC2->pNext; > } > - assert(pDef !=3D 0); > + assert(func !=3D NULL); > if (pNC2) { > + pNC2->ncFlags |=3D NC_HasAgg; > assert(SQL_FUNC_MINMAX =3D=3D > NC_MinMaxAgg); > - testcase((pDef-> > - funcFlags & > - SQL_FUNC_MINMAX) !=3D = 0); > - pNC2->ncFlags |=3D > - NC_HasAgg | (pDef-> > - funcFlags & > - = SQL_FUNC_MINMAX); > - > + if (sql_func_flag_test(func, > + = SQL_FUNC_MINMAX)) > + pNC2->ncFlags |=3D = NC_MinMaxAgg; > } > pNC->ncFlags |=3D NC_AllowAgg; > } > diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c > index 28552f64a..a66becc89 100644 > --- a/src/box/sql/vdbe.c > +++ b/src/box/sql/vdbe.c > @@ -41,6 +41,8 @@ > */ > #include "box/box.h" > #include "box/error.h" > +#include "box/func.h" > +#include "box/func_def.h" > #include "box/fk_constraint.h" > #include "box/txn.h" > #include "box/tuple.h" >=20 > @@ -1747,7 +1749,9 @@ case OP_BuiltinFunction: { > } > #endif > pCtx->is_aborted =3D false; > - (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: = R-24505-23230 */ > + assert(pCtx->func->def->language =3D=3D = FUNC_LANGUAGE_SQL_BUILTIN); > + (*((struct func_sql_builtin *)pCtx->func)->call)(pCtx, = pCtx->argc, > + pCtx->argv); >=20 > /* If the function returned an error, throw an exception */ > if (pCtx->is_aborted) >=20 > @@ -5055,7 +5059,9 @@ case OP_AggStep: { > pCtx->pOut =3D &t; > pCtx->is_aborted =3D false; > pCtx->skipFlag =3D 0; > - (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: = R-24505-23230 */ > + assert(pCtx->func->def->language =3D=3D = FUNC_LANGUAGE_SQL_BUILTIN); > + (*((struct func_sql_builtin *)pCtx->func)->call)(pCtx, = pCtx->argc, > + pCtx->argv); Nit: please, split it into two steps: type cast and function call. struct func_sql_builtin *func =3D =E2=80=A6; func->call(); > if (pCtx->is_aborted) { > sqlVdbeMemRelease(&t); > goto abort_due_to_error; >=20 > diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c > index 1aee3cf85..4ff8db621 100644 > --- a/src/box/sql/vdbeapi.c > +++ b/src/box/sql/vdbeapi.c > @@ -484,8 +484,9 @@ sql_step(sql_stmt * pStmt) > void * > sql_user_data(sql_context * p) > { > - assert(p && p->pFunc); > - return p->pFunc->pUserData; > + assert(p !=3D NULL && p->func !=3D NULL); > + assert(p->func->def->language =3D=3D FUNC_LANGUAGE_SQL_BUILTIN); > + return ((struct func_sql_builtin *)p->func)->user_data; user_data is used only for min/max functions. Let=E2=80=99s consider = removing it. > @@ -563,7 +564,9 @@ createAggContext(sql_context * p, int nByte) > void * > sql_aggregate_context(sql_context * p, int nByte) > { > - assert(p && p->pFunc && p->pFunc->xFinalize); > + assert(p !=3D NULL && p->func !=3D NULL && > + p->func->def->language =3D=3D FUNC_LANGUAGE_SQL_BUILTIN = && > + p->func->def->aggregate =3D=3D FUNC_AGGREGATE_GROUP); Please, split this assert into three ones. IMHO it would improve code readability. > diff --git a/src/box/sql/vdbemem.c b/src/box/sql/vdbemem.c > index b8c31ecec..8b2e816f2 100644 > --- a/src/box/sql/vdbemem.c > +++ b/src/box/sql/vdbemem.c > @@ -312,33 +312,28 @@ sqlVdbeMemStringify(Mem * pMem) > return 0; > } >=20 > -/* > - * Memory cell pMem contains the context of an aggregate function. > - * This routine calls the finalize method for that function. The > - * result of the aggregate is stored back into pMem. > - * > - * Return -1 if the finalizer reports an error. 0 otherwise. > - */ > int > -sqlVdbeMemFinalize(Mem * pMem, FuncDef * pFunc) > +sql_vdbemem_finalize(struct Mem *mem, struct func *func) > { > - if (ALWAYS(pFunc && pFunc->xFinalize)) { > + if (ALWAYS(func !=3D NULL && Please, move assert inside if condition to a separate stmt(s): diff --git a/src/box/sql/vdbemem.c b/src/box/sql/vdbemem.c index 8b2e816f2..958d67916 100644 --- a/src/box/sql/vdbemem.c +++ b/src/box/sql/vdbemem.c @@ -315,12 +315,12 @@ sqlVdbeMemStringify(Mem * pMem) int sql_vdbemem_finalize(struct Mem *mem, struct func *func) { - if (ALWAYS(func !=3D NULL && - func->def->language =3D=3D FUNC_LANGUAGE_SQL_BUILTIN = && - func->def->aggregate =3D=3D FUNC_AGGREGATE_GROUP)) { - sql_context ctx; - Mem t; - assert((mem->flags & MEM_Null) !=3D 0 || func =3D=3D = mem->u.func); + assert(func !=3D NULL); + assert(func->def->language =3D=3D FUNC_LANGUAGE_SQL_BUILTIN); + assert(func->def->language =3D=3D FUNC_AGGREGATE_GROUP); + sql_context ctx; + Mem t; + assert((mem->flags & MEM_Null) !=3D 0 || func =3D=3D = mem->u.func); ... > + func->def->language =3D=3D FUNC_LANGUAGE_SQL_BUILTIN = && > + func->def->aggregate =3D=3D FUNC_AGGREGATE_GROUP)) { > sql_context ctx; > Mem t; > - assert((pMem->flags & MEM_Null) !=3D 0 || pFunc =3D=3D = pMem->u.pDef); > + assert((mem->flags & MEM_Null) !=3D 0 || func =3D=3D = mem->u.func); > memset(&ctx, 0, sizeof(ctx)); > memset(&t, 0, sizeof(t)); > t.flags =3D MEM_Null; > - t.db =3D pMem->db; > + t.db =3D mem->db; > t.field_type =3D field_type_MAX; > ctx.pOut =3D &t; > - ctx.pMem =3D pMem; > - ctx.pFunc =3D pFunc; > - pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */ > - assert((pMem->flags & MEM_Dyn) =3D=3D 0); > - if (pMem->szMalloc > 0) > - sqlDbFree(pMem->db, pMem->zMalloc); > - memcpy(pMem, &t, sizeof(t)); > + ctx.pMem =3D mem; > + ctx.func =3D func; > + ((struct func_sql_builtin *)func)->finalize(&ctx); > + assert((mem->flags & MEM_Dyn) =3D=3D 0); > + if (mem->szMalloc > 0) > + sqlDbFree(mem->db, mem->zMalloc); > + memcpy(mem, &t, sizeof(t)); > if (ctx.is_aborted) > return -1; > }