Skip to content

Esql clean up constructors in field attribute #127920

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 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.elasticsearch.xpack.esql.core.expression.FieldAttribute;
import org.elasticsearch.xpack.esql.core.expression.FoldContext;
import org.elasticsearch.xpack.esql.core.expression.Literal;
import org.elasticsearch.xpack.esql.core.expression.Nullability;
import org.elasticsearch.xpack.esql.core.expression.predicate.regex.RLikePattern;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.core.type.DataType;
Expand Down Expand Up @@ -204,8 +205,12 @@ private static EvalOperator.ExpressionEvaluator evaluator(String operation) {
case "date_trunc" -> {
FieldAttribute timestamp = new FieldAttribute(
Source.EMPTY,
null,
"timestamp",
new EsField("timestamp", DataType.DATETIME, Map.of(), true)
new EsField("timestamp", DataType.DATETIME, Map.of(), true),
Nullability.TRUE,
null,
false
);
yield EvalMapper.toEvaluator(
FOLD_CONTEXT,
Expand Down Expand Up @@ -255,19 +260,51 @@ private static EvalOperator.ExpressionEvaluator evaluator(String operation) {
}

private static FieldAttribute longField() {
return new FieldAttribute(Source.EMPTY, "long", new EsField("long", DataType.LONG, Map.of(), true));
return new FieldAttribute(
Source.EMPTY,
null,
"long",
new EsField("long", DataType.LONG, Map.of(), true),
Nullability.TRUE,
null,
false
);
}

private static FieldAttribute doubleField() {
return new FieldAttribute(Source.EMPTY, "double", new EsField("double", DataType.DOUBLE, Map.of(), true));
return new FieldAttribute(
Source.EMPTY,
null,
"double",
new EsField("double", DataType.DOUBLE, Map.of(), true),
Nullability.TRUE,
null,
false
);
}

private static FieldAttribute intField() {
return new FieldAttribute(Source.EMPTY, "int", new EsField("int", DataType.INTEGER, Map.of(), true));
return new FieldAttribute(
Source.EMPTY,
null,
"int",
new EsField("int", DataType.INTEGER, Map.of(), true),
Nullability.TRUE,
null,
false
);
}

private static FieldAttribute keywordField() {
return new FieldAttribute(Source.EMPTY, "keyword", new EsField("keyword", DataType.KEYWORD, Map.of(), true));
return new FieldAttribute(
Source.EMPTY,
null,
"keyword",
new EsField("keyword", DataType.KEYWORD, Map.of(), true),
Nullability.TRUE,
null,
false
);
}

private static Configuration configuration() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,6 @@ public class FieldAttribute extends TypedAttribute {
private final String parentName;
private final EsField field;

public FieldAttribute(Source source, String name, EsField field) {
this(source, null, name, field);
}

public FieldAttribute(Source source, @Nullable String parentName, String name, EsField field) {
this(source, parentName, name, field, Nullability.TRUE, null, false);
}

public FieldAttribute(Source source, @Nullable String parentName, String name, EsField field, boolean synthetic) {
this(source, parentName, name, field, Nullability.TRUE, null, synthetic);
}

public FieldAttribute(
Source source,
@Nullable String parentName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.elasticsearch.xpack.esql.core.expression.FieldAttribute;
import org.elasticsearch.xpack.esql.core.expression.Literal;
import org.elasticsearch.xpack.esql.core.expression.Nullability;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.core.type.EsField;
Expand Down Expand Up @@ -46,15 +47,15 @@ public static FieldAttribute fieldAttribute() {
}

public static FieldAttribute fieldAttribute(String name, DataType type) {
return new FieldAttribute(EMPTY, name, new EsField(name, type, emptyMap(), randomBoolean()));
return new FieldAttribute(EMPTY, null, name, new EsField(name, type, emptyMap(), randomBoolean()), Nullability.TRUE, null, false);
}

public static FieldAttribute getFieldAttribute(String name) {
return getFieldAttribute(name, INTEGER);
}

public static FieldAttribute getFieldAttribute(String name, DataType dataType) {
return new FieldAttribute(EMPTY, name, new EsField(name + "f", dataType, emptyMap(), true));
return new FieldAttribute(EMPTY, null, name, new EsField(name + "f", dataType, emptyMap(), true), Nullability.TRUE, null, false);
}

/** Similar to {@link String#strip()}, but removes the WS throughout the entire string. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.elasticsearch.xpack.esql.core.expression.FieldAttribute;
import org.elasticsearch.xpack.esql.core.expression.FoldContext;
import org.elasticsearch.xpack.esql.core.expression.Literal;
import org.elasticsearch.xpack.esql.core.expression.Nullability;
import org.elasticsearch.xpack.esql.core.expression.ReferenceAttribute;
import org.elasticsearch.xpack.esql.core.expression.predicate.regex.RLikePattern;
import org.elasticsearch.xpack.esql.core.expression.predicate.regex.WildcardPattern;
Expand Down Expand Up @@ -202,15 +203,15 @@ public static FieldAttribute getFieldAttribute(String name) {
}

public static FieldAttribute getFieldAttribute(String name, DataType dataType) {
return new FieldAttribute(EMPTY, name, new EsField(name + "f", dataType, emptyMap(), true));
return new FieldAttribute(EMPTY, null, name, new EsField(name + "f", dataType, emptyMap(), true), Nullability.TRUE, null, false);
}

public static FieldAttribute fieldAttribute() {
return fieldAttribute(randomAlphaOfLength(10), randomFrom(DataType.types()));
}

public static FieldAttribute fieldAttribute(String name, DataType type) {
return new FieldAttribute(EMPTY, name, new EsField(name, type, emptyMap(), randomBoolean()));
return new FieldAttribute(EMPTY, null, name, new EsField(name, type, emptyMap(), randomBoolean()), Nullability.TRUE, null, false);
}

public static Literal of(Object value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ private static void mappingAsAttributes(List<Attribute> list, Source source, Str

FieldAttribute attribute = t instanceof UnsupportedEsField uef
? new UnsupportedAttribute(source, name, uef)
: new FieldAttribute(source, parentName, name, t);
: new FieldAttribute(source, parentName, name, t, Nullability.TRUE, null, false);
// primitive branch
if (DataType.isPrimitive(type)) {
list.add(attribute);
Expand Down Expand Up @@ -458,7 +458,7 @@ private LocalRelation tableMapAsRelation(Source source, Map<String, Column> mapT
Column column = entry.getValue();
// create a fake ES field - alternative is to use a ReferenceAttribute
EsField field = new EsField(name, column.type(), Map.of(), false, false);
attributes.add(new FieldAttribute(source, null, name, field));
attributes.add(new FieldAttribute(source, null, name, field, Nullability.TRUE, null, false));
// prepare the block for the supplier
blocks[i++] = column.values();
}
Expand Down Expand Up @@ -881,11 +881,19 @@ private static Attribute invalidInsistAttribute(FieldAttribute fa) {
fa.dataType().typeName()
)
);
return new FieldAttribute(fa.source(), name, field);
return new FieldAttribute(fa.source(), null, name, field, Nullability.TRUE, null, false);
}

private static FieldAttribute insistKeyword(Attribute attribute) {
return new FieldAttribute(attribute.source(), attribute.name(), new PotentiallyUnmappedKeywordEsField(attribute.name()));
return new FieldAttribute(
attribute.source(),
null,
attribute.name(),
new PotentiallyUnmappedKeywordEsField(attribute.name()),
Nullability.TRUE,
null,
false
);
}

private LogicalPlan resolveDedup(Dedup dedup, List<Attribute> childrenOutput) {
Expand Down Expand Up @@ -1687,7 +1695,15 @@ private Expression createIfDoesNotAlreadyExist(
// NOTE: The name has to start with $$ to not break bwc with 8.15 - in that version, this is how we had to mark this as
// synthetic to work around a bug.
String unionTypedFieldName = Attribute.rawTemporaryName(fa.name(), "converted_to", resolvedField.getDataType().typeName());
FieldAttribute unionFieldAttribute = new FieldAttribute(fa.source(), fa.parentName(), unionTypedFieldName, resolvedField, true);
FieldAttribute unionFieldAttribute = new FieldAttribute(
fa.source(),
fa.parentName(),
unionTypedFieldName,
resolvedField,
Nullability.TRUE,
null,
true
);
int existingIndex = unionFieldAttributes.indexOf(unionFieldAttribute);
if (existingIndex >= 0) {
// Do not generate multiple name/type combinations with different IDs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.elasticsearch.xpack.esql.core.expression.Attribute;
import org.elasticsearch.xpack.esql.core.expression.FieldAttribute;
import org.elasticsearch.xpack.esql.core.expression.MetadataAttribute;
import org.elasticsearch.xpack.esql.core.expression.Nullability;
import org.elasticsearch.xpack.esql.optimizer.PhysicalOptimizerRules;
import org.elasticsearch.xpack.esql.plan.physical.EsQueryExec;
import org.elasticsearch.xpack.esql.plan.physical.EsSourceExec;
Expand All @@ -29,7 +30,15 @@ public ReplaceSourceAttributes() {

@Override
protected PhysicalPlan rule(EsSourceExec plan) {
var docId = new FieldAttribute(plan.source(), EsQueryExec.DOC_ID_FIELD.getName(), EsQueryExec.DOC_ID_FIELD);
var docId = new FieldAttribute(
plan.source(),
null,
EsQueryExec.DOC_ID_FIELD.getName(),
EsQueryExec.DOC_ID_FIELD,
Nullability.TRUE,
null,
false
);
final List<Attribute> attributes = new ArrayList<>();
attributes.add(docId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.elasticsearch.index.IndexMode;
import org.elasticsearch.xpack.esql.core.expression.Attribute;
import org.elasticsearch.xpack.esql.core.expression.FieldAttribute;
import org.elasticsearch.xpack.esql.core.expression.Nullability;
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
import org.elasticsearch.xpack.esql.core.tree.NodeUtils;
import org.elasticsearch.xpack.esql.core.tree.Source;
Expand Down Expand Up @@ -139,7 +140,10 @@ private static List<Attribute> flatten(Source source, Map<String, EsField> mappi
source,
parent != null ? parent.name() : null,
parent != null ? parent.name() + "." + name : name,
t
t,
Nullability.TRUE,
null,
false
);
list.add(f);
// object or nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.elasticsearch.xpack.esql.core.expression.MapExpression;
import org.elasticsearch.xpack.esql.core.expression.MetadataAttribute;
import org.elasticsearch.xpack.esql.core.expression.NamedExpression;
import org.elasticsearch.xpack.esql.core.expression.Nullability;
import org.elasticsearch.xpack.esql.core.expression.ReferenceAttribute;
import org.elasticsearch.xpack.esql.core.expression.UnresolvedAttribute;
import org.elasticsearch.xpack.esql.core.tree.Source;
Expand Down Expand Up @@ -184,7 +185,10 @@ public void testAttributeResolution() {
var limit = as(plan, Limit.class);
var eval = as(limit.child(), Eval.class);
assertEquals(1, eval.fields().size());
assertEquals(new Alias(EMPTY, "e", new FieldAttribute(EMPTY, "emp_no", idx.mapping().get("emp_no"))), eval.fields().get(0));
assertEquals(
new Alias(EMPTY, "e", new FieldAttribute(EMPTY, null, "emp_no", idx.mapping().get("emp_no"), Nullability.TRUE, null, false)),
eval.fields().get(0)
);

assertEquals(2, eval.output().size());
Attribute empNo = eval.output().get(0);
Expand Down Expand Up @@ -2911,7 +2915,15 @@ public void testResolveInsist_fieldDoesNotExist_createsUnmappedField() {
var limit = as(plan, Limit.class);
var insist = as(limit.child(), Insist.class);
assertThat(insist.output(), hasSize(analyze("FROM test").output().size() + 1));
var expectedAttribute = new FieldAttribute(Source.EMPTY, "foo", new PotentiallyUnmappedKeywordEsField("foo"));
var expectedAttribute = new FieldAttribute(
Source.EMPTY,
null,
"foo",
new PotentiallyUnmappedKeywordEsField("foo"),
Nullability.TRUE,
null,
false
);
assertThat(insist.insistedAttributes(), is(List.of(expectedAttribute)));
assertThat(insist.output().getLast(), is(expectedAttribute));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.elasticsearch.xpack.esql.core.expression.FieldAttribute;
import org.elasticsearch.xpack.esql.core.expression.FoldContext;
import org.elasticsearch.xpack.esql.core.expression.Literal;
import org.elasticsearch.xpack.esql.core.expression.Nullability;
import org.elasticsearch.xpack.esql.core.expression.TypeResolutions;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.core.type.DataType;
Expand Down Expand Up @@ -446,7 +447,15 @@ public static Stream<DataType> validFunctionParameters() {
* Build an {@link Attribute} that loads a field.
*/
public static FieldAttribute field(String name, DataType type) {
return new FieldAttribute(Source.synthetic(name), name, new EsField(name, type, Map.of(), true));
return new FieldAttribute(
Source.synthetic(name),
null,
name,
new EsField(name, type, Map.of(), true),
Nullability.TRUE,
null,
false
);
}

/**
Expand All @@ -455,7 +464,7 @@ public static FieldAttribute field(String name, DataType type) {
public static Expression deepCopyOfField(String name, DataType type) {
return new DeepCopy(
Source.synthetic(name),
new FieldAttribute(Source.synthetic(name), name, new EsField(name, type, Map.of(), true))
new FieldAttribute(Source.synthetic(name), null, name, new EsField(name, type, Map.of(), true), Nullability.TRUE, null, false)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.esql.core.expression.FieldAttribute;
import org.elasticsearch.xpack.esql.core.expression.Literal;
import org.elasticsearch.xpack.esql.core.expression.Nullability;
import org.elasticsearch.xpack.esql.core.tree.Location;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.core.type.DataType;
Expand Down Expand Up @@ -53,7 +54,15 @@ public void testArithmeticFunctionName() {
}

public void testNameForArithmeticFunctionAppliedOnTableColumn() {
FieldAttribute fa = new FieldAttribute(EMPTY, "myField", new EsField("myESField", DataType.INTEGER, emptyMap(), true));
FieldAttribute fa = new FieldAttribute(
EMPTY,
null,
"myField",
new EsField("myESField", DataType.INTEGER, emptyMap(), true),
Nullability.TRUE,
null,
false
);
String e = "myField + 10";
Add add = new Add(s(e), fa, l(10));
assertEquals(e, add.sourceText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,15 @@ protected Coalesce build(Source source, List<Expression> args) {

public void testCoalesceIsLazy() {
List<Expression> sub = new ArrayList<>(testCase.getDataAsFields());
FieldAttribute evil = new FieldAttribute(Source.EMPTY, "evil", new EsField("evil", sub.get(0).dataType(), Map.of(), true));
FieldAttribute evil = new FieldAttribute(
Source.EMPTY,
null,
"evil",
new EsField("evil", sub.get(0).dataType(), Map.of(), true),
Nullability.TRUE,
null,
false
);
sub.add(evil);
Coalesce exp = build(Source.EMPTY, sub);
Layout.Builder builder = new Layout.Builder();
Expand Down
Loading