aboutsummaryrefslogtreecommitdiff
path: root/src/trace_processor/sqlite/sqlite_table.cc
blob: 2ad925c878281aa982d9f8c3c0af9e96f0ea5ffc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
 * Copyright (C) 2018 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/sqlite/sqlite_table.h"

#include <string.h>
#include <algorithm>
#include <cinttypes>
#include <map>
#include <memory>

#include "perfetto/base/logging.h"
#include "perfetto/base/status.h"
#include "perfetto/ext/base/status_or.h"
#include "perfetto/ext/base/string_view.h"
#include "sqlite3.h"
#include "src/trace_processor/sqlite/sqlite_engine.h"
#include "src/trace_processor/tp_metatrace.h"
#include "src/trace_processor/util/status_macros.h"

namespace perfetto {
namespace trace_processor {

namespace {

std::string TypeToSqlString(SqlValue::Type type) {
  switch (type) {
    case SqlValue::Type::kString:
      return "TEXT";
    case SqlValue::Type::kLong:
      return "BIGINT";
    case SqlValue::Type::kDouble:
      return "DOUBLE";
    case SqlValue::Type::kBytes:
      return "BLOB";
    case SqlValue::Type::kNull:
      PERFETTO_FATAL("Cannot map unknown column type");
  }
  PERFETTO_FATAL("Not reached");  // For gcc
}

std::string OpToDebugString(int op) {
  switch (op) {
    case SQLITE_INDEX_CONSTRAINT_EQ:
      return "=";
    case SQLITE_INDEX_CONSTRAINT_NE:
      return "!=";
    case SQLITE_INDEX_CONSTRAINT_GE:
      return ">=";
    case SQLITE_INDEX_CONSTRAINT_GT:
      return ">";
    case SQLITE_INDEX_CONSTRAINT_LE:
      return "<=";
    case SQLITE_INDEX_CONSTRAINT_LT:
      return "<";
    case SQLITE_INDEX_CONSTRAINT_LIKE:
      return "like";
    case SQLITE_INDEX_CONSTRAINT_ISNULL:
      return "is null";
    case SQLITE_INDEX_CONSTRAINT_ISNOTNULL:
      return "is not null";
    case SQLITE_INDEX_CONSTRAINT_GLOB:
      return "glob";
    case SQLITE_INDEX_CONSTRAINT_LIMIT:
      return "limit";
    case SQLITE_INDEX_CONSTRAINT_OFFSET:
      return "offset";
    case SqliteTable::CustomFilterOpcode::kSourceGeqOpCode:
      return "source_geq";
    default:
      PERFETTO_FATAL("Operator to string conversion not impemented for %d", op);
  }
}

void ConstraintsToString(const QueryConstraints& qc,
                         const SqliteTable::Schema& schema,
                         std::string& out) {
  bool is_first = true;
  for (const auto& cs : qc.constraints()) {
    if (!is_first) {
      out.append(",");
    }
    out.append(schema.columns()[static_cast<size_t>(cs.column)].name());
    out.append(" ");
    out.append(OpToDebugString(cs.op));
    is_first = false;
  }
}

void OrderByToString(const QueryConstraints& qc,
                     const SqliteTable::Schema& schema,
                     std::string& out) {
  bool is_first = true;
  for (const auto& ob : qc.order_by()) {
    if (!is_first) {
      out.append(",");
    }
    out.append(schema.columns()[static_cast<size_t>(ob.iColumn)].name());
    out.append(" ");
    out.append(std::to_string(ob.desc));
    is_first = false;
  }
}

std::string QcDebugStr(const QueryConstraints& qc,
                       const SqliteTable::Schema& schema) {
  std::string str_result;
  str_result.reserve(512);

  str_result.append("C");
  str_result.append(std::to_string(qc.constraints().size()));
  str_result.append(",");
  ConstraintsToString(qc, schema, str_result);
  str_result.append(";");

  str_result.append("O");
  str_result.append(std::to_string(qc.order_by().size()));
  str_result.append(",");
  OrderByToString(qc, schema, str_result);
  str_result.append(";");

  str_result.append("U");
  str_result.append(std::to_string(qc.cols_used()));

  return str_result;
}

void WriteQueryConstraintsToMetatrace(metatrace::Record* r,
                                      const QueryConstraints& qc,
                                      const SqliteTable::Schema& schema) {
  r->AddArg("constraint_count", std::to_string(qc.constraints().size()));
  std::string constraints;
  ConstraintsToString(qc, schema, constraints);
  r->AddArg("constraints", constraints);
  r->AddArg("order_by_count", std::to_string(qc.order_by().size()));
  std::string order_by;
  OrderByToString(qc, schema, order_by);
  r->AddArg("order_by", order_by);
  r->AddArg("columns_used", std::to_string(qc.cols_used()));
}

}  // namespace

// static
bool SqliteTable::debug = false;

SqliteTable::SqliteTable() = default;
SqliteTable::~SqliteTable() = default;

base::Status SqliteTable::ModifyConstraints(QueryConstraints*) {
  return base::OkStatus();
}

int SqliteTable::FindFunction(const char*, FindFunctionFn*, void**) {
  return 0;
}

base::Status SqliteTable::Update(int, sqlite3_value**, sqlite3_int64*) {
  return base::ErrStatus("Updating not supported");
}

bool SqliteTable::ReadConstraints(int idxNum, const char* idxStr, int argc) {
  bool cache_hit = true;
  if (idxNum != qc_hash_) {
    qc_cache_ = QueryConstraints::FromString(idxStr);
    qc_hash_ = idxNum;
    cache_hit = false;
  }

  PERFETTO_TP_TRACE(metatrace::Category::QUERY, "SQLITE_TABLE_READ_CONSTRAINTS",
                    [&](metatrace::Record* r) {
                      r->AddArg("cache_hit", std::to_string(cache_hit));
                      r->AddArg("name", name_);
                      WriteQueryConstraintsToMetatrace(r, qc_cache_, schema_);
                      r->AddArg("raw_constraints", idxStr);
                      r->AddArg("argc", std::to_string(argc));
                    });

  // Logging this every ReadConstraints just leads to log spam on joins making
  // it unusable. Instead, only print this out when we miss the cache (which
  // happens precisely when the constraint set from SQLite changes.)
  if (SqliteTable::debug && !cache_hit) {
    PERFETTO_LOG("[%s::ParseConstraints] constraints=%s argc=%d", name_.c_str(),
                 QcDebugStr(qc_cache_, schema_).c_str(), argc);
  }
  return cache_hit;
}

////////////////////////////////////////////////////////////////////////////////
// SqliteTable::BaseCursor implementation
////////////////////////////////////////////////////////////////////////////////

SqliteTable::BaseCursor::BaseCursor(SqliteTable* table) : table_(table) {
  // This is required to prevent us from leaving this field uninitialised if
  // we ever move construct the Cursor.
  pVtab = table;
}
SqliteTable::BaseCursor::~BaseCursor() = default;

////////////////////////////////////////////////////////////////////////////////
// SqliteTable::Column implementation
////////////////////////////////////////////////////////////////////////////////

SqliteTable::Column::Column(size_t index,
                            std::string name,
                            SqlValue::Type type,
                            bool hidden)
    : index_(index), name_(name), type_(type), hidden_(hidden) {}

////////////////////////////////////////////////////////////////////////////////
// SqliteTable::Schema implementation
////////////////////////////////////////////////////////////////////////////////

SqliteTable::Schema::Schema() = default;

SqliteTable::Schema::Schema(std::vector<Column> columns,
                            std::vector<size_t> primary_keys)
    : columns_(std::move(columns)), primary_keys_(std::move(primary_keys)) {
  for (size_t i = 0; i < columns_.size(); i++) {
    PERFETTO_CHECK(columns_[i].index() == i);
  }
  for (auto key : primary_keys_) {
    PERFETTO_CHECK(key < columns_.size());
  }
}

SqliteTable::Schema::Schema(const Schema&) = default;
SqliteTable::Schema& SqliteTable::Schema::operator=(const Schema&) = default;

std::string SqliteTable::Schema::ToCreateTableStmt() const {
  std::string stmt = "CREATE TABLE x(";
  for (size_t i = 0; i < columns_.size(); ++i) {
    const Column& col = columns_[i];
    stmt += " " + col.name();

    if (col.type() != SqlValue::Type::kNull) {
      stmt += " " + TypeToSqlString(col.type());
    } else if (std::find(primary_keys_.begin(), primary_keys_.end(), i) !=
               primary_keys_.end()) {
      PERFETTO_FATAL("Unknown type for primary key column %s",
                     col.name().c_str());
    }
    if (col.hidden()) {
      stmt += " HIDDEN";
    }
    stmt += ",";
  }
  stmt += " PRIMARY KEY(";
  for (size_t i = 0; i < primary_keys_.size(); i++) {
    if (i != 0)
      stmt += ", ";
    stmt += columns_[primary_keys_[i]].name();
  }
  stmt += ")) WITHOUT ROWID;";
  return stmt;
}

////////////////////////////////////////////////////////////////////////////////
// TypedSqliteTableBase implementation
////////////////////////////////////////////////////////////////////////////////

TypedSqliteTableBase::~TypedSqliteTableBase() = default;

base::Status TypedSqliteTableBase::DeclareAndAssignVtab(
    std::unique_ptr<SqliteTable> table,
    sqlite3_vtab** tab) {
  auto create_stmt = table->schema().ToCreateTableStmt();
  PERFETTO_DLOG("Create table statement: %s", create_stmt.c_str());
  RETURN_IF_ERROR(table->engine_->DeclareVirtualTable(create_stmt));
  *tab = table.release();
  return base::OkStatus();
}

int TypedSqliteTableBase::xDestroy(sqlite3_vtab* t) {
  delete static_cast<SqliteTable*>(t);
  return SQLITE_OK;
}

int TypedSqliteTableBase::xDestroyFatal(sqlite3_vtab*) {
  PERFETTO_FATAL("xDestroy should not be called");
}

int TypedSqliteTableBase::xConnectRestoreTable(sqlite3*,
                                               void* arg,
                                               int,
                                               const char* const* argv,
                                               sqlite3_vtab** tab,
                                               char** pzErr) {
  auto* xArg = static_cast<BaseModuleArg*>(arg);

  // SQLite guarantees that argv[2] contains the name of the table.
  std::string table_name = argv[2];
  base::StatusOr<std::unique_ptr<SqliteTable>> table =
      xArg->engine->RestoreSqliteTable(table_name);
  if (!table.status().ok()) {
    *pzErr = sqlite3_mprintf("%s", table.status().c_message());
    return SQLITE_ERROR;
  }
  base::Status status = DeclareAndAssignVtab(std::move(table.value()), tab);
  if (!status.ok()) {
    *pzErr = sqlite3_mprintf("%s", status.c_message());
    return SQLITE_ERROR;
  }
  return SQLITE_OK;
}

int TypedSqliteTableBase::xDisconnectSaveTable(sqlite3_vtab* t) {
  auto* table = static_cast<TypedSqliteTableBase*>(t);
  base::Status status = table->engine_->SaveSqliteTable(
      table->name(), std::unique_ptr<SqliteTable>(table));
  return table->SetStatusAndReturn(status);
}

base::Status TypedSqliteTableBase::InitInternal(SqliteEngine* engine,
                                                int argc,
                                                const char* const* argv) {
  // Set the engine to allow saving into it later.
  engine_ = engine;

  // SQLite guarantees that argv[0] will be the "module" name: this is the
  // same as |table_name| passed to the Register function.
  module_name_ = argv[0];

  // SQLite guarantees that argv[2] contains the name of the table: for
  // non-arg taking tables, this will be the same as |table_name| but for
  // arg-taking tables, this will be the table name as defined by the
  // user in the CREATE VIRTUAL TABLE call.
  name_ = argv[2];

  Schema schema;
  RETURN_IF_ERROR(Init(argc, argv, &schema));
  schema_ = std::move(schema);
  return base::OkStatus();
}

int TypedSqliteTableBase::xOpen(sqlite3_vtab* t,
                                sqlite3_vtab_cursor** ppCursor) {
  auto* table = static_cast<TypedSqliteTableBase*>(t);
  *ppCursor =
      static_cast<sqlite3_vtab_cursor*>(table->CreateCursor().release());
  return SQLITE_OK;
}

int TypedSqliteTableBase::xBestIndex(sqlite3_vtab* t, sqlite3_index_info* idx) {
  auto* table = static_cast<TypedSqliteTableBase*>(t);

  QueryConstraints qc(idx->colUsed);

  for (int i = 0; i < idx->nConstraint; i++) {
    const auto& cs = idx->aConstraint[i];
    if (!cs.usable)
      continue;
    qc.AddConstraint(cs.iColumn, cs.op, i);
  }

  for (int i = 0; i < idx->nOrderBy; i++) {
    int column = idx->aOrderBy[i].iColumn;
    bool desc = idx->aOrderBy[i].desc;
    qc.AddOrderBy(column, desc);
  }

  int ret = table->SetStatusAndReturn(table->ModifyConstraints(&qc));
  if (ret != SQLITE_OK)
    return ret;

  BestIndexInfo info;
  info.estimated_cost = idx->estimatedCost;
  info.estimated_rows = idx->estimatedRows;
  info.sqlite_omit_constraint.resize(qc.constraints().size());

  ret = table->BestIndex(qc, &info);

  if (ret != SQLITE_OK)
    return ret;

  idx->orderByConsumed = qc.order_by().empty() || info.sqlite_omit_order_by;
  idx->estimatedCost = info.estimated_cost;
  idx->estimatedRows = info.estimated_rows;

  // First pass: mark all constraints as omitted to ensure that any pruned
  // constraints are not checked for by SQLite.
  for (int i = 0; i < idx->nConstraint; ++i) {
    auto& u = idx->aConstraintUsage[i];
    u.omit = true;
  }

  // Second pass: actually set the correct omit and index values for all
  // retained constraints.
  for (uint32_t i = 0; i < qc.constraints().size(); ++i) {
    auto& u = idx->aConstraintUsage[qc.constraints()[i].a_constraint_idx];
    u.omit = info.sqlite_omit_constraint[i];
    u.argvIndex = static_cast<int>(i) + 1;
  }

  PERFETTO_TP_TRACE(
      metatrace::Category::QUERY, "SQLITE_TABLE_BEST_INDEX",
      [&](metatrace::Record* r) {
        r->AddArg("name", table->name());
        WriteQueryConstraintsToMetatrace(r, qc, table->schema());
        r->AddArg("order_by_consumed", std::to_string(idx->orderByConsumed));
        r->AddArg("estimated_cost", std::to_string(idx->estimatedCost));
        r->AddArg("estimated_rows",
                  std::to_string(static_cast<int64_t>(idx->estimatedRows)));
      });

  auto out_qc_str = qc.ToNewSqlite3String();
  if (SqliteTable::debug) {
    PERFETTO_LOG(
        "[%s::BestIndex] constraints=%s orderByConsumed=%d estimatedCost=%f "
        "estimatedRows=%" PRId64,
        table->name().c_str(), QcDebugStr(qc, table->schema()).c_str(),
        idx->orderByConsumed, idx->estimatedCost,
        static_cast<int64_t>(idx->estimatedRows));
  }

  idx->idxStr = out_qc_str.release();
  idx->needToFreeIdxStr = true;
  idx->idxNum = ++table->best_index_num_;

  return SQLITE_OK;
}

}  // namespace trace_processor
}  // namespace perfetto