-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[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
Changes from 1 commit
ba26cd1
8947d37
fb24edf
898ed81
ae9290a
bbca7dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| /* | ||
| * 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 scala.util.parsing.input.CharArrayReader.EofCh | ||
| import org.apache.spark.sql.catalyst.plans.logical._ | ||
| import org.apache.spark.sql.catalyst.SqlLexical | ||
| import scala.util.parsing.combinator.lexical.StdLexical | ||
|
|
||
| /** | ||
| * A simple Hive SQL pre parser. It parses the commands like cache,uncache etc and | ||
| * remaining actual query will be parsed by HiveQl.parseSql | ||
| */ | ||
|
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. Since this class takes over all HiveQL parsing work (although it delegates to an underlying Hive parser), it's not accurate to call it a "pre parser", maybe this:
Contributor
Author
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. Looks good. I updated as per your comment |
||
| class HiveSqlParser extends StandardTokenParsers with PackratParsers { | ||
|
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. Maybe
Contributor
Author
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. OK. Changed the file name. |
||
|
|
||
| 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")) { | ||
|
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. I'd like to handle
Contributor
Author
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. OK. |
||
| 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 | ||
|
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. Nit: I'd prefer
Contributor
Author
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. updated |
||
| ) | ||
|
|
||
| protected lazy val hiveQl: Parser[LogicalPlan] = | ||
| remainingQuery ^^ { | ||
| case r => HiveQl.parseSql(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, | ||
|
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. Nit: space after
Contributor
Author
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. Updated |
||
| 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()) | ||
| } | ||
|
|
||
| } | ||
|
|
||
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.
Can we move this to
HiveQl? It seems more reasonable to put all HiveQL parsing logic together.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.
OK. I have moved it to HiveQl now.