private String generateCreateTableSql()

in odps-sdk/odps-sdk-core/src/main/java/com/aliyun/odps/Tables.java [1349:1476]


    private String generateCreateTableSql() {
      if (!StringUtils.isNullOrEmpty(selectStatement)) {
        return handleSelectStatementCause();
      }

      StringBuilder sql = new StringBuilder();
      // create [external] table [if not exists] <table_name>
      if (tableFormat == TableFormat.EXTERNAL) {
        sql.append("CREATE EXTERNAL TABLE ");
      } else {
        sql.append("CREATE TABLE ");
      }
      if (ifNotExists) {
        sql.append("IF NOT EXISTS ");
      }
      sql.append(NameSpaceSchemaUtils.getFullName(projectName, schemaName, tableName));

      // [(<col_name> <data_type> [not null] [default <default_value>] [comment <col_comment>], ...)]
      List<Column> columns = tableSchema.getColumns();
      sql.append(" (");
      for (int i = 0; i < columns.size(); i++) {
        Column column = columns.get(i);
        sql.append(CommonUtils.quoteRef(column.getName())).append(" ")
            .append(getTypeName(column.getTypeInfo()));
        if (!column.isNullable()) {
          sql.append(" NOT NULL");
        }
        if (StringUtils.isNotBlank(column.getDefaultValue())) {
          sql.append(" DEFAULT ").append(CommonUtils.quoteStr(column.getDefaultValue()));
        }
        if (column.getComment() != null) {
          sql.append(" COMMENT ").append(CommonUtils.quoteStr(column.getComment()));
        }
        if (i + 1 < columns.size()) {
          sql.append(',');
        }
      }
      if (primaryKeys != null && !primaryKeys.isEmpty()) {
        sql.append(", PRIMARY KEY(").append(primaryKeys.stream().map(CommonUtils::quoteRef).collect(
            Collectors.joining(","))).append(")");
      }
      sql.append(')');

      // [comment <table_comment>]
      if (comment != null) {
        sql.append(" COMMENT ").append(CommonUtils.quoteStr(comment)).append(" ");
      }

      // [partitioned by (<col_name> <data_type> [comment <col_comment>], ...)]
      // [auto partitioned by (<generate_expression> AS <col_name>)]
      List<Column> partitionColumns = tableSchema.getPartitionColumns();
      if (!partitionColumns.isEmpty()) {
        // Currently, the auto-partition table only has one partition column (there may be more in the future),
        // so here we only take the first column to check to determine whether the table is an auto-partition table.
        if (partitionColumns.get(0).getGenerateExpression() != null) {
          sql.append(" AUTO PARTITIONED BY (").append(partitionColumns.get(0).getGenerateExpression())
              .append(" AS ").append(CommonUtils.quoteRef(partitionColumns.get(0).getName()))
              .append(")");
        } else {
          sql.append(" PARTITIONED BY (");
          for (int i = 0; i < partitionColumns.size(); i++) {
            Column column = partitionColumns.get(i);
            sql.append(CommonUtils.quoteRef(column.getName())).append(" ")
                .append(getTypeName(column.getTypeInfo()));
            if (column.getComment() != null) {
              sql.append(" COMMENT ").append(CommonUtils.quoteStr(column.getComment()));
            }
            if (i + 1 < partitionColumns.size()) {
              sql.append(',');
            }
          }
          sql.append(')');
        }
      }
      // [clustered by | range clustered by (<col_name> [, <col_name>, ...]) [sorted by (<col_name> [asc | desc] [, <col_name> [asc | desc] ...])] into <number_of_buckets> buckets]
      if (clusterInfo != null) {
        sql.append(clusterInfo);
      }
      // [stored by StorageHandler]
      if (storageHandler != null) {
        sql.append(" STORED BY ").append(CommonUtils.quoteStr(storageHandler));
      }
      // [with serdeproperties (options)]
      if (serdeProperties != null && !serdeProperties.isEmpty()) {
        sql.append(" WITH SERDEPROPERTIES (");
        int index = 0;
        for (Map.Entry<String, String> entry : serdeProperties.entrySet()) {
          index++;
          sql.append(CommonUtils.quoteStr(entry.getKey())).append("=").append(CommonUtils.quoteStr(entry.getValue()));
          if (index != serdeProperties.size()) {
            sql.append(" , ");
          }
        }
        sql.append(")");
      }
      // [location <osslocation>]
      if (!StringUtils.isNullOrEmpty(location)) {
        sql.append(" LOCATION ").append(CommonUtils.quoteStr(location));
      }
      // [USING '<resource_name>']
      if (usingResources != null && !usingResources.isEmpty()) {
        sql.append(" USING ").append(CommonUtils.quoteStr(String.join(",", usingResources)));
      }
      // [tblproperties("transactional"="true")]
      Map<String, String> allTblProperties = getTblProperties();
      if (!allTblProperties.isEmpty()) {
        sql.append(" TBLPROPERTIES(");
        for (Map.Entry<String, String> entry : allTblProperties.entrySet()) {
          sql.append(CommonUtils.quoteStr(entry.getKey())).append("=").append(CommonUtils.quoteStr(entry.getValue())).append(",");
        }
        sql.deleteCharAt(sql.length() - 1);
        sql.append(")");
      }
      // [lifecycle <days>]
      if (lifeCycle != null) {
        sql.append(" LIFECYCLE ").append(lifeCycle);
      }

      if (dataHubInfo != null) {
        sql.append(dataHubInfo);
      }

      sql.append(';');
      if (debug) {
        System.out.println(sql);
      }
      return sql.toString();
    }