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: limit total rows copied in COPY TABLE FROM stmt #3819

Merged
merged 12 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
22 changes: 18 additions & 4 deletions src/operator/src/statement/copy_table_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ use crate::statement::StatementExecutor;

const DEFAULT_BATCH_SIZE: usize = 8192;
const DEFAULT_READ_BUFFER: usize = 256 * 1024;
const MAX_INSERT_ROWS: usize = 1000;

enum FileMetadata {
Parquet {
Expand Down Expand Up @@ -377,6 +378,11 @@ impl StatementExecutor {

let mut rows_inserted = 0;
let mut insert_cost = 0;
let max_insert_rows = req
.with
.get("max_insert_rows")
v0y4g3r marked this conversation as resolved.
Show resolved Hide resolved
.and_then(|val| val.parse::<usize>().ok())
.unwrap_or(MAX_INSERT_ROWS);
for (compat_schema, file_schema_projection, projected_table_schema, file_metadata) in files
{
let mut stream = self
Expand Down Expand Up @@ -427,6 +433,10 @@ impl StatementExecutor {
rows_inserted += rows;
insert_cost += cost;
}

if rows_inserted >= max_insert_rows {
v0y4g3r marked this conversation as resolved.
Show resolved Hide resolved
return Ok(gen_insert_output(rows_inserted, insert_cost));
}
}

if !pending.is_empty() {
Expand All @@ -436,13 +446,17 @@ impl StatementExecutor {
}
}

Ok(Output::new(
OutputData::AffectedRows(rows_inserted),
OutputMeta::new_with_cost(insert_cost),
))
Ok(gen_insert_output(rows_inserted, insert_cost))
}
}

fn gen_insert_output(rows_inserted: usize, insert_cost: usize) -> Output {
Output::new(
OutputData::AffectedRows(rows_inserted),
OutputMeta::new_with_cost(insert_cost),
)
}

/// Executes all pending inserts all at once, drain pending requests and reset pending bytes.
async fn batch_insert(
pending: &mut Vec<impl Future<Output = Result<Output>>>,
Expand Down
21 changes: 21 additions & 0 deletions tests/cases/standalone/common/copy/copy_from_fs_parquet.result
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ select * from with_pattern order by ts;
| host2 | 88.8 | 333.3 | 2022-06-15T07:02:38 |
+-------+------+--------+---------------------+

CREATE TABLE with_limit_rows(host string, cpu double, memory double, ts timestamp time index);

Affected Rows: 0

Copy with_limit_rows FROM '/tmp/demo/export/parquet/' WITH (MAX_INSERT_ROWS = 10);

Affected Rows: 2

select * from with_limit_rows order by ts;

+-------+------+--------+---------------------+
| host | cpu | memory | ts |
+-------+------+--------+---------------------+
| host1 | 66.6 | 1024.0 | 2022-06-15T07:02:37 |
| host2 | 88.8 | 333.3 | 2022-06-15T07:02:38 |
+-------+------+--------+---------------------+

drop table demo;

Affected Rows: 0
Expand All @@ -77,3 +94,7 @@ drop table with_pattern;

Affected Rows: 0

drop table with_limit_rows;

Affected Rows: 0

8 changes: 8 additions & 0 deletions tests/cases/standalone/common/copy/copy_from_fs_parquet.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@ Copy with_pattern FROM '/tmp/demo/export/parquet/' WITH (PATTERN = 'demo.*');

select * from with_pattern order by ts;

CREATE TABLE with_limit_rows(host string, cpu double, memory double, ts timestamp time index);

Copy with_limit_rows FROM '/tmp/demo/export/parquet/' WITH (MAX_INSERT_ROWS = 10);
v0y4g3r marked this conversation as resolved.
Show resolved Hide resolved

select * from with_limit_rows order by ts;

drop table demo;

drop table with_filename;

drop table with_path;

drop table with_pattern;

drop table with_limit_rows;
Loading