-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reasoner): add reasoner udf (#17)
- Loading branch information
1 parent
b4b3511
commit d43a78c
Showing
70 changed files
with
6,066 additions
and
1 deletion.
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 |
---|---|---|
@@ -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> |
100 changes: 100 additions & 0 deletions
100
reasoner/udf/src/main/java/com/antgroup/openspg/reasoner/udf/UdfMng.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,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(); | ||
} |
27 changes: 27 additions & 0 deletions
27
reasoner/udf/src/main/java/com/antgroup/openspg/reasoner/udf/UdfMngFactory.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,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(); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
reasoner/udf/src/main/java/com/antgroup/openspg/reasoner/udf/builtin/udaf/Avg.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 @@ | ||
/* | ||
* 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$; | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
reasoner/udf/src/main/java/com/antgroup/openspg/reasoner/udf/builtin/udaf/ConcatAgg.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,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(); | ||
} | ||
} |
Oops, something went wrong.