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

JSON + JSONB operators -> and ->> pushing down implementation in WHERE + new SQLite err codes #109

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

mkgrgis
Copy link
Contributor

@mkgrgis mkgrgis commented Jan 10, 2025

In this PR about JSON operators support

  • Implement pushing down for -> and ->> operators for left both JSON and JSONB operand in WHERE clauses for both string and integer arguments. Based on https://sqlite.org/json1.html
  • Add types/json test case set showing -> and ->> behaviour for both JSON and JSONB for both SELECT and WHERE usage contexts
  • Add/restore JSONB missing support in existed code infrastructure
  • Switch SQLite error codes to more informative modern form instead of legacy codes. Based on https://sqlite.org/rescode.html#extended_result_code_list
  • Improve CI scripts: separate SQLite version will used for tests, not SQLite version from CI OS.
  • Full SQLite jsonb support.
  • Add auto_import test for FOREIGN IMPORT SCHEMA behaviour. This test have versions with GIS support and without GIS support.
  • Show || and substr pushing down in tests.
  • Add sqlite_fdw_sqlite_version() and sqlite_fdw_sqlite_code_source() SQL functions.

Motivation: osmium OSM→PostGIS tool generates OSM tags as JSON or JSONB data. Typical usage look like ... WHERE t->>'building' IS NOT NULL or ... WHERE t->>'access' = 'private'.

@mkgrgis mkgrgis mentioned this pull request Jan 10, 2025
@mkgrgis
Copy link
Contributor Author

mkgrgis commented Jan 10, 2025

Hello, pgspider team! There is some JSON support problem in CI. Could anyone diagnose?
I have no such problems with OS package

SQLite version 3.40.1 2022-12-28 14:03:47
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> select json_set('[0,1,2]','$[#]','new');
[0,1,2,"new"]

@lamdn1409
Copy link
Contributor

lamdn1409 commented Jan 23, 2025

Hello, pgspider team! There is some JSON support problem in CI. Could anyone diagnose? I have no such problems with OS package

SQLite version 3.40.1 2022-12-28 14:03:47
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> select json_set('[0,1,2]','$[#]','new');
[0,1,2,"new"]

@mkgrgis

SQLite version on CI is 3.46.0.
The test is OK on my environment (SQLite 3.45.3).
Could you try to change SQLite version on CI?

@mkgrgis
Copy link
Contributor Author

mkgrgis commented Jan 26, 2025

Could you try to change SQLite version on CI?

@lamdn1409 , thanks for proposition, but bc7515d unfortunately gives no result. Maybe we don't know something about compilation in CI environtment.

@mkgrgis
Copy link
Contributor Author

mkgrgis commented Jan 28, 2025

@lamdn1409 , I am still cannot explain the bad result. In the log there is checking whether to support JSON functions... yes line, also default JSON functions status as enabled documented and tested operators listed in documentation.
UPD: Look like Ubuntu have internal SQLite version, because CI can execute some tests without SQLite source code archive downloading. Hence we have version switching problem in case of types/json test.

@mkgrgis
Copy link
Contributor Author

mkgrgis commented Jan 29, 2025

@lamdn1409, in CI was problem of SQLite install: usual library directory is /usr/lib for Ububtu and Debian, but not usual /usr/local/lib. Unfortunately, this change didn't help to fix JSON test results. There are still checking whether to support JSON functions... yes and failed TCs with -> and ->> operators.

@mkgrgis
Copy link
Contributor Author

mkgrgis commented Feb 3, 2025

@lamdn1409 , please look at the state after ef6cae1 : here is no SQLite downloading, bull all tests except json are successfully! Hence SQLite downloading in CI is absolutely not effective for the environment.

@mkgrgis
Copy link
Contributor Author

mkgrgis commented Feb 3, 2025

@lamdn1409 , now there are successfully tests, please review a code.

@lamdn1409
Copy link
Contributor

@mkgrgis sorry for late reply, I had a long vacation.
Glad to know the issue was resolved. Could you tell me what root cause is?

@mkgrgis
Copy link
Contributor Author

mkgrgis commented Feb 6, 2025

@mkgrgis sorry for late reply, I had a long vacation.

I had too :-) My congratulations!

Glad to know the issue was resolved. Could you tell me what root cause is?

CI SQLite compilation script is not effective in complex with FDW compilation. There was wrong --prefix for Ubuntu, but this is not all. Our FDW compilation scripts uses system libsqlite, not downloaded and compiled - this is root cause.
My effective patch is only Ubuntu upgrading, not full FDW compilation normalizing against downloaded SQLite.

@mkgrgis
Copy link
Contributor Author

mkgrgis commented Feb 10, 2025

Ping, @lamdn1409 ?

@lamdn1409
Copy link
Contributor

lamdn1409 commented Feb 10, 2025

@mkgrgis Thanks for your information. I understood the root cause. However, I concern about the downloaded SQLite version maybe not used yet. I'm thinking about 02 solutions:

  1. Remove the existing SQLite, then install the downloaded SQLite into the system folder.
  2. Keep the existing SQLite, install the downloaded SQLite into a folder, then build SQLite FDW with that installed SQLite.
    How do you think?

@mkgrgis
Copy link
Contributor Author

mkgrgis commented Feb 10, 2025

I prefer the second variant, @lamdn1409:

  1. Keep the existing SQLite, install the downloaded SQLite into a folder, then build SQLite FDW with that installed SQLite.

But I don't think this is a subject of this PR.

@lamdn1409
Copy link
Contributor

lamdn1409 commented Feb 11, 2025

@mkgrgis

I prefer the second variant

me, too. However, let me discuss with our members first.

But I don't think this is a subject of this PR.

I think so, we need to separate this issue. Unfortunately, we cannot spend time to solve the issue at the moment. Could you please support to solve it?

@mkgrgis
Copy link
Contributor Author

mkgrgis commented Feb 11, 2025

@lamdn1409 , let's continue review of the code in this PR.

Could you please support to solve it?

No problems. This planned after pure_float fix and schemas support PR, but before returning to RETURNING PR. Will this ok?

I think here is something about PG_LDFLAGS.

@lamdn1409
Copy link
Contributor

lamdn1409 commented Feb 12, 2025

No problems. This planned after pure_float fix and schemas support PR, but before returning to RETURNING PR. Will this ok?

Thank you. Your plan is OK.

@lamdn1409
Copy link
Contributor

@mkgrgis

let's continue review of the code in this PR.

I finished reviewing the PR. Please confirm my comments.

@mkgrgis
Copy link
Contributor Author

mkgrgis commented Feb 12, 2025

Please confirm my comments.

Where are this comments @lamdn1409 ? Could you please give URL because I don't see anything here?

@mkgrgis mkgrgis requested a review from lamdn1409 February 12, 2025 13:48
@mkgrgis
Copy link
Contributor Author

mkgrgis commented Feb 13, 2025

@lamdn1409 , SQLite compilation autonomy was successfully used in current CI state. Do you think #112 can be closed now?

@mkgrgis mkgrgis changed the title JSON operators pushing down implementation + new SQLite err codes JSON operators -> and ->> pushing down implementation in WHERE + new SQLite err codes Feb 14, 2025
@lamdn1409
Copy link
Contributor

@lamdn1409 , SQLite compilation autonomy was successfully used in current CI state. Do you think #112 can be closed now?

@mkgrgis Please create a PR for reviewing, it's other issue.

@mkgrgis
Copy link
Contributor Author

mkgrgis commented Feb 14, 2025

@mkgrgis Please create a PR for reviewing, it's other issue.

@lamdn1409 , this problem was detected and resolved in this PR. Currently all tests use not system, but compiled and downloaded SQLite library. You can ensure this. CI Ubuntu is old, but SQLite is more newer.

@lamdn1409
Copy link
Contributor

@mkgrgis Please create a PR for reviewing, it's other issue.

@lamdn1409 , this problem was detected and resolved in this PR. Currently all tests use not system, but compiled and downloaded SQLite library. You can ensure this. CI Ubuntu is old, but SQLite is more newer.

@mkgrgis It's OK for me. I will review the changes about CI.

@mkgrgis
Copy link
Contributor Author

mkgrgis commented Feb 18, 2025

Some prepare steps for SQLite jsonb future support.

Could you please explain your implementation?

This is about #109 (comment). In this PR we cannot implement full SQLite jsonb support, but we can: filter WHERE, get or read SELECT, and partially write UPDATE. Unfortunately jsonb INSERT is very hard for this PR.

@mkgrgis
Copy link
Contributor Author

mkgrgis commented Feb 18, 2025

Some prepare steps for SQLite jsonb future support.

Could you please explain your implementation?

This is about #109 (comment). In this PR we cannot implement full SQLite jsonb support, but we can: filter WHERE, get or read SELECT, and partially write UPDATE. Unfortunately jsonb INSERT is very hard for this PR.

@mkgrgis mkgrgis changed the title JSON operators -> and ->> pushing down implementation in WHERE + new SQLite err codes JSON + JSONB operators -> and ->> pushing down implementation in WHERE + new SQLite err codes Feb 18, 2025
@mkgrgis
Copy link
Contributor Author

mkgrgis commented Feb 24, 2025

@lamdn1409 , FYI about SQLite JSONB an SQLite official software engineer Stephan Beal thinks we should use SQL jsonb() only and bind only char* values with text affinity. See https://sqlite.org/forum/forumpost/f958865005

@mkgrgis
Copy link
Contributor Author

mkgrgis commented Feb 25, 2025

@lamdn1409 , full SQLite JSONB support was implemented. I think TC 015 confirm binding without errors for a group of values. The solution is based on in-memory read-only SQLite connection where select jsonb('....') is executed. This is not very elegant solution, but this was recommended by SQLite official software engineer Stephan Beal.

@mkgrgis mkgrgis requested a review from lamdn1409 February 26, 2025 05:18
@mkgrgis
Copy link
Contributor Author

mkgrgis commented Feb 26, 2025

@lamdn1409 , are any problems in PR now?

@lamdn1409
Copy link
Contributor

@lamdn1409 , are any problems in PR now?

@mkgrgis How about this issue #109 (comment)?

@mkgrgis
Copy link
Contributor Author

mkgrgis commented Feb 27, 2025

@mkgrgis How about this issue #109 (comment)?

I need some help with -rpath, @lamdn1409. Could you please get SQLite version after your proposed values of environment variables by #109 (comment) ? In my cases this is OS SQLite version. You can use

Datum
sqlite_fdw_sqlite_version(PG_FUNCTION_ARGS)
{
	PG_RETURN_INT32(SQLITE_VERSION_NUMBER);
}

with

CREATE OR REPLACE FUNCTION sqlite_fdw_sqlite_version()
RETURNS int
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
COMMENT ON FUNCTION sqlite_fdw_sqlite_version()
IS 'Returns used SQLite code version';

@lamdn1409
Copy link
Contributor

lamdn1409 commented Feb 27, 2025

@mkgrgis I can get correct SQLite version with this modification on Makefile. Could you try it?

-SHLIB_LINK := -lsqlite3
+SHLIB_LINK := -L/home/vagrant/sqlite3.46/lib -lsqlite3
+PG_CPPFLAGS += -I/home/vagrant/sqlite3.46/include
+SELECT sqlite_version();
+ sqlite_version
+----------------
+        3046000
+(1 row)
+

My system SQLite still be 3.49.0

[vagrant@localhost sqlite_fdw]$ sqlite3
SQLite version 3.49.0 2025-02-06 11:55:18
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite>

Please note that sqlite3 in test.sh should be changed accordingly.

sqlite3 "$testdir/post.db" < sql/init_data/init_post.sql;
sqlite3 "$testdir/core.db" < sql/init_data/init_core.sql;
sqlite3 "$testdir/common.db" < sql/init_data/init.sql;
sqlite3 "$testdir/selectfunc.db" < sql/init_data/init_selectfunc.sql;

@mkgrgis
Copy link
Contributor Author

mkgrgis commented Feb 27, 2025

Thanks, @lamdn1409 , I am beginning this experiment. Please look at my new test libsqlite and 2 new functions. I think this should be included in the PR for CI SQLite version controlling.

@mkgrgis
Copy link
Contributor Author

mkgrgis commented Feb 27, 2025

@lamdn1409 , please look at current results. I think SQLite install process is successfully, we have normal -rpath. Also make info message give us normal -rpath, but tests gives impossible combination of newer SQlite version number and old functionality and code source:

 SELECT sqlite_fdw_sqlite_version();
  sqlite_fdw_sqlite_version 
 ---------------------------
-                   3037002
+                   3049000

but

 SELECT sqlite_fdw_sqlite_code_source();
                             sqlite_fdw_sqlite_code_source                             
 --------------------------------------------------------------------------------------
- 2025-02-06 11:55:18 4a7dd425dc2a0e5082a9049c9b4a9d4f199a71583d014c24b4cfe276c5a77cde
+ 2022-01-06 13:25:41 872ba256cbf61d9290b571c0e6d82a20c224ca3ad82971edc46b29818d5dalt1

and no JSON functions.

Maybe we have different library and header? See https://sqlite.org/c3ref/libversion.html
This document recommends

assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
assert( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,80)==0 );
assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );

@mkgrgis
Copy link
Contributor Author

mkgrgis commented Feb 27, 2025

@lamdn1409 , separate SQLite version for testing problem was resolved!
Based on your modifications I have found such Makefile configuration as

ifdef SQLITE_FOR_TESTING_DIR
SHLIB_LINK := -L$(SQLITE_FOR_TESTING_DIR)/lib -lsqlite3
PG_CFLAGS += -I$(SQLITE_FOR_TESTING_DIR)/include -Wl,-rpath,$(SQLITE_FOR_TESTING_DIR)/lib
else
SHLIB_LINK := -lsqlite3
endif

Please note additional -rpath, because otherwise header gives us a new SQLITE_VERSION_NUMBER, but old library by runtime paths gives old sqlite3_libversion_number() C call result.

…ite err codes
@lamdn1409
Copy link
Contributor

Please note additional -rpath, because otherwise header gives us a new SQLITE_VERSION_NUMBER, but old library by runtime paths gives old sqlite3_libversion_number() C call result.

It's great the issue was resolved. Congratulations.
I concern whether -L$(SQLITE_FOR_TESTING_DIR)/lib is still necessary?
This change may work?

ifdef SQLITE_FOR_TESTING_DIR
SHLIB_LINK := -Wl,-rpath,$(SQLITE_FOR_TESTING_DIR)/lib -lsqlite3
PG_CFLAGS += -I$(SQLITE_FOR_TESTING_DIR)/include 
else
SHLIB_LINK := -lsqlite3
endif

@mkgrgis
Copy link
Contributor Author

mkgrgis commented Feb 28, 2025

-L$(SQLITE_FOR_TESTING_DIR)/lib is still necessary

This parameter allows us to use (on linking ld step) library functions which is specified for not OS SQLite library version. Without this path we will not use any new C APIs from a new versions, only from OS libsqlite version. High stability of SQLite C APIs gives your illusion about unnecessary -L. How do you think @lamdn1409 ?

@mkgrgis mkgrgis requested a review from lamdn1409 February 28, 2025 04:00
@lamdn1409
Copy link
Contributor

This parameter allows us to use (on linking ld step) library functions which is specified for not OS SQLite library version. Without >this path we will not use any new C APIs from a new versions, only from OS libsqlite version

I got it. Thanks for explanation.
I have no more comments for this PR.

@mkgrgis
Copy link
Contributor Author

mkgrgis commented Feb 28, 2025

Thanks, @lamdn1409 for the great work! I was happy to work with you around of this PR. SQLite JSONB was really unexpected for me, but we was able to ensure full support of this data structure in the PR.

@mkgrgis
Copy link
Contributor Author

mkgrgis commented Mar 5, 2025

@t-kataym , could you please give some approximate dates of this PR review? I am having rebased drafts for other PRs, but some little changes after you review are still possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants