yql_user_data.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #pragma once
  2. #include <library/cpp/enumbitset/enumbitset.h>
  3. #include <yql/essentials/core/file_storage/file_storage.h>
  4. #include <util/generic/hash.h>
  5. #include <util/generic/hash_set.h>
  6. #include <functional>
  7. namespace NYql {
  8. // -- user files --
  9. enum class EUserDataType {
  10. URL = 1, // indicates that provided user data represents URL which can be used to obtain table data
  11. PATH = 2, // indicates that provided user data represents file path which can be used to obtain table data
  12. RAW_INLINE_DATA = 3, // table data is inside the provided data block
  13. };
  14. enum class EUserDataBlockUsage {
  15. Begin,
  16. Path = Begin,
  17. Content,
  18. Udf,
  19. Library,
  20. PgExt,
  21. End,
  22. };
  23. typedef TEnumBitSet<EUserDataBlockUsage, static_cast<int>(EUserDataBlockUsage::Begin),
  24. static_cast<int>(EUserDataBlockUsage::End)> TUserDataBlockUsage;
  25. struct TUserDataBlock {
  26. EUserDataType Type = EUserDataType::PATH;
  27. TString UrlToken;
  28. TString Data;
  29. TUserDataBlockUsage Usage;
  30. TFileLinkPtr FrozenFile;
  31. // Prefix added to all UDF module names
  32. TString CustomUdfPrefix = {};
  33. THashMap<TString, TString> Options = {};
  34. };
  35. class TUserDataKey {
  36. public:
  37. enum class EDataType {
  38. FILE, UDF
  39. };
  40. static TUserDataKey File(const TString& alias) {
  41. return { EDataType::FILE, alias };
  42. }
  43. static TUserDataKey File(const TStringBuf& alias) {
  44. return { EDataType::FILE, TString(alias) };
  45. }
  46. static TUserDataKey Udf(const TString& alias) {
  47. return { EDataType::UDF, alias };
  48. }
  49. static TUserDataKey Udf(const TStringBuf& alias) {
  50. return { EDataType::UDF, TString(alias) };
  51. }
  52. inline bool IsFile() const { return Type_ == EDataType::FILE; }
  53. inline bool IsUdf() const { return Type_ == EDataType::UDF; }
  54. inline const TString& Alias() const { return Alias_; }
  55. inline EDataType Type() const { return Type_; }
  56. bool operator<(const TUserDataKey& other) const {
  57. return std::tie(Type_, Alias_) < std::tie(other.Type_, other.Alias_);
  58. }
  59. struct THash {
  60. size_t operator()(const TUserDataKey& key) {
  61. auto type = static_cast<size_t>(key.Type_);
  62. return CombineHashes(type, ComputeHash(key.Alias_));
  63. }
  64. };
  65. struct TEqualTo {
  66. bool operator()(const TUserDataKey& k1, const TUserDataKey& k2) const {
  67. return k1.Type_ == k2.Type_ && k1.Alias_ == k2.Alias_;
  68. }
  69. };
  70. private:
  71. inline TUserDataKey(EDataType type, TString alias)
  72. : Type_(type)
  73. , Alias_(std::move(alias))
  74. {
  75. }
  76. private:
  77. const EDataType Type_;
  78. const TString Alias_;
  79. };
  80. inline IOutputStream& operator<<(IOutputStream& os, const TUserDataKey& key) {
  81. os << "Type: " << key.Type() << ", alias: " << key.Alias();
  82. return os;
  83. }
  84. using TUserDataTable = THashMap<TUserDataKey, TUserDataBlock, TUserDataKey::THash, TUserDataKey::TEqualTo>;
  85. using TTokenResolver = std::function<TString(const TString&, const TString&)>;
  86. } // namespace NYql