Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
AnalysisContext.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 
17 import java.io.StringReader;
18 import java.util.List;
19 import java.util.Set;
20 
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 
27 import com.cloudera.impala.thrift.TAccessEvent;
28 import com.cloudera.impala.thrift.TQueryCtx;
29 import com.google.common.base.Preconditions;
30 
34 public class AnalysisContext {
35  private final static Logger LOG = LoggerFactory.getLogger(AnalysisContext.class);
36  private final ImpaladCatalog catalog_;
37  private final TQueryCtx queryCtx_;
39 
40  // Set in analyze()
42 
43  public AnalysisContext(ImpaladCatalog catalog, TQueryCtx queryCtx,
44  AuthorizationConfig authzConfig) {
45  catalog_ = catalog;
46  queryCtx_ = queryCtx;
47  authzConfig_ = authzConfig;
48  }
49 
50  static public class AnalysisResult {
54 
55  public boolean isAlterTableStmt() { return stmt_ instanceof AlterTableStmt; }
56  public boolean isAlterViewStmt() { return stmt_ instanceof AlterViewStmt; }
57  public boolean isComputeStatsStmt() { return stmt_ instanceof ComputeStatsStmt; }
58  public boolean isQueryStmt() { return stmt_ instanceof QueryStmt; }
59  public boolean isInsertStmt() { return stmt_ instanceof InsertStmt; }
60  public boolean isDropDbStmt() { return stmt_ instanceof DropDbStmt; }
61  public boolean isDropTableOrViewStmt() {
62  return stmt_ instanceof DropTableOrViewStmt;
63  }
64  public boolean isDropFunctionStmt() { return stmt_ instanceof DropFunctionStmt; }
65  public boolean isDropDataSrcStmt() { return stmt_ instanceof DropDataSrcStmt; }
66  public boolean isDropStatsStmt() { return stmt_ instanceof DropStatsStmt; }
67  public boolean isCreateTableLikeStmt() {
68  return stmt_ instanceof CreateTableLikeStmt;
69  }
70  public boolean isCreateViewStmt() { return stmt_ instanceof CreateViewStmt; }
71  public boolean isCreateTableAsSelectStmt() {
72  return stmt_ instanceof CreateTableAsSelectStmt;
73  }
74  public boolean isCreateTableStmt() { return stmt_ instanceof CreateTableStmt; }
75  public boolean isCreateDbStmt() { return stmt_ instanceof CreateDbStmt; }
76  public boolean isCreateUdfStmt() { return stmt_ instanceof CreateUdfStmt; }
77  public boolean isCreateUdaStmt() { return stmt_ instanceof CreateUdaStmt; }
78  public boolean isCreateDataSrcStmt() { return stmt_ instanceof CreateDataSrcStmt; }
79  public boolean isLoadDataStmt() { return stmt_ instanceof LoadDataStmt; }
80  public boolean isUseStmt() { return stmt_ instanceof UseStmt; }
81  public boolean isSetStmt() { return stmt_ instanceof SetStmt; }
82  public boolean isShowTablesStmt() { return stmt_ instanceof ShowTablesStmt; }
83  public boolean isShowDbsStmt() { return stmt_ instanceof ShowDbsStmt; }
84  public boolean isShowDataSrcsStmt() { return stmt_ instanceof ShowDataSrcsStmt; }
85  public boolean isShowStatsStmt() { return stmt_ instanceof ShowStatsStmt; }
86  public boolean isShowFunctionsStmt() { return stmt_ instanceof ShowFunctionsStmt; }
87  public boolean isShowCreateTableStmt() {
88  return stmt_ instanceof ShowCreateTableStmt;
89  }
90  public boolean isShowFilesStmt() { return stmt_ instanceof ShowFilesStmt; }
91  public boolean isDescribeStmt() { return stmt_ instanceof DescribeStmt; }
92  public boolean isResetMetadataStmt() { return stmt_ instanceof ResetMetadataStmt; }
93  public boolean isExplainStmt() { return stmt_.isExplain(); }
94  public boolean isShowRolesStmt() { return stmt_ instanceof ShowRolesStmt; }
95  public boolean isShowGrantRoleStmt() { return stmt_ instanceof ShowGrantRoleStmt; }
96  public boolean isCreateDropRoleStmt() { return stmt_ instanceof CreateDropRoleStmt; }
97  public boolean isGrantRevokeRoleStmt() {
98  return stmt_ instanceof GrantRevokeRoleStmt;
99  }
100  public boolean isGrantRevokePrivStmt() {
101  return stmt_ instanceof GrantRevokePrivStmt;
102  }
103 
104  public boolean isCatalogOp() {
105  return isUseStmt() || isViewMetadataStmt() || isDdlStmt();
106  }
107 
108  private boolean isDdlStmt() {
116  }
117 
118  private boolean isViewMetadataStmt() {
122  }
123 
124  private boolean isGrantRevokeStmt() {
126  }
127 
128  public boolean isDmlStmt() {
129  return isInsertStmt();
130  }
131 
133  Preconditions.checkState(isAlterTableStmt());
134  return (AlterTableStmt) stmt_;
135  }
136 
138  Preconditions.checkState(isAlterViewStmt());
139  return (AlterViewStmt) stmt_;
140  }
141 
143  Preconditions.checkState(isComputeStatsStmt());
144  return (ComputeStatsStmt) stmt_;
145  }
146 
148  Preconditions.checkState(isCreateTableLikeStmt());
149  return (CreateTableLikeStmt) stmt_;
150  }
151 
153  Preconditions.checkState(isCreateViewStmt());
154  return (CreateViewStmt) stmt_;
155  }
156 
158  Preconditions.checkState(isCreateTableAsSelectStmt());
160  }
161 
163  Preconditions.checkState(isCreateTableStmt());
164  return (CreateTableStmt) stmt_;
165  }
166 
168  return tmpCreateTableStmt_;
169  }
170 
172  Preconditions.checkState(isCreateDbStmt());
173  return (CreateDbStmt) stmt_;
174  }
175 
177  Preconditions.checkState(isCreateUdfStmt());
178  return (CreateUdfStmt) stmt_;
179  }
180 
182  Preconditions.checkState(isCreateUdfStmt());
183  return (CreateUdaStmt) stmt_;
184  }
185 
187  Preconditions.checkState(isDropDbStmt());
188  return (DropDbStmt) stmt_;
189  }
190 
192  Preconditions.checkState(isDropTableOrViewStmt());
193  return (DropTableOrViewStmt) stmt_;
194  }
195 
197  Preconditions.checkState(isDropFunctionStmt());
198  return (DropFunctionStmt) stmt_;
199  }
200 
202  Preconditions.checkState(isLoadDataStmt());
203  return (LoadDataStmt) stmt_;
204  }
205 
207  Preconditions.checkState(isQueryStmt());
208  return (QueryStmt) stmt_;
209  }
210 
213  return getCreateTableAsSelectStmt().getInsertStmt();
214  } else {
215  Preconditions.checkState(isInsertStmt());
216  return (InsertStmt) stmt_;
217  }
218  }
219 
220  public UseStmt getUseStmt() {
221  Preconditions.checkState(isUseStmt());
222  return (UseStmt) stmt_;
223  }
224 
225  public SetStmt getSetStmt() {
226  Preconditions.checkState(isSetStmt());
227  return (SetStmt) stmt_;
228  }
229 
231  Preconditions.checkState(isShowTablesStmt());
232  return (ShowTablesStmt) stmt_;
233  }
234 
236  Preconditions.checkState(isShowDbsStmt());
237  return (ShowDbsStmt) stmt_;
238  }
239 
241  Preconditions.checkState(isShowDataSrcsStmt());
242  return (ShowDataSrcsStmt) stmt_;
243  }
244 
246  Preconditions.checkState(isShowStatsStmt());
247  return (ShowStatsStmt) stmt_;
248  }
249 
251  Preconditions.checkState(isShowFunctionsStmt());
252  return (ShowFunctionsStmt) stmt_;
253  }
254 
256  Preconditions.checkState(isShowFilesStmt());
257  return (ShowFilesStmt) stmt_;
258  }
259 
261  Preconditions.checkState(isDescribeStmt());
262  return (DescribeStmt) stmt_;
263  }
264 
266  Preconditions.checkState(isShowCreateTableStmt());
267  return (ShowCreateTableStmt) stmt_;
268  }
269 
270  public StatementBase getStmt() { return stmt_; }
271  public Analyzer getAnalyzer() { return analyzer_; }
272  public Set<TAccessEvent> getAccessEvents() { return analyzer_.getAccessEvents(); }
273  public boolean requiresRewrite() {
274  return analyzer_.containsSubquery() && !(stmt_ instanceof CreateViewStmt)
275  && !(stmt_ instanceof AlterViewStmt);
276  }
277  public String getJsonLineageGraph() { return analyzer_.getSerializedLineageGraph(); }
278  }
279 
292  public void analyze(String stmt) throws AnalysisException {
293  Analyzer analyzer = new Analyzer(catalog_, queryCtx_, authzConfig_);
294  analyze(stmt, analyzer);
295  }
296 
300  public void analyze(String stmt, Analyzer analyzer) throws AnalysisException {
301  SqlScanner input = new SqlScanner(new StringReader(stmt));
302  SqlParser parser = new SqlParser(input);
303  try {
305  analysisResult_.analyzer_ = analyzer;
306  if (analysisResult_.analyzer_ == null) {
307  analysisResult_.analyzer_ = new Analyzer(catalog_, queryCtx_, authzConfig_);
308  }
309  analysisResult_.stmt_ = (StatementBase) parser.parse().value;
310  if (analysisResult_.stmt_ == null) return;
311 
312  // For CTAS, we copy the create statement in case we have to create a new CTAS
313  // statement after a query rewrite.
315  analysisResult_.tmpCreateTableStmt_ =
316  ((CreateTableAsSelectStmt)analysisResult_.stmt_).getCreateStmt().clone();
317  }
318 
319  analysisResult_.stmt_.analyze(analysisResult_.analyzer_);
320  boolean isExplain = analysisResult_.isExplainStmt();
321 
322  // Check if we need to rewrite the statement.
324  StatementBase rewrittenStmt = StmtRewriter.rewrite(analysisResult_);
325  // Re-analyze the rewritten statement.
326  Preconditions.checkNotNull(rewrittenStmt);
328  analysisResult_.analyzer_ = new Analyzer(catalog_, queryCtx_, authzConfig_);
329  analysisResult_.stmt_ = rewrittenStmt;
330  analysisResult_.stmt_.analyze(analysisResult_.analyzer_);
331  LOG.trace("rewrittenStmt: " + rewrittenStmt.toSql());
332  if (isExplain) analysisResult_.stmt_.setIsExplain();
333  Preconditions.checkState(!analysisResult_.requiresRewrite());
334  }
335  } catch (AnalysisException e) {
336  // Don't wrap AnalysisExceptions in another AnalysisException
337  throw e;
338  } catch (Exception e) {
339  throw new AnalysisException(parser.getErrorMsg(stmt), e);
340  }
341  }
342 
344  public Analyzer getAnalyzer() { return getAnalysisResult().getAnalyzer(); }
345 }
void analyze(String stmt, Analyzer analyzer)
AnalysisContext(ImpaladCatalog catalog, TQueryCtx queryCtx, AuthorizationConfig authzConfig)