Tarantool development patches archive
 help / color / mirror / Atom feed
From: Mergen Imeev via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: vdavydov@tarantool.org
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH v2 5/5] sql: encapsulate SQL built-in functions opcodes
Date: Wed, 18 Aug 2021 17:35:05 +0300	[thread overview]
Message-ID: <a13316720d3ef4f168760713d8be72230d780324.1629297142.git.imeevma@gmail.com> (raw)
In-Reply-To: <cover.1629297142.git.imeevma@gmail.com>

This patch encapsulates opcodes for SQL built-in functions, which allows
us to modify the structure of SQL built-in functions and add an
ApplyType opcode if necessary.

Part of #6105
---
 src/box/sql/expr.c   |  8 +++-----
 src/box/sql/func.c   | 23 +++++++++++++++++++++++
 src/box/sql/select.c | 12 ++++--------
 src/box/sql/sqlInt.h |  8 ++++++++
 4 files changed, 38 insertions(+), 13 deletions(-)

diff --git a/src/box/sql/expr.c b/src/box/sql/expr.c
index 0544539d2..bf3ee359b 100644
--- a/src/box/sql/expr.c
+++ b/src/box/sql/expr.c
@@ -4165,14 +4165,12 @@ sqlExprCodeTarget(Parse * pParse, Expr * pExpr, int target)
 				sqlVdbeAddOp4(v, OP_CollSeq, 0, 0, 0,
 						  (char *)coll, P4_COLLSEQ);
 			}
-			struct func *func = sql_func_find(pExpr);
-			if (func == NULL) {
+			if (sql_emit_func_call(v, pExpr, OP_BuiltinFunction0,
+					       constMask, r1, target,
+					       nFarg) != 0) {
 				pParse->is_aborted = true;
 				break;
 			}
-			sqlVdbeAddOp4(v, OP_BuiltinFunction0, constMask, r1,
-				      target, (char *)func, P4_FUNC);
-			sqlVdbeChangeP5(v, (u8) nFarg);
 			if (nFarg && constMask == 0) {
 				sqlReleaseTempRange(pParse, r1, nFarg);
 			}
diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index 29235ed62..5b42fa767 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -2877,3 +2877,26 @@ sql_func_result(struct Expr *expr)
 	}
 	return field_type_MAX;
 }
+
+int
+sql_emit_func_call(struct Vdbe *vdbe, struct Expr *expr, int op, int mask,
+		   int r1, int r2, uint8_t argc)
+{
+	struct func *func = sql_func_find(expr);
+	if (func == NULL)
+		return -1;
+	sqlVdbeAddOp4(vdbe, op, mask, r1, r2, (char *)func, P4_FUNC);
+	sqlVdbeChangeP5(vdbe, argc);
+	return 0;
+}
+
+int
+sql_emit_func_finalize(struct Vdbe *vdbe, struct Expr *expr, int reg,
+		       uint8_t argc)
+{
+	struct func *func = sql_func_find(expr);
+	if (func == NULL)
+		return -1;
+	sqlVdbeAddOp4(vdbe, OP_AggFinal, reg, argc, 0, (char *)func, P4_FUNC);
+	return 0;
+}
diff --git a/src/box/sql/select.c b/src/box/sql/select.c
index 2f8a7a031..87f2012f1 100644
--- a/src/box/sql/select.c
+++ b/src/box/sql/select.c
@@ -5572,10 +5572,8 @@ finalizeAggFunctions(Parse * pParse, AggInfo * pAggInfo)
 	for (i = 0, pF = pAggInfo->aFunc; i < pAggInfo->nFunc; i++, pF++) {
 		ExprList *pList = pF->pExpr->x.pList;
 		assert(!ExprHasProperty(pF->pExpr, EP_xIsSelect));
-		sqlVdbeAddOp2(v, OP_AggFinal, pF->iMem,
-				  pList ? pList->nExpr : 0);
-		struct func *func = sql_func_find(pF->pExpr);
-		sqlVdbeAppendP4(v, func, P4_FUNC);
+		sql_emit_func_finalize(v, pF->pExpr, pF->iMem,
+				       pList ? pList->nExpr : 0);
 	}
 }
 
@@ -5634,10 +5632,8 @@ updateAccumulator(Parse * pParse, AggInfo * pAggInfo)
 			sqlVdbeAddOp4(v, OP_CollSeq, regHit, 0, 0,
 					  (char *)coll, P4_COLLSEQ);
 		}
-		sqlVdbeAddOp3(v, OP_AggStep0, 0, regAgg, pF->iMem);
-		struct func *func = sql_func_find(pF->pExpr);
-		sqlVdbeAppendP4(v, func, P4_FUNC);
-		sqlVdbeChangeP5(v, (u8) nArg);
+		sql_emit_func_call(v, pF->pExpr, OP_AggStep0, 0, regAgg,
+				   pF->iMem, nArg);
 		sql_expr_type_cache_change(pParse, regAgg, nArg);
 		sqlReleaseTempRange(pParse, regAgg, nArg);
 		if (addrNext) {
diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h
index 01701e271..5966f841f 100644
--- a/src/box/sql/sqlInt.h
+++ b/src/box/sql/sqlInt.h
@@ -4410,6 +4410,14 @@ sql_func_flags(uint8_t id);
 enum field_type
 sql_func_result(struct Expr *expr);
 
+int
+sql_emit_func_call(struct Vdbe *vdbe, struct Expr *expr, int op, int mask,
+		   int r1, int r2, uint8_t argc);
+
+int
+sql_emit_func_finalize(struct Vdbe *vdbe, struct Expr *expr, int reg,
+		       uint8_t argc);
+
 /**
  * Generate VDBE code to halt execution with correct error if
  * the object with specified key is already present (or doesn't
-- 
2.25.1


      parent reply	other threads:[~2021-08-18 14:37 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-18 14:34 [Tarantool-patches] [PATCH v2 0/5] Prepare for static arguments type check Mergen Imeev via Tarantool-patches
2021-08-18 14:34 ` [Tarantool-patches] [PATCH v2 1/5] sql: modify arithmetic aggregate functions Mergen Imeev via Tarantool-patches
2021-08-18 14:34 ` [Tarantool-patches] [PATCH v2 2/5] sql: introduce SQL built-in functions to parser Mergen Imeev via Tarantool-patches
2021-08-19  8:35   ` Vladimir Davydov via Tarantool-patches
2021-08-21  0:27     ` Safin Timur via Tarantool-patches
2021-08-18 14:35 ` [Tarantool-patches] [PATCH v2 3/5] sql: separate functions in parser Mergen Imeev via Tarantool-patches
2021-08-18 14:35 ` [Tarantool-patches] [PATCH v2 4/5] sql: separate function flags from functions Mergen Imeev via Tarantool-patches
2021-08-18 14:35 ` Mergen Imeev via Tarantool-patches [this message]

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=a13316720d3ef4f168760713d8be72230d780324.1629297142.git.imeevma@gmail.com \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=imeevma@tarantool.org \
    --cc=vdavydov@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v2 5/5] sql: encapsulate SQL built-in functions opcodes' \
    /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