This is an automated email from the ASF dual-hosted git repository.

loogn pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/geaflow.git


The following commit(s) were added to refs/heads/master by this push:
     new e065ad6d [GQL] Add direction(edge) UDF for GQL (#617)
e065ad6d is described below

commit e065ad6d0cb4a6420dc738fa82323404d7bee5fb
Author: Leomrlin <[email protected]>
AuthorDate: Mon Sep 15 20:30:58 2025 +0800

    [GQL] Add direction(edge) UDF for GQL (#617)
    
    * add direction udf
    
    * fix import
    
    * fix checkstyle
---
 .../schema/function/BuildInSqlFunctionTable.java   |  9 ++++-
 .../geaflow/dsl/udf/table/other/Direction.java     | 41 ++++++++++++++++++++
 .../apache/geaflow/dsl/util/GraphSchemaUtil.java   |  5 +++
 .../geaflow/dsl/runtime/query/GQLMatchTest.java    | 10 +++++
 .../src/test/resources/expect/gql_match_014.txt    |  1 +
 .../src/test/resources/query/gql_match_014.sql     | 44 ++++++++++++++++++++++
 6 files changed, 109 insertions(+), 1 deletion(-)

diff --git 
a/geaflow/geaflow-dsl/geaflow-dsl-plan/src/main/java/org/apache/geaflow/dsl/schema/function/BuildInSqlFunctionTable.java
 
b/geaflow/geaflow-dsl/geaflow-dsl-plan/src/main/java/org/apache/geaflow/dsl/schema/function/BuildInSqlFunctionTable.java
index c32c9bc8..4962c5f9 100644
--- 
a/geaflow/geaflow-dsl/geaflow-dsl-plan/src/main/java/org/apache/geaflow/dsl/schema/function/BuildInSqlFunctionTable.java
+++ 
b/geaflow/geaflow-dsl/geaflow-dsl-plan/src/main/java/org/apache/geaflow/dsl/schema/function/BuildInSqlFunctionTable.java
@@ -24,7 +24,12 @@ import com.google.common.collect.Lists;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import org.apache.calcite.sql.*;
+
+import org.apache.calcite.sql.SqlFunction;
+import org.apache.calcite.sql.SqlFunctionCategory;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.SqlSyntax;
 import org.apache.calcite.sql.util.ListSqlOperatorTable;
 import org.apache.geaflow.dsl.common.exception.GeaFlowDSLException;
 import org.apache.geaflow.dsl.common.function.UDAF;
@@ -74,6 +79,7 @@ import org.apache.geaflow.dsl.udf.table.date.Year;
 import org.apache.geaflow.dsl.udf.table.math.E;
 import org.apache.geaflow.dsl.udf.table.math.Log2;
 import org.apache.geaflow.dsl.udf.table.math.Round;
+import org.apache.geaflow.dsl.udf.table.other.Direction;
 import org.apache.geaflow.dsl.udf.table.other.EdgeSrcId;
 import org.apache.geaflow.dsl.udf.table.other.EdgeTargetId;
 import org.apache.geaflow.dsl.udf.table.other.EdgeTimestamp;
@@ -187,6 +193,7 @@ public class BuildInSqlFunctionTable extends 
ListSqlOperatorTable {
 
             // udf.table.other
             .add(GeaFlowFunction.of(If.class))
+            .add(GeaFlowFunction.of(Direction.class))
             .add(GeaFlowFunction.of(Label.class))
             .add(GeaFlowFunction.of(VertexId.class))
             .add(GeaFlowFunction.of(EdgeSrcId.class))
diff --git 
a/geaflow/geaflow-dsl/geaflow-dsl-plan/src/main/java/org/apache/geaflow/dsl/udf/table/other/Direction.java
 
b/geaflow/geaflow-dsl/geaflow-dsl-plan/src/main/java/org/apache/geaflow/dsl/udf/table/other/Direction.java
new file mode 100644
index 00000000..157be636
--- /dev/null
+++ 
b/geaflow/geaflow-dsl/geaflow-dsl-plan/src/main/java/org/apache/geaflow/dsl/udf/table/other/Direction.java
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.geaflow.dsl.udf.table.other;
+
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.geaflow.common.binary.BinaryString;
+import org.apache.geaflow.dsl.common.data.RowEdge;
+import org.apache.geaflow.dsl.common.function.Description;
+import org.apache.geaflow.dsl.common.function.UDF;
+import org.apache.geaflow.dsl.planner.GQLJavaTypeFactory;
+import org.apache.geaflow.dsl.util.GraphSchemaUtil;
+
+@Description(name = "direction", description = "Returns direction for edge")
+public class Direction extends UDF implements GraphMetaFieldAccessFunction {
+
+    public BinaryString eval(RowEdge edge) {
+        return BinaryString.fromString(edge.getDirect().name());
+    }
+
+    @Override
+    public RelDataType getReturnRelDataType(GQLJavaTypeFactory typeFactory) {
+        return GraphSchemaUtil.getCurrentGraphDirectionType(typeFactory);
+    }
+}
diff --git 
a/geaflow/geaflow-dsl/geaflow-dsl-plan/src/main/java/org/apache/geaflow/dsl/util/GraphSchemaUtil.java
 
b/geaflow/geaflow-dsl/geaflow-dsl-plan/src/main/java/org/apache/geaflow/dsl/util/GraphSchemaUtil.java
index 676f618c..ea37484f 100644
--- 
a/geaflow/geaflow-dsl/geaflow-dsl-plan/src/main/java/org/apache/geaflow/dsl/util/GraphSchemaUtil.java
+++ 
b/geaflow/geaflow-dsl/geaflow-dsl-plan/src/main/java/org/apache/geaflow/dsl/util/GraphSchemaUtil.java
@@ -21,6 +21,7 @@ package org.apache.geaflow.dsl.util;
 
 import java.util.Optional;
 import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.sql.type.SqlTypeName;
 import org.apache.geaflow.dsl.common.exception.GeaFlowDSLException;
 import org.apache.geaflow.dsl.planner.GQLJavaTypeFactory;
 
@@ -70,6 +71,10 @@ public class GraphSchemaUtil {
         }
     }
 
+    public static RelDataType getCurrentGraphDirectionType(GQLJavaTypeFactory 
typeFactory) {
+        return typeFactory.createSqlType(SqlTypeName.VARCHAR);
+    }
+
     public static Optional<RelDataType> 
getCurrentGraphEdgeTimestampType(GQLJavaTypeFactory typeFactory) {
         if (typeFactory.getCurrentGraph() == null) {
             throw new GeaFlowDSLException("Cannot get edge ts type without 
setting current graph");
diff --git 
a/geaflow/geaflow-dsl/geaflow-dsl-runtime/src/test/java/org/apache/geaflow/dsl/runtime/query/GQLMatchTest.java
 
b/geaflow/geaflow-dsl/geaflow-dsl-runtime/src/test/java/org/apache/geaflow/dsl/runtime/query/GQLMatchTest.java
index 625de4f0..90b790d6 100644
--- 
a/geaflow/geaflow-dsl/geaflow-dsl-runtime/src/test/java/org/apache/geaflow/dsl/runtime/query/GQLMatchTest.java
+++ 
b/geaflow/geaflow-dsl/geaflow-dsl-runtime/src/test/java/org/apache/geaflow/dsl/runtime/query/GQLMatchTest.java
@@ -148,4 +148,14 @@ public class GQLMatchTest {
             .execute()
             .checkSinkResult();
     }
+
+    @Test
+    public void testMatch_014() throws Exception {
+        QueryTester
+                .build()
+                .withGraphDefine("/query/modern_graph.sql")
+                .withQueryPath("/query/gql_match_014.sql")
+                .execute()
+                .checkSinkResult();
+    }
 }
diff --git 
a/geaflow/geaflow-dsl/geaflow-dsl-runtime/src/test/resources/expect/gql_match_014.txt
 
b/geaflow/geaflow-dsl/geaflow-dsl-runtime/src/test/resources/expect/gql_match_014.txt
new file mode 100644
index 00000000..50c458ba
--- /dev/null
+++ 
b/geaflow/geaflow-dsl/geaflow-dsl-runtime/src/test/resources/expect/gql_match_014.txt
@@ -0,0 +1 @@
+1,1.0,OUT,1,4
\ No newline at end of file
diff --git 
a/geaflow/geaflow-dsl/geaflow-dsl-runtime/src/test/resources/query/gql_match_014.sql
 
b/geaflow/geaflow-dsl/geaflow-dsl-runtime/src/test/resources/query/gql_match_014.sql
new file mode 100644
index 00000000..ba1fba8d
--- /dev/null
+++ 
b/geaflow/geaflow-dsl/geaflow-dsl-runtime/src/test/resources/query/gql_match_014.sql
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+CREATE TABLE tbl_result (
+  a_id bigint,
+  weight double,
+  dir varchar,
+  flag bigint,
+  b_id bigint
+) WITH (
+       type='file',
+       geaflow.dsl.file.path='${target}'
+);
+
+USE GRAPH modern;
+
+INSERT INTO tbl_result
+SELECT
+       a_id,
+       weight,
+       dir,
+       flag,
+       b_id
+FROM (
+MATCH (a:person where id = 1)-[e:knows|created]-(b:person) WHERE e.~label = 
'knows' and b.id = 4
+RETURN a.id as a_id, e.weight as weight, direction(e) as dir,
+IF(direction(e) = 'OUT', 1, 0) as flag, b.id as b_id
+)
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to