You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by na...@apache.org on 2010/08/27 07:41:45 UTC

svn commit: r990026 [7/10] - in /hadoop/hive/trunk: ./ eclipse-templates/ metastore/if/ metastore/src/gen-cpp/ metastore/src/gen-javabean/org/apache/hadoop/hive/metastore/api/ metastore/src/gen-php/ metastore/src/gen-py/hive_metastore/ metastore/src/ja...

Modified: hadoop/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStoreClient.java
URL: http://svn.apache.org/viewvc/hadoop/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStoreClient.java?rev=990026&r1=990025&r2=990026&view=diff
==============================================================================
--- hadoop/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStoreClient.java (original)
+++ hadoop/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStoreClient.java Fri Aug 27 05:41:43 2010
@@ -79,6 +79,7 @@ public class HiveMetaStoreClient impleme
       conf = new HiveConf(HiveMetaStoreClient.class);
     }
 
+
     boolean localMetaStore = conf.getBoolean("hive.metastore.local", false);
     if (localMetaStore) {
       // instantiate the metastore server handler directly instead of connecting
@@ -204,19 +205,6 @@ public class HiveMetaStoreClient impleme
     }
   }
 
-  public void dropTable(String tableName, boolean deleteData)
-      throws MetaException, NoSuchObjectException {
-    // assume that it is default database
-    try {
-      this.dropTable(MetaStoreUtils.DEFAULT_DATABASE_NAME, tableName,
-          deleteData, false);
-    } catch (NoSuchObjectException e) {
-      throw e;
-    } catch (Exception e) {
-      MetaStoreUtils.logAndThrowMetaException(e);
-    }
-  }
-
   /**
    * @param new_part
    * @return the added partition
@@ -256,19 +244,21 @@ public class HiveMetaStoreClient impleme
     return deepCopy(
         client.append_partition_by_name(dbName, tableName, partName));
   }
+
   /**
-   * @param name
-   * @param location_uri
+   * Create a new Database
+   * @param db
    * @return true or false
    * @throws AlreadyExistsException
+   * @throws InvalidObjectException
    * @throws MetaException
    * @throws TException
    * @see org.apache.hadoop.hive.metastore.api.ThriftHiveMetastore.Iface#create_database(java.lang.String,
    *      java.lang.String)
    */
-  public boolean createDatabase(String name, String location_uri)
-      throws AlreadyExistsException, MetaException, TException {
-    return client.create_database(name, location_uri);
+  public void createDatabase(Database db)
+      throws AlreadyExistsException, InvalidObjectException, MetaException, TException {
+    client.create_database(db);
   }
 
   /**
@@ -315,14 +305,32 @@ public class HiveMetaStoreClient impleme
   /**
    * @param name
    * @return true or false
+   * @throws NoSuchObjectException
+   * @throws InvalidOperationException
    * @throws MetaException
    * @throws TException
    * @see org.apache.hadoop.hive.metastore.api.ThriftHiveMetastore.Iface#drop_database(java.lang.String)
    */
-  public boolean dropDatabase(String name) throws MetaException, TException {
-    return client.drop_database(name);
+  public void dropDatabase(String name)
+      throws NoSuchObjectException, InvalidOperationException, MetaException, TException {
+    dropDatabase(name, true, false);
   }
 
+
+  public void dropDatabase(String name, boolean deleteData, boolean ignoreUnknownDb)
+      throws NoSuchObjectException, InvalidOperationException, MetaException, TException {
+    try {
+      getDatabase(name);
+    } catch (NoSuchObjectException e) {
+      if (!ignoreUnknownDb) {
+        throw e;
+      }
+      return;
+    }
+    client.drop_database(name, deleteData);
+  }
+
+
   /**
    * @param tbl_name
    * @param db_name
@@ -431,7 +439,7 @@ public class HiveMetaStoreClient impleme
    * @throws TException
    * @see org.apache.hadoop.hive.metastore.api.ThriftHiveMetastore.Iface#drop_type(java.lang.String)
    */
-  public boolean dropType(String type) throws MetaException, TException {
+  public boolean dropType(String type) throws NoSuchObjectException, MetaException, TException {
     return client.drop_type(type);
   }
 
@@ -461,8 +469,29 @@ public class HiveMetaStoreClient impleme
    * @throws TException
    * @see org.apache.hadoop.hive.metastore.api.ThriftHiveMetastore.Iface#get_databases()
    */
-  public List<String> getDatabases() throws MetaException, TException {
-    return client.get_databases();
+  public List<String> getDatabases(String databasePattern)
+    throws MetaException {
+    try {
+      return client.get_databases(databasePattern);
+    } catch (Exception e) {
+      MetaStoreUtils.logAndThrowMetaException(e);
+    }
+    return null;
+  }
+
+  /**
+   * @return the list of databases
+   * @throws MetaException
+   * @throws TException
+   * @see org.apache.hadoop.hive.metastore.api.ThriftHiveMetastore.Iface#get_all_databases()
+   */
+  public List<String> getAllDatabases() throws MetaException {
+    try {
+      return client.get_all_databases();
+    } catch (Exception e) {
+      MetaStoreUtils.logAndThrowMetaException(e);
+    }
+    return null;
   }
 
   /**
@@ -537,14 +566,14 @@ public class HiveMetaStoreClient impleme
    * @return the type
    * @throws MetaException
    * @throws TException
+   * @throws NoSuchObjectException
    * @see org.apache.hadoop.hive.metastore.api.ThriftHiveMetastore.Iface#get_type(java.lang.String)
    */
-  public Type getType(String name) throws MetaException, TException {
+  public Type getType(String name) throws NoSuchObjectException, MetaException, TException {
     return deepCopy(client.get_type(name));
   }
 
-  public List<String> getTables(String dbname, String tablePattern)
-      throws MetaException {
+  public List<String> getTables(String dbname, String tablePattern) throws MetaException {
     try {
       return client.get_tables(dbname, tablePattern);
     } catch (Exception e) {
@@ -553,26 +582,25 @@ public class HiveMetaStoreClient impleme
     return null;
   }
 
-  public List<String> getTables(String tablePattern) throws MetaException {
-    String dbname = MetaStoreUtils.DEFAULT_DATABASE_NAME;
-    return this.getTables(dbname, tablePattern);
+  public List<String> getAllTables(String dbname) throws MetaException {
+    try {
+      return client.get_all_tables(dbname);
+    } catch (Exception e) {
+      MetaStoreUtils.logAndThrowMetaException(e);
+    }
+    return null;
   }
 
-  public boolean tableExists(String tableName) throws MetaException,
+  public boolean tableExists(String databaseName, String tableName) throws MetaException,
       TException, UnknownDBException {
     try {
-      client.get_table(MetaStoreUtils.DEFAULT_DATABASE_NAME, tableName);
+      client.get_table(databaseName, tableName);
     } catch (NoSuchObjectException e) {
       return false;
     }
     return true;
   }
 
-  public Table getTable(String tableName) throws MetaException, TException,
-      NoSuchObjectException {
-    return getTable(MetaStoreUtils.DEFAULT_DATABASE_NAME, tableName);
-  }
-
   public List<String> listPartitionNames(String dbName, String tblName,
       short max) throws MetaException, TException {
     return client.get_partition_names(dbName, tblName, max);
@@ -604,7 +632,7 @@ public class HiveMetaStoreClient impleme
       UnknownDBException {
     return deepCopyFieldSchemas(client.get_fields(db, tableName));
   }
-  
+
   /**
    * create an index
    * @param index the index object
@@ -613,12 +641,12 @@ public class HiveMetaStoreClient impleme
    * @throws MetaException
    * @throws NoSuchObjectException
    * @throws TException
-   * @throws AlreadyExistsException 
+   * @throws AlreadyExistsException
    */
   public void createIndex(Index index, Table indexTable) throws AlreadyExistsException, InvalidObjectException, MetaException, NoSuchObjectException, TException {
     client.add_index(index, indexTable);
   }
-  
+
   /**
    * @param dbName
    * @param tblName
@@ -652,7 +680,7 @@ public class HiveMetaStoreClient impleme
 
   /**
    * list all the index names of the give base table.
-   * 
+   *
    * @param db_name
    * @param tbl_name
    * @param max
@@ -664,7 +692,7 @@ public class HiveMetaStoreClient impleme
       throws NoSuchObjectException, MetaException, TException {
     return client.get_indexes(dbName, tblName, max);
   }
-  
+
   /**
    * @param db
    * @param tableName

Modified: hadoop/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/IMetaStoreClient.java
URL: http://svn.apache.org/viewvc/hadoop/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/IMetaStoreClient.java?rev=990026&r1=990025&r2=990026&view=diff
==============================================================================
--- hadoop/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/IMetaStoreClient.java (original)
+++ hadoop/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/IMetaStoreClient.java Fri Aug 27 05:41:43 2010
@@ -23,9 +23,9 @@ import java.util.Map;
 
 import org.apache.hadoop.hive.metastore.api.AlreadyExistsException;
 import org.apache.hadoop.hive.metastore.api.ConfigValSecurityException;
+import org.apache.hadoop.hive.metastore.api.Database;
 import org.apache.hadoop.hive.metastore.api.FieldSchema;
 import org.apache.hadoop.hive.metastore.api.Index;
-import org.apache.hadoop.hive.metastore.api.IndexAlreadyExistsException;
 import org.apache.hadoop.hive.metastore.api.InvalidObjectException;
 import org.apache.hadoop.hive.metastore.api.InvalidOperationException;
 import org.apache.hadoop.hive.metastore.api.MetaException;
@@ -44,29 +44,18 @@ public interface IMetaStoreClient {
 
   public void close();
 
+  public List<String> getDatabases(String databasePattern)
+      throws MetaException, TException;
+
+  public List<String> getAllDatabases()
+      throws MetaException, TException;
+
   public List<String> getTables(String dbName, String tablePattern)
-      throws MetaException, UnknownTableException, TException,
-      UnknownDBException;
+      throws MetaException, TException, UnknownDBException;
+
+  public List<String> getAllTables(String dbName)
+      throws MetaException, TException, UnknownDBException;
 
-  /**
-   * Drop the table.
-   *
-   * @param tableName
-   *          The table to drop
-   * @param deleteData
-   *          Should we delete the underlying data
-   * @throws MetaException
-   *           Could not drop table properly.
-   * @throws UnknownTableException
-   *           The table wasn't found.
-   * @throws TException
-   *           A thrift communication error occurred
-   * @throws NoSuchObjectException
-   *           The table wasn't found.
-   */
-  public void dropTable(String tableName, boolean deleteData)
-      throws MetaException, UnknownTableException, TException,
-      NoSuchObjectException;
 
   /**
    * Drop the table.
@@ -87,28 +76,27 @@ public interface IMetaStoreClient {
       boolean ignoreUknownTab) throws MetaException, TException,
       NoSuchObjectException;
 
+  public void dropTable(String dbname, String tableName)
+      throws MetaException, TException, NoSuchObjectException;
+
   // public void createTable(String tableName, Properties schema) throws
   // MetaException, UnknownTableException,
   // TException;
 
-  public boolean tableExists(String tableName) throws MetaException,
+  public boolean tableExists(String databaseName, String tableName) throws MetaException,
       TException, UnknownDBException;
 
   /**
-   * Get a table object.
-   *
-   * @param tableName
-   *          Name of the table to fetch.
-   * @return An object representing the table.
-   * @throws MetaException
-   *           Could not fetch the table
-   * @throws TException
-   *           A thrift communication error occurred
-   * @throws NoSuchObjectException
-   *           In case the table wasn't found.
+   * Get a Database Object
+   * @param databaseName  name of the database to fetch
+   * @return
+   * @throws NoSuchObjectException The database does not exist
+   * @throws MetaException Could not fetch the database
+   * @throws TException A thrift communication error occurred
    */
-  public Table getTable(String tableName) throws MetaException, TException,
-      NoSuchObjectException;
+    public Database getDatabase(String databaseName)
+        throws NoSuchObjectException, MetaException, TException;
+
 
   /**
    * Get a table object.
@@ -227,10 +215,14 @@ public interface IMetaStoreClient {
   public void alter_table(String defaultDatabaseName, String tblName,
       Table table) throws InvalidOperationException, MetaException, TException;
 
-  public boolean createDatabase(String name, String location_uri)
-      throws AlreadyExistsException, MetaException, TException;
+  public void createDatabase(Database db)
+      throws InvalidObjectException, AlreadyExistsException, MetaException, TException;
+
+  public void dropDatabase(String name)
+      throws NoSuchObjectException, InvalidOperationException, MetaException, TException;
 
-  public boolean dropDatabase(String name) throws MetaException, TException;
+  public void dropDatabase(String name, boolean deleteData, boolean ignoreUnknownDb)
+      throws NoSuchObjectException, InvalidOperationException, MetaException, TException;
 
   /**
    * @param db_name
@@ -339,13 +331,13 @@ public interface IMetaStoreClient {
    * @throws MetaException
    * @throws NoSuchObjectException
    * @throws TException
-   * @throws AlreadyExistsException 
+   * @throws AlreadyExistsException
    */
   public void createIndex(Index index, Table indexTable) throws InvalidObjectException,
       MetaException, NoSuchObjectException, TException, AlreadyExistsException;
 
   /**
-   * 
+   *
    * @param dbName
    * @param tblName
    * @param indexName
@@ -375,7 +367,7 @@ public interface IMetaStoreClient {
 
   /**
    * list all the index names of the give base table.
-   * 
+   *
    * @param db_name
    * @param tbl_name
    * @param max
@@ -385,7 +377,7 @@ public interface IMetaStoreClient {
    */
   public List<String> listIndexNames(String db_name, String tbl_name,
       short max) throws MetaException, TException;
-  
+
   /**
    * @param db_name
    * @param tbl_name

Modified: hadoop/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreUtils.java
URL: http://svn.apache.org/viewvc/hadoop/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreUtils.java?rev=990026&r1=990025&r2=990026&view=diff
==============================================================================
--- hadoop/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreUtils.java (original)
+++ hadoop/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreUtils.java Fri Aug 27 05:41:43 2010
@@ -60,7 +60,8 @@ public class MetaStoreUtils {
   protected static final Log LOG = LogFactory.getLog("hive.log");
 
   public static final String DEFAULT_DATABASE_NAME = "default";
-  
+  public static final String DEFAULT_DATABASE_COMMENT = "Default Hive database";
+
   /**
    * printStackTrace
    *
@@ -324,135 +325,6 @@ public class MetaStoreUtils {
     return "map<" + k + "," + v + ">";
   }
 
-  public static Table getTable(Configuration conf, Properties schema)
-      throws MetaException {
-    Table t = new Table();
-    t.setSd(new StorageDescriptor());
-    t
-        .setTableName(schema
-            .getProperty(org.apache.hadoop.hive.metastore.api.Constants.META_TABLE_NAME));
-    t
-        .getSd()
-        .setLocation(
-            schema
-                .getProperty(org.apache.hadoop.hive.metastore.api.Constants.META_TABLE_LOCATION));
-    t.getSd().setInputFormat(
-      schema.getProperty(
-        org.apache.hadoop.hive.metastore.api.Constants.FILE_INPUT_FORMAT,
-        org.apache.hadoop.mapred.SequenceFileInputFormat.class.getName()));
-    t.getSd().setOutputFormat(
-      schema.getProperty(
-        org.apache.hadoop.hive.metastore.api.Constants.FILE_OUTPUT_FORMAT,
-        org.apache.hadoop.mapred.SequenceFileOutputFormat.class.getName()));
-    t.setPartitionKeys(new ArrayList<FieldSchema>());
-    t.setDbName(MetaStoreUtils.DEFAULT_DATABASE_NAME);
-    String part_cols_str = schema
-        .getProperty(org.apache.hadoop.hive.metastore.api.Constants.META_TABLE_PARTITION_COLUMNS);
-    t.setPartitionKeys(new ArrayList<FieldSchema>());
-    if (part_cols_str != null && (part_cols_str.trim().length() != 0)) {
-      String[] part_keys = part_cols_str.trim().split("/");
-      for (String key : part_keys) {
-        FieldSchema part = new FieldSchema();
-        part.setName(key);
-        part.setType(org.apache.hadoop.hive.serde.Constants.STRING_TYPE_NAME); // default
-                                                                               // partition
-                                                                               // key
-        t.getPartitionKeys().add(part);
-      }
-    }
-    t.getSd()
-        .setNumBuckets(
-            Integer.parseInt(schema.getProperty(
-                org.apache.hadoop.hive.metastore.api.Constants.BUCKET_COUNT,
-                "-1")));
-    String bucketFieldName = schema
-        .getProperty(org.apache.hadoop.hive.metastore.api.Constants.BUCKET_FIELD_NAME);
-    t.getSd().setBucketCols(new ArrayList<String>(1));
-    if ((bucketFieldName != null) && (bucketFieldName.trim().length() != 0)) {
-      t.getSd().setBucketCols(new ArrayList<String>(1));
-      t.getSd().getBucketCols().add(bucketFieldName);
-    }
-
-    t.getSd().setSerdeInfo(new SerDeInfo());
-    t.getSd().getSerdeInfo().setParameters(new HashMap<String, String>());
-    t.getSd().getSerdeInfo().setName(t.getTableName());
-    t
-        .getSd()
-        .getSerdeInfo()
-        .setSerializationLib(
-            schema
-                .getProperty(org.apache.hadoop.hive.serde.Constants.SERIALIZATION_LIB));
-    setSerdeParam(t.getSd().getSerdeInfo(), schema,
-        org.apache.hadoop.hive.serde.Constants.SERIALIZATION_CLASS);
-    setSerdeParam(t.getSd().getSerdeInfo(), schema,
-        org.apache.hadoop.hive.serde.Constants.SERIALIZATION_FORMAT);
-    if (org.apache.commons.lang.StringUtils
-        .isNotBlank(schema
-            .getProperty(org.apache.hadoop.hive.serde.Constants.SERIALIZATION_CLASS))) {
-      setSerdeParam(t.getSd().getSerdeInfo(), schema,
-          org.apache.hadoop.hive.metastore.api.Constants.META_TABLE_SERDE);
-    }
-    // needed for MetadataTypedColumnSetSerDe and LazySimpleSerDe
-    setSerdeParam(t.getSd().getSerdeInfo(), schema,
-        org.apache.hadoop.hive.metastore.api.Constants.META_TABLE_COLUMNS);
-    // needed for LazySimpleSerDe
-    setSerdeParam(t.getSd().getSerdeInfo(), schema,
-        org.apache.hadoop.hive.metastore.api.Constants.META_TABLE_COLUMN_TYPES);
-    // needed for DynamicSerDe
-    setSerdeParam(t.getSd().getSerdeInfo(), schema,
-        org.apache.hadoop.hive.serde.Constants.SERIALIZATION_DDL);
-
-    String colstr = schema
-        .getProperty(org.apache.hadoop.hive.metastore.api.Constants.META_TABLE_COLUMNS);
-    List<FieldSchema> fields = new ArrayList<FieldSchema>();
-    if (colstr != null) {
-      String[] cols = colstr.split(",");
-      for (String colName : cols) {
-        FieldSchema col = new FieldSchema(colName,
-            org.apache.hadoop.hive.serde.Constants.STRING_TYPE_NAME,
-            "'default'");
-        fields.add(col);
-      }
-    }
-
-    if (fields.size() == 0) {
-      // get the fields from serde
-      try {
-        fields = getFieldsFromDeserializer(t.getTableName(), getDeserializer(
-            conf, schema));
-      } catch (SerDeException e) {
-        LOG.error(StringUtils.stringifyException(e));
-        throw new MetaException("Invalid serde or schema. " + e.getMessage());
-      }
-    }
-    t.getSd().setCols(fields);
-
-    t.setOwner(schema.getProperty("creator"));
-
-    // remove all the used up parameters to find out the remaining parameters
-    schema.remove(Constants.META_TABLE_NAME);
-    schema.remove(Constants.META_TABLE_LOCATION);
-    schema.remove(Constants.FILE_INPUT_FORMAT);
-    schema.remove(Constants.FILE_OUTPUT_FORMAT);
-    schema.remove(Constants.META_TABLE_PARTITION_COLUMNS);
-    schema.remove(Constants.BUCKET_COUNT);
-    schema.remove(Constants.BUCKET_FIELD_NAME);
-    schema.remove(org.apache.hadoop.hive.serde.Constants.SERIALIZATION_CLASS);
-    schema.remove(org.apache.hadoop.hive.serde.Constants.SERIALIZATION_FORMAT);
-    schema.remove(org.apache.hadoop.hive.serde.Constants.SERIALIZATION_LIB);
-    schema.remove(Constants.META_TABLE_SERDE);
-    schema.remove(Constants.META_TABLE_COLUMNS);
-    schema.remove(Constants.META_TABLE_COLUMN_TYPES);
-
-    // add the remaining unknown parameters to the table's parameters
-    t.setParameters(new HashMap<String, String>());
-    for (Entry<Object, Object> e : schema.entrySet()) {
-      t.getParameters().put(e.getKey().toString(), e.getValue().toString());
-    }
-
-    return t;
-  }
-
   public static void setSerdeParam(SerDeInfo sdi, Properties schema,
       String param) {
     String val = schema.getProperty(param);
@@ -883,7 +755,7 @@ public class MetaStoreUtils {
     }
     return true;
   }
-  
+
   public static String getIndexTableName(String dbName, String baseTblName, String indexName) {
     return dbName + "__" + baseTblName + "_" + indexName + "__";
   }
@@ -894,5 +766,5 @@ public class MetaStoreUtils {
     }
     return TableType.INDEX_TABLE.toString().equals(table.getTableType());
   }
-  
+
 }

Modified: hadoop/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java
URL: http://svn.apache.org/viewvc/hadoop/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java?rev=990026&r1=990025&r2=990026&view=diff
==============================================================================
--- hadoop/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java (original)
+++ hadoop/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java Fri Aug 27 05:41:43 2010
@@ -286,126 +286,133 @@ public class ObjectStore implements RawS
     }
   }
 
-  public boolean createDatabase(Database db) {
-    boolean success = false;
+  public void createDatabase(Database db) throws InvalidObjectException, MetaException {
     boolean commited = false;
-    MDatabase mdb = new MDatabase(db.getName().toLowerCase(), db
-        .getDescription());
+    MDatabase mdb = new MDatabase();
+    mdb.setName(db.getName().toLowerCase());
+    mdb.setLocationUri(db.getLocationUri());
+    mdb.setDescription(db.getDescription());
     try {
       openTransaction();
       pm.makePersistent(mdb);
-      success = true;
       commited = commitTransaction();
     } finally {
       if (!commited) {
         rollbackTransaction();
       }
     }
-    return success;
-  }
-
-  public boolean createDatabase(String name) {
-    // TODO: get default path
-    Database db = new Database(name, "default_path");
-    return this.createDatabase(db);
   }
 
   @SuppressWarnings("nls")
   private MDatabase getMDatabase(String name) throws NoSuchObjectException {
-    MDatabase db = null;
+    MDatabase mdb = null;
     boolean commited = false;
     try {
       openTransaction();
-      name = name.toLowerCase();
+      name = name.toLowerCase().trim();
       Query query = pm.newQuery(MDatabase.class, "name == dbname");
       query.declareParameters("java.lang.String dbname");
       query.setUnique(true);
-      db = (MDatabase) query.execute(name.trim());
-      pm.retrieve(db);
+      mdb = (MDatabase) query.execute(name);
+      pm.retrieve(mdb);
       commited = commitTransaction();
     } finally {
       if (!commited) {
         rollbackTransaction();
       }
     }
-    if (db == null) {
+    if (mdb == null) {
       throw new NoSuchObjectException("There is no database named " + name);
     }
-    return db;
+    return mdb;
   }
 
   public Database getDatabase(String name) throws NoSuchObjectException {
-    MDatabase db = null;
+    MDatabase mdb = null;
     boolean commited = false;
     try {
       openTransaction();
-      db = getMDatabase(name);
+      mdb = getMDatabase(name);
       commited = commitTransaction();
     } finally {
       if (!commited) {
         rollbackTransaction();
       }
     }
-    return new Database(db.getName(), db.getDescription());
+    Database db = new Database();
+    db.setName(mdb.getName());
+    db.setDescription(mdb.getDescription());
+    db.setLocationUri(mdb.getLocationUri());
+    return db;
   }
 
-  public boolean dropDatabase(String dbname) {
-
+  public boolean dropDatabase(String dbname) throws NoSuchObjectException, MetaException {
     boolean success = false;
-    boolean commited = false;
+    LOG.info("Dropping database " + dbname + " along with all tables");
+    dbname = dbname.toLowerCase();
     try {
       openTransaction();
 
       // first drop tables
-      dbname = dbname.toLowerCase();
-      LOG.info("Dropping database along with all tables " + dbname);
-      Query q1 = pm.newQuery(MTable.class, "database.name == dbName");
-      q1.declareParameters("java.lang.String dbName");
-      List<MTable> mtbls = (List<MTable>) q1.execute(dbname.trim());
-      pm.deletePersistentAll(mtbls);
+      for (String tableName : getAllTables(dbname)) {
+        dropTable(dbname, tableName);
+      }
 
       // then drop the database
-      Query query = pm.newQuery(MDatabase.class, "name == dbName");
-      query.declareParameters("java.lang.String dbName");
-      query.setUnique(true);
-      MDatabase db = (MDatabase) query.execute(dbname.trim());
+      MDatabase db = getMDatabase(dbname);
       pm.retrieve(db);
-
-      // StringIdentity id = new StringIdentity(MDatabase.class, dbname);
-      // MDatabase db = (MDatabase) pm.getObjectById(id);
       if (db != null) {
         pm.deletePersistent(db);
       }
-      commited = commitTransaction();
-      success = true;
-    } catch (JDOObjectNotFoundException e) {
-      LOG.debug("database not found " + dbname, e);
-      commited = commitTransaction();
+      success = commitTransaction();
     } finally {
-      if (!commited) {
+      if (!success) {
         rollbackTransaction();
       }
     }
     return success;
   }
 
-  public List<String> getDatabases() {
-    List dbs = null;
+
+  public List<String> getDatabases(String pattern) throws MetaException {
     boolean commited = false;
+    List<String> databases = null;
     try {
       openTransaction();
-      Query query = pm.newQuery(MDatabase.class);
-      query.setResult("name");
-      query.setResultClass(String.class);
-      query.setOrdering("name asc");
-      dbs = (List) query.execute();
+      // Take the pattern and split it on the | to get all the composing
+      // patterns
+      String[] subpatterns = pattern.trim().split("\\|");
+      String query = "select name from org.apache.hadoop.hive.metastore.model.MDatabase where (";
+      boolean first = true;
+      for (String subpattern : subpatterns) {
+        subpattern = "(?i)" + subpattern.replaceAll("\\*", ".*");
+        if (!first) {
+          query = query + " || ";
+        }
+        query = query + " name.matches(\"" + subpattern + "\")";
+        first = false;
+      }
+      query = query + ")";
+
+      Query q = pm.newQuery(query);
+      q.setResult("name");
+      q.setOrdering("name ascending");
+      Collection names = (Collection) q.execute();
+      databases = new ArrayList<String>();
+      for (Iterator i = names.iterator(); i.hasNext();) {
+        databases.add((String) i.next());
+      }
       commited = commitTransaction();
     } finally {
       if (!commited) {
         rollbackTransaction();
       }
     }
-    return dbs;
+    return databases;
+  }
+
+  public List<String> getAllDatabases() throws MetaException {
+    return getDatabases(".*");
   }
 
   private MType getMType(Type type) {
@@ -470,7 +477,6 @@ public class ObjectStore implements RawS
   }
 
   public boolean dropType(String typeName) {
-
     boolean success = false;
     boolean commited = false;
     try {
@@ -494,8 +500,7 @@ public class ObjectStore implements RawS
     return success;
   }
 
-  public void createTable(Table tbl) throws InvalidObjectException,
-      MetaException {
+  public void createTable(Table tbl) throws InvalidObjectException, MetaException {
     boolean commited = false;
     try {
       openTransaction();
@@ -509,8 +514,7 @@ public class ObjectStore implements RawS
     }
   }
 
-  public boolean dropTable(String dbName, String tableName) {
-
+  public boolean dropTable(String dbName, String tableName) throws MetaException {
     boolean success = false;
     try {
       openTransaction();
@@ -552,11 +556,13 @@ public class ObjectStore implements RawS
     List<String> tbls = null;
     try {
       openTransaction();
-      dbName = dbName.toLowerCase();
+      dbName = dbName.toLowerCase().trim();
       // Take the pattern and split it on the | to get all the composing
       // patterns
       String[] subpatterns = pattern.trim().split("\\|");
-      String query = "select tableName from org.apache.hadoop.hive.metastore.model.MTable where database.name == dbName && (";
+      String query =
+        "select tableName from org.apache.hadoop.hive.metastore.model.MTable "
+        + "where database.name == dbName && (";
       boolean first = true;
       for (String subpattern : subpatterns) {
         subpattern = "(?i)" + subpattern.replaceAll("\\*", ".*");
@@ -571,7 +577,8 @@ public class ObjectStore implements RawS
       Query q = pm.newQuery(query);
       q.declareParameters("java.lang.String dbName");
       q.setResult("tableName");
-      Collection names = (Collection) q.execute(dbName.trim());
+      q.setOrdering("tableName ascending");
+      Collection names = (Collection) q.execute(dbName);
       tbls = new ArrayList<String>();
       for (Iterator i = names.iterator(); i.hasNext();) {
         tbls.add((String) i.next());
@@ -585,18 +592,21 @@ public class ObjectStore implements RawS
     return tbls;
   }
 
+  public List<String> getAllTables(String dbName) throws MetaException {
+    return getTables(dbName, ".*");
+  }
+
   private MTable getMTable(String db, String table) {
     MTable mtbl = null;
     boolean commited = false;
     try {
       openTransaction();
-      db = db.toLowerCase();
-      table = table.toLowerCase();
-      Query query = pm.newQuery(MTable.class,
-          "tableName == table && database.name == db");
+      db = db.toLowerCase().trim();
+      table = table.toLowerCase().trim();
+      Query query = pm.newQuery(MTable.class, "tableName == table && database.name == db");
       query.declareParameters("java.lang.String table, java.lang.String db");
       query.setUnique(true);
-      mtbl = (MTable) query.execute(table.trim(), db.trim());
+      mtbl = (MTable) query.execute(table, db);
       pm.retrieve(mtbl);
       commited = commitTransaction();
     } finally {
@@ -641,7 +651,7 @@ public class ObjectStore implements RawS
     } catch (NoSuchObjectException e) {
       LOG.error(StringUtils.stringifyException(e));
       throw new InvalidObjectException("Database " + tbl.getDbName()
-          + " doesn't exsit.");
+          + " doesn't exist.");
     }
 
     // If the table has property EXTERNAL set, update table type
@@ -790,8 +800,8 @@ public class ObjectStore implements RawS
     boolean commited = false;
     try {
       openTransaction();
-      dbName = dbName.toLowerCase();
-      tableName = tableName.toLowerCase();
+      dbName = dbName.toLowerCase().trim();
+      tableName = tableName.toLowerCase().trim();
       MTable mtbl = getMTable(dbName, tableName);
       if (mtbl == null) {
         commited = commitTransaction();
@@ -801,13 +811,11 @@ public class ObjectStore implements RawS
       // redundant
       String name = Warehouse.makePartName(convertToFieldSchemas(mtbl
           .getPartitionKeys()), part_vals);
-      Query query = pm
-          .newQuery(MPartition.class,
-              "table.tableName == t1 && table.database.name == t2 && partitionName == t3");
-      query
-          .declareParameters("java.lang.String t1, java.lang.String t2, java.lang.String t3");
+      Query query = pm.newQuery(MPartition.class,
+          "table.tableName == t1 && table.database.name == t2 && partitionName == t3");
+      query.declareParameters("java.lang.String t1, java.lang.String t2, java.lang.String t3");
       query.setUnique(true);
-      mpart = (MPartition) query.execute(tableName.trim(), dbName.trim(), name);
+      mpart = (MPartition) query.execute(tableName, dbName, name);
       pm.retrieve(mpart);
       commited = commitTransaction();
     } finally {
@@ -888,14 +896,15 @@ public class ObjectStore implements RawS
     try {
       openTransaction();
       LOG.debug("Executing getPartitionNames");
-      dbName = dbName.toLowerCase();
-      tableName = tableName.toLowerCase();
-      Query q = pm
-          .newQuery("select partitionName from org.apache.hadoop.hive.metastore.model.MPartition where table.database.name == t1 && table.tableName == t2 order by partitionName asc");
+      dbName = dbName.toLowerCase().trim();
+      tableName = tableName.toLowerCase().trim();
+      Query q = pm.newQuery(
+          "select partitionName from org.apache.hadoop.hive.metastore.model.MPartition "
+          + "where table.database.name == t1 && table.tableName == t2 "
+          + "order by partitionName asc");
       q.declareParameters("java.lang.String t1, java.lang.String t2");
       q.setResult("partitionName");
-      Collection names = (Collection) q
-          .execute(dbName.trim(), tableName.trim());
+      Collection names = (Collection) q.execute(dbName, tableName);
       pns = new ArrayList<String>();
       for (Iterator i = names.iterator(); i.hasNext();) {
         pns.add((String) i.next());
@@ -917,13 +926,12 @@ public class ObjectStore implements RawS
     try {
       openTransaction();
       LOG.debug("Executing listMPartitions");
-      dbName = dbName.toLowerCase();
-      tableName = tableName.toLowerCase();
+      dbName = dbName.toLowerCase().trim();
+      tableName = tableName.toLowerCase().trim();
       Query query = pm.newQuery(MPartition.class,
           "table.tableName == t1 && table.database.name == t2");
       query.declareParameters("java.lang.String t1, java.lang.String t2");
-      mparts = (List<MPartition>) query
-          .execute(tableName.trim(), dbName.trim());
+      mparts = (List<MPartition>) query.execute(tableName, dbName);
       LOG.debug("Done executing query for listMPartitions");
       pm.retrieveAll(mparts);
       success = commitTransaction();
@@ -1077,27 +1085,25 @@ public class ObjectStore implements RawS
     }
     return success;
   }
-  
+
   private MIndex getMIndex(String dbName, String originalTblName, String indexName) throws MetaException {
     MIndex midx = null;
     boolean commited = false;
     try {
       openTransaction();
-      dbName = dbName.toLowerCase();
-      originalTblName = originalTblName.toLowerCase();
+      dbName = dbName.toLowerCase().trim();
+      originalTblName = originalTblName.toLowerCase().trim();
       MTable mtbl = getMTable(dbName, originalTblName);
       if (mtbl == null) {
         commited = commitTransaction();
         return null;
       }
 
-      Query query = pm
-          .newQuery(MIndex.class,
-              "origTable.tableName == t1 && origTable.database.name == t2 && indexName == t3");
-      query
-          .declareParameters("java.lang.String t1, java.lang.String t2, java.lang.String t3");
+      Query query = pm.newQuery(MIndex.class,
+        "origTable.tableName == t1 && origTable.database.name == t2 && indexName == t3");
+      query.declareParameters("java.lang.String t1, java.lang.String t2, java.lang.String t3");
       query.setUnique(true);
-      midx = (MIndex) query.execute(originalTblName.trim(), dbName.trim(), indexName);
+      midx = (MIndex) query.execute(originalTblName, dbName, indexName);
       pm.retrieve(midx);
       commited = commitTransaction();
     } finally {
@@ -1126,7 +1132,7 @@ public class ObjectStore implements RawS
     return new Index(
     mIndex.getIndexName(),
     mIndex.getIndexHandlerClass(),
-    MetaStoreUtils.DEFAULT_DATABASE_NAME,
+    mIndex.getOrigTable().getDatabase().getName(),
     mIndex.getOrigTable().getTableName(),
     mIndex.getCreateTime(),
     mIndex.getLastAccessTime(),
@@ -1156,7 +1162,7 @@ public class ObjectStore implements RawS
       }
     }
   }
-  
+
   private List<MIndex> listMIndexes(String dbName, String origTableName,
       int max) {
     boolean success = false;
@@ -1164,13 +1170,12 @@ public class ObjectStore implements RawS
     try {
       openTransaction();
       LOG.debug("Executing listMIndexes");
-      dbName = dbName.toLowerCase();
-      origTableName = origTableName.toLowerCase();
+      dbName = dbName.toLowerCase().trim();
+      origTableName = origTableName.toLowerCase().trim();
       Query query = pm.newQuery(MIndex.class,
           "origTable.tableName == t1 && origTable.database.name == t2");
       query.declareParameters("java.lang.String t1, java.lang.String t2");
-      mindexes = (List<MIndex>) query
-          .execute(origTableName.trim(), dbName.trim());
+      mindexes = (List<MIndex>) query.execute(origTableName, dbName);
       LOG.debug("Done executing query for listMIndexes");
       pm.retrieveAll(mindexes);
       success = commitTransaction();
@@ -1191,14 +1196,15 @@ public class ObjectStore implements RawS
     try {
       openTransaction();
       LOG.debug("Executing listIndexNames");
-      dbName = dbName.toLowerCase();
-      origTableName = origTableName.toLowerCase();
-      Query q = pm
-          .newQuery("select indexName from org.apache.hadoop.hive.metastore.model.MIndex where origTable.database.name == t1 && origTable.tableName == t2 order by indexName asc");
+      dbName = dbName.toLowerCase().trim();
+      origTableName = origTableName.toLowerCase().trim();
+      Query q = pm.newQuery(
+          "select indexName from org.apache.hadoop.hive.metastore.model.MIndex "
+          + "where origTable.database.name == t1 && origTable.tableName == t2 "
+          + "order by indexName asc");
       q.declareParameters("java.lang.String t1, java.lang.String t2");
       q.setResult("indexName");
-      Collection names = (Collection) q
-          .execute(dbName.trim(), origTableName.trim());
+      Collection names = (Collection) q.execute(dbName, origTableName);
       pns = new ArrayList<String>();
       for (Iterator i = names.iterator(); i.hasNext();) {
         pns.add((String) i.next());

Modified: hadoop/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/RawStore.java
URL: http://svn.apache.org/viewvc/hadoop/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/RawStore.java?rev=990026&r1=990025&r2=990026&view=diff
==============================================================================
--- hadoop/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/RawStore.java (original)
+++ hadoop/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/RawStore.java Fri Aug 27 05:41:43 2010
@@ -37,7 +37,7 @@ public interface RawStore extends Config
   /**
    * Opens a new one or the one already created Every call of this function must
    * have corresponding commit or rollback function call
-   * 
+   *
    * @return an active transaction
    */
 
@@ -46,7 +46,7 @@ public interface RawStore extends Config
   /**
    * if this is the commit of the first open call then an actual commit is
    * called.
-   * 
+   *
    * @return true or false
    */
   public abstract boolean commitTransaction();
@@ -56,16 +56,17 @@ public interface RawStore extends Config
    */
   public abstract void rollbackTransaction();
 
-  public abstract boolean createDatabase(Database db) throws MetaException;
-
-  public abstract boolean createDatabase(String name) throws MetaException;
+  public abstract void createDatabase(Database db)
+      throws InvalidObjectException, MetaException;
 
   public abstract Database getDatabase(String name)
       throws NoSuchObjectException;
 
-  public abstract boolean dropDatabase(String dbname);
+  public abstract boolean dropDatabase(String dbname) throws NoSuchObjectException, MetaException;
 
-  public abstract List<String> getDatabases() throws MetaException;
+  public abstract List<String> getDatabases(String pattern) throws MetaException;
+
+  public abstract List<String> getAllDatabases() throws MetaException;
 
   public abstract boolean createType(Type type);
 
@@ -100,23 +101,25 @@ public interface RawStore extends Config
   public List<String> getTables(String dbName, String pattern)
       throws MetaException;
 
+  public List<String> getAllTables(String dbName) throws MetaException;
+
   public abstract List<String> listPartitionNames(String db_name,
       String tbl_name, short max_parts) throws MetaException;
 
   public abstract void alterPartition(String db_name, String tbl_name,
       Partition new_part) throws InvalidObjectException, MetaException;
-  
+
   public abstract boolean addIndex(Index index)
       throws InvalidObjectException, MetaException;
-  
+
   public abstract Index getIndex(String dbName, String origTableName, String indexName) throws MetaException;
 
   public abstract boolean dropIndex(String dbName, String origTableName, String indexName) throws MetaException;
 
   public abstract List<Index> getIndexes(String dbName,
       String origTableName, int max) throws MetaException;
-  
+
   public abstract List<String> listIndexNames(String dbName,
       String origTableName, short max) throws MetaException;
-  
+
 }

Modified: hadoop/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/Warehouse.java
URL: http://svn.apache.org/viewvc/hadoop/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/Warehouse.java?rev=990026&r1=990025&r2=990026&view=diff
==============================================================================
--- hadoop/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/Warehouse.java (original)
+++ hadoop/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/Warehouse.java Fri Aug 27 05:41:43 2010
@@ -18,6 +18,8 @@
 
 package org.apache.hadoop.hive.metastore;
 
+import static org.apache.hadoop.hive.metastore.MetaStoreUtils.DEFAULT_DATABASE_NAME;
+
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.util.ArrayList;
@@ -47,7 +49,9 @@ import org.apache.hadoop.hive.metastore.
 public class Warehouse {
   private Path whRoot;
   private final Configuration conf;
-  String whRootString;
+  private final String whRootString;
+
+  private static final String DATABASE_WAREHOUSE_SUFFIX = ".db";
 
   public static final Log LOG = LogFactory.getLog("hive.metastore.warehouse");
 
@@ -117,10 +121,10 @@ public class Warehouse {
   }
 
   public Path getDefaultDatabasePath(String dbName) throws MetaException {
-    if (dbName.equalsIgnoreCase(MetaStoreUtils.DEFAULT_DATABASE_NAME)) {
+    if (dbName.equalsIgnoreCase(DEFAULT_DATABASE_NAME)) {
       return getWhRoot();
     }
-    return new Path(getWhRoot(), dbName.toLowerCase() + ".db");
+    return new Path(getWhRoot(), dbName.toLowerCase() + DATABASE_WAREHOUSE_SUFFIX);
   }
 
   public Path getDefaultTablePath(String dbName, String tableName)
@@ -328,7 +332,7 @@ public class Warehouse {
     }
     return FileUtils.makePartName(colNames, vals);
   }
-  
+
   public static List<String> getPartValuesFromPartName(String partName)
       throws MetaException {
     LinkedHashMap<String, String> partSpec = Warehouse.makeSpecFromName(partName);

Modified: hadoop/hive/trunk/metastore/src/model/org/apache/hadoop/hive/metastore/model/MDatabase.java
URL: http://svn.apache.org/viewvc/hadoop/hive/trunk/metastore/src/model/org/apache/hadoop/hive/metastore/model/MDatabase.java?rev=990026&r1=990025&r2=990026&view=diff
==============================================================================
--- hadoop/hive/trunk/metastore/src/model/org/apache/hadoop/hive/metastore/model/MDatabase.java (original)
+++ hadoop/hive/trunk/metastore/src/model/org/apache/hadoop/hive/metastore/model/MDatabase.java Fri Aug 27 05:41:43 2010
@@ -27,6 +27,7 @@ package org.apache.hadoop.hive.metastore
  */
 public class MDatabase {
   private String name;
+  private String locationUri;
   private String description;
 
   /**
@@ -37,11 +38,13 @@ public class MDatabase {
   /**
    * To create a database object
    * @param name of the database
-   * @param location future use
+   * @param locationUri Location of the database in the warehouse
+   * @param description Comment describing the database
    */
-  public MDatabase(String name, String location) {
+  public MDatabase(String name, String locationUri, String description) {
     this.name = name;
-    this.description = location;
+    this.locationUri = locationUri;
+    this.description = description;
   }
 
   /**
@@ -59,6 +62,20 @@ public class MDatabase {
   }
 
   /**
+   * @return the location_uri
+   */
+  public String getLocationUri() {
+    return locationUri;
+  }
+
+  /**
+   * @param locationUri the locationUri to set
+   */
+  public void setLocationUri(String locationUri) {
+    this.locationUri = locationUri;
+  }
+
+  /**
    * @return the description
    */
   public String getDescription() {
@@ -71,5 +88,4 @@ public class MDatabase {
   public void setDescription(String description) {
     this.description = description;
   }
-
 }

Modified: hadoop/hive/trunk/metastore/src/model/package.jdo
URL: http://svn.apache.org/viewvc/hadoop/hive/trunk/metastore/src/model/package.jdo?rev=990026&r1=990025&r2=990026&view=diff
==============================================================================
--- hadoop/hive/trunk/metastore/src/model/package.jdo (original)
+++ hadoop/hive/trunk/metastore/src/model/package.jdo Fri Aug 27 05:41:43 2010
@@ -8,12 +8,15 @@
         <column name="DB_ID"/>
       </datastore-identity>
       <field name="name">  
-        <column name="NAME" length="128" jdbc-type="VARCHAR"/>  
+        <column name="NAME" length="128" jdbc-type="VARCHAR"/>
         <index name="UniqueDatabase" unique="true"/>
       </field>
-      <field name="description">  
-        <column name="DESC" length="767" jdbc-type="VARCHAR"/>  
-      </field>  
+      <field name="description">
+        <column name="DESC" length="4000" jdbc-type="VARCHAR" allows-null="true"/>
+      </field>
+      <field name="locationUri">
+        <column name="DB_LOCATION_URI" length="4000" jdbc-type="VARCHAR" allows-null="false"/>
+      </field>
     </class>
 
     <class name="MFieldSchema" embedded-only="true" table="TYPE_FIELDS" detachable="true">

Added: hadoop/hive/trunk/metastore/src/test/org/apache/hadoop/hive/metastore/TestEmbeddedHiveMetaStore.java
URL: http://svn.apache.org/viewvc/hadoop/hive/trunk/metastore/src/test/org/apache/hadoop/hive/metastore/TestEmbeddedHiveMetaStore.java?rev=990026&view=auto
==============================================================================
--- hadoop/hive/trunk/metastore/src/test/org/apache/hadoop/hive/metastore/TestEmbeddedHiveMetaStore.java (added)
+++ hadoop/hive/trunk/metastore/src/test/org/apache/hadoop/hive/metastore/TestEmbeddedHiveMetaStore.java Fri Aug 27 05:41:43 2010
@@ -0,0 +1,49 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hive.metastore;
+
+import org.apache.hadoop.util.StringUtils;
+
+public class TestEmbeddedHiveMetaStore extends TestHiveMetaStore {
+
+  @Override
+  protected void setUp() throws Exception {
+    super.setUp();
+
+    try {
+      client = new HiveMetaStoreClient(hiveConf, null);
+    } catch (Throwable e) {
+      System.err.println("Unable to open the metastore");
+      System.err.println(StringUtils.stringifyException(e));
+      throw new Exception(e);
+    }
+  }
+
+  @Override
+  protected void tearDown() throws Exception {
+    try {
+      super.tearDown();
+      client.close();
+    } catch (Throwable e) {
+      System.err.println("Unable to close metastore");
+      System.err.println(StringUtils.stringifyException(e));
+      throw new Exception(e);
+    }
+  }
+}

Modified: hadoop/hive/trunk/metastore/src/test/org/apache/hadoop/hive/metastore/TestHiveMetaStore.java
URL: http://svn.apache.org/viewvc/hadoop/hive/trunk/metastore/src/test/org/apache/hadoop/hive/metastore/TestHiveMetaStore.java?rev=990026&r1=990025&r2=990026&view=diff
==============================================================================
--- hadoop/hive/trunk/metastore/src/test/org/apache/hadoop/hive/metastore/TestHiveMetaStore.java (original)
+++ hadoop/hive/trunk/metastore/src/test/org/apache/hadoop/hive/metastore/TestHiveMetaStore.java Fri Aug 27 05:41:43 2010
@@ -48,40 +48,25 @@ import org.apache.hadoop.hive.serde.Cons
 import org.apache.hadoop.util.StringUtils;
 import org.apache.thrift.TException;
 
-public class TestHiveMetaStore extends TestCase {
-  private HiveMetaStoreClient client;
-  private HiveConf hiveConf;
+public abstract class TestHiveMetaStore extends TestCase {
+  protected static HiveMetaStoreClient client;
+  protected static HiveConf hiveConf;
+  protected static Warehouse warehouse;
+  protected static boolean isThriftClient = false;
+
+  private static final String TEST_DB1_NAME = "testdb1";
+  private static final String TEST_DB2_NAME = "testdb2";
 
   @Override
   protected void setUp() throws Exception {
-    super.setUp();
     hiveConf = new HiveConf(this.getClass());
+    warehouse = new Warehouse(hiveConf);
 
     // set some values to use for getting conf. vars
     hiveConf.set("hive.key1", "value1");
     hiveConf.set("hive.key2", "http://www.example.com");
     hiveConf.set("hive.key3", "");
     hiveConf.set("hive.key4", "0");
-
-    try {
-      client = new HiveMetaStoreClient(hiveConf, null);
-    } catch (Throwable e) {
-      System.err.println("Unable to open the metastore");
-      System.err.println(StringUtils.stringifyException(e));
-      throw new Exception(e);
-    }
-  }
-
-  @Override
-  protected void tearDown() throws Exception {
-    try {
-      super.tearDown();
-      client.close();
-    } catch (Throwable e) {
-      System.err.println("Unable to close metastore");
-      System.err.println(StringUtils.stringifyException(e));
-      throw new Exception(e);
-    }
   }
 
   public void testNameMethods() {
@@ -118,11 +103,11 @@ public class TestHiveMetaStore extends T
    * @throws Exception
    */
   public void testPartition() throws Exception {
-    partitionTester(client, hiveConf, false);
+    partitionTester(client, hiveConf);
   }
 
-  public static void partitionTester(HiveMetaStoreClient client, HiveConf hiveConf,
-      boolean isThriftClient) throws Exception {
+  public static void partitionTester(HiveMetaStoreClient client, HiveConf hiveConf)
+    throws Exception {
     try {
       String dbName = "compdb";
       String tblName = "comptbl";
@@ -139,9 +124,10 @@ public class TestHiveMetaStore extends T
       vals3.add("15");
 
       client.dropTable(dbName, tblName);
-      client.dropDatabase(dbName);
-      boolean ret = client.createDatabase(dbName, "strange_loc");
-      assertTrue("Unable to create the databse " + dbName, ret);
+      silentDropDatabase(dbName);
+      Database db = new Database();
+      db.setName(dbName);
+      client.createDatabase(db);
 
       client.dropType(typeName);
       Type typ1 = new Type();
@@ -151,8 +137,7 @@ public class TestHiveMetaStore extends T
           new FieldSchema("name", Constants.STRING_TYPE_NAME, ""));
       typ1.getFields().add(
           new FieldSchema("income", Constants.INT_TYPE_NAME, ""));
-      ret = client.createType(typ1);
-      assertTrue("Unable to create type " + typeName, ret);
+      client.createType(typ1);
 
       Table tbl = new Table();
       tbl.setDbName(dbName);
@@ -181,7 +166,7 @@ public class TestHiveMetaStore extends T
 
       client.createTable(tbl);
 
-      if(isThriftClient) {
+      if (isThriftClient) {
         // the createTable() above does not update the location in the 'tbl'
         // object when the client is a thrift client and the code below relies
         // on the location being present in the 'tbl' object - so get the table
@@ -306,9 +291,9 @@ public class TestHiveMetaStore extends T
       Path partPath = new Path(part2.getSd().getLocation());
       FileSystem fs = FileSystem.get(partPath.toUri(), hiveConf);
 
+
       assertTrue(fs.exists(partPath));
-      ret = client.dropPartition(dbName, tblName, part.getValues(), true);
-      assertTrue(ret);
+      client.dropPartition(dbName, tblName, part.getValues(), true);
       assertFalse(fs.exists(partPath));
 
       // Test append_partition_by_name
@@ -326,12 +311,11 @@ public class TestHiveMetaStore extends T
       // add the partition again so that drop table with a partition can be
       // tested
       retp = client.add_partition(part);
-      assertNotNull("Unable to create partition " + part, ret);
+      assertNotNull("Unable to create partition " + part, retp);
 
       client.dropTable(dbName, tblName);
 
-      ret = client.dropType(typeName);
-      assertTrue("Unable to drop type " + typeName, ret);
+      client.dropType(typeName);
 
       // recreate table as external, drop partition and it should
       // still exist
@@ -343,8 +327,11 @@ public class TestHiveMetaStore extends T
       client.dropPartition(dbName, tblName, part.getValues(), true);
       assertTrue(fs.exists(partPath));
 
-      ret = client.dropDatabase(dbName);
-      assertTrue("Unable to create the databse " + dbName, ret);
+      for (String tableName : client.getTables(dbName, "*")) {
+        client.dropTable(dbName, tableName);
+      }
+
+      client.dropDatabase(dbName);
 
     } catch (Exception e) {
       System.err.println(StringUtils.stringifyException(e));
@@ -363,9 +350,11 @@ public class TestHiveMetaStore extends T
       vals.add("14");
 
       client.dropTable(dbName, tblName);
-      client.dropDatabase(dbName);
-      boolean ret = client.createDatabase(dbName, "strange_loc");
-      assertTrue("Unable to create the databse " + dbName, ret);
+      silentDropDatabase(dbName);
+      Database db = new Database();
+      db.setName(dbName);
+      db.setDescription("Alter Partition Test database");
+      client.createDatabase(db);
 
       ArrayList<FieldSchema> cols = new ArrayList<FieldSchema>(2);
       cols.add(new FieldSchema("name", Constants.STRING_TYPE_NAME, ""));
@@ -398,6 +387,14 @@ public class TestHiveMetaStore extends T
 
       client.createTable(tbl);
 
+      if (isThriftClient) {
+        // the createTable() above does not update the location in the 'tbl'
+        // object when the client is a thrift client and the code below relies
+        // on the location being present in the 'tbl' object - so get the table
+        // from the metastore
+        tbl = client.getTable(dbName, tblName);
+      }
+
       Partition part = new Partition();
       part.setDbName(dbName);
       part.setTableName(tblName);
@@ -426,8 +423,7 @@ public class TestHiveMetaStore extends T
 
       client.dropTable(dbName, tblName);
 
-      ret = client.dropDatabase(dbName);
-      assertTrue("Unable to create the databse " + dbName, ret);
+      client.dropDatabase(dbName);
     } catch (Exception e) {
       System.err.println(StringUtils.stringifyException(e));
       System.err.println("testPartition() failed.");
@@ -438,40 +434,40 @@ public class TestHiveMetaStore extends T
   public void testDatabase() throws Throwable {
     try {
       // clear up any existing databases
-      client.dropDatabase("test1");
-      client.dropDatabase("test2");
+      silentDropDatabase(TEST_DB1_NAME);
+      silentDropDatabase(TEST_DB2_NAME);
 
-      boolean ret = client.createDatabase("test1", "strange_loc");
-      assertTrue("Unable to create the databse", ret);
+      Database db = new Database();
+      db.setName(TEST_DB1_NAME);
+      client.createDatabase(db);
 
-      Database db = client.getDatabase("test1");
+      db = client.getDatabase(TEST_DB1_NAME);
 
       assertEquals("name of returned db is different from that of inserted db",
-          "test1", db.getName());
-      assertEquals(
-          "location of the returned db is different from that of inserted db",
-          "strange_loc", db.getDescription());
-
-      boolean ret2 = client.createDatabase("test2", "another_strange_loc");
-      assertTrue("Unable to create the databse", ret2);
+          TEST_DB1_NAME, db.getName());
+      assertEquals("location of the returned db is different from that of inserted db",
+          warehouse.getDefaultDatabasePath(TEST_DB1_NAME).toString(), db.getLocationUri());
+
+      Database db2 = new Database();
+      db2.setName(TEST_DB2_NAME);
+      client.createDatabase(db2);
 
-      Database db2 = client.getDatabase("test2");
+      db2 = client.getDatabase(TEST_DB2_NAME);
 
       assertEquals("name of returned db is different from that of inserted db",
-          "test2", db2.getName());
-      assertEquals(
-          "location of the returned db is different from that of inserted db",
-          "another_strange_loc", db2.getDescription());
-
-      List<String> dbs = client.getDatabases();
-
-      assertTrue("first database is not test1", dbs.contains("test1"));
-      assertTrue("second database is not test2", dbs.contains("test2"));
-
-      ret = client.dropDatabase("test1");
-      assertTrue("couldn't delete first database", ret);
-      ret = client.dropDatabase("test2");
-      assertTrue("couldn't delete second database", ret);
+          TEST_DB2_NAME, db2.getName());
+      assertEquals("location of the returned db is different from that of inserted db",
+          warehouse.getDefaultDatabasePath(TEST_DB2_NAME).toString(), db2.getLocationUri());
+
+      List<String> dbs = client.getDatabases(".*");
+
+      assertTrue("first database is not " + TEST_DB1_NAME, dbs.contains(TEST_DB1_NAME));
+      assertTrue("second database is not " + TEST_DB2_NAME, dbs.contains(TEST_DB2_NAME));
+
+      client.dropDatabase(TEST_DB1_NAME);
+      client.dropDatabase(TEST_DB2_NAME);
+      silentDropDatabase(TEST_DB1_NAME);
+      silentDropDatabase(TEST_DB2_NAME);
     } catch (Throwable e) {
       System.err.println(StringUtils.stringifyException(e));
       System.err.println("testDatabase() failed.");
@@ -495,9 +491,13 @@ public class TestHiveMetaStore extends T
       ret = client.dropType(Constants.INT_TYPE_NAME);
       assertTrue("unable to drop type integer", ret);
 
-      Type typ1_3 = null;
-      typ1_3 = client.getType(Constants.INT_TYPE_NAME);
-      assertNull("unable to drop type integer", typ1_3);
+      boolean exceptionThrown = false;
+      try {
+        client.getType(Constants.INT_TYPE_NAME);
+      } catch (NoSuchObjectException e) {
+        exceptionThrown = true;
+      }
+      assertTrue("Expected NoSuchObjectException", exceptionThrown);
     } catch (Exception e) {
       System.err.println(StringUtils.stringifyException(e));
       System.err.println("testSimpleTypeApi() failed.");
@@ -554,9 +554,13 @@ public class TestHiveMetaStore extends T
       ret = client.dropType("Person");
       assertTrue("unable to drop type Person", ret);
 
-      Type typ1_3 = null;
-      typ1_3 = client.getType("Person");
-      assertNull("unable to drop type Person", typ1_3);
+      boolean exceptionThrown = false;
+      try {
+        client.getType("Person");
+      } catch (NoSuchObjectException e) {
+        exceptionThrown = true;
+      }
+      assertTrue("Expected NoSuchObjectException", exceptionThrown);
     } catch (Exception e) {
       System.err.println(StringUtils.stringifyException(e));
       System.err.println("testComplexTypeApi() failed.");
@@ -572,9 +576,11 @@ public class TestHiveMetaStore extends T
       String typeName = "Person";
 
       client.dropTable(dbName, tblName);
-      client.dropDatabase(dbName);
-      boolean ret = client.createDatabase(dbName, "strange_loc");
-      assertTrue("Unable to create the databse " + dbName, ret);
+      silentDropDatabase(dbName);
+
+      Database db = new Database();
+      db.setName(dbName);
+      client.createDatabase(db);
 
       client.dropType(typeName);
       Type typ1 = new Type();
@@ -584,8 +590,7 @@ public class TestHiveMetaStore extends T
           new FieldSchema("name", Constants.STRING_TYPE_NAME, ""));
       typ1.getFields().add(
           new FieldSchema("income", Constants.INT_TYPE_NAME, ""));
-      ret = client.createType(typ1);
-      assertTrue("Unable to create type " + typeName, ret);
+      client.createType(typ1);
 
       Table tbl = new Table();
       tbl.setDbName(dbName);
@@ -610,6 +615,14 @@ public class TestHiveMetaStore extends T
 
       client.createTable(tbl);
 
+      if (isThriftClient) {
+        // the createTable() above does not update the location in the 'tbl'
+        // object when the client is a thrift client and the code below relies
+        // on the location being present in the 'tbl' object - so get the table
+        // from the metastore
+        tbl = client.getTable(dbName, tblName);
+      }
+
       Table tbl2 = client.getTable(dbName, tblName);
       assertNotNull(tbl2);
       assertEquals(tbl2.getDbName(), dbName);
@@ -647,6 +660,9 @@ public class TestHiveMetaStore extends T
       }
 
       client.createTable(tbl2);
+      if (isThriftClient) {
+        tbl2 = client.getTable(tbl2.getDbName(), tbl2.getTableName());
+      }
 
       Table tbl3 = client.getTable(dbName, tblName2);
       assertNotNull(tbl3);
@@ -683,18 +699,15 @@ public class TestHiveMetaStore extends T
           (tbl2.getPartitionKeys() == null)
               || (tbl2.getPartitionKeys().size() == 0));
 
-      FileSystem fs = FileSystem.get((new Path(tbl.getSd().getLocation())).toUri(),
-                                     hiveConf);
+      FileSystem fs = FileSystem.get((new Path(tbl.getSd().getLocation())).toUri(), hiveConf);
       client.dropTable(dbName, tblName);
       assertFalse(fs.exists(new Path(tbl.getSd().getLocation())));
 
       client.dropTable(dbName, tblName2);
       assertTrue(fs.exists(new Path(tbl2.getSd().getLocation())));
 
-      ret = client.dropType(typeName);
-      assertTrue("Unable to drop type " + typeName, ret);
-      ret = client.dropDatabase(dbName);
-      assertTrue("Unable to drop databse " + dbName, ret);
+      client.dropType(typeName);
+      client.dropDatabase(dbName);
     } catch (Exception e) {
       System.err.println(StringUtils.stringifyException(e));
       System.err.println("testSimpleTable() failed.");
@@ -703,15 +716,17 @@ public class TestHiveMetaStore extends T
   }
 
   public void testAlterTable() throws Exception {
-    try {
-      String dbName = "alterdb";
-      String invTblName = "alter-tbl";
-      String tblName = "altertbl";
+    String dbName = "alterdb";
+    String invTblName = "alter-tbl";
+    String tblName = "altertbl";
 
+    try {
       client.dropTable(dbName, tblName);
-      client.dropDatabase(dbName);
-      boolean ret = client.createDatabase(dbName, "strange_loc");
-      assertTrue("Unable to create the databse " + dbName, ret);
+      silentDropDatabase(dbName);
+
+      Database db = new Database();
+      db.setName(dbName);
+      client.createDatabase(db);
 
       ArrayList<FieldSchema> invCols = new ArrayList<FieldSchema>(2);
       invCols.add(new FieldSchema("n-ame", Constants.STRING_TYPE_NAME, ""));
@@ -753,6 +768,10 @@ public class TestHiveMetaStore extends T
       tbl.getSd().setCols(cols);
       client.createTable(tbl);
 
+      if (isThriftClient) {
+        tbl = client.getTable(tbl.getDbName(), tbl.getTableName());
+      }
+
       // now try to invalid alter table
       Table tbl2 = client.getTable(dbName, tblName);
       failed = false;
@@ -776,18 +795,22 @@ public class TestHiveMetaStore extends T
       assertEquals("Alter table didn't succeed. Num buckets is different ",
           tbl2.getSd().getNumBuckets(), tbl3.getSd().getNumBuckets());
       // check that data has moved
-      FileSystem fs = FileSystem.get((new Path(tbl.getSd().getLocation())).toUri(),
-                                     hiveConf);
+      FileSystem fs = FileSystem.get((new Path(tbl.getSd().getLocation())).toUri(), hiveConf);
       assertFalse("old table location still exists", fs.exists(new Path(tbl
           .getSd().getLocation())));
       assertTrue("data did not move to new location", fs.exists(new Path(tbl3
           .getSd().getLocation())));
-      assertEquals("alter table didn't move data correct location", tbl3
-          .getSd().getLocation(), tbl2.getSd().getLocation());
+
+      if (!isThriftClient) {
+        assertEquals("alter table didn't move data correct location", tbl3
+            .getSd().getLocation(), tbl2.getSd().getLocation());
+      }
     } catch (Exception e) {
       System.err.println(StringUtils.stringifyException(e));
       System.err.println("testSimpleTable() failed.");
       throw e;
+    } finally {
+      silentDropDatabase(dbName);
     }
   }
 
@@ -799,9 +822,10 @@ public class TestHiveMetaStore extends T
 
     try {
       client.dropTable(dbName, tblName);
-      client.dropDatabase(dbName);
-      boolean ret = client.createDatabase(dbName, "strange_loc");
-      assertTrue("Unable to create the databse " + dbName, ret);
+      silentDropDatabase(dbName);
+      Database db = new Database();
+      db.setName(dbName);
+      client.createDatabase(db);
 
       client.dropType(typeName);
       Type typ1 = new Type();
@@ -811,8 +835,7 @@ public class TestHiveMetaStore extends T
           new FieldSchema("name", Constants.STRING_TYPE_NAME, ""));
       typ1.getFields().add(
           new FieldSchema("income", Constants.INT_TYPE_NAME, ""));
-      ret = client.createType(typ1);
-      assertTrue("Unable to create type " + typeName, ret);
+      client.createType(typ1);
 
       Table tbl = new Table();
       tbl.setDbName(dbName);
@@ -889,8 +912,7 @@ public class TestHiveMetaStore extends T
       client.dropTable(dbName, tblName);
       boolean ret = client.dropType(typeName);
       assertTrue("Unable to drop type " + typeName, ret);
-      ret = client.dropDatabase(dbName);
-      assertTrue("Unable to create the databse " + dbName, ret);
+      client.dropDatabase(dbName);
     }
   }
 
@@ -898,20 +920,21 @@ public class TestHiveMetaStore extends T
 
     String val = "value";
 
-    try {
-      assertEquals(client.getConfigValue("hive.key1", val), "value1");
-      assertEquals(client.getConfigValue("hive.key2", val),
-          "http://www.example.com");
-      assertEquals(client.getConfigValue("hive.key3", val), "");
-      assertEquals(client.getConfigValue("hive.key4", val), "0");
-      assertEquals(client.getConfigValue("hive.key5", val), val);
-      assertEquals(client.getConfigValue(null, val), val);
-    } catch (TException e) {
-      e.printStackTrace();
-      assert (false);
-    } catch (ConfigValSecurityException e) {
-      e.printStackTrace();
-      assert (false);
+    if (!isThriftClient) {
+      try {
+        assertEquals(client.getConfigValue("hive.key1", val), "value1");
+        assertEquals(client.getConfigValue("hive.key2", val), "http://www.example.com");
+        assertEquals(client.getConfigValue("hive.key3", val), "");
+        assertEquals(client.getConfigValue("hive.key4", val), "0");
+        assertEquals(client.getConfigValue("hive.key5", val), val);
+        assertEquals(client.getConfigValue(null, val), val);
+      } catch (TException e) {
+        e.printStackTrace();
+          assert (false);
+      } catch (ConfigValSecurityException e) {
+        e.printStackTrace();
+        assert (false);
+      }
     }
 
     boolean threwException = false;
@@ -934,4 +957,15 @@ public class TestHiveMetaStore extends T
     part.setCreateTime(part_get.getCreateTime());
     part.putToParameters(org.apache.hadoop.hive.metastore.api.Constants.DDL_TIME, Long.toString(part_get.getCreateTime()));
   }
+
+  private static void silentDropDatabase(String dbName) throws MetaException, TException {
+    try {
+      for (String tableName : client.getTables(dbName, "*")) {
+        client.dropTable(dbName, tableName);
+      }
+      client.dropDatabase(dbName);
+    } catch (NoSuchObjectException e) {
+    } catch (InvalidOperationException e) {
+    }
+  }
 }

Added: hadoop/hive/trunk/metastore/src/test/org/apache/hadoop/hive/metastore/TestRemoteHiveMetaStore.java
URL: http://svn.apache.org/viewvc/hadoop/hive/trunk/metastore/src/test/org/apache/hadoop/hive/metastore/TestRemoteHiveMetaStore.java?rev=990026&view=auto
==============================================================================
--- hadoop/hive/trunk/metastore/src/test/org/apache/hadoop/hive/metastore/TestRemoteHiveMetaStore.java (added)
+++ hadoop/hive/trunk/metastore/src/test/org/apache/hadoop/hive/metastore/TestRemoteHiveMetaStore.java Fri Aug 27 05:41:43 2010
@@ -0,0 +1,65 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hive.metastore;
+
+import org.apache.hadoop.hive.conf.HiveConf;
+
+
+public class TestRemoteHiveMetaStore extends TestHiveMetaStore {
+  private static final String METASTORE_PORT = "29083";
+  private static boolean isServerRunning = false;
+
+  private static class RunMS implements Runnable {
+
+      @Override
+      public void run() {
+        System.out.println("Running metastore!");
+        String [] args = new String [1];
+        args[0] = METASTORE_PORT;
+        HiveMetaStore.main(args);
+      }
+
+    }
+
+  @Override
+  protected void setUp() throws Exception {
+    super.setUp();
+    if(isServerRunning) {
+      return;
+    }
+    Thread t = new Thread(new RunMS());
+    t.start();
+
+    // Wait a little bit for the metastore to start. Should probably have
+    // a better way of detecting if the metastore has started?
+    Thread.sleep(5000);
+
+    // hive.metastore.local should be defined in HiveConf
+    hiveConf.set("hive.metastore.local", "false");
+    hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS, "thrift://localhost:" + METASTORE_PORT);
+    hiveConf.setIntVar(HiveConf.ConfVars.METATORETHRIFTRETRIES, 3);
+
+    client = new HiveMetaStoreClient(hiveConf);
+    isThriftClient = true;
+
+    // Now you have the client - run necessary tests.
+    isServerRunning = true;
+  }
+
+}

Modified: hadoop/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java
URL: http://svn.apache.org/viewvc/hadoop/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java?rev=990026&r1=990025&r2=990026&view=diff
==============================================================================
--- hadoop/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java (original)
+++ hadoop/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java Fri Aug 27 05:41:43 2010
@@ -55,9 +55,12 @@ import org.apache.hadoop.hive.metastore.
 import org.apache.hadoop.hive.metastore.ProtectMode;
 import org.apache.hadoop.hive.metastore.TableType;
 import org.apache.hadoop.hive.metastore.Warehouse;
+import org.apache.hadoop.hive.metastore.api.AlreadyExistsException;
+import org.apache.hadoop.hive.metastore.api.Database;
 import org.apache.hadoop.hive.metastore.api.FieldSchema;
 import org.apache.hadoop.hive.metastore.api.InvalidOperationException;
 import org.apache.hadoop.hive.metastore.api.MetaException;
+import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
 import org.apache.hadoop.hive.metastore.api.Order;
 import org.apache.hadoop.hive.ql.Context;
 import org.apache.hadoop.hive.ql.DriverContext;
@@ -76,6 +79,7 @@ import org.apache.hadoop.hive.ql.metadat
 import org.apache.hadoop.hive.ql.plan.AddPartitionDesc;
 import org.apache.hadoop.hive.ql.plan.AlterTableDesc;
 import org.apache.hadoop.hive.ql.plan.AlterTableSimpleDesc;
+import org.apache.hadoop.hive.ql.plan.CreateDatabaseDesc;
 import org.apache.hadoop.hive.ql.plan.CreateIndexDesc;
 import org.apache.hadoop.hive.ql.plan.CreateTableDesc;
 import org.apache.hadoop.hive.ql.plan.CreateTableLikeDesc;
@@ -83,9 +87,11 @@ import org.apache.hadoop.hive.ql.plan.Cr
 import org.apache.hadoop.hive.ql.plan.DDLWork;
 import org.apache.hadoop.hive.ql.plan.DescFunctionDesc;
 import org.apache.hadoop.hive.ql.plan.DescTableDesc;
+import org.apache.hadoop.hive.ql.plan.DropDatabaseDesc;
 import org.apache.hadoop.hive.ql.plan.DropIndexDesc;
 import org.apache.hadoop.hive.ql.plan.DropTableDesc;
 import org.apache.hadoop.hive.ql.plan.MsckDesc;
+import org.apache.hadoop.hive.ql.plan.ShowDatabasesDesc;
 import org.apache.hadoop.hive.ql.plan.ShowFunctionsDesc;
 import org.apache.hadoop.hive.ql.plan.ShowLocksDesc;
 import org.apache.hadoop.hive.ql.plan.LockTableDesc;
@@ -93,6 +99,7 @@ import org.apache.hadoop.hive.ql.plan.Un
 import org.apache.hadoop.hive.ql.plan.ShowPartitionsDesc;
 import org.apache.hadoop.hive.ql.plan.ShowTableStatusDesc;
 import org.apache.hadoop.hive.ql.plan.ShowTablesDesc;
+import org.apache.hadoop.hive.ql.plan.SwitchDatabaseDesc;
 import org.apache.hadoop.hive.ql.plan.AlterTableDesc.AlterTableTypes;
 import org.apache.hadoop.hive.ql.plan.api.StageType;
 import org.apache.hadoop.hive.ql.lockmgr.HiveLock;
@@ -153,6 +160,21 @@ public class DDLTask extends Task<DDLWor
     try {
       db = Hive.get(conf);
 
+      CreateDatabaseDesc createDatabaseDesc = work.getCreateDatabaseDesc();
+      if (null != createDatabaseDesc) {
+        return createDatabase(db, createDatabaseDesc);
+      }
+
+      DropDatabaseDesc dropDatabaseDesc = work.getDropDatabaseDesc();
+      if (dropDatabaseDesc != null) {
+        return dropDatabase(db, dropDatabaseDesc);
+      }
+
+      SwitchDatabaseDesc switchDatabaseDesc = work.getSwitchDatabaseDesc();
+      if (switchDatabaseDesc != null) {
+        return switchDatabase(db, switchDatabaseDesc);
+      }
+
       CreateTableDesc crtTbl = work.getCreateTblDesc();
       if (crtTbl != null) {
         return createTable(db, crtTbl);
@@ -164,12 +186,11 @@ public class DDLTask extends Task<DDLWor
       }
 
       DropIndexDesc dropIdx = work.getDropIdxDesc();
-      if(dropIdx != null) {
+      if (dropIdx != null) {
         return dropIndex(db, dropIdx);
       }
 
       CreateTableLikeDesc crtTblLike = work.getCreateTblLikeDesc();
-
       if (crtTblLike != null) {
         return createTableLike(db, crtTblLike);
       }
@@ -195,8 +216,7 @@ public class DDLTask extends Task<DDLWor
       }
 
       AlterTableSimpleDesc simpleDesc = work.getAlterTblSimpleDesc();
-
-      if(simpleDesc != null) {
+      if (simpleDesc != null) {
         if (simpleDesc.getType() == AlterTableTypes.TOUCH) {
           return touch(db, simpleDesc);
         } else if (simpleDesc.getType() == AlterTableTypes.ARCHIVE) {
@@ -205,6 +225,7 @@ public class DDLTask extends Task<DDLWor
           return unarchive(db, simpleDesc);
         }
       }
+
       MsckDesc msckDesc = work.getMsckDesc();
       if (msckDesc != null) {
         return msck(db, msckDesc);
@@ -220,6 +241,11 @@ public class DDLTask extends Task<DDLWor
         return describeFunction(descFunc);
       }
 
+      ShowDatabasesDesc showDatabases = work.getShowDatabasesDesc();
+      if (showDatabases != null) {
+        return showDatabases(db, showDatabases);
+      }
+
       ShowTablesDesc showTbls = work.getShowTblsDesc();
       if (showTbls != null) {
         return showTables(db, showTbls);
@@ -274,7 +300,7 @@ public class DDLTask extends Task<DDLWor
   }
 
   private int dropIndex(Hive db, DropIndexDesc dropIdx) throws HiveException {
-    db.dropIndex(MetaStoreUtils.DEFAULT_DATABASE_NAME, dropIdx.getTableName(),
+    db.dropIndex(db.getCurrentDatabase(), dropIdx.getTableName(),
         dropIdx.getIndexName(), true);
     return 0;
   }
@@ -309,8 +335,7 @@ public class DDLTask extends Task<DDLWor
    */
   private int addPartition(Hive db, AddPartitionDesc addPartitionDesc) throws HiveException {
 
-    Table tbl = db.getTable(addPartitionDesc.getDbName(), addPartitionDesc
-        .getTableName());
+    Table tbl = db.getTable(addPartitionDesc.getDbName(), addPartitionDesc.getTableName());
 
     validateAlterTableType(tbl, AlterTableDesc.AlterTableTypes.ADDPARTITION);
 
@@ -904,11 +929,10 @@ public class DDLTask extends Task<DDLWor
     List<String> repairOutput = new ArrayList<String>();
     try {
       HiveMetaStoreChecker checker = new HiveMetaStoreChecker(db);
-      checker.checkMetastore(MetaStoreUtils.DEFAULT_DATABASE_NAME, msckDesc
+      checker.checkMetastore(db.getCurrentDatabase(), msckDesc
           .getTableName(), msckDesc.getPartSpecs(), result);
       if (msckDesc.isRepairPartitions()) {
-        Table table = db.getTable(MetaStoreUtils.DEFAULT_DATABASE_NAME,
-            msckDesc.getTableName());
+        Table table = db.getTable(msckDesc.getTableName());
         for (CheckResult.PartitionResult part : result.getPartitionsNotInMs()) {
           try {
             db.createPartition(table, Warehouse.makeSpecFromName(part
@@ -1020,18 +1044,17 @@ public class DDLTask extends Task<DDLWor
     Table tbl = null;
     List<String> parts = null;
 
-    tbl = db.getTable(MetaStoreUtils.DEFAULT_DATABASE_NAME, tabName);
+    tbl = db.getTable(tabName);
 
     if (!tbl.isPartitioned()) {
       console.printError("Table " + tabName + " is not a partitioned table");
       return 1;
     }
     if (showParts.getPartSpec() != null) {
-      parts = db.getPartitionNames(MetaStoreUtils.DEFAULT_DATABASE_NAME,
+      parts = db.getPartitionNames(db.getCurrentDatabase(),
           tbl.getTableName(), showParts.getPartSpec(), (short) -1);
     } else {
-      parts = db.getPartitionNames(MetaStoreUtils.DEFAULT_DATABASE_NAME, tbl
-          .getTableName(), (short) -1);
+      parts = db.getPartitionNames(db.getCurrentDatabase(), tbl.getTableName(), (short) -1);
     }
 
     // write the results in the file
@@ -1061,6 +1084,50 @@ public class DDLTask extends Task<DDLWor
   }
 
   /**
+   * Write a list of the available databases to a file.
+   *
+   * @param showDatabases
+   *          These are the databases we're interested in.
+   * @return Returns 0 when execution succeeds and above 0 if it fails.
+   * @throws HiveException
+   *           Throws this exception if an unexpected error occurs.
+   */
+  private int showDatabases(Hive db, ShowDatabasesDesc showDatabasesDesc) throws HiveException {
+    // get the databases for the desired pattern - populate the output stream
+    List<String> databases = null;
+    if (showDatabasesDesc.getPattern() != null) {
+      LOG.info("pattern: " + showDatabasesDesc.getPattern());
+      databases = db.getDatabasesByPattern(showDatabasesDesc.getPattern());
+    } else {
+      databases = db.getAllDatabases();
+    }
+    LOG.info("results : " + databases.size());
+
+    // write the results in the file
+    try {
+      Path resFile = new Path(showDatabasesDesc.getResFile());
+      FileSystem fs = resFile.getFileSystem(conf);
+      DataOutput outStream = fs.create(resFile);
+
+      for (String database : databases) {
+        // create a row per database name
+        outStream.writeBytes(database);
+        outStream.write(terminator);
+      }
+      ((FSDataOutputStream) outStream).close();
+    } catch (FileNotFoundException e) {
+      LOG.warn("show databases: " + stringifyException(e));
+      return 1;
+    } catch (IOException e) {
+      LOG.warn("show databases: " + stringifyException(e));
+      return 1;
+    } catch (Exception e) {
+      throw new HiveException(e.toString());
+    }
+    return 0;
+  }
+
+  /**
    * Write a list of the tables in the database to a file.
    *
    * @param db
@@ -1388,8 +1455,7 @@ public class DDLTask extends Task<DDLWor
     Map<String, String> part = showTblStatus.getPartSpec();
     Partition par = null;
     if (part != null) {
-      Table tbl = db.getTable(showTblStatus.getDbName(), showTblStatus
-          .getPattern());
+      Table tbl = db.getTable(showTblStatus.getDbName(), showTblStatus.getPattern());
       par = db.getPartition(tbl, part, false);
       if (par == null) {
         throw new HiveException("Partition " + part + " for table "
@@ -1512,8 +1578,7 @@ public class DDLTask extends Task<DDLWor
         colPath.indexOf('.') == -1 ? colPath.length() : colPath.indexOf('.'));
 
     // describe the table - populate the output stream
-    Table tbl = db.getTable(MetaStoreUtils.DEFAULT_DATABASE_NAME, tableName,
-        false);
+    Table tbl = db.getTable(db.getCurrentDatabase(), tableName, false);
     Partition part = null;
     try {
       Path resFile = new Path(descTbl.getResFile());
@@ -1764,8 +1829,7 @@ public class DDLTask extends Task<DDLWor
    */
   private int alterTable(Hive db, AlterTableDesc alterTbl) throws HiveException {
     // alter the table
-    Table tbl = db.getTable(MetaStoreUtils.DEFAULT_DATABASE_NAME, alterTbl
-        .getOldName());
+    Table tbl = db.getTable(alterTbl.getOldName());
 
     Partition part = null;
     if(alterTbl.getPartSpec() != null) {
@@ -2086,8 +2150,7 @@ public class DDLTask extends Task<DDLWor
     // post-execution hook
     Table tbl = null;
     try {
-      tbl = db.getTable(MetaStoreUtils.DEFAULT_DATABASE_NAME, dropTbl
-          .getTableName());
+      tbl = db.getTable(dropTbl.getTableName());
       if (!tbl.canDrop()) {
         throw new HiveException("Table " + tbl.getTableName() +
             " is protected from being dropped");
@@ -2122,25 +2185,20 @@ public class DDLTask extends Task<DDLWor
       }
 
       // drop the table
-      db
-          .dropTable(MetaStoreUtils.DEFAULT_DATABASE_NAME, dropTbl
-          .getTableName());
+      db.dropTable(db.getCurrentDatabase(), dropTbl.getTableName());
       if (tbl != null) {
         work.getOutputs().add(new WriteEntity(tbl));
       }
     } else {
       // get all partitions of the table
-      List<String> partitionNames = db.getPartitionNames(
-          MetaStoreUtils.DEFAULT_DATABASE_NAME, dropTbl.getTableName(),
-          (short) -1);
-
+      List<String> partitionNames =
+        db.getPartitionNames(db.getCurrentDatabase(), dropTbl.getTableName(), (short) -1);
       Set<Map<String, String>> partitions = new HashSet<Map<String, String>>();
-      for (int i = 0; i < partitionNames.size(); i++) {
+      for (String partitionName : partitionNames) {
         try {
-          partitions.add(Warehouse.makeSpecFromName(partitionNames.get(i)));
+          partitions.add(Warehouse.makeSpecFromName(partitionName));
         } catch (MetaException e) {
-          LOG.warn("Unrecognized partition name from metastore: "
-              + partitionNames.get(i));
+          LOG.warn("Unrecognized partition name from metastore: " + partitionName);
         }
       }
       // drop partitions in the list
@@ -2174,8 +2232,8 @@ public class DDLTask extends Task<DDLWor
       // drop all existing partitions from the list
       for (Partition partition : partsToDelete) {
         console.printInfo("Dropping the partition " + partition.getName());
-        db.dropPartition(MetaStoreUtils.DEFAULT_DATABASE_NAME, dropTbl
-            .getTableName(), partition.getValues(), true); // drop data for the
+        db.dropPartition(db.getCurrentDatabase(), dropTbl.getTableName(),
+            partition.getValues(), true); // drop data for the
         // partition
         work.getOutputs().add(new WriteEntity(partition));
       }
@@ -2199,6 +2257,57 @@ public class DDLTask extends Task<DDLWor
   }
 
   /**
+   * Create a Database
+   * @param db
+   * @param crtDb
+   * @return Always returns 0
+   * @throws HiveException
+   * @throws AlreadyExistsException
+   */
+  private int createDatabase(Hive db, CreateDatabaseDesc crtDb)
+      throws HiveException, AlreadyExistsException {
+    Database database = new Database();
+    database.setName(crtDb.getName());
+    database.setDescription(crtDb.getComment());
+    database.setLocationUri(crtDb.getLocationUri());
+
+    db.createDatabase(database, crtDb.getIfNotExists());
+    return 0;
+  }
+
+  /**
+   * Drop a Database
+   * @param db
+   * @param dropDb
+   * @return Always returns 0
+   * @throws HiveException
+   * @throws NoSuchObjectException
+   */
+  private int dropDatabase(Hive db, DropDatabaseDesc dropDb)
+      throws HiveException, NoSuchObjectException {
+    db.dropDatabase(dropDb.getDatabaseName(), true, dropDb.getIfExists());
+    return 0;
+  }
+
+  /**
+   * Switch to a different Database
+   * @param db
+   * @param switchDb
+   * @return Always returns 0
+   * @throws HiveException
+   */
+  private int switchDatabase(Hive db, SwitchDatabaseDesc switchDb)
+      throws HiveException {
+    String dbName = switchDb.getDatabaseName();
+    if (!db.databaseExists(dbName)) {
+      throw new HiveException("ERROR: The database " + dbName + " does not exist.");
+    }
+    db.setCurrentDatabase(dbName);
+    return 0;
+  }
+
+
+  /**
    * Create a new table.
    *
    * @param db
@@ -2211,7 +2320,7 @@ public class DDLTask extends Task<DDLWor
    */
   private int createTable(Hive db, CreateTableDesc crtTbl) throws HiveException {
     // create the table
-    Table tbl = new Table(crtTbl.getTableName());
+    Table tbl = new Table(db.getCurrentDatabase(), crtTbl.getTableName());
     if (crtTbl.getPartCols() != null) {
       tbl.setPartCols(crtTbl.getPartCols());
     }
@@ -2370,8 +2479,7 @@ public class DDLTask extends Task<DDLWor
    */
   private int createTableLike(Hive db, CreateTableLikeDesc crtTbl) throws HiveException {
     // Get the existing table
-    Table tbl = db.getTable(MetaStoreUtils.DEFAULT_DATABASE_NAME, crtTbl
-        .getLikeTableName());
+    Table tbl = db.getTable(crtTbl.getLikeTableName());
 
     tbl.setTableName(crtTbl.getTableName());
 
@@ -2405,7 +2513,7 @@ public class DDLTask extends Task<DDLWor
    *           Throws this exception if an unexpected error occurs.
    */
   private int createView(Hive db, CreateViewDesc crtView) throws HiveException {
-    Table tbl = new Table(crtView.getViewName());
+    Table tbl = new Table(db.getCurrentDatabase(), crtView.getViewName());
     tbl.setTableType(TableType.VIRTUAL_VIEW);
     tbl.setSerializationLib(null);
     tbl.clearSerDeInfo();