sql_expression.h 6.4 KB

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