Github user hvanhovell commented on a diff in the pull request: https://github.com/apache/spark/pull/13706#discussion_r118846490 --- Diff: sql/core/src/main/scala/org/apache/spark/sql/execution/command/macros.scala --- @@ -0,0 +1,99 @@ +/* + * 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._ +import org.apache.spark.sql.catalyst.expressions._ +import org.apache.spark.sql.types.StructField + +/** + * This class provides arguments and body expression of the macro function. + */ +case class MacroFunctionWrapper(columns: Seq[StructField], macroFunction: 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, + funcWrapper: MacroFunctionWrapper) + extends RunnableCommand { + + override def run(sparkSession: SparkSession): Seq[Row] = { + val catalog = sparkSession.sessionState.catalog + val columns = funcWrapper.columns.map { col => + AttributeReference(col.name, col.dataType, col.nullable, col.metadata)() } + val colToIndex: Map[String, Int] = columns.map(_.name).zipWithIndex.toMap + if (colToIndex.size != columns.size) { + throw new AnalysisException(s"Cannot support duplicate colNames " + + s"for CREATE TEMPORARY MACRO $macroName, actual columns: ${columns.mkString(",")}") + } + val macroFunction = funcWrapper.macroFunction.transform { + case u: UnresolvedAttribute => + val index = colToIndex.get(u.name).getOrElse( + throw new AnalysisException(s"Cannot find colName: ${u} " + + s"for CREATE TEMPORARY MACRO $macroName, actual columns: ${columns.mkString(",")}")) + BoundReference(index, columns(index).dataType, columns(index).nullable) + case u: UnresolvedFunction => + sparkSession.sessionState.catalog.lookupFunction(u.name, u.children) + case s: SubqueryExpression => + throw new AnalysisException(s"Cannot support Subquery: ${s} " + + s"for CREATE TEMPORARY MACRO $macroName") + case u: UnresolvedGenerator => + throw new AnalysisException(s"Cannot support Generator: ${u} " + + s"for CREATE TEMPORARY MACRO $macroName") + } + + val macroInfo = columns.mkString(",") + " -> " + funcWrapper.macroFunction.toString + val info = new ExpressionInfo(macroInfo, macroName, true) + val builder = (children: Seq[Expression]) => { + if (children.size != columns.size) { + throw new AnalysisException(s"Actual number of columns: ${children.size} != " + + s"expected number of columns: ${columns.size} for Macro $macroName") + } + macroFunction.transform { + // Skip to validate the input type because check it at runtime. --- End diff -- How do we check at runtime? The current code does not seem to respect the types passed, and rely on the macro's expression to do some type validation, this means you can pass anything to the macro and the user can end up with an unexpected result: ```sql create macro plus(a int, b int) as a + b; select plus(1.0, 1.0) as result -- This returns a decimal, and not an int as expected ``` So I think we should at least validate the input expressions. The hacky way would be to add casts, and have the analyzer fail if the cast cannot be made (this is terrible UX). A better way to would be to create some sentinel expression that makes sure the analyzer will insert the correct cast, and throws a relevant exception (mentioning the macro) when this blows up...
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org