YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/yql/cql/ql/sem/analyzer.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
#include "yb/yql/cql/ql/sem/analyzer.h"
16
17
#include "yb/util/status.h"
18
#include "yb/yql/cql/ql/ptree/parse_tree.h"
19
#include "yb/yql/cql/ql/ptree/sem_context.h"
20
#include "yb/yql/cql/ql/util/errcodes.h"
21
22
namespace yb {
23
namespace ql {
24
25
using std::string;
26
27
//--------------------------------------------------------------------------------------------------
28
29
18.1k
Analyzer::Analyzer(QLEnv *ql_env) : ql_env_(ql_env) {
30
18.1k
}
31
32
1
Analyzer::~Analyzer() {
33
1
}
34
35
//--------------------------------------------------------------------------------------------------
36
37
340k
CHECKED_STATUS Analyzer::Analyze(ParseTreePtr parse_tree) {
38
340k
  ParseTree *ptree = parse_tree.get();
39
340k
  DCHECK
(ptree != nullptr) << "Parse tree is null"6.61k
;
40
340k
  sem_context_ = std::make_unique<SemContext>(std::move(parse_tree), ql_env_);
41
340k
  Status s = ptree->Analyze(sem_context_.get());
42
340k
  if (PREDICT_FALSE(!s.ok())) {
43
    // When a statement is parsed for the first time, semantic analysis may fail because stale
44
    // table metadata cache was used. If that happens, clear the cache and tell the caller to
45
    // reparse. The only exception is when the keyspace, table or type or is not found in which
46
    // case no cache is used.
47
1.44k
    if (!ptree->reparsed()) {
48
1.08k
      const ErrorCode errcode = GetErrorCode(s);
49
1.08k
      if (errcode != ErrorCode::KEYSPACE_NOT_FOUND &&
50
1.08k
          errcode != ErrorCode::OBJECT_NOT_FOUND &&
51
1.08k
          
errcode != ErrorCode::TYPE_NOT_FOUND928
&&
52
1.08k
          
sem_context_->cache_used()921
) {
53
414
        ptree->ClearAnalyzedTableCache(ql_env_);
54
414
        ptree->ClearAnalyzedUDTypeCache(ql_env_);
55
414
        ptree->set_stale();
56
414
        return sem_context_->Error(ptree->root(), ErrorCode::STALE_METADATA);
57
414
      }
58
1.08k
    }
59
60
    // Before leaving the semantic step, collect all errors and place them in return status.
61
1.03k
    VLOG
(3) << "Failed to analyze parse-tree <" << ptree << ">"0
;
62
1.03k
    return sem_context_->GetStatus();
63
1.44k
  }
64
65
18.4E
  VLOG(3) << "Successfully analyzed parse-tree <" << ptree << ">";
66
339k
  return Status::OK();
67
340k
}
68
69
342k
ParseTree::UniPtr Analyzer::Done() {
70
  // When releasing the parse tree, we must free the context because it has references to the tree
71
  // which doesn't belong to this context any longer.
72
342k
  ParseTree::UniPtr ptree = sem_context_->AcquireParseTree();
73
342k
  sem_context_ = nullptr;
74
342k
  return ptree;
75
342k
}
76
77
0
bool Analyzer::cache_used() const {
78
0
  return sem_context_->cache_used();
79
0
}
80
81
}  // namespace ql
82
}  // namespace yb