Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
AlterTableSetLocationStmt.java
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // 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
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.cloudera.impala.analysis;
16 
22 import com.cloudera.impala.thrift.TAlterTableParams;
23 import com.cloudera.impala.thrift.TAlterTableSetLocationParams;
24 import com.cloudera.impala.thrift.TAlterTableType;
25 import com.google.common.base.Preconditions;
26 import org.apache.hadoop.fs.permission.FsAction;
27 
32  private final HdfsUri location_;
33 
35  PartitionSpec partitionSpec, HdfsUri location) {
36  super(tableName, partitionSpec);
37  Preconditions.checkNotNull(location);
38  this.location_ = location;
39  }
40 
41  public HdfsUri getLocation() { return location_; }
42 
43  @Override
44  public TAlterTableParams toThrift() {
45  TAlterTableParams params = super.toThrift();
46  params.setAlter_type(TAlterTableType.SET_LOCATION);
47  TAlterTableSetLocationParams locationParams =
48  new TAlterTableSetLocationParams(location_.toString());
49  if (getPartitionSpec() != null) {
50  locationParams.setPartition_spec(getPartitionSpec().toThrift());
51  }
52  params.setSet_location_params(locationParams);
53  return params;
54  }
55 
56  @Override
57  public void analyze(Analyzer analyzer) throws AnalysisException {
58  super.analyze(analyzer);
59  location_.analyze(analyzer, Privilege.ALL, FsAction.READ_WRITE);
60 
61  Table table = getTargetTable();
62  Preconditions.checkNotNull(table);
63  if (table instanceof HdfsTable) {
64  HdfsTable hdfsTable = (HdfsTable) table;
65  if (getPartitionSpec() != null) {
66  // Targeting a partition rather than a table.
67  PartitionSpec partitionSpec = getPartitionSpec();
68  HdfsPartition partition = hdfsTable.getPartition(
69  partitionSpec.getPartitionSpecKeyValues());
70  Preconditions.checkNotNull(partition);
71  if (partition.isMarkedCached()) {
72  throw new AnalysisException(String.format("Target partition is cached, " +
73  "please uncache before changing the location using: ALTER TABLE %s %s " +
74  "SET UNCACHED", table.getFullName(), partitionSpec.toSql()));
75  }
76  } else if (hdfsTable.isMarkedCached()) {
77  throw new AnalysisException(String.format("Target table is cached, please " +
78  "uncache before changing the location using: ALTER TABLE %s SET UNCACHED",
79  table.getFullName()));
80  }
81  }
82  }
83 }
AlterTableSetLocationStmt(TableName tableName, PartitionSpec partitionSpec, HdfsUri location)