@@ -27,16 +27,14 @@ import org.apache.spark.sql.auron.NativeRDD
2727import org .apache .spark .sql .auron .NativeSupports
2828import org .apache .spark .sql .auron .Shims
2929import org .apache .spark .sql .catalyst .InternalRow
30- import org .apache .spark .sql .catalyst .expressions .Ascending
31- import org .apache .spark .sql .catalyst .expressions .Attribute
32- import org .apache .spark .sql .catalyst .expressions .NullsFirst
33- import org .apache .spark .sql .catalyst .expressions .SortOrder
30+ import org .apache .spark .sql .catalyst .expressions .{Ascending , Attribute , NamedExpression , NullsFirst , SortOrder }
3431import org .apache .spark .sql .catalyst .expressions .codegen .LazilyGeneratedOrdering
3532import org .apache .spark .sql .catalyst .plans .physical .Partitioning
3633import org .apache .spark .sql .catalyst .plans .physical .SinglePartition
3734import org .apache .spark .sql .catalyst .plans .physical .UnknownPartitioning
3835import org .apache .spark .sql .execution .SparkPlan
3936import org .apache .spark .sql .execution .UnaryExecNode
37+ import org .apache .spark .sql .execution .auron .plan .NativeProjectBase .getNativeProjectBuilder
4038import org .apache .spark .sql .execution .metric .SQLMetric
4139
4240import org .apache .auron .metric .SparkMetricNode
@@ -46,9 +44,10 @@ import org.apache.auron.protobuf.PhysicalPlanNode
4644import org .apache .auron .protobuf .PhysicalSortExprNode
4745import org .apache .auron .protobuf .SortExecNode
4846
49- abstract class NativeTakeOrderedBase (
47+ abstract class NativeTakeOrderedAndProjectBase (
5048 limit : Long ,
5149 sortOrder : Seq [SortOrder ],
50+ projectList : Seq [NamedExpression ],
5251 override val child : SparkPlan )
5352 extends UnaryExecNode
5453 with NativeSupports {
@@ -76,6 +75,8 @@ abstract class NativeTakeOrderedBase(
7675 .build()
7776 }
7877
78+ private def nativeProject = getNativeProjectBuilder(projectList).buildPartial()
79+
7980 override def executeCollect (): Array [InternalRow ] = {
8081 val partial = Shims .get.createNativePartialTakeOrderedExec(limit, sortOrder, child, metrics)
8182 val ord = new LazilyGeneratedOrdering (sortOrder, output)
@@ -114,6 +115,7 @@ abstract class NativeTakeOrderedBase(
114115
115116 // check whether native converting is supported
116117 nativeSortExprs
118+ nativeProject
117119
118120 override def doExecuteNative (): NativeRDD = {
119121 val partial = Shims .get.createNativePartialTakeOrderedExec(limit, sortOrder, child, metrics)
@@ -126,6 +128,7 @@ abstract class NativeTakeOrderedBase(
126128 val shuffled = Shims .get.createNativeShuffleExchangeExec(SinglePartition , partial)
127129 val shuffledRDD = NativeHelper .executeNative(shuffled)
128130 val nativeSortExprs = this .nativeSortExprs
131+ val nativeProject = this .nativeProject
129132
130133 // take top-K from the final partition
131134 new NativeRDD (
@@ -137,19 +140,27 @@ abstract class NativeTakeOrderedBase(
137140 rddShuffleReadFull = false ,
138141 (_, taskContext) => {
139142 val inputPartition = shuffledRDD.partitions(0 )
140- val nativeTakeOrderedExec = SortExecNode
143+ val sortExec = SortExecNode
141144 .newBuilder()
142145 .setInput(shuffledRDD.nativePlan(inputPartition, taskContext))
143146 .addAllExpr(nativeSortExprs.asJava)
144147 .setFetchLimit(FetchLimit .newBuilder().setLimit(limit))
145148 .build()
146- PhysicalPlanNode .newBuilder().setSort(nativeTakeOrderedExec).build()
149+ val nativeTakeOrderedExec = PhysicalPlanNode .newBuilder().setSort(sortExec).build()
150+
151+ if (projectList != child.output) {
152+ val nativeTakeOrderedAndProjectExec =
153+ nativeProject.toBuilder.setInput(nativeTakeOrderedExec).build()
154+ PhysicalPlanNode .newBuilder().setProjection(nativeTakeOrderedAndProjectExec).build()
155+ } else {
156+ nativeTakeOrderedExec
157+ }
147158 },
148- friendlyName = " NativeRDD.FinalTakeOrdered " )
159+ friendlyName = " NativeRDD.FinalTakeOrderedAndProject " )
149160 }
150161}
151162
152- abstract class NativePartialTakeOrderedBase (
163+ abstract class NativePartialTakeOrderedAndProjectBase (
153164 limit : Long ,
154165 sortOrder : Seq [SortOrder ],
155166 override val child : SparkPlan ,
@@ -197,6 +208,6 @@ abstract class NativePartialTakeOrderedBase(
197208 .build()
198209 PhysicalPlanNode .newBuilder().setSort(nativeTakeOrderedExec).build()
199210 },
200- friendlyName = " NativeRDD.PartialTakeOrdered " )
211+ friendlyName = " NativeRDD.PartialTakeOrderedAndProject " )
201212 }
202213}
0 commit comments