mkql_fromyson.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include "mkql_fromyson.h"
  2. #include <yql/essentials/minikql/computation/mkql_computation_node_holders.h>
  3. #include <yql/essentials/minikql/mkql_node_cast.h>
  4. #include <yql/essentials/minikql/mkql_node_builder.h>
  5. namespace NKikimr {
  6. namespace NMiniKQL {
  7. namespace {
  8. template<bool IsOptional>
  9. class TFromYsonSimpleTypeWrapper : public TMutableComputationNode<TFromYsonSimpleTypeWrapper<IsOptional>> {
  10. typedef TMutableComputationNode<TFromYsonSimpleTypeWrapper<IsOptional>> TBaseComputation;
  11. public:
  12. TFromYsonSimpleTypeWrapper(TComputationMutables& mutables, IComputationNode* data, NUdf::TDataTypeId schemeType)
  13. : TBaseComputation(mutables)
  14. , Data(data)
  15. , SchemeType(NUdf::GetDataSlot(schemeType))
  16. {}
  17. NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
  18. const auto& data = Data->GetValue(ctx);
  19. if (IsOptional && !data) {
  20. return NUdf::TUnboxedValuePod();
  21. }
  22. return SimpleValueFromYson(SchemeType, data.AsStringRef());
  23. }
  24. private:
  25. void RegisterDependencies() const final {
  26. this->DependsOn(Data);
  27. }
  28. IComputationNode* const Data;
  29. const NUdf::EDataSlot SchemeType;
  30. };
  31. }
  32. IComputationNode* WrapFromYsonSimpleType(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
  33. MKQL_ENSURE(callable.GetInputsCount() == 2, "Expected 2 args");
  34. bool isOptional;
  35. const auto dataType = UnpackOptionalData(callable.GetInput(0), isOptional);
  36. const auto dataSchemeType = dataType->GetSchemeType();
  37. MKQL_ENSURE(dataSchemeType == NUdf::TDataType<char*>::Id || dataSchemeType == NUdf::TDataType<NUdf::TYson>::Id,
  38. "Expected String or Yson");
  39. const auto schemeTypeData = AS_VALUE(TDataLiteral, callable.GetInput(1));
  40. const auto schemeType = schemeTypeData->AsValue().Get<ui32>();
  41. const auto data = LocateNode(ctx.NodeLocator, callable, 0);
  42. if (isOptional) {
  43. return new TFromYsonSimpleTypeWrapper<true>(ctx.Mutables, data, static_cast<NUdf::TDataTypeId>(schemeType));
  44. } else {
  45. return new TFromYsonSimpleTypeWrapper<false>(ctx.Mutables, data, static_cast<NUdf::TDataTypeId>(schemeType));
  46. }
  47. }
  48. }
  49. }