Code review comment: Should the data parsing code be extracting into a Util and test it well ?

Code under review:
public TCreatePartitionResult createPartition(TCreatePartitionRequest request) throws TException {

        long dbId = request.getDb_id();
        long tableId = request.getTable_id();
        GlobalStateMgr state = GlobalStateMgr.getCurrentState();
        TCreatePartitionResult result = new TCreatePartitionResult();
        TStatus errorStatus = new TStatus(RUNTIME_ERROR);

        Database db = GlobalStateMgr.getCurrentState().getDb(dbId);
        if (db == null) {
            result.setStatus(errorStatus);
            result.setErr_msg(String.format("dbId=%d is not exists", dbId));
            return result;
        }
        Table table = db.getTable(tableId);
        if (table == null) {
            result.setStatus(errorStatus);
            result.setErr_msg(String.format("dbId=%d tableId=%d is not exists", dbId, tableId));
            return result;
        }
        if (!(table instanceof OlapTable)) {
            result.setStatus(errorStatus);
            result.setErr_msg(String.format("dbId=%d tableId=%d is not olap table", dbId, tableId));
            return result;
        }
        OlapTable olapTable = (OlapTable) table;

        if (request.partitionValues == null) {
            result.setStatus(errorStatus);
            result.setErr_msg("partitionValues should not null.");
            return result;
        }
        
        if (request.partitionValues.size() != 1) {
            result.setStatus(errorStatus);
            result.setErr_msg("only support single partition, partitionValues size should equal 1.");
            return result;
        }
        List<String> partitionValues = request.partitionValues.get(0);

        Set<String> partitionNames = Sets.newHashSet();
        PartitionInfo partitionInfo = olapTable.getPartitionInfo();
        if (!(partitionInfo instanceof ExpressionRangePartitionInfo)) {
            result.setStatus(errorStatus);
            result.setErr_msg("only support expression range partition.");
            return result;
        }
        List<Expr> partitionExprs = ((ExpressionRangePartitionInfo) partitionInfo).getPartitionExprs();
        if (partitionExprs.size() != 1) {
            result.setStatus(errorStatus);
            result.setErr_msg("only support one expression partitionExpr.");
            return result;
        }
        Expr expr = partitionExprs.get(0);
        if (!(expr instanceof FunctionCallExpr)) {
            result.setStatus(errorStatus);
            result.setErr_msg("only support FunctionCallExpr");
            return result;
        }
        FunctionCallExpr functionCallExpr = (FunctionCallExpr) expr;
        String fnName = functionCallExpr.getFnName().getFunction();
        if (!fnName.equals(FunctionSet.DATE_TRUNC)) {
            result.setStatus(errorStatus);
            result.setErr_msg("only support data_trunc function.");
            return result;
        }
        List<Expr> paramsExprs = functionCallExpr.getParams().exprs();
        if (paramsExprs.size() != 2) {
            result.setStatus(errorStatus);
            result.setErr_msg("params exprs size should be 2.");
            return result;
        }
        Expr granularityExpr = paramsExprs.get(0);
        if (!(granularityExpr instanceof StringLiteral)) {
            result.setStatus(errorStatus);
            result.setErr_msg("granularity is not string literal.");
            return result;
        }
        StringLiteral granularityLiteral = (StringLiteral) granularityExpr;
        String granularity = granularityLiteral.getStringValue();
        for (String partitionValue : partitionValues) {
            DateTimeFormatter beginDateTimeFormat;
            LocalDateTime beginTime;
            LocalDateTime endTime;

            try {
                beginDateTimeFormat = DateUtils.probeFormat(partitionValue);
                beginTime = DateUtils.parseStringWithDefaultHSM(partitionValue, beginDateTimeFormat);
                switch (granularity.toLowerCase()) {
                    case "day":
                        endTime = beginTime.plusDays(1);
                        break;
                    case "hour":
                        endTime = beginTime.plusHours(1);
                        break;
                    case "month":
                        endTime = beginTime.plusMonths(1);
                        break;
                    case "year":
                        endTime = beginTime.plusYears(1);
                        break;
                    default:
                        result.setStatus(errorStatus);
                        result.setErr_msg("unsupported partition granularity:" + granularity);
                        return result;
                }
                String lowerBound = beginTime.format(DateUtils.DATEKEY_FORMATTER);
                String upperBound = endTime.format(DateUtils.DATEKEY_FORMATTER);
                String partitionName = "p" + lowerBound;
                PartitionKeyDesc partitionKeyDesc = new PartitionKeyDesc(
                        Collections.singletonList(new PartitionValue(lowerBound)),
                        Collections.singletonList(new PartitionValue(upperBound)));
                Map<String, String> partitionProperties = Maps.newHashMap();
                Short replicationNum = olapTable.getTableProperty().getReplicationNum();
                partitionProperties.put("replication_num", String.valueOf(replicationNum));
                DistributionDesc distributionDesc = olapTable.getDefaultDistributionInfo().toDistributionDesc();

                SingleRangePartitionDesc singleRangePartitionDesc =
                        new SingleRangePartitionDesc(true, partitionName, partitionKeyDesc, partitionProperties);

                AddPartitionClause addPartitionClause = new AddPartitionClause(singleRangePartitionDesc, distributionDesc,
                        partitionProperties, false);
                try {
                    state.addPartitions(db, table.getName(), addPartitionClause);
                } catch (DdlException | AnalysisException e) {
                    result.setStatus(errorStatus);
                    result.setErr_msg(String.format("create partition failed. stmt:%s", addPartitionClause.toSql()));
                    return result;
                }
                partitionNames.add(partitionName);
            } catch (AnalysisException e) {
                result.setStatus(errorStatus);
                result.setErr_msg(String.format("failed to analysis partition value:%s", partitionValue));
                return result;
            }
        }

        
        List<TOlapTablePartition> partitions = Lists.newArrayList();
        List<TTabletLocation> tablets = Lists.newArrayList();
        for (String partitionName : partitionNames) {
            Partition partition = table.getPartition(partitionName);
            TOlapTablePartition tPartition = new TOlapTablePartition();
            tPartition.setId(partition.getId());
            RangePartitionInfo rangePartitionInfo = (RangePartitionInfo) olapTable.getPartitionInfo();
            Range<PartitionKey> range = rangePartitionInfo.getRange(partition.getId());
            int partColNum = rangePartitionInfo.getPartitionColumns().size();
            
            if (range.hasLowerBound() && !range.lowerEndpoint().isMinValue()) {
                for (int i = 0; i < partColNum; i++) {
                    tPartition.addToStart_keys(
                            range.lowerEndpoint().getKeys().get(i).treeToThrift().getNodes().get(0));
                }
            }
            
            if (range.hasUpperBound() && !range.upperEndpoint().isMaxValue()) {
                for (int i = 0; i < partColNum; i++) {
                    tPartition.addToEnd_keys(
                            range.upperEndpoint().getKeys().get(i).treeToThrift().getNodes().get(0));
                }
            }
            for (MaterializedIndex index : partition.getMaterializedIndices(MaterializedIndex.IndexExtState.ALL)) {
                tPartition.addToIndexes(new TOlapTableIndexTablets(index.getId(), Lists.newArrayList(
                        index.getTablets().stream().map(Tablet::getId).collect(Collectors.toList()))));
                tPartition.setNum_buckets(index.getTablets().size());
            }
            partitions.add(tPartition);
            
            int quorum = olapTable.getPartitionInfo().getQuorumNum(partition.getId(), ((OlapTable) table).writeQuorum());
            for (MaterializedIndex index : partition.getMaterializedIndices(MaterializedIndex.IndexExtState.ALL)) {
                for (Tablet tablet : index.getTablets()) {
                    
                    
                    LocalTablet localTablet = (LocalTablet) tablet;
                    Multimap<Replica, Long> bePathsMap =
                            localTablet.getNormalReplicaBackendPathMap(olapTable.getClusterId());
                    if (bePathsMap.keySet().size() < quorum) {
                        LOG.warn("auto go quorum exception");
                    }
                    
                    List<Replica> replicas = Lists.newArrayList(bePathsMap.keySet());
                    Collections.shuffle(replicas);
                    tablets.add(new TTabletLocation(tablet.getId(), replicas.stream().map(Replica::getBackendId)
                            .collect(Collectors.toList())));
                }
            }
        }
        result.setPartitions(partitions);
        result.setTablets(tablets);

        
        List<TNodeInfo> nodeInfos = Lists.newArrayList();
        TNodesInfo nodesInfo = new TNodesInfo();
        SystemInfoService systemInfoService = GlobalStateMgr.getCurrentState().getOrCreateSystemInfo(olapTable.getClusterId());
        for (Long id : systemInfoService.getBackendIds(false)) {
            Backend backend = systemInfoService.getBackend(id);
            nodesInfo.addToNodes(new TNodeInfo(backend.getId(), 0, backend.getHost(), backend.getBrpcPort()));
        }
        result.setNodes(nodeInfos);
        result.setStatus(new TStatus(OK));
        return result;
    }

Line commented on: switch (granularity.toLowerCase()) {

Revised code:
public TCreatePartitionResult createPartition(TCreatePartitionRequest request) throws TException {

        long dbId = request.getDb_id();
        long tableId = request.getTable_id();
        TCreatePartitionResult result = new TCreatePartitionResult();
        TStatus errorStatus = new TStatus(RUNTIME_ERROR);

        Database db = GlobalStateMgr.getCurrentState().getDb(dbId);
        if (db == null) {
            result.setStatus(errorStatus);
            result.setErr_msg(String.format("dbId=%d is not exists", dbId));
            return result;
        }
        Table table = db.getTable(tableId);
        if (table == null) {
            result.setStatus(errorStatus);
            result.setErr_msg(String.format("dbId=%d tableId=%d is not exists", dbId, tableId));
            return result;
        }
        if (!(table instanceof OlapTable)) {
            result.setStatus(errorStatus);
            result.setErr_msg(String.format("dbId=%d tableId=%d is not olap table", dbId, tableId));
            return result;
        }
        OlapTable olapTable = (OlapTable) table;

        if (request.partitionValues == null) {
            result.setStatus(errorStatus);
            result.setErr_msg("partitionValues should not null.");
            return result;
        }
        
        if (request.partitionValues.size() != 1) {
            result.setStatus(errorStatus);
            result.setErr_msg("only support single partition, partitionValues size should equal 1.");
            return result;
        }
        List<String> partitionValues = request.partitionValues.get(0);

        Set<String> partitionNames = Sets.newHashSet();
        PartitionInfo partitionInfo = olapTable.getPartitionInfo();
        if (!(partitionInfo instanceof ExpressionRangePartitionInfo)) {
            result.setStatus(errorStatus);
            result.setErr_msg("only support expression range partition.");
            return result;
        }
        List<Expr> partitionExprs = ((ExpressionRangePartitionInfo) partitionInfo).getPartitionExprs();
        if (partitionExprs.size() != 1) {
            result.setStatus(errorStatus);
            result.setErr_msg("only support one expression partitionExpr.");
            return result;
        }
        Expr expr = partitionExprs.get(0);
        if (!(expr instanceof FunctionCallExpr)) {
            result.setStatus(errorStatus);
            result.setErr_msg("only support FunctionCallExpr");
            return result;
        }
        FunctionCallExpr functionCallExpr = (FunctionCallExpr) expr;
        String fnName = functionCallExpr.getFnName().getFunction();
        if (!fnName.equals(FunctionSet.DATE_TRUNC)) {
            result.setStatus(errorStatus);
            result.setErr_msg("only support data_trunc function.");
            return result;
        }
        List<Expr> paramsExprs = functionCallExpr.getParams().exprs();
        if (paramsExprs.size() != 2) {
            result.setStatus(errorStatus);
            result.setErr_msg("params exprs size should be 2.");
            return result;
        }
        Expr granularityExpr = paramsExprs.get(0);
        if (!(granularityExpr instanceof StringLiteral)) {
            result.setStatus(errorStatus);
            result.setErr_msg("granularity is not string literal.");
            return result;
        }
        StringLiteral granularityLiteral = (StringLiteral) granularityExpr;
        String granularity = granularityLiteral.getStringValue();

        Map<String, AddPartitionClause> addPartitionClauseMap;
        try {
            addPartitionClauseMap = AnalyzerUtils.getAddPartitionClauseFromPartitionValues(partitionValues,
                    granularity, olapTable);
        } catch (AnalysisException ex) {
            result.setStatus(errorStatus);
            result.setErr_msg(ex.getMessage());
            return result;
        }

        GlobalStateMgr state = GlobalStateMgr.getCurrentState();
        for (AddPartitionClause addPartitionClause : addPartitionClauseMap.values()) {
            try {
                state.addPartitions(db, olapTable.getName(), addPartitionClause);
            } catch (DdlException | AnalysisException e) {
                result.setStatus(errorStatus);
                result.setErr_msg(String.format("create partition failed. stmt:%s", addPartitionClause.toSql()));
                return result;
            }
        }

        
        List<TOlapTablePartition> partitions = Lists.newArrayList();
        List<TTabletLocation> tablets = Lists.newArrayList();
        for (String partitionName : addPartitionClauseMap.keySet()) {
            Partition partition = table.getPartition(partitionName);
            TOlapTablePartition tPartition = new TOlapTablePartition();
            tPartition.setId(partition.getId());
            RangePartitionInfo rangePartitionInfo = (RangePartitionInfo) olapTable.getPartitionInfo();
            Range<PartitionKey> range = rangePartitionInfo.getRange(partition.getId());
            int partColNum = rangePartitionInfo.getPartitionColumns().size();
            
            if (range.hasLowerBound() && !range.lowerEndpoint().isMinValue()) {
                for (int i = 0; i < partColNum; i++) {
                    tPartition.addToStart_keys(
                            range.lowerEndpoint().getKeys().get(i).treeToThrift().getNodes().get(0));
                }
            }
            
            if (range.hasUpperBound() && !range.upperEndpoint().isMaxValue()) {
                for (int i = 0; i < partColNum; i++) {
                    tPartition.addToEnd_keys(
                            range.upperEndpoint().getKeys().get(i).treeToThrift().getNodes().get(0));
                }
            }
            for (MaterializedIndex index : partition.getMaterializedIndices(MaterializedIndex.IndexExtState.ALL)) {
                tPartition.addToIndexes(new TOlapTableIndexTablets(index.getId(), Lists.newArrayList(
                        index.getTablets().stream().map(Tablet::getId).collect(Collectors.toList()))));
                tPartition.setNum_buckets(index.getTablets().size());
            }
            partitions.add(tPartition);
            
            int quorum = olapTable.getPartitionInfo().getQuorumNum(partition.getId(), ((OlapTable) table).writeQuorum());
            for (MaterializedIndex index : partition.getMaterializedIndices(MaterializedIndex.IndexExtState.ALL)) {
                for (Tablet tablet : index.getTablets()) {
                    
                    
                    LocalTablet localTablet = (LocalTablet) tablet;
                    Multimap<Replica, Long> bePathsMap =
                            localTablet.getNormalReplicaBackendPathMap(olapTable.getClusterId());
                    if (bePathsMap.keySet().size() < quorum) {
                        LOG.warn("auto go quorum exception");
                    }
                    
                    List<Replica> replicas = Lists.newArrayList(bePathsMap.keySet());
                    Collections.shuffle(replicas);
                    tablets.add(new TTabletLocation(tablet.getId(), replicas.stream().map(Replica::getBackendId)
                            .collect(Collectors.toList())));
                }
            }
        }
        result.setPartitions(partitions);
        result.setTablets(tablets);

        
        List<TNodeInfo> nodeInfos = Lists.newArrayList();
        TNodesInfo nodesInfo = new TNodesInfo();
        SystemInfoService systemInfoService = GlobalStateMgr.getCurrentState().getOrCreateSystemInfo(olapTable.getClusterId());
        for (Long id : systemInfoService.getBackendIds(false)) {
            Backend backend = systemInfoService.getBackend(id);
            nodesInfo.addToNodes(new TNodeInfo(backend.getId(), 0, backend.getHost(), backend.getBrpcPort()));
        }
        result.setNodes(nodeInfos);
        result.setStatus(new TStatus(OK));
        return result;
    }