Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 0730B200CCF for ; Mon, 10 Jul 2017 05:05:35 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 05A51167AD3; Mon, 10 Jul 2017 03:05:35 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id C2718167AB8 for ; Mon, 10 Jul 2017 05:05:33 +0200 (CEST) Received: (qmail 29469 invoked by uid 500); 10 Jul 2017 03:05:33 -0000 Mailing-List: contact issues-help@carbondata.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@carbondata.apache.org Delivered-To: mailing list issues@carbondata.apache.org Received: (qmail 29273 invoked by uid 99); 10 Jul 2017 03:04:56 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 10 Jul 2017 03:04:56 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 9C9A5E96B2; Mon, 10 Jul 2017 03:04:53 +0000 (UTC) From: lionelcao To: issues@carbondata.apache.org Reply-To: issues@carbondata.apache.org References: In-Reply-To: Subject: [GitHub] carbondata pull request #1105: [WIP] Implement range interval partition Content-Type: text/plain Message-Id: <20170710030453.9C9A5E96B2@git1-us-west.apache.org> Date: Mon, 10 Jul 2017 03:04:53 +0000 (UTC) archived-at: Mon, 10 Jul 2017 03:05:35 -0000 Github user lionelcao commented on a diff in the pull request: https://github.com/apache/carbondata/pull/1105#discussion_r126328252 --- Diff: core/src/main/java/org/apache/carbondata/core/scan/partition/RangeIntervalPartitioner.java --- @@ -0,0 +1,206 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.carbondata.core.scan.partition; + +import java.io.Serializable; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + +import org.apache.carbondata.core.constants.CarbonCommonConstants; +import org.apache.carbondata.core.metadata.datatype.DataType; +import org.apache.carbondata.core.metadata.schema.PartitionInfo; +import org.apache.carbondata.core.util.CarbonProperties; + +/** + * Range Interval Partitioner + */ + +public class RangeIntervalPartitioner implements Partitioner { + + private int numPartitions; + private RangeIntervalComparator comparator; + private List boundsList = new ArrayList<>(); + private String intervalType; + private DataType partitionColumnDataType; + + private SimpleDateFormat timestampFormatter = new SimpleDateFormat(CarbonProperties.getInstance() + .getProperty(CarbonCommonConstants.CARBON_TIMESTAMP_FORMAT, + CarbonCommonConstants.CARBON_TIMESTAMP_DEFAULT_FORMAT)); + + private SimpleDateFormat dateFormatter = new SimpleDateFormat(CarbonProperties.getInstance() + .getProperty(CarbonCommonConstants.CARBON_DATE_FORMAT, + CarbonCommonConstants.CARBON_DATE_DEFAULT_FORMAT)); + + public RangeIntervalPartitioner(PartitionInfo partitionInfo) { + List values = partitionInfo.getRangeIntervalInfo(); + partitionColumnDataType = partitionInfo.getColumnSchemaList().get(0).getDataType(); + numPartitions = values.size() - 1; + // -1 is just for instead of null + boundsList.add(-1L); + for (int i = 0; i < numPartitions; i++) { + boundsList.add(PartitionUtil.getDataBasedOnDataType(values.get(i), partitionColumnDataType, + timestampFormatter, dateFormatter)); + } + comparator = new RangeIntervalComparator(); + intervalType = values.get(numPartitions); + } + + @Override public int numPartitions() { + String numParStr = CarbonProperties.getInstance().getProperty( + CarbonCommonConstants.MAX_PARTITION_FOR_RANGE_INTERVAL, + CarbonCommonConstants.DEFAULT_MAX_PARTITION_FOR_RANGE_INTERVAL); + return Integer.parseInt(numParStr); + } + + @Override public int getPartition(Object key) { + int partitionIndex = -1; + Object lastBound = boundsList.get(boundsList.size() - 1); + if (key == null) { + return 0; + } else { + for (int i = 1; i <= numPartitions; i++) { + if (comparator.compareTo(key, boundsList.get(i))) { + return i; + } + } + switch (intervalType.toLowerCase()) { + case "year": + partitionIndex = getDynamicPartitionForYear(key, lastBound); + break; + case "month": + partitionIndex = getDynamicPartitionForMonth(key, lastBound); + break; + case "week": + partitionIndex = getDynamicPartitionForWeek(key, lastBound); + break; + case "day": + partitionIndex = getDynamicPartitionForDay(key, lastBound); + break; + case "hour": + partitionIndex = getDynamicPartitionForHour(key, lastBound); + break; + default: + partitionIndex = -1; + } + // if value's partitionId is great than max partition, return 0 for it. + if (partitionIndex >= numPartitions()) { + return 0; + } + return partitionIndex; + } + } + + public int getDynamicPartitionForYear(Object key, Object lastBound) { + int partitionId = -1; + Calendar lastCalendar = Calendar.getInstance(); + Calendar keyCal = Calendar.getInstance(); + keyCal.setTimeInMillis((long)key); + lastCalendar.setTimeInMillis((long)lastBound); + while (true) { + lastCalendar.add(Calendar.YEAR, 1); + boundsList.add(boundsList.size(), lastCalendar.getTimeInMillis()); + ++numPartitions; + if (keyCal.compareTo(lastCalendar) == -1) { + partitionId = numPartitions; + break; + } + } + return partitionId; + } + + public int getDynamicPartitionForMonth(Object key, Object lastBound) { + int partitionId = -1; + Calendar lastCalendar = Calendar.getInstance(); + Calendar keyCal = Calendar.getInstance(); + keyCal.setTimeInMillis((long)key); + lastCalendar.setTimeInMillis((long)lastBound); + while (true) { + lastCalendar.add(Calendar.MONTH, 1); + boundsList.add(boundsList.size(), lastCalendar.getTimeInMillis()); + ++numPartitions; + if (keyCal.compareTo(lastCalendar) == -1) { + partitionId = numPartitions; + break; + } + } + return partitionId; --- End diff -- this should be partition index --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---