Skip to content

Commit

Permalink
tests: sqlness
Browse files Browse the repository at this point in the history
  • Loading branch information
discord9 committed Dec 4, 2024
1 parent 9a76968 commit 60509a5
Show file tree
Hide file tree
Showing 7 changed files with 473 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/common/base/src/ttl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl Display for TimeToLive {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TimeToLive::Immediate => write!(f, "immediate"),
TimeToLive::Duration(d) => write!(f, "Duration({})", d.as_secs()),
TimeToLive::Duration(d) => write!(f, "{}", humantime::Duration::from(*d)),
TimeToLive::Forever => write!(f, "forever"),
}
}
Expand All @@ -94,14 +94,14 @@ impl TimeToLive {
"forever" | "" => Ok(TimeToLive::Forever),
_ => {
let d = humantime::parse_duration(s).map_err(|e| e.to_string())?;
Ok(TimeToLive::Duration(d))
Ok(TimeToLive::from(d))
}
}
}

/// Print TimeToLive as string
///
/// omit forever variant
/// omit `Forever`` variant
pub fn as_repr_opt(&self) -> Option<String> {
match self {
TimeToLive::Immediate => Some("immediate".to_string()),
Expand All @@ -114,6 +114,7 @@ impl TimeToLive {
matches!(self, TimeToLive::Immediate)
}

/// Is the default value, which is `Forever`
pub fn is_forever(&self) -> bool {
matches!(self, TimeToLive::Forever)
}
Expand Down
22 changes: 13 additions & 9 deletions src/operator/src/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ impl Inserter {
table_name: common_catalog::format_full_table_name(catalog, schema, table_name),
})?;
let table_info = table.table_info();
check_ttl_zero_table(&ctx, &table_info);

let inserts = TableToRegion::new(&table_info, &self.partition_manager)
.convert(request)
Expand All @@ -266,6 +267,15 @@ impl Inserter {
}
}

/// Check and add regions with ttl=0 to context
pub(crate) fn check_ttl_zero_table(ctx: &QueryContextRef, table_info: &TableInfo) {
let ttl = table_info.meta.options.ttl;

if ttl.is_immediate() {
ctx.add_ttl_zero_regions(table_info.region_ids().into_iter().map(|i| i.as_u64()));
}
}

impl Inserter {
async fn do_request(
&self,
Expand Down Expand Up @@ -469,13 +479,6 @@ impl Inserter {
auto_create_table_type: AutoCreateTableType,
statement_executor: &StatementExecutor,
) -> Result<HashMap<String, TableId>> {
fn add_ttl_zero_table(ctx: &QueryContextRef, table_info: &TableInfo) {
let ttl = table_info.meta.options.ttl;

if ttl.is_immediate() {
ctx.add_ttl_zero_regions(table_info.region_ids().into_iter().map(|i| i.as_u64()));
}
}
let _timer = crate::metrics::CREATE_ALTER_ON_DEMAND
.with_label_values(&[auto_create_table_type.as_str()])
.start_timer();
Expand Down Expand Up @@ -507,7 +510,7 @@ impl Inserter {
),
})?;
let table_info = table.table_info();
add_ttl_zero_table(ctx, &table_info);
check_ttl_zero_table(ctx, &table_info);
table_name_to_ids.insert(table_info.name.clone(), table_info.table_id());
}
return Ok(table_name_to_ids);
Expand Down Expand Up @@ -572,14 +575,15 @@ impl Inserter {
}
}

// add ttl zero regions to context
for table_name in table_name_to_ids.keys() {
let table = if let Some(table) = self.get_table(catalog, &schema, table_name).await? {
table
} else {
continue;
};
let table_info = table.table_info();
add_ttl_zero_table(ctx, &table_info);
check_ttl_zero_table(ctx, &table_info);
}

Ok(table_name_to_ids)
Expand Down
2 changes: 2 additions & 0 deletions src/operator/src/req_convert/insert/stmt_to_region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use crate::error::{
ColumnNotFoundSnafu, InvalidSqlSnafu, MissingInsertBodySnafu, ParseSqlSnafu, Result,
SchemaReadOnlySnafu, TableNotFoundSnafu,
};
use crate::insert::check_ttl_zero_table;
use crate::req_convert::common::partitioner::Partitioner;
use crate::req_convert::insert::semantic_type;

Expand Down Expand Up @@ -65,6 +66,7 @@ impl<'a> StatementToRegion<'a> {
let table = self.get_table(&catalog, &schema, &table_name).await?;
let table_schema = table.schema();
let table_info = table.table_info();
check_ttl_zero_table(query_ctx, &table_info);

ensure!(
!common_catalog::consts::is_readonly_schema(&schema),
Expand Down
Loading

0 comments on commit 60509a5

Please sign in to comment.