AngersZhuuuu commented on a change in pull request #25694: [SPARK-28982][SQL] 
Implementation Spark's own GetTypeInfoOperation
URL: https://github.com/apache/spark/pull/25694#discussion_r321719469
 
 

 ##########
 File path: 
sql/hive-thriftserver/src/main/scala/org/apache/spark/sql/hive/thriftserver/cli/Type.scala
 ##########
 @@ -0,0 +1,379 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.hive.thriftserver.cli
+
+import java.sql.DatabaseMetaData
+
+import org.apache.spark.sql.types.DecimalType
+
+trait Type {
+
+  def getName: String
+
+  def isComplex: Boolean
+
+  def isQualifiedType: Boolean
+
+  def isCollectionType: Boolean
+
+  def isPrimitiveType: Boolean = !isComplex
+
+  def isComplexType: Boolean = isComplex
+
+  /**
+   * Radix for this type (typically either 2 or 10)
+   * Null is returned for data types where this is not applicable.
+   */
+  def getNumPrecRadix: Integer = {
+    if (this.isNumericType) {
+      10
+    } else {
+      null
+    }
+  }
+
+  def getMaxPrecision(): Option[Int]
+
+  def isNumericType(): Boolean = false
+
+  /**
+   * Prefix used to quote a literal of this type (may be null)
+   */
+  def getLiteralPrefix: String = null
+
+  /**
+   * Suffix used to quote a literal of this type (may be null)
+   *
+   * @return
+   */
+  def getLiteralSuffix: String = null
+
+  /**
+   * Can you use NULL for this type?
+   *
+   * @return
+   * DatabaseMetaData.typeNoNulls - does not allow NULL values
+   * DatabaseMetaData.typeNullable - allows NULL values
+   * DatabaseMetaData.typeNullableUnknown - nullability unknown
+   */
+  def getNullable: Short = {
+    // All Hive types are nullable
+    DatabaseMetaData.typeNullable.toShort
+  }
+
+  /**
+   * Is the type case sensitive?
+   *
+   * @return
+   */
+  def isCaseSensitive: Boolean = false
+
+  /**
+   * Parameters used in creating the type (may be null)
+   *
+   * @return
+   */
+  def getCreateParams: String = null
+
+  /**
+   * Can you use WHERE based on this type?
+   *
+   * @return
+   * DatabaseMetaData.typePredNone - No support
+   * DatabaseMetaData.typePredChar - Only support with WHERE .. LIKE
+   * DatabaseMetaData.typePredBasic - Supported except for WHERE .. LIKE
+   * DatabaseMetaData.typeSearchable - Supported for all WHERE ..
+   */
+  def getSearchable: Short = {
+    if (isPrimitiveType) {
+      DatabaseMetaData.typeSearchable.toShort
+    } else {
+      DatabaseMetaData.typePredNone.toShort
+    }
+  }
+
+  /**
+   * Is this type unsigned?
+   *
+   * @return
+   */
+  def isUnsignedAttribute: Boolean = {
+    if (isNumericType) {
+      false
+    } else {
+      true
+    }
+  }
+
+  /**
+   * Can this type represent money?
+   *
+   * @return
+   */
+  def isFixedPrecScale: Boolean = false
+
+  /**
+   * Can this type be used for an auto-increment value?
+   *
+   * @return
+   */
+  def isAutoIncrement: Boolean = false
+
+  /**
+   * Localized version of type name (may be null).
+   *
+   * @return
+   */
+  def getLocalizedName: String = null
+
+  /**
+   * Minimum scale supported for this type
+   *
+   * @return
+   */
+  def getMinimumScale: Short = 0.toShort
+
+  /**
+   * Maximum scale supported for this type
+   *
+   * @return
+   */
+  def getMaximumScale: Short = 0.toShort
+
+  def javaSQLType: Int
+
+  def toJavaSQLType: Int = javaSQLType
+}
+
+object Type {
+
+  val typeMap: Map[String, Type] = Map(
+    NULL.getName -> NULL,
+    STRING.getName -> STRING,
+    INTEGER.getName -> INTEGER,
+    BOOLEAN.getName -> BOOLEAN,
+    DOUBLE.getName -> DOUBLE,
+    FLOAT.getName -> FLOAT,
+    DECIMAL.getName -> DECIMAL,
+    LONG.getName -> LONG,
+    BYTE.getName -> BYTE,
+    SHORT.getName -> SHORT,
+    DATE.getName -> DATE,
+    TIMESTAMP.getName -> TIMESTAMP,
+    BINARY.getName -> BINARY
+  )
+
+  def values: Seq[Type] = typeMap.values.toSeq
+
+  case object NULL extends Type() {
+    override def getName: String = "NULL"
+
+    override def isComplex: Boolean = false
+
+    override def isQualifiedType: Boolean = false
+
+    override def isCollectionType: Boolean = false
+
+    override def getMaxPrecision(): Option[Int] = None
+
+    override def javaSQLType: Int = java.sql.Types.NULL
+  }
+
+  case object STRING extends Type {
+    override def getName: String = "STRING"
+
+    override def isComplex: Boolean = false
+
+    override def isQualifiedType: Boolean = false
+
+    override def isCollectionType: Boolean = false
+
+    override def getMaxPrecision(): Option[Int] = None
+
+    override def javaSQLType: Int = java.sql.Types.VARCHAR
+
+    override def isCaseSensitive: Boolean = true
+  }
+
+  case object INTEGER extends Type {
+    override def getName: String = "INTEGER"
 
 Review comment:
   > Out of curiosity: where is this actually defined?
   > `IntergerType.simpleString` is "int", but parser definitely accepts 
INTEGER and I can't seem to find where it's defined.
   
   When I check for simpleString, miss this place, then through 
`IntergerType.simpleString` is Integer. 
   So add a method of filter un-support  Type is better.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to