Browse Source

[YQL-17298] Fix data race on ICompare::TPtr (#1213)

Andrey Neporada 1 year ago
parent
commit
fa1ae5e61d
1 changed files with 21 additions and 10 deletions
  1. 21 10
      ydb/library/yql/minikql/comp_nodes/mkql_wide_top_sort.cpp

+ 21 - 10
ydb/library/yql/minikql/comp_nodes/mkql_wide_top_sort.cpp

@@ -20,20 +20,31 @@ struct TKeyInfo {
     bool IsOptional;
     NUdf::ICompare::TPtr Compare;
     TType* PresortType = nullptr;
-    std::optional<TGenericPresortEncoder> LeftPacker;
-    std::optional<TGenericPresortEncoder> RightPacker;
+};
+
+struct TRuntimeKeyInfo {
+    TRuntimeKeyInfo(const TKeyInfo& keyInfo)
+        : Slot(keyInfo.Slot)
+        , IsOptional(keyInfo.IsOptional)
+        , Compare(keyInfo.Compare.Get())
+    {
+        if (keyInfo.PresortType) {
+            LeftPacker = keyInfo.PresortType;
+            RightPacker = keyInfo.PresortType;
+        }
+    }
+
+    const NUdf::EDataSlot Slot;
+    const bool IsOptional;
+    const NUdf::ICompare* const Compare;
+    mutable std::optional<TGenericPresortEncoder> LeftPacker;
+    mutable std::optional<TGenericPresortEncoder> RightPacker;
 };
 
 struct TMyValueCompare {
     TMyValueCompare(const std::vector<TKeyInfo>& keys)
-        : Keys(keys)
+        : Keys(keys.cbegin(), keys.cend())
     {
-        for (auto& key : Keys) {
-            if (key.PresortType) {
-                key.LeftPacker.emplace(key.PresortType);
-                key.RightPacker.emplace(key.PresortType);
-            }
-        }
     }
 
     int operator()(const bool* directions, const NUdf::TUnboxedValuePod* left, const NUdf::TUnboxedValuePod* right) const {
@@ -64,7 +75,7 @@ struct TMyValueCompare {
         return 0;
     }
 
-    mutable std::vector<TKeyInfo> Keys;
+    const std::vector<TRuntimeKeyInfo> Keys;
 };
 
 using TComparePtr = int(*)(const bool*, const NUdf::TUnboxedValuePod*, const NUdf::TUnboxedValuePod*);