format.cpp 3.7 KB

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