Tarantool development patches archive
 help / color / mirror / Atom feed
From: Stanislav Zudin <szudin@tarantool.org>
To: tarantool-patches@freelists.org, vdavydov.dev@gmail.com
Cc: Stanislav Zudin <szudin@tarantool.org>
Subject: [PATCH 08/13] sql: aggregate sql functions support big int
Date: Fri, 15 Mar 2019 18:45:37 +0300	[thread overview]
Message-ID: <c2c406a367421658b988d0f3b056ac3c8f7060e4.1552663061.git.szudin@tarantool.org> (raw)
In-Reply-To: <cover.1552663061.git.szudin@tarantool.org>
In-Reply-To: <cover.1552663061.git.szudin@tarantool.org>

Makes sql functions avg() and sum() accept arguments with
values in the range [2^63, 2^64).
---
 src/box/sql/func.c    | 35 ++++++++++++++++++++++++++++++-----
 src/box/sql/sqlInt.h  |  3 +++
 src/box/sql/vdbeapi.c |  6 ++++++
 3 files changed, 39 insertions(+), 5 deletions(-)

diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index 8a8acc216..194dec252 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -1410,6 +1410,7 @@ struct SumCtx {
 	i64 cnt;		/* Number of elements summed */
 	u8 overflow;		/* True if integer overflow seen */
 	u8 approx;		/* True if non-integer value was input to the sum */
+	u8 is_unsigned;		/* True if value exceeded 2^63 */
 };
 
 /*
@@ -1433,16 +1434,38 @@ sumStep(sql_context * context, int argc, sql_value ** argv)
 	type = sql_value_numeric_type(argv[0]);
 	if (p && type != SQL_NULL) {
 		p->cnt++;
+		i64 v = 0;
+		bool is_signed = false;
+
 		if (type == SQL_INTEGER) {
-			i64 v = sql_value_int64(argv[0]);
+			v = sql_value_int64(argv[0]);
 			p->rSum += v;
-			if ((p->approx | p->overflow) == 0
-			    && sqlAddInt64(&p->iSum, true, v, true) != ATHR_SIGNED) {
-				p->overflow = 1;
-			}
+			is_signed = true;
+		} else if (type == SQL_UNSIGNED) {
+			v = sql_value_int64(argv[0]);
+			p->rSum += (u64)v;
+			is_signed = false;
 		} else {
 			p->rSum += sql_value_double(argv[0]);
 			p->approx = 1;
+			return;
+		}
+
+		/* proceed with the integer value */
+		if ((p->approx | p->overflow) == 0) {
+			enum arithmetic_result r = sqlAddInt64(&p->iSum,
+							       p->is_unsigned == 0,
+							       v, is_signed);
+			switch (r) {
+			case ATHR_SIGNED:
+				break;
+			case ATHR_UNSIGNED:
+				p->is_unsigned = 1;
+				break;
+			default:
+				p->overflow = 1;
+				break;
+			}
 		}
 	}
 }
@@ -1455,6 +1478,8 @@ sumFinalize(sql_context * context)
 	if (p && p->cnt > 0) {
 		if (p->overflow) {
 			sql_result_error(context, "integer overflow", -1);
+		} else if (p->is_unsigned) {
+			sql_result_uint64(context, (u64)p->iSum);
 		} else if (p->approx) {
 			sql_result_double(context, p->rSum);
 		} else {
diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h
index 7f8e3f04e..c43d8c193 100644
--- a/src/box/sql/sqlInt.h
+++ b/src/box/sql/sqlInt.h
@@ -495,6 +495,9 @@ sql_result_int(sql_context *, int);
 void
 sql_result_int64(sql_context *, sql_int64);
 
+void
+sql_result_uint64(sql_context *, sql_uint64);
+
 void
 sql_result_null(sql_context *);
 
diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c
index c3bdb6f86..6a3413954 100644
--- a/src/box/sql/vdbeapi.c
+++ b/src/box/sql/vdbeapi.c
@@ -451,6 +451,12 @@ sql_result_int64(sql_context * pCtx, i64 iVal)
 	sqlVdbeMemSetInt64(pCtx->pOut, iVal, false);
 }
 
+void
+sql_result_uint64(sql_context * pCtx, u64 iVal)
+{
+	sqlVdbeMemSetInt64(pCtx->pOut, iVal, iVal > INT64_MAX);
+}
+
 void
 sql_result_null(sql_context * pCtx)
 {
-- 
2.17.1

  parent reply	other threads:[~2019-03-15 15:45 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-15 15:45 [PATCH 00/13] sql: support -2^63 .. 2^64-1 integer type Stanislav Zudin
2019-03-15 15:45 ` [PATCH 01/13] sql: Convert big integers from string Stanislav Zudin
2019-03-25 15:10   ` [tarantool-patches] " n.pettik
2019-04-01 20:39     ` Stanislav Zudin
2019-04-02  7:27     ` Konstantin Osipov
2019-03-15 15:45 ` [PATCH 02/13] sql: make VDBE recognize big integers Stanislav Zudin
2019-03-25 15:11   ` [tarantool-patches] " n.pettik
2019-04-01 20:42     ` Stanislav Zudin
2019-04-02  7:38   ` [tarantool-patches] " Konstantin Osipov
2019-03-15 15:45 ` [PATCH 03/13] sql: removes unused function Stanislav Zudin
2019-03-25 15:11   ` [tarantool-patches] " n.pettik
2019-04-01 20:39     ` Stanislav Zudin
2019-03-15 15:45 ` [PATCH 04/13] sql: support big integers within sql binding Stanislav Zudin
2019-03-25 15:12   ` [tarantool-patches] " n.pettik
2019-04-01 20:42     ` Stanislav Zudin
2019-04-02  7:46     ` Konstantin Osipov
2019-04-02  7:44   ` [tarantool-patches] " Konstantin Osipov
2019-03-15 15:45 ` [PATCH 05/13] sql: removes redundant function Stanislav Zudin
2019-03-25 15:12   ` [tarantool-patches] " n.pettik
2019-03-15 15:45 ` [PATCH 06/13] sql: aux functions to support big integers Stanislav Zudin
2019-03-25 15:13   ` [tarantool-patches] " n.pettik
2019-03-15 15:45 ` [PATCH 07/13] sql: arithmetic functions " Stanislav Zudin
2019-03-25 15:13   ` [tarantool-patches] " n.pettik
2019-04-01 20:43     ` Stanislav Zudin
2019-04-02  7:54       ` Konstantin Osipov
2019-04-02  7:52     ` Konstantin Osipov
2019-03-15 15:45 ` Stanislav Zudin [this message]
2019-03-25 15:13   ` [tarantool-patches] Re: [PATCH 08/13] sql: aggregate sql functions support big int n.pettik
2019-04-01 20:43     ` Stanislav Zudin
2019-04-02  7:57   ` [tarantool-patches] " Konstantin Osipov
2019-03-15 15:45 ` [PATCH 09/13] sql: fixes errors Stanislav Zudin
2019-03-25 15:14   ` [tarantool-patches] " n.pettik
2019-03-15 15:45 ` [PATCH 10/13] sql: fixes an error in sqlSubInt64 Stanislav Zudin
2019-03-25 15:14   ` [tarantool-patches] " n.pettik
2019-03-15 15:45 ` [PATCH 11/13] sql: fixes an error in string to int64 conversion Stanislav Zudin
2019-03-25 15:14   ` [tarantool-patches] " n.pettik
2019-03-15 15:45 ` [PATCH 12/13] sql: fixes an error in uint64 to double casting Stanislav Zudin
2019-03-25 15:15   ` [tarantool-patches] " n.pettik
2019-03-15 15:45 ` [PATCH 13/13] sql: support -2^63 .. 2^64-1 integer type Stanislav Zudin
2019-03-25 15:25   ` [tarantool-patches] " n.pettik
2019-04-01 20:44     ` Stanislav Zudin
2019-03-25 15:10 ` [tarantool-patches] Re: [PATCH 00/13] " n.pettik
2019-04-01 20:38   ` Stanislav Zudin

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=c2c406a367421658b988d0f3b056ac3c8f7060e4.1552663061.git.szudin@tarantool.org \
    --to=szudin@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=vdavydov.dev@gmail.com \
    --subject='Re: [PATCH 08/13] sql: aggregate sql functions support big int' \
    /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