Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ include $(PGXS)
ifndef MAJORVERSION
MAJORVERSION := $(basename $(VERSION))
endif
ifeq (,$(findstring $(MAJORVERSION), 13 14 15 16 17))
$(error PostgreSQL 13, 14, 15, 16 or 17 is required to compile this extension)
ifeq (,$(findstring $(MAJORVERSION), 13 14 15 16 17 18 19))
$(error PostgreSQL 13, 14, 15, 16, 17, 18 or 19 is required to compile this extension)
endif
else
subdir = contrib/sqlite_fdw
Expand Down
2 changes: 2 additions & 0 deletions connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
#endif
#include "optimizer/cost.h"
#include "utils/builtins.h"
#include "utils/hsearch.h" /* HASHCTL, hash_create/search/seq_* */
#include "utils/inval.h"
#include "utils/tuplestore.h" /* tuplestore_putvalues */
#include "utils/syscache.h"


Expand Down
38 changes: 32 additions & 6 deletions deparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#include "catalog/pg_namespace.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h" /* INT2OID, FLOAT4OID, UUIDOID, etc. */
#include "access/htup_details.h" /* GETSTRUCT */
#include "access/transam.h" /* FirstGenbkiObjectId */
#if PG_VERSION_NUM >= 160000
#include "catalog/pg_ts_config.h"
#endif
Expand Down Expand Up @@ -406,6 +409,16 @@ sqlite_is_foreign_pathkey(PlannerInfo *root,
Oid oprid;
TypeCacheEntry *typentry;

#if PG_VERSION_NUM >= 180000
oprid = get_opfamily_member_for_cmptype(pathkey->pk_opfamily,
em->em_datatype,
em->em_datatype,
pathkey->pk_cmptype);
if (!OidIsValid(oprid))
elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
(int) pathkey->pk_cmptype, em->em_datatype, em->em_datatype,
pathkey->pk_opfamily);
#else
oprid = get_opfamily_member(pathkey->pk_opfamily,
em->em_datatype,
em->em_datatype,
Expand All @@ -414,6 +427,7 @@ sqlite_is_foreign_pathkey(PlannerInfo *root,
elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
pathkey->pk_strategy, em->em_datatype, em->em_datatype,
pathkey->pk_opfamily);
#endif

/* See whether operator is default < or > for sort expr's datatype. */
typentry = lookup_type_cache(exprType((Node *) em->em_expr),
Expand Down Expand Up @@ -1960,7 +1974,6 @@ sqlite_deparse_insert(StringInfo buf, PlannerInfo *root,
TupleDesc tupdesc = RelationGetDescr(rel);
bool all_columns_generated = true;
#endif
AttrNumber pindex;
bool first;
ListCell *lc;

Expand Down Expand Up @@ -2020,7 +2033,6 @@ sqlite_deparse_insert(StringInfo buf, PlannerInfo *root,

appendStringInfoString(buf, ") VALUES (");

pindex = 1;
first = true;
foreach(lc, targetAttrs)
{
Expand All @@ -2035,7 +2047,6 @@ sqlite_deparse_insert(StringInfo buf, PlannerInfo *root,
appendStringInfoString(buf, ", ");
first = false;
appendStringInfo(buf, "?");
pindex++;
#if PG_VERSION_NUM >= 140000
}
#endif
Expand Down Expand Up @@ -2649,7 +2660,6 @@ sqlite_deparse_update(StringInfo buf, PlannerInfo *root,
#if PG_VERSION_NUM >= 140000
TupleDesc tupdesc = RelationGetDescr(rel);
#endif
AttrNumber pindex;
bool first;
ListCell *lc;
int i;
Expand All @@ -2658,7 +2668,6 @@ sqlite_deparse_update(StringInfo buf, PlannerInfo *root,
sqlite_deparse_relation(buf, rel);
appendStringInfoString(buf, " SET ");

pindex = 2;
first = true;
foreach(lc, targetAttrs)
{
Expand All @@ -2674,7 +2683,6 @@ sqlite_deparse_update(StringInfo buf, PlannerInfo *root,
first = false;
sqlite_deparse_column_ref(buf, rtindex, attnum, root, false, true);
appendStringInfo(buf, " = ?");
pindex++;
#if PG_VERSION_NUM >= 140000
}
#endif
Expand Down Expand Up @@ -4163,6 +4171,16 @@ sqlite_append_order_by_clause(List *pathkeys, bool has_final_sort, deparse_expr_
* The datatype used by the opfamily is not necessarily the same as
* the expression type (for array types for example).
*/
#if PG_VERSION_NUM >= 180000
oprid = get_opfamily_member_for_cmptype(pathkey->pk_opfamily,
em->em_datatype,
em->em_datatype,
pathkey->pk_cmptype);
if (!OidIsValid(oprid))
elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
(int) pathkey->pk_cmptype, em->em_datatype, em->em_datatype,
pathkey->pk_opfamily);
#else
oprid = get_opfamily_member(pathkey->pk_opfamily,
em->em_datatype,
em->em_datatype,
Expand All @@ -4171,6 +4189,7 @@ sqlite_append_order_by_clause(List *pathkeys, bool has_final_sort, deparse_expr_
elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
pathkey->pk_strategy, em->em_datatype, em->em_datatype,
pathkey->pk_opfamily);
#endif

sqlite_deparse_expr(em_expr, context);
/*
Expand All @@ -4193,10 +4212,17 @@ sqlite_append_order_by_clause(List *pathkeys, bool has_final_sort, deparse_expr_
* warning message because NULLS FIRST/LAST is not implemented in
* this SQLite version.
*/
#if PG_VERSION_NUM >= 180000
if (!pathkey->pk_nulls_first && pathkey->pk_cmptype == COMPARE_LT)
elog(WARNING, "Current Sqlite Version (%d) does not support NULLS LAST for ORDER BY ASC, degraded emitted query to ORDER BY ASC NULLS FIRST (default sqlite behaviour).", sqliteVersion);
else if (pathkey->pk_nulls_first && pathkey->pk_cmptype != COMPARE_LT)
elog(WARNING, "Current Sqlite Version (%d) does not support NULLS FIRST for ORDER BY DESC, degraded emitted query to ORDER BY DESC NULLS LAST (default sqlite behaviour).", sqliteVersion);
#else
if (!pathkey->pk_nulls_first && pathkey->pk_strategy == BTLessStrategyNumber)
elog(WARNING, "Current Sqlite Version (%d) does not support NULLS LAST for ORDER BY ASC, degraded emitted query to ORDER BY ASC NULLS FIRST (default sqlite behaviour).", sqliteVersion);
else if (pathkey->pk_nulls_first && pathkey->pk_strategy != BTLessStrategyNumber)
elog(WARNING, "Current Sqlite Version (%d) does not support NULLS FIRST for ORDER BY DESC, degraded emitted query to ORDER BY DESC NULLS LAST (default sqlite behaviour).", sqliteVersion);
#endif
}
}
sqlite_reset_transmission_modes(nestlevel);
Expand Down
44 changes: 42 additions & 2 deletions sqlite_fdw.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@
#include <sqlite3.h>

#include "catalog/pg_collation.h"
#include "access/htup_details.h" /* SizeofHeapTupleHeader */
#include "catalog/pg_type.h"
#include "commands/defrem.h"
#include "commands/explain.h"
#if PG_VERSION_NUM >= 180000
#include "commands/explain_format.h" /* ExplainPropertyText, ExplainPropertyInteger */
#include "commands/explain_state.h" /* ExplainState struct definition */
#endif
#include "foreign/fdwapi.h"
#include "funcapi.h"
#include "mb/pg_wchar.h"
Expand Down Expand Up @@ -807,6 +812,9 @@ sqlite_add_paths_with_pathkeys_for_rel(PlannerInfo *root, RelOptInfo *rel, List
create_foreignscan_path(root, rel,
NULL,
rows,
#if PG_VERSION_NUM >= 180000
0, /* disabled_nodes */
#endif
startup_cost,
total_cost,
useful_pathkeys,
Expand All @@ -830,6 +838,9 @@ sqlite_add_paths_with_pathkeys_for_rel(PlannerInfo *root, RelOptInfo *rel, List
#endif
NULL,
rows,
#if PG_VERSION_NUM >= 180000
0, /* disabled_nodes */
#endif
startup_cost,
total_cost,
useful_pathkeys,
Expand Down Expand Up @@ -924,6 +935,9 @@ sqliteGetForeignPaths(PlannerInfo *root, RelOptInfo *baserel, Oid foreigntableid
NULL, /* default pathtarget */
#endif
baserel->rows,
#if PG_VERSION_NUM >= 180000
0, /* disabled_nodes */
#endif
startup_cost,
total_cost,
NIL, /* no pathkeys */
Expand Down Expand Up @@ -2500,7 +2514,11 @@ sqliteIterateDirectModify(ForeignScanState *node)
SqliteFdwDirectModifyState *dmstate = (SqliteFdwDirectModifyState *) node->fdw_state;
EState *estate = node->ss.ps.state;
TupleTableSlot *slot = node->ss.ss_ScanTupleSlot;
#if PG_VERSION_NUM >= 190000
NodeInstrumentation *instr = node->ss.ps.instrument;
#else
Instrumentation *instr = node->ss.ps.instrument;
#endif

elog(DEBUG1, "sqlite_fdw : %s", __func__);

Expand Down Expand Up @@ -2719,7 +2737,6 @@ sqliteExecForeignUpdate(EState *estate,
Oid foreignTableId = RelationGetRelid(rel);
ListCell *lc = NULL;
int bindnum = 0;
int i = 0;
int rc = 0;

elog(DEBUG1, "sqlite_fdw : %s", __func__);
Expand All @@ -2745,7 +2762,6 @@ sqliteExecForeignUpdate(EState *estate,

sqlite_bind_sql_var(bind_att, bindnum, value, fmstate->stmt, &is_null, foreignTableId);
bindnum++;
i++;
}

bindJunkColumnValue(fmstate, slot, planSlot, foreignTableId, bindnum);
Expand Down Expand Up @@ -3384,6 +3400,9 @@ sqlite_adjust_foreign_grouping_path_cost(PlannerInfo *root,
cost_sort(&sort_path,
root,
pathkeys,
#if PG_VERSION_NUM >= 180000
0, /* input_disabled_nodes */
#endif
*p_startup_cost + *p_run_cost,
retrieved_rows,
width,
Expand Down Expand Up @@ -3535,6 +3554,9 @@ sqliteGetForeignJoinPaths(PlannerInfo *root,
joinrel,
NULL, /* default pathtarget */
rows,
#if PG_VERSION_NUM >= 180000
0, /* disabled_nodes */
#endif
startup_cost,
total_cost,
NIL, /* no pathkeys */
Expand Down Expand Up @@ -3963,6 +3985,9 @@ sqlite_add_foreign_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel,
grouped_rel,
grouped_rel->reltarget,
rows,
#if PG_VERSION_NUM >= 180000
0, /* disabled_nodes */
#endif
startup_cost,
total_cost,
NIL, /* no pathkeys */
Expand All @@ -3976,6 +4001,9 @@ sqlite_add_foreign_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel,
grouped_rel,
root->upper_targets[UPPERREL_GROUP_AGG],
rows,
#if PG_VERSION_NUM >= 180000
0, /* disabled_nodes */
#endif
startup_cost,
total_cost,
NIL, /* no pathkeys */
Expand Down Expand Up @@ -4121,6 +4149,9 @@ sqlite_add_foreign_ordered_paths(PlannerInfo *root, RelOptInfo *input_rel,
input_rel,
root->upper_targets[UPPERREL_ORDERED],
rows,
#if PG_VERSION_NUM >= 180000
0, /* disabled_nodes */
#endif
startup_cost,
total_cost,
root->sort_pathkeys,
Expand All @@ -4144,6 +4175,9 @@ sqlite_add_foreign_ordered_paths(PlannerInfo *root, RelOptInfo *input_rel,
input_rel,
root->upper_targets[UPPERREL_FINAL],
rows,
#if PG_VERSION_NUM >= 180000
0, /* disabled_nodes */
#endif
startup_cost,
total_cost,
root->sort_pathkeys,
Expand Down Expand Up @@ -4331,6 +4365,9 @@ sqlite_add_foreign_final_paths(PlannerInfo *root, RelOptInfo *input_rel,
input_rel,
root->upper_targets[UPPERREL_FINAL],
rows,
#if PG_VERSION_NUM >= 180000
0, /* disabled_nodes */
#endif
startup_cost,
total_cost,
pathkeys,
Expand All @@ -4344,6 +4381,9 @@ sqlite_add_foreign_final_paths(PlannerInfo *root, RelOptInfo *input_rel,
input_rel,
root->upper_targets[UPPERREL_FINAL],
rows,
#if PG_VERSION_NUM >= 180000
0, /* disabled_nodes */
#endif
startup_cost,
total_cost,
pathkeys,
Expand Down
4 changes: 2 additions & 2 deletions sqlite_query.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ sqlite_convert_to_pg(Form_pg_attribute att, sqlite3_value * val, AttInMetadata *
case SQLITE3_TEXT: /* treated as UTF-8 text BLOB */
{
value_datum = (Datum) palloc0(value_byte_size_blob_or_utf8 + VARHDRSZ);
memcpy(VARDATA(value_datum), sqlite3_value_blob(val), value_byte_size_blob_or_utf8);
SET_VARSIZE(value_datum, value_byte_size_blob_or_utf8 + VARHDRSZ);
memcpy(VARDATA(DatumGetPointer(value_datum)), sqlite3_value_blob(val), value_byte_size_blob_or_utf8);
SET_VARSIZE(DatumGetPointer(value_datum), value_byte_size_blob_or_utf8 + VARHDRSZ);
return (struct NullableDatum) {PointerGetDatum((const void *)value_datum), false};
}
}
Expand Down