#include "mkql_builtins_impl.h" // Y_IGNORE #include namespace NKikimr { namespace NMiniKQL { namespace { template struct TSubString { static NUdf::TUnboxedValuePod Execute(NUdf::TUnboxedValuePod string, NUdf::TUnboxedValuePod start, NUdf::TUnboxedValuePod count) { return SubString(string, StartOptional && !start ? std::numeric_limits::min() : start.Get(), CountOptional && !count ? std::numeric_limits::max() : count.Get() ); } #ifndef MKQL_DISABLE_CODEGEN static Value* Generate(Value* string, Value* st, Value* cn, const TCodegenContext& ctx, BasicBlock*& block) { auto& context = ctx.Codegen.GetContext(); const auto doFunc = ConstantInt::get(Type::getInt64Ty(context), GetMethodPtr(SubString)); const auto start = StartOptional ? SelectInst::Create( IsEmpty(st, block, context), ConstantInt::get(GetTypeFor(context), std::numeric_limits::min()), GetterFor(st, context, block), "start", block ): GetterFor(st, context, block); const auto count = CountOptional ? SelectInst::Create( IsEmpty(cn, block, context), ConstantInt::get(GetTypeFor(context), std::numeric_limits::max()), GetterFor(cn, context, block), "count", block ): GetterFor(cn, context, block); const auto funType = FunctionType::get(string->getType(), {string->getType(), start->getType(), count->getType()}, false); const auto funcPtr = CastInst::Create(Instruction::IntToPtr, doFunc, PointerType::getUnqual(funType), "func", block); const auto result = CallInst::Create(funType, funcPtr, {string, start, count}, "substring", block); return result; } #endif }; template void RegisterSubstringnOpt(IBuiltinFunctionRegistry& registry, const char* name) { RegisterFunctionImpl, TTernaryArgs, NUdf::TDataType, false, false, false>, TTernaryWrap>(registry, name); RegisterFunctionImpl, TTernaryArgs, NUdf::TDataType, true, false, false>, TTernaryWrap>(registry, name); RegisterFunctionImpl, TTernaryArgs, NUdf::TDataType, false, true, false>, TTernaryWrap>(registry, name); RegisterFunctionImpl, TTernaryArgs, NUdf::TDataType, true, true, false>, TTernaryWrap>(registry, name); RegisterFunctionImpl, TTernaryArgs, NUdf::TDataType, false, false, true>, TTernaryWrap>(registry, name); RegisterFunctionImpl, TTernaryArgs, NUdf::TDataType, true, false, true>, TTernaryWrap>(registry, name); RegisterFunctionImpl, TTernaryArgs, NUdf::TDataType, false, true, true>, TTernaryWrap>(registry, name); RegisterFunctionImpl, TTernaryArgs, NUdf::TDataType, true, true, true>, TTernaryWrap>(registry, name); } } void RegisterSubstring(IBuiltinFunctionRegistry& registry) { RegisterSubstringnOpt>(registry, "Substring"); RegisterSubstringnOpt>(registry, "Substring"); } } // namespace NMiniKQL } // namespace NKikimr