-
Notifications
You must be signed in to change notification settings - Fork 181
[ISSUE-358] Implement ISO-GQL labeled predicate #824
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
Open
yyyCode
wants to merge
1
commit into
apache:master
Choose a base branch
from
yyyCode:feat/issue-358-labeled-predicate
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
...ommon/src/main/java/org/apache/geaflow/dsl/common/function/LabeledPredicateFunctions.java
This file contains hidden or 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,83 @@ | ||
| /* | ||
| * 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.common.function; | ||
|
|
||
| import org.apache.geaflow.dsl.common.data.RowEdge; | ||
| import org.apache.geaflow.dsl.common.data.RowVertex; | ||
|
|
||
| /** | ||
| * Utility class providing static methods for the ISO-GQL labeled predicate. | ||
| * | ||
| * <p>Implements ISO-GQL Section 19.9: <labeled predicate>, which tests whether a | ||
| * graph element (vertex or edge) has a given label. | ||
| * | ||
| * <p>ISO-GQL General Rules (three-valued logic): | ||
| * <ul> | ||
| * <li>If the element or the label is null, the result is Unknown (null).</li> | ||
| * <li>If the element's label equals the given label, the result is True.</li> | ||
| * <li>Otherwise, the result is False.</li> | ||
| * </ul> | ||
| */ | ||
| public class LabeledPredicateFunctions { | ||
|
|
||
| /** | ||
| * Implements the IS LABELED predicate. | ||
| * | ||
| * @param elementValue the vertex or edge to check | ||
| * @param labelValue the label name to test for | ||
| * @return Boolean: true if the element has the given label, false if not, null if either | ||
| * operand is null | ||
| */ | ||
| public static Boolean isLabeled(Object elementValue, Object labelValue) { | ||
| // ISO-GQL Rule: If element or label is null, result is Unknown (null). | ||
| if (elementValue == null || labelValue == null) { | ||
| return null; | ||
| } | ||
|
|
||
| String elementLabel = getLabel(elementValue); | ||
| return elementLabel != null && elementLabel.equals(labelValue.toString()); | ||
| } | ||
|
|
||
| /** | ||
| * Implements the IS NOT LABELED predicate. | ||
| * | ||
| * @param elementValue the vertex or edge to check | ||
| * @param labelValue the label name to test for | ||
| * @return Boolean: true if the element does NOT have the given label, false if it does, null | ||
| * if either operand is null | ||
| */ | ||
| public static Boolean isNotLabeled(Object elementValue, Object labelValue) { | ||
| Boolean result = isLabeled(elementValue, labelValue); | ||
| // Three-valued logic: NOT Unknown = Unknown (null remains null). | ||
| return result == null ? null : !result; | ||
| } | ||
|
|
||
| private static String getLabel(Object elementValue) { | ||
| if (elementValue instanceof RowVertex) { | ||
| return ((RowVertex) elementValue).getLabel(); | ||
| } | ||
| if (elementValue instanceof RowEdge) { | ||
| return ((RowEdge) elementValue).getLabel(); | ||
| } | ||
| throw new IllegalArgumentException( | ||
| "First operand of labeled predicate must be a vertex or an edge, got: " | ||
| + elementValue.getClass().getName()); | ||
| } | ||
| } |
74 changes: 74 additions & 0 deletions
74
...n/src/test/java/org/apache/geaflow/dsl/common/function/LabeledPredicateFunctionsTest.java
This file contains hidden or 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,74 @@ | ||
| /* | ||
| * 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.common.function; | ||
|
|
||
| import org.apache.geaflow.dsl.common.data.RowEdge; | ||
| import org.apache.geaflow.dsl.common.data.RowVertex; | ||
| import org.apache.geaflow.dsl.common.data.impl.types.ObjectEdge; | ||
| import org.apache.geaflow.dsl.common.data.impl.types.ObjectVertex; | ||
| import org.testng.Assert; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| public class LabeledPredicateFunctionsTest { | ||
|
|
||
| private RowVertex vertex(String label) { | ||
| ObjectVertex vertex = new ObjectVertex(1L); | ||
| vertex.setLabel(label); | ||
| return vertex; | ||
| } | ||
|
|
||
| private RowEdge edge(String label) { | ||
| ObjectEdge edge = new ObjectEdge(1L, 2L); | ||
| edge.setLabel(label); | ||
| return edge; | ||
| } | ||
|
|
||
| @Test | ||
| public void testIsLabeledOnVertex() { | ||
| Assert.assertTrue(LabeledPredicateFunctions.isLabeled(vertex("person"), "person")); | ||
| Assert.assertFalse(LabeledPredicateFunctions.isLabeled(vertex("person"), "software")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testIsLabeledOnEdge() { | ||
| Assert.assertTrue(LabeledPredicateFunctions.isLabeled(edge("knows"), "knows")); | ||
| Assert.assertFalse(LabeledPredicateFunctions.isLabeled(edge("knows"), "created")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testIsNotLabeled() { | ||
| Assert.assertFalse(LabeledPredicateFunctions.isNotLabeled(vertex("person"), "person")); | ||
| Assert.assertTrue(LabeledPredicateFunctions.isNotLabeled(vertex("person"), "software")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testThreeValuedLogicWithNullOperand() { | ||
| // Null element or null label yields Unknown (null) for both predicates. | ||
| Assert.assertNull(LabeledPredicateFunctions.isLabeled(null, "person")); | ||
| Assert.assertNull(LabeledPredicateFunctions.isLabeled(vertex("person"), null)); | ||
| Assert.assertNull(LabeledPredicateFunctions.isNotLabeled(null, "person")); | ||
| Assert.assertNull(LabeledPredicateFunctions.isNotLabeled(vertex("person"), null)); | ||
| } | ||
|
|
||
| @Test(expectedExceptions = IllegalArgumentException.class) | ||
| public void testNonGraphElementIsRejected() { | ||
| LabeledPredicateFunctions.isLabeled("not a graph element", "person"); | ||
| } | ||
| } |
This file contains hidden or 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
81 changes: 81 additions & 0 deletions
81
...-dsl/geaflow-dsl-plan/src/main/java/org/apache/geaflow/dsl/udf/table/other/IsLabeled.java
This file contains hidden or 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,81 @@ | ||
| /* | ||
| * 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.geaflow.dsl.common.data.RowEdge; | ||
| import org.apache.geaflow.dsl.common.data.RowVertex; | ||
| import org.apache.geaflow.dsl.common.function.Description; | ||
| import org.apache.geaflow.dsl.common.function.LabeledPredicateFunctions; | ||
| import org.apache.geaflow.dsl.common.function.UDF; | ||
|
|
||
| /** | ||
| * UDF implementation for the ISO-GQL IS LABELED predicate. | ||
| * | ||
| * <p>Implements ISO-GQL Section 19.9: <labeled predicate> | ||
| * | ||
| * <p><b>Syntax:</b></p> | ||
| * <pre> | ||
| * IS_LABELED(element, label) | ||
| * </pre> | ||
| * | ||
| * <p><b>Semantics:</b></p> | ||
| * Returns TRUE if the vertex or edge has the given label, FALSE if not, or NULL if either | ||
| * operand is NULL. | ||
| * | ||
| * <p><b>Example:</b></p> | ||
| * <pre> | ||
| * MATCH (a) -[e]-> (b) | ||
| * WHERE IS_LABELED(a, 'person') | ||
| * RETURN a, e, b | ||
| * </pre> | ||
| */ | ||
| @Description( | ||
| name = "is_labeled", | ||
| description = "ISO-GQL Labeled Predicate: Returns TRUE if the vertex or edge has the given " | ||
| + "label, FALSE if not, NULL if either operand is NULL. Follows ISO-GQL three-valued logic." | ||
| ) | ||
| public class IsLabeled extends UDF { | ||
|
|
||
| /** | ||
| * Evaluates the IS LABELED predicate. | ||
| * | ||
| * @param elementValue vertex or edge to check | ||
| * @param labelValue label name to test for | ||
| * @return Boolean: true if the element has the given label, false if not, null if either | ||
| * operand is null | ||
| */ | ||
| public Boolean eval(Object elementValue, Object labelValue) { | ||
| return LabeledPredicateFunctions.isLabeled(elementValue, labelValue); | ||
| } | ||
|
|
||
| /** | ||
| * Type-specific overload for vertices. | ||
| */ | ||
| public Boolean eval(RowVertex vertex, String label) { | ||
| return LabeledPredicateFunctions.isLabeled(vertex, label); | ||
| } | ||
|
|
||
| /** | ||
| * Type-specific overload for edges. | ||
| */ | ||
| public Boolean eval(RowEdge edge, String label) { | ||
| return LabeledPredicateFunctions.isLabeled(edge, label); | ||
| } | ||
| } |
82 changes: 82 additions & 0 deletions
82
...l/geaflow-dsl-plan/src/main/java/org/apache/geaflow/dsl/udf/table/other/IsNotLabeled.java
This file contains hidden or 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,82 @@ | ||
| /* | ||
| * 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.geaflow.dsl.common.data.RowEdge; | ||
| import org.apache.geaflow.dsl.common.data.RowVertex; | ||
| import org.apache.geaflow.dsl.common.function.Description; | ||
| import org.apache.geaflow.dsl.common.function.LabeledPredicateFunctions; | ||
| import org.apache.geaflow.dsl.common.function.UDF; | ||
|
|
||
| /** | ||
| * UDF implementation for the ISO-GQL IS NOT LABELED predicate. | ||
| * | ||
| * <p>Implements ISO-GQL Section 19.9: <labeled predicate> | ||
| * | ||
| * <p><b>Syntax:</b></p> | ||
| * <pre> | ||
| * IS_NOT_LABELED(element, label) | ||
| * </pre> | ||
| * | ||
| * <p><b>Semantics:</b></p> | ||
| * Returns TRUE if the vertex or edge does NOT have the given label, FALSE if it does, or NULL | ||
| * if either operand is NULL. | ||
| * | ||
| * <p><b>Example:</b></p> | ||
| * <pre> | ||
| * MATCH (a) -[e]-> (b) | ||
| * WHERE IS_NOT_LABELED(a, 'software') | ||
| * RETURN a, e, b | ||
| * </pre> | ||
| */ | ||
| @Description( | ||
| name = "is_not_labeled", | ||
| description = "ISO-GQL Labeled Predicate: Returns TRUE if the vertex or edge does NOT have " | ||
| + "the given label, FALSE if it does, NULL if either operand is NULL. Follows ISO-GQL " | ||
| + "three-valued logic." | ||
| ) | ||
| public class IsNotLabeled extends UDF { | ||
|
|
||
| /** | ||
| * Evaluates the IS NOT LABELED predicate. | ||
| * | ||
| * @param elementValue vertex or edge to check | ||
| * @param labelValue label name to test for | ||
| * @return Boolean: true if the element does NOT have the given label, false if it does, null | ||
| * if either operand is null | ||
| */ | ||
| public Boolean eval(Object elementValue, Object labelValue) { | ||
| return LabeledPredicateFunctions.isNotLabeled(elementValue, labelValue); | ||
| } | ||
|
|
||
| /** | ||
| * Type-specific overload for vertices. | ||
| */ | ||
| public Boolean eval(RowVertex vertex, String label) { | ||
| return LabeledPredicateFunctions.isNotLabeled(vertex, label); | ||
| } | ||
|
|
||
| /** | ||
| * Type-specific overload for edges. | ||
| */ | ||
| public Boolean eval(RowEdge edge, String label) { | ||
| return LabeledPredicateFunctions.isNotLabeled(edge, label); | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
...l-runtime/src/test/java/org/apache/geaflow/dsl/runtime/query/GQLLabeledPredicateTest.java
This file contains hidden or 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,35 @@ | ||
| /* | ||
| * 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.runtime.query; | ||
|
|
||
| import org.testng.annotations.Test; | ||
|
|
||
| public class GQLLabeledPredicateTest { | ||
|
|
||
| @Test | ||
| public void testLabeledPredicate_001() throws Exception { | ||
| QueryTester | ||
| .build() | ||
| .withGraphDefine("/query/modern_graph.sql") | ||
| .withQueryPath("/query/gql_labeled_predicate_001.sql") | ||
| .execute() | ||
| .checkSinkResult(); | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
...w/geaflow-dsl/geaflow-dsl-runtime/src/test/resources/expect/gql_labeled_predicate_001.txt
This file contains hidden or 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,6 @@ | ||
| 1,2,true,false,true,false | ||
| 1,3,true,true,true,true | ||
| 1,4,true,false,true,false | ||
| 4,3,true,true,true,true | ||
| 4,5,true,true,true,true | ||
| 6,3,true,true,true,true |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This package is intended for general-purpose interfaces. There is no need to split the current function into a separate
Func; I suggest placing it directly into the UDF.