utils.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #pragma once
  2. #include <yql/essentials/minikql/mkql_alloc.h>
  3. #include <yql/essentials/minikql/computation/mkql_block_item.h>
  4. #include <yql/essentials/parser/pg_catalog/catalog.h>
  5. namespace NYql {
  6. inline NKikimr::NUdf::TUnboxedValuePod ScalarDatumToPod(Datum datum) {
  7. return NKikimr::NUdf::TUnboxedValuePod((ui64)datum);
  8. }
  9. inline Datum ScalarDatumFromPod(const NKikimr::NUdf::TUnboxedValuePod& value) {
  10. return (Datum)value.Get<ui64>();
  11. }
  12. inline Datum ScalarDatumFromItem(const NKikimr::NUdf::TBlockItem& value) {
  13. return (Datum)value.As<ui64>();
  14. }
  15. inline NKikimr::NUdf::TUnboxedValuePod PointerDatumToPod(Datum datum) {
  16. auto header = ((NKikimr::NMiniKQL::TMkqlPAllocHeader*)datum) - 1;
  17. // remove this block from list
  18. header->U.Entry.Unlink();
  19. NKikimr::NUdf::IBoxedValue* raw = &header->U.Boxed;
  20. new(raw) NKikimr::NMiniKQL::TBoxedValueWithFree();
  21. NKikimr::NUdf::IBoxedValuePtr ref(raw);
  22. return NKikimr::NUdf::TUnboxedValuePod(std::move(ref));
  23. }
  24. inline NKikimr::NUdf::TUnboxedValuePod OwnedPointerDatumToPod(Datum datum) {
  25. auto header = ((NKikimr::NMiniKQL::TMkqlPAllocHeader*)datum) - 1;
  26. NKikimr::NUdf::IBoxedValue* raw = &header->U.Boxed;
  27. NKikimr::NUdf::IBoxedValuePtr ref(raw);
  28. return NKikimr::NUdf::TUnboxedValuePod(std::move(ref));
  29. }
  30. class TVPtrHolder {
  31. public:
  32. TVPtrHolder() {
  33. new(Dummy) NKikimr::NMiniKQL::TBoxedValueWithFree();
  34. }
  35. static bool IsBoxedVPtr(Datum ptr) {
  36. auto header = ((NKikimr::NMiniKQL::TMkqlPAllocHeader*)ptr) - 1;
  37. return *(const uintptr_t*)&header->U.Boxed == *(const uintptr_t*)Instance.Dummy;
  38. }
  39. private:
  40. char Dummy[sizeof(NKikimr::NUdf::IBoxedValue)];
  41. static TVPtrHolder Instance;
  42. };
  43. inline NKikimr::NUdf::TUnboxedValuePod AnyDatumToPod(Datum datum, bool passByValue) {
  44. if (passByValue) {
  45. return ScalarDatumToPod(datum);
  46. }
  47. if (TVPtrHolder::IsBoxedVPtr(datum)) {
  48. // returned one of arguments
  49. return OwnedPointerDatumToPod(datum);
  50. }
  51. return PointerDatumToPod(datum);
  52. }
  53. inline Datum PointerDatumFromPod(const NKikimr::NUdf::TUnboxedValuePod& value) {
  54. return (Datum)(((const NKikimr::NMiniKQL::TMkqlPAllocHeader*)value.AsRawBoxed()) + 1);
  55. }
  56. inline Datum PointerDatumFromItem(const NKikimr::NUdf::TBlockItem& value) {
  57. return (Datum)(value.AsStringRef().Data() + sizeof(void*));
  58. }
  59. inline ui32 GetFullVarSize(const text* s) {
  60. return VARSIZE(s);
  61. }
  62. inline ui32 GetCleanVarSize(const text* s) {
  63. return VARSIZE(s) - VARHDRSZ;
  64. }
  65. inline const char* GetVarData(const text* s) {
  66. return VARDATA(s);
  67. }
  68. inline TStringBuf GetVarBuf(const text* s) {
  69. return TStringBuf(GetVarData(s), GetCleanVarSize(s));
  70. }
  71. inline char* GetMutableVarData(text* s) {
  72. return VARDATA(s);
  73. }
  74. inline void UpdateCleanVarSize(text* s, ui32 cleanSize) {
  75. SET_VARSIZE(s, cleanSize + VARHDRSZ);
  76. }
  77. inline char* MakeCStringNotFilled(size_t size) {
  78. char* ret = (char*)palloc(size + 1);
  79. ret[size] = '\0';
  80. return ret;
  81. }
  82. inline char* MakeCString(TStringBuf s) {
  83. char* ret = MakeCStringNotFilled(s.size());
  84. memcpy(ret, s.data(), s.size());
  85. return ret;
  86. }
  87. inline char* MakeFixedStringNotFilled(size_t size) {
  88. char* ret = (char*)palloc(size);
  89. memset(ret, 0, size);
  90. return ret;
  91. }
  92. inline char* MakeFixedString(TStringBuf s, size_t size) {
  93. auto ret = MakeFixedStringNotFilled(size);
  94. Y_ENSURE(s.size() <= size);
  95. memcpy(ret, s.data(), s.size());
  96. return ret;
  97. }
  98. inline text* MakeVarNotFilled(size_t size) {
  99. text* ret = (text*)palloc(size + VARHDRSZ);
  100. UpdateCleanVarSize(ret, size);
  101. return ret;
  102. }
  103. inline text* MakeVar(TStringBuf s) {
  104. text* ret = MakeVarNotFilled(s.size());
  105. memcpy(GetMutableVarData(ret), s.data(), s.size());
  106. return ret;
  107. }
  108. inline ui32 MakeTypeIOParam(const NPg::TTypeDesc& desc) {
  109. return desc.ElementTypeId ? desc.ElementTypeId : desc.TypeId;
  110. }
  111. void PrepareVariadicArraySlow(FunctionCallInfoBaseData& callInfo, const NPg::TProcDesc& desc);
  112. void FreeVariadicArray(FunctionCallInfoBaseData& callInfo, ui32 originalArgs);
  113. inline bool PrepareVariadicArray(FunctionCallInfoBaseData& callInfo, const NPg::TProcDesc& desc) {
  114. if (!desc.VariadicArgType || desc.VariadicArgType == desc.VariadicType) {
  115. return false;
  116. }
  117. PrepareVariadicArraySlow(callInfo, desc);
  118. return true;
  119. }
  120. bool GetPgFuncAddr(ui32 procOid, FmgrInfo& finfo);
  121. class TExtensionsRegistry {
  122. class TImpl;
  123. public:
  124. static TExtensionsRegistry& Instance();
  125. void InitThread();
  126. void CleanupThread();
  127. void Load(ui32 extensionIndex, const TString& name, const TString& path);
  128. PGFunction GetFuncAddr(ui32 extensionIndex, const TString& funcName);
  129. TExtensionsRegistry();
  130. private:
  131. std::unique_ptr<TImpl> Impl_;
  132. };
  133. void RebuildSysCache();
  134. }