Skip to content

Instantly share code, notes, and snippets.

@FabienArcellier
Created June 4, 2024 12:12
Show Gist options
  • Save FabienArcellier/0f3e207a2600edc4296d85ea070333ad to your computer and use it in GitHub Desktop.
Save FabienArcellier/0f3e207a2600edc4296d85ea070333ad to your computer and use it in GitHub Desktop.
import csv
import io
from multicorn import ForeignDataWrapper
class FileCsvForeignDataWrapper(ForeignDataWrapper):
def __init__(self, options, columns):
super(FileCsvForeignDataWrapper, self).__init__(options, columns)
self.options = options
self.columns = columns
def execute(self, quals, columns):
has_header = self.options.get('header', 'false') == 'true'
with io.open(self.options["path"], 'r') as filep:
reader = csv.reader(filep, delimiter=self.options.get("delimiter", ","))
for i, line in enumerate(reader):
if i == 0 and not has_header:
continue
row = {}
for i, column_name in enumerate(self.columns):
row[column_name] = line[i]
yield row
a b
1 2
2 4
CREATE EXTENSION multicorn
CREATE SERVER file_csv foreign data wrapper multicorn options (
wrapper 'fabien_fdw.FileCsvForeignDataWrapper'
);
CREATE FOREIGN TABLE file1 (
A character varying,
B character varying
) server file_csv options (path '/data/file1.csv');
select a from file1
select * from file1 where A = '1';
EXPLAIN select * from file1 where A='1';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment