Search in sources :

Example 1 with DropTableDesc

use of org.apache.hadoop.hive.ql.ddl.table.drop.DropTableDesc in project hive by apache.

the class AcidExportSemanticAnalyzer method analyzeAcidExport.

/**
 * See {@link #isAcidExport(ASTNode)}
 * 1. create the temp table T
 * 2. compile 'insert into T select * from acidTable'
 * 3. compile 'export acidTable'  (acidTable will be replaced with T during execution)
 * 4. create task to drop T
 *
 * Using a true temp (session level) table means it should not affect replication and the table
 * is not visible outside the Session that created for security
 */
private void analyzeAcidExport(ASTNode ast) throws SemanticException {
    assert ast != null && ast.getToken() != null && ast.getToken().getType() == HiveParser.TOK_EXPORT;
    ASTNode tableTree = (ASTNode) ast.getChild(0);
    assert tableTree != null && tableTree.getType() == HiveParser.TOK_TAB;
    ASTNode tokRefOrNameExportTable = (ASTNode) tableTree.getChild(0);
    Table exportTable = getTargetTable(tokRefOrNameExportTable);
    if (exportTable != null && (exportTable.isView() || exportTable.isMaterializedView())) {
        throw new SemanticException("Views and Materialized Views can not be exported.");
    }
    assert AcidUtils.isFullAcidTable(exportTable);
    // need to create the table "manually" rather than creating a task since it has to exist to
    // compile the insert into T...
    // this is db.table
    final String newTableName = getTmptTableNameForExport(exportTable);
    final TableName newTableNameRef = HiveTableName.of(newTableName);
    Map<String, String> tblProps = new HashMap<>();
    tblProps.put(hive_metastoreConstants.TABLE_IS_TRANSACTIONAL, Boolean.FALSE.toString());
    String location;
    // it has the same life cycle as the tmp table
    try {
        // Generate a unique ID for temp table path.
        // This path will be fixed for the life of the temp table.
        Path path = new Path(SessionState.getTempTableSpace(conf), UUID.randomUUID().toString());
        path = Warehouse.getDnsPath(path, conf);
        location = path.toString();
    } catch (MetaException err) {
        throw new SemanticException("Error while generating temp table path:", err);
    }
    CreateTableLikeDesc ctlt = new CreateTableLikeDesc(newTableName, false, true, null, null, location, null, null, tblProps, // important so we get an exception on name collision
    true, Warehouse.getQualifiedName(exportTable.getTTable()), false);
    Table newTable;
    try {
        ReadEntity dbForTmpTable = new ReadEntity(db.getDatabase(exportTable.getDbName()));
        // so the plan knows we are 'reading' this db - locks, security...
        inputs.add(dbForTmpTable);
        DDLTask createTableTask = (DDLTask) TaskFactory.get(new DDLWork(new HashSet<>(), new HashSet<>(), ctlt), conf);
        // above get() doesn't set it
        createTableTask.setConf(conf);
        Context context = new Context(conf);
        createTableTask.initialize(null, null, new TaskQueue(context), context);
        createTableTask.execute();
        newTable = db.getTable(newTableName);
    } catch (HiveException ex) {
        throw new SemanticException(ex);
    }
    // now generate insert statement
    // insert into newTableName select * from ts <where partition spec>
    StringBuilder rewrittenQueryStr = generateExportQuery(newTable.getPartCols(), tokRefOrNameExportTable, tableTree, newTableName);
    ReparseResult rr = parseRewrittenQuery(rewrittenQueryStr, ctx.getCmd());
    Context rewrittenCtx = rr.rewrittenCtx;
    // it's set in parseRewrittenQuery()
    rewrittenCtx.setIsUpdateDeleteMerge(false);
    ASTNode rewrittenTree = rr.rewrittenTree;
    try {
        useSuper = true;
        // newTable has to exist at this point to compile
        super.analyze(rewrittenTree, rewrittenCtx);
    } finally {
        useSuper = false;
    }
    // now we have the rootTasks set up for Insert ... Select
    removeStatsTasks(rootTasks);
    // now make an ExportTask from temp table
    /*analyzeExport() creates TableSpec which in turn tries to build
     "public List<Partition> partitions" by looking in the metastore to find Partitions matching
     the partition spec in the Export command.  These of course don't exist yet since we've not
     ran the insert stmt yet!!!!!!!
      */
    Task<ExportWork> exportTask = ExportSemanticAnalyzer.analyzeExport(ast, newTableName, db, conf, inputs, outputs);
    // Add an alter table task to set transactional props
    // do it after populating temp table so that it's written as non-transactional table but
    // update props before export so that export archive metadata has these props.  This way when
    // IMPORT is done for this archive and target table doesn't exist, it will be created as Acid.
    Map<String, String> mapProps = new HashMap<>();
    mapProps.put(hive_metastoreConstants.TABLE_IS_TRANSACTIONAL, Boolean.TRUE.toString());
    AlterTableSetPropertiesDesc alterTblDesc = new AlterTableSetPropertiesDesc(newTableNameRef, null, null, false, mapProps, false, false, null);
    addExportTask(rootTasks, exportTask, TaskFactory.get(new DDLWork(getInputs(), getOutputs(), alterTblDesc)));
    // Now make a task to drop temp table
    // {@link DropTableAnalyzer#analyzeInternal(ASTNode ast)
    ReplicationSpec replicationSpec = new ReplicationSpec();
    DropTableDesc dropTblDesc = new DropTableDesc(newTableName, false, true, replicationSpec);
    Task<DDLWork> dropTask = TaskFactory.get(new DDLWork(new HashSet<>(), new HashSet<>(), dropTblDesc), conf);
    exportTask.addDependentTask(dropTask);
    markReadEntityForUpdate();
    if (ctx.isExplainPlan()) {
        try {
            // so that "explain" doesn't "leak" tmp tables
            // TODO: catalog
            db.dropTable(newTable.getDbName(), newTable.getTableName(), true, true, true);
        } catch (HiveException ex) {
            LOG.warn("Unable to drop " + newTableName + " due to: " + ex.getMessage(), ex);
        }
    }
}
Also used : HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) HashMap(java.util.HashMap) ExportWork(org.apache.hadoop.hive.ql.plan.ExportWork) CreateTableLikeDesc(org.apache.hadoop.hive.ql.ddl.table.create.like.CreateTableLikeDesc) TaskQueue(org.apache.hadoop.hive.ql.TaskQueue) AlterTableSetPropertiesDesc(org.apache.hadoop.hive.ql.ddl.table.misc.properties.AlterTableSetPropertiesDesc) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) HashSet(java.util.HashSet) Path(org.apache.hadoop.fs.Path) Context(org.apache.hadoop.hive.ql.Context) Table(org.apache.hadoop.hive.ql.metadata.Table) DropTableDesc(org.apache.hadoop.hive.ql.ddl.table.drop.DropTableDesc) ReadEntity(org.apache.hadoop.hive.ql.hooks.ReadEntity) TableName(org.apache.hadoop.hive.common.TableName) DDLWork(org.apache.hadoop.hive.ql.ddl.DDLWork) DDLTask(org.apache.hadoop.hive.ql.ddl.DDLTask)

Example 2 with DropTableDesc

use of org.apache.hadoop.hive.ql.ddl.table.drop.DropTableDesc in project hive by apache.

the class DropTableHandler method handle.

@Override
public List<Task<?>> handle(Context context) throws SemanticException {
    String actualDbName;
    String actualTblName;
    if (context.dmd.getDumpType() == DumpType.EVENT_RENAME_DROP_TABLE) {
        AlterTableMessage msg = deserializer.getAlterTableMessage(context.dmd.getPayload());
        actualDbName = context.isDbNameEmpty() ? msg.getDB() : context.dbName;
        actualTblName = msg.getTable();
    } else {
        DropTableMessage msg = deserializer.getDropTableMessage(context.dmd.getPayload());
        actualDbName = context.isDbNameEmpty() ? msg.getDB() : context.dbName;
        actualTblName = msg.getTable();
    }
    DropTableDesc dropTableDesc = new DropTableDesc(actualDbName + "." + actualTblName, true, true, context.eventOnlyReplicationSpec(), false);
    Task<DDLWork> dropTableTask = TaskFactory.get(new DDLWork(readEntitySet, writeEntitySet, dropTableDesc, true, context.getDumpDirectory(), context.getMetricCollector()), context.hiveConf);
    context.log.debug("Added drop tbl task : {}:{}", dropTableTask.getId(), dropTableDesc.getTableName());
    updatedMetadata.set(context.dmd.getEventTo().toString(), actualDbName, null, null);
    return Collections.singletonList(dropTableTask);
}
Also used : DropTableMessage(org.apache.hadoop.hive.metastore.messaging.DropTableMessage) DDLWork(org.apache.hadoop.hive.ql.ddl.DDLWork) AlterTableMessage(org.apache.hadoop.hive.metastore.messaging.AlterTableMessage) DropTableDesc(org.apache.hadoop.hive.ql.ddl.table.drop.DropTableDesc)

Aggregations

DDLWork (org.apache.hadoop.hive.ql.ddl.DDLWork)2 DropTableDesc (org.apache.hadoop.hive.ql.ddl.table.drop.DropTableDesc)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Path (org.apache.hadoop.fs.Path)1 TableName (org.apache.hadoop.hive.common.TableName)1 MetaException (org.apache.hadoop.hive.metastore.api.MetaException)1 AlterTableMessage (org.apache.hadoop.hive.metastore.messaging.AlterTableMessage)1 DropTableMessage (org.apache.hadoop.hive.metastore.messaging.DropTableMessage)1 Context (org.apache.hadoop.hive.ql.Context)1 TaskQueue (org.apache.hadoop.hive.ql.TaskQueue)1 DDLTask (org.apache.hadoop.hive.ql.ddl.DDLTask)1 CreateTableLikeDesc (org.apache.hadoop.hive.ql.ddl.table.create.like.CreateTableLikeDesc)1 AlterTableSetPropertiesDesc (org.apache.hadoop.hive.ql.ddl.table.misc.properties.AlterTableSetPropertiesDesc)1 ReadEntity (org.apache.hadoop.hive.ql.hooks.ReadEntity)1 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)1 Table (org.apache.hadoop.hive.ql.metadata.Table)1 ExportWork (org.apache.hadoop.hive.ql.plan.ExportWork)1