Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@
import org.apache.geaflow.dsl.udf.table.other.If;
import org.apache.geaflow.dsl.udf.table.other.IsDecimal;
import org.apache.geaflow.dsl.udf.table.other.IsDestinationOf;
import org.apache.geaflow.dsl.udf.table.other.IsLabeled;
import org.apache.geaflow.dsl.udf.table.other.IsNotDestinationOf;
import org.apache.geaflow.dsl.udf.table.other.IsNotLabeled;
import org.apache.geaflow.dsl.udf.table.other.IsNotSourceOf;
import org.apache.geaflow.dsl.udf.table.other.IsSourceOf;
import org.apache.geaflow.dsl.udf.table.other.Label;
Expand Down Expand Up @@ -226,6 +228,9 @@ public class BuildInSqlFunctionTable extends ListSqlOperatorTable {
.add(GeaFlowFunction.of(IsNotSourceOf.class))
.add(GeaFlowFunction.of(IsDestinationOf.class))
.add(GeaFlowFunction.of(IsNotDestinationOf.class))
// ISO-GQL labeled predicate
.add(GeaFlowFunction.of(IsLabeled.class))
.add(GeaFlowFunction.of(IsNotLabeled.class))
// ISO-GQL property exists predicate
.add(GeaFlowFunction.of(PropertyExists.class))
// UDAF
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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.UDF;

/**
* UDF implementation for the ISO-GQL IS LABELED predicate.
*
* <p>Implements ISO-GQL Section 19.9: &lt;labeled predicate&gt;, which tests whether a
* graph element (vertex or edge) has a given label.
*
* <p><b>Syntax:</b></p>
* <pre>
* IS_LABELED(element, label)
* </pre>
*
* <p><b>Semantics (ISO-GQL three-valued logic):</b></p>
* <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>
*
* <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) {
// 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());
}

/**
* Type-specific overload for vertices.
*/
public Boolean eval(RowVertex vertex, String label) {
return eval((Object) vertex, label);
}

/**
* Type-specific overload for edges.
*/
public Boolean eval(RowEdge edge, String label) {
return eval((Object) edge, label);
}

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());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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.UDF;

/**
* UDF implementation for the ISO-GQL IS NOT LABELED predicate.
*
* <p>Implements ISO-GQL Section 19.9: &lt;labeled predicate&gt;. This is the negation of
* {@link IsLabeled} and delegates to it so the label-extraction logic lives in one place.
*
* <p><b>Syntax:</b></p>
* <pre>
* IS_NOT_LABELED(element, label)
* </pre>
*
* <p><b>Semantics (ISO-GQL three-valued logic):</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 (NOT Unknown = Unknown).
*
* <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 {

private final IsLabeled isLabeled = new IsLabeled();

/**
* 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) {
Boolean result = isLabeled.eval(elementValue, labelValue);
// Three-valued logic: NOT Unknown = Unknown (null remains null).
return result == null ? null : !result;
}

/**
* Type-specific overload for vertices.
*/
public Boolean eval(RowVertex vertex, String label) {
return eval((Object) vertex, label);
}

/**
* Type-specific overload for edges.
*/
public Boolean eval(RowEdge edge, String label) {
return eval((Object) edge, label);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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.data.impl.types.ObjectEdge;
import org.apache.geaflow.dsl.common.data.impl.types.ObjectVertex;
import org.testng.Assert;
import org.testng.annotations.Test;

/**
* Unit tests for the ISO-GQL labeled predicate UDFs {@link IsLabeled} / {@link IsNotLabeled}.
*/
public class LabeledPredicateTest {

private final IsLabeled isLabeled = new IsLabeled();
private final IsNotLabeled isNotLabeled = new IsNotLabeled();

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(isLabeled.eval(vertex("person"), "person"));
Assert.assertFalse(isLabeled.eval(vertex("person"), "software"));
}

@Test
public void testIsLabeledOnEdge() {
Assert.assertTrue(isLabeled.eval(edge("knows"), "knows"));
Assert.assertFalse(isLabeled.eval(edge("knows"), "created"));
}

@Test
public void testIsNotLabeled() {
Assert.assertFalse(isNotLabeled.eval(vertex("person"), "person"));
Assert.assertTrue(isNotLabeled.eval(vertex("person"), "software"));
}

@Test
public void testThreeValuedLogicWithNullOperand() {
// Null element or null label yields Unknown (null) for both predicates.
Assert.assertNull(isLabeled.eval((Object) null, "person"));
Assert.assertNull(isLabeled.eval(vertex("person"), null));
Assert.assertNull(isNotLabeled.eval((Object) null, "person"));
Assert.assertNull(isNotLabeled.eval(vertex("person"), null));
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testNonGraphElementIsRejected() {
isLabeled.eval("not a graph element", "person");
}
}
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();
}
}
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.
*/

-- Test Case 1: IS_LABELED / IS_NOT_LABELED predicate on vertices and edges.
-- For every matched (a)-[e]->(b) triple, evaluate the labeled predicate against
-- the known labels of the modern graph (person / software / knows / created).

CREATE TABLE tbl_result (
a_id bigint,
b_id bigint,
a_is_person boolean,
b_is_software boolean,
a_is_not_software boolean,
e_is_created boolean
) WITH (
type='file',
geaflow.dsl.file.path='${target}'
);

USE GRAPH modern;

INSERT INTO tbl_result
SELECT
a.id,
b.id,
IS_LABELED(a, 'person') as a_is_person,
IS_LABELED(b, 'software') as b_is_software,
IS_NOT_LABELED(a, 'software') as a_is_not_software,
IS_LABELED(e, 'created') as e_is_created
FROM (
MATCH (a) -[e]-> (b)
RETURN a, e, b
)
ORDER BY a.id, b.id
;
Loading