Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
WithClause.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.util.ArrayList;
18 import java.util.List;
19 
23 import com.google.common.base.Joiner;
24 import com.google.common.base.Preconditions;
25 import com.google.common.collect.Lists;
26 
45 public class WithClause implements ParseNode {
46  private final ArrayList<View> views_;
47 
48  public WithClause(ArrayList<View> views) {
49  Preconditions.checkNotNull(views);
50  Preconditions.checkState(!views.isEmpty());
51  views_ = views;
52  }
53 
57  public WithClause(WithClause other) {
58  Preconditions.checkNotNull(other);
59  views_ = Lists.newArrayList();
60  for (View view: other.views_) {
61  views_.add(new View(view.getName(), view.getQueryStmt().clone()));
62  }
63  }
64 
70  @Override
71  public void analyze(Analyzer analyzer) throws AnalysisException {
72  // Create a new analyzer for the WITH clause with a new global state (IMPALA-1357)
73  // but a child of 'analyzer' so that the global state for 'analyzer' is not polluted
74  // during analysis of the WITH clause. withClauseAnalyzer is a child of 'analyzer' so
75  // that local views registered in parent blocks are visible here.
76  Analyzer withClauseAnalyzer = Analyzer.createWithNewGlobalState(analyzer);
77  if (analyzer.isExplain()) withClauseAnalyzer.setIsExplain();
78  try {
79  for (View view: views_) {
80  Analyzer viewAnalyzer = new Analyzer(withClauseAnalyzer);
81  view.getQueryStmt().analyze(viewAnalyzer);
82  // Register this view so that the next view can reference it.
83  withClauseAnalyzer.registerLocalView(view);
84  }
85  // Register all local views with the analyzer.
86  for (View localView: withClauseAnalyzer.getLocalViews().values()) {
87  analyzer.registerLocalView(localView);
88  }
89  // Record audit events because the resolved table references won't generate any
90  // when a view is referenced.
91  analyzer.getAccessEvents().addAll(withClauseAnalyzer.getAccessEvents());
92 
93  // Register all privilege requests made from the root analyzer.
94  for (PrivilegeRequest req: withClauseAnalyzer.getPrivilegeReqs()) {
95  analyzer.registerPrivReq(req);
96  }
97  } finally {
98  // Record missing tables in the original analyzer.
99  if (analyzer.isRootAnalyzer()) {
100  analyzer.getMissingTbls().addAll(withClauseAnalyzer.getMissingTbls());
101  }
102  }
103  }
104 
105  @Override
106  public WithClause clone() { return new WithClause(this); }
107 
108  @Override
109  public String toSql() {
110  List<String> viewStrings = Lists.newArrayList();
111  for (View view: views_) {
112  // Enclose the view alias in quotes if Hive cannot parse it without quotes.
113  // This is needed for view compatibility between Impala and Hive.
114  String aliasSql = ToSqlUtils.getIdentSql(view.getName());
115  viewStrings.add(aliasSql + " AS (" + view.getQueryStmt().toSql() + ")");
116  }
117  return "WITH " + Joiner.on(",").join(viewStrings);
118  }
119 }
Set< TAccessEvent > getAccessEvents()
Definition: Analyzer.java:2086
WithClause(ArrayList< View > views)
Definition: WithClause.java:48
Map< String, View > getLocalViews()
Definition: Analyzer.java:2302
ImmutableList< PrivilegeRequest > getPrivilegeReqs()
Definition: Analyzer.java:2077