Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial SpatiaLite ↔ PostGIS support #96

Merged
merged 90 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
90 commits
Select commit Hold shift + click to select a range
11e2bc3
Add initial SpatiaLite ↔ PostGIS support
mkgrgis Jan 11, 2024
db79d5a
Add initial SpatiaLite ↔ PostGIS support
mkgrgis Sep 30, 2024
d6534c7
Improve CI scripts supporting tests with PostGIS on CI Actions
mkgrgis Oct 7, 2024
15e1036
Merge branch 'master' into postgis_rewiews
mkgrgis Oct 7, 2024
3bc622b
Fix build process and pushing down
mkgrgis Oct 11, 2024
a0c2c57
Fix build spec Makefile
mkgrgis Oct 11, 2024
5745c92
Try to fix pushing down
mkgrgis Oct 11, 2024
55b643f
Simplify pushing predicate
mkgrgis Oct 11, 2024
38e1eb3
Try other predicate
mkgrgis Oct 11, 2024
feff025
Fix gcc warning
mkgrgis Oct 11, 2024
7db76ec
2nd trying to fix pushing prredicate
mkgrgis Oct 11, 2024
baf120f
3rd fix of pushable predicate
mkgrgis Oct 11, 2024
17c12f8
Fix malloc, improve comments
mkgrgis Oct 11, 2024
f1bdb98
Fix unused var
mkgrgis Oct 11, 2024
fbe71c9
4rd pushing predicate variant
mkgrgis Oct 11, 2024
9853c00
5th deparsing predicate variant
mkgrgis Oct 11, 2024
830bb38
Add PostGIS operator TCs
mkgrgis Oct 11, 2024
334ccae
Add other PostGIS TCs
mkgrgis Oct 11, 2024
035c3ca
Fix TC order
mkgrgis Oct 11, 2024
f8b8371
Add comments
mkgrgis Oct 11, 2024
54cfa4d
6th fix pushing predicate
mkgrgis Oct 11, 2024
919b5f6
7th ppushing predicate
mkgrgis Oct 11, 2024
3bc5b7a
8th pushing predicate
mkgrgis Oct 11, 2024
af4f06e
Revert pushdown prediacte
mkgrgis Oct 12, 2024
eddfd65
fix nogis tests
mkgrgis Oct 14, 2024
aab56af
Improve constant deparsing
mkgrgis Oct 15, 2024
996d375
Fix code style & behaviour without GIS support, add TCs
mkgrgis Oct 16, 2024
5e66258
Fix GIS constants deparsing
mkgrgis Oct 16, 2024
55408d5
Fix code style
mkgrgis Oct 16, 2024
1546cb9
Fix comment
mkgrgis Oct 16, 2024
7457e46
Expand PostGIS tests
mkgrgis Oct 16, 2024
2fedef4
Add expected result for current implementation
mkgrgis Oct 16, 2024
a61647f
Fix TC 41 comment
mkgrgis Oct 17, 2024
1a6adfe
Fix comments and TCs
mkgrgis Oct 17, 2024
c217d55
Fix file ini
mkgrgis Oct 17, 2024
b06159a
Fix nogis SQLite BLOB tests
mkgrgis Oct 18, 2024
bc5bef9
Update nogis test, commit for failed results
mkgrgis Oct 22, 2024
1d1f031
Fix nogis tests
mkgrgis Oct 22, 2024
178ac66
fix and expand tests
mkgrgis Oct 25, 2024
966ada5
Try other TCs
mkgrgis Oct 25, 2024
d9a0d5f
Fix postgis tests
mkgrgis Oct 25, 2024
98a6474
Add PostGIS tests
mkgrgis Oct 28, 2024
3f15b65
Fix PostGIS const deparsing
mkgrgis Oct 28, 2024
ca4351e
Add DEFINE branches to PostGIS const deparsing
mkgrgis Oct 28, 2024
2bd1a57
Reduce double calculation
mkgrgis Oct 28, 2024
348f228
Try other PostGIs const deparsing
mkgrgis Oct 28, 2024
27eb087
Fix extval
mkgrgis Oct 28, 2024
cc5f643
Fix function call
mkgrgis Oct 28, 2024
a120f2a
Add PostGIS TCs
mkgrgis Oct 28, 2024
89f1d10
Fix TC 61
mkgrgis Oct 28, 2024
9775492
Add new testing results
mkgrgis Oct 28, 2024
34265fe
Remove some operator tests
mkgrgis Oct 28, 2024
aedd651
Remove operators from result
mkgrgis Oct 28, 2024
84c925d
Add postgis TCs
mkgrgis Oct 29, 2024
8c5d712
Remove some built-in operators test
mkgrgis Oct 29, 2024
4a92806
Remove not unique operators from postgis test
mkgrgis Oct 29, 2024
6fa6224
Renumerate TCs
mkgrgis Oct 29, 2024
bacc068
Fix TCs
mkgrgis Oct 29, 2024
9b6f970
Fix expected
mkgrgis Oct 29, 2024
36d2b81
Fix comments, remove invalid TC
Oct 29, 2024
7d65e56
Fix expected
Oct 29, 2024
d086e3f
Add else if, sqlite_query.c
mkgrgis Oct 30, 2024
5e075a6
Fix `listed_datatype_oid` signature
Oct 30, 2024
74366ca
Fix spell
Oct 30, 2024
3e4e043
Fix `hexPrefix` legth for copy + `\0`
Oct 30, 2024
7aa5876
Fix text independence
Oct 31, 2024
a9a7d1d
Make separate `type` test for different ENABLE_GIS
Oct 31, 2024
70dec88
Fix test results path
Oct 31, 2024
da83c9c
Fix additional TCs
Oct 31, 2024
574a48d
Try to fix memory freeing
Oct 31, 2024
d31517a
Reduce double calculations
Oct 31, 2024
4da3346
Add `static` in signature, `sqlite_gis.c`
mkgrgis Nov 1, 2024
8ef9844
Add memory free call for SpatiaLite geom collection, sqlite_gis.c
mkgrgis Nov 8, 2024
68f9588
Try to use internal SpatiaLite cache
mkgrgis Nov 14, 2024
20d73d2
Add headers
mkgrgis Nov 14, 2024
fac9a43
Revert context usage
mkgrgis Nov 14, 2024
bba7648
Fix code style to SpatiaLite impl.
mkgrgis Nov 14, 2024
da25ebc
Reset tests before v2.5.0
mkgrgis Dec 12, 2024
524871f
Add v2.5.0 tests
mkgrgis Dec 12, 2024
8890cb1
Fix init data
mkgrgis Dec 12, 2024
eaeccb2
Revert "types_PostGIS" SQLite test table
mkgrgis Dec 12, 2024
18d31e4
Fix sqlite_fdw test
mkgrgis Dec 12, 2024
6c8fb13
Merge branch 'master' into postgis_2_5_0
mkgrgis Dec 12, 2024
26341be
Merge mac implementation from 'master' into postgis_2_5_0
mkgrgis Dec 20, 2024
d027a90
Remove GIS tests from extra (now in types)
mkgrgis Dec 20, 2024
009c5f7
Squash last 10 commits
mkgrgis Jan 7, 2025
88ef53c
By 2nd review: restore regress + improve docs
mkgrgis Jan 7, 2025
c53c281
Fix spell
mkgrgis Jan 7, 2025
11d62de
Fix typo by review, restore type_BOOLENAN in type_* tests, remove typ…
mkgrgis Jan 8, 2025
821a711
Fix inline doc in script
mkgrgis Jan 9, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ jobs:
- name: install SQLite, ${{ matrix.config }} mode
run: |
if [[ "${{ matrix.config }}" == "default" ]]; then
bash GitHubActions/install_sqlite.sh ${{ env.SQLITE_VERSION }} ${{ env.SQLITE_YEAR }}
bash GitHubActions/install_sqlite.sh ${{ env.SQLITE_VERSION }} ${{ env.SQLITE_YEAR }} "${{ matrix.config }}"
elif [[ "${{ matrix.config }}" == "postgis" ]]; then
bash GitHubActions/install_sqlite.sh ${{ env.SQLITE_VERSION }} ${{ env.SQLITE_YEAR }} --enable-rtree
bash GitHubActions/install_sqlite.sh ${{ env.SQLITE_VERSION }} ${{ env.SQLITE_YEAR }} "${{ matrix.config }}" --enable-rtree
fi

- name: build sqlite_fdw, ${{ matrix.config }} mode
Expand Down
98 changes: 98 additions & 0 deletions GIS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
GIS support in SQLite Foreign Data Wrapper for PostgreSQL
=========================================================

<img src="https://www.tmapy.cz/wp-content/uploads/2021/02/postgis-logo.png" align="center" height="80" alt="PostGIS"/> + <img src="https://www.gaia-gis.it/fossil/libspatialite/logo" align="center" height="80" alt="SpatiaLite"/>

SQLite FDW for PostgreSQL can connect PostgreSQL with or without [PostGIS](https://www.postgis.net/)
to [SpatiaLite](https://www.gaia-gis.it/fossil/libspatialite/index) SQLite database file.
This description contains only information about GIS support without common SQL and
system descriptions from [common FDW description](README.md).

Notes about compilation environment and PROJ library
----------------------------------------------------
Both SpatiaLite and PostGIS uses [PROJ C++ library](https://github.com/OSGeo/PROJ) from
[Open Source Geospatial Foundation](https://github.com/OSGeo) for reprojecting and some calculations.
Recommended configuration of SQLite FDW with GIS support contains both SpatiaLite and PostGIS.
**It is recommended to install only 1 version of PROJ in a system to avoid integration issue**.
Before compilation you should ensure equal versions of PROJ library required by SpatiaLite and PostGIS on
your system. Otherwise some encapsualted and unwanted memory freeing errors can occur.
If you need different PROJ library versions, you can reference [this PROJ issue](https://github.com/OSGeo/PROJ/issues/4361)
and try to use something like `CFLAGS="$CFLAGS -DPROJ_RENAME_SYMBOLS -O2"` during compilation of
SpatiaLite or PostGIS to link one of this extensions with other PROJ version.

Common conditions of GIS support
--------------------------------

1. SQLite FDW should be compiled with `ENABLE_GIS=1` environment variable value.
2. You must install SpatiaLite header files before compilation.
Linux packages like `libspatialite-dev` or `libspatialite-devel` can contain this files.
3. A column should have data type (domain) name from following list:
* addbandarg
* box2d
* box3d
* geography
* geometry
* geometry_dump
* geomval
* getfaceedges_returntype
* rastbandarg
* raster
* reclassarg
* summarystats
* topoelement
* topoelementarray
* topogeometry
* unionarg
* validatetopology_returntype

Only listed data types have full data transformation support:
* geography
* geometry

All other data types (domains) are treated as PostGIS specific, but unsupported.

You can use SpatiaLite GIS data support _without PostGIS installation_ after such
SQL commands as `CREATE DOMAIN geometry AS bytea;` and `CREATE DOMAIN geography AS bytea;`.
This allows to have in PostgreSQL PostGIS compatible `bytea` data easy
convertable to PostGIS storage format.

PostGIS and SpatiaLite vector data formats
-------------------------------------------

Vector GIS data in PostGIS can be stored in a columns with `geography` or `geometry`
data type. This columns contains a binary data.
[Well-known binary (WKB)](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry#Well-known_binary)
data storage format is a standard of [Open Geospatial Consortium (OGC)](https://en.wikipedia.org/wiki/Open_Geospatial_Consortium)
and supported by [GEOS library](https://libgeos.org). PostGIS internal GIS data
storage format based on WKB with [SRID](https://en.wikipedia.org/wiki/Spatial_reference_system#Identifiers)
additions. This format is known as [EWKB](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry#Format_variations) and supported by
[GEOS library](https://libgeos.org) and SpatiaLite input/output functions.

Hexadecimal text representation of EWKB data is a transport form for `geography`
and `geometry` GIS data between PostgreSQL and *SpatiLite input/output functions*.
Hence no PostGIS input/output functions are necessary, but all of this functions
are supported.

EWKB hexadecimal text data transport is faster than
[EWKT](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry)
but slower than EWKB blob data transport.

SpatiaLite internal storage based on `blob` data [affinity](https://www.sqlite.org/datatype3.html)
and is not a standard of [OGC](https://en.wikipedia.org/wiki/Open_Geospatial_Consortium).
Also this format doesn't supported by [GEOS library](https://libgeos.org).

Limitations
-----------

* In opposition to PostGIS, **SpatiaLite doesn't allow to store any GIS vector data without SRID**.
Hence any well-formed SpatiaLite data can be converted for PostGIS, but
well-formed PostGIS data _without SRID_ cannot be converted for SpatiaLite.
All of SpatiaLite input functions will return `NULL` in this case.
Please use [ST_SetSRID PostGIS function](https://postgis.net/docs/ST_SetSRID.html)
in case of incomplete SRID data to prepare PostGIS data for importing to SpatiaLite
or comparing with SpatiaLite data.

* Only `=` PostgreSQL operator is pushed down to SQLite (SpatiaLite) for vector GIS data such
as `geography` or `geometry`. `<>` PostgreSQL operator is NOT pushed down.

End of description.
4 changes: 2 additions & 2 deletions GitHubActions/build_postgis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
# This script downloads PostgreSQL from the official web site into ./workdir
# then builds it.
#
# Usage: ./build_postgres.sh pg_version postgis_version
# Usage: ./build_postgis.sh pg_version postgis_version
# pg_version is a PostgreSQL version to be installed like 16.0.
# postgis_version is a PostGIS version to be installed.
#
# Requirements
# - be able to connect to the PostgreSQL official web site by curl.
# - be able to connect to the PostGIS official web site by wget.
#
################################################################################

Expand Down
8 changes: 4 additions & 4 deletions GitHubActions/build_postgres.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#
################################################################################

VERSION=$1
POSTGRESQL_VERSION=$1
CONFIGURE_OPTIONS=""

while (( "$#" )); do
Expand All @@ -24,9 +24,9 @@ done

mkdir -p ./workdir
cd ./workdir
curl -O https://ftp.postgresql.org/pub/source/v${VERSION}/postgresql-${VERSION}.tar.bz2
tar xjf postgresql-${VERSION}.tar.bz2
cd postgresql-${VERSION}
curl -O https://ftp.postgresql.org/pub/source/v${POSTGRESQL_VERSION}/postgresql-${POSTGRESQL_VERSION}.tar.bz2
tar xjf postgresql-${POSTGRESQL_VERSION}.tar.bz2
cd postgresql-${POSTGRESQL_VERSION}

if [ -z "$CONFIGURE_OPTIONS" ]; then
./configure
Expand Down
2 changes: 1 addition & 1 deletion GitHubActions/build_sqlite_fdw.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# - SQLite development package is installed in a system.
################################################################################

VERSION=$1
VERSION="$1"
MODE="$2"

mkdir -p ./workdir/postgresql-${VERSION}/contrib/sqlite_fdw
Expand Down
3 changes: 1 addition & 2 deletions GitHubActions/execute_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ MODE="$2"
cd ./workdir/postgresql-${VERSION}/contrib/sqlite_fdw

if [ "$MODE" == "postgis" ]; then
MAKEFILE_OPT="ENABLE_GIS=1"
echo "$MODE mode, makefile option = $MAKEFILE_OPT"
export ENABLE_GIS=1

# Start postgres server
POSTGRES_HOME=/usr/local/pgsql
Expand Down
20 changes: 13 additions & 7 deletions GitHubActions/install_sqlite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,28 @@
# This sript downloads SQLite source code from the official web site into
# ./workdir then builds and installs it.
#
# Usage: ./install_sqlite.sh version year [configure_options]
# version: SQLite version to be installed
# Usage: ./install_sqlite.sh version year testing_mode [configure_options]
# version: SQLite version to be installed.
# year: A year of SQLite released. It is used for determining a download URL.
# configure_options are a list of option for sqlite server.
# testing_mode: 'default' or 'postgis' value.
# configure_options: are a list of option for sqlite server.
#
# Ex) ./install_sqlite.sh 3420000 2023 --enable-rtree
# Ex) ./install_sqlite.sh 3420000 2023 postgis --enable-rtree
#
# Requirements
# - be able to connect to the SQLite official web site by curl.
# - having superuser privileges
#
################################################################################

VERSION=$1
YEAR=$2
VERSION="$1"
YEAR="$2"
TESTING_MODE="$3"
t-kataym marked this conversation as resolved.
Show resolved Hide resolved

CONFIGURE_OPTIONS=""

while (( "$#" )); do
CONFIGURE_OPTIONS="$CONFIGURE_OPTIONS $3"
CONFIGURE_OPTIONS="$CONFIGURE_OPTIONS $4"
shift
done

Expand All @@ -42,3 +44,7 @@ fi

make
sudo make install

if [ "$TESTING_MODE" == "postgis" ]; then
sudo apt-get install libspatialite-dev -y
mkgrgis marked this conversation as resolved.
Show resolved Hide resolved
fi
22 changes: 19 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@
##########################################################################

MODULE_big = sqlite_fdw
OBJS = connection.o option.o deparse.o sqlite_query.o sqlite_fdw.o sqlite_data_norm.o
OBJS = connection.o option.o deparse.o sqlite_query.o sqlite_fdw.o sqlite_data_norm.o sqlite_gis.o
mkgrgis marked this conversation as resolved.
Show resolved Hide resolved

EXTENSION = sqlite_fdw
DATA = sqlite_fdw--1.0.sql sqlite_fdw--1.0--1.1.sql

ifdef ENABLE_GIS
override PG_CFLAGS += -DSQLITE_FDW_GIS_ENABLE
GISTEST=postgis
else
GISTEST=nogis
endif

ifndef REGRESS
REGRESS = extra/sqlite_fdw_post types/bitstring types/bool types/float4 types/float8 types/int4 types/int8 types/numeric types/macaddr types/macaddr8 types/out_of_range types/timestamp types/uuid extra/join extra/limit extra/aggregates extra/prepare extra/select_having extra/select extra/insert extra/update extra/encodings sqlite_fdw type aggregate selectfunc
REGRESS = extra/sqlite_fdw_post types/bitstring types/bool types/float4 types/float8 types/int4 types/int8 types/numeric types/$(GISTEST) types/macaddr types/macaddr8 types/out_of_range types/timestamp types/uuid extra/join extra/limit extra/aggregates extra/prepare extra/select_having extra/select extra/insert extra/update extra/encodings sqlite_fdw type_$(GISTEST) aggregate selectfunc
endif

REGRESS_OPTS = --encoding=utf8
Expand All @@ -33,6 +40,10 @@ endif

SHLIB_LINK := -lsqlite3

ifdef ENABLE_GIS
override SHLIB_LINK += -lspatialite
endif

ifdef USE_PGXS
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
Expand All @@ -43,7 +54,6 @@ endif
ifeq (,$(findstring $(MAJORVERSION), 13 14 15 16 17))
$(error PostgreSQL 13, 14, 15, 16 or 17 is required to compile this extension)
endif

else
subdir = contrib/sqlite_fdw
top_builddir = ../..
Expand All @@ -60,3 +70,9 @@ endif
REGRESS := $(addprefix $(REGRESS_PREFIX_SUB)/,$(REGRESS))
$(shell mkdir -p results/$(REGRESS_PREFIX_SUB)/extra)
$(shell mkdir -p results/$(REGRESS_PREFIX_SUB)/types)

ifdef ENABLE_GIS
check: temp-install
temp-install: EXTRA_INSTALL+=contrib/postgis
checkprep: EXTRA_INSTALL+=contrib/postgis
endif
Loading