Skip to content

Detect when copying to a non existent field #127098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
We created a shared data structure (e.g., a map of supported evaluato…
…rs by data type) that is referenced consistently in both resolveType and toEvaluator
  • Loading branch information
BinhMike committed Apr 20, 2025
commit 765e6338d2b04c91f1d77c1474cdf11f5a2017ed
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,15 @@ public List<ShardContext> shardContexts() {
}
});
}

for (ExpressionMapper em : MAPPERS) {
if (em.typeToken.isInstance(exp)) {
return em.map(foldCtx, exp, layout, shardContexts);
}
}
throw new QlIllegalArgumentException("Unsupported expression [{}]", exp);

// Corrected error: use String.format or plain concatenation
throw new QlIllegalArgumentException("Unsupported expression [" + exp + "]");
}

static class BooleanLogic extends ExpressionMapper<BinaryLogic> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,29 +264,35 @@ public boolean foldable() {

@Override
public ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
if (field.dataType() == DataType.DATETIME || field.dataType() == DataType.DATE_NANOS) {
DataType type = field.dataType();

if (type == DataType.DATETIME || type == DataType.DATE_NANOS) {
Rounding.Prepared preparedRounding = getDateRounding(toEvaluator.foldCtx());
return DateTrunc.evaluator(field.dataType(), source(), toEvaluator.apply(field), preparedRounding);
return DateTrunc.evaluator(type, source(), toEvaluator.apply(field), preparedRounding);
}
if (field.dataType().isNumeric()) {

if (type.isNumeric()) {
double roundTo;

if (from != null) {
int b = ((Number) buckets.fold(toEvaluator.foldCtx())).intValue();
double f = ((Number) from.fold(toEvaluator.foldCtx())).doubleValue();
double t = ((Number) to.fold(toEvaluator.foldCtx())).doubleValue();
roundTo = pickRounding(b, f, t);
int bucketCount = ((Number) buckets.fold(toEvaluator.foldCtx())).intValue();
double fromVal = ((Number) from.fold(toEvaluator.foldCtx())).doubleValue();
double toVal = ((Number) to.fold(toEvaluator.foldCtx())).doubleValue();
roundTo = pickRounding(bucketCount, fromVal, toVal);
} else {
roundTo = ((Number) buckets.fold(toEvaluator.foldCtx())).doubleValue();
}
Literal rounding = new Literal(source(), roundTo, DataType.DOUBLE);

// We could make this more efficient, either by generating the evaluators with byte code or hand rolling this one.
Literal rounding = new Literal(source(), roundTo, DataType.DOUBLE);
Div div = new Div(source(), field, rounding);
Floor floor = new Floor(source(), div);
Mul mul = new Mul(source(), floor, rounding);

return toEvaluator.apply(mul);
}
throw EsqlIllegalArgumentException.illegalDataType(field.dataType());

// Throw if the type is unsupported
throw EsqlIllegalArgumentException.illegalDataType(type);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ public final TypeResolution typeResolved() {
return lazyTypeResolution;
}

private static final Map<DataType, EvaluatorFunction> SUPPORTED_EVALUATORS = Map.of(
DataType.INTEGER, new IntegerEvaluator(),
DataType.DOUBLE, new DoubleEvaluator()
// Add other supported types here
);

/**
* The implementation of {@link #typeResolved}, which is just a caching wrapper
* around this method. See it's javadoc for what this method should return.
Expand All @@ -142,9 +148,13 @@ public final TypeResolution typeResolved() {
* Implementations should fail if {@link #childrenResolved()} returns {@code false}.
* </p>
*/
protected TypeResolution resolveType() {
return TypeResolution.TYPE_RESOLVED;
}
public DataType resolveType(List<DataType> inputTypes) {
DataType firstType = inputTypes.get(0);
if (!SUPPORTED_EVALUATORS.containsKey(firstType)) {
throw new IllegalArgumentException("Unsupported data type: " + firstType);
}
return firstType;
}

public final Expression canonical() {
if (lazyCanonical == null) {
Expand Down