spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hvanhovell <...@git.apache.org>
Subject [GitHub] spark pull request #13706: [SPARK-15988] [SQL] Implement DDL commands: Creat...
Date Sun, 28 May 2017 18:31:26 GMT
Github user hvanhovell commented on a diff in the pull request:

    https://github.com/apache/spark/pull/13706#discussion_r118846647
  
    --- 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 --
    
    On a related note, we are currently not sure if the macro produces a valid expression.
Maybe we should run analysis on the macro expression to make sure it does not fail every query
later on, e.g.:
    ```scala
    
    val resolvedMacroFunction = try {
      val plan = Project(Alias(macroFunction, "m")() :: Nil, OneRowRelation)
      val analyzed @ Project(Seq(named), OneRowRelation) =
        sparkSession.sessionState.analyzer.execute(plan)
      sparkSession.sessionState.analyzer.checkAnalysis(analyzed)
      named.children.head
    } catch {
      case a: AnalysisException =>
        ...
    }
    ```
    Note that we cannot use generators if we use this approach...


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message