Skip to content

Commit

Permalink
Use in the superclass rather than adding .
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisberkhout committed Aug 29, 2024
1 parent 645730a commit a376466
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public class Space extends UnaryScalarFunction {

static final long MAX_LENGTH = MB.toBytes(1);

private final Expression number;

@FunctionInfo(
returnType = "keyword",
description = "Returns a string of `number` spaces.",
Expand All @@ -52,7 +50,6 @@ public Space(
@Param(name = "number", type = { "integer" }, description = "Number of spaces in result.") Expression number
) {
super(source, number);
this.number = number;
}

private Space(StreamInput in) throws IOException {
Expand All @@ -62,7 +59,7 @@ private Space(StreamInput in) throws IOException {
@Override
public void writeTo(StreamOutput out) throws IOException {
source().writeTo(out);
out.writeNamedWriteable(number);
out.writeNamedWriteable(field);
}

@Override
Expand All @@ -81,12 +78,7 @@ protected TypeResolution resolveType() {
return new TypeResolution("Unresolved children");
}

return isType(number, dt -> dt == DataType.INTEGER, sourceText(), DEFAULT, "integer");
}

@Override
public boolean foldable() {
return number.foldable();
return isType(field, dt -> dt == DataType.INTEGER, sourceText(), DEFAULT, "integer");
}

@Evaluator(warnExceptions = { IllegalArgumentException.class })
Expand Down Expand Up @@ -116,22 +108,19 @@ public Expression replaceChildren(List<Expression> newChildren) {

@Override
protected NodeInfo<? extends Expression> info() {
return NodeInfo.create(this, Space::new, number);
return NodeInfo.create(this, Space::new, field);
}

@Override
public ExpressionEvaluator.Factory toEvaluator(Function<Expression, ExpressionEvaluator.Factory> toEvaluator) {
if (number.foldable()) {
int num = (int) number.fold();
if (field.foldable()) {
int num = (int) field.fold();
checkNumber(num);
return toEvaluator.apply(new Literal(source(), " ".repeat(num), KEYWORD));
}

ExpressionEvaluator.Factory numberExpr = toEvaluator.apply(number);
ExpressionEvaluator.Factory numberExpr = toEvaluator.apply(field);
return new SpaceEvaluator.Factory(source(), context -> new BreakingBytesRefBuilder(context.breaker(), "space"), numberExpr);
}

Expression number() {
return number;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected Space createTestInstance() {
@Override
protected Space mutateInstance(Space instance) throws IOException {
Source source = instance.source();
Expression number = instance.number();
Expression number = instance.field();
number = randomValueOtherThan(number, AbstractExpressionSerializationTests::randomChild);
return new Space(source, number);
}
Expand Down

0 comments on commit a376466

Please sign in to comment.