Skip to content

Commit

Permalink
Merge pull request #2 from radondb/master-import-override-param
Browse files Browse the repository at this point in the history
import add override param
  • Loading branch information
zlianzhuang authored Mar 27, 2023
2 parents a391a4b + 358d376 commit a29e2ad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ aws_s3.table_import_from_s3 (
credentials aws_commons._aws_credentials_1,
endpoint_url text default null,
read_timeout integer default 60,
override boolean default false,
tempfile_dir text default '/var/lib/postgresql/data/'
)
```
Expand All @@ -96,6 +97,7 @@ s3_info | An aws_commons._s3_uri_1 composite type containing the bucket, file pa
credentials | An aws_commons._aws_credentials_1 composite type containing the access key, secret key, session token credentials. To omit a value, set it `null`.
endpoint_url | optional endpoint to use (e.g., `http://localhost:4566`)
read_timeout | The time in seconds till a timeout exception is thrown when attempting to read from a connection.The default is 60 seconds.
override | Whether to overwrite data. If true, will truncate table.
tempfile_dir | Specify temporary file location

##### Example
Expand Down Expand Up @@ -175,6 +177,7 @@ aws_s3.table_import_from_s3 (
session_token text,
endpoint_url text default null,
read_timeout integer default 60,
override boolean default false,
tempfile_dir text default '/var/lib/postgresql/data/'
)
```
Expand All @@ -192,6 +195,7 @@ secret_key | aws secret key
session_token | optional session token
endpoint_url | optional endpoint to use (e.g., `http://localhost:4566`)
read_timeout | The time in seconds till a timeout exception is thrown when attempting to read from a connection.The default is 60 seconds.
override | Whether to overwrite data. If true, will truncate table.
tempfile_dir | Specify temporary file location

##### Example
Expand Down
17 changes: 13 additions & 4 deletions aws_s3--1.0.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ CREATE OR REPLACE FUNCTION aws_s3.table_import_from_s3 (
session_token text default null,
endpoint_url text default null,
read_timeout integer default 60,
override boolean default false,
tempfile_dir text default '/var/lib/postgresql/data/'
) RETURNS int
LANGUAGE plpython3u
Expand Down Expand Up @@ -91,6 +92,9 @@ AS $$
**aws_settings
)

if override:
plpy.execute("TRUNCATE TABLE {table_name} RESTRICT;".format(table_name=table_name))

formatted_column_list = "({column_list})".format(column_list=column_list) if column_list else ''
num_rows = 0

Expand Down Expand Up @@ -146,14 +150,15 @@ CREATE OR REPLACE FUNCTION aws_s3.table_import_from_s3(
credentials aws_commons._aws_credentials_1,
endpoint_url text default null,
read_timeout integer default 60,
override boolean default false,
tempfile_dir text default '/var/lib/postgresql/data/'
) RETURNS INT
LANGUAGE plpython3u
AS $$

plan = plpy.prepare(
'SELECT aws_s3.table_import_from_s3($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) AS num_rows',
['TEXT', 'TEXT', 'TEXT', 'TEXT', 'TEXT', 'TEXT', 'TEXT', 'TEXT', 'TEXT', 'TEXT', 'INTEGER', 'TEXT']
'SELECT aws_s3.table_import_from_s3($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) AS num_rows',
['TEXT', 'TEXT', 'TEXT', 'TEXT', 'TEXT', 'TEXT', 'TEXT', 'TEXT', 'TEXT', 'TEXT', 'INTEGER', 'BOOLEAN', 'TEXT']
)
return plan.execute(
[
Expand All @@ -167,7 +172,9 @@ AS $$
credentials['secret_key'],
credentials['session_token'],
endpoint_url,
read_timeout
read_timeout,
override,
tempfile_dir
]
)[0]['num_rows']
$$;
Expand Down Expand Up @@ -306,7 +313,9 @@ AS $$
credentials.get('session_token') if credentials else None,
options,
endpoint_url,
read_timeout
read_timeout,
override,
tempfile_dir
]
)
$$;

0 comments on commit a29e2ad

Please sign in to comment.