#include #include #include #include namespace DB { namespace ErrorCodes { extern const int LOGICAL_ERROR; } ASTIndexDeclaration::ASTIndexDeclaration(ASTPtr expression, ASTPtr type, const String & name_) : name(name_) { if (!expression) throw Exception(ErrorCodes::LOGICAL_ERROR, "Index declaration must have an expression"); children.push_back(expression); if (type) { if (!dynamic_cast(type.get())) throw Exception(ErrorCodes::LOGICAL_ERROR, "Index declaration type must be a function"); children.push_back(type); } } ASTPtr ASTIndexDeclaration::clone() const { ASTPtr expr = getExpression(); if (expr) expr = expr->clone(); ASTPtr type = getType(); if (type) type = type->clone(); auto res = std::make_shared(expr, type, name); res->granularity = granularity; return res; } ASTPtr ASTIndexDeclaration::getExpression() const { if (children.size() <= expression_idx) throw Exception(ErrorCodes::LOGICAL_ERROR, "Index declaration must have an expression"); return children[expression_idx]; } std::shared_ptr ASTIndexDeclaration::getType() const { if (children.size() <= type_idx) return nullptr; auto func_ast = std::dynamic_pointer_cast(children[type_idx]); if (!func_ast) throw Exception(ErrorCodes::LOGICAL_ERROR, "Index declaration type must be a function"); return func_ast; } void ASTIndexDeclaration::formatImpl(const FormatSettings & s, FormatState & state, FormatStateStacked frame) const { if (auto expr = getExpression()) { if (part_of_create_index_query) { if (expr->as()) { s.ostr << "("; expr->formatImpl(s, state, frame); s.ostr << ")"; } else expr->formatImpl(s, state, frame); } else { s.ostr << backQuoteIfNeed(name); s.ostr << " "; expr->formatImpl(s, state, frame); } } if (auto type = getType()) { s.ostr << (s.hilite ? hilite_keyword : "") << " TYPE " << (s.hilite ? hilite_none : ""); type->formatImpl(s, state, frame); } if (granularity) { s.ostr << (s.hilite ? hilite_keyword : "") << " GRANULARITY " << (s.hilite ? hilite_none : ""); s.ostr << granularity; } } }