Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
CreateTableDataSrcStmt.java
Go to the documentation of this file.
1 // Copyright 2014 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 static com.cloudera.impala.catalog.DataSourceTable.TBL_PROP_API_VER;
18 import static com.cloudera.impala.catalog.DataSourceTable.TBL_PROP_CLASS;
19 import static com.cloudera.impala.catalog.DataSourceTable.TBL_PROP_DATA_SRC_NAME;
20 import static com.cloudera.impala.catalog.DataSourceTable.TBL_PROP_INIT_STRING;
21 import static com.cloudera.impala.catalog.DataSourceTable.TBL_PROP_LOCATION;
22 
23 import java.util.List;
24 import java.util.Map;
25 
31 import com.cloudera.impala.thrift.THdfsFileFormat;
32 import com.google.common.base.Preconditions;
33 import com.google.common.base.Strings;
34 import com.google.common.collect.Lists;
35 import com.google.common.collect.Maps;
36 import org.apache.hadoop.fs.permission.FsAction;
37 
45 
46  public CreateTableDataSrcStmt(TableName tableName, List<ColumnDef> columnDefs,
47  String dataSourceName, String initString, String comment, boolean ifNotExists) {
48  super(tableName, columnDefs, Lists.<ColumnDef>newArrayList(), false, comment,
49  RowFormat.DEFAULT_ROW_FORMAT, THdfsFileFormat.TEXT, null, null, ifNotExists,
50  createInitialTableProperties(dataSourceName, initString),
51  Maps.<String, String>newHashMap());
52  }
53 
58  private static Map<String, String> createInitialTableProperties(
59  String dataSourceName, String initString) {
60  Preconditions.checkNotNull(dataSourceName);
61  Map<String, String> tableProperties = Maps.newHashMap();
62  tableProperties.put(TBL_PROP_DATA_SRC_NAME, dataSourceName.toLowerCase());
63  tableProperties.put(TBL_PROP_INIT_STRING, Strings.nullToEmpty(initString));
64  return tableProperties;
65  }
66 
67  @Override
68  public void analyze(Analyzer analyzer) throws AnalysisException {
69  super.analyze(analyzer);
70  String dataSourceName = getTblProperties().get(TBL_PROP_DATA_SRC_NAME);
71  DataSource dataSource = analyzer.getCatalog().getDataSource(dataSourceName);
72  if (dataSource == null) {
73  throw new AnalysisException("Data source does not exist: " + dataSourceName);
74  }
75 
76  for (ColumnDef col: getColumnDefs()) {
77  if (!DataSourceTable.isSupportedColumnType(col.getType())) {
78  throw new AnalysisException("Tables produced by an external data source do " +
79  "not support the column type: " + col.getType());
80  }
81  }
82  // Add table properties from the DataSource catalog object now that we have access
83  // to the catalog. These are stored in the table metadata because DataSource catalog
84  // objects are not currently persisted.
85  String location = dataSource.getLocation();
86  getTblProperties().put(TBL_PROP_LOCATION, location);
87  getTblProperties().put(TBL_PROP_CLASS, dataSource.getClassName());
88  getTblProperties().put(TBL_PROP_API_VER, dataSource.getApiVersion());
89  new HdfsUri(location).analyze(analyzer, Privilege.ALL, FsAction.READ);
90  // TODO: check class exists and implements API version
91  }
92 }
static boolean isSupportedColumnType(Type colType)
static final RowFormat DEFAULT_ROW_FORMAT
Definition: RowFormat.java:32
CreateTableDataSrcStmt(TableName tableName, List< ColumnDef > columnDefs, String dataSourceName, String initString, String comment, boolean ifNotExists)
static Map< String, String > createInitialTableProperties(String dataSourceName, String initString)