forked from dbt-labs/redshift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unload.sql
79 lines (74 loc) · 1.91 KB
/
unload.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
-- Redshift UNLOAD grammar (see: http://docs.aws.amazon.com/redshift/latest/dg/r_UNLOAD.html)
{#
UNLOAD ('select-statement')
TO 's3://object-path/name-prefix'
authorization
[ option [ ... ] ]
where option is
{ MANIFEST
| DELIMITER [ AS ] 'delimiter-char'
| FIXEDWIDTH [ AS ] 'fixedwidth-spec' }
| ENCRYPTED
| BZIP2
| GZIP
| ADDQUOTES
| NULL [ AS ] 'null-string'
| ESCAPE
| ALLOWOVERWRITE
| PARALLEL [ { ON | TRUE } | { OFF | FALSE } ]
[ MAXFILESIZE [AS] max-size [ MB | GB ] ]
#}
-- Unloads a Redshift table to S3
{% macro unload_table(schema,
table,
s3_path,
iam_role=None,
aws_key=None,
aws_secret=None,
manifest=False,
delimiter=",",
null_as="",
max_file_size='6 GB',
escape=True,
compression=None,
add_quotes=False,
encrypted=False,
overwrite=False,
parallel=False) %}
-- compile UNLOAD statement
UNLOAD ('SELECT * FROM "{{ schema }}"."{{ table }}"')
TO '{{ s3_path }}'
{% if iam_role %}
IAM_ROLE '{{ iam_role }}'
{% elif aws_key and aws_secret %}
ACCESS_KEY_ID '{{ aws_key }}'
SECRET_ACCESS_KEY '{{ aws_secret }}'
{% else %}
-- Raise an error if authorization args are not present
{{ exceptions.raise_compiler_error("You must provide AWS authorization parameters via 'iam_role' or 'aws_key' and 'aws_secret'.") }}
{% endif %}
{% if manifest %}
MANIFEST
{% endif %}
DELIMITER AS '{{ delimiter }}'
NULL AS '{{ null_as }}'
MAXFILESIZE AS {{ max_file_size }}
{% if escape %}
ESCAPE
{% endif %}
{% if compression %}
{{ compression|upper }}
{% endif %}
{% if add_quotes %}
ADDQUOTES
{% endif %}
{% if encrypted %}
ENCRYPTED
{% endif %}
{% if overwrite %}
ALLOWOVERWRITE
{% endif %}
{% if not parallel %}
PARALLEL OFF
{% endif %}
{% endmacro %}