forked from dbt-labs/redshift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unload.sql
105 lines (102 loc) · 2.6 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
-- 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
{ [ FORMAT [ AS ] ] CSV | PARQUET
| PARTITION BY ( column_name [, ... ] ) [ INCLUDE ]
| MANIFEST [ VERBOSE ]
| HEADER
| DELIMITER [ AS ] 'delimiter-char'
| FIXEDWIDTH [ AS ] 'fixedwidth-spec'
| ENCRYPTED [ AUTO ]
| BZIP2
| GZIP
| ZSTD
| ADDQUOTES
| NULL [ AS ] 'null-string'
| ESCAPE
| ALLOWOVERWRITE
| PARALLEL [ { ON | TRUE } | { OFF | FALSE } ]
| MAXFILESIZE [AS] max-size [ MB | GB ]
| REGION [AS] 'aws-region' }
#}
-- Unloads a Redshift table to S3
{% macro unload_table(schema,
table,
s3_path,
iam_role=None,
aws_key=None,
aws_secret=None,
aws_region=None,
aws_token=None,
manifest=False,
header=False,
format=None,
delimiter=",",
null_as="",
max_file_size='6 GB',
escape=True,
compression=None,
add_quotes=False,
encrypted=False,
overwrite=False,
parallel=False,
partition_by=None
) %}
-- 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 }}'
{% if aws_token %}
SESSION_TOKEN '{{ aws_token }}'
{% endif %}
{% 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 %}
{% if header %}
HEADER
{% endif %}
{% if format %}
FORMAT AS {{format|upper}}
{% endif %}
{% if not format %}
DELIMITER AS '{{ delimiter }}'
{% endif %}
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 %}
{% if aws_region %}
REGION '{{ aws_region }}'
{% endif %}
{% if partition_by %}
PARTITION BY ( {{ partition_by | join(', ') }} )
{% endif %}
{% endmacro %}