sql_expression.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #pragma once
  2. #include "sql_translation.h"
  3. #include <yql/essentials/core/sql_types/yql_atom_enums.h>
  4. namespace NSQLTranslationV1 {
  5. using namespace NSQLv1Generated;
  6. class TSqlExpression: public TSqlTranslation {
  7. public:
  8. enum class ESmartParenthesis {
  9. Default,
  10. GroupBy,
  11. InStatement,
  12. SqlLambdaParams,
  13. };
  14. TSqlExpression(TContext& ctx, NSQLTranslation::ESqlMode mode)
  15. : TSqlTranslation(ctx, mode)
  16. {
  17. }
  18. TNodePtr Build(const TRule_expr& node);
  19. void SetSmartParenthesisMode(ESmartParenthesis mode) {
  20. SmartParenthesisMode = mode;
  21. }
  22. void MarkAsNamed() {
  23. MaybeUnnamedSmartParenOnTop = false;
  24. }
  25. TMaybe<TExprOrIdent> LiteralExpr(const TRule_literal_value& node);
  26. private:
  27. struct TTrailingQuestions {
  28. size_t Count = 0;
  29. TPosition Pos;
  30. };
  31. TNodePtr BindParameterRule(const TRule_bind_parameter& rule, const TTrailingQuestions& tail);
  32. TNodePtr LambdaRule(const TRule_lambda& rule);
  33. TNodePtr CastRule(const TRule_cast_expr& rule);
  34. TNodePtr BitCastRule(const TRule_bitcast_expr& rule);
  35. TNodePtr ExistsRule(const TRule_exists_expr& rule);
  36. TNodePtr CaseRule(const TRule_case_expr& rule);
  37. TMaybe<TExprOrIdent> AtomExpr(const TRule_atom_expr& node, const TTrailingQuestions& tail);
  38. TMaybe<TExprOrIdent> InAtomExpr(const TRule_in_atom_expr& node, const TTrailingQuestions& tail);
  39. TNodePtr JsonInputArg(const TRule_json_common_args& node);
  40. TNodePtr JsonPathSpecification(const TRule_jsonpath_spec& node);
  41. TNodePtr JsonReturningTypeRule(const TRule_type_name_simple& node);
  42. TNodePtr JsonValueCaseHandler(const TRule_json_case_handler& node, EJsonValueHandlerMode& mode);
  43. void AddJsonValueCaseHandlers(const TRule_json_value& node, TVector<TNodePtr>& children);
  44. void AddJsonVariable(const TRule_json_variable& node, TVector<TNodePtr>& children);
  45. void AddJsonVariables(const TRule_json_variables& node, TVector<TNodePtr>& children);
  46. TNodePtr JsonVariables(const TRule_json_common_args& node);
  47. void AddJsonCommonArgs(const TRule_json_common_args& node, TVector<TNodePtr>& children);
  48. TNodePtr JsonValueExpr(const TRule_json_value& node);
  49. void AddJsonExistsHandler(const TRule_json_exists& node, TVector<TNodePtr>& children);
  50. TNodePtr JsonExistsExpr(const TRule_json_exists& node);
  51. EJsonQueryWrap JsonQueryWrapper(const TRule_json_query& node);
  52. EJsonQueryHandler JsonQueryHandler(const TRule_json_query_handler& node);
  53. TNodePtr JsonQueryExpr(const TRule_json_query& node);
  54. TNodePtr JsonApiExpr(const TRule_json_api_expr& node);
  55. template<typename TUnaryCasualExprRule>
  56. TNodePtr UnaryCasualExpr(const TUnaryCasualExprRule& node, const TTrailingQuestions& tail);
  57. template<typename TUnarySubExprRule>
  58. TNodePtr UnaryExpr(const TUnarySubExprRule& node, const TTrailingQuestions& tail);
  59. bool SqlLambdaParams(const TNodePtr& node, TVector<TSymbolNameWithPos>& args, ui32& optionalArgumentsCount);
  60. bool SqlLambdaExprBody(TContext& ctx, const TRule_lambda_body& node, TVector<TNodePtr>& exprSeq);
  61. bool SqlLambdaExprBody(TContext& ctx, const TRule_expr& node, TVector<TNodePtr>& exprSeq);
  62. TNodePtr KeyExpr(const TRule_key_expr& node) {
  63. TSqlExpression expr(Ctx, Mode);
  64. return expr.Build(node.GetRule_expr2());
  65. }
  66. TNodePtr SubExpr(const TRule_con_subexpr& node, const TTrailingQuestions& tail);
  67. TNodePtr SubExpr(const TRule_xor_subexpr& node, const TTrailingQuestions& tail);
  68. TNodePtr SubExpr(const TRule_mul_subexpr& node, const TTrailingQuestions& tail);
  69. TNodePtr SubExpr(const TRule_add_subexpr& node, const TTrailingQuestions& tail);
  70. TNodePtr SubExpr(const TRule_bit_subexpr& node, const TTrailingQuestions& tail);
  71. TNodePtr SubExpr(const TRule_neq_subexpr& node, const TTrailingQuestions& tailExternal);
  72. TNodePtr SubExpr(const TRule_eq_subexpr& node, const TTrailingQuestions& tail);
  73. TNodePtr SubExpr(const TRule_or_subexpr& node, const TTrailingQuestions& tail);
  74. TNodePtr SubExpr(const TRule_and_subexpr& node, const TTrailingQuestions& tail);
  75. template <typename TNode, typename TGetNode, typename TIter>
  76. TNodePtr BinOpList(const TNode& node, TGetNode getNode, TIter begin, TIter end, const TTrailingQuestions& tail);
  77. template <typename TGetNode, typename TIter>
  78. TNodePtr BinOpList(const TRule_bit_subexpr& node, TGetNode getNode, TIter begin, TIter end, const TTrailingQuestions& tail);
  79. template <typename TGetNode, typename TIter>
  80. TNodePtr BinOpList(const TRule_eq_subexpr& node, TGetNode getNode, TIter begin, TIter end, const TTrailingQuestions& tail);
  81. TNodePtr BinOperList(const TString& opName, TVector<TNodePtr>::const_iterator begin, TVector<TNodePtr>::const_iterator end) const;
  82. TNodePtr RowPatternVarAccess(const TString& alias, const TRule_unary_subexpr_suffix_TBlock1_TBlock1_TAlt3_TBlock2 block);
  83. struct TCaseBranch {
  84. TNodePtr Pred;
  85. TNodePtr Value;
  86. };
  87. TCaseBranch ReduceCaseBranches(TVector<TCaseBranch>::const_iterator begin, TVector<TCaseBranch>::const_iterator end) const;
  88. template <typename TNode, typename TGetNode, typename TIter>
  89. TNodePtr BinOper(const TString& operName, const TNode& node, TGetNode getNode, TIter begin, TIter end, const TTrailingQuestions& tail);
  90. TNodePtr SqlInExpr(const TRule_in_expr& node, const TTrailingQuestions& tail);
  91. void UnexpectedQuestionToken(const TTrailingQuestions& tail) {
  92. YQL_ENSURE(tail.Count > 0);
  93. Ctx.Error(tail.Pos) << "Unexpected token '?' at the end of expression";
  94. }
  95. TNodePtr SmartParenthesis(const TRule_smart_parenthesis& node);
  96. ESmartParenthesis SmartParenthesisMode = ESmartParenthesis::Default;
  97. bool MaybeUnnamedSmartParenOnTop = true;
  98. THashMap<TString, TNodePtr> ExprShortcuts;
  99. };
  100. bool ChangefeedSettingsEntry(const TRule_changefeed_settings_entry& node, TSqlExpression& ctx, TChangefeedSettings& settings, bool alter);
  101. bool ChangefeedSettings(const TRule_changefeed_settings& node, TSqlExpression& ctx, TChangefeedSettings& settings, bool alter);
  102. bool CreateChangefeed(const TRule_changefeed& node, TSqlExpression& ctx, TVector<TChangefeedDescription>& changefeeds);
  103. bool Expr(TSqlExpression& sqlExpr, TVector<TNodePtr>& exprNodes, const TRule_expr& node);
  104. bool ExprList(TSqlExpression& sqlExpr, TVector<TNodePtr>& exprNodes, const TRule_expr_list& node);
  105. } // namespace NSQLTranslationV1