format_hints.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "format_hints.h"
  2. #include <yt/cpp/mapreduce/common/helpers.h>
  3. #include <yt/cpp/mapreduce/interface/config.h>
  4. #include <yt/cpp/mapreduce/interface/operation.h>
  5. #include <util/string/builder.h>
  6. namespace NYT::NDetail {
  7. using ::ToString;
  8. ////////////////////////////////////////////////////////////////////////////////
  9. static void ApplyEnableTypeConversion(TFormat* format, const TFormatHints& formatHints)
  10. {
  11. if (formatHints.EnableAllToStringConversion_) {
  12. format->Config.Attributes()["enable_all_to_string_conversion"] = *formatHints.EnableAllToStringConversion_;
  13. }
  14. if (formatHints.EnableStringToAllConversion_) {
  15. format->Config.Attributes()["enable_string_to_all_conversion"] = *formatHints.EnableStringToAllConversion_;
  16. }
  17. if (formatHints.EnableIntegralTypeConversion_) {
  18. format->Config.Attributes()["enable_integral_type_conversion"] = *formatHints.EnableIntegralTypeConversion_;
  19. }
  20. if (formatHints.EnableIntegralToDoubleConversion_) {
  21. format->Config.Attributes()["enable_integral_to_double_conversion"] = *formatHints.EnableIntegralToDoubleConversion_;
  22. }
  23. if (formatHints.EnableTypeConversion_) {
  24. format->Config.Attributes()["enable_type_conversion"] = *formatHints.EnableTypeConversion_;
  25. }
  26. }
  27. template <>
  28. void ApplyFormatHints<TNode>(TFormat* format, const TMaybe<TFormatHints>& formatHints)
  29. {
  30. Y_VERIFY(format);
  31. if (!formatHints) {
  32. return;
  33. }
  34. ApplyEnableTypeConversion(format, *formatHints);
  35. if (formatHints->SkipNullValuesForTNode_) {
  36. Y_ENSURE_EX(
  37. format->Config.AsString() == "yson",
  38. TApiUsageError() << "SkipNullForTNode option must be used with yson format, actual format: " << format->Config.AsString());
  39. format->Config.Attributes()["skip_null_values"] = formatHints->SkipNullValuesForTNode_;
  40. }
  41. if (formatHints->ComplexTypeMode_) {
  42. Y_ENSURE_EX(
  43. format->Config.AsString() == "yson",
  44. TApiUsageError() << "ComplexTypeMode option must be used with yson format, actual format: "
  45. << format->Config.AsString());
  46. format->Config.Attributes()["complex_type_mode"] = ToString(*formatHints->ComplexTypeMode_);
  47. }
  48. }
  49. template <>
  50. void ApplyFormatHints<TYaMRRow>(TFormat* format, const TMaybe<TFormatHints>& formatHints)
  51. {
  52. Y_VERIFY(format);
  53. if (!formatHints) {
  54. return;
  55. }
  56. ythrow TApiUsageError() << "Yamr format currently has no supported format hints";
  57. }
  58. template <>
  59. void ApplyFormatHints<::google::protobuf::Message>(TFormat* format, const TMaybe<TFormatHints>& formatHints)
  60. {
  61. Y_VERIFY(format);
  62. if (!formatHints) {
  63. return;
  64. }
  65. ythrow TApiUsageError() << "Protobuf format currently has no supported format hints";
  66. }
  67. ////////////////////////////////////////////////////////////////////////////////
  68. } // namespace NYT::NDetail