Skip to content

Commit

Permalink
initial import of aequitas code
Browse files Browse the repository at this point in the history
  • Loading branch information
JerrySievert committed Jan 22, 2024
0 parents commit 0f40d5d
Show file tree
Hide file tree
Showing 19 changed files with 881 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
Cpp11BracedListStyle: 'false'
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: 'true'
SpacesInSquareBrackets: 'true'
UseTab: Never
AlignConsecutiveAssignments: 'true'


...
54 changes: 54 additions & 0 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Aequitas Build and Test
on:
push:
pull_request:
jobs:
build-and-test:
strategy:
matrix:
os: [ubuntu-latest]
version: [REL_14_STABLE, REL_15_STABLE, REL_16_STABLE]
runs-on: ${{ matrix.os }}

steps:
- name: Test details
run: echo Build and test Aequitas on ${{ matrix.os }} with PostgreSQL ${{ matrix.version }} branch

- name: Checkout and build PostgreSQL code
run: |
sudo apt-get update -qq
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
sudo apt-get install -y build-essential libreadline-dev zlib1g-dev flex bison libxml2-dev libxslt-dev libssl-dev libxml2-utils xsltproc ccache pkg-config libc++-dev libc++abi-dev libglib2.0-dev libtinfo5 cmake libstdc++-12-dev
rm -rf postgres
git clone --branch ${{ matrix.version }} --single-branch --depth 1 https://github.com/postgres/postgres.git
pushd postgres
git branch
./configure --prefix=$PWD/inst/ --enable-cassert --enable-debug --with-openssl
make -j4 install
- name: Start Postgres
run: |
pushd postgres
cd inst/bin
./initdb -D data
./pg_ctl -D data -l logfile start
popd
- name: Checkout aequitas extension code
uses: actions/checkout@v2
with:
path: aequitas

- name: Build and test aequitas extension
id: regression-tests
run: |
export PATH="${PWD}/postgres/inst/bin:$PATH"
pushd aequitas
make install
make installcheck
popd
- name: Print regression.diffs if regression tests failed
if: failure() && steps.regression-tests.outcome != 'success'
run: |
cat aequitas/regression.diffs
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.so
*.o
.clangd

results

9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Copyright (c) 2024, Hydra Inc

Permission to use, copy, modify, and distribute this software and its documentation for any purpose, without fee, and without a written agreement is hereby granted, provided that the above copyright notice and this paragraph and the following two paragraphs appear in all copies.

IN NO EVENT SHALL Hydra Inc BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF Hydra Inc HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Hydra Inc SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN “AS IS” BASIS, AND Hydra Inc HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

Some parts of this code is Copyright (c) 2008-2016, Wojciech Muła.
34 changes: 34 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
.PHONY: lintcheck

SRCS = src/text.cpp
OBJS = $(subst .cpp,.o, $(SRCS))

MODULE_big = aequitas

PG_CPPFLAGS = -O3 -std=c++17 -I src/sse -g

UNAME_S := $(shell uname -m)
ifeq ($(UNAME_S),arm64)
PG_CPPFLAGS += -stdlib=libc++ -DHAVE_NEON_INSTRUCTIONS=1
endif
ifeq ($(UNAME_S),arm64)
PG_CPPFLAGS += -stdlib=libc++ -DHAVE_NEON_INSTRUCTIONS=1
endif
ifeq ($(UNAME_S),x86_64)
SHLIB_LINK += -lrt -std=c++17 -msse4.2
PG_CPPFLAGS += -msse4.2
endif

EXTENSION = aequitas
DATA = aequitas--1.0.0.sql
PGFILEDESC = "Aequitas - tools for testing equality"

PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)

REGRESS = equality strstr

include $(PGXS)

lintcheck:
clang-tidy $(SRCS) -- -I$(INCLUDEDIR) -I$(INCLUDEDIR_SERVER) -I$(PWD)/include -std=c++17
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Aequitas - Text Comparison for PostgreSQL

Aequitas is an extension for PostgreSQL that provides string comparisons using SSE4.2 on x86_64 platforms, and SWAR64 on aarch64 and arm64 platforms.

## Usage

After installation:

```sql
CREATE EXTENSION aequitas;
```

This will create a `schema` called aequitas that contains the comparison functions:

`aequitas.equals(a TEXT, b TEXT)` - returns `BOOLEAN`, `true` if there is an exact match, and `false` if there is not.

`aequitas.strstr(haystack TEXT, needle TEXT)` - returns `INTEGER`, the position of where the needle is found in the haystack, or `-1` if it is not found.
11 changes: 11 additions & 0 deletions aequitas--1.0.0.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE SCHEMA IF NOT EXISTS aequitas;

CREATE OR REPLACE FUNCTION aequitas.strstr(left TEXT, right TEXT)
RETURNS INTEGER
AS 'MODULE_PATHNAME', 'pg_strstr'
LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;

CREATE OR REPLACE FUNCTION aequitas.equals(left TEXT, right TEXT)
RETURNS BOOLEAN
AS 'MODULE_PATHNAME', 'pg_equals'
LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
5 changes: 5 additions & 0 deletions aequitas.control
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# compare extension
comment = 'tools for comparing strings'
default_version = '1.0.0'
module_pathname = '$libdir/aequitas'
relocatable = true
29 changes: 29 additions & 0 deletions expected/equality.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
CREATE EXTENSION aequitas;
-- no match
SELECT aequitas.equals('hello', 'world');
equals
--------
f
(1 row)

-- match
SELECT aequitas.equals('hello', 'hello');
equals
--------
t
(1 row)

-- match on a 16 byte boundary
SELECT aequitas.equals('1234567890123456', '1234567890123456');
equals
--------
t
(1 row)

-- no match when partial
SELECT aequitas.equals('123456', '12345');
equals
--------
f
(1 row)

44 changes: 44 additions & 0 deletions expected/strstr.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
CREATE EXTENSION aequitas;
ERROR: extension "aequitas" already exists
-- no match
SELECT aequitas.strstr('hello', 'world');
strstr
--------
-1
(1 row)

-- match with 0
SELECT aequitas.strstr('hello', 'hello');
strstr
--------
0
(1 row)

-- match on a 16 byte boundary
SELECT aequitas.strstr('1234567890123456', '1234567890123456');
strstr
--------
0
(1 row)

-- match when partial
SELECT aequitas.strstr('123456', '12345');
strstr
--------
0
(1 row)

-- needle found in haystack
SELECT aequitas.strstr('hello world', 'ello');
strstr
--------
1
(1 row)

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

13 changes: 13 additions & 0 deletions sql/equality.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE EXTENSION aequitas;

-- no match
SELECT aequitas.equals('hello', 'world');

-- match
SELECT aequitas.equals('hello', 'hello');

-- match on a 16 byte boundary
SELECT aequitas.equals('1234567890123456', '1234567890123456');

-- no match when partial
SELECT aequitas.equals('123456', '12345');
19 changes: 19 additions & 0 deletions sql/strstr.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CREATE EXTENSION aequitas;

-- no match
SELECT aequitas.strstr('hello', 'world');

-- match with 0
SELECT aequitas.strstr('hello', 'hello');

-- match on a 16 byte boundary
SELECT aequitas.strstr('1234567890123456', '1234567890123456');

-- match when partial
SELECT aequitas.strstr('123456', '12345');

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

-- haystack in needle not found
SELECT aequitas.strstr('ello', 'hello world');
25 changes: 25 additions & 0 deletions src/sse/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright (c) 2008-2016, Wojciech Muła
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11 changes: 11 additions & 0 deletions src/sse/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#define FORCE_INLINE inline __attribute__((always_inline))
#define MAYBE_UNUSED inline __attribute__((unused))

#if defined(HAVE_NEON_INSTRUCTIONS)
# include <arm_neon.h>
# define USE_SIMPLE_MEMCMP // for fixed-memcmp.cpp
#else
# include <immintrin.h>
#endif
Loading

0 comments on commit 0f40d5d

Please sign in to comment.