Skip to content

Commit f62333e

Browse files
committed
reduce duplication
1 parent 34ba719 commit f62333e

2 files changed

Lines changed: 31 additions & 41 deletions

File tree

src/ast/query.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,10 +2823,7 @@ impl fmt::Display for PipeOperator {
28232823
} => Self::fmt_set_operation(f, "EXCEPT", set_quantifier, queries),
28242824
PipeOperator::Call { function, alias } => {
28252825
write!(f, "CALL {}", function)?;
2826-
if let Some(alias) = alias {
2827-
write!(f, " AS {}", alias)?;
2828-
}
2829-
Ok(())
2826+
Self::fmt_optional_alias(f, alias)
28302827
}
28312828
PipeOperator::Pivot {
28322829
aggregate_functions,
@@ -2841,10 +2838,7 @@ impl fmt::Display for PipeOperator {
28412838
Expr::CompoundIdentifier(value_column.to_vec()),
28422839
value_source
28432840
)?;
2844-
if let Some(alias) = alias {
2845-
write!(f, " AS {}", alias)?;
2846-
}
2847-
Ok(())
2841+
Self::fmt_optional_alias(f, alias)
28482842
}
28492843
PipeOperator::Unpivot {
28502844
value_column,
@@ -2859,16 +2853,21 @@ impl fmt::Display for PipeOperator {
28592853
name_column,
28602854
display_comma_separated(unpivot_columns)
28612855
)?;
2862-
if let Some(alias) = alias {
2863-
write!(f, " AS {}", alias)?;
2864-
}
2865-
Ok(())
2856+
Self::fmt_optional_alias(f, alias)
28662857
}
28672858
}
28682859
}
28692860
}
28702861

28712862
impl PipeOperator {
2863+
/// Helper function to format optional alias for pipe operators
2864+
fn fmt_optional_alias(f: &mut fmt::Formatter<'_>, alias: &Option<Ident>) -> fmt::Result {
2865+
if let Some(alias) = alias {
2866+
write!(f, " AS {}", alias)?;
2867+
}
2868+
Ok(())
2869+
}
2870+
28722871
/// Helper function to format set operations (UNION, INTERSECT, EXCEPT) with queries
28732872
fn fmt_set_operation(
28742873
f: &mut fmt::Formatter<'_>,

src/parser/mod.rs

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ const EOF_TOKEN: TokenWithSpan = TokenWithSpan {
195195
},
196196
};
197197

198+
// Error message constant for pipe operators that require DISTINCT
199+
const EXPECTED_FUNCTION_CALL_MSG: &str = "Expected function call after CALL";
200+
198201
/// Composite types declarations using angle brackets syntax can be arbitrary
199202
/// nested such that the following declaration is possible:
200203
/// `ARRAY<ARRAY<INT>>`
@@ -9965,6 +9968,19 @@ impl<'a> Parser<'a> {
99659968
})
99669969
}
99679970

9971+
/// Parse set quantifier for pipe operators that require DISTINCT (INTERSECT, EXCEPT)
9972+
fn parse_distinct_required_set_quantifier(&mut self, operator_name: &str) -> Result<SetQuantifier, ParserError> {
9973+
if self.parse_keywords(&[Keyword::DISTINCT, Keyword::BY, Keyword::NAME]) {
9974+
Ok(SetQuantifier::DistinctByName)
9975+
} else if self.parse_keyword(Keyword::DISTINCT) {
9976+
Ok(SetQuantifier::Distinct)
9977+
} else {
9978+
Err(ParserError::ParserError(
9979+
format!("{} pipe operator requires DISTINCT modifier", operator_name),
9980+
))
9981+
}
9982+
}
9983+
99689984
/// Parse optional alias (with or without AS keyword) for pipe operators
99699985
fn parse_optional_pipe_alias(&mut self) -> Result<Option<Ident>, ParserError> {
99709986
if self.parse_keyword(Keyword::AS) {
@@ -11199,35 +11215,15 @@ impl<'a> Parser<'a> {
1119911215
});
1120011216
}
1120111217
Keyword::INTERSECT => {
11202-
// BigQuery INTERSECT pipe operator requires DISTINCT modifier
11203-
let set_quantifier =
11204-
if self.parse_keywords(&[Keyword::DISTINCT, Keyword::BY, Keyword::NAME]) {
11205-
SetQuantifier::DistinctByName
11206-
} else if self.parse_keyword(Keyword::DISTINCT) {
11207-
SetQuantifier::Distinct
11208-
} else {
11209-
return Err(ParserError::ParserError(
11210-
"INTERSECT pipe operator requires DISTINCT modifier".to_string(),
11211-
));
11212-
};
11218+
let set_quantifier = self.parse_distinct_required_set_quantifier("INTERSECT")?;
1121311219
let queries = self.parse_pipe_operator_queries()?;
1121411220
pipe_operators.push(PipeOperator::Intersect {
1121511221
set_quantifier,
1121611222
queries,
1121711223
});
1121811224
}
1121911225
Keyword::EXCEPT => {
11220-
// BigQuery EXCEPT pipe operator requires DISTINCT modifier
11221-
let set_quantifier =
11222-
if self.parse_keywords(&[Keyword::DISTINCT, Keyword::BY, Keyword::NAME]) {
11223-
SetQuantifier::DistinctByName
11224-
} else if self.parse_keyword(Keyword::DISTINCT) {
11225-
SetQuantifier::Distinct
11226-
} else {
11227-
return Err(ParserError::ParserError(
11228-
"EXCEPT pipe operator requires DISTINCT modifier".to_string(),
11229-
));
11230-
};
11226+
let set_quantifier = self.parse_distinct_required_set_quantifier("EXCEPT")?;
1123111227
let queries = self.parse_pipe_operator_queries()?;
1123211228
pipe_operators.push(PipeOperator::Except {
1123311229
set_quantifier,
@@ -11239,16 +11235,11 @@ impl<'a> Parser<'a> {
1123911235
let function_expr = self.parse_function(function_name)?;
1124011236
// Extract Function from Expr::Function
1124111237
if let Expr::Function(function) = function_expr {
11242-
// Parse optional alias
11243-
let alias = if self.parse_keyword(Keyword::AS) {
11244-
Some(self.parse_identifier()?)
11245-
} else {
11246-
None
11247-
};
11238+
let alias = self.parse_optional_pipe_alias()?;
1124811239
pipe_operators.push(PipeOperator::Call { function, alias });
1124911240
} else {
1125011241
return Err(ParserError::ParserError(
11251-
"Expected function call after CALL".to_string(),
11242+
EXPECTED_FUNCTION_CALL_MSG.to_string(),
1125211243
));
1125311244
}
1125411245
}

0 commit comments

Comments
 (0)