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.HashMap;
0022 import java.util.Map;
0023 
0024 import org.apache.hadoop.hive.serde2.typeinfo.CharTypeInfo;
0025 import org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo;
0026 import org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo;
0027 import org.apache.hadoop.hive.serde2.typeinfo.VarcharTypeInfo;
0028 import org.apache.hive.service.rpc.thrift.TCLIServiceConstants;
0029 import org.apache.hive.service.rpc.thrift.TTypeQualifierValue;
0030 import org.apache.hive.service.rpc.thrift.TTypeQualifiers;
0031 
0032 /**
0033  * This class holds type qualifier information for a primitive type,
0034  * such as char/varchar length or decimal precision/scale.
0035  */
0036 public class TypeQualifiers {
0037   private Integer characterMaximumLength;
0038   private Integer precision;
0039   private Integer scale;
0040 
0041   public TypeQualifiers() {}
0042 
0043   public Integer getCharacterMaximumLength() {
0044     return characterMaximumLength;
0045   }
0046   public void setCharacterMaximumLength(int characterMaximumLength) {
0047     this.characterMaximumLength = characterMaximumLength;
0048   }
0049 
0050   public TTypeQualifiers toTTypeQualifiers() {
0051     TTypeQualifiers ret = null;
0052 
0053     Map<String, TTypeQualifierValue> qMap = new HashMap<String, TTypeQualifierValue>();
0054     if (getCharacterMaximumLength() != null) {
0055       TTypeQualifierValue val = new TTypeQualifierValue();
0056       val.setI32Value(getCharacterMaximumLength().intValue());
0057       qMap.put(TCLIServiceConstants.CHARACTER_MAXIMUM_LENGTH, val);
0058     }
0059 
0060     if (precision != null) {
0061       TTypeQualifierValue val = new TTypeQualifierValue();
0062       val.setI32Value(precision.intValue());
0063       qMap.put(TCLIServiceConstants.PRECISION, val);
0064     }
0065 
0066     if (scale != null) {
0067       TTypeQualifierValue val = new TTypeQualifierValue();
0068       val.setI32Value(scale.intValue());
0069       qMap.put(TCLIServiceConstants.SCALE, val);
0070     }
0071 
0072     if (qMap.size() > 0) {
0073       ret = new TTypeQualifiers(qMap);
0074     }
0075 
0076     return ret;
0077   }
0078 
0079   public static TypeQualifiers fromTTypeQualifiers(TTypeQualifiers ttq) {
0080     TypeQualifiers ret = null;
0081     if (ttq != null) {
0082       ret = new TypeQualifiers();
0083       Map<String, TTypeQualifierValue> tqMap = ttq.getQualifiers();
0084 
0085       if (tqMap.containsKey(TCLIServiceConstants.CHARACTER_MAXIMUM_LENGTH)) {
0086         ret.setCharacterMaximumLength(
0087             tqMap.get(TCLIServiceConstants.CHARACTER_MAXIMUM_LENGTH).getI32Value());
0088       }
0089 
0090       if (tqMap.containsKey(TCLIServiceConstants.PRECISION)) {
0091         ret.setPrecision(tqMap.get(TCLIServiceConstants.PRECISION).getI32Value());
0092       }
0093 
0094       if (tqMap.containsKey(TCLIServiceConstants.SCALE)) {
0095         ret.setScale(tqMap.get(TCLIServiceConstants.SCALE).getI32Value());
0096       }
0097     }
0098     return ret;
0099   }
0100 
0101   public static TypeQualifiers fromTypeInfo(PrimitiveTypeInfo pti) {
0102     TypeQualifiers result = null;
0103     if (pti instanceof VarcharTypeInfo) {
0104       result = new TypeQualifiers();
0105       result.setCharacterMaximumLength(((VarcharTypeInfo)pti).getLength());
0106     }  else if (pti instanceof CharTypeInfo) {
0107       result = new TypeQualifiers();
0108       result.setCharacterMaximumLength(((CharTypeInfo)pti).getLength());
0109     } else if (pti instanceof DecimalTypeInfo) {
0110       result = new TypeQualifiers();
0111       result.setPrecision(((DecimalTypeInfo)pti).precision());
0112       result.setScale(((DecimalTypeInfo)pti).scale());
0113     }
0114     return result;
0115   }
0116 
0117   public Integer getPrecision() {
0118     return precision;
0119   }
0120 
0121   public void setPrecision(Integer precision) {
0122     this.precision = precision;
0123   }
0124 
0125   public Integer getScale() {
0126     return scale;
0127   }
0128 
0129   public void setScale(Integer scale) {
0130     this.scale = scale;
0131   }
0132 
0133 }