This repository was archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
TAJO-1112: Implement histogram interface and a candidate histogram #200
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
91b2cb7
internal histogram support
mvhlong f40ec9d
fix minor bug
mvhlong 2f04c40
fix minor bug
mvhlong 183b971
update based on Jihoonson's comments
mvhlong 8dac208
change from using Double to Datum for the ease of future extensions
mvhlong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
...o-catalog-common/src/main/java/org/apache/tajo/catalog/statistics/EquiDepthHistogram.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| /** | ||
| * 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.tajo.catalog.statistics; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| import org.apache.tajo.catalog.proto.CatalogProtos.HistogramProto; | ||
| import org.apache.tajo.common.TajoDataTypes.Type; | ||
| import org.apache.tajo.datum.Datum; | ||
| import org.apache.tajo.util.TUtil; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
|
|
||
| public class EquiDepthHistogram extends Histogram { | ||
|
|
||
| public EquiDepthHistogram(Type dataType) { | ||
| super(dataType); | ||
| } | ||
|
|
||
| public EquiDepthHistogram(HistogramProto proto) { | ||
| super(proto); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean construct(List<Datum> samples) { | ||
| int numBuckets = samples.size() > DEFAULT_MAX_BUCKETS ? DEFAULT_MAX_BUCKETS : samples.size(); | ||
| return construct(samples, numBuckets); | ||
| } | ||
|
|
||
| /** | ||
| * An EquiDepth histogram is constructed by dividing the data points into buckets of equal frequencies (as equal as | ||
| * possible). | ||
| * | ||
| * Note that, this function, which allows the specification of the (maximum) number of buckets, should be called | ||
| * directly only in the unit tests. In non-test cases, the construct(samples) version above should be used. | ||
| */ | ||
| @VisibleForTesting | ||
| public boolean construct(List<Datum> srcSamples, int numBuckets) { | ||
| isReady = false; | ||
| buckets = TUtil.newList(); | ||
|
|
||
| ArrayList<Datum> samples = new ArrayList<Datum>(srcSamples); // sorted samples | ||
| Collections.sort(samples); | ||
|
|
||
| int averageFrequency = Math.round((float) samples.size() / numBuckets); | ||
| int bFrequency; | ||
| int bMinIndex = 0, bMaxIndex; | ||
| Datum bMin, bMax; | ||
|
|
||
| for (int i = 0; i < numBuckets && bMinIndex < samples.size(); i++) { | ||
| bMin = samples.get(bMinIndex); | ||
| bFrequency = averageFrequency; | ||
| bMaxIndex = bMinIndex + averageFrequency - 1; | ||
|
|
||
| if (bMaxIndex > samples.size() - 1) { | ||
| bMaxIndex = samples.size() - 1; | ||
| bFrequency = bMaxIndex - bMinIndex + 1; | ||
| } | ||
|
|
||
| while (bMaxIndex + 1 < samples.size() && samples.get(bMaxIndex).equals(samples.get(bMaxIndex + 1))) { | ||
| bMaxIndex++; | ||
| bFrequency++; | ||
| } | ||
| bMax = samples.get(bMaxIndex); | ||
| bMinIndex = bMaxIndex + 1; | ||
| buckets.add(new HistogramBucket(bMin, bMax, bFrequency)); | ||
| } | ||
|
|
||
| if (bMinIndex < samples.size()) { | ||
| int lastBucket = buckets.size() - 1; | ||
| int additionalFrequency = samples.size() - bMinIndex; | ||
| buckets.get(lastBucket).setFrequency(buckets.get(lastBucket).getFrequency() + additionalFrequency); | ||
| buckets.get(lastBucket).setMax(samples.get(samples.size() - 1)); | ||
| } | ||
|
|
||
| samples.clear(); | ||
| isReady = true; | ||
| lastAnalyzed = System.currentTimeMillis(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to use org.apache.hadoop.yarn.util.Clock. |
||
|
|
||
| return true; | ||
| } | ||
| } | ||
97 changes: 97 additions & 0 deletions
97
...o-catalog-common/src/main/java/org/apache/tajo/catalog/statistics/EquiWidthHistogram.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| /** | ||
| * 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.tajo.catalog.statistics; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import org.apache.tajo.catalog.proto.CatalogProtos.HistogramProto; | ||
| import org.apache.tajo.common.TajoDataTypes.Type; | ||
| import org.apache.tajo.datum.Datum; | ||
| import org.apache.tajo.util.TUtil; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
|
|
||
| public class EquiWidthHistogram extends Histogram { | ||
|
|
||
| public EquiWidthHistogram(Type dataType) { | ||
| super(dataType); | ||
| } | ||
|
|
||
| public EquiWidthHistogram(HistogramProto proto) { | ||
| super(proto); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean construct(List<Datum> samples) { | ||
| int numBuckets = samples.size() > DEFAULT_MAX_BUCKETS ? DEFAULT_MAX_BUCKETS : samples.size(); | ||
| return construct(samples, numBuckets); | ||
| } | ||
|
|
||
| /** | ||
| * Originally, an EquiWidth histogram is constructed by dividing the entire value range of the data points into | ||
| * buckets of equal sizes. Here, we construct it similarly, then improve it by removing empty buckets, to save disk | ||
| * space and processing time, and by compacting the buckets' boundaries, to increase selectivity estimation accuracy. | ||
| * | ||
| * Note that, this function, which allows the specification of the (maximum) number of buckets, should be called | ||
| * directly only in the unit tests. In non-test cases, the construct(samples) version above should be used. | ||
| */ | ||
| @VisibleForTesting | ||
| public boolean construct(List<Datum> samples, int numBuckets) { | ||
| isReady = false; | ||
| buckets = TUtil.newList(); | ||
| Datum globalMin = Datum.getRangeMax(this.dataType); | ||
| Datum globalMax = Datum.getRangeMin(this.dataType); | ||
| List<Datum> bMinValues = new ArrayList<Datum>(numBuckets); | ||
| List<Datum> bMaxValues = new ArrayList<Datum>(numBuckets); | ||
| List<Long> bFrequencies = new ArrayList<Long>(numBuckets); | ||
|
|
||
| for (Datum p : samples) { | ||
| if (p.lessThan(globalMin).asBool() == true) globalMin = p; | ||
| if (p.greaterThan(globalMax).asBool() == true) globalMax = p; | ||
| } | ||
| double bWidth = (globalMax.asFloat8() - globalMin.asFloat8()) / numBuckets; | ||
|
|
||
| for (int i = 0; i < numBuckets; i++) { | ||
| bMinValues.add(Datum.getRangeMax(this.dataType)); | ||
| bMaxValues.add(Datum.getRangeMin(this.dataType)); | ||
| bFrequencies.add(0l); | ||
| } | ||
|
|
||
| for (Datum p : samples) { | ||
| int bIndex = (int) Math.round(Math.floor((p.asFloat8() - globalMin.asFloat8()) / bWidth)); | ||
| if (bIndex > numBuckets - 1) bIndex = numBuckets - 1; | ||
| bFrequencies.set(bIndex, bFrequencies.get(bIndex) + 1); | ||
|
|
||
| if (p.lessThan(bMinValues.get(bIndex)).asBool() == true) bMinValues.set(bIndex, p); | ||
| if (p.greaterThan(bMaxValues.get(bIndex)).asBool() == true) bMaxValues.set(bIndex, p); | ||
| } | ||
|
|
||
| for (int i = 0; i < numBuckets; i++) { | ||
| if (bFrequencies.get(i).longValue() > 0) { | ||
| buckets.add(new HistogramBucket(bMinValues.get(i), bMaxValues.get(i), bFrequencies.get(i))); | ||
| } | ||
| } | ||
|
|
||
| isReady = true; | ||
| lastAnalyzed = System.currentTimeMillis(); | ||
|
|
||
| return true; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To allow the function call only in unit tests, you can use the @VisibleForTesting annotation.