yql_formatcode.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "yql_formatcode.h"
  2. #include "yql_type_resource.h"
  3. #include <yql/essentials/minikql/computation/mkql_computation_node_impl.h>
  4. #include <yql/essentials/minikql/mkql_string_util.h>
  5. #include <yql/essentials/ast/serialize/yql_expr_serialize.h>
  6. namespace NKikimr {
  7. namespace NMiniKQL {
  8. class TFormatCodeWrapper : public TMutableComputationNode<TFormatCodeWrapper> {
  9. typedef TMutableComputationNode<TFormatCodeWrapper> TBaseComputation;
  10. public:
  11. TFormatCodeWrapper(TComputationMutables& mutables, IComputationNode* code, bool annotatePosition, ui32 exprCtxMutableIndex)
  12. : TBaseComputation(mutables)
  13. , Code_(code)
  14. , AnnotatePosition_(annotatePosition)
  15. , ExprCtxMutableIndex_(exprCtxMutableIndex)
  16. {}
  17. NUdf::TUnboxedValue DoCalculate(TComputationContext& ctx) const {
  18. auto codeValue = Code_->GetValue(ctx);
  19. auto code = GetYqlCode(codeValue);
  20. NYql::TExprContext& exprCtx = GetExprContext(ctx, ExprCtxMutableIndex_);
  21. NYql::TConvertToAstSettings settings;
  22. settings.AnnotationFlags = AnnotatePosition_ ?
  23. NYql::TExprAnnotationFlags::Position :
  24. NYql::TExprAnnotationFlags::None;
  25. settings.RefAtoms = true;
  26. settings.AllowFreeArgs = true;
  27. auto ast = NYql::ConvertToAst(*code, exprCtx, settings);
  28. auto str = ast.Root->ToString(NYql::TAstPrintFlags::PerLine | NYql::TAstPrintFlags::ShortQuote);
  29. return MakeString(str);
  30. }
  31. void RegisterDependencies() const override {
  32. DependsOn(Code_);
  33. }
  34. private:
  35. IComputationNode* Code_;
  36. bool AnnotatePosition_;
  37. const ui32 ExprCtxMutableIndex_;
  38. };
  39. class TSerializeCodeWrapper : public TMutableComputationNode<TSerializeCodeWrapper> {
  40. typedef TMutableComputationNode<TSerializeCodeWrapper> TBaseComputation;
  41. public:
  42. TSerializeCodeWrapper(TComputationMutables& mutables, IComputationNode* code, ui32 exprCtxMutableIndex)
  43. : TBaseComputation(mutables)
  44. , Code_(code)
  45. , ExprCtxMutableIndex_(exprCtxMutableIndex)
  46. {}
  47. NUdf::TUnboxedValue DoCalculate(TComputationContext& ctx) const {
  48. auto codeValue = Code_->GetValue(ctx);
  49. auto code = GetYqlCode(codeValue);
  50. NYql::TExprContext& exprCtx = GetExprContext(ctx, ExprCtxMutableIndex_);
  51. auto str = NYql::SerializeGraph(*code, exprCtx,
  52. NYql::TSerializedExprGraphComponents::Graph |
  53. NYql::TSerializedExprGraphComponents::Positions);
  54. return MakeString(str);
  55. }
  56. void RegisterDependencies() const override {
  57. DependsOn(Code_);
  58. }
  59. private:
  60. IComputationNode* Code_;
  61. const ui32 ExprCtxMutableIndex_;
  62. };
  63. template <bool AnnotatePosition>
  64. IComputationNode* WrapFormatCode(TCallable& callable, const TComputationNodeFactoryContext& ctx, ui32 exprCtxMutableIndex) {
  65. MKQL_ENSURE(callable.GetInputsCount() == 1, "Expected 1 arg");
  66. auto code = LocateNode(ctx.NodeLocator, callable, 0);
  67. return new TFormatCodeWrapper(ctx.Mutables, code, AnnotatePosition, exprCtxMutableIndex);
  68. }
  69. template IComputationNode* WrapFormatCode<false>
  70. (TCallable& callable, const TComputationNodeFactoryContext& ctx, ui32 exprCtxMutableIndex);
  71. template IComputationNode* WrapFormatCode<true>
  72. (TCallable& callable, const TComputationNodeFactoryContext& ctx, ui32 exprCtxMutableIndex);
  73. IComputationNode* WrapSerializeCode(TCallable& callable, const TComputationNodeFactoryContext& ctx, ui32 exprCtxMutableIndex) {
  74. MKQL_ENSURE(callable.GetInputsCount() == 1, "Expected 1 arg");
  75. auto code = LocateNode(ctx.NodeLocator, callable, 0);
  76. return new TSerializeCodeWrapper(ctx.Mutables, code, exprCtxMutableIndex);
  77. }
  78. }
  79. }