18
18
package org .apache .carbondata .hadoop .api ;
19
19
20
20
import java .io .IOException ;
21
- import java .util .ArrayList ;
22
- import java .util .Arrays ;
23
- import java .util .HashMap ;
24
- import java .util .HashSet ;
25
- import java .util .LinkedList ;
26
- import java .util .List ;
27
- import java .util .Map ;
21
+ import java .util .*;
28
22
import java .util .concurrent .ExecutionException ;
29
23
import java .util .stream .Collectors ;
30
24
@@ -222,7 +216,9 @@ public List<InputSplit> getSplits(JobContext job) throws IOException {
222
216
*/
223
217
private List <Segment > getFilteredSegment (JobContext job , List <Segment > validSegments ,
224
218
boolean validationRequired , ReadCommittedScope readCommittedScope ) throws IOException {
225
- Segment [] segmentsToAccess = getSegmentsToAccess (job , readCommittedScope );
219
+ // first check for mapreduce.input.carboninputformat.segmentnumbers
220
+ // second check for table property of latest_segment for query
221
+ Segment [] segmentsToAccess = getSegmentsToAccess (job , readCommittedScope , validSegments );
226
222
if (segmentsToAccess .length == 0 || segmentsToAccess [0 ].getSegmentNo ().equalsIgnoreCase ("*" )) {
227
223
return validSegments ;
228
224
}
@@ -421,9 +417,11 @@ public void updateLoadMetaDataDetailsToSegments(List<Segment> validSegments,
421
417
422
418
/**
423
419
* return valid segment to access
420
+ * check for SET carbon.input.segments.<database_name>.<table_name>
424
421
*/
425
422
public Segment [] getSegmentsToAccess (JobContext job , ReadCommittedScope readCommittedScope ) {
426
423
String segmentString = job .getConfiguration ().get (INPUT_SEGMENT_NUMBERS , "" );
424
+
427
425
if (segmentString .trim ().isEmpty ()) {
428
426
return new Segment [0 ];
429
427
}
@@ -601,4 +599,60 @@ public String getSegmentIdFromFilePath(String filePath) {
601
599
}
602
600
return CarbonCommonConstants .INVALID_SEGMENT_ID ;
603
601
}
602
+
603
+ /**
604
+ * return valid segment to access
605
+ * first check for mapreduce.input.carboninputformat.segmentnumbers"
606
+ * second check for table property of latest_segment for query
607
+ */
608
+ public Segment [] getSegmentsToAccess (JobContext job , ReadCommittedScope readCommittedScope ,
609
+ List <Segment > validSegments ) {
610
+ String segmentString = job .getConfiguration ().get (INPUT_SEGMENT_NUMBERS , "" );
611
+ boolean queryLatestSegment = false ;
612
+ if (null != carbonTable ) {
613
+ queryLatestSegment = Boolean .parseBoolean (carbonTable .getTableInfo ()
614
+ .getFactTable ().getTableProperties ()
615
+ .getOrDefault (CarbonCommonConstants .TABLE_QUERY_LATEST_SEGMENT , "false" ));
616
+ }
617
+ if (segmentString .trim ().isEmpty ()) {
618
+ if (!queryLatestSegment ) {
619
+ return new Segment [0 ];
620
+ } else {
621
+ List <Segment > segments = getLatestSegment (validSegments );
622
+ return segments .toArray (new Segment [0 ]);
623
+ }
624
+ } else {
625
+ List <Segment > segments = Segment .toSegmentList (segmentString .split ("," ), readCommittedScope );
626
+ if (!queryLatestSegment ) {
627
+ return segments .toArray (new Segment [0 ]);
628
+ } else {
629
+ List <Segment > latestSegment ;
630
+ if (segments .size () > 0 && segments .get (0 ).getSegmentNo ().equalsIgnoreCase ("*" )) {
631
+ latestSegment = getLatestSegment (validSegments );
632
+ } else {
633
+ latestSegment = getLatestSegment (segments );
634
+ }
635
+ return latestSegment .toArray (new Segment [0 ]);
636
+ }
637
+ }
638
+ }
639
+
640
+ /**
641
+ * get the latest segment
642
+ * @param validSegments the in put segment for search
643
+ * @return the latest segment for query
644
+ */
645
+ public List <Segment > getLatestSegment (List <Segment > validSegments ) {
646
+ if (validSegments .isEmpty ()) {
647
+ return Collections .emptyList ();
648
+ } else {
649
+ Segment segment = validSegments .stream ().max ((a , b ) -> {
650
+ double aNo = Double .parseDouble (a .getSegmentNo ());
651
+ double bNo = Double .parseDouble (b .getSegmentNo ());
652
+ return Double .compare (aNo , bNo );
653
+ }).get ();
654
+ return Collections .singletonList (segment );
655
+ }
656
+ }
657
+
604
658
}
0 commit comments