Github user liancheng commented on a diff in the pull request:

    https://github.com/apache/spark/pull/10541#discussion_r48803221
  
    --- Diff: 
sql/hive/src/main/scala/org/apache/spark/sql/hive/SQLBuilder.scala ---
    @@ -0,0 +1,180 @@
    +/*
    + * 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 java.util.concurrent.atomic.AtomicLong
    +
    +import org.apache.spark.Logging
    +import org.apache.spark.sql.SQLContext
    +import org.apache.spark.sql.catalyst.expressions.{NamedExpression, 
Attribute, SortOrder}
    +import org.apache.spark.sql.catalyst.optimizer.ProjectCollapsing
    +import org.apache.spark.sql.catalyst.plans.logical._
    +import org.apache.spark.sql.catalyst.rules.{Rule, RuleExecutor}
    +import org.apache.spark.sql.catalyst.util.sequenceOption
    +
    +class SQLBuilder(logicalPlan: LogicalPlan, sqlContext: SQLContext) extends 
Logging {
    +  def toSQL: Option[String] = {
    +    val canonicalizedPlan = Canonicalizer.execute(logicalPlan)
    +
    +    logDebug(
    +      s"""Building SQL query string from given logical plan:
    +         |
    +         |# Original logical plan:
    +         |${logicalPlan.treeString}
    +         |# Canonicalized logical plan:
    +         |${canonicalizedPlan.treeString}
    +       """.stripMargin)
    +
    +    toSQL(canonicalizedPlan)
    +  }
    +
    +  private def toSQL(node: LogicalPlan): Option[String] = node match {
    +    case plan @ Project(list, child) =>
    +      for {
    +        listSQL <- sequenceOption(list.map(_.sql))
    +        childSQL <- toSQL(child)
    +        from = child match {
    +          case OneRowRelation => ""
    +          case _ => " FROM "
    +        }
    +      } yield s"SELECT ${listSQL.mkString(", ")}$from$childSQL"
    +
    +    case plan @ Aggregate(groupingExpressions, aggregateExpressions, 
child) =>
    +      for {
    +        aggregateSQL <- sequenceOption(aggregateExpressions.map(_.sql))
    +        groupingSQL <- sequenceOption(groupingExpressions.map(_.sql))
    +        maybeGroupBy = if (groupingSQL.isEmpty) "" else " GROUP BY "
    +        maybeFrom = child match {
    +          case OneRowRelation => ""
    +          case _ => " FROM "
    +        }
    +        childSQL <- toSQL(child).map(maybeFrom + _)
    +      } yield {
    +        s"SELECT ${aggregateSQL.mkString(", 
")}$childSQL$maybeGroupBy${groupingSQL.mkString(", ")}"
    +      }
    +
    +    case plan @ Limit(limit, child) =>
    +      for {
    +        limitSQL <- limit.sql
    +        childSQL <- toSQL(child)
    +      } yield s"$childSQL LIMIT $limitSQL"
    +
    +    case plan @ Filter(condition, child) =>
    +      for {
    +        conditionSQL <- condition.sql
    +        childSQL <- toSQL(child)
    +        whereOrHaving = child match {
    +          case _: Aggregate => "HAVING"
    --- End diff --
    
    For query plans translated directly from HiveSQL, `Filter`s always stay 
directly above `Aggregate`s. But there might be analysis rules that insert 
extra auxiliary operators (e.g. `Project`s for type casting). Using subqueries 
might be a good idea. Need more investigation. Thanks for the suggestion!


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to