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 mixed affinity UPDATE, unify UUID behaviour #95

Merged
merged 7 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
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
6 changes: 3 additions & 3 deletions README.md
mkgrgis marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ In OS `sqlite_fdw` works as executed code with permissions of user of PostgreSQL
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 @@ -542,7 +542,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 @@ -552,7 +552,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 @@ -164,6 +164,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);
mkgrgis marked this conversation as resolved.
Show resolved Hide resolved

/*
* Append remote name of specified foreign table to buf.
Expand Down Expand Up @@ -2107,6 +2108,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 @@ -2296,6 +2300,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
*/
mkgrgis marked this conversation as resolved.
Show resolved Hide resolved
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);
mkgrgis marked this conversation as resolved.
Show resolved Hide resolved
}
return sqlite_affinity_code(coltype);
}

/*
* deparse remote UPDATE statement
*
Expand Down Expand Up @@ -2346,7 +2379,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 @@ -2366,8 +2403,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)
mkgrgis marked this conversation as resolved.
Show resolved Hide resolved
appendStringInfo(buf, "strftime(");
mkgrgis marked this conversation as resolved.
Show resolved Hide resolved
}
sqlite_deparse_expr((Expr *) tle->expr, &context);
if (special_affinity)
appendStringInfoString(buf, ")");
}

sqlite_reset_transmission_modes(nestlevel);
Expand Down
Loading