Created
June 4, 2024 12:12
-
-
Save FabienArcellier/0f3e207a2600edc4296d85ea070333ad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a | b | |
---|---|---|
1 | 2 | |
2 | 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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