Skip to content

Commit deebc56

Browse files
craig[bot]fqazi
craig[bot]
andcommitted
Merge #109731
109731: sql: difference built-in as wrong return value type r=fqazi a=fqazi 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. Co-authored-by: Faizan Qazi <[email protected]>
2 parents 6a51bf4 + 9fe824c commit deebc56

File tree

4 files changed

+8
-9
lines changed

4 files changed

+8
-9
lines changed

docs/generated/sql/functions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2739,7 +2739,7 @@ The swap_ordinate_string parameter is a 2-character string naming the ordinates
27392739
</span></td><td>Immutable</td></tr>
27402740
<tr><td><a name="decompress"></a><code>decompress(data: <a href="bytes.html">bytes</a>, codec: <a href="string.html">string</a>) &rarr; <a href="bytes.html">bytes</a></code></td><td><span class="funcdesc"><p>Decompress <code>data</code> with the specified <code>codec</code> (<code>gzip</code>, ‘lz4’, ‘snappy’, 'zstd).</p>
27412741
</span></td><td>Immutable</td></tr>
2742-
<tr><td><a name="difference"></a><code>difference(source: <a href="string.html">string</a>, target: <a href="string.html">string</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Convert two strings to their Soundex codes and then reports the number of matching code positions.</p>
2742+
<tr><td><a name="difference"></a><code>difference(source: <a href="string.html">string</a>, target: <a href="string.html">string</a>) &rarr; <a href="int.html">int</a></code></td><td><span class="funcdesc"><p>Convert two strings to their Soundex codes and then reports the number of matching code positions.</p>
27432743
</span></td><td>Immutable</td></tr>
27442744
<tr><td><a name="encode"></a><code>encode(data: <a href="bytes.html">bytes</a>, format: <a href="string.html">string</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Encodes <code>data</code> using <code>format</code> (<code>hex</code> / <code>escape</code> / <code>base64</code>).</p>
27452745
</span></td><td>Immutable</td></tr>

pkg/sql/logictest/testdata/logic_test/fuzzystrmatch

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,22 @@ SELECT soundex('hello world!')
3535
----
3636
H464
3737

38-
query TTT
38+
query TTI
3939
SELECT soundex('Anne'), soundex('Ann'), difference('Anne', 'Ann');
4040
----
4141
A500 A500 4
4242

43-
query TTT
43+
query TTI
4444
SELECT soundex('Anne'), soundex('Andrew'), difference('Anne', 'Andrew');
4545
----
4646
A500 A536 2
4747

48-
query TTT
48+
query TTI
4949
SELECT soundex('Anne'), soundex('Margaret'), difference('Anne', 'Margaret');
5050
----
5151
A500 M626 0
5252

53-
query TTTT
53+
query TTTI
5454
SELECT soundex('Anne'), soundex(NULL), difference('Anne', NULL), difference(NULL, 'Bob');
5555
----
5656
A500 NULL NULL NULL

pkg/sql/sem/builtins/builtins.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"math/rand"
2929
"net"
3030
"regexp/syntax"
31-
"strconv"
3231
"strings"
3332
"time"
3433
"unicode"
@@ -3819,11 +3818,11 @@ value if you rely on the HLC for accuracy.`,
38193818
tree.FunctionProperties{Category: builtinconstants.CategoryString},
38203819
tree.Overload{
38213820
Types: tree.ParamTypes{{Name: "source", Typ: types.String}, {Name: "target", Typ: types.String}},
3822-
ReturnType: tree.FixedReturnType(types.String),
3821+
ReturnType: tree.FixedReturnType(types.Int),
38233822
Fn: func(_ context.Context, _ *eval.Context, args tree.Datums) (tree.Datum, error) {
38243823
s, t := string(tree.MustBeDString(args[0])), string(tree.MustBeDString(args[1]))
38253824
diff := fuzzystrmatch.Difference(s, t)
3826-
return tree.NewDString(strconv.Itoa(diff)), nil
3825+
return tree.NewDInt(tree.DInt(diff)), nil
38273826
},
38283827
Info: "Convert two strings to their Soundex codes and then reports the number of matching code positions.",
38293828
Volatility: volatility.Immutable,

pkg/sql/sem/builtins/fixed_oids.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,7 @@ var builtinOidsArray = []string{
12151215
1234: `array_positions(array: anyenum[], elem: anyenum) -> int[]`,
12161216
1235: `array_positions(array: tuple[], elem: tuple) -> int[]`,
12171217
1236: `soundex(source: string) -> string`,
1218-
1237: `difference(source: string, target: string) -> string`,
1218+
1237: `difference(source: string, target: string) -> int`,
12191219
1238: `levenshtein(source: string, target: string) -> int`,
12201220
1239: `levenshtein(source: string, target: string, ins_cost: int, del_cost: int, sub_cost: int) -> int`,
12211221
1240: `json_remove_path(val: jsonb, path: string[]) -> jsonb`,

0 commit comments

Comments
 (0)