-
Notifications
You must be signed in to change notification settings - Fork 28.7k
[SPARK-23203][SQL] make DataSourceV2Relation immutable #20448
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
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* 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.execution.datasources.v2 | ||
|
||
import java.util.Objects | ||
|
||
import org.apache.commons.lang3.StringUtils | ||
|
||
import org.apache.spark.sql.catalyst.expressions.{Attribute, Expression} | ||
import org.apache.spark.sql.internal.SQLConf | ||
import org.apache.spark.sql.sources.v2.DataSourceV2 | ||
import org.apache.spark.util.Utils | ||
|
||
/** | ||
* A base class for data source v2 related query plan(both logical and physical). It defines the | ||
* equals/hashCode methods according to some common information. | ||
*/ | ||
trait DataSourceV2QueryPlan { | ||
|
||
def output: Seq[Attribute] | ||
def sourceClass: Class[_ <: DataSourceV2] | ||
def filters: Set[Expression] | ||
|
||
// The metadata of this data source relation that can be used for equality test. | ||
private def metadata: Seq[Any] = Seq(output, sourceClass, filters) | ||
|
||
def canEqual(other: Any): Boolean | ||
|
||
override def equals(other: Any): Boolean = other match { | ||
case other: DataSourceV2QueryPlan => | ||
canEqual(other) && metadata == other.metadata | ||
case _ => false | ||
} | ||
|
||
override def hashCode(): Int = { | ||
metadata.map(Objects.hashCode).foldLeft(0)((a, b) => 31 * a + b) | ||
} | ||
|
||
def metadataString: String = { | ||
val entries = scala.collection.mutable.ArrayBuffer.empty[(String, String)] | ||
if (filters.nonEmpty) entries += "PushedFilter" -> filters.mkString("[", ", ", "]") | ||
|
||
val outputStr = Utils.truncatedString(output, "[", ", ", "]") | ||
val entriesStr = Utils.truncatedString(entries.map { | ||
case (key, value) => key + ": " + StringUtils.abbreviate(redact(value), 100) | ||
}, " (", ", ", ")") | ||
|
||
s"${sourceClass.getSimpleName}$outputStr$entriesStr" | ||
} | ||
|
||
private def redact(text: String): String = { | ||
Utils.redact(SQLConf.get.stringRedationPattern, text) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,36 +17,84 @@ | |
|
||
package org.apache.spark.sql.execution.datasources.v2 | ||
|
||
import org.apache.spark.sql.catalyst.expressions.AttributeReference | ||
import org.apache.spark.sql.catalyst.expressions.{AttributeReference, AttributeSet, Expression} | ||
import org.apache.spark.sql.catalyst.plans.logical.{LeafNode, Statistics} | ||
import org.apache.spark.sql.sources.v2.{DataSourceOptions, DataSourceV2, ReadSupport, ReadSupportWithSchema} | ||
import org.apache.spark.sql.sources.v2.reader._ | ||
import org.apache.spark.sql.types.StructType | ||
|
||
/** | ||
* A logical plan representing a data source relation, which will be planned to a data scan | ||
* operator finally. | ||
* | ||
* @param output The output of this relation. | ||
* @param source The instance of a data source v2 implementation. | ||
* @param options The options specified for this scan, used to create the `DataSourceReader`. | ||
* @param userSpecifiedSchema The user specified schema, used to create the `DataSourceReader`. | ||
* @param filters The predicates which are pushed and handled by this data source. | ||
* @param existingReader A mutable reader carrying some temporary stats during optimization and | ||
* planning. It's always None before optimization, and does not take part in | ||
* the equality of this plan, which means this plan is still immutable. | ||
*/ | ||
case class DataSourceV2Relation( | ||
fullOutput: Seq[AttributeReference], | ||
reader: DataSourceReader) extends LeafNode with DataSourceReaderHolder { | ||
output: Seq[AttributeReference], | ||
source: DataSourceV2, | ||
options: DataSourceOptions, | ||
userSpecifiedSchema: Option[StructType], | ||
filters: Set[Expression], | ||
existingReader: Option[DataSourceReader]) extends LeafNode with DataSourceV2QueryPlan { | ||
|
||
override def references: AttributeSet = AttributeSet.empty | ||
|
||
override def sourceClass: Class[_ <: DataSourceV2] = source.getClass | ||
|
||
override def canEqual(other: Any): Boolean = other.isInstanceOf[DataSourceV2Relation] | ||
|
||
def reader: DataSourceReader = existingReader.getOrElse { | ||
(source, userSpecifiedSchema) match { | ||
case (ds: ReadSupportWithSchema, Some(schema)) => | ||
ds.createReader(schema, options) | ||
|
||
case (ds: ReadSupport, None) => | ||
ds.createReader(options) | ||
|
||
case (ds: ReadSupport, Some(schema)) => | ||
val reader = ds.createReader(options) | ||
// Sanity check, this should be guaranteed by `DataFrameReader.load` | ||
assert(reader.readSchema() == schema) | ||
reader | ||
|
||
case _ => throw new IllegalStateException() | ||
} | ||
} | ||
|
||
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. Do we need to override a 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. What is the output of this node in 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. What is the behavior we expect when users call Also another potential issue is about storing the statistics in the external catalog? Do we still have the previous issues discussed in #14712? 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. data source v2 doesn't support tables yet, so we don't have this problem now. |
||
override def computeStats(): Statistics = reader match { | ||
case r: SupportsReportStatistics => | ||
Statistics(sizeInBytes = r.getStatistics.sizeInBytes().orElse(conf.defaultSizeInBytes)) | ||
case _ => | ||
Statistics(sizeInBytes = conf.defaultSizeInBytes) | ||
} | ||
|
||
override def simpleString: String = s"Relation $metadataString" | ||
} | ||
|
||
/** | ||
* A specialization of DataSourceV2Relation with the streaming bit set to true. Otherwise identical | ||
* to the non-streaming relation. | ||
*/ | ||
class StreamingDataSourceV2Relation( | ||
fullOutput: Seq[AttributeReference], | ||
reader: DataSourceReader) extends DataSourceV2Relation(fullOutput, reader) { | ||
case class StreamingDataSourceV2Relation( | ||
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. Similar to |
||
output: Seq[AttributeReference], | ||
reader: DataSourceReader) extends LeafNode { | ||
override def isStreaming: Boolean = true | ||
} | ||
|
||
object DataSourceV2Relation { | ||
def apply(reader: DataSourceReader): DataSourceV2Relation = { | ||
new DataSourceV2Relation(reader.readSchema().toAttributes, reader) | ||
def apply( | ||
source: DataSourceV2, | ||
schema: StructType, | ||
options: DataSourceOptions, | ||
userSpecifiedSchema: Option[StructType]): DataSourceV2Relation = { | ||
DataSourceV2Relation( | ||
schema.toAttributes, source, options, userSpecifiedSchema, Set.empty, None) | ||
} | ||
} |
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.
Why this plan does not extend
MultiInstanceRelation
?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.
Could you add a test for self join? Just to ensure it still works.
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.
good catch! Yea this is a bug, but to respect the rule about solving different issues in different PR, I'd like to fix it in a new PR.