sql_translation.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #pragma once
  2. #include "context.h"
  3. #include <yql/essentials/parser/proto_ast/gen/v1_proto_split/SQLv1Parser.pb.main.h>
  4. #include <library/cpp/charset/ci_string.h>
  5. namespace NSQLTranslationV1 {
  6. using namespace NYql;
  7. using namespace NSQLv1Generated;
  8. inline TPosition GetPos(const TToken& token) {
  9. return TPosition(token.GetColumn(), token.GetLine());
  10. }
  11. template <typename TToken>
  12. TIdentifier GetIdentifier(TTranslation& ctx, const TToken& node) {
  13. auto token = node.GetToken1();
  14. return TIdentifier(TPosition(token.GetColumn(), token.GetLine()), ctx.Identifier(token));
  15. }
  16. TIdentifier GetKeywordId(TTranslation& ctx, const TRule_keyword& node);
  17. inline TString GetKeyword(TTranslation& ctx, const TRule_keyword& node) {
  18. return GetKeywordId(ctx, node).Name;
  19. }
  20. template <typename TRule>
  21. inline TString GetKeyword(TTranslation& ctx, const TRule& node) {
  22. return GetIdentifier(ctx, node).Name;
  23. }
  24. inline TString Id(const TRule_identifier& node, TTranslation& ctx) {
  25. // identifier: ID_PLAIN | ID_QUOTED;
  26. return ctx.Identifier(node.GetToken1());
  27. }
  28. TString Id(const TRule_id& node, TTranslation& ctx);
  29. TString Id(const TRule_id_or_type& node, TTranslation& ctx);
  30. TString Id(const TRule_id_as_compat& node, TTranslation& ctx);
  31. TString Id(const TRule_an_id_as_compat& node, TTranslation& ctx);
  32. TString Id(const TRule_id_schema& node, TTranslation& ctx);
  33. TString Id(const TRule_an_id_or_type& node, TTranslation& ctx);
  34. std::pair<bool, TString> Id(const TRule_id_or_at& node, TTranslation& ctx);
  35. TString Id(const TRule_id_table& node, TTranslation& ctx);
  36. TString Id(const TRule_an_id_table& node, TTranslation& ctx);
  37. TString Id(const TRule_id_table_or_type& node, TTranslation& ctx);
  38. TString Id(const TRule_id_expr& node, TTranslation& ctx);
  39. bool IsQuotedId(const TRule_id_expr& node, TTranslation& ctx);
  40. TString Id(const TRule_id_expr_in& node, TTranslation& ctx);
  41. TString Id(const TRule_id_window& node, TTranslation& ctx);
  42. TString Id(const TRule_id_without& node, TTranslation& ctx);
  43. TString Id(const TRule_id_hint& node, TTranslation& ctx);
  44. TString Id(const TRule_an_id& node, TTranslation& ctx);
  45. TString Id(const TRule_an_id_schema& node, TTranslation& ctx);
  46. TString Id(const TRule_an_id_expr& node, TTranslation& ctx);
  47. TString Id(const TRule_an_id_window& node, TTranslation& ctx);
  48. TString Id(const TRule_an_id_without& node, TTranslation& ctx);
  49. TString Id(const TRule_an_id_hint& node, TTranslation& ctx);
  50. TString Id(const TRule_an_id_pure& node, TTranslation& ctx);
  51. template<typename TRule>
  52. inline TIdentifier IdEx(const TRule& node, TTranslation& ctx) {
  53. const TString name(Id(node, ctx));
  54. const TPosition pos(ctx.Context().Pos());
  55. return TIdentifier(pos, name);
  56. }
  57. bool NamedNodeImpl(const TRule_bind_parameter& node, TString& name, TTranslation& ctx);
  58. TString OptIdPrefixAsStr(const TRule_opt_id_prefix& node, TTranslation& ctx, const TString& defaultStr = {});
  59. TString OptIdPrefixAsStr(const TRule_opt_id_prefix_or_type& node, TTranslation& ctx, const TString& defaultStr = {});
  60. void PureColumnListStr(const TRule_pure_column_list& node, TTranslation& ctx, TVector<TString>& outList);
  61. bool NamedNodeImpl(const TRule_opt_bind_parameter& node, TString& name, bool& isOptional, TTranslation& ctx);
  62. TDeferredAtom PureColumnOrNamed(const TRule_pure_column_or_named& node, TTranslation& ctx);
  63. bool PureColumnOrNamedListStr(const TRule_pure_column_or_named_list& node, TTranslation& ctx, TVector<TDeferredAtom>& outList);
  64. std::pair<TString, TViewDescription> TableKeyImpl(const std::pair<bool, TString>& nameWithAt, TViewDescription view, TTranslation& ctx);
  65. std::pair<TString, TViewDescription> TableKeyImpl(const TRule_table_key& node, TTranslation& ctx, bool hasAt);
  66. TMaybe<TColumnConstraints> ColumnConstraints(const TRule_column_schema& node, TTranslation& ctx);
  67. /// \return optional prefix
  68. TString ColumnNameAsStr(TTranslation& ctx, const TRule_column_name& node, TString& id);
  69. TString ColumnNameAsSingleStr(TTranslation& ctx, const TRule_column_name& node);
  70. class TSqlQuery;
  71. struct TSymbolNameWithPos {
  72. TString Name;
  73. TPosition Pos;
  74. };
  75. class TSqlTranslation: public TTranslation {
  76. protected:
  77. TSqlTranslation(TContext& ctx, NSQLTranslation::ESqlMode mode)
  78. : TTranslation(ctx)
  79. , Mode(mode)
  80. {
  81. /// \todo remove NSQLTranslation::ESqlMode params
  82. YQL_ENSURE(ctx.Settings.Mode == mode);
  83. }
  84. protected:
  85. enum class EExpr {
  86. Regular,
  87. GroupBy,
  88. SqlLambdaParams,
  89. };
  90. TNodePtr NamedExpr(const TRule_named_expr& node, EExpr exprMode = EExpr::Regular);
  91. bool NamedExprList(const TRule_named_expr_list& node, TVector<TNodePtr>& exprs, EExpr exprMode = EExpr::Regular);
  92. bool BindList(const TRule_bind_parameter_list& node, TVector<TSymbolNameWithPos>& bindNames);
  93. bool ActionOrSubqueryArgs(const TRule_action_or_subquery_args& node, TVector<TSymbolNameWithPos>& bindNames, ui32& optionalArgsCount);
  94. bool ModulePath(const TRule_module_path& node, TVector<TString>& path);
  95. bool NamedBindList(const TRule_named_bind_parameter_list& node, TVector<TSymbolNameWithPos>& names,
  96. TVector<TSymbolNameWithPos>& aliases);
  97. bool NamedBindParam(const TRule_named_bind_parameter& node, TSymbolNameWithPos& name, TSymbolNameWithPos& alias);
  98. TNodePtr NamedNode(const TRule_named_nodes_stmt& rule, TVector<TSymbolNameWithPos>& names);
  99. bool ImportStatement(const TRule_import_stmt& stmt, TVector<TString>* namesPtr = nullptr);
  100. TNodePtr DoStatement(const TRule_do_stmt& stmt, bool makeLambda, const TVector<TString>& args = {});
  101. bool DefineActionOrSubqueryStatement(const TRule_define_action_or_subquery_stmt& stmt, TSymbolNameWithPos& nameAndPos, TNodePtr& lambda);
  102. bool DefineActionOrSubqueryBody(TSqlQuery& query, TBlocks& blocks, const TRule_define_action_or_subquery_body& body);
  103. TNodePtr IfStatement(const TRule_if_stmt& stmt);
  104. TNodePtr ForStatement(const TRule_for_stmt& stmt);
  105. TMaybe<TTableArg> TableArgImpl(const TRule_table_arg& node);
  106. bool TableRefImpl(const TRule_table_ref& node, TTableRef& result, bool unorderedSubquery);
  107. TMaybe<TSourcePtr> AsTableImpl(const TRule_table_ref& node);
  108. bool ClusterExpr(const TRule_cluster_expr& node, bool allowWildcard, TString& service, TDeferredAtom& cluster);
  109. bool ClusterExprOrBinding(const TRule_cluster_expr& node, TString& service, TDeferredAtom& cluster, bool& isBinding);
  110. bool ApplyTableBinding(const TString& binding, TTableRef& tr, TTableHints& hints);
  111. TMaybe<TColumnSchema> ColumnSchemaImpl(const TRule_column_schema& node);
  112. bool CreateTableEntry(const TRule_create_table_entry& node, TCreateTableParameters& params, const bool isCreateTableAs);
  113. bool FillFamilySettingsEntry(const TRule_family_settings_entry& settingNode, TFamilyEntry& family);
  114. bool FillFamilySettings(const TRule_family_settings& settingsNode, TFamilyEntry& family);
  115. bool CreateTableSettings(const TRule_with_table_settings& settingsNode, TCreateTableParameters& params);
  116. bool StoreTableSettingsEntry(const TIdentifier& id, const TRule_table_setting_value* value, TTableSettings& settings,
  117. ETableType tableType, bool alter, bool reset);
  118. bool StoreTableSettingsEntry(const TIdentifier& id, const TRule_table_setting_value* value, TTableSettings& settings,
  119. bool alter, bool reset);
  120. bool StoreExternalTableSettingsEntry(const TIdentifier& id, const TRule_table_setting_value* value, TTableSettings& settings,
  121. bool alter, bool reset);
  122. bool StoreTableSettingsEntry(const TIdentifier& id, const TRule_table_setting_value& value, TTableSettings& settings, ETableType tableType, bool alter = false);
  123. bool StoreDataSourceSettingsEntry(const TIdentifier& id, const TRule_table_setting_value* value, std::map<TString, TDeferredAtom>& result);
  124. bool StoreDataSourceSettingsEntry(const TRule_alter_table_setting_entry& entry, std::map<TString, TDeferredAtom>& result);
  125. bool StoreResourcePoolSettingsEntry(const TIdentifier& id, const TRule_table_setting_value* value, std::map<TString, TDeferredAtom>& result);
  126. bool StoreResourcePoolSettingsEntry(const TRule_alter_table_setting_entry& entry, std::map<TString, TDeferredAtom>& result);
  127. bool StoreResourcePoolClassifierSettingsEntry(const TIdentifier& id, const TRule_table_setting_value* value, std::map<TString, TDeferredAtom>& result);
  128. bool StoreResourcePoolClassifierSettingsEntry(const TRule_alter_table_setting_entry& entry, std::map<TString, TDeferredAtom>& result);
  129. bool ResetTableSettingsEntry(const TIdentifier& id, TTableSettings& settings, ETableType tableType);
  130. bool CreateTableIndex(const TRule_table_index& node, TVector<TIndexDescription>& indexes);
  131. bool CreateIndexSettings(const TRule_with_index_settings& settingsNode, TIndexDescription::EType indexType, TIndexDescription::TIndexSettings& indexSettings);
  132. bool CreateIndexSettingEntry(const TIdentifier& id, const TRule_index_setting_value& value, TIndexDescription::EType indexType, TIndexDescription::TIndexSettings& indexSettings);
  133. template<typename T>
  134. std::tuple<bool, T, TString> GetIndexSettingValue(const TRule_index_setting_value& node);
  135. TIdentifier GetTopicConsumerId(const TRule_topic_consumer_ref& node);
  136. bool CreateConsumerSettings(const TRule_topic_consumer_settings& settingsNode, TTopicConsumerSettings& settings);
  137. bool CreateTopicSettings(const TRule_topic_settings& node, TTopicSettings& params);
  138. bool CreateTopicConsumer(const TRule_topic_create_consumer_entry& node,
  139. TVector<TTopicConsumerDescription>& consumers);
  140. bool CreateTopicEntry(const TRule_create_topic_entry& node, TCreateTopicParameters& params);
  141. bool AlterTopicConsumer(const TRule_alter_topic_alter_consumer& node,
  142. THashMap<TString, TTopicConsumerDescription>& alterConsumers);
  143. bool AlterTopicConsumerEntry(const TRule_alter_topic_alter_consumer_entry& node,
  144. TTopicConsumerDescription& alterConsumer);
  145. bool AlterTopicAction(const TRule_alter_topic_action& node, TAlterTopicParameters& params);
  146. TNodePtr TypeSimple(const TRule_type_name_simple& node, bool onlyDataAllowed);
  147. TNodePtr TypeDecimal(const TRule_type_name_decimal& node);
  148. TNodePtr AddOptionals(const TNodePtr& node, size_t optionalCount);
  149. TMaybe<std::pair<TVector<TNodePtr>, bool>> CallableArgList(const TRule_callable_arg_list& argList, bool namedArgsStarted);
  150. TNodePtr IntegerOrBind(const TRule_integer_or_bind& node);
  151. TNodePtr TypeNameTag(const TRule_type_name_tag& node);
  152. TNodePtr TypeNodeOrBind(const TRule_type_name_or_bind& node);
  153. TNodePtr SerialTypeNode(const TRule_type_name_or_bind& node);
  154. TNodePtr TypeNode(const TRule_type_name& node);
  155. TNodePtr TypeNode(const TRule_type_name_composite& node);
  156. TNodePtr ValueConstructorLiteral(const TRule_value_constructor_literal& node);
  157. TNodePtr ValueConstructor(const TRule_value_constructor& node);
  158. TNodePtr ListLiteral(const TRule_list_literal& node);
  159. TNodePtr DictLiteral(const TRule_dict_literal& node);
  160. TNodePtr StructLiteral(const TRule_struct_literal& node);
  161. TMaybe<TTableHints> TableHintsImpl(const TRule_table_hints& node, const TString& provider, const TString& keyFunc = "");
  162. bool TableHintImpl(const TRule_table_hint& rule, TTableHints& hints, const TString& provider, const TString& keyFunc = "");
  163. bool SimpleTableRefImpl(const TRule_simple_table_ref& node, TTableRef& result);
  164. bool TopicRefImpl(const TRule_topic_ref& node, TTopicRef& result);
  165. TWindowSpecificationPtr WindowSpecification(const TRule_window_specification_details& rule);
  166. bool OrderByClause(const TRule_order_by_clause& node, TVector<TSortSpecificationPtr>& orderBy);
  167. bool SortSpecificationList(const TRule_sort_specification_list& node, TVector<TSortSpecificationPtr>& sortSpecs);
  168. bool IsDistinctOptSet(const TRule_opt_set_quantifier& node) const;
  169. bool IsDistinctOptSet(const TRule_opt_set_quantifier& node, TPosition& distinctPos) const;
  170. bool AddObjectFeature(std::map<TString, TDeferredAtom>& result, const TRule_object_feature& feature);
  171. bool BindParameterClause(const TRule_bind_parameter& node, TDeferredAtom& result);
  172. bool ObjectFeatureValueClause(const TRule_object_feature_value& node, TDeferredAtom& result);
  173. bool ParseObjectFeatures(std::map<TString, TDeferredAtom>& result, const TRule_object_features& features);
  174. bool ParseExternalDataSourceSettings(std::map<TString, TDeferredAtom>& result, const TRule_with_table_settings& settings);
  175. bool ParseExternalDataSourceSettings(std::map<TString, TDeferredAtom>& result, std::set<TString>& toReset, const TRule_alter_external_data_source_action& alterActions);
  176. bool ParseViewOptions(std::map<TString, TDeferredAtom>& features, const TRule_with_table_settings& options);
  177. bool ParseViewQuery(std::map<TString, TDeferredAtom>& features, const TRule_select_stmt& query);
  178. bool ParseResourcePoolSettings(std::map<TString, TDeferredAtom>& result, const TRule_with_table_settings& settings);
  179. bool ParseResourcePoolSettings(std::map<TString, TDeferredAtom>& result, std::set<TString>& toReset, const TRule_alter_resource_pool_action& alterAction);
  180. bool ParseResourcePoolClassifierSettings(std::map<TString, TDeferredAtom>& result, const TRule_with_table_settings& settings);
  181. bool ParseResourcePoolClassifierSettings(std::map<TString, TDeferredAtom>& result, std::set<TString>& toReset, const TRule_alter_resource_pool_classifier_action& alterAction);
  182. bool RoleNameClause(const TRule_role_name& node, TDeferredAtom& result, bool allowSystemRoles);
  183. bool PasswordParameter(const TRule_password_option& passwordOption, TUserParameters& result);
  184. bool HashParameter(const TRule_hash_option& hashOption, TUserParameters& result);
  185. void LoginParameter(const TRule_login_option& loginOption, std::optional<bool>& canLogin);
  186. bool UserParameters(const std::vector<TRule_user_option>& optionsList, TUserParameters& result, bool isCreateUser);
  187. bool PermissionNameClause(const TRule_permission_name_target& node, TVector<TDeferredAtom>& result, bool withGrantOption);
  188. bool PermissionNameClause(const TRule_permission_name& node, TDeferredAtom& result);
  189. bool PermissionNameClause(const TRule_permission_id& node, TDeferredAtom& result);
  190. bool StoreStringSettingsEntry(const TIdentifier& id, const TRule_table_setting_value* value, std::map<TString, TDeferredAtom>& result);
  191. bool StoreStringSettingsEntry(const TRule_alter_table_setting_entry& entry, std::map<TString, TDeferredAtom>& result);
  192. bool ParseBackupCollectionSettings(std::map<TString, TDeferredAtom>& result, const TRule_backup_collection_settings& settings);
  193. bool ParseBackupCollectionSettings(std::map<TString, TDeferredAtom>& result, std::set<TString>& toReset, const TRule_alter_backup_collection_actions& actions);
  194. bool ParseBackupCollectionTables(TVector<TDeferredAtom>& result, const TRule_table_list& tables);
  195. bool ParseBackupCollectionEntry(
  196. bool& addDatabase,
  197. bool& removeDatabase,
  198. TVector<TDeferredAtom>& addTables,
  199. TVector<TDeferredAtom>& removeTables,
  200. const TRule_alter_backup_collection_entry& entry);
  201. bool ParseBackupCollectionEntries(
  202. bool& addDatabase,
  203. bool& removeDatabase,
  204. TVector<TDeferredAtom>& addTables,
  205. TVector<TDeferredAtom>& removeTables,
  206. const TRule_alter_backup_collection_entries& entries);
  207. bool ParseTransferLambda(TString& lambdaText, const TRule_lambda_or_parameter& lambdaOrParameter);
  208. bool ValidateAuthMethod(const std::map<TString, TDeferredAtom>& result);
  209. bool ValidateExternalTable(const TCreateTableParameters& params);
  210. TNodePtr ReturningList(const ::NSQLv1Generated::TRule_returning_columns_list& columns);
  211. private:
  212. bool SimpleTableRefCoreImpl(const TRule_simple_table_ref_core& node, TTableRef& result);
  213. static bool IsValidFrameSettings(TContext& ctx, const TFrameSpecification& frameSpec, size_t sortSpecSize);
  214. static TString FrameSettingsToString(EFrameSettings settings, bool isUnbounded);
  215. bool FrameBound(const TRule_window_frame_bound& rule, TFrameBoundPtr& bound);
  216. bool FrameClause(const TRule_window_frame_clause& node, TFrameSpecificationPtr& frameSpec, size_t sortSpecSize);
  217. bool SortSpecification(const TRule_sort_specification& node, TVector<TSortSpecificationPtr>& sortSpecs);
  218. bool ClusterExpr(const TRule_cluster_expr& node, bool allowWildcard, bool allowBinding, TString& service, TDeferredAtom& cluster, bool& isBinding);
  219. bool StructLiteralItem(TVector<TNodePtr>& labels, const TRule_expr& label, TVector<TNodePtr>& values, const TRule_expr& value);
  220. bool ValidateTableSettings(const TTableSettings& settings);
  221. protected:
  222. NSQLTranslation::ESqlMode Mode;
  223. };
  224. TNodePtr LiteralNumber(TContext& ctx, const TRule_integer& node);
  225. bool StoreString(const TRule_family_setting_value& from, TNodePtr& to, TContext& ctx);
  226. bool StoreInt(const TRule_family_setting_value& from, TNodePtr& to, TContext& ctx);
  227. template<typename TChar>
  228. struct TPatternComponent {
  229. TBasicString<TChar> Prefix;
  230. TBasicString<TChar> Suffix;
  231. bool IsSimple = true;
  232. void AppendPlain(TChar c) {
  233. if (IsSimple) {
  234. Prefix.push_back(c);
  235. }
  236. Suffix.push_back(c);
  237. }
  238. void AppendAnyChar() {
  239. IsSimple = false;
  240. Suffix.clear();
  241. }
  242. };
  243. template<typename TChar>
  244. TVector<TPatternComponent<TChar>> SplitPattern(const TBasicString<TChar>& pattern, TMaybe<char> escape, bool& inEscape) {
  245. inEscape = false;
  246. TVector<TPatternComponent<TChar>> result;
  247. TPatternComponent<TChar> current;
  248. bool prevIsPercentChar = false;
  249. for (const TChar c : pattern) {
  250. if (inEscape) {
  251. current.AppendPlain(c);
  252. inEscape = false;
  253. prevIsPercentChar = false;
  254. } else if (escape && c == static_cast<TChar>(*escape)) {
  255. inEscape = true;
  256. } else if (c == '%') {
  257. if (!prevIsPercentChar) {
  258. result.push_back(std::move(current));
  259. }
  260. current = {};
  261. prevIsPercentChar = true;
  262. } else if (c == '_') {
  263. current.AppendAnyChar();
  264. prevIsPercentChar = false;
  265. } else {
  266. current.AppendPlain(c);
  267. prevIsPercentChar = false;
  268. }
  269. }
  270. result.push_back(std::move(current));
  271. return result;
  272. }
  273. bool ParseNumbers(TContext& ctx, const TString& strOrig, ui64& value, TString& suffix);
  274. } // namespace NSQLTranslationV1