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

feat(query): support purge option in copy into table #7518

Merged
merged 6 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ CREATE STAGE [ IF NOT EXISTS ] <internal_stage_name>
[ FILE_FORMAT = ( { TYPE = { CSV | PARQUET } [ formatTypeOptions ] ) } ]
[ COPY_OPTIONS = ( copyOptions ) ]
[ COMMENT = '<string_literal>' ]

-- External stage
CREATE STAGE [ IF NOT EXISTS ] <external_stage_name>
externalStageParams
Expand Down Expand Up @@ -70,8 +70,8 @@ externalStageParams ::=
### formatTypeOptions
```
formatTypeOptions ::=
RECORD_DELIMITER = '<character>'
FIELD_DELIMITER = '<character>'
RECORD_DELIMITER = '<character>'
FIELD_DELIMITER = '<character>'
SKIP_HEADER = <integer>
```

Expand All @@ -85,11 +85,13 @@ formatTypeOptions ::=
```
copyOptions ::=
[ SIZE_LIMIT = <num> ]
[ PURGE = <bool> ]
```

| Parameters | Description | Required |
| ----------- | ----------- | --- |
| `SIZE_LIMIT = <num>` | Number (> 0) that specifies the maximum rows of data to be loaded for a given COPY statement. Default `0` | Optional |
| `PURGE = <bool>` | True specifies that the command will purge the files in the stage if they are loaded successfully into table. Default `false` | Optional |


## Examples
Expand Down
4 changes: 2 additions & 2 deletions docs/doc/30-reference/30-sql/10-dml/dml-copy-into-location.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ externalLocation (for Amazon S3) ::=
### formatTypeOptions
```
formatTypeOptions ::=
RECORD_DELIMITER = '<character>'
FIELD_DELIMITER = '<character>'
RECORD_DELIMITER = '<character>'
FIELD_DELIMITER = '<character>'
SKIP_HEADER = <integer>
```

Expand Down
8 changes: 5 additions & 3 deletions docs/doc/30-reference/30-sql/10-dml/dml-copy-into-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ A regular expression pattern string, enclosed in single quotes, specifying the f

```
formatTypeOptions ::=
RECORD_DELIMITER = '<character>'
FIELD_DELIMITER = '<character>'
RECORD_DELIMITER = '<character>'
FIELD_DELIMITER = '<character>'
SKIP_HEADER = <integer>
COMPRESSION = AUTO | GZIP | BZ2 | BROTLI | ZSTD | DEFLATE | RAW_DEFLATE | NONE
```
Expand Down Expand Up @@ -129,7 +129,7 @@ Default: `NONE`

Values:

| Values | Notes |
| Values | Notes |
|---------------|-----------------------------------------------------------------|
| `AUTO` | Auto detect compression via file extensions |
| `GZIP` | |
Expand All @@ -145,11 +145,13 @@ Values:
```
copyOptions ::=
[ SIZE_LIMIT = <num> ]
[ PURGE = <bool> ]
```

| Parameters | Description | Required |
| ----------- | ----------- | --- |
| `SIZE_LIMIT = <num>` | Number (> 0) that specifies the maximum rows of data to be loaded for a given COPY statement. Default `0` | Optional |
| `PURGE = <bool>` | True that specifies the command will purge the files in the stage if they are loaded successfully into table. Default `false` | Optional |

## Examples

Expand Down
2 changes: 2 additions & 0 deletions src/meta/proto-conv/src/user_from_to_protobuf_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ impl FromToProto for mt::CopyOptions {
Ok(mt::CopyOptions {
on_error,
size_limit,
purge: p.purge,
})
}

Expand All @@ -582,6 +583,7 @@ impl FromToProto for mt::CopyOptions {
Ok(pb::user_stage_info::CopyOptions {
on_error: Some(on_error),
size_limit,
purge: self.purge,
})
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/meta/proto-conv/tests/it/user_proto_conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ fn test_fs_stage_info() -> mt::UserStageInfo {
copy_options: mt::CopyOptions {
on_error: mt::OnErrorMode::SkipFileNum(666),
size_limit: 1038,
purge: false,
},
comment: "test".to_string(),
..Default::default()
Expand Down Expand Up @@ -111,6 +112,7 @@ fn test_s3_stage_info() -> mt::UserStageInfo {
copy_options: mt::CopyOptions {
on_error: mt::OnErrorMode::SkipFileNum(666),
size_limit: 1038,
purge: false,
},
comment: "test".to_string(),
..Default::default()
Expand Down Expand Up @@ -140,6 +142,7 @@ fn test_gcs_stage_info() -> mt::UserStageInfo {
copy_options: mt::CopyOptions {
on_error: mt::OnErrorMode::SkipFileNum(666),
size_limit: 1038,
purge: false,
},
comment: "test".to_string(),
..Default::default()
Expand Down
1 change: 1 addition & 0 deletions src/meta/protos/proto/user.proto
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ message UserStageInfo {
message CopyOptions {
OnErrorMode on_error = 1;
uint64 size_limit = 2;
bool purge = 3;
}

string stage_name = 1;
Expand Down
1 change: 1 addition & 0 deletions src/meta/types/src/user_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ impl FromStr for OnErrorMode {
pub struct CopyOptions {
pub on_error: OnErrorMode,
pub size_limit: usize,
pub purge: bool,
}

#[derive(serde::Serialize, serde::Deserialize, Default, Clone, Debug, Eq, PartialEq)]
Expand Down
5 changes: 5 additions & 0 deletions src/query/ast/src/ast/format/ast_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,11 @@ impl<'ast> Visitor<'ast> for AstFormatVisitor {
let size_limit_node = FormatTreeNode::new(size_limit_format_ctx);
children.push(size_limit_node);

let purge_name = format!("Purge {}", copy.purge);
let purge_name_ctx = AstFormatContext::new(purge_name);
let purge_name_node = FormatTreeNode::new(purge_name_ctx);
children.push(purge_name_node);

let name = "Copy".to_string();
let format_ctx = AstFormatContext::with_children(name, children.len());
let node = FormatTreeNode::with_children(format_ctx, children);
Expand Down
5 changes: 5 additions & 0 deletions src/query/ast/src/ast/format/syntax/dml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ pub(crate) fn pretty_copy(copy_stmt: CopyStmt) -> RcDoc {
} else {
RcDoc::nil()
})
.append(
RcDoc::line()
.append(RcDoc::text("PURGE = "))
.append(RcDoc::text(format!("{}", copy_stmt.purge))),
)
}

fn pretty_copy_unit(copy_unit: CopyUnit) -> RcDoc {
Expand Down
2 changes: 2 additions & 0 deletions src/query/ast/src/ast/statements/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub struct CopyStmt<'a> {
/// TODO(xuanwo): parse into validation_mode directly.
pub validation_mode: String,
pub size_limit: usize,
pub purge: bool,
sundy-li marked this conversation as resolved.
Show resolved Hide resolved
}

impl Display for CopyStmt<'_> {
Expand Down Expand Up @@ -72,6 +73,7 @@ impl Display for CopyStmt<'_> {
write!(f, " SIZE_LIMIT = {}", self.size_limit)?;
}

write!(f, " PURGE = {}", self.purge)?;
Ok(())
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/query/ast/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,10 @@ pub fn literal_f64(i: Input) -> IResult<f64> {
)(i)
}

pub fn literal_bool(i: Input) -> IResult<bool> {
alt((value(true, rule! { TRUE }), value(false, rule! { FALSE })))(i)
}

pub fn literal_string(i: Input) -> IResult<String> {
map_res(
rule! {
Expand Down
4 changes: 3 additions & 1 deletion src/query/ast/src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,8 +741,9 @@ pub fn statement(i: Input) -> IResult<StatementMsg> {
~ ( FILE_FORMAT ~ "=" ~ #options)?
~ ( VALIDATION_MODE ~ "=" ~ #literal_string)?
~ ( SIZE_LIMIT ~ "=" ~ #literal_u64)?
~ ( PURGE ~ "=" ~ #literal_bool)?
},
|(_, _, dst, _, src, files, pattern, file_format, validation_mode, size_limit)| {
|(_, _, dst, _, src, files, pattern, file_format, validation_mode, size_limit, purge)| {
Statement::Copy(CopyStmt {
src,
dst,
Expand All @@ -751,6 +752,7 @@ pub fn statement(i: Input) -> IResult<StatementMsg> {
file_format: file_format.map(|v| v.2).unwrap_or_default(),
validation_mode: validation_mode.map(|v| v.2).unwrap_or_default(),
size_limit: size_limit.map(|v| v.2).unwrap_or_default() as usize,
purge: purge.map(|v| v.2).unwrap_or_default(),
})
},
);
Expand Down
4 changes: 2 additions & 2 deletions src/query/ast/tests/it/testdata/statement-error.txt
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ error:
--> SQL:1:38
|
1 | COPY INTO mytable FROM 's3://bucket' CREDENTIAL = ();
| ^^^^^^^^^^ expected `CONNECTION`, `CREDENTIALS`, `ENCRYPTION`, `FILES`, `PATTERN`, `FILE_FORMAT`, or 4 more ...
| ^^^^^^^^^^ expected `CONNECTION`, `CREDENTIALS`, `ENCRYPTION`, `FILES`, `PATTERN`, `FILE_FORMAT`, or 5 more ...


---------- Input ----------
Expand All @@ -289,7 +289,7 @@ error:
--> SQL:1:33
|
1 | COPY INTO mytable FROM @mystage CREDENTIALS = ();
| ^^^^^^^^^^^ expected `FILES`, `PATTERN`, `FILE_FORMAT`, `VALIDATION_MODE`, `SIZE_LIMIT`, `FORMAT`, or 1 more ...
| ^^^^^^^^^^^ expected `FILES`, `PATTERN`, `FILE_FORMAT`, `VALIDATION_MODE`, `SIZE_LIMIT`, `PURGE`, or 2 more ...


---------- Input ----------
Expand Down
21 changes: 14 additions & 7 deletions src/query/ast/tests/it/testdata/statement.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5511,7 +5511,7 @@ COPY INTO mytable
size_limit=10;
---------- Output ---------
COPY INTO mytable FROM 's3://mybucket/data.csv' FILE_FORMAT = ( field_delimiter = ',' record_delimiter = '
' skip_header = '1' type = 'CSV' ) SIZE_LIMIT = 10
' skip_header = '1' type = 'CSV' ) SIZE_LIMIT = 10 PURGE = false
---------- AST ------------
Copy(
CopyStmt {
Expand Down Expand Up @@ -5542,6 +5542,7 @@ Copy(
},
validation_mode: "",
size_limit: 10,
purge: false,
},
)

Expand All @@ -5561,7 +5562,7 @@ COPY INTO mytable
size_limit=10;
---------- Output ---------
COPY INTO mytable FROM 's3://mybucket/data.csv' CONNECTION = ( endpoint_url='http://127.0.0.1:9900' ) FILE_FORMAT = ( field_delimiter = ',' record_delimiter = '
' skip_header = '1' type = 'CSV' ) SIZE_LIMIT = 10
' skip_header = '1' type = 'CSV' ) SIZE_LIMIT = 10 PURGE = false
---------- AST ------------
Copy(
CopyStmt {
Expand Down Expand Up @@ -5594,6 +5595,7 @@ Copy(
},
validation_mode: "",
size_limit: 10,
purge: false,
},
)

Expand All @@ -5610,7 +5612,7 @@ COPY INTO mytable
size_limit=10;
---------- Output ---------
COPY INTO mytable FROM @my_stage/ FILE_FORMAT = ( field_delimiter = ',' record_delimiter = '
' skip_header = '1' type = 'CSV' ) SIZE_LIMIT = 10
' skip_header = '1' type = 'CSV' ) SIZE_LIMIT = 10 PURGE = false
---------- AST ------------
Copy(
CopyStmt {
Expand All @@ -5637,6 +5639,7 @@ Copy(
},
validation_mode: "",
size_limit: 10,
purge: false,
},
)

Expand All @@ -5653,7 +5656,7 @@ COPY INTO 's3://mybucket/data.csv'
size_limit=10;
---------- Output ---------
COPY INTO 's3://mybucket/data.csv' FROM mytable FILE_FORMAT = ( field_delimiter = ',' record_delimiter = '
' skip_header = '1' type = 'CSV' ) SIZE_LIMIT = 10
' skip_header = '1' type = 'CSV' ) SIZE_LIMIT = 10 PURGE = false
---------- AST ------------
Copy(
CopyStmt {
Expand Down Expand Up @@ -5684,6 +5687,7 @@ Copy(
},
validation_mode: "",
size_limit: 10,
purge: false,
},
)

Expand All @@ -5700,7 +5704,7 @@ COPY INTO @my_stage
size_limit=10;
---------- Output ---------
COPY INTO @my_stage/ FROM mytable FILE_FORMAT = ( field_delimiter = ',' record_delimiter = '
' skip_header = '1' type = 'CSV' ) SIZE_LIMIT = 10
' skip_header = '1' type = 'CSV' ) SIZE_LIMIT = 10 PURGE = false
---------- AST ------------
Copy(
CopyStmt {
Expand All @@ -5727,6 +5731,7 @@ Copy(
},
validation_mode: "",
size_limit: 10,
purge: false,
},
)

Expand All @@ -5750,7 +5755,7 @@ COPY INTO mytable
size_limit=10;
---------- Output ---------
COPY INTO mytable FROM 's3://mybucket/data.csv' CONNECTION = ( aws_key_id='access_key' aws_secret_key='secret_key' master_key='master_key' ) FILE_FORMAT = ( field_delimiter = ',' record_delimiter = '
' skip_header = '1' type = 'CSV' ) SIZE_LIMIT = 10
' skip_header = '1' type = 'CSV' ) SIZE_LIMIT = 10 PURGE = false
---------- AST ------------
Copy(
CopyStmt {
Expand Down Expand Up @@ -5785,6 +5790,7 @@ Copy(
},
validation_mode: "",
size_limit: 10,
purge: false,
},
)

Expand All @@ -5801,7 +5807,7 @@ COPY INTO mytable
size_limit=10;
---------- Output ---------
COPY INTO mytable FROM @external_stage/path/to/file.csv FILE_FORMAT = ( field_delimiter = ',' record_delimiter = '
' skip_header = '1' type = 'CSV' ) SIZE_LIMIT = 10
' skip_header = '1' type = 'CSV' ) SIZE_LIMIT = 10 PURGE = false
---------- AST ------------
Copy(
CopyStmt {
Expand All @@ -5828,6 +5834,7 @@ Copy(
},
validation_mode: "",
size_limit: 10,
purge: false,
},
)

Expand Down
Loading