mkql_contains.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "mkql_contains.h"
  2. #include <yql/essentials/minikql/computation/mkql_computation_node_codegen.h> // Y_IGNORE
  3. #include <yql/essentials/minikql/mkql_node_cast.h>
  4. namespace NKikimr {
  5. namespace NMiniKQL {
  6. namespace {
  7. class TContainsWrapper : public TMutableCodegeneratorNode<TContainsWrapper> {
  8. typedef TMutableCodegeneratorNode<TContainsWrapper> TBaseComputation;
  9. public:
  10. TContainsWrapper(TComputationMutables& mutables, IComputationNode* dict, IComputationNode* key)
  11. : TBaseComputation(mutables, EValueRepresentation::Embedded)
  12. , Dict(dict)
  13. , Key(key)
  14. {
  15. }
  16. NUdf::TUnboxedValuePod DoCalculate(TComputationContext& compCtx) const {
  17. return NUdf::TUnboxedValuePod(Dict->GetValue(compCtx).Contains(Key->GetValue(compCtx)));
  18. }
  19. #ifndef MKQL_DISABLE_CODEGEN
  20. Value* DoGenerateGetValue(const TCodegenContext& ctx, BasicBlock*& block) const {
  21. auto& context = ctx.Codegen.GetContext();
  22. const auto valueType = Type::getInt128Ty(context);
  23. const auto dict = GetNodeValue(Dict, ctx, block);
  24. const auto keyp = *Stateless || ctx.AlwaysInline ?
  25. new AllocaInst(valueType, 0U, "key", &ctx.Func->getEntryBlock().back()):
  26. new AllocaInst(valueType, 0U, "key", block);
  27. GetNodeValue(keyp, Key, ctx, block);
  28. const auto cont = CallBoxedValueVirtualMethod<NUdf::TBoxedValueAccessor::EMethod::Contains>(Type::getInt1Ty(context), dict, ctx.Codegen, block, keyp);
  29. ValueUnRef(Key->GetRepresentation(), keyp, ctx, block);
  30. if (Dict->IsTemporaryValue())
  31. CleanupBoxed(dict, ctx, block);
  32. return MakeBoolean(cont, context, block);
  33. }
  34. #endif
  35. private:
  36. void RegisterDependencies() const final {
  37. DependsOn(Dict);
  38. DependsOn(Key);
  39. }
  40. IComputationNode* const Dict;
  41. IComputationNode* const Key;
  42. };
  43. }
  44. IComputationNode* WrapContains(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
  45. MKQL_ENSURE(callable.GetInputsCount() == 2, "Expected 2 args");
  46. const auto dict = LocateNode(ctx.NodeLocator, callable, 0);
  47. const auto key = LocateNode(ctx.NodeLocator, callable, 1);
  48. return new TContainsWrapper(ctx.Mutables, dict, key);
  49. }
  50. }
  51. }