ShreelekhyaG commented on a change in pull request #4113:
URL: https://github.com/apache/carbondata/pull/4113#discussion_r613149270



##########
File path: 
integration/spark/src/main/scala/org/apache/spark/sql/execution/command/table/CarbonDescribeFormattedCommand.scala
##########
@@ -370,3 +374,147 @@ private[sql] case class CarbonDescribeFormattedCommand(
 
   override protected def opName: String = "DESC FORMATTED"
 }
+
+case class CarbonDescribeColumnCommand(
+    databaseNameOp: Option[String],
+    tableName: String,
+    inputFieldNames: java.util.List[String])
+  extends MetadataCommand {
+
+  override val output: Seq[Attribute] = Seq(
+    // Column names are based on Hive.
+    AttributeReference("col_name", StringType, nullable = false,
+      new MetadataBuilder().putString("comment", "name of the 
column").build())(),
+    AttributeReference("data_type", StringType, nullable = false,
+      new MetadataBuilder().putString("comment", "data type of the 
column").build())(),
+    AttributeReference("comment", StringType, nullable = true,
+      new MetadataBuilder().putString("comment", "comment of the 
column").build())()
+  )
+
+  override def processMetadata(sparkSession: SparkSession): Seq[Row] = {
+    val (tableSchema, carbonTable) = Checker.getSchemaAndTable(databaseNameOp, 
tableName)
+    val inputField = 
tableSchema.find(_.name.equalsIgnoreCase(inputFieldNames.get(0)))
+    if (inputField.isEmpty) {
+      throw new MalformedCarbonCommandException(
+        s"Column ${ inputFieldNames.get(0) } does not exists in the table " +
+        s"${ carbonTable.getDatabaseName }.$tableName")
+    }
+    setAuditTable(carbonTable)
+    var results = Seq[(String, String, String)]()
+    var currField = inputField.get
+    val inputFieldsIterator = inputFieldNames.iterator()
+    var inputColumn = inputFieldsIterator.next()
+    while (results.isEmpty) {
+      breakable {
+        if 
(currField.dataType.typeName.equalsIgnoreCase(CarbonCommonConstants.ARRAY)) {
+          if (inputFieldsIterator.hasNext) {
+            val nextField = inputFieldsIterator.next()
+            // child of an array can be only 'item'
+            if (!nextField.equalsIgnoreCase("item")) {
+              throw handleException(nextField, currField.name, 
carbonTable.getTableName)
+            }
+            // make the child type as current field to describe further nested 
types.
+            currField = StructField("item", 
currField.dataType.asInstanceOf[ArrayType].elementType)
+            inputColumn += "." + currField.name
+            break()
+          }
+          // if no further nested column given, display field information and 
its children.
+          results = Seq((inputColumn,
+            currField.dataType.typeName, 
currField.getComment().getOrElse("null")),
+            ("## Children of " + inputColumn + ":  ", "", ""))
+          results ++= Seq(("item", currField.dataType.asInstanceOf[ArrayType]
+            .elementType.simpleString, "null"))
+        }
+        else if 
(currField.dataType.typeName.equalsIgnoreCase(CarbonCommonConstants.STRUCT)) {
+          if (inputFieldsIterator.hasNext) {
+            val nextField = inputFieldsIterator.next()
+            val nextCurrField = 
currField.dataType.asInstanceOf[StructType].fields
+              .find(_.name.equalsIgnoreCase(nextField))
+            // verify if the input child name exists in the schema
+            if (!nextCurrField.isDefined) {
+              throw handleException(nextField, currField.name, 
carbonTable.getTableName)
+            }
+            // make the child type as current field to describe further nested 
types.
+            currField = nextCurrField.get
+            inputColumn += "." + currField.name
+            break()
+          }
+          // if no further nested column given, display field information and 
its children.
+          results = Seq((inputColumn,
+            currField.dataType.typeName, 
currField.getComment().getOrElse("null")),
+            ("## Children of " + inputColumn + ":  ", "", ""))
+          results ++= 
currField.dataType.asInstanceOf[StructType].fields.map(child => {
+            (child.name, child.dataType.simpleString, "null")
+          })
+        } else if 
(currField.dataType.typeName.equalsIgnoreCase(CarbonCommonConstants.MAP)) {
+          val children = currField.dataType.asInstanceOf[MapType]
+          if (inputFieldsIterator.hasNext) {
+            val nextField = inputFieldsIterator.next().toLowerCase()
+            // children of map can be only 'key' and 'value'
+            val nextCurrField = nextField match {
+              case "key" => StructField("key", children.keyType)
+              case "value" => StructField("value", children.valueType)
+              case _ => throw handleException(nextField, currField.name, 
carbonTable.getTableName)
+            }
+            // make the child type as current field to describe further nested 
types.
+            currField = nextCurrField
+            inputColumn += "." + currField.name
+            break()
+          }
+          // if no further nested column given, display field information and 
its children.
+          results = Seq((inputColumn,
+            currField.dataType.typeName, 
currField.getComment().getOrElse("null")),
+            ("## Children of " + inputColumn + ":  ", "", ""))
+          results ++= Seq(("key", children.keyType.simpleString, "null"),
+            ("value", children.valueType.simpleString, "null"))
+        } else {
+          results = Seq((inputColumn,
+            currField.dataType.typeName, 
currField.getComment().getOrElse("null")))
+        }
+      }
+    }
+    results.map { case (c1, c2, c3) => Row(c1, c2, c3) }
+  }
+
+  def handleException(nextField: String, currField: String, tableName: 
String): Throwable = {

Review comment:
       testcase present in -` test describe with invalid table and field names`




-- 
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:
us...@infra.apache.org


Reply via email to