Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Postgresql CSV import for older versions. #487

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
75 changes: 49 additions & 26 deletions odo/backends/sql_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,32 +151,55 @@ def compile_from_csv_postgres(element, compiler, **kwargs):
raise ValueError(
r'PostgreSQL does not support line terminators other than \n'
)
return compiler.process(
sa.text(
"""
COPY {0} FROM :path (
FORMAT CSV,
DELIMITER :delimiter,
NULL :na_value,
QUOTE :quotechar,
ESCAPE :escapechar,
HEADER :header,
ENCODING :encoding
)
""".format(compiler.preparer.format_table(element.element))
).bindparams(
path=os.path.abspath(element.csv.path),
delimiter=element.delimiter,
na_value=element.na_value,
quotechar=element.quotechar,
escapechar=element.escapechar,
header=element.header,
encoding=element.encoding or element.bind(
'show client_encoding'
).execute().scalar()
),
**kwargs
)
postgres_version = element.bind.execute('select version()').scalar()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am worried about the perf penalty we will pay to check this on every insert. What do you think about creating a WeakKeyDictionary from bind to version? This will let us save these checks across calls.

if int(postgres_version.split()[1].split('.')[0]) >= 9:
return compiler.process(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in order to make this easier to follow, what do you think about breaking these two calls into helper functions like _process_pg_8_or_lower or process_pg_9_and_greater?

sa.text(
"""
COPY {0} FROM :path (
FORMAT CSV,
DELIMITER :delimiter,
NULL :na_value,
QUOTE :quotechar,
ESCAPE :escapechar,
HEADER :header,
ENCODING :encoding
)
""".format(compiler.preparer.format_table(element.element))
).bindparams(
path=os.path.abspath(element.csv.path),
delimiter=element.delimiter,
na_value=element.na_value,
quotechar=element.quotechar,
escapechar=element.escapechar,
header=element.header,
encoding=element.encoding or element.bind.execute(
'show client_encoding'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably also memoize the encoding if we are caching the version.

).scalar()
),
**kwargs
)
else:
return compiler.process(
sa.text((
"""
COPY {0} FROM :path
NULL :na_value
DELIMITER :delimiter
CSV %s
QUOTE :quotechar
ESCAPE :escapechar
""" % ('HEADER' if element.header else '')
).format(compiler.preparer.format_table(element.element))
).bindparams(
path=os.path.abspath(element.csv.path),
delimiter=element.delimiter,
na_value=element.na_value,
quotechar=element.quotechar,
escapechar=element.escapechar,
),
**kwargs
)


try:
Expand Down