Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ValuesStmt.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 
20 import com.google.common.base.Preconditions;
21 import com.google.common.collect.Lists;
22 
30 public class ValuesStmt extends UnionStmt {
31 
32  public ValuesStmt(List<UnionOperand> operands,
33  ArrayList<OrderByElement> orderByElements, LimitElement limitElement) {
34  super(operands, orderByElements, limitElement);
35  }
36 
37  @Override
38  protected String queryStmtToSql(QueryStmt queryStmt) {
39  StringBuilder strBuilder = new StringBuilder();
40  strBuilder.append("(");
41  appendSelectList((SelectStmt) queryStmt, strBuilder);
42  strBuilder.append(")");
43  return strBuilder.toString();
44  }
45 
46  @Override
47  public String toSql() {
48  StringBuilder strBuilder = new StringBuilder();
49 
50  if (withClause_ != null) {
51  strBuilder.append(withClause_.toSql());
52  strBuilder.append(" ");
53  }
54 
55  Preconditions.checkState(operands_.size() > 0);
56  strBuilder.append("VALUES(");
57  for (int i = 0; i < operands_.size(); ++i) {
58  if (operands_.size() != 1) strBuilder.append("(");
59  appendSelectList((SelectStmt) operands_.get(i).getQueryStmt(), strBuilder);
60  if (operands_.size() != 1) strBuilder.append(")");
61  strBuilder.append((i+1 != operands_.size()) ? ", " : "");
62  }
63  strBuilder.append(")");
64  return strBuilder.toString();
65  }
66 
67  private void appendSelectList(SelectStmt select, StringBuilder strBuilder) {
68  SelectList selectList = select.getSelectList();
69  for (int j = 0; j < selectList.getItems().size(); ++j) {
70  strBuilder.append(selectList.getItems().get(j).toSql());
71  strBuilder.append((j+1 != selectList.getItems().size()) ? ", " : "");
72  }
73  }
74 
75  @Override
76  public QueryStmt clone() {
77  List<UnionOperand> operandClones = Lists.newArrayList();
78  for (UnionOperand operand: operands_) {
79  operandClones.add(operand.clone());
80  }
81  ValuesStmt valuesClone = new ValuesStmt(operandClones, cloneOrderByElements(),
82  limitElement_ == null ? null : limitElement_.clone());
83  valuesClone.setWithClause(cloneWithClause());
84  return valuesClone;
85  }
86 }
ValuesStmt(List< UnionOperand > operands, ArrayList< OrderByElement > orderByElements, LimitElement limitElement)
Definition: ValuesStmt.java:32
ArrayList< OrderByElement > cloneOrderByElements()
Definition: QueryStmt.java:311
void appendSelectList(SelectStmt select, StringBuilder strBuilder)
Definition: ValuesStmt.java:67
final List< UnionOperand > operands_
Definition: UnionStmt.java:103
String queryStmtToSql(QueryStmt queryStmt)
Definition: ValuesStmt.java:38