Back to home page

OSCL-LXR

 
 

    


0001 /**
0002  * Licensed to the Apache Software Foundation (ASF) under one
0003  * or more contributor license agreements.  See the NOTICE file
0004  * distributed with this work for additional information
0005  * regarding copyright ownership.  The ASF licenses this file
0006  * to you under the Apache License, Version 2.0 (the
0007  * "License"); you may not use this file except in compliance
0008  * with the License.  You may obtain a copy of the License at
0009  *
0010  *     http://www.apache.org/licenses/LICENSE-2.0
0011  *
0012  * Unless required by applicable law or agreed to in writing, software
0013  * distributed under the License is distributed on an "AS IS" BASIS,
0014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0015  * See the License for the specific language governing permissions and
0016  * limitations under the License.
0017  */
0018 
0019 package org.apache.hive.service.cli;
0020 
0021 import java.util.ArrayList;
0022 import java.util.List;
0023 
0024 import org.apache.hadoop.hive.metastore.api.FieldSchema;
0025 import org.apache.hadoop.hive.metastore.api.Schema;
0026 import org.apache.hadoop.hive.serde2.thrift.Type;
0027 import org.apache.hive.service.rpc.thrift.TColumnDesc;
0028 import org.apache.hive.service.rpc.thrift.TTableSchema;
0029 
0030 /**
0031  * TableSchema.
0032  *
0033  */
0034 public class TableSchema {
0035   private final List<ColumnDescriptor> columns = new ArrayList<ColumnDescriptor>();
0036 
0037   public TableSchema() {
0038   }
0039 
0040   public TableSchema(int numColumns) {
0041     // TODO: remove this constructor
0042   }
0043 
0044   public TableSchema(TTableSchema tTableSchema) {
0045     for (TColumnDesc tColumnDesc : tTableSchema.getColumns()) {
0046       columns.add(new ColumnDescriptor(tColumnDesc));
0047     }
0048   }
0049 
0050   public TableSchema(List<FieldSchema> fieldSchemas) {
0051     int pos = 1;
0052     for (FieldSchema field : fieldSchemas) {
0053       columns.add(new ColumnDescriptor(field.getName(), field.getComment(),
0054           new TypeDescriptor(field.getType()), pos++));
0055     }
0056   }
0057 
0058   public TableSchema(Schema schema) {
0059     this(schema.getFieldSchemas());
0060   }
0061 
0062   public List<ColumnDescriptor> getColumnDescriptors() {
0063     return new ArrayList<ColumnDescriptor>(columns);
0064   }
0065 
0066   public ColumnDescriptor getColumnDescriptorAt(int pos) {
0067     return columns.get(pos);
0068   }
0069 
0070   public int getSize() {
0071     return columns.size();
0072   }
0073 
0074   public void clear() {
0075     columns.clear();
0076   }
0077 
0078 
0079   public TTableSchema toTTableSchema() {
0080     TTableSchema tTableSchema = new TTableSchema();
0081     for (ColumnDescriptor col : columns) {
0082       tTableSchema.addToColumns(col.toTColumnDesc());
0083     }
0084     return tTableSchema;
0085   }
0086 
0087   public TypeDescriptor[] toTypeDescriptors() {
0088     TypeDescriptor[] types = new TypeDescriptor[columns.size()];
0089     for (int i = 0; i < types.length; i++) {
0090       types[i] = columns.get(i).getTypeDescriptor();
0091     }
0092     return types;
0093   }
0094 
0095   public TableSchema addPrimitiveColumn(String columnName, Type columnType, String columnComment) {
0096     columns.add(ColumnDescriptor.newPrimitiveColumnDescriptor(columnName, columnComment, columnType, columns.size() + 1));
0097     return this;
0098   }
0099 
0100   public TableSchema addStringColumn(String columnName, String columnComment) {
0101     columns.add(ColumnDescriptor.newPrimitiveColumnDescriptor(columnName, columnComment, Type.STRING_TYPE, columns.size() + 1));
0102     return this;
0103   }
0104 }