@@ -142,11 +142,19 @@ object RewriteDistinctAggregates extends Rule[LogicalPlan] {
142
142
143
143
// Setup unique distinct aggregate children.
144
144
val distinctAggChildren = distinctAggGroups.keySet.flatten.toSeq.distinct
145
- val distinctAggChildAttrMap = distinctAggChildren.map(expressionAttributePair)
146
- val distinctAggChildAttrs = distinctAggChildAttrMap.map(_._2)
145
+ val distinctAggChildFoldable = distinctAggChildren.filter(_.foldable)
146
+ // 1.only unfoldable child should be expand
147
+ // 2.if foldable child mapped to AttributeRefference using expressionAttributePair,
148
+ // the udaf function(such as ApproximatePercentile)
149
+ // which has a foldable TypeCheck will failed,because AttributeRefference is unfoldable
150
+ val distinctAggChildUnFoldableAttrMap = distinctAggChildren
151
+ .filter(! _.foldable).map(expressionAttributePair)
152
+
153
+ val distinctAggChildrenUnFoldableAttrs = distinctAggChildUnFoldableAttrMap.map(_._2)
147
154
148
155
// Setup expand & aggregate operators for distinct aggregate expressions.
149
- val distinctAggChildAttrLookup = distinctAggChildAttrMap.toMap
156
+ val distinctAggChildAttrLookup = (distinctAggChildUnFoldableAttrMap
157
+ ++ distinctAggChildFoldable.map(c => c -> c)).toMap
150
158
val distinctAggOperatorMap = distinctAggGroups.toSeq.zipWithIndex.map {
151
159
case ((group, expressions), i) =>
152
160
val id = Literal (i + 1 )
@@ -172,11 +180,15 @@ object RewriteDistinctAggregates extends Rule[LogicalPlan] {
172
180
// Setup expand for the 'regular' aggregate expressions.
173
181
val regularAggExprs = aggExpressions.filter(! _.isDistinct)
174
182
val regularAggChildren = regularAggExprs.flatMap(_.aggregateFunction.children).distinct
175
- val regularAggChildAttrMap = regularAggChildren.map(expressionAttributePair)
176
-
183
+ val regularAggChildFoldable = regularAggChildren.filter(_.foldable)
184
+ val regularAggChildUnFoldable = regularAggChildren.filter(! _.foldable)
185
+ val regularAggChildUnFoldableAttrMap = regularAggChildUnFoldable
186
+ .map(expressionAttributePair)
187
+ val regularAggChildUnFoldableAttrs = regularAggChildUnFoldableAttrMap.map(_._2)
177
188
// Setup aggregates for 'regular' aggregate expressions.
178
189
val regularGroupId = Literal (0 )
179
- val regularAggChildAttrLookup = regularAggChildAttrMap.toMap
190
+ val regularAggChildAttrLookup = (regularAggChildUnFoldableAttrMap
191
+ ++ regularAggChildFoldable.map(c => c -> c)).toMap
180
192
val regularAggOperatorMap = regularAggExprs.map { e =>
181
193
// Perform the actual aggregation in the initial aggregate.
182
194
val af = patchAggregateFunctionChildren(e.aggregateFunction)(regularAggChildAttrLookup)
@@ -207,13 +219,13 @@ object RewriteDistinctAggregates extends Rule[LogicalPlan] {
207
219
Seq (a.groupingExpressions ++
208
220
distinctAggChildren.map(nullify) ++
209
221
Seq (regularGroupId) ++
210
- regularAggChildren )
222
+ regularAggChildUnFoldable )
211
223
} else {
212
224
Seq .empty[Seq [Expression ]]
213
225
}
214
226
215
227
// Construct the distinct aggregate input projections.
216
- val regularAggNulls = regularAggChildren .map(nullify)
228
+ val regularAggNulls = regularAggChildUnFoldable .map(nullify)
217
229
val distinctAggProjections = distinctAggOperatorMap.map {
218
230
case (projection, _) =>
219
231
a.groupingExpressions ++
@@ -224,12 +236,13 @@ object RewriteDistinctAggregates extends Rule[LogicalPlan] {
224
236
// Construct the expand operator.
225
237
val expand = Expand (
226
238
regularAggProjection ++ distinctAggProjections,
227
- groupByAttrs ++ distinctAggChildAttrs ++ Seq (gid) ++ regularAggChildAttrMap.map(_._2),
239
+ groupByAttrs ++ distinctAggChildrenUnFoldableAttrs ++ Seq (gid)
240
+ ++ regularAggChildUnFoldableAttrs,
228
241
a.child)
229
242
230
243
// Construct the first aggregate operator. This de-duplicates the all the children of
231
244
// distinct operators, and applies the regular aggregate operators.
232
- val firstAggregateGroupBy = groupByAttrs ++ distinctAggChildAttrs :+ gid
245
+ val firstAggregateGroupBy = groupByAttrs ++ distinctAggChildrenUnFoldableAttrs :+ gid
233
246
val firstAggregate = Aggregate (
234
247
firstAggregateGroupBy,
235
248
firstAggregateGroupBy ++ regularAggOperatorMap.map(_._2),
0 commit comments