Skip to content

Commit ef04565

Browse files
committed
Add support for table sample in Clickhouse
1 parent f58f9bb commit ef04565

File tree

5 files changed

+158
-172
lines changed

5 files changed

+158
-172
lines changed

src/ast/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ pub use self::query::{
6969
OrderBy, OrderByExpr, PivotValueSource, ProjectionSelect, Query, RenameSelectItem,
7070
RepetitionQuantifier, ReplaceSelectElement, ReplaceSelectItem, RowsPerMatch, Select,
7171
SelectInto, SelectItem, SetExpr, SetOperator, SetQuantifier, Setting, SymbolDefinition, Table,
72-
TableAlias, TableAliasColumnDef, TableFactor, TableFunctionArgs, TableSampleBernoulli,
73-
TableSampleBucket, TableSampleImplicit, TableSampleKind, TableSampleMethod,
74-
TableSampleMethodName, TableSampleSeed, TableSampleSeedModifier, TableSampleSystem,
75-
TableSampleUnit, TableVersion, TableWithJoins, Top, TopQuantity, ValueTableMode, Values,
76-
WildcardAdditionalOptions, With, WithFill,
72+
TableAlias, TableAliasColumnDef, TableFactor, TableFunctionArgs, TableSample,
73+
TableSampleBucket, TableSampleKind, TableSampleMethod, TableSampleModifier,
74+
TableSampleQuantity, TableSampleSeed, TableSampleSeedModifier, TableSampleUnit, TableVersion,
75+
TableWithJoins, Top, TopQuantity, ValueTableMode, Values, WildcardAdditionalOptions, With,
76+
WithFill,
7777
};
7878

7979
pub use self::trigger::{

src/ast/query.rs

+62-74
Original file line numberDiff line numberDiff line change
@@ -1156,86 +1156,85 @@ pub enum TableFactor {
11561156

11571157
pub enum TableSampleKind {
11581158
/// Table sample located before the table alias option
1159-
BeforeTableAlias(Box<TableSampleMethod>),
1159+
BeforeTableAlias(Box<TableSample>),
11601160
/// Table sample located after the table alias option
1161-
AfterTableAlias(Box<TableSampleMethod>),
1161+
AfterTableAlias(Box<TableSample>),
11621162
}
11631163

1164-
/// The table sample method options
11651164
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
11661165
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11671166
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1168-
pub enum TableSampleMethod {
1169-
Bernoulli(TableSampleBernoulli),
1170-
System(TableSampleSystem),
1171-
Bucket(TableSampleBucket),
1172-
Implicit(TableSampleImplicit),
1167+
pub struct TableSample {
1168+
pub modifier: TableSampleModifier,
1169+
pub name: Option<TableSampleMethod>,
1170+
pub quantity: Option<TableSampleQuantity>,
1171+
pub seed: Option<TableSampleSeed>,
1172+
pub bucket: Option<TableSampleBucket>,
1173+
pub offset: Option<Expr>,
11731174
}
11741175

1175-
/// The table sample method names
11761176
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
11771177
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11781178
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1179-
pub enum TableSampleMethodName {
1180-
Row,
1181-
Bernoulli,
1182-
System,
1183-
Block,
1179+
pub enum TableSampleModifier {
1180+
Sample,
1181+
TableSample,
11841182
}
11851183

1186-
impl fmt::Display for TableSampleMethodName {
1184+
impl fmt::Display for TableSampleModifier {
11871185
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11881186
match self {
1189-
TableSampleMethodName::Bernoulli => write!(f, "BERNOULLI"),
1190-
TableSampleMethodName::Row => write!(f, "ROW"),
1191-
TableSampleMethodName::System => write!(f, "SYSTEM"),
1192-
TableSampleMethodName::Block => write!(f, "BLOCK"),
1187+
TableSampleModifier::Sample => write!(f, "SAMPLE")?,
1188+
TableSampleModifier::TableSample => write!(f, "TABLESAMPLE")?,
11931189
}
1190+
Ok(())
11941191
}
11951192
}
11961193

11971194
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
11981195
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11991196
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1200-
pub struct TableSampleBernoulli {
1201-
pub name: TableSampleMethodName,
1202-
pub probability: Option<Value>,
1203-
pub value: Option<Value>,
1197+
pub struct TableSampleQuantity {
1198+
pub parenthesized: bool,
1199+
pub value: Expr,
12041200
pub unit: Option<TableSampleUnit>,
12051201
}
12061202

1207-
impl fmt::Display for TableSampleBernoulli {
1203+
impl fmt::Display for TableSampleQuantity {
12081204
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1209-
write!(f, " {} (", self.name)?;
1210-
if let Some(probability) = &self.probability {
1211-
write!(f, "{})", probability)?;
1212-
} else if let Some(value) = &self.value {
1213-
write!(f, "{}", value)?;
1214-
if let Some(unit) = &self.unit {
1215-
write!(f, " {}", unit)?;
1216-
}
1205+
if self.parenthesized {
1206+
write!(f, "(")?;
1207+
}
1208+
write!(f, "{}", self.value)?;
1209+
if let Some(unit) = &self.unit {
1210+
write!(f, " {}", unit)?;
1211+
}
1212+
if self.parenthesized {
12171213
write!(f, ")")?;
12181214
}
12191215
Ok(())
12201216
}
12211217
}
12221218

1219+
/// The table sample method names
12231220
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
12241221
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
12251222
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1226-
pub struct TableSampleSystem {
1227-
pub name: TableSampleMethodName,
1228-
pub probability: Value,
1229-
pub seed: Option<TableSampleSeed>,
1223+
pub enum TableSampleMethod {
1224+
Row,
1225+
Bernoulli,
1226+
System,
1227+
Block,
12301228
}
12311229

1232-
impl fmt::Display for TableSampleSystem {
1230+
impl fmt::Display for TableSampleMethod {
12331231
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1234-
write!(f, " {} ({})", self.name, self.probability)?;
1235-
if let Some(seed) = &self.seed {
1236-
write!(f, " {} ({})", seed.modifier, seed.value)?;
1232+
match self {
1233+
TableSampleMethod::Bernoulli => write!(f, "BERNOULLI"),
1234+
TableSampleMethod::Row => write!(f, "ROW"),
1235+
TableSampleMethod::System => write!(f, "SYSTEM"),
1236+
TableSampleMethod::Block => write!(f, "BLOCK"),
12371237
}
1238-
Ok(())
12391238
}
12401239
}
12411240

@@ -1247,6 +1246,13 @@ pub struct TableSampleSeed {
12471246
pub value: Value,
12481247
}
12491248

1249+
impl fmt::Display for TableSampleSeed {
1250+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1251+
write!(f, "{} ({})", self.modifier, self.value)?;
1252+
Ok(())
1253+
}
1254+
}
1255+
12501256
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
12511257
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
12521258
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
@@ -1299,41 +1305,23 @@ impl fmt::Display for TableSampleBucket {
12991305
Ok(())
13001306
}
13011307
}
1302-
1303-
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1304-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1305-
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1306-
pub struct TableSampleImplicit {
1307-
pub value: Value,
1308-
pub unit: Option<TableSampleUnit>,
1309-
}
1310-
1311-
impl fmt::Display for TableSampleImplicit {
1308+
impl fmt::Display for TableSample {
13121309
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1313-
write!(f, "{}", self.value)?;
1314-
if let Some(unit) = &self.unit {
1315-
write!(f, " {}", unit)?;
1310+
write!(f, " {}", self.modifier)?;
1311+
if let Some(name) = &self.name {
1312+
write!(f, " {}", name)?;
13161313
}
1317-
Ok(())
1318-
}
1319-
}
1320-
1321-
impl fmt::Display for TableSampleMethod {
1322-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1323-
write!(f, " TABLESAMPLE")?;
1324-
match self {
1325-
TableSampleMethod::Bernoulli(sample) => {
1326-
write!(f, "{}", sample)?;
1327-
}
1328-
TableSampleMethod::System(sample) => {
1329-
write!(f, "{}", sample)?;
1330-
}
1331-
TableSampleMethod::Bucket(sample) => {
1332-
write!(f, " ({})", sample)?;
1333-
}
1334-
TableSampleMethod::Implicit(sample) => {
1335-
write!(f, " ({})", sample)?;
1336-
}
1314+
if let Some(quantity) = &self.quantity {
1315+
write!(f, " {}", quantity)?;
1316+
}
1317+
if let Some(seed) = &self.seed {
1318+
write!(f, " {}", seed)?;
1319+
}
1320+
if let Some(bucket) = &self.bucket {
1321+
write!(f, " ({})", bucket)?;
1322+
}
1323+
if let Some(offset) = &self.offset {
1324+
write!(f, " OFFSET {}", offset)?;
13371325
}
13381326
Ok(())
13391327
}

0 commit comments

Comments
 (0)