value.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. #include "value.h"
  2. #include <yql/essentials/minikql/dom/node.h>
  3. namespace NYql::NJsonPath {
  4. using namespace NUdf;
  5. using namespace NDom;
  6. using namespace NKikimr;
  7. using namespace NKikimr::NBinaryJson;
  8. TArrayIterator::TArrayIterator()
  9. : Iterator(TEmptyMarker())
  10. {
  11. }
  12. TArrayIterator::TArrayIterator(const TUnboxedValue& iterator)
  13. : Iterator(iterator)
  14. {
  15. }
  16. TArrayIterator::TArrayIterator(TUnboxedValue&& iterator)
  17. : Iterator(std::move(iterator))
  18. {
  19. }
  20. TArrayIterator::TArrayIterator(const NBinaryJson::TArrayIterator& iterator)
  21. : Iterator(iterator)
  22. {
  23. }
  24. TArrayIterator::TArrayIterator(NBinaryJson::TArrayIterator&& iterator)
  25. : Iterator(std::move(iterator))
  26. {
  27. }
  28. bool TArrayIterator::Next(TValue& value) {
  29. if (std::holds_alternative<TEmptyMarker>(Iterator)) {
  30. return false;
  31. } else if (auto* iterator = std::get_if<NBinaryJson::TArrayIterator>(&Iterator)) {
  32. if (!iterator->HasNext()) {
  33. return false;
  34. }
  35. value = TValue(iterator->Next());
  36. return true;
  37. } else if (auto* iterator = std::get_if<TUnboxedValue>(&Iterator)) {
  38. TUnboxedValue result;
  39. const bool success = iterator->Next(result);
  40. if (success) {
  41. value = TValue(result);
  42. }
  43. return success;
  44. } else {
  45. Y_ABORT("Unexpected variant case in Next");
  46. }
  47. }
  48. TObjectIterator::TObjectIterator()
  49. : Iterator(TEmptyMarker())
  50. {
  51. }
  52. TObjectIterator::TObjectIterator(const TUnboxedValue& iterator)
  53. : Iterator(iterator)
  54. {
  55. }
  56. TObjectIterator::TObjectIterator(TUnboxedValue&& iterator)
  57. : Iterator(std::move(iterator))
  58. {
  59. }
  60. TObjectIterator::TObjectIterator(const NBinaryJson::TObjectIterator& iterator)
  61. : Iterator(iterator)
  62. {
  63. }
  64. TObjectIterator::TObjectIterator(NBinaryJson::TObjectIterator&& iterator)
  65. : Iterator(std::move(iterator))
  66. {
  67. }
  68. bool TObjectIterator::Next(TValue& key, TValue& value) {
  69. if (std::holds_alternative<TEmptyMarker>(Iterator)) {
  70. return false;
  71. } else if (auto* iterator = std::get_if<NBinaryJson::TObjectIterator>(&Iterator)) {
  72. if (!iterator->HasNext()) {
  73. return false;
  74. }
  75. const auto [itKey, itValue] = iterator->Next();
  76. key = TValue(itKey);
  77. value = TValue(itValue);
  78. return true;
  79. } else if (auto* iterator = std::get_if<TUnboxedValue>(&Iterator)) {
  80. TUnboxedValue itKey;
  81. TUnboxedValue itValue;
  82. const bool success = iterator->NextPair(itKey, itValue);
  83. if (success) {
  84. key = TValue(itKey);
  85. value = TValue(itValue);
  86. }
  87. return success;
  88. } else {
  89. Y_ABORT("Unexpected variant case in Next");
  90. }
  91. }
  92. TValue::TValue()
  93. : Value(MakeEntity())
  94. {
  95. }
  96. TValue::TValue(const TUnboxedValue& value)
  97. : Value(value)
  98. {
  99. }
  100. TValue::TValue(TUnboxedValue&& value)
  101. : Value(std::move(value))
  102. {
  103. }
  104. TValue::TValue(const TEntryCursor& value)
  105. : Value(value)
  106. {
  107. UnpackInnerValue();
  108. }
  109. TValue::TValue(TEntryCursor&& value)
  110. : Value(std::move(value))
  111. {
  112. UnpackInnerValue();
  113. }
  114. TValue::TValue(const TContainerCursor& value)
  115. : Value(value)
  116. {
  117. UnpackInnerValue();
  118. }
  119. TValue::TValue(TContainerCursor&& value)
  120. : Value(std::move(value))
  121. {
  122. UnpackInnerValue();
  123. }
  124. EValueType TValue::GetType() const {
  125. if (const auto* value = std::get_if<TEntryCursor>(&Value)) {
  126. switch (value->GetType()) {
  127. case EEntryType::BoolFalse:
  128. case EEntryType::BoolTrue:
  129. return EValueType::Bool;
  130. case EEntryType::Null:
  131. return EValueType::Null;
  132. case EEntryType::Number:
  133. return EValueType::Number;
  134. case EEntryType::String:
  135. return EValueType::String;
  136. case EEntryType::Container:
  137. Y_ABORT("Logical error: TEntryCursor with Container type must be converted to TContainerCursor");
  138. }
  139. } else if (const auto* value = std::get_if<TContainerCursor>(&Value)) {
  140. switch (value->GetType()) {
  141. case EContainerType::Array:
  142. return EValueType::Array;
  143. case EContainerType::Object:
  144. return EValueType::Object;
  145. case EContainerType::TopLevelScalar:
  146. Y_ABORT("Logical error: TContainerCursor with TopLevelScalar type must be converted to TEntryCursor");
  147. }
  148. } else if (const auto* value = std::get_if<TUnboxedValue>(&Value)) {
  149. switch (GetNodeType(*value)) {
  150. case ENodeType::Bool:
  151. return EValueType::Bool;
  152. case ENodeType::Double:
  153. case ENodeType::Int64:
  154. case ENodeType::Uint64:
  155. return EValueType::Number;
  156. case ENodeType::Dict:
  157. case ENodeType::Attr:
  158. return EValueType::Object;
  159. case ENodeType::List:
  160. return EValueType::Array;
  161. case ENodeType::String:
  162. return EValueType::String;
  163. case ENodeType::Entity:
  164. return EValueType::Null;
  165. }
  166. } else {
  167. Y_ABORT("Unexpected variant case in GetType");
  168. }
  169. }
  170. bool TValue::Is(EValueType type) const {
  171. return GetType() == type;
  172. }
  173. bool TValue::IsBool() const {
  174. return Is(EValueType::Bool);
  175. }
  176. bool TValue::IsNumber() const {
  177. return Is(EValueType::Number);
  178. }
  179. bool TValue::IsString() const {
  180. return Is(EValueType::String);
  181. }
  182. bool TValue::IsNull() const {
  183. return Is(EValueType::Null);
  184. }
  185. bool TValue::IsObject() const {
  186. return Is(EValueType::Object);
  187. }
  188. bool TValue::IsArray() const {
  189. return Is(EValueType::Array);
  190. }
  191. double TValue::GetNumber() const {
  192. Y_DEBUG_ABORT_UNLESS(IsNumber());
  193. if (const auto* value = std::get_if<TEntryCursor>(&Value)) {
  194. return value->GetNumber();
  195. } else if (const auto* value = std::get_if<TUnboxedValue>(&Value)) {
  196. if (IsNodeType(*value, ENodeType::Double)) {
  197. return value->Get<double>();
  198. } else if (IsNodeType(*value, ENodeType::Int64)) {
  199. return static_cast<double>(value->Get<i64>());
  200. } else {
  201. return static_cast<double>(value->Get<ui64>());
  202. }
  203. } else {
  204. Y_ABORT("Unexpected variant case in GetNumber");
  205. }
  206. }
  207. bool TValue::GetBool() const {
  208. Y_DEBUG_ABORT_UNLESS(IsBool());
  209. if (const auto* value = std::get_if<TEntryCursor>(&Value)) {
  210. return value->GetType() == EEntryType::BoolTrue;
  211. } else if (const auto* value = std::get_if<TUnboxedValue>(&Value)) {
  212. return value->Get<bool>();
  213. } else {
  214. Y_ABORT("Unexpected variant case in GetBool");
  215. }
  216. }
  217. const TStringBuf TValue::GetString() const {
  218. Y_DEBUG_ABORT_UNLESS(IsString());
  219. if (const auto* value = std::get_if<TEntryCursor>(&Value)) {
  220. return value->GetString();
  221. } else if (const auto* value = std::get_if<TUnboxedValue>(&Value)) {
  222. return value->AsStringRef();
  223. } else {
  224. Y_ABORT("Unexpected variant case in GetString");
  225. }
  226. }
  227. ui32 TValue::GetSize() const {
  228. Y_DEBUG_ABORT_UNLESS(IsArray() || IsObject());
  229. if (const auto* value = std::get_if<TContainerCursor>(&Value)) {
  230. return value->GetSize();
  231. } else if (const auto* value = std::get_if<TUnboxedValue>(&Value)) {
  232. if (value->IsEmbedded()) {
  233. return 0;
  234. }
  235. if (IsNodeType(*value, ENodeType::List)) {
  236. return value->GetListLength();
  237. } else {
  238. return value->GetDictLength();
  239. }
  240. } else {
  241. Y_ABORT("Unexpected variant case in GetSize");
  242. }
  243. }
  244. TValue TValue::GetElement(ui32 index) const {
  245. Y_DEBUG_ABORT_UNLESS(IsArray());
  246. if (const auto* value = std::get_if<TContainerCursor>(&Value)) {
  247. return TValue(value->GetElement(index));
  248. } else if (const auto* value = std::get_if<TUnboxedValue>(&Value)) {
  249. return TValue(value->Lookup(TUnboxedValuePod(index)));
  250. } else {
  251. Y_ABORT("Unexpected variant case in GetElement");
  252. }
  253. }
  254. TArrayIterator TValue::GetArrayIterator() const {
  255. Y_DEBUG_ABORT_UNLESS(IsArray());
  256. if (const auto* value = std::get_if<TContainerCursor>(&Value)) {
  257. return TArrayIterator(value->GetArrayIterator());
  258. } else if (const auto* value = std::get_if<TUnboxedValue>(&Value)) {
  259. if (value->IsEmbedded()) {
  260. return TArrayIterator();
  261. }
  262. return TArrayIterator(value->GetListIterator());
  263. } else {
  264. Y_ABORT("Unexpected variant case in GetArrayIterator");
  265. }
  266. }
  267. TMaybe<TValue> TValue::Lookup(const TStringBuf key) const {
  268. Y_DEBUG_ABORT_UNLESS(IsObject());
  269. if (const auto* value = std::get_if<TContainerCursor>(&Value)) {
  270. const auto payload = value->Lookup(key);
  271. if (!payload.Defined()) {
  272. return Nothing();
  273. }
  274. return TValue(*payload);
  275. } else if (const auto* value = std::get_if<TUnboxedValue>(&Value)) {
  276. if (value->IsEmbedded()) {
  277. return Nothing();
  278. }
  279. // Lookup on TUnboxedValue can be performed only with TUnboxedValue key.
  280. // To avoid allocating new string we use our custom Lookup method defined
  281. // on underlying TMapNode that accepts TStringRef
  282. const auto* dict = static_cast<const TMapNode*>(value->AsBoxed().Get());
  283. if (const auto payload = dict->Lookup(key)) {
  284. return {TValue(payload)};
  285. } else {
  286. return Nothing();
  287. }
  288. } else {
  289. Y_ABORT("Unexpected variant case in Lookup");
  290. }
  291. }
  292. TObjectIterator TValue::GetObjectIterator() const {
  293. Y_DEBUG_ABORT_UNLESS(IsObject());
  294. if (const auto* value = std::get_if<TContainerCursor>(&Value)) {
  295. return TObjectIterator(value->GetObjectIterator());
  296. } else if (const auto* value = std::get_if<TUnboxedValue>(&Value)) {
  297. if (value->IsEmbedded()) {
  298. return TObjectIterator();
  299. }
  300. return TObjectIterator(value->GetDictIterator());
  301. } else {
  302. Y_ABORT("Unexpected variant case in GetObjectIterator");
  303. }
  304. }
  305. TUnboxedValue TValue::ConvertToUnboxedValue(const NUdf::IValueBuilder* valueBuilder) const {
  306. if (const auto* value = std::get_if<TEntryCursor>(&Value)) {
  307. return ReadElementToJsonDom(*value, valueBuilder);
  308. } else if (const auto* value = std::get_if<TContainerCursor>(&Value)) {
  309. return ReadContainerToJsonDom(*value, valueBuilder);
  310. } else if (const auto* value = std::get_if<TUnboxedValue>(&Value)) {
  311. return *value;
  312. } else {
  313. Y_ABORT("Unexpected variant case in ConvertToUnboxedValue");
  314. }
  315. }
  316. void TValue::UnpackInnerValue() {
  317. // If TEntryCursor points to container, we need to extract TContainerCursor
  318. if (const auto* value = std::get_if<TEntryCursor>(&Value)) {
  319. if (value->GetType() == EEntryType::Container) {
  320. Value = value->GetContainer();
  321. }
  322. }
  323. // If TContainerCursor points to top level scalar, we need to extract TEntryCursor
  324. if (const auto* value = std::get_if<TContainerCursor>(&Value)) {
  325. if (value->GetType() == EContainerType::TopLevelScalar) {
  326. Value = value->GetElement(0);
  327. }
  328. }
  329. }
  330. }