@@ -308,14 +308,45 @@ public void removeColumn(String measurementId) {
308308
309309 @ Override
310310 public IChunkWriter createIChunkWriter () {
311- return new AlignedChunkWriterImpl (schemaList );
311+ return new AlignedChunkWriterImpl (getActiveSchemaList ());
312+ }
313+
314+ private List <IMeasurementSchema > getActiveSchemaList () {
315+ if (measurementIndexMap .size () == schemaList .size ()) {
316+ return schemaList ;
317+ }
318+ List <IMeasurementSchema > activeSchemaList = new ArrayList <>(measurementIndexMap .size ());
319+ for (int i = 0 ; i < schemaList .size (); i ++) {
320+ IMeasurementSchema schema = schemaList .get (i );
321+ Integer activeColumnIndex = measurementIndexMap .get (schema .getMeasurementId ());
322+ if (activeColumnIndex != null && activeColumnIndex == i ) {
323+ activeSchemaList .add (schema );
324+ }
325+ }
326+ return activeSchemaList ;
327+ }
328+
329+ private List <TSDataType > getDataTypes (List <IMeasurementSchema > schemaList ) {
330+ if (schemaList == this .schemaList ) {
331+ return dataTypes ;
332+ }
333+ List <TSDataType > activeDataTypes = new ArrayList <>(schemaList .size ());
334+ for (IMeasurementSchema schema : schemaList ) {
335+ activeDataTypes .add (schema .getType ());
336+ }
337+ return activeDataTypes ;
312338 }
313339
314340 @ SuppressWarnings ({"squid:S6541" , "squid:S3776" })
315341 public void encodeWorkingAlignedTVList (
316342 BlockingQueue <Object > ioTaskQueue ,
317343 long maxNumberOfPointsInChunk ,
318344 int maxNumberOfPointsInPage ) {
345+ List <IMeasurementSchema > activeSchemaList = getActiveSchemaList ();
346+ if (activeSchemaList .isEmpty ()) {
347+ return ;
348+ }
349+
319350 AlignedTVList alignedWorkingListForFlush = (AlignedTVList ) workingListForFlush ;
320351 BitMap allValueColDeletedMap = alignedWorkingListForFlush .getAllValueColDeletedMap ();
321352
@@ -377,28 +408,35 @@ public void encodeWorkingAlignedTVList(
377408 }
378409
379410 handleEncoding (
380- ioTaskQueue , chunkRange , timeDuplicateInfo , allValueColDeletedMap , maxNumberOfPointsInPage );
411+ ioTaskQueue ,
412+ chunkRange ,
413+ timeDuplicateInfo ,
414+ allValueColDeletedMap ,
415+ maxNumberOfPointsInPage ,
416+ activeSchemaList );
381417 }
382418
383419 private void handleEncoding (
384420 BlockingQueue <Object > ioTaskQueue ,
385421 List <List <Integer >> chunkRange ,
386422 boolean [] timeDuplicateInfo ,
387423 BitMap allValueColDeletedMap ,
388- int maxNumberOfPointsInPage ) {
424+ int maxNumberOfPointsInPage ,
425+ List <IMeasurementSchema > activeSchemaList ) {
389426 AlignedTVList alignedWorkingListForFlush = (AlignedTVList ) workingListForFlush ;
390- List <TSDataType > dataTypes = alignedWorkingListForFlush . getTsDataTypes ( );
391- Pair <Long , Integer >[] lastValidPointIndexForTimeDupCheck = new Pair [dataTypes .size ()];
427+ List <Integer > columnIndexList = buildColumnIndexList ( activeSchemaList );
428+ Pair <Long , Integer >[] lastValidPointIndexForTimeDupCheck = new Pair [activeSchemaList .size ()];
392429 for (List <Integer > pageRange : chunkRange ) {
393- AlignedChunkWriterImpl alignedChunkWriter = new AlignedChunkWriterImpl (schemaList );
430+ AlignedChunkWriterImpl alignedChunkWriter = new AlignedChunkWriterImpl (activeSchemaList );
394431 for (int pageNum = 0 ; pageNum < pageRange .size () / 2 ; pageNum += 1 ) {
395- for (int columnIndex = 0 ; columnIndex < dataTypes .size (); columnIndex ++) {
432+ for (int columnIndex = 0 ; columnIndex < activeSchemaList .size (); columnIndex ++) {
433+ int tvListColumnIndex = columnIndexList .get (columnIndex );
396434 // Pair of Time and Index
397435 if (Objects .nonNull (timeDuplicateInfo )
398436 && lastValidPointIndexForTimeDupCheck [columnIndex ] == null ) {
399437 lastValidPointIndexForTimeDupCheck [columnIndex ] = new Pair <>(Long .MIN_VALUE , null );
400438 }
401- TSDataType tsDataType = dataTypes .get (columnIndex );
439+ TSDataType tsDataType = activeSchemaList .get (columnIndex ). getType ( );
402440 for (int sortedRowIndex = pageRange .get (pageNum * 2 );
403441 sortedRowIndex <= pageRange .get (pageNum * 2 + 1 );
404442 sortedRowIndex ++) {
@@ -411,8 +449,10 @@ private void handleEncoding(
411449 // skip time duplicated rows
412450 long time = alignedWorkingListForFlush .getTime (sortedRowIndex );
413451 if (Objects .nonNull (timeDuplicateInfo )) {
414- if (!alignedWorkingListForFlush .isNullValue (
415- alignedWorkingListForFlush .getValueIndex (sortedRowIndex ), columnIndex )) {
452+ if (tvListColumnIndex >= 0
453+ && !alignedWorkingListForFlush .isNullValue (
454+ alignedWorkingListForFlush .getValueIndex (sortedRowIndex ),
455+ tvListColumnIndex )) {
416456 lastValidPointIndexForTimeDupCheck [columnIndex ].left = time ;
417457 lastValidPointIndexForTimeDupCheck [columnIndex ].right =
418458 alignedWorkingListForFlush .getValueIndex (sortedRowIndex );
@@ -432,21 +472,24 @@ private void handleEncoding(
432472 // write(T:3,V:null)
433473
434474 int originRowIndex ;
435- if (Objects .nonNull (lastValidPointIndexForTimeDupCheck [columnIndex ])
475+ if (tvListColumnIndex >= 0
476+ && Objects .nonNull (lastValidPointIndexForTimeDupCheck [columnIndex ])
436477 && (time == lastValidPointIndexForTimeDupCheck [columnIndex ].left )) {
437478 originRowIndex = lastValidPointIndexForTimeDupCheck [columnIndex ].right ;
438479 } else {
439480 originRowIndex = alignedWorkingListForFlush .getValueIndex (sortedRowIndex );
440481 }
441482
442- boolean isNull = alignedWorkingListForFlush .isNullValue (originRowIndex , columnIndex );
483+ boolean isNull =
484+ tvListColumnIndex < 0
485+ || alignedWorkingListForFlush .isNullValue (originRowIndex , tvListColumnIndex );
443486 switch (tsDataType ) {
444487 case BOOLEAN :
445488 alignedChunkWriter .writeByColumn (
446489 time ,
447490 !isNull
448491 && alignedWorkingListForFlush .getBooleanByValueIndex (
449- originRowIndex , columnIndex ),
492+ originRowIndex , tvListColumnIndex ),
450493 isNull );
451494 break ;
452495 case INT32 :
@@ -456,7 +499,7 @@ private void handleEncoding(
456499 isNull
457500 ? 0
458501 : alignedWorkingListForFlush .getIntByValueIndex (
459- originRowIndex , columnIndex ),
502+ originRowIndex , tvListColumnIndex ),
460503 isNull );
461504 break ;
462505 case INT64 :
@@ -466,7 +509,7 @@ private void handleEncoding(
466509 isNull
467510 ? 0
468511 : alignedWorkingListForFlush .getLongByValueIndex (
469- originRowIndex , columnIndex ),
512+ originRowIndex , tvListColumnIndex ),
470513 isNull );
471514 break ;
472515 case FLOAT :
@@ -475,7 +518,7 @@ private void handleEncoding(
475518 isNull
476519 ? 0
477520 : alignedWorkingListForFlush .getFloatByValueIndex (
478- originRowIndex , columnIndex ),
521+ originRowIndex , tvListColumnIndex ),
479522 isNull );
480523 break ;
481524 case DOUBLE :
@@ -484,7 +527,7 @@ private void handleEncoding(
484527 isNull
485528 ? 0
486529 : alignedWorkingListForFlush .getDoubleByValueIndex (
487- originRowIndex , columnIndex ),
530+ originRowIndex , tvListColumnIndex ),
488531 isNull );
489532 break ;
490533 case TEXT :
@@ -495,7 +538,7 @@ private void handleEncoding(
495538 isNull
496539 ? null
497540 : alignedWorkingListForFlush .getBinaryByValueIndex (
498- originRowIndex , columnIndex ),
541+ originRowIndex , tvListColumnIndex ),
499542 isNull );
500543 break ;
501544 default :
@@ -541,15 +584,23 @@ public void encode(BlockingQueue<Object> ioTaskQueue, BatchEncodeInfo encodeInfo
541584 return ;
542585 }
543586
544- AlignedChunkWriterImpl alignedChunkWriter = new AlignedChunkWriterImpl (schemaList );
587+ List <IMeasurementSchema > activeSchemaList = getActiveSchemaList ();
588+ if (activeSchemaList .isEmpty ()) {
589+ return ;
590+ }
591+
592+ AlignedChunkWriterImpl alignedChunkWriter = new AlignedChunkWriterImpl (activeSchemaList );
545593
546594 // create MergeSortAlignedTVListIterator.
547595 List <AlignedTVList > alignedTvLists = new ArrayList <>(sortedList );
548596 alignedTvLists .add ((AlignedTVList ) workingListForFlush );
549- List <Integer > columnIndexList = buildColumnIndexList (schemaList );
597+ List <Integer > columnIndexList = buildColumnIndexList (activeSchemaList );
550598 MemPointIterator timeValuePairIterator =
551599 MemPointIteratorFactory .create (
552- dataTypes , columnIndexList , alignedTvLists , encodeInfo .maxNumberOfPointsInPage );
600+ getDataTypes (activeSchemaList ),
601+ columnIndexList ,
602+ alignedTvLists ,
603+ encodeInfo .maxNumberOfPointsInPage );
553604
554605 while (timeValuePairIterator .hasNextBatch ()) {
555606 timeValuePairIterator .encodeBatch (alignedChunkWriter , encodeInfo , times );
@@ -566,7 +617,7 @@ public void encode(BlockingQueue<Object> ioTaskQueue, BatchEncodeInfo encodeInfo
566617 } catch (InterruptedException e ) {
567618 Thread .currentThread ().interrupt ();
568619 }
569- alignedChunkWriter = new AlignedChunkWriterImpl (schemaList );
620+ alignedChunkWriter = new AlignedChunkWriterImpl (activeSchemaList );
570621 encodeInfo .reset ();
571622 }
572623 }
0 commit comments