YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/yql/cql/ql/statement.cc
Line
Count
Source (jump to first uncovered line)
1
//--------------------------------------------------------------------------------------------------
2
// Copyright (c) YugaByte, Inc.
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5
// in compliance with the License.  You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software distributed under the License
10
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11
// or implied.  See the License for the specific language governing permissions and limitations
12
// under the License.
13
//
14
//--------------------------------------------------------------------------------------------------
15
16
#include "yb/yql/cql/ql/statement.h"
17
18
#include "yb/util/result.h"
19
20
#include "yb/yql/cql/ql/ptree/list_node.h"
21
#include "yb/yql/cql/ql/ptree/pt_dml.h"
22
#include "yb/yql/cql/ql/ql_processor.h"
23
#include "yb/yql/cql/ql/util/errcodes.h"
24
25
namespace yb {
26
namespace ql {
27
28
using std::list;
29
using std::shared_ptr;
30
using std::string;
31
using std::unique_ptr;
32
33
Statement::Statement(const string& keyspace, const string& text)
34
6.32k
    : keyspace_(keyspace), text_(text) {
35
6.32k
}
36
37
412
Statement::~Statement() {
38
412
}
39
40
Status Statement::Prepare(QLProcessor *processor, const MemTrackerPtr& mem_tracker,
41
10.9k
                          const bool internal, PreparedResult::UniPtr *result) {
42
  // Prepare the statement (parse and semantically analysis). Do so within an exclusive lock.
43
10.9k
  if (!prepared_.load(std::memory_order_acquire)) {
44
3.32k
    std::lock_guard<std::mutex> guard(parse_tree_mutex_);
45
46
3.32k
    if (parse_tree_ == nullptr) {
47
3.25k
      ParseTree::UniPtr parse_tree;
48
3.25k
      RETURN_NOT_OK(processor->Prepare(text_, &parse_tree, false /* reparsed */, mem_tracker,
49
3.25k
                                       internal));
50
3.08k
      parse_tree_ = std::move(parse_tree);
51
3.08k
      prepared_.store(true, std::memory_order_release);
52
3.08k
    }
53
3.32k
  }
54
55
  // Return prepared result if requested and the statement is a DML. Do not need a lock here
56
  // because we have verified that the parse tree is either present already or we have successfully
57
  // prepared the statement above. The parse tree is guaranteed read-only afterwards.
58
10.7k
  if (result != nullptr && parse_tree_->root() != nullptr) {
59
4.86k
    const TreeNode& stmt = *parse_tree_->root();
60
4.86k
    switch (stmt.opcode()) {
61
376
      case TreeNodeOpcode::kPTSelectStmt: FALLTHROUGH_INTENDED;
62
4.18k
      case TreeNodeOpcode::kPTInsertStmt: FALLTHROUGH_INTENDED;
63
4.42k
      case TreeNodeOpcode::kPTUpdateStmt: FALLTHROUGH_INTENDED;
64
4.42k
      case TreeNodeOpcode::kPTExplainStmt: FALLTHROUGH_INTENDED;
65
4.50k
      case TreeNodeOpcode::kPTDeleteStmt:
66
4.50k
        result->reset(new PreparedResult(static_cast<const PTDmlStmt&>(stmt)));
67
4.50k
        break;
68
15
      case TreeNodeOpcode::kPTListNode:
69
15
        result->reset(new PreparedResult(static_cast<const PTListNode&>(stmt)));
70
15
        break;
71
337
      default:
72
337
        break;
73
10.7k
    }
74
10.7k
  }
75
76
10.7k
  return Status::OK();
77
10.7k
}
78
79
4.77M
Result<const ParseTree&> Statement::GetParseTree() const {
80
  // Validate that the statement has been prepared and is not stale.
81
4.77M
  if (!prepared_.load(std::memory_order_acquire)) {
82
0
    return ErrorStatus(ErrorCode::UNPREPARED_STATEMENT);
83
0
  }
84
618
  DCHECK(parse_tree_ != nullptr) << "Parse tree missing";
85
4.77M
  if (parse_tree_->stale()) {
86
0
    return ErrorStatus(ErrorCode::STALE_METADATA);
87
0
  }
88
4.77M
  return static_cast<const ParseTree&>(*parse_tree_);
89
4.77M
}
90
91
Status Statement::ExecuteAsync(QLProcessor* processor, const StatementParameters& params,
92
4.38M
                               StatementExecutedCallback cb) const {
93
4.38M
  const ParseTree& parse_tree = VERIFY_RESULT(GetParseTree());
94
4.38M
  processor->ExecuteAsync(parse_tree, params, std::move(cb));
95
4.38M
  return Status::OK();
96
4.38M
}
97
98
}  // namespace ql
99
}  // namespace yb