From 026593ad079e336ee742cae2b76913a10a33a5c0 Mon Sep 17 00:00:00 2001
From: Faizan Qazi
Date: Wed, 30 Aug 2023 16:13:24 +0000
Subject: [PATCH] sql: difference built-in as wrong return value type
Previously, when the difference built-in was added into
CRDB, the return type was text instead of integer. To
address this, this patch fixes the return type to
match Postgres.
Fixes: #109666
Release note (bug fix): The difference builtin had its
return type incorrectly set to a string instead of an integer.
---
docs/generated/sql/functions.md | 2 +-
pkg/sql/logictest/testdata/logic_test/fuzzystrmatch | 8 ++++----
pkg/sql/sem/builtins/builtins.go | 5 ++---
pkg/sql/sem/builtins/fixed_oids.go | 2 +-
4 files changed, 8 insertions(+), 9 deletions(-)
diff --git a/docs/generated/sql/functions.md b/docs/generated/sql/functions.md
index 3d1ff111e5bf..dd8316ed04fa 100644
--- a/docs/generated/sql/functions.md
+++ b/docs/generated/sql/functions.md
@@ -2769,7 +2769,7 @@ The output can be used to recreate a database.’
difference(source: string, target: string) → string | Convert two strings to their Soundex codes and then reports the number of matching code positions.
+difference(source: string, target: string) → int | Convert two strings to their Soundex codes and then reports the number of matching code positions.
| Immutable |
encode(data: bytes, format: string) → string | Encodes data using format (hex / escape / base64 ).
| Immutable |
diff --git a/pkg/sql/logictest/testdata/logic_test/fuzzystrmatch b/pkg/sql/logictest/testdata/logic_test/fuzzystrmatch
index 3454db635570..de2a2258b9ec 100644
--- a/pkg/sql/logictest/testdata/logic_test/fuzzystrmatch
+++ b/pkg/sql/logictest/testdata/logic_test/fuzzystrmatch
@@ -35,22 +35,22 @@ SELECT soundex('hello world!')
----
H464
-query TTT
+query TTI
SELECT soundex('Anne'), soundex('Ann'), difference('Anne', 'Ann');
----
A500 A500 4
-query TTT
+query TTI
SELECT soundex('Anne'), soundex('Andrew'), difference('Anne', 'Andrew');
----
A500 A536 2
-query TTT
+query TTI
SELECT soundex('Anne'), soundex('Margaret'), difference('Anne', 'Margaret');
----
A500 M626 0
-query TTTT
+query TTTI
SELECT soundex('Anne'), soundex(NULL), difference('Anne', NULL), difference(NULL, 'Bob');
----
A500 NULL NULL NULL
diff --git a/pkg/sql/sem/builtins/builtins.go b/pkg/sql/sem/builtins/builtins.go
index f995903c8919..38b9b0d5f1c6 100644
--- a/pkg/sql/sem/builtins/builtins.go
+++ b/pkg/sql/sem/builtins/builtins.go
@@ -28,7 +28,6 @@ import (
"math/rand"
"net"
"regexp/syntax"
- "strconv"
"strings"
"time"
"unicode"
@@ -3809,11 +3808,11 @@ value if you rely on the HLC for accuracy.`,
tree.FunctionProperties{Category: builtinconstants.CategoryString},
tree.Overload{
Types: tree.ParamTypes{{Name: "source", Typ: types.String}, {Name: "target", Typ: types.String}},
- ReturnType: tree.FixedReturnType(types.String),
+ ReturnType: tree.FixedReturnType(types.Int),
Fn: func(_ context.Context, _ *eval.Context, args tree.Datums) (tree.Datum, error) {
s, t := string(tree.MustBeDString(args[0])), string(tree.MustBeDString(args[1]))
diff := fuzzystrmatch.Difference(s, t)
- return tree.NewDString(strconv.Itoa(diff)), nil
+ return tree.NewDInt(tree.DInt(diff)), nil
},
Info: "Convert two strings to their Soundex codes and then reports the number of matching code positions.",
Volatility: volatility.Immutable,
diff --git a/pkg/sql/sem/builtins/fixed_oids.go b/pkg/sql/sem/builtins/fixed_oids.go
index fc100ecfe8b9..219652479ccd 100644
--- a/pkg/sql/sem/builtins/fixed_oids.go
+++ b/pkg/sql/sem/builtins/fixed_oids.go
@@ -1215,7 +1215,7 @@ var builtinOidsArray = []string{
1234: `array_positions(array: anyenum[], elem: anyenum) -> int[]`,
1235: `array_positions(array: tuple[], elem: tuple) -> int[]`,
1236: `soundex(source: string) -> string`,
- 1237: `difference(source: string, target: string) -> string`,
+ 1237: `difference(source: string, target: string) -> int`,
1238: `levenshtein(source: string, target: string) -> int`,
1239: `levenshtein(source: string, target: string, ins_cost: int, del_cost: int, sub_cost: int) -> int`,
1240: `json_remove_path(val: jsonb, path: string[]) -> jsonb`,
|