Skip to content

Commit

Permalink
Merge github.com:mkgrgis/sqlite_fdw into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
mkgrgis committed Jan 26, 2024
2 parents cee315a + a8a8c06 commit 608e71c
Show file tree
Hide file tree
Showing 47 changed files with 7,297 additions and 8,477 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ OBJS = connection.o option.o deparse.o sqlite_query.o sqlite_fdw.o sqlite_data_n
EXTENSION = sqlite_fdw
DATA = sqlite_fdw--1.0.sql sqlite_fdw--1.0--1.1.sql

REGRESS = extra/sqlite_fdw_post extra/float4 extra/float8 extra/int4 extra/int8 extra/numeric extra/join extra/limit extra/aggregates extra/prepare extra/select_having extra/select extra/insert extra/update extra/timestamp extra/encodings extra/bool extra/uuid sqlite_fdw type aggregate selectfunc
REGRESS = extra/sqlite_fdw_post extra/float4 extra/float8 extra/int4 extra/int8 extra/numeric extra/bitstring extra/bool extra/timestamp extra/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_OPTS = --encoding=utf8

SQLITE_LIB = sqlite3
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ SQLite `NULL` affinity always can be transparent converted for a nullable column

This option can disallow any write operations on foreign server user data througth SQLite file readonly access mode. This option driven only by foreign server owner role can not be overwritten by any `updatable` option value. This is a strong restiction by PostgreSQL foreign server owner user not to modify data in any foreign server tables. Also see [data modification access](#data-modification-access) about conditions of succesfully data modification.

- **truncatable** as *boolean*, optional, default *false*
- **truncatable** as *boolean*, optional, default *true*

Allows foreign tables to be truncated using the `TRUNCATE` command.

- **keep_connections** as *boolean*, optional, default *false*
- **keep_connections** as *boolean*, optional, default *true*

Allows to keep connections to SQLite while there is no SQL operations between PostgreSQL and SQLite.

Expand Down Expand Up @@ -228,7 +228,7 @@ About access model and possible data modifications problems see [data modificati
in SQLite (mixed affinity case). Updated and inserted values will have this affinity. Default preferred SQLite affinity for `timestamp` and `uuid` PostgreSQL data types is `text`.

- Use `INT` value for SQLite column (epoch Unix Time) to be treated/visualized as `timestamp` in PostgreSQL.
- Use `BLOB` value for SQLite column to be treated/visualized as `uuid` in PostgreSQL 14+.
- Use `BLOB` value for SQLite column to be treated/visualized as `uuid`.

- **key** as *boolean*, optional, default *false*

Expand Down Expand Up @@ -562,7 +562,7 @@ Limitations
### UUID values
- `sqlite_fdw` UUID values support exists only for `uuid` columns in foreign table. SQLite documentation recommends to store UUID as value with both `blob` and `text` [affinity](https://www.sqlite.org/datatype3.html). `sqlite_fdw` can pushdown both reading and filtering both `text` and `blob` values.
- Expected affinity of UUID value in SQLite table determined by `column_type` option of the column
for `INSERT` and `UPDATE` commands. In PostgreSQL 14- only `text` data affinity is availlable, PostgreSQL 14+ supports also `blob` data affinity.
for `INSERT` and `UPDATE` commands. PostgreSQL supports both `blob` and `text` [affinity](https://www.sqlite.org/datatype3.html).

### bit and varbit support
- `sqlite_fdw` PostgreSQL `bit`/`varbit` values support based on `int` SQLite data affinity, because there is no per bit operations for SQLite `blob` affinity data. Maximum SQLite `int` affinity value is 8 bytes length, hence maximum `bit`/`varbit` values length is 64 bits.
Expand All @@ -572,7 +572,7 @@ Tests
-----
Test directory have structure as following:

```sql
```
+---sql
| +---12.16
| | filename1.sql
Expand Down
56 changes: 56 additions & 0 deletions deparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ static void sqlite_get_relation_column_alias_ids(Var *node, RelOptInfo *foreignr
static char *sqlite_quote_identifier(const char *s, char q);
static bool sqlite_contain_immutable_functions_walker(Node *node, void *context);
static bool sqlite_is_valid_type(Oid type);
int preferred_sqlite_affinity (Oid relid, int varattno);

/*
* Append remote name of specified foreign table to buf.
Expand Down Expand Up @@ -2115,6 +2116,9 @@ sqlite_deparse_column_ref(StringInfo buf, int varno, int varattno, PlannerInfo *
}
}

/*
* Get column option with optionname for a variable attribute in deparsing context
*/
static char *
sqlite_deparse_column_option(int varno, int varattno, PlannerInfo *root, char *optionname)
{
Expand Down Expand Up @@ -2304,6 +2308,35 @@ sqlite_deparse_update(StringInfo buf, PlannerInfo *root,
}
}

/* Preferred SQLite affinity from "column_type" foreign column option
* SQLITE_NULL if no value or no normal value
*/
int
preferred_sqlite_affinity (Oid relid, int varattno)
{
char *coltype = NULL;
List *options;
ListCell *lc;

elog(DEBUG4, "sqlite_fdw : %s ", __func__);
if (varattno == 0)
return SQLITE_NULL;

options = GetForeignColumnOptions(relid, varattno);
foreach(lc, options)
{
DefElem *def = (DefElem *) lfirst(lc);

if (strcmp(def->defname, "column_type") == 0)
{
coltype = defGetString(def);
break;
}
elog(DEBUG4, "column type = %s", coltype);
}
return sqlite_affinity_code(coltype);
}

/*
* deparse remote UPDATE statement
*
Expand Down Expand Up @@ -2354,7 +2387,11 @@ sqlite_deparse_direct_update_sql(StringInfo buf, PlannerInfo *root,
forboth(lc, targetlist, lc2, targetAttrs)
{
int attnum = lfirst_int(lc2);
int preferred_affinity = SQLITE_NULL;
TargetEntry *tle;
RangeTblEntry *rte;
bool special_affinity = false;
Oid pg_attyp;
#if (PG_VERSION_NUM >= 140000)
tle = lfirst_node(TargetEntry, lc);

Expand All @@ -2374,8 +2411,27 @@ sqlite_deparse_direct_update_sql(StringInfo buf, PlannerInfo *root,
first = false;

sqlite_deparse_column_ref(buf, rtindex, attnum, root, false, true);

/* Get RangeTblEntry from array in PlannerInfo. */
rte = planner_rt_fetch(rtindex, root);
pg_attyp = get_atttype(rte->relid, attnum);
preferred_affinity = preferred_sqlite_affinity(rte->relid, attnum);

appendStringInfoString(buf, " = ");

special_affinity = (pg_attyp == UUIDOID && preferred_affinity == SQLITE3_TEXT) ||
(pg_attyp == TIMESTAMPOID && preferred_affinity == SQLITE_INTEGER);
if (special_affinity)
{
elog(DEBUG3, "sqlite_fdw : aff %d\n", preferred_affinity);
if (pg_attyp == UUIDOID && preferred_affinity == SQLITE3_TEXT)
appendStringInfo(buf, "sqlite_fdw_uuid_str(");
if (pg_attyp == TIMESTAMPOID && preferred_affinity == SQLITE_INTEGER)
appendStringInfo(buf, "strftime(");
}
sqlite_deparse_expr((Expr *) tle->expr, &context);
if (special_affinity)
appendStringInfoString(buf, ")");
}

sqlite_reset_transmission_modes(nestlevel);
Expand Down
Loading

0 comments on commit 608e71c

Please sign in to comment.