bool ParserSelectQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) { auto select_query = std::make_shared<ASTSelectQuery>(); node = select_query; ParserKeyword s_select("SELECT"); ParserKeyword s_distinct("DISTINCT"); ParserKeyword s_from("FROM"); ParserKeyword s_prewhere("PREWHERE"); ParserKeyword s_where("WHERE"); ParserKeyword s_group_by("GROUP BY"); ParserKeyword s_with("WITH"); ParserKeyword s_totals("TOTALS"); ParserKeyword s_having("HAVING"); ParserKeyword s_order_by("ORDER BY"); ParserKeyword s_limit("LIMIT"); ParserKeyword s_settings("SETTINGS"); ParserKeyword s_by("BY"); ParserNotEmptyExpressionList exp_list(false); ParserNotEmptyExpressionList exp_list_for_with_clause(false, true); /// Set prefer_alias_to_column_name for each alias. ParserNotEmptyExpressionList exp_list_for_select_clause(true); /// Allows aliases without AS keyword. ParserExpressionWithOptionalAlias exp_elem(false); ParserOrderByExpressionList order_list; /// WITH expr list { if (s_with.ignore(pos, expected)) { if (!exp_list_for_with_clause.parse(pos, select_query->with_expression_list, expected)) return false; } } /// SELECT [DISTINCT] expr list { if (!s_select.ignore(pos, expected)) return false; if (s_distinct.ignore(pos, expected)) select_query->distinct = true; if (!exp_list_for_select_clause.parse(pos, select_query->select_expression_list, expected)) return false; } /// FROM database.table or FROM table or FROM (subquery) or FROM tableFunction(...) if (s_from.ignore(pos, expected)) { if (!ParserTablesInSelectQuery().parse(pos, select_query->tables, expected)) return false; } /// PREWHERE expr if (s_prewhere.ignore(pos, expected)) { if (!exp_elem.parse(pos, select_query->prewhere_expression, expected)) return false; } /// WHERE expr if (s_where.ignore(pos, expected)) { if (!exp_elem.parse(pos, select_query->where_expression, expected)) return false; } /// GROUP BY expr list if (s_group_by.ignore(pos, expected)) { if (!exp_list.parse(pos, select_query->group_expression_list, expected)) return false; } /// WITH TOTALS if (s_with.ignore(pos, expected)) { if (!s_totals.ignore(pos, expected)) return false; select_query->group_by_with_totals = true; } /// HAVING expr if (s_having.ignore(pos, expected)) { if (!exp_elem.parse(pos, select_query->having_expression, expected)) return false; } /// ORDER BY expr ASC|DESC COLLATE 'locale' list if (s_order_by.ignore(pos, expected)) { if (!order_list.parse(pos, select_query->order_expression_list, expected)) return false; } /// LIMIT length | LIMIT offset, length | LIMIT count BY expr-list if (s_limit.ignore(pos, expected)) { ParserToken s_comma(TokenType::Comma); ParserNumber num; if (!num.parse(pos, select_query->limit_length, expected)) return false; if (s_comma.ignore(pos, expected)) { select_query->limit_offset = select_query->limit_length; if (!num.parse(pos, select_query->limit_length, expected)) return false; } else if (s_by.ignore(pos, expected)) { select_query->limit_by_value = select_query->limit_length; select_query->limit_length = nullptr; if (!exp_list.parse(pos, select_query->limit_by_expression_list, expected)) return false; } } /// LIMIT length | LIMIT offset, length if (s_limit.ignore(pos, expected)) { if (!select_query->limit_by_value || select_query->limit_length) return false; ParserToken s_comma(TokenType::Comma); ParserNumber num; if (!num.parse(pos, select_query->limit_length, expected)) return false; if (s_comma.ignore(pos, expected)) { select_query->limit_offset = select_query->limit_length; if (!num.parse(pos, select_query->limit_length, expected)) return false; } } /// SETTINGS key1 = value1, key2 = value2, ... if (s_settings.ignore(pos, expected)) { ParserSetQuery parser_settings(true); if (!parser_settings.parse(pos, select_query->settings, expected)) return false; } if (select_query->with_expression_list) select_query->children.push_back(select_query->with_expression_list); select_query->children.push_back(select_query->select_expression_list); if (select_query->tables) select_query->children.push_back(select_query->tables); if (select_query->prewhere_expression) select_query->children.push_back(select_query->prewhere_expression); if (select_query->where_expression) select_query->children.push_back(select_query->where_expression); if (select_query->group_expression_list) select_query->children.push_back(select_query->group_expression_list); if (select_query->having_expression) select_query->children.push_back(select_query->having_expression); if (select_query->order_expression_list) select_query->children.push_back(select_query->order_expression_list); if (select_query->limit_by_value) select_query->children.push_back(select_query->limit_by_value); if (select_query->limit_by_expression_list) select_query->children.push_back(select_query->limit_by_expression_list); if (select_query->limit_offset) select_query->children.push_back(select_query->limit_offset); if (select_query->limit_length) select_query->children.push_back(select_query->limit_length); if (select_query->settings) select_query->children.push_back(select_query->settings); return true; }
bool ParserInsertQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) { ParserKeyword s_insert_into("INSERT INTO"); ParserKeyword s_table("TABLE"); ParserKeyword s_function("FUNCTION"); ParserToken s_dot(TokenType::Dot); ParserKeyword s_values("VALUES"); ParserKeyword s_format("FORMAT"); ParserKeyword s_select("SELECT"); ParserKeyword s_with("WITH"); ParserToken s_lparen(TokenType::OpeningRoundBracket); ParserToken s_rparen(TokenType::ClosingRoundBracket); ParserIdentifier name_p; ParserList columns_p(std::make_unique<ParserCompoundIdentifier>(), std::make_unique<ParserToken>(TokenType::Comma), false); ParserFunction table_function_p; ASTPtr database; ASTPtr table; ASTPtr columns; ASTPtr format; ASTPtr select; ASTPtr table_function; /// Insertion data const char * data = nullptr; if (!s_insert_into.ignore(pos, expected)) return false; s_table.ignore(pos, expected); if (s_function.ignore(pos, expected)) { if (!table_function_p.parse(pos, table_function, expected)) return false; } else { if (!name_p.parse(pos, table, expected)) return false; if (s_dot.ignore(pos, expected)) { database = table; if (!name_p.parse(pos, table, expected)) return false; } } /// Is there a list of columns if (s_lparen.ignore(pos, expected)) { if (!columns_p.parse(pos, columns, expected)) return false; if (!s_rparen.ignore(pos, expected)) return false; } Pos before_select = pos; /// VALUES or FORMAT or SELECT if (s_values.ignore(pos, expected)) { data = pos->begin; } else if (s_format.ignore(pos, expected)) { auto name_pos = pos; if (!name_p.parse(pos, format, expected)) return false; data = name_pos->end; if (data < end && *data == ';') throw Exception("You have excessive ';' symbol before data for INSERT.\n" "Example:\n\n" "INSERT INTO t (x, y) FORMAT TabSeparated\n" ";\tHello\n" "2\tWorld\n" "\n" "Note that there is no ';' just after format name, " "you need to put at least one whitespace symbol before the data.", ErrorCodes::SYNTAX_ERROR); while (data < end && (*data == ' ' || *data == '\t' || *data == '\f')) ++data; /// Data starts after the first newline, if there is one, or after all the whitespace characters, otherwise. if (data < end && *data == '\r') ++data; if (data < end && *data == '\n') ++data; } else if (s_select.ignore(pos, expected) || s_with.ignore(pos,expected)) { pos = before_select; ParserSelectWithUnionQuery select_p; select_p.parse(pos, select, expected); } else { return false; } auto query = std::make_shared<ASTInsertQuery>(); node = query; if (table_function) { query->table_function = table_function; } else { getIdentifierName(database, query->database); getIdentifierName(table, query->table); } getIdentifierName(format, query->format); query->columns = columns; query->select = select; query->data = data != end ? data : nullptr; query->end = end; if (columns) query->children.push_back(columns); if (select) query->children.push_back(select); return true; }
bool ParserAlterQuery::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected) { Pos begin = pos; ParserKeyword s_alter_table("ALTER TABLE"); ParserKeyword s_add_column("ADD COLUMN"); ParserKeyword s_drop_column("DROP COLUMN"); ParserKeyword s_modify_column("MODIFY COLUMN"); ParserKeyword s_modify_primary_key("MODIFY PRIMARY KEY"); ParserKeyword s_attach_partition("ATTACH PARTITION"); ParserKeyword s_detach_partition("DETACH PARTITION"); ParserKeyword s_drop_partition("DROP PARTITION"); ParserKeyword s_attach_part("ATTACH PART"); ParserKeyword s_fetch_partition("FETCH PARTITION"); ParserKeyword s_freeze_partition("FREEZE PARTITION"); ParserKeyword s_reshard("RESHARD"); ParserKeyword s_partition("PARTITION"); ParserKeyword s_after("AFTER"); ParserKeyword s_from("FROM"); ParserKeyword s_from_partition("FROM PARTITION"); ParserKeyword s_copy("COPY"); ParserKeyword s_to("TO"); ParserKeyword s_using("USING"); ParserKeyword s_coordinate("COORDINATE"); ParserKeyword s_with("WITH"); ParserKeyword s_name("NAME"); ParserString s_dot("."); ParserString s_comma(","); ParserString s_doubledot(".."); ParserWhitespaceOrComments ws; ParserIdentifier table_parser; ParserCompoundIdentifier parser_name; ParserCompoundColumnDeclaration parser_col_decl; ParserLiteral parser_literal; ParserUnsignedInteger parser_uint; ParserStringLiteral parser_string_literal; ASTPtr table; ASTPtr database; String cluster_str; ASTPtr col_type; ASTPtr col_after; ASTPtr col_drop; auto query = std::make_shared<ASTAlterQuery>(); ws.ignore(pos, end); if (!s_alter_table.ignore(pos, end, max_parsed_pos, expected)) return false; ws.ignore(pos, end); if (!table_parser.parse(pos, end, database, max_parsed_pos, expected)) return false; /// Parse [db].name if (s_dot.ignore(pos, end)) { if (!table_parser.parse(pos, end, table, max_parsed_pos, expected)) return false; query->table = typeid_cast<ASTIdentifier &>(*table).name; query->database = typeid_cast<ASTIdentifier &>(*database).name; } else { table = database; query->table = typeid_cast<ASTIdentifier &>(*table).name; } ws.ignore(pos, end); if (ParserKeyword{"ON"}.ignore(pos, end, max_parsed_pos, expected)) { if (!ASTQueryWithOnCluster::parse(pos, end, cluster_str, max_parsed_pos, expected)) return false; } bool parsing_finished = false; do { ASTAlterQuery::Parameters params; ws.ignore(pos, end); if (s_add_column.ignore(pos, end, max_parsed_pos, expected)) { ws.ignore(pos, end); if (!parser_col_decl.parse(pos, end, params.col_decl, max_parsed_pos, expected)) return false; ws.ignore(pos, end); if (s_after.ignore(pos, end, max_parsed_pos, expected)) { ws.ignore(pos, end); if(!parser_name.parse(pos, end, params.column, max_parsed_pos, expected)) return false; } params.type = ASTAlterQuery::ADD_COLUMN; } else if (s_drop_partition.ignore(pos, end, max_parsed_pos, expected)) { ws.ignore(pos, end); if (!parser_literal.parse(pos, end, params.partition, max_parsed_pos, expected)) return false; params.type = ASTAlterQuery::DROP_PARTITION; } else if (s_drop_column.ignore(pos, end, max_parsed_pos, expected)) { ws.ignore(pos, end); if (!parser_name.parse(pos, end, params.column, max_parsed_pos, expected)) return false; params.type = ASTAlterQuery::DROP_COLUMN; params.detach = false; if (s_from_partition.ignore(pos, end, max_parsed_pos, expected)) { ws.ignore(pos, end); if (!parser_literal.parse(pos, end, params.partition, max_parsed_pos, expected)) return false; } } else if (s_detach_partition.ignore(pos, end, max_parsed_pos, expected)) { ws.ignore(pos, end); if (!parser_literal.parse(pos, end, params.partition, max_parsed_pos, expected)) return false; params.type = ASTAlterQuery::DROP_PARTITION; params.detach = true; } else if (s_attach_partition.ignore(pos, end, max_parsed_pos, expected)) { ws.ignore(pos, end); if (!parser_literal.parse(pos, end, params.partition, max_parsed_pos, expected)) return false; params.type = ASTAlterQuery::ATTACH_PARTITION; } else if (s_attach_part.ignore(pos, end, max_parsed_pos, expected)) { ws.ignore(pos, end); if (!parser_literal.parse(pos, end, params.partition, max_parsed_pos, expected)) return false; params.part = true; params.type = ASTAlterQuery::ATTACH_PARTITION; } else if (s_fetch_partition.ignore(pos, end, max_parsed_pos, expected)) { ws.ignore(pos, end); if (!parser_literal.parse(pos, end, params.partition, max_parsed_pos, expected)) return false; ws.ignore(pos, end); if (!s_from.ignore(pos, end, max_parsed_pos, expected)) return false; ws.ignore(pos, end); ASTPtr ast_from; if (!parser_string_literal.parse(pos, end, ast_from, max_parsed_pos, expected)) return false; params.from = typeid_cast<const ASTLiteral &>(*ast_from).value.get<const String &>(); params.type = ASTAlterQuery::FETCH_PARTITION; } else if (s_freeze_partition.ignore(pos, end, max_parsed_pos, expected)) { ws.ignore(pos, end); if (!parser_literal.parse(pos, end, params.partition, max_parsed_pos, expected)) return false; ws.ignore(pos, end); /// WITH NAME 'name' - place local backup to directory with specified name if (s_with.ignore(pos, end, max_parsed_pos, expected)) { ws.ignore(pos, end); if (!s_name.ignore(pos, end, max_parsed_pos, expected)) return false; ws.ignore(pos, end); ASTPtr ast_with_name; if (!parser_string_literal.parse(pos, end, ast_with_name, max_parsed_pos, expected)) return false; params.with_name = typeid_cast<const ASTLiteral &>(*ast_with_name).value.get<const String &>(); ws.ignore(pos, end); } params.type = ASTAlterQuery::FREEZE_PARTITION; } else if (s_modify_column.ignore(pos, end, max_parsed_pos, expected)) { ws.ignore(pos, end); if (!parser_col_decl.parse(pos, end, params.col_decl, max_parsed_pos, expected)) return false; ws.ignore(pos, end); params.type = ASTAlterQuery::MODIFY_COLUMN; } else if (s_modify_primary_key.ignore(pos, end, max_parsed_pos, expected)) { ws.ignore(pos, end); if (!ParserString("(").ignore(pos, end, max_parsed_pos, expected)) return false; ws.ignore(pos, end); if (!ParserNotEmptyExpressionList(false).parse(pos, end, params.primary_key, max_parsed_pos, expected)) return false; ws.ignore(pos, end); if (!ParserString(")").ignore(pos, end, max_parsed_pos, expected)) return false; ws.ignore(pos, end); params.type = ASTAlterQuery::MODIFY_PRIMARY_KEY; } else if (s_reshard.ignore(pos, end, max_parsed_pos, expected)) { ParserList weighted_zookeeper_paths_p(std::make_unique<ParserWeightedZooKeeperPath>(), std::make_unique<ParserString>(","), false); ParserExpressionWithOptionalAlias parser_sharding_key_expr(false); ParserStringLiteral parser_coordinator; ws.ignore(pos, end); if (s_copy.ignore(pos, end, max_parsed_pos, expected)) params.do_copy = true; ws.ignore(pos, end); if (s_partition.ignore(pos, end, max_parsed_pos, expected)) { ws.ignore(pos, end); if (!parser_uint.parse(pos, end, params.partition, max_parsed_pos, expected)) return false; ws.ignore(pos, end); if (s_doubledot.ignore(pos, end, max_parsed_pos, expected)) { ws.ignore(pos, end); if (!parser_uint.parse(pos, end, params.last_partition, max_parsed_pos, expected)) return false; } } ws.ignore(pos, end); if (!s_to.ignore(pos, end, max_parsed_pos, expected)) return false; ws.ignore(pos, end); if (!weighted_zookeeper_paths_p.parse(pos, end, params.weighted_zookeeper_paths, max_parsed_pos, expected)) return false; ws.ignore(pos, end); if (!s_using.ignore(pos, end, max_parsed_pos, expected)) return false; ws.ignore(pos, end); if (!parser_sharding_key_expr.parse(pos, end, params.sharding_key_expr, max_parsed_pos, expected)) return false; ws.ignore(pos, end); if (s_coordinate.ignore(pos, end, max_parsed_pos, expected)) { ws.ignore(pos, end); if (!s_with.ignore(pos, end, max_parsed_pos, expected)) return false; ws.ignore(pos, end); if (!parser_coordinator.parse(pos, end, params.coordinator, max_parsed_pos, expected)) return false; ws.ignore(pos, end); } params.type = ASTAlterQuery::RESHARD_PARTITION; } else return false; ws.ignore(pos, end); if (!s_comma.ignore(pos, end, max_parsed_pos, expected)) { ws.ignore(pos, end); parsing_finished = true; } query->addParameters(params); } while (!parsing_finished); query->range = StringRange(begin, end); query->cluster = cluster_str; node = query; return true; }