Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
View.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.catalog;
16 
17 import java.io.StringReader;
18 import java.util.List;
19 import java.util.Set;
20 
21 import org.apache.hadoop.hive.metastore.HiveMetaStoreClient;
22 import org.apache.hadoop.hive.metastore.api.FieldSchema;
23 
26 import com.cloudera.impala.analysis.SqlParser;
27 import com.cloudera.impala.analysis.SqlScanner;
28 import com.cloudera.impala.thrift.TCatalogObjectType;
29 import com.cloudera.impala.thrift.TTable;
30 import com.cloudera.impala.thrift.TTableDescriptor;
31 import com.cloudera.impala.thrift.TTableType;
32 
41 public class View extends Table {
42 
43  // The original SQL-string given as view definition. Set during analysis.
44  // Corresponds to Hive's viewOriginalText.
45  private String originalViewDef_;
46 
47  // Query statement (as SQL string) that defines the View for view substitution.
48  // It is a transformation of the original view definition, e.g., to enforce the
49  // explicit column definitions even if the original view definition has explicit
50  // column aliases.
51  // If column definitions were given, then this "expanded" view definition
52  // wraps the original view definition in a select stmt as follows.
53  //
54  // SELECT viewName.origCol1 AS colDesc1, viewName.origCol2 AS colDesc2, ...
55  // FROM (originalViewDef) AS viewName
56  //
57  // Corresponds to Hive's viewExpandedText, but is not identical to the SQL
58  // Hive would produce in view creation.
59  private String inlineViewDef_;
60 
61  // View definition created by parsing inlineViewDef_ into a QueryStmt.
63 
64  // Set if this View is from a WITH clause and not persisted in the catalog.
65  private final boolean isLocalView_;
66 
67  public View(TableId id, org.apache.hadoop.hive.metastore.api.Table msTable,
68  Db db, String name, String owner) {
69  super(id, msTable, db, name, owner);
70  isLocalView_ = false;
71  }
72 
76  public View(String alias, QueryStmt queryStmt) {
77  super(null, null, null, alias, null);
78  isLocalView_ = true;
79  queryStmt_ = queryStmt;
80  }
81 
82  @Override
83  public void load(Table oldValue, HiveMetaStoreClient client,
84  org.apache.hadoop.hive.metastore.api.Table msTbl) throws TableLoadingException {
85  try {
86  // Load columns.
87  List<FieldSchema> fieldSchemas = client.getFields(db_.getName(), name_);
88  for (int i = 0; i < fieldSchemas.size(); ++i) {
89  FieldSchema s = fieldSchemas.get(i);
90  Type type = parseColumnType(s);
91  Column col = new Column(s.getName(), type, s.getComment(), i);
92  addColumn(col);
93  }
94  // These fields are irrelevant for views.
96  numRows_ = -1;
97  init();
98  } catch (TableLoadingException e) {
99  throw e;
100  } catch (Exception e) {
101  throw new TableLoadingException("Failed to load metadata for view: " + name_, e);
102  }
103  }
104 
105  @Override
106  protected void loadFromThrift(TTable t) throws TableLoadingException {
107  super.loadFromThrift(t);
108  init();
109  }
110 
117  private void init() throws TableLoadingException {
118  // Set view-definition SQL strings.
119  originalViewDef_ = getMetaStoreTable().getViewOriginalText();
120  inlineViewDef_ = getMetaStoreTable().getViewExpandedText();
121  // Parse the expanded view definition SQL-string into a QueryStmt and
122  // populate a view definition.
123  SqlScanner input = new SqlScanner(new StringReader(inlineViewDef_));
124  SqlParser parser = new SqlParser(input);
125  ParseNode node = null;
126  try {
127  node = (ParseNode) parser.parse().value;
128  } catch (Exception e) {
129  // Do not pass e as the exception cause because it might reveal the existence
130  // of tables that the user triggering this load may not have privileges on.
131  throw new TableLoadingException(
132  String.format("Failed to parse view-definition statement of view: " +
133  "%s.%s", db_.getName(), name_));
134  }
135  // Make sure the view definition parses to a query statement.
136  if (!(node instanceof QueryStmt)) {
137  throw new TableLoadingException(String.format("View definition of %s.%s " +
138  "is not a query statement", db_.getName(), name_));
139  }
140  queryStmt_ = (QueryStmt) node;
141  }
142 
143  @Override
144  public TCatalogObjectType getCatalogObjectType() { return TCatalogObjectType.VIEW; }
145  public QueryStmt getQueryStmt() { return queryStmt_; }
146  public String getOriginalViewDef() { return originalViewDef_; }
147  public String getInlineViewDef() { return inlineViewDef_; }
148 
149  @Override
150  public int getNumNodes() {
151  throw new IllegalStateException("Cannot call getNumNodes() on a view.");
152  }
153 
154  public boolean isLocalView() { return isLocalView_; }
155 
156  @Override
157  public TTableDescriptor toThriftDescriptor(Set<Long> referencedPartitions) {
158  throw new IllegalStateException("Cannot call toThriftDescriptor() on a view.");
159  }
160 
161  @Override
162  public TTable toThrift() {
163  TTable view = super.toThrift();
164  view.setTable_type(TTableType.VIEW);
165  return view;
166  }
167 }
void addColumn(Column col)
Definition: Table.java:114
TTableDescriptor toThriftDescriptor(Set< Long > referencedPartitions)
Definition: View.java:157
final boolean isLocalView_
Definition: View.java:65
View(String alias, QueryStmt queryStmt)
Definition: View.java:76
Type parseColumnType(FieldSchema fs)
Definition: Table.java:331
View(TableId id, org.apache.hadoop.hive.metastore.api.Table msTable, Db db, String name, String owner)
Definition: View.java:67
org.apache.hadoop.hive.metastore.api.Table getMetaStoreTable()
Definition: Table.java:398
Table(TableId id, org.apache.hadoop.hive.metastore.api.Table msTable, Db db, String name, String owner)
Definition: Table.java:91
TCatalogObjectType getCatalogObjectType()
Definition: View.java:144
void loadFromThrift(TTable t)
Definition: View.java:106
string name
Definition: cpu-info.cc:50
void load(Table oldValue, HiveMetaStoreClient client, org.apache.hadoop.hive.metastore.api.Table msTbl)
Definition: View.java:83