66 */
77package org .elasticsearch .xpack .esql .core .expression ;
88
9- import org .elasticsearch .TransportVersions ;
109import org .elasticsearch .common .Strings ;
1110import org .elasticsearch .common .io .stream .NamedWriteableRegistry ;
1211import org .elasticsearch .common .io .stream .StreamInput ;
2221import java .io .IOException ;
2322import java .util .Objects ;
2423
24+ import static org .elasticsearch .TransportVersions .ESQL_FIELD_ATTRIBUTE_DROP_TYPE ;
2525import static org .elasticsearch .xpack .esql .core .util .PlanStreamInput .readCachedStringWithVersionCheck ;
2626import static org .elasticsearch .xpack .esql .core .util .PlanStreamOutput .writeCachedStringWithVersionCheck ;
2727
@@ -65,47 +65,12 @@ public FieldAttribute(
6565 @ Nullable NameId id ,
6666 boolean synthetic
6767 ) {
68- this (source , parentName , name , field .getDataType (), field , nullability , id , synthetic );
69- }
70-
71- /**
72- * Used only for testing. Do not use this otherwise, as an explicitly set type will be ignored the next time this FieldAttribute is
73- * {@link FieldAttribute#clone}d.
74- */
75- FieldAttribute (
76- Source source ,
77- @ Nullable String parentName ,
78- String name ,
79- DataType type ,
80- EsField field ,
81- Nullability nullability ,
82- @ Nullable NameId id ,
83- boolean synthetic
84- ) {
85- super (source , name , type , nullability , id , synthetic );
68+ super (source , name , field .getDataType (), nullability , id , synthetic );
8669 this .parentName = parentName ;
8770 this .field = field ;
8871 }
8972
90- @ Deprecated
91- /**
92- * Old constructor from when this had a qualifier string. Still needed to not break serialization.
93- */
94- private FieldAttribute (
95- Source source ,
96- @ Nullable String parentName ,
97- String name ,
98- DataType type ,
99- EsField field ,
100- @ Nullable String qualifier ,
101- Nullability nullability ,
102- @ Nullable NameId id ,
103- boolean synthetic
104- ) {
105- this (source , parentName , name , type , field , nullability , id , synthetic );
106- }
107-
108- private FieldAttribute (StreamInput in ) throws IOException {
73+ private static FieldAttribute innerReadFrom (StreamInput in ) throws IOException {
10974 /*
11075 * The funny casting dance with `(StreamInput & PlanStreamInput) in` is required
11176 * because we're in esql-core here and the real PlanStreamInput is in
@@ -114,57 +79,44 @@ private FieldAttribute(StreamInput in) throws IOException {
11479 * and NameId. This should become a hard cast when we move everything out
11580 * of esql-core.
11681 */
117- this (
118- Source .readFrom ((StreamInput & PlanStreamInput ) in ),
119- readParentName (in ),
120- readCachedStringWithVersionCheck (in ),
121- DataType .readFrom (in ),
122- EsField .readFrom (in ),
123- in .readOptionalString (),
124- in .readEnum (Nullability .class ),
125- NameId .readFrom ((StreamInput & PlanStreamInput ) in ),
126- in .readBoolean ()
127- );
82+ Source source = Source .readFrom ((StreamInput & PlanStreamInput ) in );
83+ String parentName = ((PlanStreamInput ) in ).readOptionalCachedString ();
84+ String name = readCachedStringWithVersionCheck (in );
85+ if (in .getTransportVersion ().before (ESQL_FIELD_ATTRIBUTE_DROP_TYPE )) {
86+ DataType .readFrom (in );
87+ }
88+ EsField field = EsField .readFrom (in );
89+ if (in .getTransportVersion ().before (ESQL_FIELD_ATTRIBUTE_DROP_TYPE )) {
90+ in .readOptionalString ();
91+ }
92+ Nullability nullability = in .readEnum (Nullability .class );
93+ NameId nameId = NameId .readFrom ((StreamInput & PlanStreamInput ) in );
94+ boolean synthetic = in .readBoolean ();
95+ return new FieldAttribute (source , parentName , name , field , nullability , nameId , synthetic );
12896 }
12997
13098 @ Override
13199 public void writeTo (StreamOutput out ) throws IOException {
132100 if (((PlanStreamOutput ) out ).writeAttributeCacheHeader (this )) {
133101 Source .EMPTY .writeTo (out );
134- writeParentName ( out );
102+ (( PlanStreamOutput ) out ). writeOptionalCachedString ( parentName );
135103 writeCachedStringWithVersionCheck (out , name ());
136- dataType ().writeTo (out );
104+ if (out .getTransportVersion ().before (ESQL_FIELD_ATTRIBUTE_DROP_TYPE )) {
105+ dataType ().writeTo (out );
106+ }
137107 field .writeTo (out );
138- // We used to write the qualifier here. We can still do if needed in the future.
139- out .writeOptionalString (null );
108+ if (out .getTransportVersion ().before (ESQL_FIELD_ATTRIBUTE_DROP_TYPE )) {
109+ // We used to write the qualifier here. We can still do if needed in the future.
110+ out .writeOptionalString (null );
111+ }
140112 out .writeEnum (nullable ());
141113 id ().writeTo (out );
142114 out .writeBoolean (synthetic ());
143115 }
144116 }
145117
146118 public static FieldAttribute readFrom (StreamInput in ) throws IOException {
147- return ((PlanStreamInput ) in ).readAttributeWithCache (FieldAttribute ::new );
148- }
149-
150- private void writeParentName (StreamOutput out ) throws IOException {
151- if (out .getTransportVersion ().onOrAfter (TransportVersions .V_8_17_0 )) {
152- ((PlanStreamOutput ) out ).writeOptionalCachedString (parentName );
153- } else {
154- // Previous versions only used the parent field attribute to retrieve the parent's name, so we can use just any
155- // fake FieldAttribute here as long as the name is correct.
156- FieldAttribute fakeParent = parentName () == null ? null : new FieldAttribute (Source .EMPTY , parentName (), field ());
157- out .writeOptionalWriteable (fakeParent );
158- }
159- }
160-
161- private static String readParentName (StreamInput in ) throws IOException {
162- if (in .getTransportVersion ().onOrAfter (TransportVersions .V_8_17_0 )) {
163- return ((PlanStreamInput ) in ).readOptionalCachedString ();
164- }
165-
166- FieldAttribute parent = in .readOptionalWriteable (FieldAttribute ::readFrom );
167- return parent == null ? null : parent .name ();
119+ return ((PlanStreamInput ) in ).readAttributeWithCache (FieldAttribute ::innerReadFrom );
168120 }
169121
170122 @ Override
0 commit comments