MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / parse_optional_alias

Method parse_optional_alias

src/sql-parser/src/parser.rs:7180–7214  ·  view source on GitHub ↗

Parse `AS identifier` (or simply `identifier` if it's not a reserved keyword) Some examples with aliases: `SELECT 1 foo`, `SELECT COUNT(*) AS cnt`, `SELECT ... FROM t1 foo, t2 bar`, `SELECT ... FROM (...) AS bar`

(&mut self, is_reserved: F)

Source from the content-addressed store, hash-verified

7178 /// Some examples with aliases: `SELECT 1 foo`, `SELECT COUNT(*) AS cnt`,
7179 /// `SELECT ... FROM t1 foo, t2 bar`, `SELECT ... FROM (...) AS bar`
7180 fn parse_optional_alias<F>(&mut self, is_reserved: F) -> Result<Option<Ident>, ParserError>
7181 where
7182 F: FnOnce(Keyword) -> bool,
7183 {
7184 let after_as = self.parse_keyword(AS);
7185 match self.next_token() {
7186 // Do not accept `AS OF`, which is reserved for providing timestamp information
7187 // to queries.
7188 Some(Token::Keyword(OF)) => {
7189 self.prev_token();
7190 if after_as {
7191 self.prev_token();
7192 }
7193 Ok(None)
7194 }
7195 // Accept any other identifier after `AS` (though many dialects have restrictions on
7196 // keywords that may appear here). If there's no `AS`: don't parse keywords,
7197 // which may start a construct allowed in this position, to be parsed as aliases.
7198 // (For example, in `FROM t1 JOIN` the `JOIN` will always be parsed as a keyword,
7199 // not an alias.)
7200 Some(Token::Keyword(kw)) if after_as || !is_reserved(kw) => Ok(Some(kw.into())),
7201 Some(Token::Ident(id)) => Ok(Some(self.new_identifier(id)?)),
7202 not_an_ident => {
7203 if after_as {
7204 return self.expected(
7205 self.peek_prev_pos(),
7206 "an identifier after AS",
7207 not_an_ident,
7208 );
7209 }
7210 self.prev_token();
7211 Ok(None) // no alias found
7212 }
7213 }
7214 }
7215
7216 /// Parse `AS identifier` when the AS is describing a table-valued object,
7217 /// like in `... FROM generate_series(1, 10) AS t (col)`. In this case

Callers 4

parse_join_constraintMethod · 0.80
parse_updateMethod · 0.80
parse_select_itemMethod · 0.80

Calls 6

parse_keywordMethod · 0.80
next_tokenMethod · 0.80
prev_tokenMethod · 0.80
new_identifierMethod · 0.80
expectedMethod · 0.80
peek_prev_posMethod · 0.80

Tested by

no test coverage detected