mkql_builtins_invprestr.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "mkql_builtins_impl.h" // Y_IGNORE
  2. #include <yql/essentials/minikql/mkql_string_util.h>
  3. #include <yql/essentials/minikql/computation/presort.h>
  4. #include <util/thread/singleton.h>
  5. namespace NKikimr {
  6. namespace NMiniKQL {
  7. namespace {
  8. struct TStringDescEncoder : public TPresortEncoder {
  9. TStringDescEncoder() {
  10. AddType(NUdf::EDataSlot::String, false, true);
  11. }
  12. };
  13. NUdf::TUnboxedValuePod Presort(NUdf::TUnboxedValuePod arg) {
  14. auto encoder = FastTlsSingleton<TStringDescEncoder>();
  15. encoder->Start();
  16. encoder->Encode(arg);
  17. return MakeString(encoder->Finish());
  18. }
  19. NUdf::TUnboxedValuePod Inverse(NUdf::TUnboxedValuePod arg) {
  20. auto input = arg.AsStringRef();
  21. auto ret = MakeStringNotFilled(input.Size());
  22. auto readPtr = input.Data();
  23. auto writePtr = ret.AsStringRef().Data();
  24. for (ui32 i = 0; i < input.Size(); ++i) {
  25. writePtr[i] = readPtr[i] ^ 0xff;
  26. }
  27. return ret;
  28. }
  29. template <typename TInput, typename TOutput>
  30. struct TInversePresortString {
  31. static NUdf::TUnboxedValuePod Execute(const NUdf::TUnboxedValuePod& arg)
  32. {
  33. return Presort(arg);
  34. }
  35. #ifndef MKQL_DISABLE_CODEGEN
  36. static Value* Generate(Value* arg, const TCodegenContext& ctx, BasicBlock*& block)
  37. {
  38. return CallUnaryUnboxedValueFunction(&Presort, Type::getInt128Ty(ctx.Codegen.GetContext()), arg, ctx.Codegen, block);
  39. }
  40. #endif
  41. };
  42. template <typename TInput, typename TOutput>
  43. struct TInverseString {
  44. static NUdf::TUnboxedValuePod Execute(const NUdf::TUnboxedValuePod& arg)
  45. {
  46. return Inverse(arg);
  47. }
  48. #ifndef MKQL_DISABLE_CODEGEN
  49. static Value* Generate(Value* arg, const TCodegenContext& ctx, BasicBlock*& block)
  50. {
  51. return CallUnaryUnboxedValueFunction(&Inverse, Type::getInt128Ty(ctx.Codegen.GetContext()), arg, ctx.Codegen, block);
  52. }
  53. #endif
  54. };
  55. }
  56. void RegisterInversePresortString(IBuiltinFunctionRegistry& registry) {
  57. const auto name = "InversePresortString";
  58. RegisterFunction<NUdf::TDataType<char*>, NUdf::TDataType<char*>, TInversePresortString, TUnaryArgs>(registry, name);
  59. RegisterFunction<NUdf::TDataType<NUdf::TUtf8>, NUdf::TDataType<char*>, TInversePresortString, TUnaryArgs>(registry, name);
  60. }
  61. void RegisterInverseString(IBuiltinFunctionRegistry& registry) {
  62. const auto name = "InverseString";
  63. RegisterFunction<NUdf::TDataType<char*>, NUdf::TDataType<char*>, TInverseString, TUnaryArgs>(registry, name);
  64. }
  65. } // namespace NMiniKQL
  66. } // namespace NKikimr