-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-12293][SQL] Support UnsafeRow in LocalTableScan #10283
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ca4326a
Support UnsafeRow in LocalTableScan.
viirya a0a991a
Add copy().
viirya f0e6ac0
Fix several bugs.
viirya ee89482
Merge remote-tracking branch 'upstream/master' into unsaferow-localta…
viirya 97a81c3
Fix remaining failed tests.
viirya 5b2a09c
Merge remote-tracking branch 'upstream/master' into unsaferow-localta…
viirya 97d390f
Ignore the test for Limit node as it accepts UnsafeRow now.
viirya 2500de3
For comment.
viirya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -18,7 +18,8 @@ | |
| package org.apache.spark.sql.catalyst.plans.logical | ||
|
|
||
| import org.apache.spark.sql.Row | ||
| import org.apache.spark.sql.catalyst.expressions.Attribute | ||
| import org.apache.spark.sql.catalyst.encoders.{ExpressionEncoder, RowEncoder} | ||
| import org.apache.spark.sql.catalyst.expressions.{Attribute, BindReferences, UnsafeProjection, UnsafeRow} | ||
| import org.apache.spark.sql.catalyst.{CatalystTypeConverters, InternalRow, analysis} | ||
| import org.apache.spark.sql.types.{StructField, StructType} | ||
|
|
||
|
|
@@ -29,20 +30,27 @@ object LocalRelation { | |
| new LocalRelation(StructType(output1 +: output).toAttributes) | ||
| } | ||
|
|
||
| def fromInternalRows(output: Seq[Attribute], data: Seq[InternalRow]): LocalRelation = { | ||
| val projection = UnsafeProjection.create(output.map(_.dataType).toArray) | ||
| new LocalRelation(output, data.map(projection(_).copy())) | ||
| } | ||
|
|
||
| def fromExternalRows(output: Seq[Attribute], data: Seq[Row]): LocalRelation = { | ||
| val schema = StructType.fromAttributes(output) | ||
| val converter = CatalystTypeConverters.createToCatalystConverter(schema) | ||
| LocalRelation(output, data.map(converter(_).asInstanceOf[InternalRow])) | ||
| val encoder = RowEncoder(schema) | ||
| LocalRelation(output, data.map(encoder.toRow(_).copy().asInstanceOf[UnsafeRow])) | ||
| } | ||
|
|
||
| def fromProduct(output: Seq[Attribute], data: Seq[Product]): LocalRelation = { | ||
| def fromProduct[T <: Product : ExpressionEncoder]( | ||
| output: Seq[Attribute], | ||
| data: Seq[T]): LocalRelation = { | ||
| val encoder = implicitly[ExpressionEncoder[T]] | ||
|
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. we can use |
||
| val schema = StructType.fromAttributes(output) | ||
| val converter = CatalystTypeConverters.createToCatalystConverter(schema) | ||
| LocalRelation(output, data.map(converter(_).asInstanceOf[InternalRow])) | ||
| new LocalRelation(output, data.map(encoder.toRow(_).copy().asInstanceOf[UnsafeRow])) | ||
| } | ||
| } | ||
|
|
||
| case class LocalRelation(output: Seq[Attribute], data: Seq[InternalRow] = Nil) | ||
| case class LocalRelation(output: Seq[Attribute], data: Seq[UnsafeRow] = Nil) | ||
| extends LeafNode with analysis.MultiInstanceRelation { | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ import org.apache.spark.scheduler.{SparkListener, SparkListenerApplicationEnd} | |
| import org.apache.spark.sql.SQLConf.SQLConfEntry | ||
| import org.apache.spark.sql.catalyst.analysis._ | ||
| import org.apache.spark.sql.catalyst.encoders.encoderFor | ||
| import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder | ||
| import org.apache.spark.sql.catalyst.errors.DialectException | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.optimizer.{DefaultOptimizer, Optimizer} | ||
|
|
@@ -426,6 +427,7 @@ class SQLContext private[sql]( | |
| */ | ||
| @Experimental | ||
| def createDataFrame[A <: Product : TypeTag](data: Seq[A]): DataFrame = { | ||
| implicit def encoder[T : TypeTag]: ExpressionEncoder[T] = ExpressionEncoder() | ||
| SQLContext.setActive(self) | ||
| val schema = ScalaReflection.schemaFor[A].dataType.asInstanceOf[StructType] | ||
| val attributeSeq = schema.toAttributes | ||
|
|
@@ -501,7 +503,7 @@ class SQLContext private[sql]( | |
| def createDataset[T : Encoder](data: Seq[T]): Dataset[T] = { | ||
| val enc = encoderFor[T] | ||
| val attributes = enc.schema.toAttributes | ||
| val encoded = data.map(d => enc.toRow(d).copy()) | ||
| val encoded = data.map(d => enc.toRow(d).copy().asInstanceOf[UnsafeRow]) | ||
| val plan = new LocalRelation(attributes, encoded) | ||
|
|
||
| new Dataset[T](this, plan) | ||
|
|
@@ -604,7 +606,9 @@ class SQLContext private[sql]( | |
| val className = beanClass.getName | ||
| val beanInfo = Introspector.getBeanInfo(beanClass) | ||
| val rows = SQLContext.beansToRows(data.asScala.iterator, beanInfo, attrSeq) | ||
| DataFrame(self, LocalRelation(attrSeq, rows.toSeq)) | ||
| val projection = UnsafeProjection.create(attrSeq) | ||
| DataFrame(self, | ||
| LocalRelation(attrSeq, rows.toSeq.map(projection(_).copy().asInstanceOf[UnsafeRow]))) | ||
|
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.
|
||
| } | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The return type of
UnsafeProjection.applyisUnsafeRowalready, looks like we don't need theasInstanceOfhere?