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/ptree/parse_tree.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
// Parse Tree Declaration.
16
//--------------------------------------------------------------------------------------------------
17
18
#include "yb/yql/cql/ql/ptree/parse_tree.h"
19
20
#include "yb/yql/cql/ql/ptree/list_node.h"
21
#include "yb/yql/cql/ql/ptree/tree_node.h"
22
#include "yb/yql/cql/ql/ptree/sem_context.h"
23
#include "yb/yql/cql/ql/ptree/sem_state.h"
24
25
namespace yb {
26
namespace ql {
27
28
using std::string;
29
30
//--------------------------------------------------------------------------------------------------
31
// Parse Tree
32
//--------------------------------------------------------------------------------------------------
33
34
ParseTree::ParseTree(const string& stmt, const bool reparsed, const MemTrackerPtr& mem_tracker,
35
                     const bool internal)
36
    : stmt_(stmt),
37
      reparsed_(reparsed),
38
      buffer_allocator_(mem_tracker ?
39
                        std::make_shared<MemoryTrackingBufferAllocator>(HeapBufferAllocator::Get(),
40
                                                                        mem_tracker) :
41
                        nullptr),
42
      ptree_mem_(buffer_allocator_ ? buffer_allocator_.get() : HeapBufferAllocator::Get()),
43
      psem_mem_(buffer_allocator_ ? buffer_allocator_.get() : HeapBufferAllocator::Get()),
44
335k
      internal_(internal) {
45
335k
}
46
47
333k
ParseTree::~ParseTree() {
48
  // Make sure we delete the tree first before deleting the memory pools.
49
333k
  root_ = nullptr;
50
333k
}
51
52
333k
CHECKED_STATUS ParseTree::Analyze(SemContext *sem_context) {
53
333k
  if (root_ == nullptr) {
54
3
    LOG(INFO) << "Parse tree is NULL";
55
3
    return Status::OK();
56
3
  }
57
58
  // Each analysis process needs to have state variables.
59
  // Setup a base sem_state variable before traversing the statement tree.
60
333k
  SemState sem_state(sem_context);
61
62
0
  DCHECK_EQ(root_->opcode(), TreeNodeOpcode::kPTListNode) << "statement list expected";
63
333k
  const auto lnode = std::static_pointer_cast<PTListNode>(root_);
64
333k
  switch (lnode->size()) {
65
0
    case 0:
66
0
      return sem_context->Error(lnode, "Unexpected empty statement list",
67
0
                                ErrorCode::SQL_STATEMENT_INVALID);
68
329k
    case 1: {
69
329k
      const TreeNode::SharedPtr tnode = lnode->node_list().front();
70
329k
      if (internal_) {
71
1.41k
        tnode->set_internal();
72
1.41k
      }
73
329k
      RETURN_NOT_OK(tnode->Analyze(sem_context));
74
      // Hoist the statement to the root node.
75
328k
      root_ = tnode;
76
328k
      return Status::OK();
77
329k
    }
78
752
    default:
79
752
      return lnode->AnalyzeStatementBlock(sem_context);
80
333k
  }
81
333k
}
82
83
323k
void ParseTree::AddAnalyzedTable(const client::YBTableName& table_name) {
84
323k
  analyzed_tables_.insert(table_name);
85
323k
}
86
87
2.50k
void ParseTree::ClearAnalyzedTableCache(QLEnv* ql_env) const {
88
2.54k
  for (const auto& table_name : analyzed_tables_) {
89
2.54k
    ql_env->RemoveCachedTableDesc(table_name);
90
2.54k
  }
91
2.50k
}
92
93
74
void ParseTree::AddAnalyzedUDType(const std::string& keyspace_name, const std::string& type_name) {
94
74
  analyzed_types_.insert(std::make_pair(keyspace_name, type_name));
95
74
}
96
97
2.51k
void ParseTree::ClearAnalyzedUDTypeCache(QLEnv *ql_env) const {
98
6
  for (const auto& type_name : analyzed_types_) {
99
6
    ql_env->RemoveCachedUDType(type_name.first, type_name.second);
100
6
  }
101
2.51k
}
102
103
}  // namespace ql
104
}  // namespace yb