Search in sources :

Example 1 with MTable

use of org.apache.hadoop.hive.metastore.model.MTable in project hive by apache.

the class ObjectStore method deletePartitionColumnStatistics.

@Override
public boolean deletePartitionColumnStatistics(String dbName, String tableName, String partName, List<String> partVals, String colName) throws NoSuchObjectException, MetaException, InvalidObjectException, InvalidInputException {
    boolean ret = false;
    Query query = null;
    if (dbName == null) {
        dbName = MetaStoreUtils.DEFAULT_DATABASE_NAME;
    }
    if (tableName == null) {
        throw new InvalidInputException("Table name is null.");
    }
    try {
        openTransaction();
        MTable mTable = getMTable(dbName, tableName);
        MPartitionColumnStatistics mStatsObj;
        List<MPartitionColumnStatistics> mStatsObjColl;
        if (mTable == null) {
            throw new NoSuchObjectException("Table " + tableName + "  for which stats deletion is requested doesn't exist");
        }
        MPartition mPartition = getMPartition(dbName, tableName, partVals);
        if (mPartition == null) {
            throw new NoSuchObjectException("Partition " + partName + " for which stats deletion is requested doesn't exist");
        }
        query = pm.newQuery(MPartitionColumnStatistics.class);
        String filter;
        String parameters;
        if (colName != null) {
            filter = "partition.partitionName == t1 && dbName == t2 && tableName == t3 && " + "colName == t4";
            parameters = "java.lang.String t1, java.lang.String t2, " + "java.lang.String t3, java.lang.String t4";
        } else {
            filter = "partition.partitionName == t1 && dbName == t2 && tableName == t3";
            parameters = "java.lang.String t1, java.lang.String t2, java.lang.String t3";
        }
        query.setFilter(filter);
        query.declareParameters(parameters);
        if (colName != null) {
            query.setUnique(true);
            mStatsObj = (MPartitionColumnStatistics) query.executeWithArray(partName.trim(), HiveStringUtils.normalizeIdentifier(dbName), HiveStringUtils.normalizeIdentifier(tableName), HiveStringUtils.normalizeIdentifier(colName));
            pm.retrieve(mStatsObj);
            if (mStatsObj != null) {
                pm.deletePersistent(mStatsObj);
            } else {
                throw new NoSuchObjectException("Column stats doesn't exist for db=" + dbName + " table=" + tableName + " partition=" + partName + " col=" + colName);
            }
        } else {
            mStatsObjColl = (List<MPartitionColumnStatistics>) query.execute(partName.trim(), HiveStringUtils.normalizeIdentifier(dbName), HiveStringUtils.normalizeIdentifier(tableName));
            pm.retrieveAll(mStatsObjColl);
            if (mStatsObjColl != null) {
                pm.deletePersistentAll(mStatsObjColl);
            } else {
                throw new NoSuchObjectException("Column stats doesn't exist for db=" + dbName + " table=" + tableName + " partition" + partName);
            }
        }
        ret = commitTransaction();
    } catch (NoSuchObjectException e) {
        rollbackTransaction();
        throw e;
    } finally {
        if (!ret) {
            rollbackTransaction();
        }
        if (query != null) {
            query.closeAll();
        }
    }
    return ret;
}
Also used : InvalidInputException(org.apache.hadoop.hive.metastore.api.InvalidInputException) MTable(org.apache.hadoop.hive.metastore.model.MTable) Query(javax.jdo.Query) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) MPartitionColumnStatistics(org.apache.hadoop.hive.metastore.model.MPartitionColumnStatistics) MPartition(org.apache.hadoop.hive.metastore.model.MPartition)

Example 2 with MTable

use of org.apache.hadoop.hive.metastore.model.MTable in project hive by apache.

the class ObjectStore method convertPartCols.

private List<HiveObjectPrivilege> convertPartCols(List<MPartitionColumnPrivilege> privs) {
    List<HiveObjectPrivilege> result = new ArrayList<HiveObjectPrivilege>();
    for (MPartitionColumnPrivilege priv : privs) {
        String pname = priv.getPrincipalName();
        PrincipalType ptype = PrincipalType.valueOf(priv.getPrincipalType());
        MPartition mpartition = priv.getPartition();
        MTable mtable = mpartition.getTable();
        MDatabase mdatabase = mtable.getDatabase();
        HiveObjectRef objectRef = new HiveObjectRef(HiveObjectType.COLUMN, mdatabase.getName(), mtable.getTableName(), mpartition.getValues(), priv.getColumnName());
        PrivilegeGrantInfo grantor = new PrivilegeGrantInfo(priv.getPrivilege(), priv.getCreateTime(), priv.getGrantor(), PrincipalType.valueOf(priv.getGrantorType()), priv.getGrantOption());
        result.add(new HiveObjectPrivilege(objectRef, pname, ptype, grantor));
    }
    return result;
}
Also used : MDatabase(org.apache.hadoop.hive.metastore.model.MDatabase) HiveObjectPrivilege(org.apache.hadoop.hive.metastore.api.HiveObjectPrivilege) MTable(org.apache.hadoop.hive.metastore.model.MTable) PrivilegeGrantInfo(org.apache.hadoop.hive.metastore.api.PrivilegeGrantInfo) HiveObjectRef(org.apache.hadoop.hive.metastore.api.HiveObjectRef) ArrayList(java.util.ArrayList) MPartitionColumnPrivilege(org.apache.hadoop.hive.metastore.model.MPartitionColumnPrivilege) PrincipalType(org.apache.hadoop.hive.metastore.api.PrincipalType) MPartition(org.apache.hadoop.hive.metastore.model.MPartition)

Example 3 with MTable

use of org.apache.hadoop.hive.metastore.model.MTable in project hive by apache.

the class ObjectStore method convertToMPart.

/**
   * Convert a Partition object into an MPartition, which is an object backed by the db
   * If the Partition's set of columns is the same as the parent table's AND useTableCD
   * is true, then this partition's storage descriptor's column descriptor will point
   * to the same one as the table's storage descriptor.
   * @param part the partition to convert
   * @param useTableCD whether to try to use the parent table's column descriptor.
   * @return the model partition object
   * @throws InvalidObjectException
   * @throws MetaException
   */
private MPartition convertToMPart(Partition part, boolean useTableCD) throws InvalidObjectException, MetaException {
    if (part == null) {
        return null;
    }
    MTable mt = getMTable(part.getDbName(), part.getTableName());
    if (mt == null) {
        throw new InvalidObjectException("Partition doesn't have a valid table or database name");
    }
    // If this partition's set of columns is the same as the parent table's,
    // use the parent table's, so we do not create a duplicate column descriptor,
    // thereby saving space
    MStorageDescriptor msd;
    if (useTableCD && mt.getSd() != null && mt.getSd().getCD() != null && mt.getSd().getCD().getCols() != null && part.getSd() != null && convertToFieldSchemas(mt.getSd().getCD().getCols()).equals(part.getSd().getCols())) {
        msd = convertToMStorageDescriptor(part.getSd(), mt.getSd().getCD());
    } else {
        msd = convertToMStorageDescriptor(part.getSd());
    }
    return new MPartition(Warehouse.makePartName(convertToFieldSchemas(mt.getPartitionKeys()), part.getValues()), mt, part.getValues(), part.getCreateTime(), part.getLastAccessTime(), msd, part.getParameters());
}
Also used : MTable(org.apache.hadoop.hive.metastore.model.MTable) InvalidObjectException(org.apache.hadoop.hive.metastore.api.InvalidObjectException) MStorageDescriptor(org.apache.hadoop.hive.metastore.model.MStorageDescriptor) MPartition(org.apache.hadoop.hive.metastore.model.MPartition)

Example 4 with MTable

use of org.apache.hadoop.hive.metastore.model.MTable in project hive by apache.

the class ObjectStore method addForeignKeys.

private void addForeignKeys(List<SQLForeignKey> fks, boolean retrieveCD) throws InvalidObjectException, MetaException {
    List<MConstraint> mpkfks = new ArrayList<MConstraint>();
    String currentConstraintName = null;
    for (int i = 0; i < fks.size(); i++) {
        AttachedMTableInfo nParentTable = getMTable(fks.get(i).getPktable_db(), fks.get(i).getPktable_name(), retrieveCD);
        MTable parentTable = nParentTable.mtbl;
        if (parentTable == null) {
            throw new InvalidObjectException("Parent table not found: " + fks.get(i).getPktable_name());
        }
        AttachedMTableInfo nChildTable = getMTable(fks.get(i).getFktable_db(), fks.get(i).getFktable_name(), retrieveCD);
        MTable childTable = nChildTable.mtbl;
        if (childTable == null) {
            throw new InvalidObjectException("Child table not found: " + fks.get(i).getFktable_name());
        }
        MColumnDescriptor parentCD = retrieveCD ? nParentTable.mcd : parentTable.getSd().getCD();
        List<MFieldSchema> parentCols = parentCD == null ? null : parentCD.getCols();
        int parentIntegerIndex = getColumnIndexFromTableColumns(parentCols, fks.get(i).getPkcolumn_name());
        if (parentIntegerIndex == -1) {
            throw new InvalidObjectException("Parent column not found: " + fks.get(i).getPkcolumn_name());
        }
        MColumnDescriptor childCD = retrieveCD ? nChildTable.mcd : childTable.getSd().getCD();
        List<MFieldSchema> childCols = childCD.getCols();
        int childIntegerIndex = getColumnIndexFromTableColumns(childCols, fks.get(i).getFkcolumn_name());
        if (childIntegerIndex == -1) {
            throw new InvalidObjectException("Child column not found: " + fks.get(i).getFkcolumn_name());
        }
        if (fks.get(i).getFk_name() == null) {
            // the uniqueness of the generated constraint name.
            if (fks.get(i).getKey_seq() == 1) {
                currentConstraintName = generateConstraintName(fks.get(i).getFktable_db(), fks.get(i).getFktable_name(), fks.get(i).getPktable_db(), fks.get(i).getPktable_name(), fks.get(i).getPkcolumn_name(), fks.get(i).getFkcolumn_name(), "fk");
            }
        } else {
            currentConstraintName = fks.get(i).getFk_name();
        }
        Integer updateRule = fks.get(i).getUpdate_rule();
        Integer deleteRule = fks.get(i).getDelete_rule();
        int enableValidateRely = (fks.get(i).isEnable_cstr() ? 4 : 0) + (fks.get(i).isValidate_cstr() ? 2 : 0) + (fks.get(i).isRely_cstr() ? 1 : 0);
        MConstraint mpkfk = new MConstraint(currentConstraintName, MConstraint.FOREIGN_KEY_CONSTRAINT, fks.get(i).getKey_seq(), deleteRule, updateRule, enableValidateRely, parentTable, childTable, parentCD, childCD, childIntegerIndex, parentIntegerIndex);
        mpkfks.add(mpkfk);
    }
    pm.makePersistentAll(mpkfks);
}
Also used : MTable(org.apache.hadoop.hive.metastore.model.MTable) MFieldSchema(org.apache.hadoop.hive.metastore.model.MFieldSchema) ArrayList(java.util.ArrayList) MConstraint(org.apache.hadoop.hive.metastore.model.MConstraint) InvalidObjectException(org.apache.hadoop.hive.metastore.api.InvalidObjectException) MColumnDescriptor(org.apache.hadoop.hive.metastore.model.MColumnDescriptor) MConstraint(org.apache.hadoop.hive.metastore.model.MConstraint)

Example 5 with MTable

use of org.apache.hadoop.hive.metastore.model.MTable in project hive by apache.

the class ObjectStore method convertToMTable.

private MTable convertToMTable(Table tbl) throws InvalidObjectException, MetaException {
    if (tbl == null) {
        return null;
    }
    MDatabase mdb = null;
    try {
        mdb = getMDatabase(tbl.getDbName());
    } catch (NoSuchObjectException e) {
        LOG.error(StringUtils.stringifyException(e));
        throw new InvalidObjectException("Database " + tbl.getDbName() + " doesn't exist.");
    }
    // If the table has property EXTERNAL set, update table type
    // accordingly
    String tableType = tbl.getTableType();
    boolean isExternal = "TRUE".equals(tbl.getParameters().get("EXTERNAL"));
    if (TableType.MANAGED_TABLE.toString().equals(tableType)) {
        if (isExternal) {
            tableType = TableType.EXTERNAL_TABLE.toString();
        }
    }
    if (TableType.EXTERNAL_TABLE.toString().equals(tableType)) {
        if (!isExternal) {
            tableType = TableType.MANAGED_TABLE.toString();
        }
    }
    // A new table is always created with a new column descriptor
    return new MTable(HiveStringUtils.normalizeIdentifier(tbl.getTableName()), mdb, convertToMStorageDescriptor(tbl.getSd()), tbl.getOwner(), tbl.getCreateTime(), tbl.getLastAccessTime(), tbl.getRetention(), convertToMFieldSchemas(tbl.getPartitionKeys()), tbl.getParameters(), tbl.getViewOriginalText(), tbl.getViewExpandedText(), tbl.isRewriteEnabled(), tableType);
}
Also used : MDatabase(org.apache.hadoop.hive.metastore.model.MDatabase) MTable(org.apache.hadoop.hive.metastore.model.MTable) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) InvalidObjectException(org.apache.hadoop.hive.metastore.api.InvalidObjectException)

Aggregations

MTable (org.apache.hadoop.hive.metastore.model.MTable)30 ArrayList (java.util.ArrayList)16 MPartition (org.apache.hadoop.hive.metastore.model.MPartition)12 Query (javax.jdo.Query)9 MConstraint (org.apache.hadoop.hive.metastore.model.MConstraint)8 InvalidObjectException (org.apache.hadoop.hive.metastore.api.InvalidObjectException)7 MDatabase (org.apache.hadoop.hive.metastore.model.MDatabase)6 MPartitionColumnPrivilege (org.apache.hadoop.hive.metastore.model.MPartitionColumnPrivilege)6 MPartitionPrivilege (org.apache.hadoop.hive.metastore.model.MPartitionPrivilege)6 MTableColumnPrivilege (org.apache.hadoop.hive.metastore.model.MTableColumnPrivilege)6 MetaException (org.apache.hadoop.hive.metastore.api.MetaException)5 NoSuchObjectException (org.apache.hadoop.hive.metastore.api.NoSuchObjectException)5 Partition (org.apache.hadoop.hive.metastore.api.Partition)5 MTablePrivilege (org.apache.hadoop.hive.metastore.model.MTablePrivilege)5 Collection (java.util.Collection)4 HiveObjectPrivilege (org.apache.hadoop.hive.metastore.api.HiveObjectPrivilege)4 HiveObjectRef (org.apache.hadoop.hive.metastore.api.HiveObjectRef)4 PrincipalPrivilegeSet (org.apache.hadoop.hive.metastore.api.PrincipalPrivilegeSet)4 PrincipalType (org.apache.hadoop.hive.metastore.api.PrincipalType)4 LinkedList (java.util.LinkedList)3