-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-3654][SQL] Implement all extended HiveQL statements/commands with a separate parser combinator #2590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ba26cd1
Created seperate parser for hql.It pre parses the commands like
ravipesala 8947d37
Removed duplicate code
ravipesala fb24edf
Updated the code as per admin comments
ravipesala 898ed81
Removed spaces
ravipesala ae9290a
Fixed style issues as per Admin comments
ravipesala bbca7dd
Fixed code as per admin comments.
ravipesala File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
sql/hive/src/main/scala/org/apache/spark/sql/hive/ExtendedHiveQlParser.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| /* | ||
| * 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 | ||
|
|
||
| import scala.language.implicitConversions | ||
| import scala.util.parsing.combinator.syntactical.StandardTokenParsers | ||
| import scala.util.parsing.combinator.PackratParsers | ||
| import org.apache.spark.sql.catalyst.plans.logical._ | ||
| import org.apache.spark.sql.catalyst.SqlLexical | ||
|
|
||
| /** | ||
| * A parser that recognizes all HiveQL constructs together with several Spark SQL specific | ||
| * extensions like CACHE TABLE and UNCACHE TABLE. | ||
| */ | ||
| private[hive] class ExtendedHiveQlParser extends StandardTokenParsers with PackratParsers { | ||
|
|
||
| def apply(input: String): LogicalPlan = { | ||
| // Special-case out set commands since the value fields can be | ||
| // complex to handle without RegexParsers. Also this approach | ||
| // is clearer for the several possible cases of set commands. | ||
| if (input.trim.toLowerCase.startsWith("set")) { | ||
| input.trim.drop(3).split("=", 2).map(_.trim) match { | ||
| case Array("") => // "set" | ||
| SetCommand(None, None) | ||
| case Array(key) => // "set key" | ||
| SetCommand(Some(key), None) | ||
| case Array(key, value) => // "set key=value" | ||
| SetCommand(Some(key), Some(value)) | ||
| } | ||
| } else if (input.trim.startsWith("!")) { | ||
| ShellCommand(input.drop(1)) | ||
| } else { | ||
| phrase(query)(new lexical.Scanner(input)) match { | ||
| case Success(r, x) => r | ||
| case x => sys.error(x.toString) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| protected case class Keyword(str: String) | ||
|
|
||
| protected val CACHE = Keyword("CACHE") | ||
| protected val SET = Keyword("SET") | ||
| protected val ADD = Keyword("ADD") | ||
| protected val JAR = Keyword("JAR") | ||
| protected val TABLE = Keyword("TABLE") | ||
| protected val AS = Keyword("AS") | ||
| protected val UNCACHE = Keyword("UNCACHE") | ||
| protected val FILE = Keyword("FILE") | ||
| protected val DFS = Keyword("DFS") | ||
| protected val SOURCE = Keyword("SOURCE") | ||
|
|
||
| protected implicit def asParser(k: Keyword): Parser[String] = | ||
| lexical.allCaseVersions(k.str).map(x => x : Parser[String]).reduce(_ | _) | ||
|
|
||
| protected def allCaseConverse(k: String): Parser[String] = | ||
| lexical.allCaseVersions(k).map(x => x : Parser[String]).reduce(_ | _) | ||
|
|
||
| protected val reservedWords = | ||
| this.getClass | ||
| .getMethods | ||
| .filter(_.getReturnType == classOf[Keyword]) | ||
| .map(_.invoke(this).asInstanceOf[Keyword].str) | ||
|
|
||
| override val lexical = new SqlLexical(reservedWords) | ||
|
|
||
| protected lazy val query: Parser[LogicalPlan] = | ||
| cache | uncache | addJar | addFile | dfs | source | hiveQl | ||
|
|
||
| protected lazy val hiveQl: Parser[LogicalPlan] = | ||
| remainingQuery ^^ { | ||
| case r => HiveQl.createPlan(r.trim()) | ||
| } | ||
|
|
||
| /** It returns all remaining query */ | ||
| protected lazy val remainingQuery: Parser[String] = new Parser[String] { | ||
| def apply(in: Input) = | ||
| Success( | ||
| in.source.subSequence(in.offset, in.source.length).toString, | ||
| in.drop(in.source.length())) | ||
| } | ||
|
|
||
| /** It returns all query */ | ||
| protected lazy val allQuery: Parser[String] = new Parser[String] { | ||
| def apply(in: Input) = | ||
| Success(in.source.toString, in.drop(in.source.length())) | ||
| } | ||
|
|
||
| protected lazy val cache: Parser[LogicalPlan] = | ||
| CACHE ~ TABLE ~> ident ~ opt(AS ~> hiveQl) ^^ { | ||
| case tableName ~ None => CacheCommand(tableName, true) | ||
| case tableName ~ Some(plan) => | ||
| CacheTableAsSelectCommand(tableName, plan) | ||
| } | ||
|
|
||
| protected lazy val uncache: Parser[LogicalPlan] = | ||
| UNCACHE ~ TABLE ~> ident ^^ { | ||
| case tableName => CacheCommand(tableName, false) | ||
| } | ||
|
|
||
| protected lazy val addJar: Parser[LogicalPlan] = | ||
| ADD ~ JAR ~> remainingQuery ^^ { | ||
| case rq => AddJar(rq.trim()) | ||
| } | ||
|
|
||
| protected lazy val addFile: Parser[LogicalPlan] = | ||
| ADD ~ FILE ~> remainingQuery ^^ { | ||
| case rq => AddFile(rq.trim()) | ||
| } | ||
|
|
||
| protected lazy val dfs: Parser[LogicalPlan] = | ||
| DFS ~> allQuery ^^ { | ||
| case aq => NativeCommand(aq.trim()) | ||
| } | ||
|
|
||
| protected lazy val source: Parser[LogicalPlan] = | ||
| SOURCE ~> remainingQuery ^^ { | ||
| case rq => SourceCommand(rq.trim()) | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noticed lots of trailing spaces in this file, please remove them. |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: adjust indentation