You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@paimon.apache.org by "YannByron (via GitHub)" <gi...@apache.org> on 2023/08/22 03:08:46 UTC

[GitHub] [incubator-paimon] YannByron commented on a diff in pull request #1785: [spark] Supports parser of Spark call procedure command

YannByron commented on code in PR #1785:
URL: https://github.com/apache/incubator-paimon/pull/1785#discussion_r1300854833


##########
paimon-spark/paimon-spark-common/src/main/scala/org/apache/spark/sql/catalyst/parser/extensions/PaimonSparkSqlExtensionsParser.scala:
##########
@@ -0,0 +1,268 @@
+/*
+ * 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.catalyst.parser.extensions
+
+import org.antlr.v4.runtime._
+import org.antlr.v4.runtime.atn.PredictionMode
+import org.antlr.v4.runtime.misc.{Interval, ParseCancellationException}
+import org.antlr.v4.runtime.tree.TerminalNodeImpl
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.catalyst.{FunctionIdentifier, TableIdentifier}
+import org.apache.spark.sql.catalyst.expressions.Expression
+import org.apache.spark.sql.catalyst.parser.{ParseException, ParserInterface}
+import org.apache.spark.sql.catalyst.parser.extensions.PaimonSqlExtensionsParser.{NonReservedContext, QuotedIdentifierContext}
+import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
+import org.apache.spark.sql.catalyst.trees.Origin
+import org.apache.spark.sql.internal.VariableSubstitution
+import org.apache.spark.sql.types.{DataType, StructType}
+
+import java.util.Locale
+
+/**
+ * The implementation of [[ParserInterface]] that parsers the sql extension.
+ *
+ * <p>Most of the content of this class is referenced from Iceberg's
+ * IcebergSparkSqlExtensionsParser.
+ *
+ * @param delegate
+ *   The extension parser.
+ */
+class PaimonSparkSqlExtensionsParser(delegate: ParserInterface)
+  extends ParserInterface
+  with Logging {
+
+  private lazy val substitutor = new VariableSubstitution()
+  private lazy val astBuilder = new PaimonSqlExtensionsAstBuilder(delegate)
+
+  /** Parses a string to a LogicalPlan. */
+  override def parsePlan(sqlText: String): LogicalPlan = {
+    val sqlTextAfterSubstitution = substitutor.substitute(sqlText)
+    if (isCommand(sqlTextAfterSubstitution)) {
+      parse(sqlTextAfterSubstitution)(parser => astBuilder.visit(parser.singleStatement()))
+        .asInstanceOf[LogicalPlan]
+    } else {
+      delegate.parsePlan(sqlText)
+    }
+  }
+
+  /** Parses a string to an Expression. */
+  override def parseExpression(sqlText: String): Expression = {
+    delegate.parseExpression(sqlText)
+  }
+
+  /** Parses a string to a TableIdentifier. */
+  override def parseTableIdentifier(sqlText: String): TableIdentifier = {
+    delegate.parseTableIdentifier(sqlText)
+  }
+
+  /** Parses a string to a FunctionIdentifier. */
+  override def parseFunctionIdentifier(sqlText: String): FunctionIdentifier = {
+    delegate.parseFunctionIdentifier(sqlText)
+  }
+
+  /**
+   * Creates StructType for a given SQL string, which is a comma separated list of field definitions
+   * which will preserve the correct Hive metadata.
+   */
+  override def parseTableSchema(sqlText: String): StructType = {
+    delegate.parseTableSchema(sqlText)
+  }
+
+  /** Parses a string to a DataType. */
+  override def parseDataType(sqlText: String): DataType = {
+    delegate.parseDataType(sqlText)
+  }
+
+  /** Parses a string to a multi-part identifier. */
+  override def parseMultipartIdentifier(sqlText: String): Seq[String] = {
+    delegate.parseMultipartIdentifier(sqlText)
+  }
+
+  /** Returns whether SQL text is command. */
+  private def isCommand(sqlText: String): Boolean = {
+    val normalized = sqlText
+      .toLowerCase(Locale.ROOT)
+      .trim()
+      .replaceAll("--.*?\\n", " ")
+      .replaceAll("\\s+", " ")
+      .replaceAll("/\\*.*?\\*/", " ")
+      .trim()
+    normalized.startsWith("call")
+  }
+
+  protected def parse[T](command: String)(toResult: PaimonSqlExtensionsParser => T): T = {
+    val lexer = new PaimonSqlExtensionsLexer(
+      new UpperCaseCharStream(CharStreams.fromString(command)))
+    lexer.removeErrorListeners()
+    lexer.addErrorListener(PaimonParseErrorListener)
+
+    val tokenStream = new CommonTokenStream(lexer)
+    val parser = new PaimonSqlExtensionsParser(tokenStream)
+    parser.addParseListener(PaimonSqlExtensionsPostProcessor)
+    parser.removeErrorListeners()
+    parser.addErrorListener(PaimonParseErrorListener)
+
+    try {
+      try {
+        parser.getInterpreter.setPredictionMode(PredictionMode.SLL)
+        toResult(parser)
+      } catch {
+        case _: ParseCancellationException =>
+          tokenStream.seek(0)
+          parser.reset()
+          parser.getInterpreter.setPredictionMode(PredictionMode.LL)
+          toResult(parser)
+      }
+    } catch {
+      case e: PaimonParseException if e.command.isDefined =>
+        throw e
+      case e: PaimonParseException =>
+        throw e.withCommand(command)
+      case e: AnalysisException =>
+        val position = Origin(e.line, e.startPosition)
+        throw new PaimonParseException(Option(command), e.message, position, position)
+    }
+  }
+
+  override def parseQuery(sqlText: String): LogicalPlan = {

Review Comment:
   I think we can delete `PaimonSparkSqlExtensionsParser` files in Spark3.1, 3.2, 3.2, just remain this in spark-common.
   And remove this `override` identifier from this method that added in https://issues.apache.org/jira/browse/SPARK-37266. 



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

To unsubscribe, e-mail: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org