executor.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #pragma once
  2. #include <yql/essentials/minikql/jsonpath/parser/binary.h>
  3. #include "value.h"
  4. #include <yql/essentials/public/issue/yql_issue.h>
  5. #include <yql/essentials/utils/yql_panic.h>
  6. #include <yql/essentials/public/udf/udf_value.h>
  7. #include <yql/essentials/public/udf/udf_value_builder.h>
  8. #include <yql/essentials/public/udf/udf_allocator.h>
  9. #include <library/cpp/json/json_value.h>
  10. #include <library/cpp/containers/stack_vector/stack_vec.h>
  11. #include <util/generic/ptr.h>
  12. #include <util/generic/stack.h>
  13. #include <util/generic/hash.h>
  14. #include <util/generic/maybe.h>
  15. #include <variant>
  16. namespace NYql::NJsonPath {
  17. using TJsonNodes = TSmallVec<TValue>;
  18. class TResult {
  19. public:
  20. TResult(TJsonNodes&& nodes);
  21. TResult(const TJsonNodes& nodes);
  22. TResult(TIssue&& issue);
  23. const TJsonNodes& GetNodes() const;
  24. TJsonNodes& GetNodes();
  25. const TIssue& GetError() const;
  26. bool IsError() const;
  27. private:
  28. std::variant<TJsonNodes, TIssue> Result;
  29. };
  30. class TArraySubscript {
  31. public:
  32. TArraySubscript(i64 from, TPosition fromPos)
  33. : From(from)
  34. , FromPos(fromPos)
  35. , HasTo(false)
  36. {
  37. }
  38. TArraySubscript(i64 from, TPosition fromPos, i64 to, TPosition toPos)
  39. : From(from)
  40. , FromPos(fromPos)
  41. , To(to)
  42. , ToPos(toPos)
  43. , HasTo(true)
  44. {
  45. }
  46. i64 GetFrom() const {
  47. return From;
  48. }
  49. TPosition GetFromPos() const {
  50. return FromPos;
  51. }
  52. i64 GetTo() const {
  53. YQL_ENSURE(IsRange());
  54. return To;
  55. }
  56. TPosition GetToPos() const {
  57. return ToPos;
  58. }
  59. bool IsRange() const {
  60. return HasTo;
  61. }
  62. private:
  63. i64 From = 0;
  64. TPosition FromPos;
  65. i64 To = 0;
  66. TPosition ToPos;
  67. bool HasTo;
  68. };
  69. using TVariablesMap = THashMap<TString, TValue>;
  70. class TExecutor {
  71. public:
  72. TExecutor(
  73. const TJsonPathPtr path,
  74. const TJsonNodes& input,
  75. const TVariablesMap& variables,
  76. const NUdf::IValueBuilder* valueBuilder);
  77. TResult Execute();
  78. private:
  79. constexpr static double EPSILON = 1e-20;
  80. static bool IsZero(double value);
  81. static bool IsEqual(double a, double b);
  82. static bool IsLess(double a, double b);
  83. static bool IsGreater(double a, double b);
  84. bool IsStrict() const;
  85. bool IsLax() const;
  86. TResult Execute(const TJsonPathItem& item);
  87. TResult ContextObject();
  88. TResult Variable(const TJsonPathItem& item);
  89. TResult LastArrayIndex(const TJsonPathItem& item);
  90. TResult NumberLiteral(const TJsonPathItem& item);
  91. TResult MemberAccess(const TJsonPathItem& item);
  92. TResult WildcardMemberAccess(const TJsonPathItem& item);
  93. TMaybe<TIssue> EnsureSingleSubscript(TPosition pos, const TJsonNodes& index, i64& result);
  94. TMaybe<TIssue> EnsureArraySubscripts(const TJsonPathItem& item, TVector<TArraySubscript>& result);
  95. TResult ArrayAccess(const TJsonPathItem& item);
  96. TResult WildcardArrayAccess(const TJsonPathItem& item);
  97. TResult UnaryArithmeticOp(const TJsonPathItem& item);
  98. TMaybe<TIssue> EnsureBinaryArithmeticOpArgument(TPosition pos, const TJsonNodes& nodes, double& result);
  99. TResult BinaryArithmeticOp(const TJsonPathItem& item);
  100. TMaybe<TIssue> EnsureLogicalOpArgument(TPosition pos, const TJsonNodes& nodes, TMaybe<bool>& result);
  101. TResult BinaryLogicalOp(const TJsonPathItem& item);
  102. TResult UnaryLogicalOp(const TJsonPathItem& item);
  103. TResult BooleanLiteral(const TJsonPathItem& item);
  104. TResult NullLiteral();
  105. TResult StringLiteral(const TJsonPathItem& item);
  106. TMaybe<bool> CompareValues(const TValue& left, const TValue& right, EJsonPathItemType operation);
  107. TResult CompareOp(const TJsonPathItem& item);
  108. TResult FilterObject(const TJsonPathItem& item);
  109. TResult FilterPredicate(const TJsonPathItem& item);
  110. TResult NumericMethod(const TJsonPathItem& item);
  111. TResult DoubleMethod(const TJsonPathItem& item);
  112. TResult TypeMethod(const TJsonPathItem& item);
  113. TResult SizeMethod(const TJsonPathItem& item);
  114. TResult KeyValueMethod(const TJsonPathItem& item);
  115. TResult StartsWithPredicate(const TJsonPathItem& item);
  116. TResult IsUnknownPredicate(const TJsonPathItem& item);
  117. TResult ExistsPredicate(const TJsonPathItem& item);
  118. TResult LikeRegexPredicate(const TJsonPathItem& item);
  119. TJsonNodes OptionalUnwrapArrays(const TJsonNodes& input);
  120. TJsonNodes OptionalArrayWrapNodes(const TJsonNodes& input);
  121. TStack<TValue> ArraySubscriptSource;
  122. TStack<TValue> CurrentFilterObject;
  123. TJsonPathReader Reader;
  124. TJsonNodes Input;
  125. const TVariablesMap& Variables;
  126. const NUdf::IValueBuilder* ValueBuilder;
  127. };
  128. }