-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize common case: SELECT COUNT(*) FROM Table Fix #1192
## Description Running the query "SELECT COUNT(*) FROM Table" takes a lot of time for big tables, Spark scan all the parquet files just to return the number of rows, that information is available from Delta Logs. Resolves #1192 Created unit tests to validate the optimization works, including cases not covered by this optimization. ## Does this PR introduce _any_ user-facing changes? Only performance improvement Closes #1377 Signed-off-by: Shixiong Zhu <zsxwing@gmail.com> GitOrigin-RevId: a9116e42a9c805adc967dd3e802f84d502f50a8b
- Loading branch information
1 parent
1521be5
commit 0c349da
Showing
6 changed files
with
412 additions
and
6 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
.../src/main/scala/org/apache/spark/sql/delta/optimizer/OptimizeMetadataOnlyDeltaQuery.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,75 @@ | ||
/* | ||
* Copyright (2021) The Delta Lake Project Authors. | ||
* | ||
* Licensed 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.delta.optimizer | ||
|
||
import org.apache.spark.sql.Column | ||
import org.apache.spark.sql.catalyst.InternalRow | ||
import org.apache.spark.sql.catalyst.expressions.{Alias, Literal} | ||
import org.apache.spark.sql.catalyst.expressions.aggregate.{AggregateExpression, Complete, Count} | ||
import org.apache.spark.sql.catalyst.planning.PhysicalOperation | ||
import org.apache.spark.sql.catalyst.plans.logical._ | ||
import org.apache.spark.sql.delta.DeltaTable | ||
import org.apache.spark.sql.delta.files.TahoeLogFileIndex | ||
import org.apache.spark.sql.delta.stats.DeltaScanGenerator | ||
import org.apache.spark.sql.functions.{count, sum} | ||
|
||
trait OptimizeMetadataOnlyDeltaQuery { | ||
def optimizeQueryWithMetadata(plan: LogicalPlan): LogicalPlan = { | ||
plan.transformUpWithSubqueries { | ||
case agg@CountStarDeltaTable(countValue) => | ||
LocalRelation(agg.output, Seq(InternalRow(countValue))) | ||
} | ||
} | ||
|
||
protected def getDeltaScanGenerator(index: TahoeLogFileIndex): DeltaScanGenerator | ||
|
||
object CountStarDeltaTable { | ||
def unapply(plan: Aggregate): Option[Long] = { | ||
plan match { | ||
case Aggregate( | ||
Nil, | ||
Seq(Alias(AggregateExpression(Count(Seq(Literal(1, _))), Complete, false, None, _), _)), | ||
PhysicalOperation(_, Nil, DeltaTable(tahoeLogFileIndex: TahoeLogFileIndex))) => | ||
extractGlobalCount(tahoeLogFileIndex) | ||
case _ => None | ||
} | ||
} | ||
|
||
private def extractGlobalCount(tahoeLogFileIndex: TahoeLogFileIndex): Option[Long] = { | ||
val row = getDeltaScanGenerator(tahoeLogFileIndex).filesWithStatsForScan(Nil) | ||
.agg( | ||
sum("stats.numRecords"), | ||
count(new Column("*")), | ||
count(new Column("stats.numRecords"))) | ||
.first | ||
|
||
val numOfFiles = row.getLong(1) | ||
val numOfFilesWithStats = row.getLong(2) | ||
|
||
if (numOfFiles == numOfFilesWithStats) { | ||
val numRecords = if (row.isNullAt(0)) { | ||
0 // It is Null if deltaLog.snapshot.allFiles is empty | ||
} else { row.getLong(0) } | ||
|
||
Some(numRecords) | ||
} else { | ||
// If COUNT(*) is greater than COUNT(numRecords) means not every AddFile records has stats | ||
None | ||
} | ||
} | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.