yql_reprcode.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "yql_formatcode.h"
  2. #include "yql_type_resource.h"
  3. #include "yql_position.h"
  4. #include <yql/essentials/providers/common/schema/expr/yql_expr_schema.h>
  5. #include <yql/essentials/minikql/computation/mkql_computation_node_impl.h>
  6. #include <yql/essentials/minikql/mkql_node_cast.h>
  7. #include <yql/essentials/providers/common/codec/yql_codec.h>
  8. namespace NKikimr {
  9. namespace NMiniKQL {
  10. class TReprCodeWrapper : public TMutableComputationNode<TReprCodeWrapper> {
  11. typedef TMutableComputationNode<TReprCodeWrapper> TBaseComputation;
  12. public:
  13. TReprCodeWrapper(TComputationMutables& mutables, IComputationNode* value, const TString& yson, ui32 exprCtxMutableIndex, NYql::TPosition pos)
  14. : TBaseComputation(mutables)
  15. , Value_(value)
  16. , Yson_(yson)
  17. , ExprCtxMutableIndex_(exprCtxMutableIndex)
  18. , Pos_(pos)
  19. {}
  20. NUdf::TUnboxedValue DoCalculate(TComputationContext& ctx) const {
  21. auto exprCtxPtr = GetExprContextPtr(ctx, ExprCtxMutableIndex_);
  22. const NYql::TTypeAnnotationNode* type = NYql::NCommon::ParseTypeFromYson(TStringBuf{Yson_}, *exprCtxPtr, Pos_);
  23. auto value = Value_->GetValue(ctx);
  24. auto node = NYql::NCommon::ValueToExprLiteral(type, value, *exprCtxPtr, exprCtxPtr->AppendPosition(Pos_));
  25. return NUdf::TUnboxedValuePod(new TYqlCodeResource(exprCtxPtr, node));
  26. }
  27. void RegisterDependencies() const override {
  28. DependsOn(Value_);
  29. }
  30. private:
  31. IComputationNode* Value_;
  32. TString Yson_;
  33. ui32 ExprCtxMutableIndex_;
  34. NYql::TPosition Pos_;
  35. };
  36. IComputationNode* WrapReprCode(TCallable& callable, const TComputationNodeFactoryContext& ctx, ui32 exprCtxMutableIndex) {
  37. MKQL_ENSURE(callable.GetInputsCount() == 5, "Expected 5 args");
  38. auto pos = ExtractPosition(callable);
  39. auto value = LocateNode(ctx.NodeLocator, callable, 3);
  40. TString yson(TStringBuf(AS_VALUE(TDataLiteral, callable.GetInput(4))->AsValue().AsStringRef()));
  41. return new TReprCodeWrapper(ctx.Mutables, value, yson, exprCtxMutableIndex, pos);
  42. }
  43. }
  44. }