forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-23877][SQL] Use filter predicates to prune partitions in metad…
…ata-only queries ## What changes were proposed in this pull request? This updates the OptimizeMetadataOnlyQuery rule to use filter expressions when listing partitions, if there are filter nodes in the logical plan. This avoids listing all partitions for large tables on the driver. This also fixes a minor bug where the partitions returned from fsRelation cannot be serialized without hitting a stack level too deep error. This is caused by serializing a stream to executors, where the stream is a recursive structure. If the stream is too long, the serialization stack reaches the maximum level of depth. The fix is to create a LocalRelation using an Array instead of the incoming Seq. ## How was this patch tested? Existing tests for metadata-only queries. Author: Ryan Blue <[email protected]> Closes apache#20988 from rdblue/SPARK-23877-metadata-only-push-filters.
- Loading branch information
Showing
2 changed files
with
132 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
sql/hive/src/test/scala/org/apache/spark/sql/hive/OptimizeHiveMetadataOnlyQuerySuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* 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 org.scalatest.BeforeAndAfter | ||
|
||
import org.apache.spark.metrics.source.HiveCatalogMetrics | ||
import org.apache.spark.sql.QueryTest | ||
import org.apache.spark.sql.catalyst.expressions.NamedExpression | ||
import org.apache.spark.sql.catalyst.plans.logical.{Distinct, Filter, Project, SubqueryAlias} | ||
import org.apache.spark.sql.hive.test.TestHiveSingleton | ||
import org.apache.spark.sql.test.SQLTestUtils | ||
import org.apache.spark.sql.types.{IntegerType, StructField, StructType} | ||
|
||
class OptimizeHiveMetadataOnlyQuerySuite extends QueryTest with TestHiveSingleton | ||
with BeforeAndAfter with SQLTestUtils { | ||
|
||
import spark.implicits._ | ||
|
||
before { | ||
sql("CREATE TABLE metadata_only (id bigint, data string) PARTITIONED BY (part int)") | ||
(0 to 10).foreach(p => sql(s"ALTER TABLE metadata_only ADD PARTITION (part=$p)")) | ||
} | ||
|
||
test("SPARK-23877: validate metadata-only query pushes filters to metastore") { | ||
withTable("metadata_only") { | ||
val startCount = HiveCatalogMetrics.METRIC_PARTITIONS_FETCHED.getCount | ||
|
||
// verify the number of matching partitions | ||
assert(sql("SELECT DISTINCT part FROM metadata_only WHERE part < 5").collect().length === 5) | ||
|
||
// verify that the partition predicate was pushed down to the metastore | ||
assert(HiveCatalogMetrics.METRIC_PARTITIONS_FETCHED.getCount - startCount === 5) | ||
} | ||
} | ||
|
||
test("SPARK-23877: filter on projected expression") { | ||
withTable("metadata_only") { | ||
val startCount = HiveCatalogMetrics.METRIC_PARTITIONS_FETCHED.getCount | ||
|
||
// verify the matching partitions | ||
val partitions = spark.internalCreateDataFrame(Distinct(Filter(($"x" < 5).expr, | ||
Project(Seq(($"part" + 1).as("x").expr.asInstanceOf[NamedExpression]), | ||
spark.table("metadata_only").logicalPlan.asInstanceOf[SubqueryAlias].child))) | ||
.queryExecution.toRdd, StructType(Seq(StructField("x", IntegerType)))) | ||
|
||
checkAnswer(partitions, Seq(1, 2, 3, 4).toDF("x")) | ||
|
||
// verify that the partition predicate was not pushed down to the metastore | ||
assert(HiveCatalogMetrics.METRIC_PARTITIONS_FETCHED.getCount - startCount == 11) | ||
} | ||
} | ||
} |