Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(reasoner): add reasoner udf #17

Merged
merged 5 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion reasoner/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<packaging>pom</packaging>
<modules>
<module>common</module>
<module>udf</module>
</modules>

<properties>
Expand Down Expand Up @@ -55,6 +56,11 @@
<artifactId>reasoner-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.antgroup.openspg.reasoner</groupId>
<artifactId>reasoner-udf</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.antlr</groupId>
Expand Down Expand Up @@ -104,7 +110,7 @@
<dependency>
<groupId>io.github.lukehutch</groupId>
<artifactId>fast-classpath-scanner</artifactId>
<version>2.6.0</version>
<version>2.21</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down Expand Up @@ -528,6 +534,18 @@
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.emf</groupId>
<artifactId>org.eclipse.emf.common</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.emf</groupId>
<artifactId>org.eclipse.emf.ecore</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.emf</groupId>
<artifactId>org.eclipse.emf.ecore.xmi</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Expand Down
65 changes: 65 additions & 0 deletions reasoner/udf/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2023 Ant Group CO., Ltd.
~
~ Licensed 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>
<parent>
<groupId>com.antgroup.openspg.reasoner</groupId>
<artifactId>reasoner-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>reasoner-udf</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>com.antgroup.openspg.reasoner</groupId>
<artifactId>reasoner-common</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>io.github.lukehutch</groupId>
<artifactId>fast-classpath-scanner</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>com.google.geometry</groupId>
<artifactId>s2-geometry</artifactId>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-swing</artifactId>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 2023 Ant Group CO., Ltd.
*
* Licensed 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.
*/

package com.antgroup.openspg.reasoner.udf;

import com.antgroup.openspg.reasoner.common.types.KgType;
import com.antgroup.openspg.reasoner.udf.model.IUdfMeta;
import com.antgroup.openspg.reasoner.udf.model.RuntimeUdfMeta;
import com.antgroup.openspg.reasoner.udf.model.UdafMeta;
import com.antgroup.openspg.reasoner.udf.model.UdfParameterTypeHint;
import com.antgroup.openspg.reasoner.udf.model.UdtfMeta;
import java.util.List;
import java.util.Map;

public interface UdfMng {
/**
* Query UDF meta information, which can be used to determine whether UDF exists KgType is
* compatible with java Object type
*
* <p>Query is inefficient, please cache for use
*
* @param name
* @param paramTypeList
* @return
*/
IUdfMeta getUdfMeta(String name, List<KgType> paramTypeList);

/**
* use for querying the parameter list of the UDF can only be obtained on runtime
*
* @param name
* @return
*/
RuntimeUdfMeta getRuntimeUdfMeta(String name);

/**
* Query UDAF meta information
*
* <p>Query is inefficient, please cache for use
*
* @param name
* @param rowDataType
* @return
*/
UdafMeta getUdafMeta(String name, KgType rowDataType);

/**
* Query UDTF meta information
*
* @param name
* @param rowDataTypes
* @return
*/
UdtfMeta getUdtfMeta(String name, List<KgType> rowDataTypes);

/**
* get all udf meta information, for udf registration
*
* @return
*/
List<IUdfMeta> getAllUdfMeta();

/**
* get all runtime udf meta, for udf registration on QLExpress
*
* @return
*/
List<RuntimeUdfMeta> getAllRuntimeUdfMeta();

/**
* get all udaf for registration
*
* @return
*/
List<UdafMeta> getAllUdafMeta();

/**
* get all udtf meta for registration
*
* @return
*/
List<UdtfMeta> getAllUdtfMeta();

/**
* get udf type list hint
*
* <p>KTObject means support all kind types
*/
Map<String, Map<String, UdfParameterTypeHint>> getUdfTypeHint();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2023 Ant Group CO., Ltd.
*
* Licensed 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.
*/

package com.antgroup.openspg.reasoner.udf;

import com.antgroup.openspg.reasoner.udf.impl.UdfMngImpl;

public class UdfMngFactory {
/**
* Factory mode
*
* @return
*/
public static UdfMng getUdfMng() {
return UdfMngImpl.getInstance();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2023 Ant Group CO., Ltd.
*
* Licensed 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.
*/

package com.antgroup.openspg.reasoner.udf.builtin.udaf;

import com.antgroup.openspg.reasoner.common.types.KTDouble$;
import com.antgroup.openspg.reasoner.common.types.KTInteger$;
import com.antgroup.openspg.reasoner.common.types.KTLong$;
import com.antgroup.openspg.reasoner.common.types.KTString$;
import com.antgroup.openspg.reasoner.common.types.KgType;
import com.antgroup.openspg.reasoner.udf.model.BaseUdaf;
import com.antgroup.openspg.reasoner.udf.model.UdfDefine;

public class Avg {

/** avg double compute function */
@UdfDefine(name = "avg", compatibleName = "Avg")
public static class AvgDouble implements BaseUdaf {
private double sum = 0.0;
private int count = 0;

@Override
public KgType getInputRowType() {
return KTDouble$.MODULE$;
}

@Override
public KgType getResultType() {
return KTDouble$.MODULE$;
}

@Override
public void initialize(Object... params) {}

@Override
public void update(Object row) {
sum += ((Number) row).doubleValue();
count++;
}

@Override
public void merge(BaseUdaf function) {
AvgDouble other = (AvgDouble) function;
this.sum += other.sum;
this.count += other.count;
}

@Override
public Object evaluate() {
return this.sum / this.count;
}
}

@UdfDefine(name = "avg", compatibleName = "Avg")
public static class AvgInt extends AvgDouble {
@Override
public KgType getInputRowType() {
return KTInteger$.MODULE$;
}
}

@UdfDefine(name = "avg", compatibleName = "Avg")
public static class AvgLong extends AvgDouble {
@Override
public KgType getInputRowType() {
return KTLong$.MODULE$;
}
}

/** avg string compute function */
@UdfDefine(name = "avg", compatibleName = "Avg")
public static class AvgString extends AvgDouble {
@Override
public KgType getInputRowType() {
return KTString$.MODULE$;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2023 Ant Group CO., Ltd.
*
* Licensed 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.
*/

package com.antgroup.openspg.reasoner.udf.builtin.udaf;

import com.antgroup.openspg.reasoner.common.types.KTObject$;
import com.antgroup.openspg.reasoner.common.types.KTString$;
import com.antgroup.openspg.reasoner.common.types.KgType;
import com.antgroup.openspg.reasoner.udf.model.BaseUdaf;
import com.antgroup.openspg.reasoner.udf.model.UdfDefine;

@UdfDefine(name = "concat_agg", compatibleName = "concat_agg")
public class ConcatAgg implements BaseUdaf {
private StringBuilder stringBuilder;
private static final String SPLITTER = ",";

@Override
public KgType getInputRowType() {
return KTObject$.MODULE$;
}

@Override
public KgType getResultType() {
return KTString$.MODULE$;
}

@Override
public void initialize(Object... params) {
this.stringBuilder = new StringBuilder();
}

@Override
public void update(Object row) {
if (stringBuilder.length() > 0) {
stringBuilder.append(SPLITTER);
}
stringBuilder.append(row);
}

@Override
public void merge(BaseUdaf function) {
ConcatAgg other = (ConcatAgg) function;
if (stringBuilder.length() > 0) {
stringBuilder.append(SPLITTER);
}
this.stringBuilder.append(other.stringBuilder.toString());
}

@Override
public Object evaluate() {
return this.stringBuilder.toString();
}
}
Loading
Loading