-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GIE Compiler] support udf in compiler
- Loading branch information
Showing
16 changed files
with
586 additions
and
49 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
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
138 changes: 138 additions & 0 deletions
138
...ompiler/src/main/java/com/alibaba/graphscope/common/ir/meta/function/BuiltInFunction.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,138 @@ | ||
/* | ||
* | ||
* * Copyright 2020 Alibaba Group Holding Limited. | ||
* * | ||
* * 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. | ||
* * See the License for the specific language governing permissions and | ||
* * limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.alibaba.graphscope.common.ir.meta.function; | ||
|
||
import com.alibaba.graphscope.common.ir.tools.config.GraphOpt; | ||
import com.alibaba.graphscope.common.ir.type.GraphLabelType; | ||
import com.alibaba.graphscope.common.ir.type.GraphPathType; | ||
import com.alibaba.graphscope.common.ir.type.GraphSchemaType; | ||
import com.alibaba.graphscope.common.ir.type.GraphTypeFamily; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
import org.apache.calcite.sql.type.*; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public enum BuiltInFunction { | ||
GS_FUNCTION_RELATIONSHIPS( | ||
"gs.function.relationships", | ||
// set return type inference of function 'gs.function.relationships': | ||
// 1. get first parameter type which should be `GraphPathType` | ||
// 2. get the inner edge type of `GraphPathType` | ||
// 3. convert to array type which has the edge type as the component | ||
ReturnTypes.ARG0 | ||
.andThen( | ||
(op, type) -> { | ||
GraphPathType.ElementType elementType = | ||
(GraphPathType.ElementType) type.getComponentType(); | ||
return elementType.getExpandType(); | ||
}) | ||
.andThen(SqlTypeTransforms.TO_ARRAY), | ||
// set operand type checker of function 'gs.function.relationships' | ||
GraphOperandTypes.family(GraphTypeFamily.PATH), | ||
GraphInferTypes.FIRST_KNOWN), | ||
|
||
GS_FUNCTION_NODES( | ||
"gs.function.nodes", | ||
ReturnTypes.ARG0 | ||
.andThen( | ||
(op, type) -> { | ||
GraphPathType.ElementType elementType = | ||
(GraphPathType.ElementType) type.getComponentType(); | ||
return elementType.getGetVType(); | ||
}) | ||
.andThen(SqlTypeTransforms.TO_ARRAY), | ||
GraphOperandTypes.family(GraphTypeFamily.PATH), | ||
GraphInferTypes.FIRST_KNOWN), | ||
|
||
GS_FUNCTION_START_NODE( | ||
"gs.function.startNode", | ||
ReturnTypes.ARG0.andThen( | ||
(op, type) -> { | ||
GraphSchemaType edgeType = (GraphSchemaType) type; | ||
List<GraphLabelType.Entry> srcLabels = | ||
edgeType.getLabelType().getLabelsEntry().stream() | ||
.map( | ||
e -> | ||
new GraphLabelType.Entry() | ||
.label(e.getSrcLabel()) | ||
.labelId(e.getSrcLabelId())) | ||
.collect(Collectors.toList()); | ||
return new GraphSchemaType( | ||
GraphOpt.Source.VERTEX, | ||
new GraphLabelType(srcLabels), | ||
ImmutableList.of()); | ||
}), | ||
GraphOperandTypes.family(GraphTypeFamily.EDGE), | ||
GraphInferTypes.FIRST_KNOWN), | ||
|
||
GS_FUNCTION_END_NODE( | ||
"gs.function.endNode", | ||
ReturnTypes.ARG0.andThen( | ||
(op, type) -> { | ||
GraphSchemaType edgeType = (GraphSchemaType) type; | ||
List<GraphLabelType.Entry> endLabels = | ||
edgeType.getLabelType().getLabelsEntry().stream() | ||
.map( | ||
e -> | ||
new GraphLabelType.Entry() | ||
.label(e.getDstLabel()) | ||
.labelId(e.getDstLabelId())) | ||
.collect(Collectors.toList()); | ||
return new GraphSchemaType( | ||
GraphOpt.Source.VERTEX, | ||
new GraphLabelType(endLabels), | ||
ImmutableList.of()); | ||
}), | ||
GraphOperandTypes.family(GraphTypeFamily.EDGE), | ||
GraphInferTypes.FIRST_KNOWN); | ||
|
||
BuiltInFunction( | ||
String signature, | ||
SqlReturnTypeInference returnTypeInference, | ||
SqlOperandTypeChecker operandTypeChecker, | ||
SqlOperandTypeInference operandTypeInference) { | ||
this.signature = signature; | ||
this.returnTypeInference = returnTypeInference; | ||
this.operandTypeChecker = operandTypeChecker; | ||
this.operandTypeInference = operandTypeInference; | ||
} | ||
|
||
private final String signature; | ||
private final SqlReturnTypeInference returnTypeInference; | ||
private final SqlOperandTypeChecker operandTypeChecker; | ||
private final SqlOperandTypeInference operandTypeInference; | ||
|
||
public String getSignature() { | ||
return this.signature; | ||
} | ||
|
||
public SqlReturnTypeInference getReturnTypeInference() { | ||
return this.returnTypeInference; | ||
} | ||
|
||
public SqlOperandTypeChecker getOperandTypeChecker() { | ||
return this.operandTypeChecker; | ||
} | ||
|
||
public SqlOperandTypeInference getOperandTypeInference() { | ||
return this.operandTypeInference; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...e/compiler/src/main/java/com/alibaba/graphscope/common/ir/meta/function/FunctionMeta.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,78 @@ | ||
/* | ||
* | ||
* * Copyright 2020 Alibaba Group Holding Limited. | ||
* * | ||
* * 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. | ||
* * See the License for the specific language governing permissions and | ||
* * limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.alibaba.graphscope.common.ir.meta.function; | ||
|
||
import com.alibaba.graphscope.common.ir.meta.procedure.StoredProcedureMeta; | ||
|
||
import org.apache.calcite.sql.type.*; | ||
|
||
import java.util.stream.Collectors; | ||
|
||
public class FunctionMeta { | ||
private final String signature; | ||
private final SqlReturnTypeInference returnTypeInference; | ||
private final SqlOperandTypeChecker operandTypeChecker; | ||
private final SqlOperandTypeInference operandTypeInference; | ||
|
||
public FunctionMeta( | ||
String signature, | ||
SqlReturnTypeInference returnTypeInference, | ||
SqlOperandTypeChecker operandTypeChecker, | ||
SqlOperandTypeInference operandTypeInference) { | ||
this.signature = signature; | ||
this.returnTypeInference = returnTypeInference; | ||
this.operandTypeChecker = operandTypeChecker; | ||
this.operandTypeInference = operandTypeInference; | ||
} | ||
|
||
public FunctionMeta(BuiltInFunction function) { | ||
this( | ||
function.getSignature(), | ||
function.getReturnTypeInference(), | ||
function.getOperandTypeChecker(), | ||
function.getOperandTypeInference()); | ||
} | ||
|
||
public FunctionMeta(StoredProcedureMeta meta) { | ||
this( | ||
meta.getName(), | ||
ReturnTypes.explicit(meta.getReturnType().getFieldList().get(0).getType()), | ||
GraphOperandTypes.metaTypeChecker(meta), | ||
InferTypes.explicit( | ||
meta.getParameters().stream() | ||
.map(k -> k.getDataType()) | ||
.collect(Collectors.toList()))); | ||
} | ||
|
||
public String getSignature() { | ||
return this.signature; | ||
} | ||
|
||
public SqlReturnTypeInference getReturnTypeInference() { | ||
return this.returnTypeInference; | ||
} | ||
|
||
public SqlOperandTypeChecker getOperandTypeChecker() { | ||
return this.operandTypeChecker; | ||
} | ||
|
||
public SqlOperandTypeInference getOperandTypeInference() { | ||
return this.operandTypeInference; | ||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
...compiler/src/main/java/com/alibaba/graphscope/common/ir/meta/function/GraphFunctions.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,119 @@ | ||
/* | ||
* | ||
* * Copyright 2020 Alibaba Group Holding Limited. | ||
* * | ||
* * 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. | ||
* * See the License for the specific language governing permissions and | ||
* * limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.alibaba.graphscope.common.ir.meta.function; | ||
|
||
import com.alibaba.graphscope.common.config.Configs; | ||
import com.alibaba.graphscope.common.config.GraphConfig; | ||
import com.alibaba.graphscope.common.ir.meta.procedure.StoredProcedureMeta; | ||
import com.google.common.collect.Maps; | ||
|
||
import org.apache.calcite.sql.type.GraphInferTypes; | ||
import org.apache.calcite.sql.type.OperandTypes; | ||
import org.apache.calcite.sql.type.ReturnTypes; | ||
import org.apache.calcite.sql.type.SqlTypeName; | ||
import org.yaml.snakeyaml.Yaml; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class GraphFunctions { | ||
public static final String FUNCTION_PREFIX = "gs.function."; | ||
private final Map<String, FunctionMeta> functionMetaMap; | ||
|
||
private static GraphFunctions instance; | ||
|
||
public static GraphFunctions instance(Configs configs) { | ||
if (instance == null) { | ||
instance = new GraphFunctions(configs); | ||
} | ||
return instance; | ||
} | ||
|
||
private GraphFunctions(Configs configs) { | ||
this.functionMetaMap = Maps.newHashMap(); | ||
this.registerBuiltInFunctions(); | ||
this.registerConfigFunctions(configs); | ||
} | ||
|
||
// initialize built-in functions | ||
private void registerBuiltInFunctions() { | ||
for (BuiltInFunction function : BuiltInFunction.values()) { | ||
FunctionMeta meta = new FunctionMeta(function); | ||
functionMetaMap.put(function.getSignature(), meta); | ||
} | ||
} | ||
|
||
// initialize functions from configuration: | ||
// 1. load the functions from the settings of 'graph.functions' | ||
// 2. if the setting not found, load the default resource under the classpath | ||
private void registerConfigFunctions(Configs configs) { | ||
try (InputStream stream = loadConfigAsStream(configs)) { | ||
Yaml yaml = new Yaml(); | ||
Map<String, Object> map = yaml.load(stream); | ||
if (map != null) { | ||
Object functions = map.get("graph_functions"); | ||
if (functions instanceof List) { | ||
for (Object function : (List) functions) { | ||
String functionYaml = yaml.dump(function); | ||
StoredProcedureMeta meta = | ||
StoredProcedureMeta.Deserializer.perform( | ||
new ByteArrayInputStream( | ||
functionYaml.getBytes(StandardCharsets.UTF_8))); | ||
functionMetaMap.put(meta.getName(), new FunctionMeta(meta)); | ||
} | ||
} | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private InputStream loadConfigAsStream(Configs configs) throws Exception { | ||
String functionConfig = GraphConfig.GRAPH_FUNCTIONS_URI.get(configs); | ||
if (!functionConfig.isEmpty()) { | ||
File file = new File(functionConfig); | ||
if (file.exists()) { | ||
return new FileInputStream(file); | ||
} | ||
} | ||
return Thread.currentThread() | ||
.getContextClassLoader() | ||
.getResourceAsStream("conf/graph_functions.yaml"); | ||
} | ||
|
||
public FunctionMeta getFunction(String functionName) { | ||
FunctionMeta meta = functionMetaMap.get(functionName); | ||
if (meta == null) { | ||
// if not exist, create a new function meta with no constraints on operands and return | ||
// types | ||
meta = | ||
new FunctionMeta( | ||
functionName, | ||
ReturnTypes.explicit(SqlTypeName.ANY), | ||
OperandTypes.VARIADIC, | ||
GraphInferTypes.FIRST_KNOWN); | ||
} | ||
return meta; | ||
} | ||
} |
Oops, something went wrong.