3030import org .apache .iotdb .db .queryengine .plan .statement .crud .InsertTabletStatement ;
3131import org .apache .iotdb .service .rpc .thrift .TPipeTransferReq ;
3232
33+ import org .apache .tsfile .enums .TSDataType ;
3334import org .apache .tsfile .utils .PublicBAOS ;
3435import org .apache .tsfile .utils .ReadWriteIOUtils ;
3536import org .apache .tsfile .write .record .Tablet ;
@@ -164,6 +165,9 @@ private void deserializeTPipeTransferRawReq(
164165 final InsertTabletStatement insertTabletStatement =
165166 TabletStatementConverter .deserializeStatementFromTabletFormat (
166167 buffer , false , tabletStringInternPool );
168+ // Legacy tablets do not serialize column categories. Since hasSchema=1 can be
169+ // misread as FIELD, the current reader may return a corrupt statement instead of failing.
170+ ensureStatementDeserializedFromCurrentTabletFormat (insertTabletStatement );
167171 isAligned = insertTabletStatement .isAligned ();
168172 statement = insertTabletStatement ;
169173 return ;
@@ -188,6 +192,44 @@ private void deserializeTPipeTransferRawReq(
188192 isAligned = ReadWriteIOUtils .readBool (buffer );
189193 }
190194
195+ private static void ensureStatementDeserializedFromCurrentTabletFormat (
196+ final InsertTabletStatement statement ) {
197+ final String [] measurements = statement .getMeasurements ();
198+ final TSDataType [] dataTypes = statement .getDataTypes ();
199+
200+ if (Objects .isNull (measurements )
201+ || Objects .isNull (dataTypes )
202+ || measurements .length != dataTypes .length ) {
203+ throw new IllegalArgumentException (
204+ "Incomplete schema in current tablet format deserialization." );
205+ }
206+
207+ final Object [] columns = statement .getColumns ();
208+ if (Objects .nonNull (columns ) && columns .length != measurements .length ) {
209+ throw new IllegalArgumentException (
210+ "Column count is inconsistent with schema count in current tablet format deserialization." );
211+ }
212+
213+ for (int i = 0 ; i < measurements .length ; ++i ) {
214+ if (Objects .isNull (measurements [i ]) || Objects .isNull (dataTypes [i ])) {
215+ throw new IllegalArgumentException (
216+ "Incomplete measurement schema in current tablet format deserialization." );
217+ }
218+ if (statement .getRowCount () > 0 && (Objects .isNull (columns ) || Objects .isNull (columns [i ]))) {
219+ throw new IllegalArgumentException (
220+ "Incomplete column values in current tablet format deserialization." );
221+ }
222+ }
223+
224+ final long [] times = statement .getTimes ();
225+ if (statement .getRowCount () > 0
226+ && measurements .length > 0
227+ && (Objects .isNull (times ) || times .length < statement .getRowCount ())) {
228+ throw new IllegalArgumentException (
229+ "Incomplete timestamps in current tablet format deserialization." );
230+ }
231+ }
232+
191233 /////////////////////////////// Air Gap ///////////////////////////////
192234
193235 /**
0 commit comments