2017-04-01 09:19:00 +00:00
# include <Parsers/ASTAlterQuery.h>
2017-04-16 05:40:17 +00:00
# include <iomanip>
2019-07-25 18:21:01 +00:00
# include <IO/WriteHelpers.h>
2017-04-16 05:40:17 +00:00
2016-01-28 01:00:27 +00:00
namespace DB
{
namespace ErrorCodes
{
2017-04-01 07:20:54 +00:00
extern const int UNEXPECTED_AST_STRUCTURE ;
2016-01-28 01:00:27 +00:00
}
2018-06-09 15:53:14 +00:00
ASTPtr ASTAlterCommand : : clone ( ) const
{
auto res = std : : make_shared < ASTAlterCommand > ( * this ) ;
res - > children . clear ( ) ;
if ( col_decl )
{
res - > col_decl = col_decl - > clone ( ) ;
res - > children . push_back ( res - > col_decl ) ;
}
if ( column )
{
res - > column = column - > clone ( ) ;
res - > children . push_back ( res - > column ) ;
}
2018-11-06 18:25:36 +00:00
if ( order_by )
2018-10-15 18:47:47 +00:00
{
2018-11-06 18:25:36 +00:00
res - > order_by = order_by - > clone ( ) ;
res - > children . push_back ( res - > order_by ) ;
2018-10-15 18:47:47 +00:00
}
2018-06-09 15:53:14 +00:00
if ( partition )
{
res - > partition = partition - > clone ( ) ;
res - > children . push_back ( res - > partition ) ;
}
if ( predicate )
{
res - > predicate = predicate - > clone ( ) ;
res - > children . push_back ( res - > predicate ) ;
}
2019-04-15 09:30:45 +00:00
if ( ttl )
{
res - > ttl = ttl - > clone ( ) ;
res - > children . push_back ( res - > ttl ) ;
}
2019-07-24 12:56:39 +00:00
if ( settings_changes )
{
res - > settings_changes = settings_changes - > clone ( ) ;
res - > children . push_back ( res - > settings_changes ) ;
}
2019-05-28 21:17:48 +00:00
if ( values )
{
res - > values = values - > clone ( ) ;
res - > children . push_back ( res - > values ) ;
}
2018-06-09 15:53:14 +00:00
return res ;
}
void ASTAlterCommand : : formatImpl (
const FormatSettings & settings , FormatState & state , FormatStateStacked frame ) const
{
std : : string indent_str = settings . one_line ? " " : std : : string ( 4 * frame . indent , ' ' ) ;
if ( type = = ASTAlterCommand : : ADD_COLUMN )
{
2018-12-21 14:53:00 +00:00
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < " ADD COLUMN " < < ( if_not_exists ? " IF NOT EXISTS " : " " ) < < ( settings . hilite ? hilite_none : " " ) ;
2018-06-09 15:53:14 +00:00
col_decl - > formatImpl ( settings , state , frame ) ;
/// AFTER
if ( column )
{
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < " AFTER " < < ( settings . hilite ? hilite_none : " " ) ;
column - > formatImpl ( settings , state , frame ) ;
}
}
else if ( type = = ASTAlterCommand : : DROP_COLUMN )
{
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str
2018-12-21 14:53:00 +00:00
< < ( clear_column ? " CLEAR " : " DROP " ) < < " COLUMN " < < ( if_exists ? " IF EXISTS " : " " ) < < ( settings . hilite ? hilite_none : " " ) ;
2018-06-09 15:53:14 +00:00
column - > formatImpl ( settings , state , frame ) ;
if ( partition )
{
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < " IN PARTITION " < < ( settings . hilite ? hilite_none : " " ) ;
partition - > formatImpl ( settings , state , frame ) ;
}
}
else if ( type = = ASTAlterCommand : : MODIFY_COLUMN )
{
2018-12-21 14:53:00 +00:00
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < " MODIFY COLUMN " < < ( if_exists ? " IF EXISTS " : " " ) < < ( settings . hilite ? hilite_none : " " ) ;
2018-06-09 15:53:14 +00:00
col_decl - > formatImpl ( settings , state , frame ) ;
}
2019-06-16 12:10:34 +00:00
else if ( type = = ASTAlterCommand : : COMMENT_COLUMN )
{
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < " COMMENT COLUMN " < < ( if_exists ? " IF EXISTS " : " " ) < < ( settings . hilite ? hilite_none : " " ) ;
column - > formatImpl ( settings , state , frame ) ;
settings . ostr < < " " < < ( settings . hilite ? hilite_none : " " ) ;
comment - > formatImpl ( settings , state , frame ) ;
}
2018-10-15 18:47:47 +00:00
else if ( type = = ASTAlterCommand : : MODIFY_ORDER_BY )
{
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < " MODIFY ORDER BY " < < ( settings . hilite ? hilite_none : " " ) ;
2018-11-06 18:25:36 +00:00
order_by - > formatImpl ( settings , state , frame ) ;
2018-10-15 18:47:47 +00:00
}
2019-02-05 14:50:25 +00:00
else if ( type = = ASTAlterCommand : : ADD_INDEX )
{
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < " ADD INDEX " < < ( if_not_exists ? " IF NOT EXISTS " : " " ) < < ( settings . hilite ? hilite_none : " " ) ;
index_decl - > formatImpl ( settings , state , frame ) ;
/// AFTER
if ( index )
{
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < " AFTER " < < ( settings . hilite ? hilite_none : " " ) ;
index - > formatImpl ( settings , state , frame ) ;
}
}
else if ( type = = ASTAlterCommand : : DROP_INDEX )
{
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str
2019-05-09 14:25:18 +00:00
< < ( clear_index ? " CLEAR " : " DROP " ) < < " INDEX " < < ( if_exists ? " IF EXISTS " : " " ) < < ( settings . hilite ? hilite_none : " " ) ;
2019-02-05 14:50:25 +00:00
index - > formatImpl ( settings , state , frame ) ;
2019-05-09 14:25:18 +00:00
if ( partition )
{
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < " IN PARTITION " < < ( settings . hilite ? hilite_none : " " ) ;
partition - > formatImpl ( settings , state , frame ) ;
}
2019-02-05 14:50:25 +00:00
}
2019-04-09 15:36:33 +00:00
else if ( type = = ASTAlterCommand : : MATERIALIZE_INDEX )
{
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str
< < " MATERIALIZE INDEX " < < ( settings . hilite ? hilite_none : " " ) ;
2019-02-05 14:50:25 +00:00
index - > formatImpl ( settings , state , frame ) ;
2019-04-09 15:36:33 +00:00
if ( partition )
{
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < " IN PARTITION " < < ( settings . hilite ? hilite_none : " " ) ;
partition - > formatImpl ( settings , state , frame ) ;
}
2019-02-05 14:50:25 +00:00
}
2019-06-02 14:41:12 +00:00
else if ( type = = ASTAlterCommand : : ADD_CONSTRAINT )
{
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < " ADD CONSTRAINT " < < ( if_not_exists ? " IF NOT EXISTS " : " " ) < < ( settings . hilite ? hilite_none : " " ) ;
constraint_decl - > formatImpl ( settings , state , frame ) ;
}
else if ( type = = ASTAlterCommand : : DROP_CONSTRAINT )
{
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str
< < " DROP CONSTRAINT " < < ( if_exists ? " IF EXISTS " : " " ) < < ( settings . hilite ? hilite_none : " " ) ;
constraint - > formatImpl ( settings , state , frame ) ;
2019-02-05 14:50:25 +00:00
}
2018-06-09 15:53:14 +00:00
else if ( type = = ASTAlterCommand : : DROP_PARTITION )
{
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < ( detach ? " DETACH " : " DROP " ) < < " PARTITION "
< < ( settings . hilite ? hilite_none : " " ) ;
partition - > formatImpl ( settings , state , frame ) ;
}
2019-07-22 11:23:11 +00:00
else if ( type = = ASTAlterCommand : : DROP_DETACHED_PARTITION )
{
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < " DROP DETACHED " < < ( part ? " PART " : " PARTITION " )
< < ( settings . hilite ? hilite_none : " " ) ;
partition - > formatImpl ( settings , state , frame ) ;
}
2018-06-09 15:53:14 +00:00
else if ( type = = ASTAlterCommand : : ATTACH_PARTITION )
{
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < " ATTACH "
< < ( part ? " PART " : " PARTITION " ) < < ( settings . hilite ? hilite_none : " " ) ;
partition - > formatImpl ( settings , state , frame ) ;
}
2019-07-18 15:19:03 +00:00
else if ( type = = ASTAlterCommand : : MOVE_PARTITION )
2018-06-09 15:53:14 +00:00
{
2019-07-18 15:19:03 +00:00
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < " MOVE "
2019-08-20 09:59:19 +00:00
< < ( part ? " PART " : " PARTITION " ) < < ( settings . hilite ? hilite_none : " " ) ;
2018-06-09 15:53:14 +00:00
partition - > formatImpl ( settings , state , frame ) ;
2019-07-25 18:21:01 +00:00
settings . ostr < < " TO " ;
2019-07-23 09:38:26 +00:00
switch ( move_destination_type )
2018-06-09 15:53:14 +00:00
{
2019-07-23 09:38:26 +00:00
case MoveDestinationType : : DISK :
2019-07-18 15:19:03 +00:00
settings . ostr < < " DISK " ;
break ;
2019-07-23 09:38:26 +00:00
case MoveDestinationType : : VOLUME :
2019-07-18 15:19:03 +00:00
settings . ostr < < " VOLUME " ;
break ;
2019-09-17 11:59:09 +00:00
case MoveDestinationType : : TABLE :
settings . ostr < < " TABLE " ;
2019-09-16 07:27:38 +00:00
if ( ! to_database . empty ( ) )
{
settings . ostr < < ( settings . hilite ? hilite_identifier : " " ) < < backQuoteIfNeed ( to_database )
< < ( settings . hilite ? hilite_none : " " ) < < " . " ;
}
settings . ostr < < ( settings . hilite ? hilite_identifier : " " )
< < backQuoteIfNeed ( to_table )
< < ( settings . hilite ? hilite_none : " " ) ;
return ;
2018-06-09 15:53:14 +00:00
}
2019-09-17 11:59:09 +00:00
if ( move_destination_type ! = MoveDestinationType : : TABLE )
2019-07-26 08:42:17 +00:00
{
2019-09-16 07:27:38 +00:00
WriteBufferFromOwnString move_destination_name_buf ;
writeQuoted ( move_destination_name , move_destination_name_buf ) ;
settings . ostr < < move_destination_name_buf . str ( ) ;
2019-07-26 08:42:17 +00:00
}
}
2019-09-17 10:43:58 +00:00
else if ( type = = ASTAlterCommand : : REPLACE_PARTITION )
{
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < ( replace ? " REPLACE " : " ATTACH " ) < < " PARTITION "
< < ( settings . hilite ? hilite_none : " " ) ;
partition - > formatImpl ( settings , state , frame ) ;
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < " FROM " < < ( settings . hilite ? hilite_none : " " ) ;
if ( ! from_database . empty ( ) )
{
settings . ostr < < ( settings . hilite ? hilite_identifier : " " ) < < backQuoteIfNeed ( from_database )
< < ( settings . hilite ? hilite_none : " " ) < < " . " ;
}
settings . ostr < < ( settings . hilite ? hilite_identifier : " " ) < < backQuoteIfNeed ( from_table ) < < ( settings . hilite ? hilite_none : " " ) ;
}
2018-06-09 15:53:14 +00:00
else if ( type = = ASTAlterCommand : : FETCH_PARTITION )
{
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < " FETCH "
< < " PARTITION " < < ( settings . hilite ? hilite_none : " " ) ;
partition - > formatImpl ( settings , state , frame ) ;
settings . ostr < < ( settings . hilite ? hilite_keyword : " " )
< < " FROM " < < ( settings . hilite ? hilite_none : " " ) < < std : : quoted ( from , ' \' ' ) ;
}
else if ( type = = ASTAlterCommand : : FREEZE_PARTITION )
{
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < " FREEZE PARTITION " < < ( settings . hilite ? hilite_none : " " ) ;
partition - > formatImpl ( settings , state , frame ) ;
if ( ! with_name . empty ( ) )
{
settings . ostr < < " " < < ( settings . hilite ? hilite_keyword : " " ) < < " WITH NAME " < < ( settings . hilite ? hilite_none : " " )
< < " " < < std : : quoted ( with_name , ' \' ' ) ;
}
}
2018-10-30 15:04:13 +00:00
else if ( type = = ASTAlterCommand : : FREEZE_ALL )
{
2018-11-01 11:16:04 +00:00
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < " FREEZE " ;
if ( ! with_name . empty ( ) )
{
settings . ostr < < " " < < ( settings . hilite ? hilite_keyword : " " ) < < " WITH NAME " < < ( settings . hilite ? hilite_none : " " )
< < " " < < std : : quoted ( with_name , ' \' ' ) ;
}
2018-10-30 15:04:13 +00:00
}
2018-06-09 15:53:14 +00:00
else if ( type = = ASTAlterCommand : : DELETE )
{
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < " DELETE WHERE " < < ( settings . hilite ? hilite_none : " " ) ;
predicate - > formatImpl ( settings , state , frame ) ;
}
2018-08-07 13:58:11 +00:00
else if ( type = = ASTAlterCommand : : UPDATE )
{
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < " UPDATE " < < ( settings . hilite ? hilite_none : " " ) ;
update_assignments - > formatImpl ( settings , state , frame ) ;
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < " WHERE " < < ( settings . hilite ? hilite_none : " " ) ;
predicate - > formatImpl ( settings , state , frame ) ;
}
2019-04-15 09:30:45 +00:00
else if ( type = = ASTAlterCommand : : MODIFY_TTL )
{
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < " MODIFY TTL " < < ( settings . hilite ? hilite_none : " " ) ;
ttl - > formatImpl ( settings , state , frame ) ;
}
2019-07-24 12:56:39 +00:00
else if ( type = = ASTAlterCommand : : MODIFY_SETTING )
{
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < " MODIFY SETTING " < < ( settings . hilite ? hilite_none : " " ) ;
settings_changes - > formatImpl ( settings , state , frame ) ;
}
2019-05-28 21:17:48 +00:00
else if ( type = = ASTAlterCommand : : LIVE_VIEW_REFRESH )
{
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < " REFRESH " < < ( settings . hilite ? hilite_none : " " ) ;
}
2018-06-09 15:53:14 +00:00
else
throw Exception ( " Unexpected type of ALTER " , ErrorCodes : : UNEXPECTED_AST_STRUCTURE ) ;
}
2016-01-28 01:00:27 +00:00
2018-06-09 15:53:14 +00:00
ASTPtr ASTAlterCommandList : : clone ( ) const
2016-01-28 01:00:27 +00:00
{
2018-06-09 15:53:14 +00:00
auto res = std : : make_shared < ASTAlterCommandList > ( ) ;
for ( ASTAlterCommand * command : commands )
res - > add ( command - > clone ( ) ) ;
return res ;
2016-01-28 01:00:27 +00:00
}
2018-06-09 15:53:14 +00:00
void ASTAlterCommandList : : formatImpl ( const FormatSettings & settings , FormatState & state , FormatStateStacked frame ) const
2016-01-28 01:00:27 +00:00
{
2018-06-09 15:53:14 +00:00
std : : string indent_str = settings . one_line ? " " : std : : string ( 4 * frame . indent , ' ' ) ;
for ( size_t i = 0 ; i < commands . size ( ) ; + + i )
{
static_cast < IAST * > ( commands [ i ] ) - > formatImpl ( settings , state , frame ) ;
std : : string comma = ( i < ( commands . size ( ) - 1 ) ) ? " , " : " " ;
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < comma < < ( settings . hilite ? hilite_none : " " ) ;
settings . ostr < < settings . nl_or_ws ;
}
2016-01-28 01:00:27 +00:00
}
2018-06-09 15:53:14 +00:00
2017-04-02 17:37:49 +00:00
/** Get the text that identifies this element. */
2018-12-07 12:34:40 +00:00
String ASTAlterQuery : : getID ( char delim ) const
2016-01-28 01:00:27 +00:00
{
2018-12-07 12:34:40 +00:00
return " AlterQuery " + ( delim + database ) + delim + table ;
2016-01-28 01:00:27 +00:00
}
ASTPtr ASTAlterQuery : : clone ( ) const
{
2017-04-01 07:20:54 +00:00
auto res = std : : make_shared < ASTAlterQuery > ( * this ) ;
2018-06-09 15:53:14 +00:00
res - > children . clear ( ) ;
if ( command_list )
res - > set ( res - > command_list , command_list - > clone ( ) ) ;
2017-04-01 07:20:54 +00:00
return res ;
2016-01-28 01:00:27 +00:00
}
2017-08-03 17:00:41 +00:00
void ASTAlterQuery : : formatQueryImpl ( const FormatSettings & settings , FormatState & state , FormatStateStacked frame ) const
2016-01-28 01:00:27 +00:00
{
2018-05-17 15:17:07 +00:00
frame . need_parens = false ;
2017-04-01 07:20:54 +00:00
2017-08-03 17:00:41 +00:00
std : : string indent_str = settings . one_line ? " " : std : : string ( 4u * frame . indent , ' ' ) ;
2017-04-01 07:20:54 +00:00
2019-05-28 21:17:48 +00:00
if ( is_live_view )
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < " ALTER LIVE VIEW " < < ( settings . hilite ? hilite_none : " " ) ;
else
settings . ostr < < ( settings . hilite ? hilite_keyword : " " ) < < indent_str < < " ALTER TABLE " < < ( settings . hilite ? hilite_none : " " ) ;
2017-04-01 07:20:54 +00:00
if ( ! table . empty ( ) )
{
if ( ! database . empty ( ) )
{
settings . ostr < < indent_str < < backQuoteIfNeed ( database ) ;
settings . ostr < < " . " ;
}
settings . ostr < < indent_str < < backQuoteIfNeed ( table ) ;
}
2017-04-21 12:39:28 +00:00
formatOnCluster ( settings ) ;
2017-04-01 07:20:54 +00:00
settings . ostr < < settings . nl_or_ws ;
2018-06-09 15:53:14 +00:00
FormatStateStacked frame_nested = frame ;
frame_nested . need_parens = false ;
+ + frame_nested . indent ;
static_cast < IAST * > ( command_list ) - > formatImpl ( settings , state , frame_nested ) ;
2016-01-28 01:00:27 +00:00
}
}