16
16
import org .elasticsearch .compute .operator .BreakingBytesRefBuilder ;
17
17
import org .elasticsearch .compute .operator .EvalOperator .ExpressionEvaluator ;
18
18
import org .elasticsearch .xpack .esql .core .expression .Expression ;
19
+ import org .elasticsearch .xpack .esql .core .expression .Literal ;
19
20
import org .elasticsearch .xpack .esql .core .tree .NodeInfo ;
20
21
import org .elasticsearch .xpack .esql .core .tree .Source ;
21
22
import org .elasticsearch .xpack .esql .core .type .DataType ;
32
33
import static org .elasticsearch .common .unit .ByteSizeUnit .MB ;
33
34
import static org .elasticsearch .xpack .esql .core .expression .TypeResolutions .ParamOrdinal .DEFAULT ;
34
35
import static org .elasticsearch .xpack .esql .core .expression .TypeResolutions .isType ;
36
+ import static org .elasticsearch .xpack .esql .core .type .DataType .KEYWORD ;
35
37
36
38
public class Space extends UnaryScalarFunction {
37
39
public static final NamedWriteableRegistry .Entry ENTRY = new NamedWriteableRegistry .Entry (Expression .class , "Space" , Space ::new );
@@ -70,7 +72,7 @@ public String getWriteableName() {
70
72
71
73
@ Override
72
74
public DataType dataType () {
73
- return DataType . KEYWORD ;
75
+ return KEYWORD ;
74
76
}
75
77
76
78
@ Override
@@ -87,15 +89,15 @@ public boolean foldable() {
87
89
return number .foldable ();
88
90
}
89
91
90
- @ Evaluator (extraName = "Constant" )
91
- static BytesRef processConstant (@ Fixed (includeInToString = false , build = true ) BreakingBytesRefBuilder scratch , @ Fixed int number ) {
92
- return processInner (scratch , number );
93
- }
94
-
95
92
@ Evaluator (warnExceptions = { IllegalArgumentException .class })
96
93
static BytesRef process (@ Fixed (includeInToString = false , build = true ) BreakingBytesRefBuilder scratch , int number ) {
97
94
checkNumber (number );
98
- return processInner (scratch , number );
95
+ scratch .grow (number );
96
+ scratch .clear ();
97
+ for (int i = 0 ; i < number ; ++i ) {
98
+ scratch .append ((byte ) ' ' );
99
+ }
100
+ return scratch .bytesRefView ();
99
101
}
100
102
101
103
static void checkNumber (int number ) {
@@ -107,15 +109,6 @@ static void checkNumber(int number) {
107
109
}
108
110
}
109
111
110
- static BytesRef processInner (BreakingBytesRefBuilder scratch , int number ) {
111
- scratch .grow (number );
112
- scratch .clear ();
113
- for (int i = 0 ; i < number ; ++i ) {
114
- scratch .append ((byte ) ' ' );
115
- }
116
- return scratch .bytesRefView ();
117
- }
118
-
119
112
@ Override
120
113
public Expression replaceChildren (List <Expression > newChildren ) {
121
114
return new Space (source (), newChildren .get (0 ));
@@ -131,7 +124,7 @@ public ExpressionEvaluator.Factory toEvaluator(Function<Expression, ExpressionEv
131
124
if (number .foldable ()) {
132
125
int num = (int ) number .fold ();
133
126
checkNumber (num );
134
- return new SpaceConstantEvaluator . Factory (source (), context -> new BreakingBytesRefBuilder ( context . breaker ( ), "space" ), num );
127
+ return toEvaluator . apply ( new Literal (source (), " " . repeat ( num ), KEYWORD ) );
135
128
}
136
129
137
130
ExpressionEvaluator .Factory numberExpr = toEvaluator .apply (number );
0 commit comments