mkql_builtins_substring.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "mkql_builtins_impl.h" // Y_IGNORE
  2. #include <yql/essentials/minikql/mkql_string_util.h>
  3. namespace NKikimr {
  4. namespace NMiniKQL {
  5. namespace {
  6. template <bool StartOptional, bool CountOptional>
  7. struct TSubString {
  8. static NUdf::TUnboxedValuePod Execute(NUdf::TUnboxedValuePod string, NUdf::TUnboxedValuePod start, NUdf::TUnboxedValuePod count)
  9. {
  10. return SubString(string,
  11. StartOptional && !start ? std::numeric_limits<ui32>::min() : start.Get<ui32>(),
  12. CountOptional && !count ? std::numeric_limits<ui32>::max() : count.Get<ui32>()
  13. );
  14. }
  15. #ifndef MKQL_DISABLE_CODEGEN
  16. static Value* Generate(Value* string, Value* st, Value* cn, const TCodegenContext& ctx, BasicBlock*& block)
  17. {
  18. auto& context = ctx.Codegen.GetContext();
  19. const auto doFunc = ConstantInt::get(Type::getInt64Ty(context), GetMethodPtr(SubString));
  20. const auto start = StartOptional ?
  21. SelectInst::Create(
  22. IsEmpty(st, block, context),
  23. ConstantInt::get(GetTypeFor<ui32>(context), std::numeric_limits<ui32>::min()),
  24. GetterFor<ui32>(st, context, block), "start", block
  25. ):
  26. GetterFor<ui32>(st, context, block);
  27. const auto count = CountOptional ?
  28. SelectInst::Create(
  29. IsEmpty(cn, block, context),
  30. ConstantInt::get(GetTypeFor<ui32>(context), std::numeric_limits<ui32>::max()),
  31. GetterFor<ui32>(cn, context, block), "count", block
  32. ):
  33. GetterFor<ui32>(cn, context, block);
  34. const auto funType = FunctionType::get(string->getType(), {string->getType(), start->getType(), count->getType()}, false);
  35. const auto funcPtr = CastInst::Create(Instruction::IntToPtr, doFunc, PointerType::getUnqual(funType), "func", block);
  36. const auto result = CallInst::Create(funType, funcPtr, {string, start, count}, "substring", block);
  37. return result;
  38. }
  39. #endif
  40. };
  41. template <typename TInput>
  42. void RegisterSubstringnOpt(IBuiltinFunctionRegistry& registry, const char* name) {
  43. RegisterFunctionImpl<TSubString<false, false>, TTernaryArgs<TInput, TInput, NUdf::TDataType<ui32>, NUdf::TDataType<ui32>, false, false, false>, TTernaryWrap<false>>(registry, name);
  44. RegisterFunctionImpl<TSubString<false, false>, TTernaryArgs<TInput, TInput, NUdf::TDataType<ui32>, NUdf::TDataType<ui32>, true, false, false>, TTernaryWrap<true>>(registry, name);
  45. RegisterFunctionImpl<TSubString<true, false>, TTernaryArgs<TInput, TInput, NUdf::TDataType<ui32>, NUdf::TDataType<ui32>, false, true, false>, TTernaryWrap<false>>(registry, name);
  46. RegisterFunctionImpl<TSubString<true, false>, TTernaryArgs<TInput, TInput, NUdf::TDataType<ui32>, NUdf::TDataType<ui32>, true, true, false>, TTernaryWrap<true>>(registry, name);
  47. RegisterFunctionImpl<TSubString<false, true>, TTernaryArgs<TInput, TInput, NUdf::TDataType<ui32>, NUdf::TDataType<ui32>, false, false, true>, TTernaryWrap<false>>(registry, name);
  48. RegisterFunctionImpl<TSubString<false, true>, TTernaryArgs<TInput, TInput, NUdf::TDataType<ui32>, NUdf::TDataType<ui32>, true, false, true>, TTernaryWrap<true>>(registry, name);
  49. RegisterFunctionImpl<TSubString<true, true>, TTernaryArgs<TInput, TInput, NUdf::TDataType<ui32>, NUdf::TDataType<ui32>, false, true, true>, TTernaryWrap<false>>(registry, name);
  50. RegisterFunctionImpl<TSubString<true, true>, TTernaryArgs<TInput, TInput, NUdf::TDataType<ui32>, NUdf::TDataType<ui32>, true, true, true>, TTernaryWrap<true>>(registry, name);
  51. }
  52. }
  53. void RegisterSubstring(IBuiltinFunctionRegistry& registry) {
  54. RegisterSubstringnOpt<NUdf::TDataType<char*>>(registry, "Substring");
  55. RegisterSubstringnOpt<NUdf::TDataType<NUdf::TUtf8>>(registry, "Substring");
  56. }
  57. } // namespace NMiniKQL
  58. } // namespace NKikimr