Skip to content

Commit

Permalink
Remove PG code from pgduckdb_tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-- committed Nov 12, 2024
1 parent acb93f0 commit 3a5bb6a
Show file tree
Hide file tree
Showing 18 changed files with 138 additions and 48 deletions.
4 changes: 3 additions & 1 deletion include/pgduckdb/catalog/pgduckdb_schema.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#pragma once

#include "duckdb/catalog/catalog_entry/schema_catalog_entry.hpp"
#include "pgduckdb/pg_declarations.hpp"
#include "pgduckdb/pg/declarations.hpp"

#include "pgduckdb/utility/cpp_only_file.hpp" // Must be last include.

namespace pgduckdb {

Expand Down
4 changes: 3 additions & 1 deletion include/pgduckdb/catalog/pgduckdb_table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include "duckdb/catalog/catalog_entry/table_catalog_entry.hpp"
#include "duckdb/storage/table_storage_info.hpp"

#include "pgduckdb/pg_declarations.hpp"
#include "pgduckdb/pg/declarations.hpp"

#include "pgduckdb/utility/cpp_only_file.hpp" // Must be last include.

namespace pgduckdb {

Expand Down
2 changes: 1 addition & 1 deletion include/pgduckdb/catalog/pgduckdb_transaction.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "duckdb/transaction/transaction.hpp"
#include "pgduckdb/pg_declarations.hpp"
#include "pgduckdb/pg/declarations.hpp"

#include "pgduckdb/utility/cpp_only_file.hpp" // Must be last include.

Expand Down
2 changes: 1 addition & 1 deletion include/pgduckdb/catalog/pgduckdb_transaction_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "duckdb/transaction/transaction_manager.hpp"
#include "duckdb/common/reference_map.hpp"
#include "pgduckdb/pg_declarations.hpp"
#include "pgduckdb/pg/declarations.hpp"

#include "pgduckdb/utility/cpp_only_file.hpp" // Must be last include.

Expand Down
7 changes: 7 additions & 0 deletions include/pgduckdb/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern "C" {
bool errstart(int elevel, const char *domain);
void errfinish(const char *filename, int lineno, const char *funcname);
int errmsg_internal(const char *fmt,...);
bool message_level_is_interesting(int elevel);
}

namespace pgduckdb {
Expand All @@ -24,6 +25,12 @@ namespace pgduckdb {
#define WARNING_CLIENT_ONLY 20

// From PG elog.h
#ifdef __GNUC__
#define pg_attribute_unused() __attribute__((unused))
#else
#define pg_attribute_unused()
#endif

#if defined(errno) && defined(__linux__)
#define pd_prevent_errno_in_scope() int __errno_location pg_attribute_unused()
#elif defined(errno) && (defined(__darwin__) || defined(__FreeBSD__))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <inttypes.h>

/*
This file contains a few Postgres declarations.
Expand All @@ -10,11 +12,20 @@ It should not include any C++ code, only Postgres C declarations.
*/

extern "C" {
typedef uint32_t BlockNumber;

typedef double Cardinality;

typedef uintptr_t Datum;

struct FormData_pg_attribute;
typedef FormData_pg_attribute *Form_pg_attribute;

struct FormData_pg_class;
typedef FormData_pg_class *Form_pg_class;

struct HeapTupleData;

struct Node;

typedef unsigned int Oid;
Expand All @@ -25,4 +36,9 @@ typedef struct RelationData *Relation;
struct SnapshotData;
typedef struct SnapshotData *Snapshot;

struct TupleDescData;
typedef struct TupleDescData *TupleDesc;

struct TupleTableSlot;

}
22 changes: 22 additions & 0 deletions include/pgduckdb/pg/relations.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "pgduckdb/pg/declarations.hpp"

namespace pgduckdb {

// FIXME - this is a macro in Postgres, would be great to inline it to avoid the overhead of a function call
TupleDesc PDRelationGetDescr(Relation relation);

Relation RelationIdGetRelation(Oid relationId);

void RelationClose(Relation relation);

int GetTupleDescNatts(const TupleDesc tupleDesc);

const char *GetAttName(const Form_pg_attribute);

Form_pg_attribute GetAttr(const TupleDesc tupleDesc, int i);

void EstimateRelSize(Relation rel, int32_t *attr_widths, BlockNumber *pages, double *tuples, double *allvisfrac);

}
5 changes: 5 additions & 0 deletions include/pgduckdb/pg/snapshots.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

extern "C" {
extern Snapshot GetActiveSnapshot(void);
}
2 changes: 1 addition & 1 deletion include/pgduckdb/pgduckdb_ddl.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "pgduckdb/pg_declarations.hpp"
#include "pgduckdb/pg/declarations.hpp"

void DuckdbHandleDDL(Node *ParseTree, const char *queryString);
void DuckdbTruncateTable(Oid relation_oid);
2 changes: 1 addition & 1 deletion include/pgduckdb/pgduckdb_metadata_cache.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "pgduckdb/pg_declarations.hpp"
#include "pgduckdb/pg/declarations.hpp"

namespace pgduckdb {
bool IsExtensionRegistered();
Expand Down
19 changes: 7 additions & 12 deletions include/pgduckdb/pgduckdb_types.hpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
#pragma once

#include "duckdb.hpp"
#include "duckdb/common/shared_ptr.hpp"
#include "duckdb/common/extra_type_info.hpp"

extern "C" {
#include "postgres.h"
#include "executor/tuptable.h"
}

#include "pgduckdb/scan/postgres_scan.hpp"
#include "pgduckdb/pg/declarations.hpp"

namespace pgduckdb {

class PostgresScanGlobalState;
class PostgresScanLocalState;
typedef uint64_t idx_t; // From duckdb.h

// DuckDB has date starting from 1/1/1970 while PG starts from 1/1/2000
constexpr int32_t PGDUCKDB_DUCK_DATE_OFFSET = 10957;
constexpr int64_t PGDUCKDB_DUCK_TIMESTAMP_OFFSET = INT64CONST(10957) * USECS_PER_DAY;
constexpr int64_t PGDUCKDB_DUCK_TIMESTAMP_OFFSET = static_cast<int64_t>(10957) * static_cast<int64_t>(86400000000) /* USECS_PER_DAY */;

duckdb::LogicalType ConvertPostgresToDuckColumnType(Form_pg_attribute &attribute);
Oid GetPostgresDuckDBType(duckdb::LogicalType type);
int32 GetPostgresDuckDBTypemod(duckdb::LogicalType type);
int32_t GetPostgresDuckDBTypemod(duckdb::LogicalType type);
duckdb::Value ConvertPostgresParameterToDuckValue(Datum value, Oid postgres_type);
void ConvertPostgresToDuckValue(Oid attr_type, Datum value, duckdb::Vector &result, idx_t offset);
bool ConvertDuckToPostgresValue(TupleTableSlot *slot, duckdb::Value &value, idx_t col);
Expand Down
8 changes: 6 additions & 2 deletions include/pgduckdb/scan/postgres_seq_scan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
#include "duckdb.hpp"

#include "pgduckdb/pgduckdb_guc.h"
#include "pgduckdb/scan/postgres_scan.hpp"
#include "pgduckdb/scan/heap_reader.hpp"
#include "pgduckdb/pg/declarations.hpp"

namespace pgduckdb {

class HeapReaderGlobalState;
class HeapReader;
class PostgresScanGlobalState;
class PostgresScanLocalState;

// Global State

struct PostgresSeqScanGlobalState : public duckdb::GlobalTableFunctionState {
Expand Down
2 changes: 2 additions & 0 deletions include/pgduckdb/utility/rename_ruleutils.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

/*
* This file contains the renaming of the functions exposed by
* vendor/pg_ruleutils.h functions to avoid conflicts with the PostgreSQL
Expand Down
34 changes: 16 additions & 18 deletions src/catalog/pgduckdb_table.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
#include "pgduckdb/pgduckdb_types.hpp"
#include "pgduckdb/pgduckdb_process_lock.hpp"
#include "pgduckdb/catalog/pgduckdb_schema.hpp"
#include "pgduckdb/catalog/pgduckdb_table.hpp"
#include "duckdb/parser/parsed_data/create_table_info.hpp"
#include "pgduckdb/scan/postgres_seq_scan.hpp"
#include "pgduckdb/scan/postgres_scan.hpp"
#include "pgduckdb/pgduckdb_utils.hpp"

#include "pgduckdb/catalog/pgduckdb_schema.hpp"
#include "pgduckdb/logger.hpp"
#include "pgduckdb/pg/relations.hpp"
#include "pgduckdb/pgduckdb_process_lock.hpp"
#include "pgduckdb/pgduckdb_types.hpp" // ConvertPostgresToDuckColumnType
#include "pgduckdb/scan/postgres_seq_scan.hpp"

extern "C" {
#include "utils/rel.h" // RelationGetDescr
#include "optimizer/plancat.h" // estimate_rel_size
#include "catalog/namespace.h" // makeRangeVarFromNameList
}
#include "duckdb/parser/parsed_data/create_table_info.hpp"

#include "pgduckdb/utility/cpp_only_file.hpp" // Must be last include.

namespace pgduckdb {

Expand All @@ -29,16 +26,17 @@ PostgresTable::~PostgresTable() {
Relation
PostgresTable::OpenRelation(Oid relid) {
std::lock_guard<std::mutex> lock(DuckdbProcessLock::GetLock());
return PostgresFunctionGuard(RelationIdGetRelation, relid);
return RelationIdGetRelation(relid);
}

void
PostgresTable::SetTableInfo(duckdb::CreateTableInfo &info, Relation rel) {
auto tupleDesc = RelationGetDescr(rel);
auto tupleDesc = PDRelationGetDescr(rel);

for (int i = 0; i < tupleDesc->natts; ++i) {
Form_pg_attribute attr = &tupleDesc->attrs[i];
auto col_name = duckdb::string(NameStr(attr->attname));
const auto n = GetTupleDescNatts(tupleDesc);
for (int i = 0; i < n; ++i) {
Form_pg_attribute attr = GetAttr(tupleDesc, i);
auto col_name = duckdb::string(GetAttName(attr));
auto duck_type = ConvertPostgresToDuckColumnType(attr);
info.columns.AddColumn(duckdb::ColumnDefinition(col_name, duck_type));
/* Log column name and type */
Expand All @@ -52,7 +50,7 @@ PostgresTable::GetTableCardinality(Relation rel) {
Cardinality cardinality;
BlockNumber n_pages;
double allvisfrac;
estimate_rel_size(rel, NULL, &n_pages, &cardinality, &allvisfrac);
EstimateRelSize(rel, NULL, &n_pages, &cardinality, &allvisfrac);
return cardinality;
}

Expand Down
6 changes: 2 additions & 4 deletions src/catalog/pgduckdb_transaction_manager.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#include "pgduckdb/catalog/pgduckdb_transaction_manager.hpp"
#include "pgduckdb/catalog/pgduckdb_transaction.hpp"
#include "pgduckdb/pg/snapshots.hpp"
#include "pgduckdb/pgduckdb_process_lock.hpp"

#include "duckdb/main/attached_database.hpp"

extern "C" {
#include "postgres.h"
#include "utils/snapmgr.h" // GetActiveSnapshot
}
#include "pgduckdb/utility/cpp_only_file.hpp" // Must be last include.

namespace pgduckdb {

Expand Down
39 changes: 39 additions & 0 deletions src/pg/relations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "pgduckdb/pg/relations.hpp"

extern "C" {
#include "postgres.h"
#include "utils/rel.h"
#include "optimizer/plancat.h" // estimate_rel_size
}

namespace pgduckdb {

TupleDesc PDRelationGetDescr(Relation relation) {
return relation->rd_att;
}

int GetTupleDescNatts(const TupleDesc tupleDesc) {
return tupleDesc->natts;
}

const char *GetAttName(const Form_pg_attribute att) {
return NameStr(att->attname);
}

Form_pg_attribute GetAttr(const TupleDesc tupleDesc, int i) {
return &tupleDesc->attrs[i];
}

Relation RelationIdGetRelation(Oid relationId) {
return ::RelationIdGetRelation(relationId);
}

void RelationClose(Relation relation) {
::RelationClose(relation);
}

void EstimateRelSize(Relation rel, int32_t *attr_widths, BlockNumber *pages, double *tuples, double *allvisfrac) {
::estimate_rel_size(rel, attr_widths, pages, tuples, allvisfrac);
}

} // namespace pgduckdb
3 changes: 3 additions & 0 deletions src/pgduckdb_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ extern "C" {
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/timestamp.h"
#if PG_VERSION_NUM >= 160000
#include "varatt.h"
#endif
}

#include "pgduckdb/pgduckdb_filter.hpp"
Expand Down
9 changes: 3 additions & 6 deletions src/scan/postgres_seq_scan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
#include "pgduckdb/scan/postgres_seq_scan.hpp"
#include "pgduckdb/pgduckdb_types.hpp"
#include "pgduckdb/logger.hpp"
#include <inttypes.h>

extern "C" {
#include "utils/rel.h" // RelationGetDescr
}
#include "pgduckdb/scan/heap_reader.hpp"
#include "pgduckdb/pg/relations.hpp"

namespace pgduckdb {

Expand All @@ -19,7 +16,7 @@ PostgresSeqScanGlobalState::PostgresSeqScanGlobalState(Relation rel, duckdb::Tab
: m_global_state(duckdb::make_shared_ptr<PostgresScanGlobalState>()),
m_heap_reader_global_state(duckdb::make_shared_ptr<HeapReaderGlobalState>(rel)), m_rel(rel) {
m_global_state->InitGlobalState(input);
m_global_state->m_tuple_desc = RelationGetDescr(m_rel);
m_global_state->m_tuple_desc = PDRelationGetDescr(m_rel);
m_global_state->InitRelationMissingAttrs(m_global_state->m_tuple_desc);
pd_log(DEBUG2, "(DuckDB/PostgresSeqScanGlobalState) Running %" PRIu64 " threads -- ", (uint64_t)MaxThreads());
}
Expand Down

0 comments on commit 3a5bb6a

Please sign in to comment.