// Path: src/main/java/com/hortonworks/spark/sql/hive/llap/CreateTableBuilder.java
// public class CreateTableBuilder implements com.hortonworks.hwc.CreateTableBuilder {
//     private HiveWarehouseSession hive;
//     private String database;
//     private String tableName;
//     private boolean ifNotExists;
//     private List<Pair<String, String>> cols = new ArrayList<>();
//     private List<Pair<String, String>> parts = new ArrayList<>();
//     private List<Pair<String, String>> props = new ArrayList<>();
//     private String[] clusters;
//     private Long buckets;
// 
//     public CreateTableBuilder(HiveWarehouseSession hive, String database, String tableName) {
//         this.hive = hive;
//         this.tableName = tableName;
//         this.database = database;
//     }
// 
//     @Override
//     public CreateTableBuilder ifNotExists() {
//         this.ifNotExists = true;
//         return this;
//     }
// 
//     @Override
//     public CreateTableBuilder column(String name, String type) {
//         cols.add(Pair.of(name, type));
//         return this;
//     }
// 
//     @Override
//     public CreateTableBuilder partition(String name, String type) {
//         parts.add(Pair.of(name, type));
//         return this;
//     }
// 
//     @Override
//     public CreateTableBuilder prop(String key, String value) {
//         props.add(Pair.of(key, value));
//         return this;
//     }
// 
//     @Override
//     public CreateTableBuilder clusterBy(long numBuckets, String ... columns) {
//         this.buckets = numBuckets;
//         this.clusters = columns;
//         return this;
//     }
// 
//     @Override
//     public void create() {
//         hive.executeUpdate(this.toString());
//     }
// 
//     public String toString() {
//         StringBuilder builder = new StringBuilder();
//         builder.append(createTablePrelude(database, tableName, ifNotExists));
//         if(cols.size() > 0) {
//             List<String> colsStrings = new ArrayList<>();
//             for(Pair<String, String> col : cols) {
//                 colsStrings.add(col.getKey() + " " + col.getValue());
//             }
//             builder.append(columnSpec(join(",", colsStrings)));
//         }
//         if(parts.size() > 0) {
//             List<String> partsStrings = new ArrayList<>();
//             for(Pair<String, String> part : parts) {
//                 partsStrings.add(part.getKey() + " " + part.getValue());
//             }
//             builder.append(partitionSpec(join(",", partsStrings)));
//         }
//         if(clusters != null) {
//             builder.append(bucketSpec(join(",", clusters), buckets));
//         }
//         //Currently only managed ORC tables are supported
//         builder.append(" STORED AS ORC ");
//         if(props.size() > 0) {
//             List<String> keyValueStrings = new ArrayList<>();
//             for(Pair<String, String> keyValue : props) {
//                 keyValueStrings.add(format("\"%s\"=\"%s\"", keyValue.getKey(), keyValue.getValue()));
//             }
//             builder.append(tblProperties(join(",", keyValueStrings)));
//         }
//         return builder.toString();
//     }
// }


import com.hortonworks.spark.sql.hive.llap.CreateTableBuilder;
import org.apache.hadoop.hive.llap.FieldDesc;
import org.apache.hadoop.hive.llap.Schema;
import org.apache.spark.sql.types.*;
import java.util.ArrayList;
import java.util.List;
import static java.lang.String.format;
package com.hortonworks.spark.sql.hive.llap.util;




public class SchemaUtil {

  private static final String HIVE_TYPE_STRING = "HIVE_TYPE_STRING";

  public static StructType convertSchema(Schema schema) {
    List<FieldDesc> columns = schema.getColumns();
    List<String> types = new ArrayList<>();
    for(FieldDesc fieldDesc : columns) {
      String name;
      if(fieldDesc.getName().contains(".")) {
        name = fieldDesc.getName().split("\\.")[1];
      } else {
        name = fieldDesc.getName();
      }
      types.add(format("`%s` %s", name, fieldDesc.getTypeInfo().toString()));
    }
    return StructType.fromDDL(String.join(", ", types));
  }

  public static String[] columnNames(StructType schema) {
    String[] requiredColumns = new String[schema.length()];
    int i = 0;
    for (StructField field : schema.fields()) {
      requiredColumns[i] = field.name();
      i++;
    }
    return requiredColumns;
  }

  /**
   *
   * Builds create table query from given dataframe schema.
   *
   * @param schema  spark dataframe schema
   * @param database database name for create table query
   * @param table table name for create table query
   * @return a create table query string
   */
  public static String buildHiveCreateTableQueryFromSparkDFSchema(StructType schema, String database, String table) {
// Path: src/main/java/com/hortonworks/spark/sql/hive/llap/CreateTableBuilder.java
// public class CreateTableBuilder implements com.hortonworks.hwc.CreateTableBuilder {
//     private HiveWarehouseSession hive;
//     private String database;
//     private String tableName;
//     private boolean ifNotExists;
//     private List<Pair<String, String>> cols = new ArrayList<>();
//     private List<Pair<String, String>> parts = new ArrayList<>();
//     private List<Pair<String, String>> props = new ArrayList<>();
//     private String[] clusters;
//     private Long buckets;
// 
//     public CreateTableBuilder(HiveWarehouseSession hive, String database, String tableName) {
//         this.hive = hive;
//         this.tableName = tableName;
//         this.database = database;
//     }
// 
//     @Override
//     public CreateTableBuilder ifNotExists() {
//         this.ifNotExists = true;
//         return this;
//     }
// 
//     @Override
//     public CreateTableBuilder column(String name, String type) {
//         cols.add(Pair.of(name, type));
//         return this;
//     }
// 
//     @Override
//     public CreateTableBuilder partition(String name, String type) {
//         parts.add(Pair.of(name, type));
//         return this;
//     }
// 
//     @Override
//     public CreateTableBuilder prop(String key, String value) {
//         props.add(Pair.of(key, value));
//         return this;
//     }
// 
//     @Override
//     public CreateTableBuilder clusterBy(long numBuckets, String ... columns) {
//         this.buckets = numBuckets;
//         this.clusters = columns;
//         return this;
//     }
// 
//     @Override
//     public void create() {
//         hive.executeUpdate(this.toString());
//     }
// 
//     public String toString() {
//         StringBuilder builder = new StringBuilder();
//         builder.append(createTablePrelude(database, tableName, ifNotExists));
//         if(cols.size() > 0) {
//             List<String> colsStrings = new ArrayList<>();
//             for(Pair<String, String> col : cols) {
//                 colsStrings.add(col.getKey() + " " + col.getValue());
//             }
//             builder.append(columnSpec(join(",", colsStrings)));
//         }
//         if(parts.size() > 0) {
//             List<String> partsStrings = new ArrayList<>();
//             for(Pair<String, String> part : parts) {
//                 partsStrings.add(part.getKey() + " " + part.getValue());
//             }
//             builder.append(partitionSpec(join(",", partsStrings)));
//         }
//         if(clusters != null) {
//             builder.append(bucketSpec(join(",", clusters), buckets));
//         }
//         //Currently only managed ORC tables are supported
//         builder.append(" STORED AS ORC ");
//         if(props.size() > 0) {
//             List<String> keyValueStrings = new ArrayList<>();
//             for(Pair<String, String> keyValue : props) {
//                 keyValueStrings.add(format("\"%s\"=\"%s\"", keyValue.getKey(), keyValue.getValue()));
//             }
//             builder.append(tblProperties(join(",", keyValueStrings)));
//         }
//         return builder.toString();
//     }
// }


// Path: src/main/java/com/hortonworks/spark/sql/hive/llap/util/SchemaUtil.java
import com.hortonworks.spark.sql.hive.llap.CreateTableBuilder;
import org.apache.hadoop.hive.llap.FieldDesc;
import org.apache.hadoop.hive.llap.Schema;
import org.apache.spark.sql.types.*;
import java.util.ArrayList;
import java.util.List;
import static java.lang.String.format;

package com.hortonworks.spark.sql.hive.llap.util;




public class SchemaUtil {

  private static final String HIVE_TYPE_STRING = "HIVE_TYPE_STRING";

  public static StructType convertSchema(Schema schema) {
    List<FieldDesc> columns = schema.getColumns();
    List<String> types = new ArrayList<>();
    for(FieldDesc fieldDesc : columns) {
      String name;
      if(fieldDesc.getName().contains(".")) {
        name = fieldDesc.getName().split("\\.")[1];
      } else {
        name = fieldDesc.getName();
      }
      types.add(format("`%s` %s", name, fieldDesc.getTypeInfo().toString()));
    }
    return StructType.fromDDL(String.join(", ", types));
  }

  public static String[] columnNames(StructType schema) {
    String[] requiredColumns = new String[schema.length()];
    int i = 0;
    for (StructField field : schema.fields()) {
      requiredColumns[i] = field.name();
      i++;
    }
    return requiredColumns;
  }

  /**
   *
   * Builds create table query from given dataframe schema.
   *
   * @param schema  spark dataframe schema
   * @param database database name for create table query
   * @param table table name for create table query
   * @return a create table query string
   */
  public static String buildHiveCreateTableQueryFromSparkDFSchema(StructType schema, String database, String table) {
    CreateTableBuilder createTableBuilder = new CreateTableBuilder(null, database, table);