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.hive.service.cli.thrift.TColumnDesc;
0027 import org.apache.hive.service.cli.thrift.TTableSchema;
0028 
0029 /**
0030  * TableSchema.
0031  *
0032  */
0033 public class TableSchema {
0034   private final List<ColumnDescriptor> columns = new ArrayList<ColumnDescriptor>();
0035 
0036   public TableSchema() {
0037   }
0038 
0039   public TableSchema(int numColumns) {
0040     // TODO: remove this constructor
0041   }
0042 
0043   public TableSchema(TTableSchema tTableSchema) {
0044     for (TColumnDesc tColumnDesc : tTableSchema.getColumns()) {
0045       columns.add(new ColumnDescriptor(tColumnDesc));
0046     }
0047   }
0048 
0049   public TableSchema(List<FieldSchema> fieldSchemas) {
0050     int pos = 1;
0051     for (FieldSchema field : fieldSchemas) {
0052       columns.add(new ColumnDescriptor(field, pos++));
0053     }
0054   }
0055 
0056   public TableSchema(Schema schema) {
0057     this(schema.getFieldSchemas());
0058   }
0059 
0060   public List<ColumnDescriptor> getColumnDescriptors() {
0061     return new ArrayList<ColumnDescriptor>(columns);
0062   }
0063 
0064   public ColumnDescriptor getColumnDescriptorAt(int pos) {
0065     return columns.get(pos);
0066   }
0067 
0068   public int getSize() {
0069     return columns.size();
0070   }
0071 
0072   public void clear() {
0073     columns.clear();
0074   }
0075 
0076 
0077   public TTableSchema toTTableSchema() {
0078     TTableSchema tTableSchema = new TTableSchema();
0079     for (ColumnDescriptor col : columns) {
0080       tTableSchema.addToColumns(col.toTColumnDesc());
0081     }
0082     return tTableSchema;
0083   }
0084 
0085   public Type[] toTypes() {
0086     Type[] types = new Type[columns.size()];
0087     for (int i = 0; i < types.length; i++) {
0088       types[i] = columns.get(i).getType();
0089     }
0090     return types;
0091   }
0092 
0093   public TableSchema addPrimitiveColumn(String columnName, Type columnType, String columnComment) {
0094     columns.add(ColumnDescriptor.newPrimitiveColumnDescriptor(columnName, columnComment, columnType, columns.size() + 1));
0095     return this;
0096   }
0097 
0098   public TableSchema addStringColumn(String columnName, String columnComment) {
0099     columns.add(ColumnDescriptor.newPrimitiveColumnDescriptor(columnName, columnComment, Type.STRING_TYPE, columns.size() + 1));
0100     return this;
0101   }
0102 }