#pragma once #include "context.h" #include #include namespace NSQLTranslationV1 { using namespace NYql; using namespace NSQLv1Generated; inline TPosition GetPos(const TToken& token) { return TPosition(token.GetColumn(), token.GetLine()); } template TIdentifier GetIdentifier(TTranslation& ctx, const TToken& node) { auto token = node.GetToken1(); return TIdentifier(TPosition(token.GetColumn(), token.GetLine()), ctx.Identifier(token)); } TIdentifier GetKeywordId(TTranslation& ctx, const TRule_keyword& node); inline TString GetKeyword(TTranslation& ctx, const TRule_keyword& node) { return GetKeywordId(ctx, node).Name; } template inline TString GetKeyword(TTranslation& ctx, const TRule& node) { return GetIdentifier(ctx, node).Name; } inline TString Id(const TRule_identifier& node, TTranslation& ctx) { // identifier: ID_PLAIN | ID_QUOTED; return ctx.Identifier(node.GetToken1()); } TString Id(const TRule_id& node, TTranslation& ctx); TString Id(const TRule_id_or_type& node, TTranslation& ctx); TString Id(const TRule_id_as_compat& node, TTranslation& ctx); TString Id(const TRule_an_id_as_compat& node, TTranslation& ctx); TString Id(const TRule_id_schema& node, TTranslation& ctx); TString Id(const TRule_an_id_or_type& node, TTranslation& ctx); std::pair Id(const TRule_id_or_at& node, TTranslation& ctx); TString Id(const TRule_id_table& node, TTranslation& ctx); TString Id(const TRule_an_id_table& node, TTranslation& ctx); TString Id(const TRule_id_table_or_type& node, TTranslation& ctx); TString Id(const TRule_id_expr& node, TTranslation& ctx); bool IsQuotedId(const TRule_id_expr& node, TTranslation& ctx); TString Id(const TRule_id_expr_in& node, TTranslation& ctx); TString Id(const TRule_id_window& node, TTranslation& ctx); TString Id(const TRule_id_without& node, TTranslation& ctx); TString Id(const TRule_id_hint& node, TTranslation& ctx); TString Id(const TRule_an_id& node, TTranslation& ctx); TString Id(const TRule_an_id_schema& node, TTranslation& ctx); TString Id(const TRule_an_id_expr& node, TTranslation& ctx); TString Id(const TRule_an_id_window& node, TTranslation& ctx); TString Id(const TRule_an_id_without& node, TTranslation& ctx); TString Id(const TRule_an_id_hint& node, TTranslation& ctx); TString Id(const TRule_an_id_pure& node, TTranslation& ctx); template inline TIdentifier IdEx(const TRule& node, TTranslation& ctx) { const TString name(Id(node, ctx)); const TPosition pos(ctx.Context().Pos()); return TIdentifier(pos, name); } bool NamedNodeImpl(const TRule_bind_parameter& node, TString& name, TTranslation& ctx); TString OptIdPrefixAsStr(const TRule_opt_id_prefix& node, TTranslation& ctx, const TString& defaultStr = {}); TString OptIdPrefixAsStr(const TRule_opt_id_prefix_or_type& node, TTranslation& ctx, const TString& defaultStr = {}); void PureColumnListStr(const TRule_pure_column_list& node, TTranslation& ctx, TVector& outList); bool NamedNodeImpl(const TRule_opt_bind_parameter& node, TString& name, bool& isOptional, TTranslation& ctx); TDeferredAtom PureColumnOrNamed(const TRule_pure_column_or_named& node, TTranslation& ctx); bool PureColumnOrNamedListStr(const TRule_pure_column_or_named_list& node, TTranslation& ctx, TVector& outList); std::pair TableKeyImpl(const std::pair& nameWithAt, TViewDescription view, TTranslation& ctx); std::pair TableKeyImpl(const TRule_table_key& node, TTranslation& ctx, bool hasAt); TMaybe ColumnConstraints(const TRule_column_schema& node, TTranslation& ctx); /// \return optional prefix TString ColumnNameAsStr(TTranslation& ctx, const TRule_column_name& node, TString& id); TString ColumnNameAsSingleStr(TTranslation& ctx, const TRule_column_name& node); class TSqlQuery; struct TSymbolNameWithPos { TString Name; TPosition Pos; }; class TSqlTranslation: public TTranslation { protected: TSqlTranslation(TContext& ctx, NSQLTranslation::ESqlMode mode) : TTranslation(ctx) , Mode(mode) { /// \todo remove NSQLTranslation::ESqlMode params YQL_ENSURE(ctx.Settings.Mode == mode); } protected: enum class EExpr { Regular, GroupBy, SqlLambdaParams, }; TNodePtr NamedExpr(const TRule_named_expr& node, EExpr exprMode = EExpr::Regular); bool NamedExprList(const TRule_named_expr_list& node, TVector& exprs, EExpr exprMode = EExpr::Regular); bool BindList(const TRule_bind_parameter_list& node, TVector& bindNames); bool ActionOrSubqueryArgs(const TRule_action_or_subquery_args& node, TVector& bindNames, ui32& optionalArgsCount); bool ModulePath(const TRule_module_path& node, TVector& path); bool NamedBindList(const TRule_named_bind_parameter_list& node, TVector& names, TVector& aliases); bool NamedBindParam(const TRule_named_bind_parameter& node, TSymbolNameWithPos& name, TSymbolNameWithPos& alias); TNodePtr NamedNode(const TRule_named_nodes_stmt& rule, TVector& names); bool ImportStatement(const TRule_import_stmt& stmt, TVector* namesPtr = nullptr); TNodePtr DoStatement(const TRule_do_stmt& stmt, bool makeLambda, const TVector& args = {}); bool DefineActionOrSubqueryStatement(const TRule_define_action_or_subquery_stmt& stmt, TSymbolNameWithPos& nameAndPos, TNodePtr& lambda); bool DefineActionOrSubqueryBody(TSqlQuery& query, TBlocks& blocks, const TRule_define_action_or_subquery_body& body); TNodePtr IfStatement(const TRule_if_stmt& stmt); TNodePtr ForStatement(const TRule_for_stmt& stmt); TMaybe TableArgImpl(const TRule_table_arg& node); bool TableRefImpl(const TRule_table_ref& node, TTableRef& result, bool unorderedSubquery); TMaybe AsTableImpl(const TRule_table_ref& node); bool ClusterExpr(const TRule_cluster_expr& node, bool allowWildcard, TString& service, TDeferredAtom& cluster); bool ClusterExprOrBinding(const TRule_cluster_expr& node, TString& service, TDeferredAtom& cluster, bool& isBinding); bool ApplyTableBinding(const TString& binding, TTableRef& tr, TTableHints& hints); TMaybe ColumnSchemaImpl(const TRule_column_schema& node); bool CreateTableEntry(const TRule_create_table_entry& node, TCreateTableParameters& params, const bool isCreateTableAs); bool FillFamilySettingsEntry(const TRule_family_settings_entry& settingNode, TFamilyEntry& family); bool FillFamilySettings(const TRule_family_settings& settingsNode, TFamilyEntry& family); bool CreateTableSettings(const TRule_with_table_settings& settingsNode, TCreateTableParameters& params); bool StoreTableSettingsEntry(const TIdentifier& id, const TRule_table_setting_value* value, TTableSettings& settings, ETableType tableType, bool alter, bool reset); bool StoreTableSettingsEntry(const TIdentifier& id, const TRule_table_setting_value* value, TTableSettings& settings, bool alter, bool reset); bool StoreExternalTableSettingsEntry(const TIdentifier& id, const TRule_table_setting_value* value, TTableSettings& settings, bool alter, bool reset); bool StoreTableSettingsEntry(const TIdentifier& id, const TRule_table_setting_value& value, TTableSettings& settings, ETableType tableType, bool alter = false); bool StoreDataSourceSettingsEntry(const TIdentifier& id, const TRule_table_setting_value* value, std::map& result); bool StoreDataSourceSettingsEntry(const TRule_alter_table_setting_entry& entry, std::map& result); bool StoreResourcePoolSettingsEntry(const TIdentifier& id, const TRule_table_setting_value* value, std::map& result); bool StoreResourcePoolSettingsEntry(const TRule_alter_table_setting_entry& entry, std::map& result); bool StoreResourcePoolClassifierSettingsEntry(const TIdentifier& id, const TRule_table_setting_value* value, std::map& result); bool StoreResourcePoolClassifierSettingsEntry(const TRule_alter_table_setting_entry& entry, std::map& result); bool ResetTableSettingsEntry(const TIdentifier& id, TTableSettings& settings, ETableType tableType); bool CreateTableIndex(const TRule_table_index& node, TVector& indexes); bool CreateIndexSettings(const TRule_with_index_settings& settingsNode, TIndexDescription::EType indexType, TIndexDescription::TIndexSettings& indexSettings); bool CreateIndexSettingEntry(const TIdentifier& id, const TRule_index_setting_value& value, TIndexDescription::EType indexType, TIndexDescription::TIndexSettings& indexSettings); template std::tuple GetIndexSettingValue(const TRule_index_setting_value& node); TIdentifier GetTopicConsumerId(const TRule_topic_consumer_ref& node); bool CreateConsumerSettings(const TRule_topic_consumer_settings& settingsNode, TTopicConsumerSettings& settings); bool CreateTopicSettings(const TRule_topic_settings& node, TTopicSettings& params); bool CreateTopicConsumer(const TRule_topic_create_consumer_entry& node, TVector& consumers); bool CreateTopicEntry(const TRule_create_topic_entry& node, TCreateTopicParameters& params); bool AlterTopicConsumer(const TRule_alter_topic_alter_consumer& node, THashMap& alterConsumers); bool AlterTopicConsumerEntry(const TRule_alter_topic_alter_consumer_entry& node, TTopicConsumerDescription& alterConsumer); bool AlterTopicAction(const TRule_alter_topic_action& node, TAlterTopicParameters& params); TNodePtr TypeSimple(const TRule_type_name_simple& node, bool onlyDataAllowed); TNodePtr TypeDecimal(const TRule_type_name_decimal& node); TNodePtr AddOptionals(const TNodePtr& node, size_t optionalCount); TMaybe, bool>> CallableArgList(const TRule_callable_arg_list& argList, bool namedArgsStarted); TNodePtr IntegerOrBind(const TRule_integer_or_bind& node); TNodePtr TypeNameTag(const TRule_type_name_tag& node); TNodePtr TypeNodeOrBind(const TRule_type_name_or_bind& node); TNodePtr SerialTypeNode(const TRule_type_name_or_bind& node); TNodePtr TypeNode(const TRule_type_name& node); TNodePtr TypeNode(const TRule_type_name_composite& node); TNodePtr ValueConstructorLiteral(const TRule_value_constructor_literal& node); TNodePtr ValueConstructor(const TRule_value_constructor& node); TNodePtr ListLiteral(const TRule_list_literal& node); TNodePtr DictLiteral(const TRule_dict_literal& node); TNodePtr StructLiteral(const TRule_struct_literal& node); TMaybe TableHintsImpl(const TRule_table_hints& node, const TString& provider, const TString& keyFunc = ""); bool TableHintImpl(const TRule_table_hint& rule, TTableHints& hints, const TString& provider, const TString& keyFunc = ""); bool SimpleTableRefImpl(const TRule_simple_table_ref& node, TTableRef& result); bool TopicRefImpl(const TRule_topic_ref& node, TTopicRef& result); TWindowSpecificationPtr WindowSpecification(const TRule_window_specification_details& rule); bool OrderByClause(const TRule_order_by_clause& node, TVector& orderBy); bool SortSpecificationList(const TRule_sort_specification_list& node, TVector& sortSpecs); bool IsDistinctOptSet(const TRule_opt_set_quantifier& node) const; bool IsDistinctOptSet(const TRule_opt_set_quantifier& node, TPosition& distinctPos) const; bool AddObjectFeature(std::map& result, const TRule_object_feature& feature); bool BindParameterClause(const TRule_bind_parameter& node, TDeferredAtom& result); bool ObjectFeatureValueClause(const TRule_object_feature_value& node, TDeferredAtom& result); bool ParseObjectFeatures(std::map& result, const TRule_object_features& features); bool ParseExternalDataSourceSettings(std::map& result, const TRule_with_table_settings& settings); bool ParseExternalDataSourceSettings(std::map& result, std::set& toReset, const TRule_alter_external_data_source_action& alterActions); bool ParseViewOptions(std::map& features, const TRule_with_table_settings& options); bool ParseViewQuery(std::map& features, const TRule_select_stmt& query); bool ParseResourcePoolSettings(std::map& result, const TRule_with_table_settings& settings); bool ParseResourcePoolSettings(std::map& result, std::set& toReset, const TRule_alter_resource_pool_action& alterAction); bool ParseResourcePoolClassifierSettings(std::map& result, const TRule_with_table_settings& settings); bool ParseResourcePoolClassifierSettings(std::map& result, std::set& toReset, const TRule_alter_resource_pool_classifier_action& alterAction); bool RoleNameClause(const TRule_role_name& node, TDeferredAtom& result, bool allowSystemRoles); bool PasswordParameter(const TRule_password_option& passwordOption, TUserParameters& result); bool HashParameter(const TRule_hash_option& hashOption, TUserParameters& result); void LoginParameter(const TRule_login_option& loginOption, std::optional& canLogin); bool UserParameters(const std::vector& optionsList, TUserParameters& result, bool isCreateUser); bool PermissionNameClause(const TRule_permission_name_target& node, TVector& result, bool withGrantOption); bool PermissionNameClause(const TRule_permission_name& node, TDeferredAtom& result); bool PermissionNameClause(const TRule_permission_id& node, TDeferredAtom& result); bool StoreStringSettingsEntry(const TIdentifier& id, const TRule_table_setting_value* value, std::map& result); bool StoreStringSettingsEntry(const TRule_alter_table_setting_entry& entry, std::map& result); bool ParseBackupCollectionSettings(std::map& result, const TRule_backup_collection_settings& settings); bool ParseBackupCollectionSettings(std::map& result, std::set& toReset, const TRule_alter_backup_collection_actions& actions); bool ParseBackupCollectionTables(TVector& result, const TRule_table_list& tables); bool ParseBackupCollectionEntry( bool& addDatabase, bool& removeDatabase, TVector& addTables, TVector& removeTables, const TRule_alter_backup_collection_entry& entry); bool ParseBackupCollectionEntries( bool& addDatabase, bool& removeDatabase, TVector& addTables, TVector& removeTables, const TRule_alter_backup_collection_entries& entries); bool ParseTransferLambda(TString& lambdaText, const TRule_lambda_or_parameter& lambdaOrParameter); bool ValidateAuthMethod(const std::map& result); bool ValidateExternalTable(const TCreateTableParameters& params); TNodePtr ReturningList(const ::NSQLv1Generated::TRule_returning_columns_list& columns); private: bool SimpleTableRefCoreImpl(const TRule_simple_table_ref_core& node, TTableRef& result); static bool IsValidFrameSettings(TContext& ctx, const TFrameSpecification& frameSpec, size_t sortSpecSize); static TString FrameSettingsToString(EFrameSettings settings, bool isUnbounded); bool FrameBound(const TRule_window_frame_bound& rule, TFrameBoundPtr& bound); bool FrameClause(const TRule_window_frame_clause& node, TFrameSpecificationPtr& frameSpec, size_t sortSpecSize); bool SortSpecification(const TRule_sort_specification& node, TVector& sortSpecs); bool ClusterExpr(const TRule_cluster_expr& node, bool allowWildcard, bool allowBinding, TString& service, TDeferredAtom& cluster, bool& isBinding); bool StructLiteralItem(TVector& labels, const TRule_expr& label, TVector& values, const TRule_expr& value); bool ValidateTableSettings(const TTableSettings& settings); protected: NSQLTranslation::ESqlMode Mode; }; TNodePtr LiteralNumber(TContext& ctx, const TRule_integer& node); bool StoreString(const TRule_family_setting_value& from, TNodePtr& to, TContext& ctx); bool StoreInt(const TRule_family_setting_value& from, TNodePtr& to, TContext& ctx); template struct TPatternComponent { TBasicString Prefix; TBasicString Suffix; bool IsSimple = true; void AppendPlain(TChar c) { if (IsSimple) { Prefix.push_back(c); } Suffix.push_back(c); } void AppendAnyChar() { IsSimple = false; Suffix.clear(); } }; template TVector> SplitPattern(const TBasicString& pattern, TMaybe escape, bool& inEscape) { inEscape = false; TVector> result; TPatternComponent current; bool prevIsPercentChar = false; for (const TChar c : pattern) { if (inEscape) { current.AppendPlain(c); inEscape = false; prevIsPercentChar = false; } else if (escape && c == static_cast(*escape)) { inEscape = true; } else if (c == '%') { if (!prevIsPercentChar) { result.push_back(std::move(current)); } current = {}; prevIsPercentChar = true; } else if (c == '_') { current.AppendAnyChar(); prevIsPercentChar = false; } else { current.AppendPlain(c); prevIsPercentChar = false; } } result.push_back(std::move(current)); return result; } bool ParseNumbers(TContext& ctx, const TString& strOrig, ui64& value, TString& suffix); } // namespace NSQLTranslationV1