15 package com.cloudera.impala.analysis;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.List;
22 import org.antlr.runtime.ANTLRStringStream;
23 import org.antlr.runtime.Token;
24 import org.apache.commons.lang.StringEscapeUtils;
25 import org.apache.hadoop.hive.metastore.TableType;
26 import org.apache.hadoop.hive.ql.parse.HiveLexer;
35 import com.google.common.base.Joiner;
36 import com.google.common.base.Preconditions;
37 import com.google.common.base.Strings;
38 import com.google.common.collect.ImmutableSet;
39 import com.google.common.collect.Lists;
40 import com.google.common.collect.Maps;
51 ImmutableSet.of(
"EXTERNAL",
"comment");
67 boolean hiveNeedsQuotes =
true;
68 HiveLexer hiveLexer =
new HiveLexer(
new ANTLRStringStream(ident));
70 Token t = hiveLexer.nextToken();
72 boolean identFound = t.getType() == HiveLexer.Identifier;
73 t = hiveLexer.nextToken();
75 hiveNeedsQuotes = !(identFound && t.getType() == HiveLexer.EOF);
76 }
catch (Exception e) {
79 boolean isImpalaKeyword = SqlScanner.isKeyword(ident.toUpperCase());
83 boolean startsWithNumber =
false;
84 if (!hiveNeedsQuotes && !isImpalaKeyword) {
86 Integer.parseInt(ident.substring(0, 1));
87 startsWithNumber =
true;
88 }
catch (NumberFormatException e) {
92 if (hiveNeedsQuotes || isImpalaKeyword || startsWithNumber)
return "`" + ident +
"`";
97 StringBuilder result =
new StringBuilder();
98 for (String p: path) {
99 if (result.length() > 0) result.append(
".");
102 return result.toString();
110 ArrayList<String> colsSql = Lists.newArrayList();
112 colsSql.add(col.toString());
114 ArrayList<String> partitionColsSql = Lists.newArrayList();
116 partitionColsSql.add(col.toString());
130 Preconditions.checkNotNull(table);
131 org.apache.hadoop.hive.metastore.api.Table msTable = table.getMetaStoreTable();
132 HashMap<String, String> properties = Maps.newHashMap(msTable.getParameters());
133 boolean isExternal = msTable.getTableType() != null &&
134 msTable.getTableType().equals(TableType.EXTERNAL_TABLE.toString());
135 String comment = properties.get(
"comment");
137 properties.remove(hiddenProperty);
139 ArrayList<String> colsSql = Lists.newArrayList();
140 ArrayList<String> partitionColsSql = Lists.newArrayList();
141 boolean isHbaseTable = table instanceof
HBaseTable;
142 for (
int i = 0; i < table.getColumns().size(); i++) {
143 if (!isHbaseTable && i < table.getNumClusteringCols()) {
144 partitionColsSql.add(
columnToSql(table.getColumns().get(i)));
146 colsSql.add(
columnToSql(table.getColumns().get(i)));
149 RowFormat rowFormat = RowFormat.fromStorageDescriptor(msTable.getSd());
151 msTable.getSd().getInputFormat());
152 HdfsCompression compression = HdfsCompression.fromHdfsInputFormatClass(
153 msTable.getSd().getInputFormat());
154 String location = isHbaseTable ? null : msTable.getSd().getLocation();
155 Map<String, String> serdeParameters = msTable.getSd().getSerdeInfo().getParameters();
156 return getCreateTableSql(table.getDb().getName(), table.getName(), comment, colsSql,
157 partitionColsSql, properties, serdeParameters, isExternal,
false, rowFormat,
158 format, compression, table.getStorageHandlerClassName(), location);
167 String tableComment, List<String> columnsSql, List<String> partitionColumnsSql,
168 Map<String, String> tblProperties, Map<String, String> serdeParameters,
169 boolean isExternal,
boolean ifNotExists,
RowFormat rowFormat,
172 Preconditions.checkNotNull(tableName);
173 StringBuilder sb =
new StringBuilder(
"CREATE ");
174 if (isExternal) sb.append(
"EXTERNAL ");
176 if (ifNotExists) sb.append(
"IF NOT EXISTS ");
177 if (dbName != null) sb.append(dbName +
".");
178 if (columnsSql != null) {
179 sb.append(tableName +
" (\n ");
180 sb.append(Joiner.on(
", \n ").join(columnsSql));
184 if (tableComment != null) sb.append(
" COMMENT '" + tableComment +
"'\n");
186 if (partitionColumnsSql != null && partitionColumnsSql.size() > 0) {
187 sb.append(String.format(
"PARTITIONED BY (\n %s\n)\n",
188 Joiner.on(
", \n ").join(partitionColumnsSql)));
191 if (rowFormat != null && !rowFormat.
isDefault()) {
192 sb.append(
"ROW FORMAT DELIMITED");
194 String fieldDelim = StringEscapeUtils.escapeJava(rowFormat.getFieldDelimiter());
195 sb.append(
" FIELDS TERMINATED BY '" + fieldDelim +
"'");
198 String escapeChar = StringEscapeUtils.escapeJava(rowFormat.getEscapeChar());
199 sb.append(
" ESCAPED BY '" + escapeChar +
"'");
202 String lineDelim = StringEscapeUtils.escapeJava(rowFormat.getLineDelimiter());
203 sb.append(
" LINES TERMINATED BY '" + lineDelim +
"'");
208 if (storageHandlerClass == null) {
217 serdeParameters != null && !serdeParameters.isEmpty()) {
222 if (fileFormat != null) {
223 sb.append(
"STORED AS " + fileFormat.toSql(compression) +
"\n");
228 sb.append(
"STORED BY '" + storageHandlerClass +
"'\n");
229 if (serdeParameters != null && !serdeParameters.isEmpty()) {
234 if (location != null) {
235 sb.append(
"LOCATION '" + location +
"'\n");
237 if (tblProperties != null && !tblProperties.isEmpty()) {
240 return sb.toString();
244 StringBuilder sb =
new StringBuilder(col.
getName());
245 if (col.
getType() != null) sb.append(
" " + col.
getType().toSql());
246 if (!Strings.isNullOrEmpty(col.
getComment())) {
247 sb.append(String.format(
" COMMENT '%s'", col.getComment()));
249 return sb.toString();
253 List<String> properties = Lists.newArrayList();
254 for (Map.Entry<String, String> entry: propertyMap.entrySet()) {
255 properties.add(String.format(
"'%s'='%s'", entry.getKey(),
260 StringEscapeUtils.escapeJava(entry.getValue())));
262 return "(" + Joiner.on(
", ").join(properties) +
")";
271 if (hints == null || hints.isEmpty())
return "";
272 StringBuilder sb =
new StringBuilder();
274 sb.append(Joiner.on(
",").join(hints));
276 return sb.toString();
List< ColumnDef > getPartitionColumnDefs()
string path("/usr/lib/sasl2:/usr/lib64/sasl2:/usr/local/lib/sasl2:/usr/lib/x86_64-linux-gnu/sasl2")
static String getCreateTableSql(Table table)
List< ColumnDef > getColumnDefs()
static String getPathSql(List< String > path)
static String getPlanHintsSql(List< String > hints)
static String getIdentSql(String ident)
THdfsFileFormat getFileFormat()
static String getCreateTableSql(String dbName, String tableName, String tableComment, List< String > columnsSql, List< String > partitionColumnsSql, Map< String, String > tblProperties, Map< String, String > serdeParameters, boolean isExternal, boolean ifNotExists, RowFormat rowFormat, HdfsFileFormat fileFormat, HdfsCompression compression, String storageHandlerClass, String location)
static final ImmutableSet< String > HIDDEN_TABLE_PROPERTIES
static String propertyMapToSql(Map< String, String > propertyMap)
Map< String, String > getSerdeProperties()
static String getCreateTableSql(CreateTableStmt stmt)
static String columnToSql(Column col)