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.sql.DatabaseMetaData;
0022 import java.util.Locale;
0023 
0024 import org.apache.hadoop.hive.common.type.HiveDecimal;
0025 import org.apache.hive.service.cli.thrift.TTypeId;
0026 
0027 /**
0028  * Type.
0029  *
0030  */
0031 public enum Type {
0032   NULL_TYPE("VOID",
0033       java.sql.Types.NULL,
0034       TTypeId.NULL_TYPE),
0035   BOOLEAN_TYPE("BOOLEAN",
0036       java.sql.Types.BOOLEAN,
0037       TTypeId.BOOLEAN_TYPE),
0038   TINYINT_TYPE("TINYINT",
0039       java.sql.Types.TINYINT,
0040       TTypeId.TINYINT_TYPE),
0041   SMALLINT_TYPE("SMALLINT",
0042       java.sql.Types.SMALLINT,
0043       TTypeId.SMALLINT_TYPE),
0044   INT_TYPE("INT",
0045       java.sql.Types.INTEGER,
0046       TTypeId.INT_TYPE),
0047   BIGINT_TYPE("BIGINT",
0048       java.sql.Types.BIGINT,
0049       TTypeId.BIGINT_TYPE),
0050   FLOAT_TYPE("FLOAT",
0051       java.sql.Types.FLOAT,
0052       TTypeId.FLOAT_TYPE),
0053   DOUBLE_TYPE("DOUBLE",
0054       java.sql.Types.DOUBLE,
0055       TTypeId.DOUBLE_TYPE),
0056   STRING_TYPE("STRING",
0057       java.sql.Types.VARCHAR,
0058       TTypeId.STRING_TYPE),
0059   CHAR_TYPE("CHAR",
0060       java.sql.Types.CHAR,
0061       TTypeId.CHAR_TYPE,
0062       true, false, false),
0063   VARCHAR_TYPE("VARCHAR",
0064       java.sql.Types.VARCHAR,
0065       TTypeId.VARCHAR_TYPE,
0066       true, false, false),
0067   DATE_TYPE("DATE",
0068       java.sql.Types.DATE,
0069       TTypeId.DATE_TYPE),
0070   TIMESTAMP_TYPE("TIMESTAMP",
0071       java.sql.Types.TIMESTAMP,
0072       TTypeId.TIMESTAMP_TYPE),
0073   INTERVAL_YEAR_MONTH_TYPE("INTERVAL_YEAR_MONTH",
0074       java.sql.Types.OTHER,
0075       TTypeId.INTERVAL_YEAR_MONTH_TYPE),
0076   INTERVAL_DAY_TIME_TYPE("INTERVAL_DAY_TIME",
0077       java.sql.Types.OTHER,
0078       TTypeId.INTERVAL_DAY_TIME_TYPE),
0079   BINARY_TYPE("BINARY",
0080       java.sql.Types.BINARY,
0081       TTypeId.BINARY_TYPE),
0082   DECIMAL_TYPE("DECIMAL",
0083       java.sql.Types.DECIMAL,
0084       TTypeId.DECIMAL_TYPE,
0085       true, false, false),
0086   ARRAY_TYPE("ARRAY",
0087       java.sql.Types.ARRAY,
0088       TTypeId.ARRAY_TYPE,
0089       true, true),
0090   MAP_TYPE("MAP",
0091       java.sql.Types.JAVA_OBJECT,
0092       TTypeId.MAP_TYPE,
0093       true, true),
0094   STRUCT_TYPE("STRUCT",
0095       java.sql.Types.STRUCT,
0096       TTypeId.STRUCT_TYPE,
0097       true, false),
0098   UNION_TYPE("UNIONTYPE",
0099       java.sql.Types.OTHER,
0100       TTypeId.UNION_TYPE,
0101       true, false),
0102   USER_DEFINED_TYPE("USER_DEFINED",
0103       java.sql.Types.OTHER,
0104       TTypeId.USER_DEFINED_TYPE,
0105       true, false);
0106 
0107   private final String name;
0108   private final TTypeId tType;
0109   private final int javaSQLType;
0110   private final boolean isQualified;
0111   private final boolean isComplex;
0112   private final boolean isCollection;
0113 
0114   Type(String name, int javaSQLType, TTypeId tType, boolean isQualified, boolean isComplex, boolean isCollection) {
0115     this.name = name;
0116     this.javaSQLType = javaSQLType;
0117     this.tType = tType;
0118     this.isQualified = isQualified;
0119     this.isComplex = isComplex;
0120     this.isCollection = isCollection;
0121   }
0122 
0123   Type(String name, int javaSQLType, TTypeId tType, boolean isComplex, boolean isCollection) {
0124     this(name, javaSQLType, tType, false, isComplex, isCollection);
0125   }
0126 
0127   Type(String name, int javaSqlType, TTypeId tType) {
0128     this(name, javaSqlType, tType, false, false, false);
0129   }
0130 
0131   public boolean isPrimitiveType() {
0132     return !isComplex;
0133   }
0134 
0135   public boolean isQualifiedType() {
0136     return isQualified;
0137   }
0138 
0139   public boolean isComplexType() {
0140     return isComplex;
0141   }
0142 
0143   public boolean isCollectionType() {
0144     return isCollection;
0145   }
0146 
0147   public static Type getType(TTypeId tType) {
0148     for (Type type : values()) {
0149       if (tType.equals(type.tType)) {
0150         return type;
0151       }
0152     }
0153     throw new IllegalArgumentException("Unregonized Thrift TTypeId value: " + tType);
0154   }
0155 
0156   public static Type getType(String name) {
0157     if (name == null) {
0158       throw new IllegalArgumentException("Invalid type name: null");
0159     }
0160     for (Type type : values()) {
0161       if (name.equalsIgnoreCase(type.name)) {
0162         return type;
0163       } else if (type.isQualifiedType() || type.isComplexType()) {
0164         if (name.toUpperCase(Locale.ROOT).startsWith(type.name)) {
0165             return type;
0166         }
0167       }
0168     }
0169     throw new IllegalArgumentException("Unrecognized type name: " + name);
0170   }
0171 
0172   /**
0173    * Radix for this type (typically either 2 or 10)
0174    * Null is returned for data types where this is not applicable.
0175    */
0176   public Integer getNumPrecRadix() {
0177     if (this.isNumericType()) {
0178       return 10;
0179     }
0180     return null;
0181   }
0182 
0183   /**
0184    * Maximum precision for numeric types.
0185    * Returns null for non-numeric types.
0186    * @return
0187    */
0188   public Integer getMaxPrecision() {
0189     switch (this) {
0190     case TINYINT_TYPE:
0191       return 3;
0192     case SMALLINT_TYPE:
0193       return 5;
0194     case INT_TYPE:
0195       return 10;
0196     case BIGINT_TYPE:
0197       return 19;
0198     case FLOAT_TYPE:
0199       return 7;
0200     case DOUBLE_TYPE:
0201       return 15;
0202     case DECIMAL_TYPE:
0203       return HiveDecimal.MAX_PRECISION;
0204     default:
0205       return null;
0206     }
0207   }
0208 
0209   public boolean isNumericType() {
0210     switch (this) {
0211     case TINYINT_TYPE:
0212     case SMALLINT_TYPE:
0213     case INT_TYPE:
0214     case BIGINT_TYPE:
0215     case FLOAT_TYPE:
0216     case DOUBLE_TYPE:
0217     case DECIMAL_TYPE:
0218       return true;
0219     default:
0220       return false;
0221     }
0222   }
0223 
0224   /**
0225    * Prefix used to quote a literal of this type (may be null)
0226    */
0227   public String getLiteralPrefix() {
0228     return null;
0229   }
0230 
0231   /**
0232    * Suffix used to quote a literal of this type (may be null)
0233    * @return
0234    */
0235   public String getLiteralSuffix() {
0236     return null;
0237   }
0238 
0239   /**
0240    * Can you use NULL for this type?
0241    * @return
0242    * DatabaseMetaData.typeNoNulls - does not allow NULL values
0243    * DatabaseMetaData.typeNullable - allows NULL values
0244    * DatabaseMetaData.typeNullableUnknown - nullability unknown
0245    */
0246   public Short getNullable() {
0247     // All Hive types are nullable
0248     return DatabaseMetaData.typeNullable;
0249   }
0250 
0251   /**
0252    * Is the type case sensitive?
0253    * @return
0254    */
0255   public Boolean isCaseSensitive() {
0256     switch (this) {
0257     case STRING_TYPE:
0258       return true;
0259     default:
0260       return false;
0261     }
0262   }
0263 
0264   /**
0265    * Parameters used in creating the type (may be null)
0266    * @return
0267    */
0268   public String getCreateParams() {
0269     return null;
0270   }
0271 
0272   /**
0273    * Can you use WHERE based on this type?
0274    * @return
0275    * DatabaseMetaData.typePredNone - No support
0276    * DatabaseMetaData.typePredChar - Only support with WHERE .. LIKE
0277    * DatabaseMetaData.typePredBasic - Supported except for WHERE .. LIKE
0278    * DatabaseMetaData.typeSearchable - Supported for all WHERE ..
0279    */
0280   public Short getSearchable() {
0281     if (isPrimitiveType()) {
0282       return DatabaseMetaData.typeSearchable;
0283     }
0284     return DatabaseMetaData.typePredNone;
0285   }
0286 
0287   /**
0288    * Is this type unsigned?
0289    * @return
0290    */
0291   public Boolean isUnsignedAttribute() {
0292     if (isNumericType()) {
0293       return false;
0294     }
0295     return true;
0296   }
0297 
0298   /**
0299    * Can this type represent money?
0300    * @return
0301    */
0302   public Boolean isFixedPrecScale() {
0303     return false;
0304   }
0305 
0306   /**
0307    * Can this type be used for an auto-increment value?
0308    * @return
0309    */
0310   public Boolean isAutoIncrement() {
0311     return false;
0312   }
0313 
0314   /**
0315    * Localized version of type name (may be null).
0316    * @return
0317    */
0318   public String getLocalizedName() {
0319     return null;
0320   }
0321 
0322   /**
0323    * Minimum scale supported for this type
0324    * @return
0325    */
0326   public Short getMinimumScale() {
0327     return 0;
0328   }
0329 
0330   /**
0331    * Maximum scale supported for this type
0332    * @return
0333    */
0334   public Short getMaximumScale() {
0335     return 0;
0336   }
0337 
0338   public TTypeId toTType() {
0339     return tType;
0340   }
0341 
0342   public int toJavaSQLType() {
0343     return javaSQLType;
0344   }
0345 
0346   public String getName() {
0347     return name;
0348   }
0349 }