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/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
17.1k
Analyzer::Analyzer(QLEnv *ql_env) : ql_env_(ql_env) {
30
17.1k
}
31
32
1
Analyzer::~Analyzer() {
33
1
}
34
35
//--------------------------------------------------------------------------------------------------
36
37
333k
CHECKED_STATUS Analyzer::Analyze(ParseTreePtr parse_tree) {
38
333k
  ParseTree *ptree = parse_tree.get();
39
5.20k
  DCHECK(ptree != nullptr) << "Parse tree is null";
40
333k
  sem_context_ = std::make_unique<SemContext>(std::move(parse_tree), ql_env_);
41
333k
  Status s = ptree->Analyze(sem_context_.get());
42
333k
  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.45k
    if (!ptree->reparsed()) {
48
1.09k
      const ErrorCode errcode = GetErrorCode(s);
49
1.09k
      if (errcode != ErrorCode::KEYSPACE_NOT_FOUND &&
50
1.09k
          errcode != ErrorCode::OBJECT_NOT_FOUND &&
51
930
          errcode != ErrorCode::TYPE_NOT_FOUND &&
52
923
          sem_context_->cache_used()) {
53
417
        ptree->ClearAnalyzedTableCache(ql_env_);
54
417
        ptree->ClearAnalyzedUDTypeCache(ql_env_);
55
417
        ptree->set_stale();
56
417
        return sem_context_->Error(ptree->root(), ErrorCode::STALE_METADATA);
57
417
      }
58
1.04k
    }
59
60
    // Before leaving the semantic step, collect all errors and place them in return status.
61
0
    VLOG(3) << "Failed to analyze parse-tree <" << ptree << ">";
62
1.04k
    return sem_context_->GetStatus();
63
1.04k
  }
64
65
18.4E
  VLOG(3) << "Successfully analyzed parse-tree <" << ptree << ">";
66
332k
  return Status::OK();
67
332k
}
68
69
336k
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
336k
  ParseTree::UniPtr ptree = sem_context_->AcquireParseTree();
73
336k
  sem_context_ = nullptr;
74
336k
  return ptree;
75
336k
}
76
77
0
bool Analyzer::cache_used() const {
78
0
  return sem_context_->cache_used();
79
0
}
80
81
}  // namespace ql
82
}  // namespace yb