@@ -237,27 +237,16 @@ public RelNode visitParse(Parse node, CalcitePlanContext context) {
237
237
String pattern = (String ) node .getPattern ().getValue ();
238
238
List <String > groupCandidates =
239
239
ParseUtils .getNamedGroupCandidates (parseMethod , pattern , arguments );
240
- List <RexNode > overridingFields = new ArrayList <>();
241
240
List <RexNode > newFields =
242
241
groupCandidates .stream ()
243
242
.map (
244
- group -> {
245
- RexNode regexp =
246
- context .rexBuilder .makeCall (
247
- SqlLibraryOperators .REGEXP_EXTRACT ,
248
- sourceField ,
249
- context .rexBuilder .makeLiteral (pattern ));
250
- if (originalFieldNames .contains (group )) {
251
- overridingFields .add (context .relBuilder .field (group ));
252
- }
253
- return context .relBuilder .alias (regexp , group );
254
- })
243
+ group ->
244
+ context .rexBuilder .makeCall (
245
+ SqlLibraryOperators .REGEXP_EXTRACT ,
246
+ sourceField ,
247
+ context .rexBuilder .makeLiteral (pattern )))
255
248
.toList ();
256
- context .relBuilder .projectPlus (newFields );
257
-
258
- if (!overridingFields .isEmpty ()) {
259
- renameForOverriding (overridingFields , groupCandidates , context );
260
- }
249
+ projectPlusOverriding (newFields , groupCandidates , context );
261
250
return context .relBuilder .peek ();
262
251
}
263
252
@@ -288,33 +277,34 @@ public RelNode visitEval(Eval node, CalcitePlanContext context) {
288
277
// Overriding the existing field if the alias has the same name with original field.
289
278
String alias =
290
279
((RexLiteral ) ((RexCall ) eval ).getOperands ().get (1 )).getValueAs (String .class );
291
- if (originalFieldNames .contains (alias )) {
292
- RexNode toOverride = context .relBuilder .field (alias );
293
- context .relBuilder .projectPlus (eval );
294
- renameForOverriding (List .of (toOverride ), List .of (alias ), context );
295
- } else {
296
- context .relBuilder .projectPlus (eval );
297
- }
280
+ projectPlusOverriding (List .of (eval ), List .of (alias ), context );
298
281
}
299
282
});
300
283
return context .relBuilder .peek ();
301
284
}
302
285
303
- private void renameForOverriding (
304
- List <RexNode > toOverrideList ,
305
- List <String > newNames ,
306
- CalcitePlanContext context ) {
307
- assert toOverrideList .size () == newNames .size () : "Overriding fields are not matched" ;
308
- // 1. drop the overriding field list, it's duplicated now. For example "age, country"
309
- context .relBuilder .projectExcept (toOverrideList );
310
- // 2. get current fields list, the "age0, country0" should include in it.
286
+ private void projectPlusOverriding (
287
+ List <RexNode > newFields , List <String > newNames , CalcitePlanContext context ) {
288
+ List <String > originalFieldNames = context .relBuilder .peek ().getRowType ().getFieldNames ();
289
+ List <RexNode > toOverrideList =
290
+ originalFieldNames .stream ()
291
+ .filter (newNames ::contains )
292
+ .map (a -> (RexNode ) context .relBuilder .field (a ))
293
+ .toList ();
294
+ // 1. add the new fields, For example "age0, country0"
295
+ context .relBuilder .projectPlus (newFields );
296
+ // 2. drop the overriding field list, it's duplicated now. For example "age, country"
297
+ if (!toOverrideList .isEmpty ()) {
298
+ context .relBuilder .projectExcept (toOverrideList );
299
+ }
300
+ // 3. get current fields list, the "age0, country0" should include in it.
311
301
List <String > currentFields = context .relBuilder .peek ().getRowType ().getFieldNames ();
312
302
int length = currentFields .size ();
313
- // 3 . add new names "age, country" to the end of rename list.
303
+ // 4 . add new names "age, country" to the end of rename list.
314
304
List <String > expectedRenameFields =
315
305
new ArrayList <>(currentFields .subList (0 , length - newNames .size ()));
316
306
expectedRenameFields .addAll (newNames );
317
- // 4 . rename
307
+ // 5 . rename
318
308
context .relBuilder .rename (expectedRenameFields );
319
309
}
320
310
0 commit comments