Tarantool development patches archive
 help / color / mirror / Atom feed
From: Nikita Pettik <korablev@tarantool.org>
To: Sergey Ostanevich <sergos@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: Re: [Tarantool-patches] [PATCH v3 18/20] box: introduce prepared statements
Date: Mon, 30 Dec 2019 12:27:26 +0200	[thread overview]
Message-ID: <20191230102726.GC29923@tarantool.org> (raw)
In-Reply-To: <20191225152354.GR19594@tarantool.org>

On 25 Dec 18:23, Sergey Ostanevich wrote:
> Hi!
> 
> Thanks for the patch, LGTM with just 2 nits below. 
> 
> Sergos
> 
> On 20 Dec 15:47, Nikita Pettik wrote:
> > This patch introduces local prepared statements. Support of prepared
> > statements in IProto protocol and netbox is added in the next patch.
> > 
> > Prepared statement is an opaque instance of SQL Virtual Machine. It can
> > be executed several times without necessity of query recompilation. To
> > achieve this one can use box.prepare(...) function. It takes string of
> > SQL query to be prepared; returns extended set of meta-information
> > including statement's ID, parameter's types and names, types and names
> > of columns of the resulting set, count of parameters to be bound.  Lua
> > object representing result of :prepare() invocation also features two
> > methods - :execute() and :unprepare(). They correspond to
> > box.execute(stmt.stmt_id) and box.unprepare(stmt.stmt_id), i.e.
> > automatically substitute string of prepared statement to be executed.
> > Statements are held in prepared statement cache - for details see
> > previous commit.  After schema changes all prepared statement located in
> > cache are considered to be expired - they must be re-prepared by
> > separate :prepare() call (or be invalidated with :unrepare()).
> > 
> > Two sessions can share one prepared statements. But in current
> > implementation if statement is executed by one session, another one is
> > not able to use it and will compile it from scratch and than execute.
> 
> It would be nice to mention plans on what should/will be done for
> resolution of this.

I'm going to file an issue to provide optimization for this.
I suppose we can copy VM before executing it. But now VM
consists of several parts which are allocated/deallocated as
separate chunks. So to achieve fast copy and resources finalization
for VM it would be nice to copy them into one chunk before execution.

> Also, my previous question on DDL during the
> execution of the statement is valid - one session can ruin execution
> of a prepared statement in another session with a DDL. Should there 
> be some guard for DDL until all executions of prep statements are finished?

I don't think it is good idea. Firstly, it would lock DB for any DDL
until there's at least one statemenet under execution (taking into
account that Tarantool as a DB is assumed to be under load all the time
it means in fact that DDL would not be allowed at all). Secondly, it is
documented behaviour, so users should be aware of it.

> > SQL cache memory limit is regulated by box{sql_cache_size} which can be
> > set dynamically. However, it can be set to the value which is less than
> > the size of current free space in cache (since otherwise some statements
> > can disappear from cache).
> > 
> > Part of #2592
> > ---
> >  src/box/errcode.h          |   1 +
> >  src/box/execute.c          | 114 ++++++++
> >  src/box/execute.h          |  16 +-
> >  src/box/lua/execute.c      | 213 +++++++++++++-
> >  src/box/lua/execute.h      |   2 +-
> >  src/box/lua/init.c         |   2 +-
> >  src/box/sql/prepare.c      |   9 -
> >  test/box/misc.result       |   3 +
> >  test/sql/engine.cfg        |   3 +
> >  test/sql/prepared.result   | 687 +++++++++++++++++++++++++++++++++++++++++++++
> >  test/sql/prepared.test.lua | 240 ++++++++++++++++
> >  11 files changed, 1267 insertions(+), 23 deletions(-)
> >  create mode 100644 test/sql/prepared.result
> >  create mode 100644 test/sql/prepared.test.lua
> > 
> > diff --git a/src/box/errcode.h b/src/box/errcode.h
> > index ee44f61b3..9e12f3a31 100644
> > --- a/src/box/errcode.h
> > +++ b/src/box/errcode.h
> > @@ -259,6 +259,7 @@ struct errcode_record {
> >  	/*204 */_(ER_SQL_FUNC_WRONG_RET_COUNT,	"SQL expects exactly one argument returned from %s, got %d")\
> >  	/*205 */_(ER_FUNC_INVALID_RETURN_TYPE,	"Function '%s' returned value of invalid type: expected %s got %s") \
> >  	/*206 */_(ER_SQL_PREPARE,		"Failed to prepare SQL statement: %s") \
> > +	/*207 */_(ER_WRONG_QUERY_ID,		"Prepared statement with id %u does not exist") \
> >  
> >  /*
> >   * !IMPORTANT! Please follow instructions at start of the file
> > diff --git a/src/box/execute.c b/src/box/execute.c
> > index 3bc4988b7..09224c23a 100644
> > --- a/src/box/execute.c
> > +++ b/src/box/execute.c
> > @@ -30,6 +30,7 @@
> >   */
> >  #include "execute.h"
> >  
> > +#include "assoc.h"
> >  #include "bind.h"
> >  #include "iproto_constants.h"
> >  #include "sql/sqlInt.h"
> > @@ -45,6 +46,8 @@
> >  #include "tuple.h"
> >  #include "sql/vdbe.h"
> >  #include "box/lua/execute.h"
> > +#include "box/sql_stmt_cache.h"
> > +#include "session.h"
> >  
> >  const char *sql_info_key_strs[] = {
> >  	"row_count",
> > @@ -413,6 +416,81 @@ port_sql_dump_msgpack(struct port *port, struct obuf *out)
> >  	return 0;
> >  }
> >  
> > +static bool
> > +sql_stmt_check_schema_version(struct sql_stmt *stmt)
> 
> The naming could be better - since you state something as true or false,
> it should be definitive, like sql_stmt_schema_version_is_valid()

Ok, let's rename it to sql_stmt_schema_version_is_valid()

> > +{
> > +	return sql_stmt_schema_version(stmt) == box_schema_version();
> > +}
> > +
> > +int
> > +sql_prepare(const char *sql, int len, struct port *port)
> > +{
> > +	uint32_t stmt_id = sql_stmt_calculate_id(sql, len);
> > +	struct sql_stmt *stmt = sql_stmt_cache_find(stmt_id);
> > +	if (stmt == NULL) {
> > +		if (sql_stmt_compile(sql, len, NULL, &stmt, NULL) != 0)
> > +			return -1;
> > +		if (sql_stmt_cache_insert(stmt) != 0) {
> > +			sql_stmt_finalize(stmt);
> > +			return -1;
> > +		}
> > +	} else {
> > +		if (! sql_stmt_check_schema_version(stmt)) {
> 
> The unaries should not be space-delimited as per C style $3.1
> https://www.tarantool.io/en/doc/2.2/dev_guide/c_style_guide/

Fixed.
 

  reply	other threads:[~2019-12-30 10:27 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-20 12:47 [Tarantool-patches] [PATCH v3 00/20] sql: " Nikita Pettik
2019-12-20 12:47 ` [Tarantool-patches] [PATCH v3 01/20] sql: remove sql_prepare_v2() Nikita Pettik
2019-12-23 14:03   ` Sergey Ostanevich
2019-12-24  0:51     ` Nikita Pettik
2019-12-27 19:18       ` Sergey Ostanevich
2019-12-20 12:47 ` [Tarantool-patches] [PATCH v3 02/20] sql: refactor sql_prepare() and sqlPrepare() Nikita Pettik
2019-12-24 11:35   ` Sergey Ostanevich
2019-12-20 12:47 ` [Tarantool-patches] [PATCH v3 03/20] sql: move sql_prepare() declaration to box/execute.h Nikita Pettik
2019-12-24 11:40   ` Sergey Ostanevich
2019-12-20 12:47 ` [Tarantool-patches] [PATCH v3 04/20] sql: rename sqlPrepare() to sql_stmt_compile() Nikita Pettik
2019-12-24 12:01   ` Sergey Ostanevich
2019-12-20 12:47 ` [Tarantool-patches] [PATCH v3 05/20] sql: rename sql_finalize() to sql_stmt_finalize() Nikita Pettik
2019-12-24 12:08   ` Sergey Ostanevich
2019-12-20 12:47 ` [Tarantool-patches] [PATCH v3 06/20] sql: rename sql_reset() to sql_stmt_reset() Nikita Pettik
2019-12-24 12:09   ` Sergey Ostanevich
2019-12-20 12:47 ` [Tarantool-patches] [PATCH v3 07/20] sql: move sql_stmt_finalize() to execute.h Nikita Pettik
2019-12-24 12:11   ` Sergey Ostanevich
2019-12-20 12:47 ` [Tarantool-patches] [PATCH v3 08/20] port: increase padding of struct port Nikita Pettik
2019-12-24 12:34   ` Sergey Ostanevich
2019-12-20 12:47 ` [Tarantool-patches] [PATCH v3 09/20] port: add result set format and request type to port_sql Nikita Pettik
2019-12-25 13:37   ` Sergey Ostanevich
2019-12-20 12:47 ` [Tarantool-patches] [PATCH v3 10/20] sql: resurrect sql_bind_parameter_count() function Nikita Pettik
2019-12-24 20:23   ` Sergey Ostanevich
2019-12-20 12:47 ` [Tarantool-patches] [PATCH v3 11/20] sql: resurrect sql_bind_parameter_name() Nikita Pettik
2019-12-24 20:26   ` Sergey Ostanevich
2019-12-20 12:47 ` [Tarantool-patches] [PATCH v3 12/20] sql: add sql_stmt_schema_version() Nikita Pettik
2019-12-25 13:37   ` Sergey Ostanevich
2019-12-20 12:47 ` [Tarantool-patches] [PATCH v3 13/20] sql: introduce sql_stmt_sizeof() function Nikita Pettik
2019-12-25 13:44   ` Sergey Ostanevich
2019-12-20 12:47 ` [Tarantool-patches] [PATCH v3 14/20] box: increment schema_version on ddl operations Nikita Pettik
2019-12-25 14:33   ` Sergey Ostanevich
2019-12-20 12:47 ` [Tarantool-patches] [PATCH v3 15/20] sql: introduce sql_stmt_query_str() method Nikita Pettik
2019-12-25 14:36   ` Sergey Ostanevich
2019-12-20 12:47 ` [Tarantool-patches] [PATCH v3 16/20] sql: move sql_stmt_busy() declaration to box/execute.h Nikita Pettik
2019-12-25 14:54   ` Sergey Ostanevich
2019-12-20 12:47 ` [Tarantool-patches] [PATCH v3 17/20] sql: introduce holder for prepared statemets Nikita Pettik
2019-12-23 20:54   ` Sergey Ostanevich
2019-12-20 12:47 ` [Tarantool-patches] [PATCH v3 18/20] box: introduce prepared statements Nikita Pettik
2019-12-25 15:23   ` Sergey Ostanevich
2019-12-30 10:27     ` Nikita Pettik [this message]
2019-12-30 14:15       ` Sergey Ostanevich
2019-12-20 12:47 ` [Tarantool-patches] [PATCH v3 19/20] netbox: " Nikita Pettik
2019-12-25 20:41   ` Sergey Ostanevich
2019-12-30  9:58     ` Nikita Pettik
2019-12-30 14:16       ` Sergey Ostanevich
2019-12-20 12:47 ` [Tarantool-patches] [PATCH v3 20/20] sql: add cache statistics to box.info Nikita Pettik
2019-12-25 20:53   ` Sergey Ostanevich
2019-12-30  9:46     ` Nikita Pettik
2019-12-30 14:23       ` Sergey Ostanevich
2019-12-30  1:13 ` [Tarantool-patches] [PATCH v3 00/20] sql: prepared statements Nikita Pettik
2019-12-31  8:39 ` Kirill Yukhin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191230102726.GC29923@tarantool.org \
    --to=korablev@tarantool.org \
    --cc=sergos@tarantool.org \
    --cc=tarantool-patches@dev.tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v3 18/20] box: introduce prepared statements' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox