Skip to content

Commit

Permalink
trying to filter...
Browse files Browse the repository at this point in the history
  • Loading branch information
takanoriyanagitani committed Dec 29, 2020
1 parent b9e206c commit 82e1727
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 13 deletions.
53 changes: 53 additions & 0 deletions d_begin.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,57 @@
#pragma once

#include <utils/builtins.h>

static bool op_is_eqs(const OpExpr* o){ return 67 == o->opfuncid; }

static void val2dtype(const char* val, u8n* dtype){
for(; !!val;){
switch(strnlen(val, 1)){
case 0: return;
default: break;
}
switch(val[0]){
case 'd': u8n_set(dtype, DT_DIR); return;
case 'l': u8n_set(dtype, DT_LNK); return;
case 'f': u8n_set(dtype, DT_REG); return;
default: return;
}
break;
}
}

static void dir_fdw_set_filter(DirFdwExecState* s, const ScanState* ss){
s->d_type = u8n_create();
List* l = ss->ps.plan->qual;
ListCell* i = NULL;
TupleDesc td = ss->ss_currentRelation->rd_att;
foreach(i, l){
Expr* e = lfirst(i);
for(Node* n=(Node*)e; n && IsA(n, OpExpr);){
for(OpExpr* o=(OpExpr*)n; 2==list_length(o->args) && op_is_eqs(o);){
for(Node* left=list_nth(o->args,0); IsA(left, Var);){
for(Node* right=list_nth(o->args,1); IsA(right, Const);){
Var* lv = (Var*)left;
const char* col = NameStr(
TupleDescAttr(td, lv->varattno-1)->attname
);
for(; 0 == strncmp(col, "d_type", 6);){
Const* rc = (Const*)right;
const char* val = TextDatumGetCString(rc->constvalue);
val2dtype(val, &s->d_type);
break;
}
break;
}
break;
}
break;
}
break;
}
}
}

static void dir_fdw_begin(ForeignScanState* f, int eflags){
switch(eflags & EXEC_FLAG_EXPLAIN_ONLY){
case 0: break;
Expand All @@ -8,6 +60,7 @@ static void dir_fdw_begin(ForeignScanState* f, int eflags){

DirFdwExecState* e = palloc(sizeof(DirFdwExecState));
f->fdw_state = e;
dir_fdw_set_filter(e, &f->ss);

DirFdwOption o = {0};
dir_fdw_options(RelationGetRelid(f->ss.ss_currentRelation), &o);
Expand Down
15 changes: 10 additions & 5 deletions d_end.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#pragma once

static void dir_fdw_end(ForeignScanState* f){
DirFdwExecState* e = f->fdw_state;
switch(FreeDir(e->d)){
case 0: return;
default: break;
for(DirFdwExecState* e = f->fdw_state; NULL != e;){
for(DIR* d = e->d; NULL != d;){
switch(FreeDir(e->d)){
case 0: return;
default: break;
}
elog(WARNING, "Unable to free dir.");
break;
}
break;
}
elog(WARNING, "Unable to free dir.");
}
26 changes: 20 additions & 6 deletions d_iterate.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
#pragma once

const char* dtype_string(const u8n* u){
static const char* f = "file";
static const char* d = "directory";
static const char* l = "link";
static const char* n = "null";
switch(u->is_null){
case true: return n;
default: break;
}
switch(u->value){
case DT_REG: return f;
case DT_DIR: return d;
case DT_LNK: return l;
default: return n;
}
}

static Datum b2pgb8(const void* bytes){
static const uint16_t bufsz = 256;
int sz = bufsz + VARHDRSZ;
Expand Down Expand Up @@ -40,12 +57,9 @@ static TupleTableSlot* dir_fdw_iterate(ForeignScanState* f){
Datum* values = s->tts_values;
bool* nulls = s->tts_isnull;

values[0] = d->d_ino;
values[1] = UInt8GetDatum(d->d_type);
values[2] = b2pgb8(d->d_name);
nulls[0] = false;
nulls[1] = false;
nulls[2] = false;
nulls[0] = false; values[0] = d->d_ino;
nulls[2] = false; values[2] = b2pgb8(d->d_name);
nulls[1] = false; values[1] = d->d_type;
ExecStoreVirtualTuple(s);

return s;
Expand Down
2 changes: 1 addition & 1 deletion d_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static void dir_fdw_path(PlannerInfo* p, RelOptInfo* r, Oid o){
start,
total,
NIL, // no pathkeys
r->lateral_relids,
NULL,
NULL, // no extra plan
NIL //
));
Expand Down
2 changes: 2 additions & 0 deletions d_plan.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <optimizer/planmain.h>
#include <optimizer/restrictinfo.h>

static ForeignScan* dir_fdw_plan(
PlannerInfo* p,
Expand All @@ -11,6 +12,7 @@ static ForeignScan* dir_fdw_plan(
List* s,
Plan* outer
){
s = extract_actual_clauses(s, false);
return make_foreignscan(
l,
s,
Expand Down
2 changes: 2 additions & 0 deletions d_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ typedef struct DirFdwExecState {
const char* dirname;
int fd;
DIR* d;

u8n d_type;
} DirFdwExecState;
34 changes: 34 additions & 0 deletions d_type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

typedef struct u8n {
uint8_t value;
bool is_null;
} u8n;

void u8n_foreach(const u8n* u, void* param, void cb(uint8_t,void*)){
switch(u->is_null){
case true: return;
default: break;
}
cb(u->value, param);
}

u8n u8n_create(){
u8n u = {.value=0, .is_null=true};
return u;
}

void u8n_set(u8n* u, const uint8_t v){
u->value = v;
u->is_null = false;
}

void u8n_datum(const u8n* u, Datum* v, bool* null){
switch(u->is_null){
case true: *null = true; break;
default:
*null = false;
*v = UInt8GetDatum(u->value);
break;
}
}
2 changes: 2 additions & 0 deletions dir_fdw.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(dir_fdw_handler);
PG_FUNCTION_INFO_V1(dir_fdw_validator);

#include "d_type.h"

#include "d_options.h"
#include "d_state.h"

Expand Down
2 changes: 1 addition & 1 deletion dir_fdw.test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ CREATE FOREIGN TABLE IF NOT EXISTS dt(
dirname '/home'
);

SELECT * FROM dt;
SELECT * FROM dt WHERE d_type=4;

DROP FOREIGN TABLE dt;
DROP SERVER dsv;
Expand Down

0 comments on commit 82e1727

Please sign in to comment.