Skip to content

Commit

Permalink
Merge branch 'draft_mac_support' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
mkgrgis authored Apr 12, 2024
2 parents 1953298 + 95a3e26 commit f25a62b
Show file tree
Hide file tree
Showing 12 changed files with 1,761 additions and 21 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/bitstring extra/bool extra/float4 extra/float8 extra/int4 extra/int8 extra/numeric extra/out_of_range 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 = extra/sqlite_fdw_post extra/bitstring extra/bool extra/float4 extra/float8 extra/int4 extra/int8 extra/numeric extra/macaddr6 extra/macaddr8 extra/out_of_range 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
78 changes: 77 additions & 1 deletion deparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ sqlite_is_valid_type(Oid type)
case TIMESTAMPOID:
case TIMESTAMPTZOID:
case UUIDOID:
case MACADDROID:
return true;
}
return false;
Expand Down Expand Up @@ -2098,6 +2099,24 @@ sqlite_deparse_column_ref(StringInfo buf, int varno, int varattno, PlannerInfo *
appendStringInfoString(buf, sqlite_quote_identifier(colname, '`'));
appendStringInfoString(buf, ")");
}
else if (!dml_context && pg_atttyp == MACADDROID)
{
elog(DEBUG2, "MAC address unification for \"%s\"", colname);
appendStringInfoString(buf, "sqlite_fdw_macaddr_blob(");
if (qualify_col)
ADD_REL_QUALIFIER(buf, varno);
appendStringInfoString(buf, sqlite_quote_identifier(colname, '`'));
appendStringInfoString(buf, ", 6)");
}
else if (!dml_context && pg_atttyp == MACADDR8OID)
{
elog(DEBUG2, "MAC address unification for \"%s\"", colname);
appendStringInfoString(buf, "sqlite_fdw_macaddr_blob(");
if (qualify_col)
ADD_REL_QUALIFIER(buf, varno);
appendStringInfoString(buf, sqlite_quote_identifier(colname, '`'));
appendStringInfoString(buf, ", 8)");
}
else
{
elog(DEBUG4, "column name without data unification = \"%s\"", colname);
Expand Down Expand Up @@ -2300,6 +2319,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);
}

/*
* Preferred SQLite affinity from "column_type" foreign column option
* SQLITE_NULL if no value or no normal value
Expand Down Expand Up @@ -2416,6 +2464,32 @@ sqlite_deparse_direct_update_sql(StringInfo buf, PlannerInfo *root,
appendStringInfo(buf, "sqlite_fdw_uuid_str(");
special_affinity = true;
}
if (pg_attyp == TIMESTAMPOID && preferred_affinity == SQLITE_INTEGER)
{
appendStringInfo(buf, "strftime(");
special_affinity = true;
}
if (pg_attyp == MACADDROID && preferred_affinity == SQLITE_INTEGER)
{
appendStringInfo(buf, "sqlite_fdw_macaddr_int(");
special_affinity = true;
}
if (pg_attyp == MACADDROID && preferred_affinity == SQLITE_TEXT)
{
appendStringInfo(buf, "sqlite_fdw_macaddr_str(");
special_affinity = true;
}
if (pg_attyp == MACADDR8OID && preferred_affinity == SQLITE_INTEGER)
{
appendStringInfo(buf, "sqlite_fdw_macaddr_int(");
special_affinity = true;
}
if (pg_attyp == MACADDR8OID && preferred_affinity == SQLITE_TEXT)
{
appendStringInfo(buf, "sqlite_fdw_macaddr_str(");
special_affinity = true;
}

sqlite_deparse_expr((Expr *) tle->expr, &context);

if (special_affinity)
Expand Down Expand Up @@ -2726,6 +2800,8 @@ sqlite_deparse_const(Const *node, deparse_expr_cxt *context, int showtype)
}
break;
case UUIDOID:
case MACADDROID:
case MACADDR8OID:
/* always deparse to BLOB because this is internal PostgreSQL storage
* the string for BYTEA always seems to be in the format "\\x##"
* where # is a hex digit, Even if the value passed in is
Expand All @@ -2740,7 +2816,7 @@ sqlite_deparse_const(Const *node, deparse_expr_cxt *context, int showtype)
for (i = 0; i < strlen(extval); i++)
{
char c = extval[i];
if ( c != '-' )
if ( c != '-' && c != '.' && c != ':')
appendStringInfoChar(buf, c);
}
appendStringInfo(buf, "\'");
Expand Down
18 changes: 7 additions & 11 deletions expected/12.16/extra/uuid.out
Original file line number Diff line number Diff line change
Expand Up @@ -506,20 +506,16 @@ SELECT * FROM "type_UUID+" WHERE "u" IS NOT NULL;
--Testcase 100:
CREATE FOREIGN TABLE "type_UUIDpk" (col uuid OPTIONS (key 'true')) SERVER sqlite_svr;
--Testcase 101:
ALTER FOREIGN TABLE "type_UUIDpk" ALTER COLUMN col OPTIONS (ADD column_type 'TEXT');
--Testcase 102:
INSERT INTO "type_UUIDpk" VALUES ('{a0eebc99-9c0b4ef8-bb6d6bb9-bd380a11}');
--Testcase 103:
--Testcase 102:
INSERT INTO "type_UUIDpk" VALUES ('{b0eebc99-9c0b4ef8-bb6d6bb9-bd380a12}');
--Testcase 103: ERR - primary key
INSERT INTO "type_UUIDpk" VALUES ('{b0eebc99-9c0b4ef8-bb6d6bb9-bd380a12}');
ERROR: failed to execute remote SQL: rc=19 UNIQUE constraint failed: type_UUIDpk.col
sql=INSERT INTO main."type_UUIDpk"(`col`) VALUES (?)
--Testcase 104:
SELECT * FROM "type_UUIDpk";
col
--------------------------------------
a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11
b0eebc99-9c0b-4ef8-bb6d-6bb9bd380a12
(2 rows)

--Testcase 105: ERR - primary key
ALTER FOREIGN TABLE "type_UUIDpk" ALTER COLUMN col OPTIONS (ADD column_type 'BLOB');
--Testcase 105: NO ERR, but the same semantics!
INSERT INTO "type_UUIDpk" VALUES ('{b0eebc99-9c0b4ef8-bb6d6bb9-bd380a12}');
ERROR: failed to execute remote SQL: rc=19 UNIQUE constraint failed: type_UUIDpk.col
sql=INSERT INTO main."type_UUIDpk"(`col`) VALUES (?)
Expand Down
Loading

0 comments on commit f25a62b

Please sign in to comment.