format.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "format.h"
  2. #include "protobuf_format.h"
  3. #include "errors.h"
  4. #include <google/protobuf/descriptor.h>
  5. namespace NYT {
  6. TTableSchema CreateTableSchema(
  7. const ::google::protobuf::Descriptor& messageDescriptor,
  8. bool keepFieldsWithoutExtension)
  9. {
  10. return NDetail::CreateTableSchemaImpl(messageDescriptor, keepFieldsWithoutExtension);
  11. }
  12. ////////////////////////////////////////////////////////////////////////////////
  13. TFormat::TFormat(const TNode& config)
  14. : Config(config)
  15. { }
  16. TFormat TFormat::Protobuf(
  17. const TVector<const ::google::protobuf::Descriptor*>& descriptors,
  18. bool withDescriptors)
  19. {
  20. if (withDescriptors) {
  21. return TFormat(NDetail::MakeProtoFormatConfigWithDescriptors(descriptors));
  22. } else {
  23. return TFormat(NDetail::MakeProtoFormatConfigWithTables(descriptors));
  24. }
  25. }
  26. TFormat TFormat::YsonText()
  27. {
  28. TNode config("yson");
  29. config.Attributes()("format", "text");
  30. return TFormat(config);
  31. }
  32. TFormat TFormat::YsonBinary()
  33. {
  34. TNode config("yson");
  35. config.Attributes()("format", "binary");
  36. return TFormat(config);
  37. }
  38. TFormat TFormat::YaMRLenval()
  39. {
  40. TNode config("yamr");
  41. config.Attributes()("lenval", true)("has_subkey", true);
  42. return TFormat(config);
  43. }
  44. TFormat TFormat::Json()
  45. {
  46. return TFormat(TNode("json"));
  47. }
  48. TFormat TFormat::Dsv()
  49. {
  50. return TFormat(TNode("dsv"));
  51. }
  52. bool TFormat::IsTextYson() const
  53. {
  54. if (!Config.IsString() || Config.AsString() != "yson") {
  55. return false;
  56. }
  57. if (!Config.HasAttributes()) {
  58. return false;
  59. }
  60. const auto& attributes = Config.GetAttributes();
  61. if (!attributes.HasKey("format") || attributes["format"] != TNode("text")) {
  62. return false;
  63. }
  64. return true;
  65. }
  66. bool TFormat::IsProtobuf() const
  67. {
  68. return Config.IsString() && Config.AsString() == "protobuf";
  69. }
  70. bool TFormat::IsYamredDsv() const
  71. {
  72. return Config.IsString() && Config.AsString() == "yamred_dsv";
  73. }
  74. static TString FormatName(const TFormat& format)
  75. {
  76. if (!format.Config.IsString()) {
  77. Y_ABORT_UNLESS(format.Config.IsUndefined());
  78. return "<undefined>";
  79. }
  80. return format.Config.AsString();
  81. }
  82. TYamredDsvAttributes TFormat::GetYamredDsvAttributes() const
  83. {
  84. if (!IsYamredDsv()) {
  85. ythrow TApiUsageError() << "Cannot get yamred_dsv attributes for " << FormatName(*this) << " format";
  86. }
  87. TYamredDsvAttributes attributes;
  88. const auto& nodeAttributes = Config.GetAttributes();
  89. {
  90. const auto& keyColumns = nodeAttributes["key_column_names"];
  91. if (!keyColumns.IsList()) {
  92. ythrow yexception() << "Ill-formed format: key_column_names is of non-list type: " << keyColumns.GetType();
  93. }
  94. for (auto& column : keyColumns.AsList()) {
  95. if (!column.IsString()) {
  96. ythrow yexception() << "Ill-formed format: key_column_names: " << column.GetType();
  97. }
  98. attributes.KeyColumnNames.push_back(column.AsString());
  99. }
  100. }
  101. if (nodeAttributes.HasKey("subkey_column_names")) {
  102. const auto& subkeyColumns = nodeAttributes["subkey_column_names"];
  103. if (!subkeyColumns.IsList()) {
  104. ythrow yexception() << "Ill-formed format: subkey_column_names is not a list: " << subkeyColumns.GetType();
  105. }
  106. for (const auto& column : subkeyColumns.AsList()) {
  107. if (!column.IsString()) {
  108. ythrow yexception() << "Ill-formed format: non-string inside subkey_key_column_names: " << column.GetType();
  109. }
  110. attributes.SubkeyColumnNames.push_back(column.AsString());
  111. }
  112. }
  113. return attributes;
  114. }
  115. ////////////////////////////////////////////////////////////////////////////////
  116. } // namespace NYT