-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. add array_max_count_element() and max_count_element() functions; 2…
…. build with dependencies instead of using teradata's dependencies
- Loading branch information
Showing
20 changed files
with
405 additions
and
777 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,17 +1,23 @@ | ||
# Installation | ||
1. `mvn package` | ||
2. Copy `presto-udf-*.jar` to `PRESTO_HOME/plugin/teradata-functions/` in all presto nodes | ||
(copy to this directory because it has all jars we need) | ||
3. Restart presto | ||
1. `mvn clean assembly:assembly` | ||
2. Copy `presto-udf-*-jar-with-dependencies.jar` to `${PRESTO_HOME}/plugin/custom-functions/` in all presto nodes. | ||
(create directory if not exists) | ||
3. Restart presto cluster | ||
|
||
|
||
# Functions | ||
| Function | Return Type | Argument Types | Description | Usage | | ||
|-----------------------|-------------|----------------|--------------------------|---------------------------------------| | ||
| first_day | date | date | first day of month | first_day(current_date) | | ||
| last_day | date | date | last day of month | last_day(current_date) | | ||
| to_datetime | timestamp | date, varchar | combine the two args | to_datetime(current_date, '23:59:59') | | ||
| last_second | timestamp | date | last second of the date | last_second(current_date) | | ||
| yesterday_last_second | timestamp | | last second of yesterday | yesterday_last_second() | | ||
| yesterday | date | | yesterday | yesterday() | | ||
## Scalar Functions | ||
| Function | Return Type | Argument Types | Description | Usage | | ||
| ----------------------- | ----------- | -------------- | ------------------------------------------------------------------------------------ | ------------------------------------- | | ||
| first_day | date | date | first day of month | first_day(current_date) | | ||
| last_day | date | date | last day of month | last_day(current_date) | | ||
| to_datetime | timestamp | date, varchar | combine the two args | to_datetime(current_date, '23:59:59') | | ||
| last_second | timestamp | date | last second of the date | last_second(current_date) | | ||
| yesterday_last_second | timestamp | | last second of yesterday | yesterday_last_second() | | ||
| yesterday | date | | yesterday | yesterday() | | ||
| array_max_count_element | T | array(T) | Get maximum count element (null is not counting; if has multiple return one of them) | array_max_count_element(array['1']) | | ||
|
||
## Aggregate Functions | ||
| Function | Return Type | Argument Types | Description | Usage | | ||
| ----------------- | ----------- | -------------- | ------------------------------------------------------------------------------------ | ----------------------- | | ||
| max_count_element | VARCHAR | array(VARCHAR) | Get maximum count element (null is not counting; if has multiple return one of them) | max_count_element(name) | |
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
54 changes: 54 additions & 0 deletions
54
src/main/java/com/github/archongum/presto/udf/aggregate/MaxCountElementAggregation.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,54 @@ | ||
package com.github.archongum.presto.udf.aggregate; | ||
|
||
import com.github.archongum.presto.udf.aggregate.state.MapState; | ||
import io.airlift.slice.Slice; | ||
import io.airlift.slice.Slices; | ||
import io.prestosql.spi.block.BlockBuilder; | ||
import io.prestosql.spi.function.AggregationFunction; | ||
import io.prestosql.spi.function.CombineFunction; | ||
import io.prestosql.spi.function.Description; | ||
import io.prestosql.spi.function.InputFunction; | ||
import io.prestosql.spi.function.OutputFunction; | ||
import io.prestosql.spi.function.SqlType; | ||
import io.prestosql.spi.type.StandardTypes; | ||
|
||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
import static io.prestosql.spi.type.VarcharType.VARCHAR; | ||
|
||
|
||
@AggregationFunction("max_count_element") | ||
@Description("Get maximum count element (null is not counting; if has multiple return one of them)") | ||
public class MaxCountElementAggregation | ||
{ | ||
@InputFunction | ||
public static void input(MapState state, @SqlType(StandardTypes.VARCHAR) Slice value) | ||
{ | ||
String v = value.toStringUtf8(); | ||
Map<String, Long> map = state.getMap(); | ||
Long cnt = map.get(v); | ||
if (cnt == null) { | ||
map.put(v, 1L); | ||
} else { | ||
map.put(v, cnt+1); | ||
} | ||
} | ||
|
||
@CombineFunction | ||
public static void combine(MapState state, MapState otherState) | ||
{ | ||
otherState.getMap().forEach((k, v) -> state.getMap().merge(k, v, Long::sum)); | ||
} | ||
|
||
@OutputFunction(StandardTypes.VARCHAR) | ||
public static void output(MapState state, BlockBuilder out) | ||
{ | ||
if (state.getMap().isEmpty()) { | ||
out.appendNull(); | ||
} else { | ||
VARCHAR.writeSlice(out, | ||
Slices.utf8Slice(state.getMap().entrySet().stream().max(Entry.comparingByValue()).get().getKey())); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/github/archongum/presto/udf/aggregate/state/MapState.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,14 @@ | ||
package com.github.archongum.presto.udf.aggregate.state; | ||
|
||
import io.prestosql.spi.function.AccumulatorState; | ||
import io.prestosql.spi.function.AccumulatorStateMetadata; | ||
|
||
import java.util.Map; | ||
|
||
|
||
@AccumulatorStateMetadata(stateSerializerClass = MapStateSerializer.class, stateFactoryClass = MapStateFactory.class) | ||
public interface MapState extends AccumulatorState { | ||
Map<String, Long> getMap(); | ||
|
||
void setMap(Map<String, Long> value); | ||
} |
88 changes: 88 additions & 0 deletions
88
src/main/java/com/github/archongum/presto/udf/aggregate/state/MapStateFactory.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,88 @@ | ||
package com.github.archongum.presto.udf.aggregate.state; | ||
|
||
import io.prestosql.array.ObjectBigArray; | ||
import io.prestosql.spi.function.AccumulatorStateFactory; | ||
import io.prestosql.spi.function.GroupedAccumulatorState; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
|
||
/** | ||
* @author Archon 8/30/19 | ||
* @since | ||
*/ | ||
public class MapStateFactory implements AccumulatorStateFactory<MapState> { | ||
|
||
public static final class SingleMapState implements MapState { | ||
|
||
private Map<String, Long> map = new HashMap<>(); | ||
|
||
@Override | ||
public Map<String, Long> getMap() { | ||
return map; | ||
} | ||
|
||
@Override | ||
public void setMap(Map<String, Long> value) { | ||
this.map = value; | ||
} | ||
|
||
@Override | ||
public long getEstimatedSize() { | ||
return map.size(); | ||
} | ||
} | ||
|
||
public static class GroupedMapState implements GroupedAccumulatorState, MapState { | ||
|
||
private final ObjectBigArray<Map<String, Long>> maps = new ObjectBigArray<>(); | ||
private long groupId; | ||
|
||
@Override | ||
public Map<String, Long> getMap() { | ||
return maps.get(groupId); | ||
} | ||
|
||
@Override | ||
public void setMap(Map<String, Long> value) { | ||
maps.set(groupId, value); | ||
} | ||
|
||
@Override | ||
public void setGroupId(long groupId) { | ||
this.groupId = groupId; | ||
} | ||
|
||
@Override | ||
public void ensureCapacity(long size) { | ||
maps.ensureCapacity(size); | ||
} | ||
|
||
@Override | ||
public long getEstimatedSize() { | ||
return maps.sizeOf(); | ||
} | ||
} | ||
|
||
@Override | ||
public MapState createSingleState() { | ||
return new SingleMapState(); | ||
} | ||
|
||
@Override | ||
public Class<? extends MapState> getSingleStateClass() { | ||
return SingleMapState.class; | ||
} | ||
|
||
|
||
@Override | ||
public MapState createGroupedState() { | ||
return new GroupedMapState(); | ||
} | ||
|
||
@Override | ||
public Class<? extends MapState> getGroupedStateClass() { | ||
return GroupedMapState.class; | ||
} | ||
} |
Oops, something went wrong.