Skip to content

Commit

Permalink
1. add array_max_count_element() and max_count_element() functions; 2…
Browse files Browse the repository at this point in the history
…. build with dependencies instead of using teradata's dependencies
  • Loading branch information
archongum committed Aug 30, 2019
1 parent 767d4af commit e7c818a
Show file tree
Hide file tree
Showing 20 changed files with 405 additions and 777 deletions.
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/target/
.idea
*.iml

# Compiled class file
*.class
Expand All @@ -15,11 +16,14 @@
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# idea
target/
out/
13 changes: 0 additions & 13 deletions .idea/compiler.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/encodings.xml

This file was deleted.

14 changes: 0 additions & 14 deletions .idea/misc.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

683 changes: 0 additions & 683 deletions .idea/workspace.xml

This file was deleted.

30 changes: 18 additions & 12 deletions README.md
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) |
44 changes: 24 additions & 20 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.archon</groupId>
<groupId>com.github.archongum</groupId>
<artifactId>presto-udf</artifactId>
<version>2</version>
<version>3</version>

<properties>
<presto.version>305</presto.version>
<presto.version>318</presto.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.version>3.8.0</maven.compiler.version>
<guava.verson>26.0-jre</guava.verson>
<jackson.version>2.9.9.3</jackson.version>
<junit.version>5.4.2</junit.version>
</properties>


<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.verson}</version>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>

<dependency>
Expand All @@ -32,21 +33,16 @@
</dependency>

<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.3.1</version>
<scope>test</scope>
<groupId>io.prestosql</groupId>
<artifactId>presto-array</artifactId>
<version>${presto.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.3.1</version>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -61,7 +57,15 @@
<encoding>utf-8</encoding>
</configuration>
</plugin>

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.archon.presto;
package com.github.archongum.presto;

import com.archon.presto.udf.scalar.CommonFunctions;
import com.archon.presto.udf.scalar.DateTimeFunctions;
import com.google.common.collect.ImmutableSet;
import com.github.archongum.presto.udf.aggregate.MaxCountElementAggregation;
import com.github.archongum.presto.udf.scalar.ArrayMaxCountElementFunction;
import com.github.archongum.presto.udf.scalar.CommonFunctions;
import com.github.archongum.presto.udf.scalar.DateTimeFunctions;
import io.prestosql.spi.Plugin;

import java.util.HashSet;
import java.util.Set;


Expand All @@ -28,9 +30,11 @@ public class UdfPlugin implements Plugin
@Override
public Set<Class<?>> getFunctions()
{
return ImmutableSet.<Class<?>>builder()
.add(DateTimeFunctions.class)
.add(CommonFunctions.class)
.build();
Set<Class<?>> set = new HashSet<>();
set.add(ArrayMaxCountElementFunction.class);
set.add(CommonFunctions.class);
set.add(DateTimeFunctions.class);
set.add(MaxCountElementAggregation.class);
return set;
}
}
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()));
}
}
}
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);
}
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;
}
}
Loading

0 comments on commit e7c818a

Please sign in to comment.