Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 01650200B4B for ; Thu, 21 Jul 2016 14:16:48 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id F3FC6160A6D; Thu, 21 Jul 2016 12:16:47 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 458D6160A68 for ; Thu, 21 Jul 2016 14:16:47 +0200 (CEST) Received: (qmail 20928 invoked by uid 500); 21 Jul 2016 12:16:46 -0000 Mailing-List: contact reviews-help@spark.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Delivered-To: mailing list reviews@spark.apache.org Received: (qmail 20916 invoked by uid 99); 21 Jul 2016 12:16:46 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 21 Jul 2016 12:16:46 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 1C929E1101; Thu, 21 Jul 2016 12:16:46 +0000 (UTC) From: dongjoon-hyun To: reviews@spark.apache.org Reply-To: reviews@spark.apache.org References: In-Reply-To: Subject: [GitHub] spark pull request #14132: [SPARK-16475][SQL] Broadcast Hint for SQL Queries Content-Type: text/plain Message-Id: <20160721121646.1C929E1101@git1-us-west.apache.org> Date: Thu, 21 Jul 2016 12:16:46 +0000 (UTC) archived-at: Thu, 21 Jul 2016 12:16:48 -0000 Github user dongjoon-hyun commented on a diff in the pull request: https://github.com/apache/spark/pull/14132#discussion_r71694195 --- Diff: sql/core/src/main/scala/org/apache/spark/sql/catalyst/SQLBuilder.scala --- @@ -425,6 +449,44 @@ class SQLBuilder(logicalPlan: LogicalPlan) extends Logging { } } + /** + * Merge and move upward to the nearest Project. + * A broadcast hint comment is scattered into multiple nodes inside the plan, and the + * information of BroadcastHint resides its current position inside the plan. In order to + * reconstruct broadcast hint comment, we need to pack the information of BroadcastHint into + * Hint("BROADCAST", _, _) and collect them up by moving upward to the nearest Project node. + */ + object NormalizeBroadcastHint extends Rule[LogicalPlan] { + override def apply(plan: LogicalPlan): LogicalPlan = plan transformUp { + // Capture the broadcasted information and store it in Hint. + case BroadcastHint(child @ SubqueryAlias(_, Project(_, SQLTable(database, table, _, _)))) => + Hint("BROADCAST", Seq(table), child) + + // Nearest Project is found. + case p @ Project(_, Hint(_, _, _)) => p + + // Merge BROADCAST hints up to the nearest Project. + case Hint("BROADCAST", params1, h @ Hint("BROADCAST", params2, _)) => + h.copy(parameters = params1 ++ params2) + case j @ Join(h1 @ Hint("BROADCAST", p1, left), h2 @ Hint("BROADCAST", p2, right), _, _) => + h1.copy(parameters = p1 ++ p2, child = j.copy(left = left, right = right)) + + // Bubble up BROADCAST hints to the nearest Project. + case j @ Join(h @ Hint("BROADCAST", _, hintChild), _, _, _) => + h.copy(child = j.copy(left = hintChild)) + case j @ Join(_, h @ Hint("BROADCAST", _, hintChild), _, _) => + h.copy(child = j.copy(right = hintChild)) --- End diff -- Here is the candidate. Since we cannot take advantage of `case` statement, I added a function for that. ``` private def isBroadcastHint(logicalPlan: LogicalPlan): Boolean = logicalPlan.isInstanceOf[Hint] && logicalPlan.asInstanceOf[Hint].name.equals("BRAODCAST") ... case j: Join if j.children.exists(isBroadcastHint) => val newChildren = j.children.map(x => if (isBroadcastHint(x)) x.asInstanceOf[Hint].child else x) Hint("BROADCAST", j.children.filter(isBroadcastHint).map(_.asInstanceOf[Hint]).flatMap(_.parameters), j.copy(left = newChildren(0), right = newChildren(1))) ``` --- 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