Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::Parser Class Reference

The parser is responsible for parsing the query and converting it into a set of parsed statements. The parsed statements can then be converted into a plan and executed. More...

Collaboration diagram for duckdb::Parser:

Public Member Functions

 Parser (ParserOptions options=ParserOptions())
void ParseQuery (const string &query)
 Attempts to parse a query into a series of SQL statements. Returns whether or not the parsing was successful. If the parsing was successful, the parsed statements will be stored in the statements variable.
unique_ptr< SQLStatement > GetStatement (const string &query)
void ThrowParserOverrideError (ParserOverrideResult &result)

Static Public Member Functions

static vector< SimplifiedToken > Tokenize (const string &query)
 Tokenize a query, returning the raw tokens together with their locations.
static vector< SimplifiedToken > TokenizeError (const string &error_msg)
 Tokenize an error message, returning the raw tokens together with their locations.
static KeywordCategory IsKeyword (const string &text)
 Returns true if the given text matches a keyword of the parser.
static vector< ParserKeyword > KeywordList ()
 Returns a list of all keywords in the parser.
static DUCKDB_API vector< unique_ptr< ParsedExpression > > ParseExpressionList (const string &select_list, ParserOptions options=ParserOptions())
 Parses a list of expressions (i.e. the list found in a SELECT clause).
static GroupByNode ParseGroupByList (const string &group_by, ParserOptions options=ParserOptions())
 Parses a list of GROUP BY expressions.
static vector< OrderByNode > ParseOrderList (const string &select_list, ParserOptions options=ParserOptions())
 Parses a list as found in an ORDER BY expression (i.e. including optional ASCENDING/DESCENDING modifiers).
static void ParseUpdateList (const string &update_list, vector< string > &update_columns, vector< unique_ptr< ParsedExpression > > &expressions, ParserOptions options=ParserOptions())
 Parses an update list (i.e. the list found in the SET clause of an UPDATE statement).
static vector< vector< unique_ptr< ParsedExpression > > > ParseValuesList (const string &value_list, ParserOptions options=ParserOptions())
 Parses a VALUES list (i.e. the list of expressions after a VALUES clause).
static ColumnList ParseColumnList (const string &column_list, ParserOptions options=ParserOptions())
 Parses a column list (i.e. as found in a CREATE TABLE statement).
static ColumnDefinition ParseColumnDefinition (const string &column_definition, ParserOptions options=ParserOptions())
static bool StripUnicodeSpaces (const string &query_str, string &new_query)

Public Attributes

vector< unique_ptr< SQLStatement > > statements
 The parsed SQL statements from an invocation to ParseQuery.

Private Attributes

ParserOptions options

Detailed Description

The parser is responsible for parsing the query and converting it into a set of parsed statements. The parsed statements can then be converted into a plan and executed.

Constructor & Destructor Documentation

◆ Parser()

duckdb::Parser::Parser ( ParserOptions options = ParserOptions())
explicit
346968 : options(options_p) {
346969}

Member Function Documentation

◆ ParseQuery()

void duckdb::Parser::ParseQuery ( const string & query)

Attempts to parse a query into a series of SQL statements. Returns whether or not the parsing was successful. If the parsing was successful, the parsed statements will be stored in the statements variable.

347169 {
347170 Transformer transformer(options);
347171 string parser_error;
347172 optional_idx parser_error_location;
347173 {
347174 // check if there are any unicode spaces in the string
347175 string new_query;
347176 if (StripUnicodeSpaces(query, new_query)) {
347177 // there are - strip the unicode spaces and re-run the query
347178 ParseQuery(new_query);
347179 return;
347180 }
347181 }
347182 {
347183 if (options.extensions) {
347184 bool has_strict_extension_error = false;
347185 ErrorData last_strict_extension_error;
347186 for (auto &ext : options.extensions->ParserExtensions()) {
347187 if (!ext.parser_override) {
347188 continue;
347189 }
347190 if (options.parser_override_setting == AllowParserOverride::DEFAULT_OVERRIDE) {
347191 continue;
347192 }
347193
347194 auto result = ext.parser_override(ext.parser_info.get(), query, options);
347195 if (result.type == ParserExtensionResultType::PARSE_SUCCESSFUL) {
347196 statements = std::move(result.statements);
347197 return;
347198 }
347199 if (options.parser_override_setting == AllowParserOverride::STRICT_OVERRIDE) {
347200 if (result.type == ParserExtensionResultType::DISPLAY_EXTENSION_ERROR) {
347201 has_strict_extension_error = true;
347202 last_strict_extension_error = std::move(result.error);
347203 } else {
347204 has_strict_extension_error = false;
347205 }
347206 continue;
347207 } else if (options.parser_override_setting == AllowParserOverride::FALLBACK_OVERRIDE) {
347208 continue;
347209 }
347210 }
347211 if (options.parser_override_setting == AllowParserOverride::STRICT_OVERRIDE && has_strict_extension_error) {
347212 last_strict_extension_error.Throw();
347213 }
347214 }
347215 PostgresParser::SetPreserveIdentifierCase(options.preserve_identifier_case);
347216 bool parsing_succeed = false;
347217 // Creating a new scope to prevent multiple PostgresParser destructors being called
347218 // which led to some memory issues
347219 {
347220 PostgresParser parser;
347221 parser.Parse(query);
347222 if (parser.success) {
347223 if (!parser.parse_tree) {
347224 // empty statement
347225 return;
347226 }
347227
347228 // if it succeeded, we transform the Postgres parse tree into a list of
347229 // SQLStatements
347230 transformer.TransformParseTree(parser.parse_tree, statements);
347231 parsing_succeed = true;
347232 } else {
347233 parser_error = parser.error_message;
347234 if (parser.error_location > 0) {
347235 parser_error_location = NumericCast<idx_t>(parser.error_location - 1);
347236 }
347237 }
347238 }
347239 // If DuckDB fails to parse the entire sql string, break the string down into individual statements
347240 // using ';' as the delimiter so that parser extensions can parse the statement
347241 if (parsing_succeed) {
347242 // no-op
347243 // return here would require refactoring into another function. o.w. will just no-op in order to run wrap up
347244 // code at the end of this function
347245 } else if (!options.extensions || !options.extensions->HasParserExtensions()) {
347246 throw ParserException::SyntaxError(query, parser_error, parser_error_location);
347247 } else {
347248 // split sql string into statements and re-parse using extension
347249 auto queries = SplitQueries(query);
347250 idx_t stmt_loc = 0;
347251 for (auto const &query_statement : queries) {
347252 ErrorData another_parser_error;
347253 // Creating a new scope to allow extensions to use PostgresParser, which is not reentrant
347254 {
347255 PostgresParser another_parser;
347256 another_parser.Parse(query_statement);
347257 // LCOV_EXCL_START
347258 // first see if DuckDB can parse this individual query statement
347259 if (another_parser.success) {
347260 if (!another_parser.parse_tree) {
347261 // empty statement
347262 continue;
347263 }
347264 transformer.TransformParseTree(another_parser.parse_tree, statements);
347265 // important to set in the case of a mixture of DDB and parser ext statements
347266 statements.back()->stmt_length = query_statement.size() - 1;
347267 statements.back()->stmt_location = stmt_loc;
347268 stmt_loc += query_statement.size();
347269 continue;
347270 } else {
347271 another_parser_error = ErrorData(another_parser.error_message);
347272 if (another_parser.error_location > 0) {
347273 another_parser_error.AddQueryLocation(
347274 NumericCast<idx_t>(another_parser.error_location - 1));
347275 }
347276 }
347277 } // LCOV_EXCL_STOP
347278 // LCOV_EXCL_START
347279 // let extensions parse the statement which DuckDB failed to parse
347280 bool parsed_single_statement = false;
347281 for (auto &ext : options.extensions->ParserExtensions()) {
347282 D_ASSERT(!parsed_single_statement);
347283 if (!ext.parse_function) {
347284 continue;
347285 }
347286 auto result = ext.parse_function(ext.parser_info.get(), query_statement);
347287 if (result.type == ParserExtensionResultType::PARSE_SUCCESSFUL) {
347288 auto statement = make_uniq<ExtensionStatement>(ext, std::move(result.parse_data));
347289 statement->stmt_length = query_statement.size() - 1;
347290 statement->stmt_location = stmt_loc;
347291 stmt_loc += query_statement.size();
347292 statements.push_back(std::move(statement));
347293 parsed_single_statement = true;
347294 break;
347295 } else if (result.type == ParserExtensionResultType::DISPLAY_EXTENSION_ERROR) {
347296 throw ParserException::SyntaxError(query, result.error, result.error_location);
347297 } else {
347298 // We move to the next one!
347299 }
347300 }
347301 if (!parsed_single_statement) {
347302 throw ParserException::SyntaxError(query, parser_error, parser_error_location);
347303 } // LCOV_EXCL_STOP
347304 }
347305 }
347306 }
347307 if (!statements.empty()) {
347308 auto &last_statement = statements.back();
347309 last_statement->stmt_length = query.size() - last_statement->stmt_location;
347310 for (auto &statement : statements) {
347311 statement->query = query.substr(statement->stmt_location, statement->stmt_length);
347312 statement->stmt_location = 0;
347313 statement->stmt_length = statement->query.size();
347314 if (statement->type == StatementType::CREATE_STATEMENT) {
347315 auto &create = statement->Cast<CreateStatement>();
347316 create.info->sql = statement->query;
347317 }
347318 }
347319 }
347320}
DUCKDB_API void Throw(const string &prepended_message="") const
Throw the error.
Definition duckdb.cpp:70200
vector< unique_ptr< SQLStatement > > statements
The parsed SQL statements from an invocation to ParseQuery.
Definition duckdb.cpp:23954
void ParseQuery(const string &query)
Attempts to parse a query into a series of SQL statements. Returns whether or not the parsing was suc...
Definition duckdb.cpp:347169
static cv::Ptr< QuasiDenseStereo > create(cv::Size monoImgSize, cv::String paramFilepath=cv::String())
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Tokenize()

vector< SimplifiedToken > duckdb::Parser::Tokenize ( const string & query)
static

Tokenize a query, returning the raw tokens together with their locations.

347322 {
347323 auto pg_tokens = PostgresParser::Tokenize(query);
347324 vector<SimplifiedToken> result;
347325 result.reserve(pg_tokens.size());
347326 for (auto &pg_token : pg_tokens) {
347327 SimplifiedToken token;
347328 switch (pg_token.type) {
347329 case duckdb_libpgquery::PGSimplifiedTokenType::PG_SIMPLIFIED_TOKEN_IDENTIFIER:
347330 token.type = SimplifiedTokenType::SIMPLIFIED_TOKEN_IDENTIFIER;
347331 break;
347332 case duckdb_libpgquery::PGSimplifiedTokenType::PG_SIMPLIFIED_TOKEN_NUMERIC_CONSTANT:
347333 token.type = SimplifiedTokenType::SIMPLIFIED_TOKEN_NUMERIC_CONSTANT;
347334 break;
347335 case duckdb_libpgquery::PGSimplifiedTokenType::PG_SIMPLIFIED_TOKEN_STRING_CONSTANT:
347336 token.type = SimplifiedTokenType::SIMPLIFIED_TOKEN_STRING_CONSTANT;
347337 break;
347338 case duckdb_libpgquery::PGSimplifiedTokenType::PG_SIMPLIFIED_TOKEN_OPERATOR:
347339 token.type = SimplifiedTokenType::SIMPLIFIED_TOKEN_OPERATOR;
347340 break;
347341 case duckdb_libpgquery::PGSimplifiedTokenType::PG_SIMPLIFIED_TOKEN_KEYWORD:
347342 token.type = SimplifiedTokenType::SIMPLIFIED_TOKEN_KEYWORD;
347343 break;
347344 // comments are not supported by our tokenizer right now
347345 case duckdb_libpgquery::PGSimplifiedTokenType::PG_SIMPLIFIED_TOKEN_COMMENT: // LCOV_EXCL_START
347346 token.type = SimplifiedTokenType::SIMPLIFIED_TOKEN_COMMENT;
347347 break;
347348 default:
347349 throw InternalException("Unrecognized token category");
347350 } // LCOV_EXCL_STOP
347351 token.start = NumericCast<idx_t>(pg_token.start);
347352 result.push_back(token);
347353 }
347354 return result;
347355}
Here is the caller graph for this function:

◆ TokenizeError()

vector< SimplifiedToken > duckdb::Parser::TokenizeError ( const string & error_msg)
static

Tokenize an error message, returning the raw tokens together with their locations.

347357 {
347358 idx_t error_start = 0;
347359 idx_t error_end = error_msg.size();
347360
347361 vector<SimplifiedToken> tokens;
347362 // find "XXX Error:" - this marks the start of the error message
347363 auto error = StringUtil::Find(error_msg, "Error:");
347364 if (error.IsValid()) {
347365 SimplifiedToken token;
347366 token.type = SimplifiedTokenType::SIMPLIFIED_TOKEN_ERROR_EMPHASIS;
347367 token.start = 0;
347368 tokens.push_back(token);
347369
347370 token.type = SimplifiedTokenType::SIMPLIFIED_TOKEN_ERROR;
347371 token.start = error.GetIndex() + 6;
347372 tokens.push_back(token);
347373
347374 error_start = error.GetIndex() + 6;
347375 } else {
347376 SimplifiedToken token;
347377 token.type = SimplifiedTokenType::SIMPLIFIED_TOKEN_ERROR;
347378 token.start = 0;
347379 tokens.push_back(token);
347380 }
347381
347382 // find "LINE (number)" - this marks the end of the message
347383 auto line_pos = StringUtil::Find(error_msg, "\nLINE ");
347384 if (line_pos.IsValid()) {
347385 // tokenize between
347386 error_end = line_pos.GetIndex();
347387 }
347388
347389 // now iterate over the
347390 bool in_quotes = false;
347391 char quote_char = '\0';
347392 for (idx_t i = error_start; i < error_end; i++) {
347393 if (in_quotes) {
347394 // in a quote - look for the quote character
347395 if (error_msg[i] == quote_char) {
347396 SimplifiedToken token;
347397 token.start = i;
347398 token.type = SimplifiedTokenType::SIMPLIFIED_TOKEN_ERROR;
347399 tokens.push_back(token);
347400 in_quotes = false;
347401 }
347402 if (StringUtil::CharacterIsNewline(error_msg[i])) {
347403 // found a newline in a quote, abort the quoted state entirely
347404 tokens.pop_back();
347405 in_quotes = false;
347406 }
347407 } else if (error_msg[i] == '"' || error_msg[i] == '\'') {
347408 // not quoted and found a quote - enter the quoted state
347409 SimplifiedToken token;
347410 token.start = i;
347411 token.type = SimplifiedTokenType::SIMPLIFIED_TOKEN_ERROR_SUGGESTION;
347412 token.start++;
347413 tokens.push_back(token);
347414 quote_char = error_msg[i];
347415 in_quotes = true;
347416 }
347417 }
347418 if (in_quotes) {
347419 // unterminated quotes at the end of the error - pop back the quoted state
347420 tokens.pop_back();
347421 }
347422 if (line_pos.IsValid()) {
347423 SimplifiedToken token;
347424 token.start = line_pos.GetIndex() + 1;
347425 token.type = SimplifiedTokenType::SIMPLIFIED_TOKEN_ERROR_EMPHASIS;
347426 tokens.push_back(token);
347427
347428 // tokenize the LINE part
347429 idx_t query_start;
347430 for (query_start = line_pos.GetIndex() + 6; query_start < error_msg.size(); query_start++) {
347431 if (error_msg[query_start] != ':' && !StringUtil::CharacterIsDigit(error_msg[query_start])) {
347432 break;
347433 }
347434 }
347435 if (query_start < error_msg.size()) {
347436 token.start = query_start;
347437 token.type = SimplifiedTokenType::SIMPLIFIED_TOKEN_ERROR;
347438 tokens.push_back(token);
347439
347440 idx_t query_end;
347441 for (query_end = query_start; query_end < error_msg.size(); query_end++) {
347442 if (error_msg[query_end] == '\n') {
347443 break;
347444 }
347445 }
347446 // after LINE XXX: comes a caret - look for it
347447 idx_t caret_position = error_msg.size();
347448 bool place_caret = false;
347449 idx_t caret_start = query_end + 1;
347450 if (caret_start < error_msg.size()) {
347451 for (idx_t i = caret_start; i < error_msg.size(); i++) {
347452 if (error_msg[i] == '^') {
347453 // found the caret
347454 // to get the caret position in the query we need to
347455 caret_position = i - caret_start - ((query_start - line_pos.GetIndex()) - 1);
347456 place_caret = true;
347457 break;
347458 }
347459 }
347460 }
347461
347462 // tokenize the actual query
347463 string query = error_msg.substr(query_start, query_end - query_start);
347464 auto query_tokens = Tokenize(query);
347465 for (auto &query_token : query_tokens) {
347466 if (place_caret) {
347467 // find the caret position and highlight the identifier it points to
347468 if (query_token.start >= caret_position) {
347469 // we need to place the caret here
347470 query_token.start = query_start + caret_position;
347471 query_token.type = SimplifiedTokenType::SIMPLIFIED_TOKEN_ERROR_EMPHASIS;
347472 tokens.push_back(query_token);
347473
347474 place_caret = false;
347475 continue;
347476 }
347477 }
347478 switch (query_token.type) {
347479 case SimplifiedTokenType::SIMPLIFIED_TOKEN_KEYWORD:
347480 query_token.type = SimplifiedTokenType::SIMPLIFIED_TOKEN_ERROR_EMPHASIS;
347481 break;
347482 default:
347483 query_token.type = SimplifiedTokenType::SIMPLIFIED_TOKEN_ERROR;
347484 break;
347485 }
347486 query_token.start += query_start;
347487 tokens.push_back(query_token);
347488 }
347489 if (query_end < error_msg.size()) {
347490 token.start = query_end;
347491 token.type = SimplifiedTokenType::SIMPLIFIED_TOKEN_ERROR;
347492 tokens.push_back(token);
347493 }
347494 }
347495 }
347496 return tokens;
347497}
static vector< SimplifiedToken > Tokenize(const string &query)
Tokenize a query, returning the raw tokens together with their locations.
Definition duckdb.cpp:347322
static DUCKDB_API optional_idx Find(const string &haystack, const string &needle)
Returns the position of needle string within the haystack.
Definition duckdb.cpp:109327
Here is the call graph for this function:

◆ IsKeyword()

KeywordCategory duckdb::Parser::IsKeyword ( const string & text)
static

Returns true if the given text matches a keyword of the parser.

347516 {
347517 return ToKeywordCategory(PostgresParser::IsKeyword(text));
347518}
Here is the caller graph for this function:

◆ KeywordList()

vector< ParserKeyword > duckdb::Parser::KeywordList ( )
static

Returns a list of all keywords in the parser.

347520 {
347521 auto keywords = PostgresParser::KeywordList();
347522 vector<ParserKeyword> result;
347523 for (auto &kw : keywords) {
347524 ParserKeyword res;
347525 res.name = kw.text;
347526 res.category = ToKeywordCategory(kw.category);
347527 result.push_back(res);
347528 }
347529 return result;
347530}

◆ ParseExpressionList()

vector< unique_ptr< ParsedExpression > > duckdb::Parser::ParseExpressionList ( const string & select_list,
ParserOptions options = ParserOptions() )
static

Parses a list of expressions (i.e. the list found in a SELECT clause).

347532 {
347533 // construct a mock query prefixed with SELECT
347534 string mock_query = "SELECT " + select_list;
347535 // parse the query
347536 Parser parser(options);
347537 parser.ParseQuery(mock_query);
347538 // check the statements
347539 if (parser.statements.size() != 1 || parser.statements[0]->type != StatementType::SELECT_STATEMENT) {
347540 throw ParserException("Expected a single SELECT statement");
347541 }
347542 auto &select = parser.statements[0]->Cast<SelectStatement>();
347543 if (select.node->type != QueryNodeType::SELECT_NODE) {
347544 throw ParserException("Expected a single SELECT node");
347545 }
347546 auto &select_node = select.node->Cast<SelectNode>();
347547 if (!select_node.modifiers.empty()) {
347548 throw ParserException("Cannot have any modifiers in the expression list");
347549 }
347550 if (select_node.where_clause) {
347551 throw ParserException("Cannot have a WHERE clause in the expression list");
347552 }
347553 if (!select_node.groups.group_expressions.empty()) {
347554 throw ParserException("Cannot have a GROUP BY clause in the expression list");
347555 }
347556 if (select_node.having) {
347557 throw ParserException("Cannot have a HAVING clause in the expression list");
347558 }
347559 if (select_node.qualify) {
347560 throw ParserException("Cannot have a QUALIFY clause in the expression list");
347561 }
347562 if (select_node.sample) {
347563 throw ParserException("Cannot have a SAMPLE clause in the expression list");
347564 }
347565 return std::move(select_node.select_list);
347566}
GMat select(const GMat &src1, const GMat &src2, const GMat &mask)

◆ ParseGroupByList()

GroupByNode duckdb::Parser::ParseGroupByList ( const string & group_by,
ParserOptions options = ParserOptions() )
static

Parses a list of GROUP BY expressions.

347568 {
347569 // construct a mock SELECT query with our group_by expressions
347570 string mock_query = StringUtil::Format("SELECT 42 GROUP BY %s", group_by);
347571 // parse the query
347572 Parser parser(options);
347573 parser.ParseQuery(mock_query);
347574 // check the result
347575 if (parser.statements.size() != 1 || parser.statements[0]->type != StatementType::SELECT_STATEMENT) {
347576 throw ParserException("Expected a single SELECT statement");
347577 }
347578 auto &select = parser.statements[0]->Cast<SelectStatement>();
347579 D_ASSERT(select.node->type == QueryNodeType::SELECT_NODE);
347580 auto &select_node = select.node->Cast<SelectNode>();
347581 return std::move(select_node.groups);
347582}
static string Format(const string fmt_str, ARGS... params)
Format a string using printf semantics.
Definition duckdb.hpp:4002
Here is the call graph for this function:

◆ ParseOrderList()

vector< OrderByNode > duckdb::Parser::ParseOrderList ( const string & select_list,
ParserOptions options = ParserOptions() )
static

Parses a list as found in an ORDER BY expression (i.e. including optional ASCENDING/DESCENDING modifiers).

347584 {
347585 // construct a mock query
347586 string mock_query = "SELECT * FROM tbl ORDER BY " + select_list;
347587 // parse the query
347588 Parser parser(options);
347589 parser.ParseQuery(mock_query);
347590 // check the statements
347591 if (parser.statements.size() != 1 || parser.statements[0]->type != StatementType::SELECT_STATEMENT) {
347592 throw ParserException("Expected a single SELECT statement");
347593 }
347594 auto &select = parser.statements[0]->Cast<SelectStatement>();
347595 D_ASSERT(select.node->type == QueryNodeType::SELECT_NODE);
347596 auto &select_node = select.node->Cast<SelectNode>();
347597 if (select_node.modifiers.empty() || select_node.modifiers[0]->type != ResultModifierType::ORDER_MODIFIER ||
347598 select_node.modifiers.size() != 1) {
347599 throw ParserException("Expected a single ORDER clause");
347600 }
347601 auto &order = select_node.modifiers[0]->Cast<OrderModifier>();
347602 return std::move(order.orders);
347603}

◆ ParseUpdateList()

void duckdb::Parser::ParseUpdateList ( const string & update_list,
vector< string > & update_columns,
vector< unique_ptr< ParsedExpression > > & expressions,
ParserOptions options = ParserOptions() )
static

Parses an update list (i.e. the list found in the SET clause of an UPDATE statement).

347606 {
347607 // construct a mock query
347608 string mock_query = "UPDATE tbl SET " + update_list;
347609 // parse the query
347610 Parser parser(options);
347611 parser.ParseQuery(mock_query);
347612 // check the statements
347613 if (parser.statements.size() != 1 || parser.statements[0]->type != StatementType::UPDATE_STATEMENT) {
347614 throw ParserException("Expected a single UPDATE statement");
347615 }
347616 auto &update = parser.statements[0]->Cast<UpdateStatement>();
347617 update_columns = std::move(update.set_info->columns);
347618 expressions = std::move(update.set_info->expressions);
347619}
Here is the caller graph for this function:

◆ ParseValuesList()

vector< vector< unique_ptr< ParsedExpression > > > duckdb::Parser::ParseValuesList ( const string & value_list,
ParserOptions options = ParserOptions() )
static

Parses a VALUES list (i.e. the list of expressions after a VALUES clause).

347621 {
347622 // construct a mock query
347623 string mock_query = "VALUES " + value_list;
347624 // parse the query
347625 Parser parser(options);
347626 parser.ParseQuery(mock_query);
347627 // check the statements
347628 if (parser.statements.size() != 1 || parser.statements[0]->type != StatementType::SELECT_STATEMENT) {
347629 throw ParserException("Expected a single SELECT statement");
347630 }
347631 auto &select = parser.statements[0]->Cast<SelectStatement>();
347632 if (select.node->type != QueryNodeType::SELECT_NODE) {
347633 throw ParserException("Expected a single SELECT node");
347634 }
347635 auto &select_node = select.node->Cast<SelectNode>();
347636 if (!select_node.from_table || select_node.from_table->type != TableReferenceType::EXPRESSION_LIST) {
347637 throw ParserException("Expected a single VALUES statement");
347638 }
347639 auto &values_list = select_node.from_table->Cast<ExpressionListRef>();
347640 return std::move(values_list.values);
347641}

◆ ParseColumnList()

ColumnList duckdb::Parser::ParseColumnList ( const string & column_list,
ParserOptions options = ParserOptions() )
static

Parses a column list (i.e. as found in a CREATE TABLE statement).

347643 {
347644 string mock_query = "CREATE TABLE tbl (" + column_list + ")";
347645 Parser parser(options);
347646 parser.ParseQuery(mock_query);
347647 if (parser.statements.size() != 1 || parser.statements[0]->type != StatementType::CREATE_STATEMENT) {
347648 throw ParserException("Expected a single CREATE statement");
347649 }
347650 auto &create = parser.statements[0]->Cast<CreateStatement>();
347651 if (create.info->type != CatalogType::TABLE_ENTRY) {
347652 throw InternalException("Expected a single CREATE TABLE statement");
347653 }
347654 auto &info = create.info->Cast<CreateTableInfo>();
347655 return std::move(info.columns);
347656}
Here is the call graph for this function:

◆ ParseColumnDefinition()

ColumnDefinition duckdb::Parser::ParseColumnDefinition ( const string & column_definition,
ParserOptions options = ParserOptions() )
static
347658 {
347659 auto column_list = ParseColumnList(column_definition, options);
347660 return column_list.GetColumn(LogicalIndex(0)).Copy();
347661}
static ColumnList ParseColumnList(const string &column_list, ParserOptions options=ParserOptions())
Parses a column list (i.e. as found in a CREATE TABLE statement).
Definition duckdb.cpp:347643

◆ StripUnicodeSpaces()

bool duckdb::Parser::StripUnicodeSpaces ( const string & query_str,
string & new_query )
static
347007 {
347008 const idx_t NBSP_LEN = 2;
347009 const idx_t USP_LEN = 3;
347010 idx_t pos = 0;
347011 unsigned char quote;
347012 string_t dollar_quote_tag;
347013 vector<UnicodeSpace> unicode_spaces;
347014 auto query = const_uchar_ptr_cast(query_str.c_str());
347015 auto qsize = query_str.size();
347016
347017regular:
347018 for (; pos + 2 < qsize; pos++) {
347019 if (query[pos] == 0xC2) {
347020 if (query[pos + 1] == 0xA0) {
347021 // U+00A0 - C2A0
347022 unicode_spaces.emplace_back(pos, NBSP_LEN);
347023 }
347024 }
347025 if (query[pos] == 0xE2) {
347026 if (query[pos + 1] == 0x80) {
347027 if (query[pos + 2] >= 0x80 && query[pos + 2] <= 0x8B) {
347028 // U+2000 to U+200B
347029 // E28080 - E2808B
347030 unicode_spaces.emplace_back(pos, USP_LEN);
347031 } else if (query[pos + 2] == 0xAF) {
347032 // U+202F - E280AF
347033 unicode_spaces.emplace_back(pos, USP_LEN);
347034 }
347035 } else if (query[pos + 1] == 0x81) {
347036 if (query[pos + 2] == 0x9F) {
347037 // U+205F - E2819f
347038 unicode_spaces.emplace_back(pos, USP_LEN);
347039 } else if (query[pos + 2] == 0xA0) {
347040 // U+2060 - E281A0
347041 unicode_spaces.emplace_back(pos, USP_LEN);
347042 }
347043 }
347044 } else if (query[pos] == 0xE3) {
347045 if (query[pos + 1] == 0x80 && query[pos + 2] == 0x80) {
347046 // U+3000 - E38080
347047 unicode_spaces.emplace_back(pos, USP_LEN);
347048 }
347049 } else if (query[pos] == 0xEF) {
347050 if (query[pos + 1] == 0xBB && query[pos + 2] == 0xBF) {
347051 // U+FEFF - EFBBBF
347052 unicode_spaces.emplace_back(pos, USP_LEN);
347053 }
347054 } else if (query[pos] == '"' || query[pos] == '\'') {
347055 quote = query[pos];
347056 pos++;
347057 goto in_quotes;
347058 } else if (query[pos] == '$' &&
347059 (query[pos + 1] == '$' || IsValidDollarQuotedStringTagFirstChar(query[pos + 1]))) {
347060 // (optionally tagged) dollar-quoted string
347061 auto start = &query[++pos];
347062 for (; pos + 2 < qsize; pos++) {
347063 if (query[pos] == '$') {
347064 // end of tag
347065 dollar_quote_tag =
347066 string_t(const_char_ptr_cast(start), NumericCast<uint32_t, int64_t>(&query[pos] - start));
347067 goto in_dollar_quotes;
347068 }
347069
347070 if (!IsValidDollarQuotedStringTagSubsequentChar(query[pos])) {
347071 // invalid char in dollar-quoted string, continue as normal
347072 goto regular;
347073 }
347074 }
347075 goto end;
347076 } else if (query[pos] == '-' && query[pos + 1] == '-') {
347077 goto in_comment;
347078 }
347079 }
347080 goto end;
347081in_quotes:
347082 for (; pos + 1 < qsize; pos++) {
347083 if (query[pos] == quote) {
347084 if (query[pos + 1] == quote) {
347085 // escaped quote
347086 pos++;
347087 continue;
347088 }
347089 pos++;
347090 goto regular;
347091 }
347092 }
347093 goto end;
347094in_dollar_quotes:
347095 for (; pos + 2 < qsize; pos++) {
347096 if (query[pos] == '$' &&
347097 qsize - (pos + 1) >= dollar_quote_tag.GetSize() + 1 && // found '$' and enough space left
347098 query[pos + dollar_quote_tag.GetSize() + 1] == '$' && // ending '$' at the right spot
347099 memcmp(&query[pos + 1], dollar_quote_tag.GetData(), dollar_quote_tag.GetSize()) == 0) { // tags match
347100 pos += dollar_quote_tag.GetSize() + 1;
347101 goto regular;
347102 }
347103 }
347104 goto end;
347105in_comment:
347106 for (; pos < qsize; pos++) {
347107 if (query[pos] == '\n' || query[pos] == '\r') {
347108 goto regular;
347109 }
347110 }
347111 goto end;
347112end:
347113 return ReplaceUnicodeSpaces(query_str, new_query, unicode_spaces);
347114}

◆ GetStatement()

unique_ptr< SQLStatement > duckdb::Parser::GetStatement ( const string & query)
347140 {
347141 Transformer transformer(options);
347142 vector<unique_ptr<SQLStatement>> statements;
347143 PostgresParser parser;
347144 parser.Parse(query);
347145 if (parser.success) {
347146 if (!parser.parse_tree) {
347147 // empty statement
347148 return {};
347149 }
347150 transformer.TransformParseTree(parser.parse_tree, statements);
347151 return std::move(statements[0]);
347152 }
347153 return {};
347154}

◆ ThrowParserOverrideError()

void duckdb::Parser::ThrowParserOverrideError ( ParserOverrideResult & result)
347156 {
347157 if (result.type == ParserExtensionResultType::DISPLAY_ORIGINAL_ERROR) {
347158 throw ParserException("Parser override failed to return a valid statement: %s\n\nConsider restarting the "
347159 "database and "
347160 "using the setting \"set allow_parser_override_extension=fallback\" to fallback to the "
347161 "default parser.",
347162 result.error.RawMessage());
347163 }
347164 if (result.type == ParserExtensionResultType::DISPLAY_EXTENSION_ERROR) {
347165 result.error.Throw();
347166 }
347167}

The documentation for this class was generated from the following file: