jackylk commented on a change in pull request #3612: [CARBONDATA-3694] Separate 
Materialized View command from DataMap command
URL: https://github.com/apache/carbondata/pull/3612#discussion_r379272959
 
 

 ##########
 File path: 
datamap/mv/core/src/main/scala/org/apache/carbondata/mv/extension/MVParser.scala
 ##########
 @@ -0,0 +1,204 @@
+/*
+ * 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.carbondata.mv.extension
+
+import scala.language.implicitConversions
+import scala.util.matching.Regex
+import scala.util.parsing.combinator.PackratParsers
+import scala.util.parsing.combinator.syntactical.StandardTokenParsers
+
+import org.apache.spark.sql.{DataFrame, SparkSession}
+import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
+import org.apache.spark.sql.catalyst.{SqlLexical, TableIdentifier}
+import org.apache.spark.sql.hive.CarbonMVRules
+import org.apache.spark.sql.util.{CarbonException, SparkSQLUtil}
+
+import 
org.apache.carbondata.common.exceptions.sql.MalformedCarbonCommandException
+import 
org.apache.carbondata.mv.extension.command.{CreateMaterializedViewCommand, 
DropMaterializedViewCommand, RebuildMaterializedViewCommand, 
ShowMaterializedViewCommand}
+import org.apache.carbondata.mv.rewrite.MVUdf
+
+class MVParser extends StandardTokenParsers with PackratParsers {
+
+  // Keywords used in this parser
+  protected val SELECT: Regex = carbonKeyWord("SELECT")
+  protected val CREATE: Regex = carbonKeyWord("CREATE")
+  protected val MATERIALIZED: Regex = carbonKeyWord("MATERIALIZED")
+  protected val VIEW: Regex = carbonKeyWord("VIEW")
+  protected val VIEWS: Regex = carbonKeyWord("VIEWS")
+  protected val AS: Regex = carbonKeyWord("AS")
+  protected val DROP: Regex = carbonKeyWord("DROP")
+  protected val SHOW: Regex = carbonKeyWord("SHOW")
+  protected val IF: Regex = carbonKeyWord("IF")
+  protected val EXISTS: Regex = carbonKeyWord("EXISTS")
+  protected val NOT: Regex = carbonKeyWord("NOT")
+  protected val MVPROPERTIES: Regex = carbonKeyWord("MVPROPERTIES")
+  protected val WITH: Regex = carbonKeyWord("WITH")
+  protected val DEFERRED: Regex = carbonKeyWord("DEFERRED")
+  protected val REBUILD: Regex = carbonKeyWord("REBUILD")
+  protected val ON: Regex = carbonKeyWord("ON")
+  protected val TABLE: Regex = carbonKeyWord("TABLE")
+
+  /**
+   * This will convert key word to regular expression.
+   */
+  private def carbonKeyWord(keys: String): Regex = {
+    ("(?i)" + keys).r
+  }
+
+  implicit def regexToParser(regex: Regex): Parser[String] = {
+    import lexical.Identifier
+    acceptMatch(
+      s"identifier matching regex ${ regex }",
+      { case Identifier(str) if regex.unapplySeq(str).isDefined => str }
+    )
+  }
+
+  // By default, use Reflection to find the reserved words defined in the sub 
class.
+  // NOTICE, Since the Keyword properties defined by sub class, we couldn't 
call this
+  // method during the parent class instantiation, because the sub class 
instance
+  // isn't created yet.
+  protected lazy val reservedWords: Seq[String] =
+  this
+    .getClass
+    .getMethods
+    .filter(_.getReturnType == classOf[Keyword])
+    .map(_.invoke(this).asInstanceOf[Keyword].normalize)
+
+  // Set the keywords as empty by default, will change that later.
+  override val lexical = new SqlLexical
+
+  protected case class Keyword(str: String) {
+    def normalize: String = lexical.normalizeKeyword(str)
+    def parser: Parser[String] = normalize
+  }
+
+  def parse(input: String): LogicalPlan = {
+    synchronized {
+      phrase(start)(new lexical.Scanner(input)) match {
+        case Success(plan, _) =>
+          plan
+        case failureOrError =>
+          CarbonException.analysisException(failureOrError.toString)
+      }
+    }
+  }
+
+  private lazy val start: Parser[LogicalPlan] = mvCommand
+
+  private lazy val mvCommand: Parser[LogicalPlan] =
+    createMV | dropMV | showMV | rebuildMV
 
 Review comment:
   fixed

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