-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-54022][SQL] Make DSv2 table resolution aware of cached tables #52764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* | ||
| * 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.catalyst.analysis | ||
|
|
||
| import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan | ||
|
|
||
| private[sql] trait RelationCache { | ||
| def lookup(nameParts: Seq[String], resolver: Resolver): Option[LogicalPlan] | ||
| } | ||
|
|
||
| private[sql] object RelationCache { | ||
| val empty: RelationCache = (_, _) => None | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,7 +40,6 @@ import org.apache.spark.sql.connector.write.RowLevelOperation.Command.{DELETE, M | |
| import org.apache.spark.sql.errors.DataTypeErrors.toSQLType | ||
| import org.apache.spark.sql.errors.QueryExecutionErrors | ||
| import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, ExtractV2Table} | ||
| import org.apache.spark.sql.execution.datasources.v2.V2TableRefreshUtil | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.types.{ArrayType, AtomicType, BooleanType, DataType, IntegerType, MapType, MetadataBuilder, StringType, StructField, StructType} | ||
| import org.apache.spark.util.ArrayImplicits._ | ||
|
|
@@ -689,12 +688,7 @@ case class ReplaceTableAsSelect( | |
| extends V2CreateTableAsSelectPlan { | ||
|
|
||
| override def markAsAnalyzed(ac: AnalysisContext): LogicalPlan = { | ||
| // RTAS may drop and recreate table before query execution, breaking self-references | ||
| // refresh and pin versions here to read from original table versions instead of | ||
| // newly created empty table that is meant to serve as target for append/overwrite | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why we can remove this now?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We simply moved this to exec node as I no longer have access to |
||
| val refreshedQuery = V2TableRefreshUtil.refresh(query, versionedOnly = true) | ||
| val pinnedQuery = V2TableRefreshUtil.pinVersions(refreshedQuery) | ||
| copy(query = pinnedQuery, isAnalyzed = true) | ||
| copy(isAnalyzed = true) | ||
| } | ||
|
|
||
| override def withPartitioning(rewritten: Seq[Transform]): V2CreateTablePlan = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ import org.apache.hadoop.fs.{FileSystem, Path} | |
| import org.apache.spark.internal.{Logging, MessageWithContext} | ||
| import org.apache.spark.internal.LogKeys._ | ||
| import org.apache.spark.sql.catalyst.analysis.EliminateSubqueryAliases | ||
| import org.apache.spark.sql.catalyst.analysis.Resolver | ||
| import org.apache.spark.sql.catalyst.analysis.V2TableReference | ||
| import org.apache.spark.sql.catalyst.catalog.HiveTableRelation | ||
| import org.apache.spark.sql.catalyst.expressions.{Attribute, SubqueryExpression} | ||
|
|
@@ -30,13 +31,14 @@ import org.apache.spark.sql.catalyst.plans.logical.{IgnoreCachedData, LogicalPla | |
| import org.apache.spark.sql.catalyst.trees.TreePattern.PLAN_EXPRESSION | ||
| import org.apache.spark.sql.catalyst.util.sideBySide | ||
| import org.apache.spark.sql.classic.{Dataset, SparkSession} | ||
| import org.apache.spark.sql.connector.catalog.CatalogV2Implicits.IdentifierHelper | ||
| import org.apache.spark.sql.connector.catalog.CatalogPlugin | ||
| import org.apache.spark.sql.connector.catalog.CatalogV2Implicits.{IdentifierHelper, MultipartIdentifierHelper} | ||
| import org.apache.spark.sql.connector.catalog.Identifier | ||
| import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper | ||
| import org.apache.spark.sql.execution.columnar.InMemoryRelation | ||
| import org.apache.spark.sql.execution.command.CommandUtils | ||
| import org.apache.spark.sql.execution.datasources.{FileIndex, HadoopFsRelation, LogicalRelation, LogicalRelationWithTable} | ||
| import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, ExtractV2Table, FileTable} | ||
| import org.apache.spark.sql.execution.datasources.v2.V2TableRefreshUtil | ||
| import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, ExtractV2CatalogAndIdentifier, ExtractV2Table, FileTable, V2TableRefreshUtil} | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.storage.StorageLevel | ||
| import org.apache.spark.storage.StorageLevel.MEMORY_AND_DISK | ||
|
|
@@ -240,31 +242,51 @@ class CacheManager extends Logging with AdaptiveSparkPlanHelper { | |
| name: Seq[String], | ||
| conf: SQLConf, | ||
| includeTimeTravel: Boolean): Boolean = { | ||
| def isSameName(nameInCache: Seq[String]): Boolean = { | ||
| nameInCache.length == name.length && nameInCache.zip(name).forall(conf.resolver.tupled) | ||
| } | ||
| isMatchedTableOrView(plan, name, conf.resolver, includeTimeTravel) | ||
| } | ||
|
|
||
| private def isMatchedTableOrView( | ||
| plan: LogicalPlan, | ||
| name: Seq[String], | ||
| resolver: Resolver, | ||
| includeTimeTravel: Boolean): Boolean = { | ||
|
|
||
| EliminateSubqueryAliases(plan) match { | ||
| case LogicalRelationWithTable(_, Some(catalogTable)) => | ||
| isSameName(catalogTable.identifier.nameParts) | ||
| isSameName(name, catalogTable.identifier.nameParts, resolver) | ||
|
|
||
| case DataSourceV2Relation(_, _, Some(catalog), Some(v2Ident), _, timeTravelSpec) => | ||
| val nameInCache = v2Ident.toQualifiedNameParts(catalog) | ||
| isSameName(nameInCache) && (includeTimeTravel || timeTravelSpec.isEmpty) | ||
| isSameName(name, nameInCache, resolver) && (includeTimeTravel || timeTravelSpec.isEmpty) | ||
|
|
||
| case r: V2TableReference => | ||
| isSameName(r.identifier.toQualifiedNameParts(r.catalog)) | ||
| isSameName(name, r.identifier.toQualifiedNameParts(r.catalog), resolver) | ||
|
|
||
| case v: View => | ||
| isSameName(v.desc.identifier.nameParts) | ||
| isSameName(name, v.desc.identifier.nameParts, resolver) | ||
|
|
||
| case HiveTableRelation(catalogTable, _, _, _, _) => | ||
| isSameName(catalogTable.identifier.nameParts) | ||
| isSameName(name, catalogTable.identifier.nameParts, resolver) | ||
|
|
||
| case _ => false | ||
| } | ||
| } | ||
|
|
||
| private def isSameName( | ||
| name: Seq[String], | ||
| catalog: CatalogPlugin, | ||
| ident: Identifier, | ||
| resolver: Resolver): Boolean = { | ||
| isSameName(name, ident.toQualifiedNameParts(catalog), resolver) | ||
| } | ||
|
|
||
| private def isSameName( | ||
| name: Seq[String], | ||
| nameInCache: Seq[String], | ||
| resolver: Resolver): Boolean = { | ||
| nameInCache.length == name.length && nameInCache.zip(name).forall(resolver.tupled) | ||
| } | ||
|
|
||
| private def uncacheByCondition( | ||
| spark: SparkSession, | ||
| isMatchedPlan: LogicalPlan => Boolean, | ||
|
|
@@ -354,7 +376,7 @@ class CacheManager extends Logging with AdaptiveSparkPlanHelper { | |
| cd.cachedRepresentation.cacheBuilder.clearCache() | ||
| val sessionWithConfigsOff = getOrCloneSessionWithConfigsOff(spark) | ||
| val (newKey, newCache) = sessionWithConfigsOff.withActive { | ||
| val refreshedPlan = V2TableRefreshUtil.refresh(cd.plan) | ||
| val refreshedPlan = V2TableRefreshUtil.refresh(sessionWithConfigsOff, cd.plan) | ||
| val qe = sessionWithConfigsOff.sessionState.executePlan(refreshedPlan) | ||
| qe.normalized -> InMemoryRelation(cd.cachedRepresentation.cacheBuilder, qe) | ||
| } | ||
|
|
@@ -371,6 +393,35 @@ class CacheManager extends Logging with AdaptiveSparkPlanHelper { | |
| } | ||
| } | ||
|
|
||
| private[sql] def lookupCachedTable( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's make it more straightforward:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aokolnychyi This is my last comment for this PR
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gengliangwang, I tested the rename locally, but not sure it would make the code any clearer. In fact, it only creates inconsistencies with other methods that accept name in args but don't have I feel |
||
| name: Seq[String], | ||
| resolver: Resolver): Option[LogicalPlan] = { | ||
| val cachedRelations = findCachedRelations(name, resolver) | ||
| cachedRelations match { | ||
| case cachedRelation +: _ => | ||
| CacheManager.logCacheOperation( | ||
| log"Relation cache hit for table ${MDC(TABLE_NAME, name.quoted)}") | ||
| Some(cachedRelation) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm we just return the first match? Shall we use the scan with the latest table version?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, we shouldn't have multiple matching relations after this change, but |
||
| case _ => | ||
| None | ||
| } | ||
| } | ||
|
|
||
| private def findCachedRelations( | ||
| name: Seq[String], | ||
| resolver: Resolver): Seq[LogicalPlan] = { | ||
| cachedData.flatMap { cd => | ||
| val plan = EliminateSubqueryAliases(cd.plan) | ||
| plan match { | ||
| case r @ ExtractV2CatalogAndIdentifier(catalog, ident) | ||
| if isSameName(name, catalog, ident, resolver) && r.timeTravelSpec.isEmpty => | ||
| Some(r) | ||
| case _ => | ||
| None | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Optionally returns cached data for the given [[Dataset]] | ||
| */ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be any relation cache in the future.