Skip to content

Commit

Permalink
Merge pull request #2 from hydradatabase/jd/incorrect-results-in-table
Browse files Browse the repository at this point in the history
fix bug using with tables
  • Loading branch information
JerrySievert authored Jan 25, 2024
2 parents 33ccfbc + a41bcf7 commit 7dd46a8
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 10 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
name: pg_stringtheory Build and Test
on:
push:
pull_request:
paths:
- '.github/**'
- 'src/**'
- 'expected/**'
- 'sql/**'
- '*.control'
- '*.sql'
- Makefile
jobs:
build-and-test:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
version: [REL_14_STABLE, REL_15_STABLE, REL_16_STABLE]
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
*.o
.clangd

results

/results
/regression.*
24 changes: 23 additions & 1 deletion expected/equality.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE EXTENSION stringtheory;
CREATE EXTENSION IF NOT EXISTS stringtheory;
-- no match
SELECT stringtheory.equals('hello', 'world');
equals
Expand Down Expand Up @@ -27,3 +27,25 @@ SELECT stringtheory.equals('123456', '12345');
f
(1 row)

-- test equality in a CTE
WITH a AS (SELECT md5(generate_series(1, 1000)::text) b)
SELECT count(*) FROM a
WHERE stringtheory.equals(b, md5('123'));
count
-------
1
(1 row)

-- test strstr in a table
CREATE TEMPORARY TABLE stringtheory_test
(a text);
INSERT INTO stringtheory_test
SELECT md5(generate_series(1,1000)::text);
SELECT COUNT(*) FROM stringtheory_test
WHERE stringtheory.equals(a, md5('123'));
count
-------
1
(1 row)

DROP TABLE stringtheory_test;
26 changes: 24 additions & 2 deletions expected/strstr.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE EXTENSION stringtheory;
ERROR: extension "stringtheory" already exists
CREATE EXTENSION IF NOT EXISTS stringtheory;
NOTICE: extension "stringtheory" already exists, skipping
-- no match
SELECT stringtheory.strstr('hello', 'world');
strstr
Expand Down Expand Up @@ -42,3 +42,25 @@ SELECT stringtheory.strstr('ello', 'hello world');
-1
(1 row)

-- test strstr in a CTE
WITH a AS (SELECT md5(generate_series(1, 1000)::text) b)
SELECT COUNT(*) FROM a
WHERE stringtheory.strstr(b, '00') >= 0;
count
-------
114
(1 row)

-- test strstr in a table
CREATE TEMPORARY TABLE stringtheory_test
(a text);
INSERT INTO stringtheory_test
SELECT md5(generate_series(1,1000)::text);
SELECT COUNT(*) FROM stringtheory_test
WHERE stringtheory.strstr(a, '00') >= 0;
count
-------
114
(1 row)

DROP TABLE stringtheory_test;
19 changes: 18 additions & 1 deletion sql/equality.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE EXTENSION stringtheory;
CREATE EXTENSION IF NOT EXISTS stringtheory;

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

-- no match when partial
SELECT stringtheory.equals('123456', '12345');

-- test equality in a CTE
WITH a AS (SELECT md5(generate_series(1, 1000)::text) b)
SELECT count(*) FROM a
WHERE stringtheory.equals(b, md5('123'));

-- test strstr in a table
CREATE TEMPORARY TABLE stringtheory_test
(a text);

INSERT INTO stringtheory_test
SELECT md5(generate_series(1,1000)::text);

SELECT COUNT(*) FROM stringtheory_test
WHERE stringtheory.equals(a, md5('123'));

DROP TABLE stringtheory_test;
19 changes: 18 additions & 1 deletion sql/strstr.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE EXTENSION stringtheory;
CREATE EXTENSION IF NOT EXISTS stringtheory;

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

-- haystack in needle not found
SELECT stringtheory.strstr('ello', 'hello world');

-- test strstr in a CTE
WITH a AS (SELECT md5(generate_series(1, 1000)::text) b)
SELECT COUNT(*) FROM a
WHERE stringtheory.strstr(b, '00') >= 0;

-- test strstr in a table
CREATE TEMPORARY TABLE stringtheory_test
(a text);

INSERT INTO stringtheory_test
SELECT md5(generate_series(1,1000)::text);

SELECT COUNT(*) FROM stringtheory_test
WHERE stringtheory.strstr(a, '00') >= 0;

DROP TABLE stringtheory_test;
8 changes: 6 additions & 2 deletions src/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ extern "C" {
#include "postgres.h"

#include "fmgr.h"
#include "mb/pg_wchar.h"

#if PG_VERSION_NUM >= 160000
#include "varatt.h"
#endif
Expand Down Expand Up @@ -73,7 +75,8 @@ Datum pg_strstr(PG_FUNCTION_ARGS) {
}

/* Get the results from the simd functions. */
size_t ret = fast_strstr(VARDATA(left), len_left, VARDATA(right), len_right);
size_t ret =
fast_strstr(VARDATA_ANY(left), len_left, VARDATA_ANY(right), len_right);

PG_RETURN_INT32(ret);
}
Expand All @@ -98,7 +101,8 @@ Datum pg_equals(PG_FUNCTION_ARGS) {
}

/* Get the results from the simd functions. */
size_t ret = fast_strstr(VARDATA(left), len_left, VARDATA(right), len_right);
size_t ret =
fast_strstr(VARDATA_ANY(left), len_left, VARDATA_ANY(right), len_right);

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

0 comments on commit 7dd46a8

Please sign in to comment.