Skip to content

Commit ca3070b

Browse files
committed
readyset-sql: Convert TRUNCATE
Change-Id: I9fe4f8887b7a9ded62170ad0b451543ac0130359
1 parent 2b98344 commit ca3070b

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

readyset-sql/src/ast/query.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ impl TryFromDialect<sqlparser::ast::Statement> for SqlQuery {
187187
Rollback { .. } => Ok(Self::Rollback(RollbackStatement {})),
188188
Commit { .. } => Ok(Self::Commit(CommitStatement {})),
189189
alter @ AlterTable { .. } => Ok(Self::AlterTable(alter.try_into_dialect(dialect)?)),
190+
truncate @ Truncate { .. } => Ok(Self::Truncate(truncate.try_into_dialect(dialect)?)),
190191
CreateDatabase { .. } => skipped!("CREATE DATABASE"),
191192
_ => not_yet_implemented!("other query: {value:?}"),
192193
}

readyset-sql/src/ast/truncate.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use readyset_util::fmt::fmt_with;
55
use serde::{Deserialize, Serialize};
66
use test_strategy::Arbitrary;
77

8-
use crate::{ast::*, Dialect, DialectDisplay};
8+
use crate::{ast::*, AstConversionError, Dialect, DialectDisplay, IntoDialect, TryFromDialect};
99

1010
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, Arbitrary)]
1111

@@ -21,6 +21,42 @@ pub struct TruncateStatement {
2121
pub cascade: bool,
2222
}
2323

24+
impl TryFromDialect<sqlparser::ast::Statement> for TruncateStatement {
25+
fn try_from_dialect(
26+
value: sqlparser::ast::Statement,
27+
dialect: Dialect,
28+
) -> Result<Self, AstConversionError> {
29+
if let sqlparser::ast::Statement::Truncate {
30+
table_names,
31+
partitions: _,
32+
table: _,
33+
// XXX(mvzink): I believe sqlparser is incorrect here; ONLY should be on each table
34+
// target, not the whole statement. We interpret it as applying to all tables, but
35+
// should probably upstream a fix (or verify that sqlparser is correct).
36+
only,
37+
identity,
38+
cascade,
39+
on_cluster: _,
40+
} = value
41+
{
42+
let tables = table_names
43+
.into_iter()
44+
.map(|table| TruncateTable {
45+
relation: table.name.into_dialect(dialect),
46+
only,
47+
})
48+
.collect();
49+
Ok(Self {
50+
tables,
51+
restart_identity: identity == Some(sqlparser::ast::TruncateIdentityOption::Restart),
52+
cascade: cascade == Some(sqlparser::ast::CascadeOption::Cascade),
53+
})
54+
} else {
55+
failed!("Should only be called on TRUNCATE statement, got {value:?}")
56+
}
57+
}
58+
}
59+
2460
impl DialectDisplay for TruncateStatement {
2561
fn display(&self, dialect: Dialect) -> impl fmt::Display + '_ {
2662
fmt_with(move |f| {

0 commit comments

Comments
 (0)