VenuReddy2103 commented on a change in pull request #3436: 
[CARBONDATA-3548]Geospatial Support: Modified to create and load the table with 
a nonschema dimension sort column. And added InPolygon UDF
URL: https://github.com/apache/carbondata/pull/3436#discussion_r349878765
 
 

 ##########
 File path: 
integration/spark-common/src/main/scala/org/apache/spark/sql/catalyst/CarbonDDLSqlParser.scala
 ##########
 @@ -268,6 +309,151 @@ abstract class CarbonDDLSqlParser extends 
AbstractCarbonSparkSQLParser {
     }
   }
 
+  /**
+   * The method parses, validates and processes the index_handler property.
+   *
+   * @param tableProperties Table properties
+   * @param tableFields     Sequence of table fields
+   * @return <Seq[Field]> Sequence of table fields
+   *
+   */
+  private def processIndexProperty(tableProperties: mutable.Map[String, 
String],
+                                   tableFields: Seq[Field]): Seq[Field] = {
+    val option = tableProperties.get(CarbonCommonConstants.INDEX_HANDLER)
+    val fields = ListBuffer[Field]()
+    if (option.isDefined) {
+      if (option.get.isEmpty) {
+        throw new MalformedCarbonCommandException(
+          s"Carbon ${CarbonCommonConstants.INDEX_HANDLER} property is invalid. 
" +
+            s"Option value is empty.")
+      }
+
+      val handlers = option.get.split(",")
+      handlers.foreach { e =>
+        /* Validate target column name */
+        if (tableFields.exists(_.column.equalsIgnoreCase(e))) {
+          throw new MalformedCarbonCommandException(
+            s"Carbon ${CarbonCommonConstants.INDEX_HANDLER} property is 
invalid. " +
+              s"handler value : $e is not allowed. It matches with another 
column name in table. " +
+              s"Cannot create column with it.")
+        }
+
+        val sourceColumnsOption = tableProperties.get(
+          CarbonCommonConstants.INDEX_HANDLER + s".$e.sourcecolumns")
+        if (sourceColumnsOption.isEmpty) {
+          throw new MalformedCarbonCommandException(
+            s"Carbon ${CarbonCommonConstants.INDEX_HANDLER} property is 
invalid. " +
+              s"${CarbonCommonConstants.INDEX_HANDLER}.$e.sourcecolumns 
property is not specified.")
+        } else if (sourceColumnsOption.get.isEmpty) {
+          throw new MalformedCarbonCommandException(
+            s"Carbon ${CarbonCommonConstants.INDEX_HANDLER} property is 
invalid. " +
+              s"${CarbonCommonConstants.INDEX_HANDLER}.$e.sourcecolumns 
property cannot be empty.")
+        }
+
+        /* Validate source columns */
+        val sources = sourceColumnsOption.get.split(",")
+        if (sources.distinct.length != sources.size) {
+          throw new MalformedCarbonCommandException(
+            s"Carbon ${CarbonCommonConstants.INDEX_HANDLER} property is 
invalid. " +
+              s"${CarbonCommonConstants.INDEX_HANDLER}.$e.sourcecolumns 
property " +
+              s"have duplicate columns.")
+        }
+
+        val sourceTypes = StringBuilder.newBuilder
+        sources.foreach { column =>
+          tableFields.find(_.column.equalsIgnoreCase(column)) match {
+            case Some(field) => 
sourceTypes.append(field.dataType.get).append(",")
+            case None =>
+              throw new MalformedCarbonCommandException(
+                s"Carbon ${CarbonCommonConstants.INDEX_HANDLER} property is 
invalid. " +
+                  s"Source column : $column in property " +
+                  s"${CarbonCommonConstants.INDEX_HANDLER}.$e.sourcecolumns " +
+                  "is not a valid column in table.")
+          }
+        }
+
+        tableProperties.put(CarbonCommonConstants.INDEX_HANDLER +
+          s".$e.sourcecolumntypes", sourceTypes.dropRight(1).toString())
+
+        val handlerType = 
tableProperties.get(CarbonCommonConstants.INDEX_HANDLER + s".$e.type")
+        val handlerClass = 
tableProperties.get(CarbonCommonConstants.INDEX_HANDLER + s".$e.class")
+
+        val handlerClassName: String = handlerClass match {
+          case Some(className) =>
+            className
+          case None =>
+            /* use handler type to find the default implementation */
+            if (handlerType.isEmpty) {
+              throw new MalformedCarbonCommandException(
+                s"Carbon ${CarbonCommonConstants.INDEX_HANDLER} property is 
invalid. " +
+                  s"Both ${CarbonCommonConstants.INDEX_HANDLER}.$e.class and " 
+
+                  s"${CarbonCommonConstants.INDEX_HANDLER}.$e.type properties 
are not specified")
+            } else if (handlerType.get.equalsIgnoreCase("geohash")) {
+              /* Use geoHash default implementation */
+              val className = 
classOf[org.apache.carbondata.core.util.GeoHashDefault].getName
+              
tableProperties.put(s"${CarbonCommonConstants.INDEX_HANDLER}.$e.class", 
className)
+              className
+            } else {
+              throw new MalformedCarbonCommandException(
+                s"Carbon ${CarbonCommonConstants.INDEX_HANDLER} property is 
invalid. " +
+                  s"Unsupported value : ${handlerType.get} specified in 
property " +
+                  s"${CarbonCommonConstants.INDEX_HANDLER}.$e.type")
+            }
+        }
+
+        try {
+          val handlerClass: Class[_] = 
java.lang.Class.forName(handlerClassName)
+          val instance = handlerClass.newInstance().asInstanceOf[
+            CustomIndex[Long, String, java.util.List[Array[Long]]]]
+          instance.validateOption(tableProperties.asJava)
+          instance.init(tableProperties.asJava)
+        } catch {
+          case ex: ClassNotFoundException =>
+            throw new MalformedCarbonCommandException(
+              s"Carbon ${CarbonCommonConstants.INDEX_HANDLER} property process 
failed. " +
+                s"$handlerClassName class in property " +
+                s"${CarbonCommonConstants.INDEX_HANDLER}.$e.class is failed 
with $ex")
+          case ex@(_: InstantiationError | _: IllegalAccessException) =>
+            throw new MalformedCarbonCommandException(
+              s"Carbon ${CarbonCommonConstants.INDEX_HANDLER} property process 
failed. " +
+                s"Instantiation of class $handlerClassName is failed with $ex")
+          case ex: ClassCastException =>
+            throw new MalformedCarbonCommandException(
+              s"Carbon ${CarbonCommonConstants.INDEX_HANDLER} property process 
failed. " +
+                s"Failed due to $ex")
+        }
+
+        /* Add target column in sort column */
+        var sortKey = 
tableProperties.getOrElse(CarbonCommonConstants.SORT_COLUMNS, "")
+        if (!sortKey.isEmpty) {
+          /* Source columns are not allowed to be specified in sort columns. 
Instead target column
+          is implicitly treated as sort column */
+          sources.foreach { column =>
+            sortKey.split(",").foreach { key =>
+              if (key.equalsIgnoreCase(column)) {
+                throw new MalformedCarbonCommandException(
+                  s"Carbon ${CarbonCommonConstants.INDEX_HANDLER} property is 
invalid. " +
+                    s"Source column: $key is not allowed in 
${CarbonCommonConstants.SORT_COLUMNS}" +
+                    s" property. Instead, handler value: $e is implicitly 
treated as sort column.")
+              }
+            }
+          }
+
 
 Review comment:
   Ok. Removed blank lines in the complete PR.

----------------------------------------------------------------
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


With regards,
Apache Git Services

Reply via email to