-
Notifications
You must be signed in to change notification settings - Fork 980
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DRILL-8434: Add Median Function (#2803)
- Loading branch information
Showing
9 changed files
with
337 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
col1,col2 | ||
2,25 | ||
3,32 | ||
4,49 | ||
5,32 | ||
col1,col2,col3,col4 | ||
2,25,2.0,0 | ||
3,32,3.0,0 | ||
4,49,4.5, | ||
5,32,9,9 |
This file contains 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
This file contains 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,33 @@ | ||
# | ||
# 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. | ||
# | ||
|
||
{ | ||
aggrtypes: [ | ||
{className: "Median", funcName: "median", aliasName: "", types: [ | ||
{inputType: "Int", outputType: "NullableBigInt", major: "Numeric", java: "Long", medianHelper: "StreamingIntMedianHelper"}, | ||
{inputType: "BigInt", outputType: "NullableBigInt", major: "Numeric", java: "Long", medianHelper: "StreamingIntMedianHelper"}, | ||
{inputType: "NullableInt", outputType: "NullableBigInt", major: "Numeric", java: "Long", medianHelper: "StreamingIntMedianHelper"}, | ||
{inputType: "NullableBigInt", outputType: "NullableBigInt", major: "Numeric", java: "Long", medianHelper: "StreamingIntMedianHelper"}, | ||
{inputType: "Float4", outputType: "NullableFloat8", major: "Numeric", java: "Double", medianHelper: "StreamingDoubleMedianHelper"}, | ||
{inputType: "Float8", outputType: "NullableFloat8", major: "Numeric", java: "Double", medianHelper: "StreamingDoubleMedianHelper"}, | ||
{inputType: "NullableFloat4", outputType: "NullableFloat8", major: "Numeric", java: "Double", medianHelper: "StreamingDoubleMedianHelper"}, | ||
{inputType: "NullableFloat8", outputType: "NullableFloat8", major: "Numeric", java: "Double", medianHelper: "StreamingDoubleMedianHelper"}, | ||
] | ||
} | ||
] | ||
} |
106 changes: 106 additions & 0 deletions
106
exec/java-exec/src/main/codegen/templates/MedianFunctions.java
This file contains 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,106 @@ | ||
/* | ||
* 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. | ||
*/ | ||
<@pp.dropOutputFile /> | ||
|
||
|
||
|
||
<#list aggrtypes4.aggrtypes as aggrtype> | ||
<@pp.changeOutputFile name="/org/apache/drill/exec/expr/fn/impl/gaggr/${aggrtype.className}Functions.java" /> | ||
|
||
<#include "/@includes/license.ftl" /> | ||
|
||
/* | ||
* This class is generated using freemarker and the ${.template_name} template. | ||
*/ | ||
|
||
<#-- A utility class that is used to generate java code for median functions --> | ||
|
||
package org.apache.drill.exec.expr.fn.impl.gaggr; | ||
|
||
import org.apache.drill.exec.expr.DrillAggFunc; | ||
import org.apache.drill.exec.expr.annotations.FunctionTemplate; | ||
import org.apache.drill.exec.expr.annotations.FunctionTemplate.NullHandling; | ||
import org.apache.drill.exec.expr.annotations.FunctionTemplate.FunctionScope; | ||
import org.apache.drill.exec.expr.annotations.Output; | ||
import org.apache.drill.exec.expr.annotations.Param; | ||
import org.apache.drill.exec.expr.annotations.Workspace; | ||
import org.apache.drill.exec.expr.holders.*; | ||
import org.apache.drill.exec.expr.fn.impl.StreamingMedianHelpers.StreamingMedianHelper; | ||
import org.apache.drill.exec.expr.fn.impl.StreamingMedianHelpers.StreamingIntMedianHelper; | ||
import org.apache.drill.exec.expr.fn.impl.StreamingMedianHelpers.StreamingDoubleMedianHelper; | ||
|
||
@SuppressWarnings("unused") | ||
|
||
public class ${aggrtype.className}Functions { | ||
static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(${aggrtype.className}Functions.class); | ||
|
||
<#list aggrtype.types as type> | ||
|
||
<#if aggrtype.aliasName == ""> | ||
@FunctionTemplate(name = "${aggrtype.funcName}", scope = FunctionTemplate.FunctionScope.POINT_AGGREGATE) | ||
<#else> | ||
@FunctionTemplate(names = {"${aggrtype.funcName}", "${aggrtype.aliasName}"}, scope = FunctionTemplate.FunctionScope.POINT_AGGREGATE) | ||
</#if> | ||
|
||
public static class ${type.inputType}${aggrtype.className} implements DrillAggFunc { | ||
|
||
@Param ${type.inputType}Holder input; | ||
@Output ${type.outputType}Holder median; | ||
@Workspace ObjectHolder utils; | ||
|
||
public void setup() { | ||
// Initialize the ObjectHolder | ||
utils = new ObjectHolder(); | ||
utils.obj = new org.apache.drill.exec.expr.fn.impl.StreamingMedianHelpers.${type.medianHelper}(); | ||
} | ||
|
||
@Override | ||
public void add() { | ||
org.apache.drill.exec.expr.fn.impl.StreamingMedianHelpers.${type.medianHelper} medianHelper = (org.apache.drill.exec.expr.fn.impl.StreamingMedianHelpers.${type.medianHelper}) utils.obj; | ||
<#if type.inputType?starts_with("Nullable")> | ||
sout: { | ||
if (input.isSet == 0) { | ||
// processing nullable input and the value is null, so don't do anything... | ||
break sout; | ||
} | ||
</#if> | ||
medianHelper.addNextNumber(input.value); | ||
|
||
<#if type.inputType?starts_with("Nullable")> | ||
} // end of sout block | ||
</#if> | ||
} | ||
|
||
@Override | ||
public void output() { | ||
org.apache.drill.exec.expr.fn.impl.StreamingMedianHelpers.${type.medianHelper} medianHelper = (org.apache.drill.exec.expr.fn.impl.StreamingMedianHelpers.${type.medianHelper}) utils.obj; | ||
median.value = medianHelper.getMedian(); | ||
median.isSet = 1; | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
org.apache.drill.exec.expr.fn.impl.StreamingMedianHelpers.StreamingMedianHelper medianHelper = (org.apache.drill.exec.expr.fn.impl.StreamingMedianHelpers.${type.medianHelper}) utils.obj; | ||
medianHelper.reset(); | ||
} | ||
} | ||
|
||
|
||
</#list> | ||
} | ||
</#list> |
133 changes: 133 additions & 0 deletions
133
exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/StreamingMedianHelpers.java
This file contains 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,133 @@ | ||
/* | ||
* 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.drill.exec.expr.fn.impl; | ||
|
||
import java.util.Comparator; | ||
import java.util.PriorityQueue; | ||
|
||
/** | ||
* This class implements a heap-based streaming median. | ||
* | ||
*<p> | ||
* Reference: <a href="https://www.baeldung.com/java-stream-integers-median-using-heap">Stream Integers Median using Heap</a> | ||
* </p> | ||
*/ | ||
|
||
public class StreamingMedianHelpers { | ||
|
||
public interface StreamingMedianHelper { | ||
void reset(); | ||
} | ||
|
||
public static class StreamingIntMedianHelper implements StreamingMedianHelper { | ||
private final PriorityQueue<Long> minHeap; | ||
private final PriorityQueue<Long> maxHeap; | ||
|
||
public StreamingIntMedianHelper() { | ||
super(); | ||
this.minHeap = new PriorityQueue<>(); | ||
this.maxHeap = new PriorityQueue<>(Comparator.reverseOrder()); | ||
} | ||
|
||
public void addNextNumber(Long n) { | ||
if (!minHeap.isEmpty() && n < minHeap.peek()) { | ||
maxHeap.offer(n); | ||
if (maxHeap.size() > minHeap.size() + 1) { | ||
minHeap.offer(maxHeap.poll()); | ||
} | ||
} else { | ||
minHeap.offer(n); | ||
if (minHeap.size() > maxHeap.size() + 1) { | ||
maxHeap.offer(minHeap.poll()); | ||
} | ||
} | ||
} | ||
|
||
public void addNextNumber(int n) { | ||
addNextNumber((long) n); | ||
} | ||
|
||
public Long getMedian() { | ||
Long median; | ||
if (minHeap.size() < maxHeap.size()) { | ||
median = maxHeap.peek(); | ||
} else if (minHeap.size() > maxHeap.size()) { | ||
median = minHeap.peek(); | ||
} else if (minHeap.isEmpty() && maxHeap.isEmpty()) { | ||
median = 0L; | ||
} else { | ||
median = (minHeap.peek() + maxHeap.peek()) / 2; | ||
} | ||
return median; | ||
} | ||
|
||
public void reset() { | ||
minHeap.clear(); | ||
maxHeap.clear(); | ||
} | ||
} | ||
|
||
public static class StreamingDoubleMedianHelper implements StreamingMedianHelper { | ||
private final PriorityQueue<Double> minHeap; | ||
private final PriorityQueue<Double> maxHeap; | ||
|
||
public StreamingDoubleMedianHelper() { | ||
super(); | ||
this.minHeap = new PriorityQueue<>(); | ||
this.maxHeap = new PriorityQueue<>(Comparator.reverseOrder()); | ||
} | ||
|
||
public void addNextNumber(Double n) { | ||
if (!minHeap.isEmpty() && n < minHeap.peek()) { | ||
maxHeap.offer(n); | ||
if (maxHeap.size() > minHeap.size() + 1) { | ||
minHeap.offer(maxHeap.poll()); | ||
} | ||
} else { | ||
minHeap.offer(n); | ||
if (minHeap.size() > maxHeap.size() + 1) { | ||
maxHeap.offer(minHeap.poll()); | ||
} | ||
} | ||
} | ||
|
||
public void addNextNumber(float n) { | ||
addNextNumber((double) n); | ||
} | ||
|
||
public Double getMedian() { | ||
Double median; | ||
if (minHeap.size() < maxHeap.size()) { | ||
median = maxHeap.peek(); | ||
} else if (minHeap.size() > maxHeap.size()) { | ||
median = minHeap.peek(); | ||
} else if (minHeap.isEmpty() && maxHeap.isEmpty()) { | ||
median = 0.0; | ||
} else { | ||
median = (minHeap.peek() + maxHeap.peek()) / 2; | ||
} | ||
return median; | ||
} | ||
|
||
public void reset() { | ||
minHeap.clear(); | ||
maxHeap.clear(); | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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,11 @@ | ||
category,value | ||
cat1,5 | ||
cat1,3 | ||
cat1,2 | ||
cat1,7 | ||
cat1,10 | ||
cat2,1 | ||
cat2,5 | ||
cat2,9 | ||
cat2,2 | ||
cat2,11 |
This file contains 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,5 @@ | ||
col1,col2,col3,col4 | ||
2,25,2.0,0 | ||
3,32,3.0,0 | ||
4,49,4.5, | ||
5,32,9,9 |