Changeset: 15dfc4c975c3 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/15dfc4c975c3
Added Files:
sql/test/2025/Tests/to_hex.test
Modified Files:
monetdb5/modules/mal/calc.c
sql/ChangeLog.Dec2025
sql/scripts/49_strings.sql
sql/test/2025/Tests/All
sql/test/pg_regress/Tests/strings.test
Branch: Dec2025
Log Message:
Add functions to_hex(int) and to_hex(bigint)
They return the unsigned hexadecimal string representation of their
argument.
diffs (210 lines):
diff --git a/monetdb5/modules/mal/calc.c b/monetdb5/modules/mal/calc.c
--- a/monetdb5/modules/mal/calc.c
+++ b/monetdb5/modules/mal/calc.c
@@ -653,6 +653,39 @@ CALCmax(Client cntxt, MalBlkPtr mb, MalS
return MAL_SUCCEED;
}
+static str
+CALCto_hex_int(Client ctx, str *res, const int *n)
+{
+ allocator *ma = ctx->curprg->def->ma;
+ if (is_int_nil(*n)) {
+ *res = MA_STRDUP(ma, str_nil);
+ return MAL_SUCCEED;
+ }
+ const size_t size = 9; // 32 bits -> 8 hex digits + 1 NUL
+ str buf = ma_alloc(ma, size);
+ if (buf == NULL)
+ throw(MAL, "calc.to_hex", SQLSTATE(HY013)
MAL_MALLOC_FAIL);
+ snprintf(buf, size, "%" PRIx32, (uint32_t)*n);
+ *res = buf;
+ return MAL_SUCCEED;
+}
+
+static str
+CALCto_hex_lng(Client ctx, str *res, const lng *n)
+{
+ allocator *ma = ctx->curprg->def->ma;
+ if (is_lng_nil(*n)) {
+ *res = MA_STRDUP(ma, str_nil);
+ return MAL_SUCCEED;
+ }
+ const size_t size = 17; // 64 bits -> 16 hex digits + 1 NUL
+ str buf = ma_alloc(ma, size);
+ if (buf == NULL)
+ throw(MAL, "calc.to_hex", SQLSTATE(HY013)
MAL_MALLOC_FAIL);
+ snprintf(buf, size, "%" PRIx64, (uint64_t)*n);
+ *res = buf;
+ return MAL_SUCCEED;
+}
static str
CALCmax_no_nil(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
@@ -2580,6 +2613,8 @@ mel_func calc_init_funcs[] = {
command("calc", "ptr", CMDvarCONVERTptr, false, "Cast VALUE to ptr",
args(1,2, arg("",ptr),arg("v",ptr))),
pattern("calc", "ifthenelse", CALCswitchbit, false, "If VALUE is true return
MIDDLE else RIGHT", args(1,4,
argany("",1),arg("b",bit),argany("t",1),argany("f",1))),
command("calc", "length", CMDstrlength, false, "Length of STRING", args(1,2,
arg("",int),arg("s",str))),
+ command("calc", "to_hex", CALCto_hex_int, false, "convert to unsigned
hexadecimal number representation", args(1, 2, arg("", str), arg("n", int))),
+ command("calc", "to_hex", CALCto_hex_lng, false, "convert to unsigned
hexadecimal number representation", args(1, 2, arg("", str), arg("n", lng))),
pattern("aggr", "sum", CMDBATsum, false, "Calculate aggregate sum of B.",
args(1,2, arg("",bte),batarg("b",msk))),
pattern("aggr", "sum", CMDBATsum, false, "Calculate aggregate sum of B.",
args(1,3, arg("",bte),batarg("b",msk),arg("nil_if_empty",bit))),
pattern("aggr", "sum", CMDBATsum, false, "Calculate aggregate sum of B with
candidate list.", args(1,3, arg("",bte),batarg("b",msk),batarg("s",oid))),
diff --git a/sql/ChangeLog.Dec2025 b/sql/ChangeLog.Dec2025
--- a/sql/ChangeLog.Dec2025
+++ b/sql/ChangeLog.Dec2025
@@ -1,6 +1,10 @@
# ChangeLog file for sql
# This file is updated with Maddlog
+* Mon Nov 10 2025 Joeri van Ruth <[email protected]>
+- Add functions to_hex(int) and to_hex(bigint). They return the
+ unsigned hexadecimal string representation of their argument.
+
* Wed Nov 5 2025 Joeri van Ruth <[email protected]>
- COPY BINARY has been optimized to be much faster when many string columns
are involved.
diff --git a/sql/scripts/49_strings.sql b/sql/scripts/49_strings.sql
--- a/sql/scripts/49_strings.sql
+++ b/sql/scripts/49_strings.sql
@@ -94,3 +94,14 @@ grant execute on aggregate sha512 to pub
create aggregate ripemd160(val string)
returns string with order external name aggr.ripemd160;
grant execute on aggregate ripemd160 to public;
+
+-- to_hex returns the unsigned hexadecimal representation of the given number.
+-- Compatible with Postgres.
+
+create function to_hex(n int)
+returns text external name "calc"."to_hex";
+grant execute on function to_hex(int) to public;
+
+create function to_hex(n bigint)
+returns text external name "calc"."to_hex";
+grant execute on function to_hex(bigint) to public;
diff --git a/sql/test/2025/Tests/All b/sql/test/2025/Tests/All
--- a/sql/test/2025/Tests/All
+++ b/sql/test/2025/Tests/All
@@ -1,1 +1,2 @@
empty_bind_opt_on_sys_tables
+to_hex
diff --git a/sql/test/2025/Tests/to_hex.test b/sql/test/2025/Tests/to_hex.test
new file mode 100644
--- /dev/null
+++ b/sql/test/2025/Tests/to_hex.test
@@ -0,0 +1,93 @@
+# INT TESTS
+
+query T
+select to_hex(0)
+----
+0
+
+query T
+select to_hex(1)
+----
+1
+
+query T
+select to_hex(15)
+----
+f
+
+query T
+select to_hex(0x7fff_ffff)
+----
+7fffffff
+
+query T
+select to_hex(-1)
+----
+ffffffff
+
+query T
+select to_hex(-2_147_483_647)
+----
+80000001
+
+
+
+
+
+# BIGINT TESTS
+
+query T
+select to_hex(cast(0 as bigint))
+----
+0
+
+query T
+select to_hex(cast(1 as bigint))
+----
+1
+
+query T
+select to_hex(cast(15 as bigint))
+----
+f
+
+query T
+select to_hex(cast(0x7fff_ffff as bigint))
+----
+7fffffff
+
+query T
+select to_hex(cast(0x7fff_ffff_ffff_ffff as bigint))
+----
+7fffffffffffffff
+
+
+query T
+select to_hex(cast(-1 as bigint))
+----
+ffffffffffffffff
+
+query T
+select to_hex(-9_223_372_036_854_775_807)
+----
+8000000000000001
+
+
+
+
+# OTHER
+
+query T
+select to_hex(cast(-1 as tinyint))
+----
+ffffffff
+
+query T
+select to_hex(null)
+----
+NULL
+
+query T
+select to_hex(cast(null as bigint))
+----
+NULL
diff --git a/sql/test/pg_regress/Tests/strings.test
b/sql/test/pg_regress/Tests/strings.test
--- a/sql/test/pg_regress/Tests/strings.test
+++ b/sql/test/pg_regress/Tests/strings.test
@@ -660,11 +660,15 @@ select split_part('@joeuser@mydatabase@'
----
joeuser
-statement error
+query T
select to_hex(256*256*256 - 1) AS "ffffff"
+----
+ffffff
-statement error
-select to_hex(cast(256 as bigint)*cast(256 as bigint)*cast(256 as
bigint)*cast(256 as bigint) - 1) AS "ffffffff"
+query T
+select to_hex(cast(256 * 256 * 256 * 256 - 1 as bigint)) AS "ffffffff"
+----
+ffffffff
query I rowsort
select md5('') = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE"
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]