trace_processor: Update ExtendWithColumn to use SharedPtr

Change-Id: I8b4b3ce5c9eec0500ee986fdabe25e52b491960e
diff --git a/Android.bp b/Android.bp
index 12c79b2..3e3a51a 100644
--- a/Android.bp
+++ b/Android.bp
@@ -6437,6 +6437,7 @@
   name: "perfetto_src_trace_processor_db_unittests",
   srcs: [
     "src/trace_processor/db/compare_unittest.cc",
+    "src/trace_processor/db/table_unittest.cc",
   ],
 }
 
diff --git a/src/trace_processor/db/BUILD.gn b/src/trace_processor/db/BUILD.gn
index 5e15f66..f1a959f 100644
--- a/src/trace_processor/db/BUILD.gn
+++ b/src/trace_processor/db/BUILD.gn
@@ -35,10 +35,15 @@
 
 perfetto_unittest_source_set("unittests") {
   testonly = true
-  sources = [ "compare_unittest.cc" ]
+  sources = [
+    "compare_unittest.cc",
+    "table_unittest.cc",
+  ]
   deps = [
     ":lib",
     "../../../gn:default_deps",
     "../../../gn:gtest_and_gmock",
+    "../../base:base",
+    "../tables:tables",
   ]
 }
diff --git a/src/trace_processor/db/column.cc b/src/trace_processor/db/column.cc
index 80dcf5b..94a5c19 100644
--- a/src/trace_processor/db/column.cc
+++ b/src/trace_processor/db/column.cc
@@ -33,7 +33,7 @@
              col_idx,
              row_map_idx,
              column.sparse_vector_,
-             nullptr) {}
+             column.owned_sparse_vector_) {}
 
 Column::Column(const char* name,
                ColumnType type,
@@ -42,8 +42,8 @@
                uint32_t col_idx_in_table,
                uint32_t row_map_idx,
                SparseVectorBase* sparse_vector,
-               std::unique_ptr<SparseVectorBase> owned_sparse_vector)
-    : owned_sparse_vector_(std::move(owned_sparse_vector)),
+               std::shared_ptr<SparseVectorBase> owned_sparse_vector)
+    : owned_sparse_vector_(owned_sparse_vector),
       type_(type),
       sparse_vector_(sparse_vector),
       name_(name),
diff --git a/src/trace_processor/db/column.h b/src/trace_processor/db/column.h
index 62a48b5..24ff8f5 100644
--- a/src/trace_processor/db/column.h
+++ b/src/trace_processor/db/column.h
@@ -425,7 +425,7 @@
          uint32_t col_idx_in_table,
          uint32_t row_map_idx,
          SparseVectorBase* sparse_vector,
-         std::unique_ptr<SparseVectorBase> owned_sparse_vector);
+         std::shared_ptr<SparseVectorBase> owned_sparse_vector);
 
   Column(const Column&) = delete;
   Column& operator=(const Column&) = delete;
@@ -579,7 +579,7 @@
   // Only filled for columns which own the data inside them. Generally this is
   // only true for columns which are dynamically generated at runtime.
   // Keep this before |sparse_vector_|.
-  std::unique_ptr<SparseVectorBase> owned_sparse_vector_;
+  std::shared_ptr<SparseVectorBase> owned_sparse_vector_;
 
   // type_ is used to cast sparse_vector_ to the correct type.
   ColumnType type_ = ColumnType::kInt64;
diff --git a/src/trace_processor/db/table_unittest.cc b/src/trace_processor/db/table_unittest.cc
new file mode 100644
index 0000000..21e0c80
--- /dev/null
+++ b/src/trace_processor/db/table_unittest.cc
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "src/trace_processor/db/table.h"
+#include "perfetto/ext/base/optional.h"
+#include "src/trace_processor/db/typed_column.h"
+#include "src/trace_processor/tables/macros.h"
+
+#include "test/gtest_and_gmock.h"
+
+namespace perfetto {
+namespace trace_processor {
+namespace {
+
+constexpr uint32_t kColumnCount = 1024;
+
+std::unique_ptr<SparseVector<int64_t>> Column() {
+  auto c = std::unique_ptr<SparseVector<int64_t>>(new SparseVector<int64_t>());
+  for (int64_t i = 0; i < kColumnCount; ++i)
+    c->Append(i);
+  return c;
+}
+
+uint32_t Flags() {
+  return TypedColumn<int64_t>::default_flags();
+}
+
+#define PERFETTO_TP_TEST_EVENT_TABLE_DEF(NAME, PARENT, C) \
+  NAME(TestEventTable, "event")                           \
+  PARENT(PERFETTO_TP_ROOT_TABLE_PARENT_DEF, C)            \
+  C(int64_t, ts, Column::Flag::kSorted)                   \
+  C(int64_t, arg_set_id)
+PERFETTO_TP_TABLE(PERFETTO_TP_TEST_EVENT_TABLE_DEF);
+
+TestEventTable::~TestEventTable() = default;
+
+TEST(TableTest, ExtendingTableTwice) {
+  StringPool pool;
+  TestEventTable table{&pool, nullptr};
+
+  for (uint32_t i = 0; i < kColumnCount; ++i)
+    table.Insert(TestEventTable::Row(i));
+
+  Table filtered_table;
+  {
+    filtered_table = table.ExtendWithColumn("a", Column(), Flags())
+                         .ExtendWithColumn("b", Column(), Flags())
+                         .Filter({});
+  }
+  ASSERT_TRUE(filtered_table.GetColumnByName("a")->Max().has_value());
+  ASSERT_TRUE(filtered_table.GetColumnByName("b")->Max().has_value());
+}
+
+}  // namespace
+}  // namespace trace_processor
+}  // namespace perfetto