Skip to content

Commit 7dd46a8

Browse files
authored
Merge pull request #2 from hydradatabase/jd/incorrect-results-in-table
fix bug using with tables
2 parents 33ccfbc + a41bcf7 commit 7dd46a8

File tree

7 files changed

+100
-10
lines changed

7 files changed

+100
-10
lines changed

.github/workflows/build_and_test.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
name: pg_stringtheory Build and Test
22
on:
33
push:
4-
pull_request:
4+
paths:
5+
- '.github/**'
6+
- 'src/**'
7+
- 'expected/**'
8+
- 'sql/**'
9+
- '*.control'
10+
- '*.sql'
11+
- Makefile
512
jobs:
613
build-and-test:
714
strategy:
15+
fail-fast: false
816
matrix:
917
os: [ubuntu-latest]
1018
version: [REL_14_STABLE, REL_15_STABLE, REL_16_STABLE]

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
*.o
33
.clangd
44

5-
results
6-
5+
/results
6+
/regression.*

expected/equality.out

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CREATE EXTENSION stringtheory;
1+
CREATE EXTENSION IF NOT EXISTS stringtheory;
22
-- no match
33
SELECT stringtheory.equals('hello', 'world');
44
equals
@@ -27,3 +27,25 @@ SELECT stringtheory.equals('123456', '12345');
2727
f
2828
(1 row)
2929

30+
-- test equality in a CTE
31+
WITH a AS (SELECT md5(generate_series(1, 1000)::text) b)
32+
SELECT count(*) FROM a
33+
WHERE stringtheory.equals(b, md5('123'));
34+
count
35+
-------
36+
1
37+
(1 row)
38+
39+
-- test strstr in a table
40+
CREATE TEMPORARY TABLE stringtheory_test
41+
(a text);
42+
INSERT INTO stringtheory_test
43+
SELECT md5(generate_series(1,1000)::text);
44+
SELECT COUNT(*) FROM stringtheory_test
45+
WHERE stringtheory.equals(a, md5('123'));
46+
count
47+
-------
48+
1
49+
(1 row)
50+
51+
DROP TABLE stringtheory_test;

expected/strstr.out

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
CREATE EXTENSION stringtheory;
2-
ERROR: extension "stringtheory" already exists
1+
CREATE EXTENSION IF NOT EXISTS stringtheory;
2+
NOTICE: extension "stringtheory" already exists, skipping
33
-- no match
44
SELECT stringtheory.strstr('hello', 'world');
55
strstr
@@ -42,3 +42,25 @@ SELECT stringtheory.strstr('ello', 'hello world');
4242
-1
4343
(1 row)
4444

45+
-- test strstr in a CTE
46+
WITH a AS (SELECT md5(generate_series(1, 1000)::text) b)
47+
SELECT COUNT(*) FROM a
48+
WHERE stringtheory.strstr(b, '00') >= 0;
49+
count
50+
-------
51+
114
52+
(1 row)
53+
54+
-- test strstr in a table
55+
CREATE TEMPORARY TABLE stringtheory_test
56+
(a text);
57+
INSERT INTO stringtheory_test
58+
SELECT md5(generate_series(1,1000)::text);
59+
SELECT COUNT(*) FROM stringtheory_test
60+
WHERE stringtheory.strstr(a, '00') >= 0;
61+
count
62+
-------
63+
114
64+
(1 row)
65+
66+
DROP TABLE stringtheory_test;

sql/equality.sql

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CREATE EXTENSION stringtheory;
1+
CREATE EXTENSION IF NOT EXISTS stringtheory;
22

33
-- no match
44
SELECT stringtheory.equals('hello', 'world');
@@ -11,3 +11,20 @@ SELECT stringtheory.equals('1234567890123456', '1234567890123456');
1111

1212
-- no match when partial
1313
SELECT stringtheory.equals('123456', '12345');
14+
15+
-- test equality in a CTE
16+
WITH a AS (SELECT md5(generate_series(1, 1000)::text) b)
17+
SELECT count(*) FROM a
18+
WHERE stringtheory.equals(b, md5('123'));
19+
20+
-- test strstr in a table
21+
CREATE TEMPORARY TABLE stringtheory_test
22+
(a text);
23+
24+
INSERT INTO stringtheory_test
25+
SELECT md5(generate_series(1,1000)::text);
26+
27+
SELECT COUNT(*) FROM stringtheory_test
28+
WHERE stringtheory.equals(a, md5('123'));
29+
30+
DROP TABLE stringtheory_test;

sql/strstr.sql

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CREATE EXTENSION stringtheory;
1+
CREATE EXTENSION IF NOT EXISTS stringtheory;
22

33
-- no match
44
SELECT stringtheory.strstr('hello', 'world');
@@ -17,3 +17,20 @@ SELECT stringtheory.strstr('hello world', 'ello');
1717

1818
-- haystack in needle not found
1919
SELECT stringtheory.strstr('ello', 'hello world');
20+
21+
-- test strstr in a CTE
22+
WITH a AS (SELECT md5(generate_series(1, 1000)::text) b)
23+
SELECT COUNT(*) FROM a
24+
WHERE stringtheory.strstr(b, '00') >= 0;
25+
26+
-- test strstr in a table
27+
CREATE TEMPORARY TABLE stringtheory_test
28+
(a text);
29+
30+
INSERT INTO stringtheory_test
31+
SELECT md5(generate_series(1,1000)::text);
32+
33+
SELECT COUNT(*) FROM stringtheory_test
34+
WHERE stringtheory.strstr(a, '00') >= 0;
35+
36+
DROP TABLE stringtheory_test;

src/text.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ extern "C" {
22
#include "postgres.h"
33

44
#include "fmgr.h"
5+
#include "mb/pg_wchar.h"
6+
57
#if PG_VERSION_NUM >= 160000
68
#include "varatt.h"
79
#endif
@@ -73,7 +75,8 @@ Datum pg_strstr(PG_FUNCTION_ARGS) {
7375
}
7476

7577
/* Get the results from the simd functions. */
76-
size_t ret = fast_strstr(VARDATA(left), len_left, VARDATA(right), len_right);
78+
size_t ret =
79+
fast_strstr(VARDATA_ANY(left), len_left, VARDATA_ANY(right), len_right);
7780

7881
PG_RETURN_INT32(ret);
7982
}
@@ -98,7 +101,8 @@ Datum pg_equals(PG_FUNCTION_ARGS) {
98101
}
99102

100103
/* Get the results from the simd functions. */
101-
size_t ret = fast_strstr(VARDATA(left), len_left, VARDATA(right), len_right);
104+
size_t ret =
105+
fast_strstr(VARDATA_ANY(left), len_left, VARDATA_ANY(right), len_right);
102106

103107
/* If the result is 0, strings are equal. */
104108
PG_RETURN_BOOL(ret == 0);

0 commit comments

Comments
 (0)