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/ptree/pt_drop.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
// Treenode definitions for DROP statements.
16
//--------------------------------------------------------------------------------------------------
17
18
#include "yb/yql/cql/ql/ptree/pt_drop.h"
19
20
#include "yb/yql/cql/ql/ptree/pt_option.h"
21
#include "yb/yql/cql/ql/ptree/sem_context.h"
22
#include "yb/yql/cql/ql/ptree/yb_location.h"
23
24
DECLARE_bool(use_cassandra_authentication);
25
26
namespace yb {
27
namespace ql {
28
29
PTDropStmt::PTDropStmt(MemoryContext *memctx,
30
                       YBLocation::SharedPtr loc,
31
                       ObjectType drop_type,
32
                       PTQualifiedNameListNode::SharedPtr names,
33
                       bool drop_if_exists)
34
    : TreeNode(memctx, loc),
35
      drop_type_(drop_type),
36
      names_(names),
37
3.65k
      drop_if_exists_(drop_if_exists) {
38
3.65k
}
39
40
3.59k
PTDropStmt::~PTDropStmt() {
41
3.59k
}
42
43
3.65k
CHECKED_STATUS PTDropStmt::Analyze(SemContext *sem_context) {
44
3.65k
  if (drop_type_ == ObjectType::ROLE) {
45
737
    RETURN_NOT_AUTH_ENABLED(sem_context);
46
737
  }
47
48
3.65k
  if (names_->size() > 1) {
49
0
    return sem_context->Error(names_, "Only one object name is allowed in a drop statement",
50
0
                              ErrorCode::CQL_STATEMENT_INVALID);
51
0
  }
52
53
  // Processing object name.
54
3.65k
  RETURN_NOT_OK(name()->AnalyzeName(sem_context, drop_type()));
55
56
3.64k
  if (FLAGS_use_cassandra_authentication) {
57
1.91k
    switch (drop_type()) {
58
4
      case ObjectType::INDEX: {
59
        // The permissions for dropping an index get checked in the master because we don't know
60
        // for which table this index was created for.
61
4
        break;
62
0
      }
63
247
      case ObjectType::TABLE: {
64
247
        RETURN_NOT_OK(sem_context->CheckHasTablePermission(loc(), PermissionType::DROP_PERMISSION,
65
247
            yb_table_name()));
66
239
        break;
67
247
      }
68
239
      case ObjectType::TYPE:
69
36
        if (!sem_context->CheckHasAllKeyspacesPermission(loc(),
70
36
            PermissionType::DROP_PERMISSION).ok()) {
71
24
          RETURN_NOT_OK(sem_context->CheckHasKeyspacePermission(loc(),
72
24
              PermissionType::DROP_PERMISSION, yb_table_name().namespace_name()));
73
24
        }
74
32
        break;
75
886
      case ObjectType::SCHEMA:
76
886
        RETURN_NOT_OK(sem_context->CheckHasKeyspacePermission(loc(),
77
886
            PermissionType::DROP_PERMISSION, yb_table_name().namespace_name()));
78
876
        break;
79
876
      case ObjectType::ROLE:
80
737
        RETURN_NOT_OK(sem_context->CheckHasRolePermission(loc(), PermissionType::DROP_PERMISSION,
81
737
            name()->QLName()));
82
737
        if (sem_context->CurrentRoleName() == name()->QLName()) {
83
3
          return sem_context->Error(this, "Cannot DROP primary role for current login",
84
3
              ErrorCode::INVALID_REQUEST);
85
3
        }
86
734
        break;
87
734
      default:
88
0
        return sem_context->Error(this, ErrorCode::FEATURE_NOT_SUPPORTED);
89
1.91k
    }
90
1.91k
  }
91
92
3.62k
  return Status::OK();
93
3.64k
}
94
95
0
void PTDropStmt::PrintSemanticAnalysisResult(SemContext *sem_context) {
96
0
  MCString sem_output("\t", sem_context->PTempMem());
97
98
0
  switch (drop_type()) {
99
0
    case ObjectType::TABLE: sem_output += "Table "; break;
100
0
    case ObjectType::SCHEMA: sem_output += "Keyspace "; break;
101
0
    case ObjectType::TYPE: sem_output += "Type "; break;
102
0
    case ObjectType::INDEX: sem_output += "Index "; break;
103
0
    case ObjectType::ROLE: sem_output += "Role "; break;
104
0
    default: sem_output += "UNKNOWN OBJECT ";
105
0
  }
106
107
0
  sem_output += name()->last_name();
108
0
  sem_output += (drop_if_exists()? " IF EXISTS" : "");
109
0
  VLOG(3) << "SEMANTIC ANALYSIS RESULT (" << *loc_ << "):\n" << sem_output;
110
0
}
111
112
}  // namespace ql
113
}  // namespace yb