Example usage for java.sql ResultSet getArray

List of usage examples for java.sql ResultSet getArray

Introduction

In this page you can find the example usage for java.sql ResultSet getArray.

Prototype

Array getArray(String columnLabel) throws SQLException;

Source Link

Document

Retrieves the value of the designated column in the current row of this ResultSet object as an Array object in the Java programming language.

Usage

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public Table getTable(String dbName, String tableName) throws MetaException, NoSuchObjectException {
    boolean success = false;

    Connection con;//from  w  w w.j  a va 2s. c  o m
    Statement ps = null;
    Table tbl = new Table();

    dbName = dbName.toLowerCase();
    tableName = tableName.toLowerCase();

    try {
        con = getSegmentConnectionForRead(dbName);
    } catch (MetaStoreConnectException e1) {
        LOG.error("get table error, db=" + dbName + ", tbl=" + tableName + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("get table error, db=" + dbName + ", tbl=" + tableName + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        ps = con.createStatement();

        String sql = "SELECT tbl_id, create_time"
                + ", is_compressed, retention, tbl_type, db_name, tbl_name, tbl_owner "
                + ", tbl_format, pri_part_type, sub_part_type, pri_part_key, sub_part_key "
                + ", input_format, output_format, serde_name, serde_lib, tbl_location, tbl_comment "
                + " from TBLS where db_name='" + dbName + "' and tbl_name='" + tableName + "'";

        ResultSet tblSet = ps.executeQuery(sql);
        boolean isTblFind = false;
        StorageDescriptor sd = null;
        SerDeInfo sdInfo = null;
        String priPartKey = null;
        String subPartKey = null;
        Partition priPart = null;
        Partition subPart = null;
        long tblID = 0;

        String comment = null;
        String format = null;
        Timestamp createTime = null;
        String tblType = null;

        boolean hasPriPart = false;
        boolean hasSubPart = false;

        while (tblSet.next()) {
            isTblFind = true;
            tblID = tblSet.getLong(1);

            createTime = tblSet.getTimestamp(2);

            if (createTime != null) {
                tbl.setCreateTime((int) (createTime.getTime() / 1000));
            }

            sd = new StorageDescriptor();
            sdInfo = new SerDeInfo();
            sd.setCompressed(tblSet.getBoolean(3));

            tbl.setRetention((int) tblSet.getLong(4));
            tblType = tblSet.getString(5);
            tbl.setTableType(tblType);
            tbl.setDbName(tblSet.getString(6));
            tbl.setTableName(tblSet.getString(7));
            tbl.setOwner(tblSet.getString(8));

            format = tblSet.getString(9);

            priPartKey = tblSet.getString(12);
            subPartKey = tblSet.getString(13);

            if (priPartKey != null && !priPartKey.isEmpty()) {
                hasPriPart = true;
                priPart = new Partition();
                priPart.setLevel(0);
                priPart.setDbName(tblSet.getString(6));
                priPart.setTableName(tblSet.getString(7));
                priPart.setParType(tblSet.getString(10));
            }

            if (subPartKey != null && !subPartKey.isEmpty()) {
                hasSubPart = true;
                subPart = new Partition();
                subPart.setLevel(1);
                subPart.setDbName(tblSet.getString(6));
                subPart.setTableName(tblSet.getString(7));
                subPart.setParType(tblSet.getString(11));
            }

            sd.setInputFormat(tblSet.getString(14));
            sd.setOutputFormat(tblSet.getString(15));
            sdInfo.setName(tblSet.getString(16));
            sdInfo.setSerializationLib(tblSet.getString(17));
            sd.setLocation(tblSet.getString(18));
            comment = tblSet.getString(19);

            break;
        }

        tblSet.close();

        if (!isTblFind) {
            LOG.error(dbName + "." + tableName + " table not found");
            throw new NoSuchObjectException(dbName + "." + tableName + " table not found");
        }

        List<FieldSchema> fieldList = new ArrayList<FieldSchema>();
        Map<String, FieldSchema> fieldMap = new LinkedHashMap<String, FieldSchema>();

        sql = "SELECT column_name, type_name, comment from columns where tbl_id=" + tblID
                + " order by column_index asc";
        ResultSet colSet = ps.executeQuery(sql);
        while (colSet.next()) {
            FieldSchema field = new FieldSchema();
            field.setName(colSet.getString(1));
            field.setType(colSet.getString(2));
            field.setComment(colSet.getString(3));

            fieldList.add(field);
            fieldMap.put(colSet.getString(1), field);
        }
        colSet.close();

        sd.setCols(fieldList);

        sql = "SELECT param_type, param_key, param_value  from table_params where tbl_id=" + tblID;
        ResultSet paramSet = ps.executeQuery(sql);
        Map<String, String> tblParamMap = new HashMap<String, String>();
        Map<String, String> sdParamMap = new HashMap<String, String>();
        Map<String, String> serdeParam = new HashMap<String, String>();

        while (paramSet.next()) {
            String type = paramSet.getString(1);
            if (type == null)
                continue;

            if (type.equalsIgnoreCase("sd")) {
                sdParamMap.put(paramSet.getString(2), paramSet.getString(3));
            } else if (type.equalsIgnoreCase("serde")) {
                serdeParam.put(paramSet.getString(2), paramSet.getString(3));
            } else if (type.equalsIgnoreCase("tbl")) {
                tblParamMap.put(paramSet.getString(2), paramSet.getString(3));
            } else {
                tblParamMap.put(paramSet.getString(2), paramSet.getString(3));
            }
        }
        paramSet.close();

        if (comment != null && !comment.isEmpty()) {
            tblParamMap.put("comment", comment);
        }

        if (format != null && !format.isEmpty()) {
            tblParamMap.put("type", format);
        }

        tbl.setParameters(tblParamMap);
        sd.setParameters(sdParamMap);
        sdInfo.setParameters(serdeParam);

        List<String> bucketCols = new ArrayList<String>();
        sql = "select bucket_col_name from bucket_cols where tbl_id=" + tblID + " order by col_index asc";
        ResultSet bucketSet = ps.executeQuery(sql);
        while (bucketSet.next()) {
            bucketCols.add(bucketSet.getString(1));
        }

        bucketSet.close();
        if (bucketCols.size() > 0) {
            sd.setBucketCols(bucketCols);
            String numBucketStr = sd.getParameters().get("NUM_BUCKETS");
            if (numBucketStr == null) {
                sd.setNumBuckets(-1);
            } else {
                sd.setNumBuckets(Integer.valueOf(numBucketStr));
            }
        } else {
            sd.setBucketCols(bucketCols);
            sd.setNumBuckets(-1);
        }

        sd.getParameters().remove("NUM_BUCKETS");

        List<Order> sortCols = new ArrayList<Order>();
        sql = "select sort_column_name, sort_order from sort_cols where tbl_id=" + tblID
                + " order by col_index asc";
        ResultSet sortSet = ps.executeQuery(sql);
        while (sortSet.next()) {
            Order o = new Order();
            o.setCol(sortSet.getString(1));
            o.setOrder(sortSet.getInt(2));
            sortCols.add(o);
        }

        sortSet.close();
        sd.setSortCols(sortCols);

        sd.setSerdeInfo(sdInfo);
        tbl.setSd(sd);

        if (hasPriPart) {
            sql = "SELECT level, part_name, part_values from  PARTITIONS where tbl_id=" + tblID;
            ResultSet partSet = ps.executeQuery(sql);
            Map<String, List<String>> priPartSpace = new LinkedHashMap<String, List<String>>();
            Map<String, List<String>> subPartSpace = new LinkedHashMap<String, List<String>>();

            while (partSet.next()) {
                int level = partSet.getInt(1);
                switch (level) {
                case 0:
                    String priName = partSet.getString(2);
                    List<String> priValueList = new ArrayList<String>();
                    Array priSpaceArray = partSet.getArray(3);

                    if (priSpaceArray != null) {
                        ResultSet priValueSet = priSpaceArray.getResultSet();

                        while (priValueSet.next()) {
                            priValueList.add(priValueSet.getString(2));
                        }
                    }

                    priPartSpace.put(priName, priValueList);
                    break;

                case 1:
                    String subName = partSet.getString(2);
                    List<String> subValueList = new ArrayList<String>();
                    Array subSpaceArray = partSet.getArray(3);

                    if (subSpaceArray != null) {
                        ResultSet subValueSet = subSpaceArray.getResultSet();
                        while (subValueSet.next()) {
                            subValueList.add(subValueSet.getString(2));
                        }
                    }

                    subPartSpace.put(subName, subValueList);
                    break;

                default:
                    break;
                }
            }

            partSet.close();

            priPart.setParSpaces(priPartSpace);

            priPart.setParKey(fieldMap.get(priPartKey.toLowerCase()));

            if (hasSubPart) {
                subPart.setParSpaces(subPartSpace);
                subPart.setParKey(fieldMap.get(subPartKey.toLowerCase()));
            }
        }

        tbl.setPriPartition(priPart);
        tbl.setSubPartition(subPart);

        if (tblType.equalsIgnoreCase("VIRTUAL_VIEW")) {
            sql = "select view_original_text, view_expanded_text, vtables from " + " tdwview where tbl_id="
                    + tblID;

            ResultSet viewSet = ps.executeQuery(sql);
            while (viewSet.next()) {
                tbl.setViewOriginalText(viewSet.getString(1));
                tbl.setViewExpandedText(viewSet.getString(2));
                tbl.setVtables(viewSet.getString(3));
                break;
            }
        }

        con.commit();
        success = true;
    } catch (SQLException sqlex) {
        sqlex.printStackTrace();
        LOG.error("get table error, db=" + dbName + ", tbl=" + tableName + ", msg=" + sqlex.getMessage());
        throw new MetaException(sqlex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(ps);
        closeConnection(con);
    }

    if (success)
        return tbl;
    else
        return null;
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

public void addPartition(String dbName, String tblName, AddPartitionDesc addPartitionDesc)
        throws InvalidObjectException, MetaException {
    boolean success = false;

    Connection con = null;/*from w w  w  . j a  va 2s .c  om*/
    PreparedStatement ps = null;
    Statement stmt = null;
    dbName = dbName.toLowerCase();
    tblName = tblName.toLowerCase();

    boolean isPathMaked = false;
    ArrayList<Path> pathToMake = new ArrayList<Path>();
    Warehouse wh = new Warehouse(hiveConf);

    long tblID = 0;

    try {
        con = getSegmentConnection(dbName);
    } catch (MetaStoreConnectException e1) {
        LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                + addPartitionDesc.getLevel() + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                + addPartitionDesc.getLevel() + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
        stmt = con.createStatement();

        String tblType = null;
        boolean hasPriPart = false;
        boolean hasSubPart = false;
        String priPartKey = null;
        String subPartKey = null;
        String priPartType = null;
        String subPartType = null;

        String priKeyType = null;
        String subKeyType = null;
        ResultSet tblSet = null;
        boolean isTblFind = false;
        boolean isColFind = false;

        String tblFormat = null;
        String tblLocation = null;

        PrimitiveTypeInfo pti = null;
        ObjectInspector StringIO = null;
        ObjectInspector ValueIO = null;
        ObjectInspectorConverters.Converter converter1 = null;
        ObjectInspectorConverters.Converter converter2 = null;

        ArrayList<String> partToAdd = new ArrayList<String>();
        String sql = null;

        HiveConf hconf = (HiveConf) hiveConf;
        boolean externalPartition = hconf.getBoolVar(HiveConf.ConfVars.HIVESUPPORTEXTERNALPARTITION);

        if (addPartitionDesc.getLevel() == 0) {
            sql = "SELECT tbl_id, tbl_type, pri_part_type, pri_part_key, tbl_format, tbl_location"
                    + " from TBLS where db_name='" + dbName + "' and tbl_name='" + tblName + "'";

            tblSet = stmt.executeQuery(sql);
            isTblFind = false;

            while (tblSet.next()) {
                isTblFind = true;
                tblID = tblSet.getLong(1);
                tblType = tblSet.getString(2);
                priPartKey = tblSet.getString(4);
                priPartType = tblSet.getString(3);
                tblFormat = tblSet.getString(5);
                tblLocation = tblSet.getString(6);

                if (priPartType != null && !priPartType.isEmpty()) {
                    hasPriPart = true;
                }
                break;
            }
            tblSet.close();

            if (!isTblFind) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                        + addPartitionDesc.getLevel() + ", msg=" + "can not find table " + dbName + ":"
                        + tblName);

                throw new MetaException("can not find table " + dbName + ":" + tblName);
            }

            if (!tblType.equalsIgnoreCase("MANAGED_TABLE")) {
                if (tblType.equalsIgnoreCase("EXTERNAL_TABLE") && tblFormat != null
                        && tblFormat.equalsIgnoreCase("pgdata")) {
                    LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + addPartitionDesc.getLevel() + ", msg=" + tblType + ":" + tblFormat
                            + " can not support alter partition");
                    throw new MetaException(tblType + ":" + tblFormat + " can not support alter partition");
                }

                if (externalPartition && tblType.equalsIgnoreCase("EXTERNAL_TABLE")
                        && (tblFormat == null || !tblFormat.equalsIgnoreCase("pgdata"))) {
                } else {
                    LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + addPartitionDesc.getLevel() + ", msg=" + tblType
                            + " can not support alter partition");

                    throw new MetaException(tblType + " can not support alter partition");
                }
            }

            if (!hasPriPart) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                        + addPartitionDesc.getLevel() + ", msg=" + "table " + dbName + ":" + tblName
                        + " is not pri-partitioned");

                throw new MetaException("table " + dbName + ":" + tblName + " is not pri-partitioned");
            }

            sql = "SELECT type_name from COLUMNS where tbl_id=" + tblID + " and column_name='"
                    + priPartKey.toLowerCase() + "'";
            isColFind = false;
            ResultSet colSet = stmt.executeQuery(sql);
            while (colSet.next()) {
                isColFind = true;
                priKeyType = colSet.getString(1);
                break;
            }
            colSet.close();

            if (!isColFind) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                        + addPartitionDesc.getLevel() + ", msg=" + "table "
                        + "can not find partition key information " + priPartKey);

                throw new MetaException("can not find partition key information " + priPartKey);
            }

            pti = new PrimitiveTypeInfo();
            pti.setTypeName(priKeyType);
            StringIO = PrimitiveObjectInspectorFactory
                    .getPrimitiveJavaObjectInspector(PrimitiveCategory.STRING);
            ValueIO = PrimitiveObjectInspectorFactory
                    .getPrimitiveWritableObjectInspector(pti.getPrimitiveCategory());
            converter1 = ObjectInspectorConverters.getConverter(StringIO, ValueIO);
            converter2 = ObjectInspectorConverters.getConverter(StringIO, ValueIO);

            if ((addPartitionDesc.getPartType().equalsIgnoreCase("RANGE_PARTITION")
                    && !priPartType.equalsIgnoreCase("range"))
                    || (addPartitionDesc.getPartType().equalsIgnoreCase("LIST_PARTITION")
                            && !priPartType.equalsIgnoreCase("list"))) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                        + addPartitionDesc.getLevel() + ", msg=" + "can not add  a "
                        + addPartitionDesc.getPartType() + " partition, but the pri-partition type is "
                        + priPartType);

                throw new MetaException("can not add  a " + addPartitionDesc.getPartType()
                        + " partition, but the pri-partition type is " + priPartType);
            }

            LinkedHashMap<String, List<String>> partSpaces = new LinkedHashMap<String, List<String>>();
            Set<String> subPartNameSet = new TreeSet<String>();

            sql = "SELECT level, part_name, part_values from PARTITIONS where" + " tbl_id=" + tblID;// + " order by level asc";

            ResultSet partSet = stmt.executeQuery(sql);
            int partLevel = 0;

            while (partSet.next()) {
                partLevel = partSet.getInt(1);

                if (partLevel == 0) {
                    String partName = partSet.getString(2);
                    List<String> valueList = new ArrayList<String>();
                    Array spaceArray = partSet.getArray(3);

                    ResultSet priValueSet = spaceArray.getResultSet();

                    while (priValueSet.next()) {
                        valueList.add(priValueSet.getString(2));
                    }

                    partSpaces.put(partName, valueList);
                } else if (partLevel == 1) {
                    String partName = partSet.getString(2);
                    subPartNameSet.add(partName);
                }
            }
            partSet.close();

            partToAdd = new ArrayList<String>();

            LinkedHashMap<String, List<String>> addPartSpaces = (LinkedHashMap<String, List<String>>) addPartitionDesc
                    .getParSpaces();

            Iterator<String> itr = addPartSpaces.keySet().iterator();

            while (itr.hasNext()) {
                String key = itr.next().toLowerCase();
                if (partSpaces.containsKey(key)) {
                    LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + addPartitionDesc.getLevel() + ", msg=" + "table : " + tblName
                            + " have already contain a pri parititon named: " + key);

                    throw new MetaException(
                            "table : " + tblName + " have already contain a pri parititon named: " + key);
                }
                partToAdd.add(key);
            }

            Iterator<List<String>> listItr = addPartSpaces.values().iterator();

            while (listItr.hasNext()) {
                Iterator<String> valueItr = listItr.next().iterator();
                if (valueItr.hasNext()) {
                    String value = valueItr.next();

                    if (converter1.convert(value) == null) {
                        LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                                + addPartitionDesc.getLevel() + ", msg=" + "value : " + value
                                + " should be type of " + priKeyType);

                        throw new MetaException("value : " + value + " should be type of " + priKeyType);
                    }

                    Iterator<List<String>> PartValuesItr = partSpaces.values().iterator();
                    while (PartValuesItr.hasNext()) {
                        if (PartValuesItr.next().contains(value)) {
                            LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                                    + addPartitionDesc.getLevel() + ", msg=" + "table : " + tblName
                                    + " have already contain a pri partition contain value: " + value);

                            throw new MetaException("table : " + tblName
                                    + " have already contain a pri partition contain value: " + value);
                        }
                    }
                }
            }

            ps = con.prepareStatement(
                    "INSERT INTO partitions(level, tbl_id, " + " part_name, part_values) values(?,?,?,?)");

            for (Map.Entry<String, List<String>> entry : addPartSpaces.entrySet()) {
                ps.setInt(1, 0);
                ps.setLong(2, tblID);

                Array spaceArray = con.createArrayOf("varchar", entry.getValue().toArray());
                ps.setArray(4, spaceArray);
                ps.setString(3, entry.getKey());

                ps.addBatch();
            }
            ps.executeBatch();

            if (!tblType.equalsIgnoreCase("EXTERNAL_TABLE")) {
                for (String partName : partToAdd) {
                    if (tblLocation == null || tblLocation.trim().isEmpty()) {
                        pathToMake.addAll(wh.getPriPartitionPaths(dbName, tblName, partName, subPartNameSet));
                    } else {
                        pathToMake.addAll(Warehouse.getPriPartitionPaths(new Path(tblLocation), partName,
                                subPartNameSet));
                    }
                }
            } else {
                for (String partName : partToAdd) {
                    pathToMake.addAll(
                            Warehouse.getPriPartitionPaths(new Path(tblLocation), partName, subPartNameSet));
                }
            }
        } else if (addPartitionDesc.getLevel() == 1) {
            sql = "SELECT tbl_id, tbl_type, sub_part_type, sub_part_key, tbl_format, tbl_location"
                    + " from TBLS where db_name='" + dbName.toLowerCase() + "' and tbl_name='"
                    + tblName.toLowerCase() + "'";

            tblSet = stmt.executeQuery(sql);
            isTblFind = false;

            while (tblSet.next()) {
                isTblFind = true;
                tblID = tblSet.getLong(1);
                tblType = tblSet.getString(2);
                subPartKey = tblSet.getString(4);
                subPartType = tblSet.getString(3);
                tblFormat = tblSet.getString(5);
                tblLocation = tblSet.getString(6);

                if (subPartType != null && !subPartType.isEmpty()) {
                    hasSubPart = true;
                }

                break;
            }

            tblSet.close();
            if (!isTblFind) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                        + addPartitionDesc.getLevel() + ", msg=" + "can not find table " + dbName + ":"
                        + tblName);

                throw new MetaException("can not find table " + dbName + ":" + tblName);
            }

            if (!tblType.equalsIgnoreCase("MANAGED_TABLE")) {
                if (tblType.equalsIgnoreCase("EXTERNAL_TABLE") && tblFormat != null
                        && tblFormat.equalsIgnoreCase("pgdata")) {
                    LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + addPartitionDesc.getLevel() + ", msg=" + tblType + ":" + tblFormat
                            + " can not support alter partition");
                    throw new MetaException(tblType + ":" + tblFormat + " can not support alter partition");
                }

                if (externalPartition && tblType.equalsIgnoreCase("EXTERNAL_TABLE")
                        && (tblFormat == null || !tblFormat.equalsIgnoreCase("pgdata"))) {
                } else {
                    LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + addPartitionDesc.getLevel() + ", msg=" + tblType
                            + " can not support alter partition");

                    throw new MetaException(tblType + " can not support alter partition");
                }
            }

            if (!hasSubPart) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                        + addPartitionDesc.getLevel() + ", msg=" + "table " + dbName + ":" + tblName
                        + " is not sun-partitioned");

                throw new MetaException("table " + dbName + ":" + tblName + " is not sun-partitioned");
            }

            sql = "SELECT type_name from COLUMNS where tbl_id=" + tblID + " and column_name='"
                    + subPartKey.toLowerCase() + "'";

            isColFind = false;
            ResultSet colSet = stmt.executeQuery(sql);
            while (colSet.next()) {
                isColFind = true;
                subKeyType = colSet.getString(1);
                break;
            }

            colSet.close();

            if (!isColFind) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                        + addPartitionDesc.getLevel() + ", msg=" + "can not find partition key information "
                        + priPartKey);

                throw new MetaException("can not find partition key information " + priPartKey);
            }

            pti = new PrimitiveTypeInfo();
            pti.setTypeName(subKeyType);
            StringIO = PrimitiveObjectInspectorFactory
                    .getPrimitiveJavaObjectInspector(PrimitiveCategory.STRING);
            ValueIO = PrimitiveObjectInspectorFactory
                    .getPrimitiveWritableObjectInspector(pti.getPrimitiveCategory());
            converter1 = ObjectInspectorConverters.getConverter(StringIO, ValueIO);
            converter2 = ObjectInspectorConverters.getConverter(StringIO, ValueIO);

            if ((addPartitionDesc.getPartType().equalsIgnoreCase("RANGE_PARTITION")
                    && !subPartType.equalsIgnoreCase("range"))
                    || (addPartitionDesc.getPartType().equalsIgnoreCase("LIST_PARTITION")
                            && !subPartType.equalsIgnoreCase("list"))) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                        + addPartitionDesc.getLevel() + ", msg=" + "you can not add  a "
                        + addPartitionDesc.getPartType() + " partition, but the sub-partition type is "
                        + subPartType);

                throw new MetaException("you can not add  a " + addPartitionDesc.getPartType()
                        + " partition, but the sub-partition type is " + subPartType);
            }

            LinkedHashMap<String, List<String>> partSpaces = new LinkedHashMap<String, List<String>>();
            Set<String> partNameSet = new TreeSet<String>();

            sql = "SELECT level,  part_name, part_values from PARTITIONS where" + " tbl_id=" + tblID;// + " order by level asc";

            ResultSet partSet = stmt.executeQuery(sql);
            int partLevel = 0;

            while (partSet.next()) {
                partLevel = partSet.getInt(1);

                if (partLevel == 1) {
                    String partName = partSet.getString(2);
                    List<String> valueList = new ArrayList<String>();
                    Array spaceArray = partSet.getArray(3);

                    ResultSet priValueSet = spaceArray.getResultSet();

                    while (priValueSet.next()) {
                        valueList.add(priValueSet.getString(2));
                    }
                    partSpaces.put(partName, valueList);
                } else if (partLevel == 0) {
                    String partName = partSet.getString(2);
                    partNameSet.add(partName);
                }
            }

            partToAdd = new ArrayList<String>();

            LinkedHashMap<String, List<String>> addPartSpaces = (LinkedHashMap<String, List<String>>) addPartitionDesc
                    .getParSpaces();

            Iterator<String> itr = addPartSpaces.keySet().iterator();

            while (itr.hasNext()) {
                String key = itr.next().toLowerCase();
                if (partSpaces.containsKey(key)) {
                    LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + addPartitionDesc.getLevel() + ", msg=" + "table : " + tblName
                            + " have already contain a sub parititon named: " + key);

                    throw new MetaException(
                            "table : " + tblName + " have already contain a sub parititon named: " + key);
                }

                if (key.equalsIgnoreCase("default")) {
                    LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + addPartitionDesc.getLevel() + ", msg="
                            + "use : 'alter table tblname add default subpartition' to add default subpartition!");

                    throw new MetaException(
                            "use : 'alter table tblname add default subpartition' to add default subpartition!");
                }
                partToAdd.add(key);
            }

            Iterator<List<String>> listItr = addPartSpaces.values().iterator();

            while (listItr.hasNext()) {
                Iterator<String> valueItr = listItr.next().iterator();
                if (valueItr.hasNext()) {
                    String value = valueItr.next();

                    if (converter1.convert(value) == null) {
                        LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                                + addPartitionDesc.getLevel() + ", msg=" + "value : " + value
                                + " should be type of " + priKeyType);

                        throw new MetaException("value : " + value + " should be type of " + priKeyType);
                    }

                    Iterator<List<String>> PartValuesItr = partSpaces.values().iterator();
                    while (PartValuesItr.hasNext()) {
                        if (PartValuesItr.next().contains(value)) {
                            LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                                    + addPartitionDesc.getLevel() + ", msg=" + "table : " + tblName
                                    + " have already contain a sub partition contain value: " + value);

                            throw new MetaException("table : " + tblName
                                    + " have already contain a sub partition contain value: " + value);
                        }
                    }
                }
            }

            ps = con.prepareStatement(
                    "INSERT INTO partitions(level, tbl_id, " + " part_name, part_values) values(?,?,?,?)");

            for (Map.Entry<String, List<String>> entry : addPartSpaces.entrySet()) {
                ps.setInt(1, 1);
                ps.setLong(2, tblID);

                Array spaceArray = con.createArrayOf("varchar", entry.getValue().toArray());
                ps.setArray(4, spaceArray);
                ps.setString(3, entry.getKey());

                ps.addBatch();
            }
            ps.executeBatch();

            if (!tblType.equalsIgnoreCase("EXTERNAL_TABLE")) {
                for (String partName : partToAdd) {
                    if (tblLocation == null || tblLocation.trim().isEmpty()) {
                        pathToMake.addAll(wh.getSubPartitionPaths(dbName, tblName, partNameSet, partName));
                    } else {
                        pathToMake.addAll(
                                Warehouse.getSubPartitionPaths(new Path(tblLocation), partNameSet, partName));
                    }
                }
            } else {
                for (String partName : partToAdd) {
                    pathToMake.addAll(
                            Warehouse.getSubPartitionPaths(new Path(tblLocation), partNameSet, partName));
                }
            }
        }

        con.commit();
        success = true;
    } catch (SQLException ex) {
        ex.printStackTrace();
        LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                + addPartitionDesc.getLevel() + ", msg=" + ex.getMessage());

        throw new MetaException(ex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }

            if (isPathMaked) {
                for (Path path : pathToMake) {
                    wh.deleteDir(path, false);
                }
            }
        }

        closeStatement(ps);
        closeConnection(con);
    }

    if (success) {
        boolean mkDirOK = false;
        List<Path> createdPath = new ArrayList<Path>();
        try {
            for (Path path : pathToMake) {
                mkDirOK = wh.mkdirs(path);
                if (!mkDirOK) {
                    break;
                }

                createdPath.add(path);
            }
        } catch (Exception x) {
            mkDirOK = false;
        }

        if (!mkDirOK) {
            dropPartitionMeta(dbName, tblID, addPartitionDesc);
            if (!createdPath.isEmpty()) {
                for (Path path : createdPath) {
                    wh.deleteDir(path, true);
                }
            }

            throw new MetaException("can not create hdfs path, add partition failed");
        }

    }
}