15 package com.cloudera.impala.analysis;
17 import java.util.ArrayList;
18 import java.util.List;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 import com.cloudera.impala.thrift.TCreateOrAlterViewParams;
26 import com.cloudera.impala.thrift.TTableName;
27 import com.google.common.base.Preconditions;
28 import com.google.common.collect.Lists;
29 import com.google.common.collect.Sets;
35 private final static Logger
LOG =
36 LoggerFactory.getLogger(CreateOrAlterViewStmtBase.class);
70 ArrayList<ColumnDef> columnDefs, String comment,
QueryStmt viewDefStmt) {
71 Preconditions.checkNotNull(tableName);
72 Preconditions.checkNotNull(viewDefStmt);
73 this.ifNotExists_ = ifNotExists;
74 this.tableName_ = tableName;
75 this.columnDefs_ = columnDefs;
76 this.comment_ = comment;
77 this.viewDefStmt_ = viewDefStmt;
86 Preconditions.checkNotNull(
dbName_);
87 Preconditions.checkNotNull(
owner_);
91 Preconditions.checkState(!columnDefs_.isEmpty());
92 if (
columnDefs_.size() != viewDefStmt_.getColLabels().size()) {
96 "%s columns (%s) than the view-definition query statement returns (%s).",
97 cmp,
columnDefs_.size(), viewDefStmt_.getColLabels().size()));
101 Preconditions.checkState(
103 for (
int i = 0; i < columnDefs_.size(); ++i) {
110 List<Expr> exprs = viewDefStmt_.getBaseTblResultExprs();
111 List<String> labels = viewDefStmt_.getColLabels();
112 Preconditions.checkState(exprs.size() == labels.size());
113 for (
int i = 0; i < viewDefStmt_.getColLabels().size(); ++i) {
115 colDef.setType(exprs.get(i).getType());
116 finalColDefs_.add(colDef);
122 Set<String> distinctColNames = Sets.newHashSet();
125 if (!distinctColNames.add(colDesc.getColName().toLowerCase())) {
142 StringBuilder sb =
new StringBuilder();
143 sb.append(
"SELECT ");
144 for (
int i = 0; i < finalColDefs_.size(); ++i) {
145 String colRef = ToSqlUtils.getIdentSql(viewDefStmt_.getColLabels().
get(i));
146 String colAlias = ToSqlUtils.getIdentSql(finalColDefs_.get(i).getColName());
147 sb.append(String.format(
"%s.%s AS %s", tableName_.getTbl(), colRef, colAlias));
148 sb.append((i+1 != finalColDefs_.size()) ?
", " :
"");
151 sb.append(String.format(
" FROM (%s) %s",
originalViewDef_, tableName_.getTbl()));
160 List<String> colDefs = Lists.newArrayList();
162 colDefs.add(
dbName_ +
"." +
getTbl() +
"." + colDef.getColName());
164 graph.addTargetColumnLabels(colDefs);
165 graph.computeLineageGraph(viewDefStmt_.getResultExprs(), analyzer);
166 LOG.trace(
"lineage: " + graph.debugString());
170 TCreateOrAlterViewParams params =
new TCreateOrAlterViewParams();
171 params.setView_name(
new TTableName(
getDb(),
getTbl()));
173 params.addToColumns(col.toThrift());
188 Preconditions.checkNotNull(
dbName_);
196 Preconditions.checkNotNull(
owner_);
205 public String
getTbl() {
return tableName_.getTbl(); }
final boolean ifNotExists_
final ArrayList< ColumnDef > columnDefs_
String getOriginalViewDef()
ArrayList< ColumnDef > finalColDefs_
final QueryStmt viewDefStmt_
CreateOrAlterViewStmtBase(boolean ifNotExists, TableName tableName, ArrayList< ColumnDef > columnDefs, String comment, QueryStmt viewDefStmt)
void createColumnAndViewDefs(Analyzer analyzer)
ArrayList< Expr > getBaseTblResultExprs()
final TableName tableName_
void computeLineageGraph(Analyzer analyzer)
String getInlineViewDef()
List< ColumnDef > getColumnDescs()
abstract ArrayList< String > getColLabels()
TCreateOrAlterViewParams toThrift()