-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-15988] [SQL] Implement DDL commands: Create/Drop temporary macro #13706
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 2 commits
f2433c2
0b93636
301e950
808a5fa
f4ed3bc
af0136d
5550496
9fe1881
b8ffdc9
fb8b57a
e895a9c
8d520eb
651b485
277ba9f
314913d
3d05e4f
22d8b1a
d91f633
1eb23c7
ad85109
b52698f
3eacebc
fce1121
eaff4e9
97632a9
4ee32e9
b539e94
1563f12
4d8e843
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 |
|---|---|---|
|
|
@@ -97,6 +97,9 @@ statement | |
| | CREATE TEMPORARY? FUNCTION qualifiedName AS className=STRING | ||
| (USING resource (',' resource)*)? #createFunction | ||
| | DROP TEMPORARY? FUNCTION (IF EXISTS)? qualifiedName #dropFunction | ||
| | CREATE TEMPORARY MACRO macroName=identifier | ||
| '('(columns=colTypeList)?')' expression #createMacro | ||
|
||
| | DROP TEMPORARY MACRO (IF EXISTS)? macroName=identifier #dropMacro | ||
| | EXPLAIN (LOGICAL | FORMATTED | EXTENDED | CODEGEN)? statement #explain | ||
| | SHOW TABLES ((FROM | IN) db=identifier)? | ||
| (LIKE? pattern=STRING)? #showTables | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ import org.antlr.v4.runtime.tree.TerminalNode | |
| import org.apache.spark.sql.SaveMode | ||
| import org.apache.spark.sql.catalyst.{FunctionIdentifier, TableIdentifier} | ||
| import org.apache.spark.sql.catalyst.catalog._ | ||
| import org.apache.spark.sql.catalyst.expressions.AttributeReference | ||
| import org.apache.spark.sql.catalyst.parser._ | ||
| import org.apache.spark.sql.catalyst.parser.SqlBaseParser._ | ||
| import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, OneRowRelation, ScriptInputOutputSchema} | ||
|
|
@@ -589,6 +590,38 @@ class SparkSqlAstBuilder(conf: SQLConf) extends AstBuilder { | |
| ctx.TEMPORARY != null) | ||
| } | ||
|
|
||
| /** | ||
| * Create a [[CreateMacroCommand]] command. | ||
| * | ||
| * For example: | ||
| * {{{ | ||
| * CREATE TEMPORARY MACRO macro_name([col_name col_type, ...]) expression; | ||
| * }}} | ||
| */ | ||
| override def visitCreateMacro(ctx: CreateMacroContext): LogicalPlan = withOrigin(ctx) { | ||
| val arguments = Option(ctx.columns).toSeq.flatMap(visitCatalogColumns).map { col => | ||
|
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. Call |
||
| AttributeReference(col.name, CatalystSqlParser.parseDataType(col.dataType))() | ||
| } | ||
| val e = expression(ctx.expression) | ||
| CreateMacroCommand( | ||
| ctx.macroName.getText, | ||
| MacroFunctionWrapper(arguments, e)) | ||
| } | ||
|
|
||
| /** | ||
| * Create a [[DropMacroCommand]] command. | ||
| * | ||
| * For example: | ||
| * {{{ | ||
| * DROP TEMPORARY MACRO [IF EXISTS] macro_name; | ||
| * }}} | ||
| */ | ||
| override def visitDropMacro(ctx: DropMacroContext): LogicalPlan = withOrigin(ctx) { | ||
| DropMacroCommand( | ||
| ctx.macroName.getText, | ||
| ctx.EXISTS != null) | ||
| } | ||
|
|
||
| /** | ||
| * Create a [[DropTableCommand]] command. | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /* | ||
| * 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.execution.command | ||
|
|
||
| import org.apache.spark.sql.{AnalysisException, Row, SparkSession} | ||
| import org.apache.spark.sql.catalyst.analysis.{FunctionRegistry, UnresolvedAttribute} | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
|
|
||
| /** | ||
| * This class provides arguments and body expression of the macro. | ||
| */ | ||
| case class MacroFunctionWrapper(arguments: Seq[AttributeReference], body: Expression) | ||
|
||
|
|
||
| /** | ||
| * The DDL command that creates a macro. | ||
| * To create a temporary macro, the syntax of using this command in SQL is: | ||
| * {{{ | ||
| * CREATE TEMPORARY MACRO macro_name([col_name col_type, ...]) expression; | ||
| * }}} | ||
| */ | ||
| case class CreateMacroCommand(macroName: String, macroFunction: MacroFunctionWrapper) | ||
| extends RunnableCommand { | ||
|
|
||
| override def run(sparkSession: SparkSession): Seq[Row] = { | ||
| val catalog = sparkSession.sessionState.catalog | ||
| val inputSet = AttributeSet(macroFunction.arguments) | ||
|
||
| val colNames = macroFunction.arguments.map(_.name) | ||
|
||
| val colToIndex: Map[String, Int] = colNames.zipWithIndex.toMap | ||
| macroFunction.body.transformUp { | ||
| case u @ UnresolvedAttribute(nameParts) => | ||
| assert(nameParts.length == 1) | ||
|
||
| colToIndex.get(nameParts.head).getOrElse( | ||
| throw new AnalysisException(s"Cannot create temporary macro '$macroName', " + | ||
| s"cannot resolve: [${u}] given input columns: [${inputSet}]")) | ||
|
||
| u | ||
|
||
| case _: SubqueryExpression => | ||
| throw new AnalysisException(s"Cannot create temporary macro '$macroName', " + | ||
| s"cannot support subquery for macro.") | ||
| } | ||
|
|
||
| val macroInfo = macroFunction.arguments.mkString(",") + "->" + macroFunction.body.toString | ||
| val info = new ExpressionInfo(macroInfo, macroName) | ||
| val builder = (children: Seq[Expression]) => { | ||
| if (children.size != colNames.size) { | ||
| throw new AnalysisException(s"actual number of arguments: ${children.size} != " + | ||
| s"expected number of arguments: ${colNames.size} for Macro $macroName") | ||
| } | ||
| macroFunction.body.transformUp { | ||
| case u @ UnresolvedAttribute(nameParts) => | ||
|
||
| assert(nameParts.length == 1) | ||
| colToIndex.get(nameParts.head).map(children(_)).getOrElse( | ||
| throw new AnalysisException(s"Macro '$macroInfo' cannot resolve '$u' " + | ||
| s"given input expressions: [${children.mkString(",")}]")) | ||
| } | ||
| } | ||
| catalog.createTempFunction(macroName, info, builder, ignoreIfExists = false) | ||
| Seq.empty[Row] | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The DDL command that drops a macro. | ||
| * ifExists: returns an error if the macro doesn't exist, unless this is true. | ||
| * {{{ | ||
| * DROP TEMPORARY MACRO [IF EXISTS] macro_name; | ||
| * }}} | ||
| */ | ||
| case class DropMacroCommand(macroName: String, ifExists: Boolean) | ||
| extends RunnableCommand { | ||
|
|
||
| override def run(sparkSession: SparkSession): Seq[Row] = { | ||
|
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. This will drop any function... Can we make it Macro specific? |
||
| val catalog = sparkSession.sessionState.catalog | ||
| if (FunctionRegistry.builtin.functionExists(macroName)) { | ||
| throw new AnalysisException(s"Cannot drop native function '$macroName'") | ||
| } | ||
| catalog.dropTempFunction(macroName, ifExists) | ||
| Seq.empty[Row] | ||
| } | ||
| } | ||
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.
Does Hive also support non-temporary macro's.
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.
No, Now Hive only support temporary macro's.