public void upgradeBaseSchema(Connection conn, int currentVersion) {
    if (!isLoaded()) {
      throw new TajoInternalError("Database schema files are not loaded.");
    }

    final List<SchemaPatch> candidatePatches = new ArrayList<>();
    Statement stmt;

    for (SchemaPatch patch : this.catalogStore.getPatches()) {
      if (currentVersion >= patch.getPriorVersion()) {
        candidatePatches.add(patch);
      }
    }

    Collections.sort(candidatePatches);
    try {
      stmt = conn.createStatement();
    } catch (SQLException e) {
      throw new TajoInternalError(e);
    }

    for (SchemaPatch patch : candidatePatches) {
      for (DatabaseObject object : patch.getObjects()) {
        try {
          stmt.executeUpdate(object.getSql());
          LOG.info(object.getName() + " " + object.getType() + " was created or altered.");
        } catch (SQLException e) {
          throw new TajoInternalError(e);
        }
      }
    }

    CatalogUtil.closeQuietly(stmt);
  }
    protected List<DatabaseObject> mergeDatabaseObjects(List<DatabaseObject> objects) {
      int maxIdx = -1;

      for (DatabaseObject object : objects) {
        maxIdx = Math.max(maxIdx, object.getOrder());
      }

      final List<DatabaseObject> orderedObjects = createListAndFillNull(maxIdx);
      final List<DatabaseObject> unorderedObjects = new ArrayList<>();
      final List<DatabaseObject> mergedObjects = new ArrayList<>();

      for (DatabaseObject object : objects) {
        if (object.getOrder() > -1) {
          int objIdx = object.getOrder();

          if (objIdx < orderedObjects.size() && orderedObjects.get(objIdx) != null) {
            throw new TajoInternalError(
                "This catalog configuration contains duplicated order of DatabaseObject");
          }

          orderedObjects.add(objIdx, object);
        } else {
          unorderedObjects.add(object);
        }
      }

      for (DatabaseObject object : orderedObjects) {
        if (object != null) {
          mergedObjects.add(object);
        }
      }

      for (DatabaseObject object : unorderedObjects) {
        if (object != null) {
          mergedObjects.add(object);
        }
      }

      return mergedObjects;
    }
  public void createBaseSchema(Connection conn) {
    Statement stmt;

    if (!isLoaded()) {
      throw new TajoInternalError("Database schema files are not loaded.");
    }

    try {
      stmt = conn.createStatement();
    } catch (SQLException e) {
      throw new TajoInternalError(e);
    }

    for (DatabaseObject object : catalogStore.getSchema().getObjects()) {
      try {
        String[] params;
        if (DatabaseObjectType.INDEX == object.getType()) {
          params = new String[2];
          params[0] = object.getDependsOn();
          params[1] = object.getName();
        } else {
          params = new String[1];
          params[0] = object.getName();
        }

        if (checkExistence(conn, object.getType(), params)) {
          LOG.info("Skip to create " + object.getName() + " databse object. Already exists.");
        } else {
          stmt.executeUpdate(object.getSql());
          LOG.info(object.getName() + " " + object.getType() + " is created.");
        }
      } catch (SQLException e) {
        throw new TajoInternalError(e);
      }
    }

    CatalogUtil.closeQuietly(stmt);
  }
  public void dropBaseSchema(Connection conn) {
    if (!isLoaded()) {
      throw new TajoInternalError("Schema files are not loaded yet.");
    }

    List<DatabaseObject> failedObjects = new ArrayList<>();
    Statement stmt = null;

    try {
      stmt = conn.createStatement();
    } catch (SQLException e) {
      throw new TajoInternalError(e);
    }

    for (DatabaseObject object : catalogStore.getSchema().getObjects()) {
      try {
        if (DatabaseObjectType.TABLE == object.getType()
            || DatabaseObjectType.SEQUENCE == object.getType()
            || DatabaseObjectType.VIEW == object.getType()) {
          stmt.executeUpdate(getDropSQL(object.getType(), object.getName()));
        }
      } catch (SQLException e) {
        failedObjects.add(object);
      }
    }
    CatalogUtil.closeQuietly(stmt);

    if (failedObjects.size() > 0) {
      StringBuffer errorMessage = new StringBuffer(64);
      errorMessage.append("Failed to drop database objects ");

      for (int idx = 0; idx < failedObjects.size(); idx++) {
        DatabaseObject object = failedObjects.get(idx);
        errorMessage.append(object.getType().toString()).append(" ").append(object.getName());
        if ((idx + 1) < failedObjects.size()) {
          errorMessage.append(',');
        }
      }

      LOG.warn(errorMessage.toString());
    }
  }
  public boolean isInitialized(Connection conn) {
    if (!isLoaded()) {
      throw new TajoInternalError("Database schema files are not loaded.");
    }

    boolean result = true;

    for (DatabaseObject object : catalogStore.getSchema().getObjects()) {
      try {
        if (DatabaseObjectType.INDEX == object.getType()) {
          result &= checkExistence(conn, object.getType(), object.getDependsOn(), object.getName());
        } else {
          result &= checkExistence(conn, object.getType(), object.getName());
        }
      } catch (SQLException e) {
        throw new TajoInternalError(e);
      }

      if (!result) {
        break;
      }
    }
    return result;
  }