You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@geaflow.apache.org by "kitalkuyo-gita (via GitHub)" <gi...@apache.org> on 2025/09/02 06:31:37 UTC

[PR] feat: support same predicate [geaflow]

kitalkuyo-gita opened a new pull request, #604:
URL: https://github.com/apache/geaflow/pull/604

   ### What changes were proposed in this pull request?
   Related to [issue-368](https://github.com/apache/geaflow/issues/368)
   
   ### How was this PR tested?
   - [ ] Tests have Added for the changes
   - [ ] Production environment verified
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@geaflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@geaflow.apache.org
For additional commands, e-mail: issues-help@geaflow.apache.org


Re: [PR] feat: support same predicate [geaflow]

Posted by "Leomrlin (via GitHub)" <gi...@apache.org>.
Leomrlin commented on code in PR #604:
URL: https://github.com/apache/geaflow/pull/604#discussion_r2321301843


##########
geaflow/geaflow-dsl/geaflow-dsl-parser/src/main/java/org/apache/geaflow/dsl/sqlnode/SqlSamePredicatePattern.java:
##########
@@ -0,0 +1,268 @@
+/*
+ * 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.sqlnode;
+
+import java.util.List;
+import java.util.Objects;
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.SqlSpecialOperator;
+import org.apache.calcite.sql.SqlWriter;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.validate.SqlValidator;
+import org.apache.calcite.sql.validate.SqlValidatorScope;
+import org.apache.calcite.util.ImmutableNullableList;
+
+
+/**
+ * SQL node representing a same predicate pattern in GQL.
+ * This node represents a pattern where two path patterns share a common predicate condition.
+ *
+ * <p>Example: MATCH (a:person) -> (b) | (a:person) -> (c) WHERE SAME(a.age > 25)

Review Comment:
   The SAME predicate is primarily used to assert whether entities in a path are same. Is the EXISTS syntax implemented here?



##########
geaflow/geaflow-dsl/geaflow-dsl-parser/src/main/java/org/apache/geaflow/dsl/sqlnode/SqlSamePredicatePattern.java:
##########
@@ -0,0 +1,268 @@
+/*
+ * 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.sqlnode;
+
+import java.util.List;
+import java.util.Objects;
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.SqlSpecialOperator;
+import org.apache.calcite.sql.SqlWriter;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.validate.SqlValidator;
+import org.apache.calcite.sql.validate.SqlValidatorScope;
+import org.apache.calcite.util.ImmutableNullableList;
+
+
+/**
+ * SQL node representing a same predicate pattern in GQL.
+ * This node represents a pattern where two path patterns share a common predicate condition.
+ *
+ * <p>Example: MATCH (a:person) -> (b) | (a:person) -> (c) WHERE SAME(a.age > 25)
+ *
+ * <p>The left and right path patterns must satisfy the same predicate condition.
+ */
+public class SqlSamePredicatePattern extends SqlCall {
+
+    /**
+     * Operator for same predicate pattern.
+     */
+    public static final SqlOperator OPERATOR = new SqlSpecialOperator("SAME_PREDICATE", SqlKind.OTHER);
+
+    /**
+     * Left path pattern.
+     */
+    private SqlNode left;
+
+    /**
+     * Right path pattern.
+     */
+    private SqlNode right;
+
+    /**
+     * Shared predicate condition that must be satisfied by both path patterns.
+     */
+    private SqlNode predicate;
+
+    /**
+     * Whether to use distinct semantics (true) or union all (false).
+     */
+    private boolean isDistinct;
+
+    /**
+     * Constructor for SqlSamePredicatePattern.
+     *
+     * @param pos parser position
+     * @param left left path pattern
+     * @param right right path pattern
+     * @param predicate shared predicate condition
+     * @param isDistinct whether to use distinct semantics
+     */
+    public SqlSamePredicatePattern(SqlParserPos pos, SqlNode left, SqlNode right,
+                                   SqlNode predicate, boolean isDistinct) {
+        super(pos);
+        this.left = Objects.requireNonNull(left, "left path pattern cannot be null");
+        this.right = Objects.requireNonNull(right, "right path pattern cannot be null");
+        this.predicate = Objects.requireNonNull(predicate, "predicate cannot be null");
+        this.isDistinct = isDistinct;
+    }
+
+    @Override
+    public SqlOperator getOperator() {
+        return OPERATOR;
+    }
+
+    @Override
+    public List<SqlNode> getOperandList() {
+        return ImmutableNullableList.of(left, right, predicate);
+    }
+
+    @Override
+    public void setOperand(int i, SqlNode operand) {
+        switch (i) {
+            case 0:
+                this.left = Objects.requireNonNull(operand, "left path pattern cannot be null");
+                break;
+            case 1:
+                this.right = Objects.requireNonNull(operand, "right path pattern cannot be null");
+                break;
+            case 2:
+                this.predicate = Objects.requireNonNull(operand, "predicate cannot be null");
+                break;
+            default:
+                throw new IllegalArgumentException("Illegal index: " + i);
+        }
+    }
+
+    /**
+     * Get the left path pattern.
+     *
+     * @return left path pattern
+     */
+    public SqlNode getLeft() {
+        return left;
+    }
+
+    /**
+     * Get the right path pattern.
+     *
+     * @return right path pattern
+     */
+    public SqlNode getRight() {
+        return right;
+    }
+
+    /**
+     * Get the shared predicate condition.
+     *
+     * @return predicate condition
+     */
+    public SqlNode getPredicate() {
+        return predicate;
+    }
+
+    /**
+     * Check if this pattern uses distinct semantics.
+     *
+     * @return true if distinct, false if union all
+     */
+    public boolean isDistinct() {
+        return isDistinct;
+    }
+
+    /**
+     * Check if this pattern uses union all semantics.
+     *
+     * @return true if union all, false if distinct
+     */
+    public boolean isUnionAll() {
+        return !isDistinct;
+    }
+
+    /**
+     * Set the left path pattern.
+     *
+     * @param left left path pattern
+     */
+    public void setLeft(SqlNode left) {
+        this.left = Objects.requireNonNull(left, "left path pattern cannot be null");
+    }
+
+    /**
+     * Set the right path pattern.
+     *
+     * @param right right path pattern
+     */
+    public void setRight(SqlNode right) {
+        this.right = Objects.requireNonNull(right, "right path pattern cannot be null");
+    }
+
+    /**
+     * Set the shared predicate condition.
+     *
+     * @param predicate predicate condition
+     */
+    public void setPredicate(SqlNode predicate) {
+        this.predicate = Objects.requireNonNull(predicate, "predicate cannot be null");
+    }
+
+    /**
+     * Set whether to use distinct semantics.
+     *
+     * @param isDistinct true if distinct, false if union all
+     */
+    public void setDistinct(boolean isDistinct) {
+        this.isDistinct = isDistinct;
+    }
+
+    @Override
+    public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
+        // Unparse left path pattern
+        left.unparse(writer, leftPrec, rightPrec);
+
+        // Unparse union operator
+        if (isDistinct) {
+            writer.print(" | ");

Review Comment:
   Here make the corresponding changes.



##########
geaflow/geaflow-dsl/geaflow-dsl-parser/src/main/java/org/apache/geaflow/dsl/sqlnode/SqlSamePredicatePattern.java:
##########
@@ -0,0 +1,268 @@
+/*
+ * 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.sqlnode;
+
+import java.util.List;
+import java.util.Objects;
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.SqlSpecialOperator;
+import org.apache.calcite.sql.SqlWriter;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.validate.SqlValidator;
+import org.apache.calcite.sql.validate.SqlValidatorScope;
+import org.apache.calcite.util.ImmutableNullableList;
+
+
+/**
+ * SQL node representing a same predicate pattern in GQL.
+ * This node represents a pattern where two path patterns share a common predicate condition.
+ *
+ * <p>Example: MATCH (a:person) -> (b) | (a:person) -> (c) WHERE SAME(a.age > 25)
+ *
+ * <p>The left and right path patterns must satisfy the same predicate condition.
+ */
+public class SqlSamePredicatePattern extends SqlCall {
+
+    /**
+     * Operator for same predicate pattern.
+     */
+    public static final SqlOperator OPERATOR = new SqlSpecialOperator("SAME_PREDICATE", SqlKind.OTHER);
+
+    /**
+     * Left path pattern.
+     */
+    private SqlNode left;
+
+    /**
+     * Right path pattern.
+     */
+    private SqlNode right;
+
+    /**
+     * Shared predicate condition that must be satisfied by both path patterns.
+     */
+    private SqlNode predicate;
+
+    /**
+     * Whether to use distinct semantics (true) or union all (false).
+     */
+    private boolean isDistinct;
+
+    /**
+     * Constructor for SqlSamePredicatePattern.
+     *
+     * @param pos parser position
+     * @param left left path pattern
+     * @param right right path pattern
+     * @param predicate shared predicate condition
+     * @param isDistinct whether to use distinct semantics
+     */
+    public SqlSamePredicatePattern(SqlParserPos pos, SqlNode left, SqlNode right,
+                                   SqlNode predicate, boolean isDistinct) {
+        super(pos);
+        this.left = Objects.requireNonNull(left, "left path pattern cannot be null");
+        this.right = Objects.requireNonNull(right, "right path pattern cannot be null");
+        this.predicate = Objects.requireNonNull(predicate, "predicate cannot be null");
+        this.isDistinct = isDistinct;
+    }
+
+    @Override
+    public SqlOperator getOperator() {
+        return OPERATOR;
+    }
+
+    @Override
+    public List<SqlNode> getOperandList() {
+        return ImmutableNullableList.of(left, right, predicate);
+    }
+
+    @Override
+    public void setOperand(int i, SqlNode operand) {
+        switch (i) {
+            case 0:
+                this.left = Objects.requireNonNull(operand, "left path pattern cannot be null");
+                break;
+            case 1:
+                this.right = Objects.requireNonNull(operand, "right path pattern cannot be null");
+                break;
+            case 2:
+                this.predicate = Objects.requireNonNull(operand, "predicate cannot be null");
+                break;
+            default:
+                throw new IllegalArgumentException("Illegal index: " + i);
+        }
+    }
+
+    /**
+     * Get the left path pattern.
+     *
+     * @return left path pattern
+     */
+    public SqlNode getLeft() {
+        return left;
+    }
+
+    /**
+     * Get the right path pattern.
+     *
+     * @return right path pattern
+     */
+    public SqlNode getRight() {
+        return right;
+    }
+
+    /**
+     * Get the shared predicate condition.
+     *
+     * @return predicate condition
+     */
+    public SqlNode getPredicate() {
+        return predicate;
+    }
+
+    /**
+     * Check if this pattern uses distinct semantics.
+     *
+     * @return true if distinct, false if union all
+     */
+    public boolean isDistinct() {

Review Comment:
   The EXISTS sql node doesn't need distinct and union, the copying here is redundant.



##########
geaflow/geaflow-dsl/geaflow-dsl-plan/src/main/java/org/apache/geaflow/dsl/optimize/rule/SamePredicateOptimizationRule.java:
##########
@@ -0,0 +1,78 @@
+/*
+ * 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.optimize.rule;
+
+import java.util.Arrays;
+import java.util.List;
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rex.RexNode;
+import org.apache.geaflow.dsl.rel.match.MatchFilter;
+import org.apache.geaflow.dsl.rel.match.MatchSamePredicate;
+import org.apache.geaflow.dsl.rel.match.MatchUnion;
+import org.apache.geaflow.dsl.util.GQLRexUtil;
+
+/**
+ * Optimization rule for same predicate patterns.
+ * This rule converts MatchSamePredicate to a more efficient MatchUnion + MatchFilter combination.
+ *
+ * The transformation is:
+ * MatchSamePredicate(left, right, condition, distinct) ->
+ * MatchFilter(MatchUnion(left, right, distinct), condition)
+ */
+public class SamePredicateOptimizationRule extends RelOptRule {

Review Comment:
   What does this optimization rule mean?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@geaflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@geaflow.apache.org
For additional commands, e-mail: issues-help@geaflow.apache.org


Re: [PR] feat: support same predicate [geaflow]

Posted by "kitalkuyo-gita (via GitHub)" <gi...@apache.org>.
kitalkuyo-gita commented on PR #604:
URL: https://github.com/apache/geaflow/pull/604#issuecomment-3257189739

   > The SAME predicate is primarily used to assert whether entities in a path are same. Is the EXISTS syntax implemented in this pr?
   > 
   > Here is a syntax definition for reference. `<same predicate> ::= SAME <left paren> <element variable reference> <comma> <element variable reference> [ { <comma> <element variable reference> }... ] <right paren>`
   
   
   I personally think what you said makes some sense. But this is rarely needed in practice because the entities in the path are usually different You can see the following two examples
   
   Expected SAME (Entity Comparison) by yours:
   --Check if a, b, and c are the same entity
   MATCH (a:person) -> (b) | (a:person) -> (c) WHERE SAME(a, b, c)
   --This is rarely needed in practice because the entities in the path are usually different
   
   Actual implementation of SAME (Conditional Sharing):
   --Find people over 25 years old who are connected to both b and c at the same time
   MATCH (a:person) -> (b) | (a:person) -> (c) WHERE SAME(a.age > 25)
   --This is very useful in practice, ensuring that multiple paths meet the same conditions
   
   Exits (path existence):
   --Find people with friends
   MATCH (a:person) WHERE EXISTS (a) -> (b:person)
   --Find people without friends
   MATCH (a:person) WHERE NOT EXISTS (a) -> (b:person)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@geaflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@geaflow.apache.org
For additional commands, e-mail: issues-help@geaflow.apache.org


Re: [PR] feat: support same predicate [geaflow]

Posted by "kitalkuyo-gita (via GitHub)" <gi...@apache.org>.
kitalkuyo-gita commented on PR #604:
URL: https://github.com/apache/geaflow/pull/604#issuecomment-3257169875

   > The SAME predicate is primarily used to assert whether entities in a path are same. Is the EXISTS syntax implemented in this pr?
   > 
   > Here is a syntax definition for reference. `<same predicate> ::= SAME <left paren> <element variable reference> <comma> <element variable reference> [ { <comma> <element variable reference> }... ] <right paren>`
   
   
   - EXISTS Syntax - Fully Implemented Feature
   
   Usage: Check if a match exists for a certain path
   
   Grammar Format:
   EXISTS PathPattern
   
   Actual SQL Example: 
   ```
   -- 检查b节点是否有出边连接到c节点
   MATCH (a:person WHERE id = 1)-[e]->(b)
   WHERE EXISTS (b) -> (c)
   RETURN a, e, b
   
   -- 检查b节点是否有入边连接
   MATCH (a:person WHERE id = 1)-[e]->(b)  
   WHERE EXISTS (b) <- (c:person where id != 1)
   RETURN a, e, b
   
   -- 否定存在性检查
   MATCH (a:person WHERE id = 1)-[e]->(b)
   WHERE NOT EXISTS (b) -> (c)
   RETURN a, e, b
   ```
   
   Test Case Proof : The project already has a complete EXISTS test case 
   - gql_subquery_005.sql
   - gql_subquery_006.sql 
   - gql_subquery_007.sql
   - gql_subquery_009.sql
     
   2. SAME predicate - New features implemented in this PR
   
   Purpose : Share the same conditions among multiple path patterns 
   
   Grammar Format:
   MATCH path1 | path2 WHERE SAME(condition)
   
   Actual SQL Example: 
   ```
   -- 基本用法:两个路径都要求a.age > 25
   MATCH (a:person) -> (b) | (a:person) -> (c) WHERE SAME(a.age > 25)
   RETURN a.id as a_id, a.age as a_age, b.id as b_id, c.id as c_id
   
   -- 支持DISTINCT语义
   MATCH (a:person) -> (b) | (a:person) -> (c) WHERE SAME(a.age > 25) DISTINCT
   RETURN a.id as a_id, a.age as a_age, b.id as b_id, c.id as c_id
   
   -- 支持多个路径模式
   MATCH (a:person) -> (b) | (a:person) -> (c) | (a:person) -> (d) WHERE SAME(a.age > 25)
   RETURN a.id as a_id, a.age as a_age, b.id as b_id, c.id as c_id, d.id as d_id
   
   -- 复杂条件:涉及多个变量的条件
   MATCH (a:person) -> (b) | (a:person) -> (c) WHERE SAME(a.age > 25 AND b.id != c.id)
   RETURN a.id as a_id, a.age as a_age, b.id as b_id, c.id as c_id
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@geaflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@geaflow.apache.org
For additional commands, e-mail: issues-help@geaflow.apache.org


Re: [PR] feat: support same predicate [geaflow]

Posted by "kitalkuyo-gita (via GitHub)" <gi...@apache.org>.
kitalkuyo-gita commented on code in PR #604:
URL: https://github.com/apache/geaflow/pull/604#discussion_r2324202067


##########
geaflow/geaflow-dsl/geaflow-dsl-plan/src/main/java/org/apache/geaflow/dsl/optimize/rule/SamePredicateOptimizationRule.java:
##########
@@ -0,0 +1,78 @@
+/*
+ * 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.optimize.rule;
+
+import java.util.Arrays;
+import java.util.List;
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rex.RexNode;
+import org.apache.geaflow.dsl.rel.match.MatchFilter;
+import org.apache.geaflow.dsl.rel.match.MatchSamePredicate;
+import org.apache.geaflow.dsl.rel.match.MatchUnion;
+import org.apache.geaflow.dsl.util.GQLRexUtil;
+
+/**
+ * Optimization rule for same predicate patterns.
+ * This rule converts MatchSamePredicate to a more efficient MatchUnion + MatchFilter combination.
+ *
+ * The transformation is:
+ * MatchSamePredicate(left, right, condition, distinct) ->
+ * MatchFilter(MatchUnion(left, right, distinct), condition)
+ */
+public class SamePredicateOptimizationRule extends RelOptRule {

Review Comment:
   ## SamePredicateOptimizationRule.java Optimization Rule Detailed Explanation
   
   ### 1. **Core Function of the Optimization Rule**
   
   This optimization rule is used to **convert the SAME predicate pattern into a more efficient execution plan**.
   
   **Conversion Process**:
   ```
   MatchSamePredicate(left, right, condition, distinct)
   ↓ (Optimized)
   MatchFilter(MatchUnion(left, right, distinct), condition)
   ```
   
   ### 2. **Why is this optimization necessary? **
   
   #### **Issues before optimization**:
   - `MatchSamePredicate` is a **special operator**, requiring special execution logic.
   - The executor needs to understand the semantics of the SAME predicate, resulting in high implementation complexity.
   - Difficulty leveraging existing optimization rules (such as predicate pushdown and index optimization).
   
   #### **Benefits after optimization**:
   - Converted to a **standard Union + Filter combination**, simplifying execution logic.
   - Can leverage existing optimization rules and indexes.
   - More standardized execution plans, facilitating further optimization.
   
   ### 3. **Specific Conversion Example**
   
   #### **Original SQL**:
   ```sql
   MATCH (a:person) -> (b) | (a:person) -> (c) WHERE SAME(a.age > 25)
   RETURN a.id, b.id, c.id
   ```
   
   #### **Execution Plan before optimization**:
   ```
   MATCHSamePredicate(
   left: (a:person) -> (c) (b),
   right: (a:person) -> (c),
   condition: a.age > 25,
   distinct: true
   )
   ```
   
   #### **Optimized Execution Plan**:
   ```
   MatchFilter(
   condition: a.age > 25,
   input: MatchUnion(
   inputs: [(a:person) -> (b), (a:person) -> (c)],
   all: false // distinct = true
   )
   )
   ```
   
   ### 4. **Practical Application Examples**
   
   #### **Scenario 1: Basic Condition Sharing**
   ```sql
   -- Find people older than 25 who are both friends and colleagues
   MATCH (a:person) -> (b:friend) | (a:person) -> (c:colleague) WHERE SAME(a.age > 25)
   ```
   
   **Before Optimization**: Requires special handling of the SAME predicate
   **After Optimization**:
   1. First, union the two paths: `(a:person) -> (b:friend)` ∪ `(a:person) -> (c:colleague)`
   2. Then filter: `a.age > 25`
   
   #### **Scenario 2: Sharing Complex Conditions**
   ```sql
   -- Finding People Meeting Complex Conditions
   MATCH (a:person) -> (b) | (a:person) -> (c) WHERE SAME(a.age > 25 AND a.name = 'marko')
   ```
   
   **After**:
   1. Union: `(a:person) -> (b)` ∪ `(a:person) -> (c)`
   2. Filter: `a.age > 25 AND a.name = 'marko'`
   
   #### **Scenario 3: DISTINCT Semantics**
   ```sql
   -- Using DISTINCT Semantics
   MATCH (a:person) -> (b) | (a:person) -> (c) WHERE SAME(a.age > 25 AND a.name = 'marko') (b) | (a:person) -> (c) WHERE SAME(a.age > 25) DISTINCT
   ```
   
   **After optimization**:
   1. Union with DISTINCT: `(a:person) -> (b)` ∪ `(a:person) -> (c)` (de-duplication)
   2. Filter: `a.age > 25`
   
   ### 5. **Specific Benefits of the Optimization**
   
   #### **Performance Optimization**:
   - **Predicate Pushdown**: Conditions can be pushed down before the union, reducing the amount of data.
   - **Index Utilization**: Existing indexes can be leveraged to accelerate condition filtering.
   - **Parallel Execution**: Union operations can execute two paths in parallel.
   
   #### **Code Simplification**:
   - **Executor Simplification**: No special SAME predicate execution logic is required.
   - **Maintainability Improvement**: Standard union and filter operators are used.
   - **Test Simplification**: Existing union and filter test cases can be reused.
   
   ### 6. **Optimization Rule Triggering Conditions**
   
   ```java
   // This rule only applies to nodes of the MatchSamePredicate type.
   super(operand(MatchSamePredicate.class, any()));
   ```
   
   **Triggering Time**:
   - Query optimization phase
   - When the execution plan contains a `MatchSamePredicate` node
   - Automatically applied, no manual intervention required
   
   ### 7. **Cooperation with Other Optimization Rules**
   
   #### **MatchFilterMergeRule**:
   ```java
   // Can further merge multiple filters.
   MatchFilter(MatchFilter(input, condition1), condition2)
   ↓
   MatchFilter(input, condition1 AND condition2)
   ```
   
   #### **Predicate Pushdown Rule**:
   - Can push filter conditions further down to the data source.
   - Reduces the amount of intermediate results.
   
   ### 8. **Actual Execution Performance Comparison**
   
   #### **Before optimization**:
   ```
   Execution plan: MatchSamePredicate
   - Requires special execution logic
   - Difficult to leverage indexes
   - Complex execution path
   ```
   
   #### **After optimization**:
   ```
   Execution plan: MatchFilter -> MatchUnion
   - Uses standard operators
   - Can leverage index optimization
   - Clear execution path
   - Supports further optimization
   ```
   
   This optimization rule embodies the core concept of the **Query Optimizer**: transforming complex semantics into simple, optimizable combinations of standard operators.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@geaflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@geaflow.apache.org
For additional commands, e-mail: issues-help@geaflow.apache.org


Re: [PR] feat: support same predicate [geaflow]

Posted by "kitalkuyo-gita (via GitHub)" <gi...@apache.org>.
kitalkuyo-gita commented on code in PR #604:
URL: https://github.com/apache/geaflow/pull/604#discussion_r2324187751


##########
geaflow/geaflow-dsl/geaflow-dsl-parser/src/main/java/org/apache/geaflow/dsl/sqlnode/SqlSamePredicatePattern.java:
##########
@@ -0,0 +1,268 @@
+/*
+ * 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.sqlnode;
+
+import java.util.List;
+import java.util.Objects;
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.SqlSpecialOperator;
+import org.apache.calcite.sql.SqlWriter;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.validate.SqlValidator;
+import org.apache.calcite.sql.validate.SqlValidatorScope;
+import org.apache.calcite.util.ImmutableNullableList;
+
+
+/**
+ * SQL node representing a same predicate pattern in GQL.
+ * This node represents a pattern where two path patterns share a common predicate condition.
+ *
+ * <p>Example: MATCH (a:person) -> (b) | (a:person) -> (c) WHERE SAME(a.age > 25)

Review Comment:
   You may misunderstand that this feature is used to check whether entities are the same, but in fact, this feature is used to share conditions between multiple path patterns



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@geaflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@geaflow.apache.org
For additional commands, e-mail: issues-help@geaflow.apache.org


Re: [PR] feat: support same predicate [geaflow]

Posted by "kitalkuyo-gita (via GitHub)" <gi...@apache.org>.
kitalkuyo-gita commented on code in PR #604:
URL: https://github.com/apache/geaflow/pull/604#discussion_r2324202067


##########
geaflow/geaflow-dsl/geaflow-dsl-plan/src/main/java/org/apache/geaflow/dsl/optimize/rule/SamePredicateOptimizationRule.java:
##########
@@ -0,0 +1,78 @@
+/*
+ * 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.optimize.rule;
+
+import java.util.Arrays;
+import java.util.List;
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rex.RexNode;
+import org.apache.geaflow.dsl.rel.match.MatchFilter;
+import org.apache.geaflow.dsl.rel.match.MatchSamePredicate;
+import org.apache.geaflow.dsl.rel.match.MatchUnion;
+import org.apache.geaflow.dsl.util.GQLRexUtil;
+
+/**
+ * Optimization rule for same predicate patterns.
+ * This rule converts MatchSamePredicate to a more efficient MatchUnion + MatchFilter combination.
+ *
+ * The transformation is:
+ * MatchSamePredicate(left, right, condition, distinct) ->
+ * MatchFilter(MatchUnion(left, right, distinct), condition)
+ */
+public class SamePredicateOptimizationRule extends RelOptRule {

Review Comment:
   ## Optimization Rule Detailed Explanation
   
   ### 1. **Core Function of the Optimization Rule**
   
   This optimization rule is used to **convert the SAME predicate pattern into a more efficient execution plan**.
   
   **Conversion Process**:
   ```
   MatchSamePredicate(left, right, condition, distinct)
   ↓ (Optimized)
   MatchFilter(MatchUnion(left, right, distinct), condition)
   ```
   
   ### 2. **Why is this optimization necessary? **
   
   #### **Issues before optimization**:
   - `MatchSamePredicate` is a **special operator**, requiring special execution logic.
   - The executor needs to understand the semantics of the SAME predicate, resulting in high implementation complexity.
   - Difficulty leveraging existing optimization rules (such as predicate pushdown and index optimization).
   
   #### **Benefits after optimization**:
   - Converted to a **standard Union + Filter combination**, simplifying execution logic.
   - Can leverage existing optimization rules and indexes.
   - More standardized execution plans, facilitating further optimization.
   
   ### 3. **Specific Conversion Example**
   
   #### **Original SQL**:
   ```sql
   MATCH (a:person) -> (b) | (a:person) -> (c) WHERE SAME(a.age > 25)
   RETURN a.id, b.id, c.id
   ```
   
   #### **Execution Plan before optimization**:
   ```
   MATCHSamePredicate(
   left: (a:person) -> (c) (b),
   right: (a:person) -> (c),
   condition: a.age > 25,
   distinct: true
   )
   ```
   
   #### **Optimized Execution Plan**:
   ```
   MatchFilter(
   condition: a.age > 25,
   input: MatchUnion(
   inputs: [(a:person) -> (b), (a:person) -> (c)],
   all: false // distinct = true
   )
   )
   ```
   
   ### 4. **Practical Application Examples**
   
   #### **Scenario 1: Basic Condition Sharing**
   ```sql
   -- Find people older than 25 who are both friends and colleagues
   MATCH (a:person) -> (b:friend) | (a:person) -> (c:colleague) WHERE SAME(a.age > 25)
   ```
   
   **Before Optimization**: Requires special handling of the SAME predicate
   **After Optimization**:
   1. First, union the two paths: `(a:person) -> (b:friend)` ∪ `(a:person) -> (c:colleague)`
   2. Then filter: `a.age > 25`
   
   #### **Scenario 2: Sharing Complex Conditions**
   ```sql
   -- Finding People Meeting Complex Conditions
   MATCH (a:person) -> (b) | (a:person) -> (c) WHERE SAME(a.age > 25 AND a.name = 'marko')
   ```
   
   **After**:
   1. Union: `(a:person) -> (b)` ∪ `(a:person) -> (c)`
   2. Filter: `a.age > 25 AND a.name = 'marko'`
   
   #### **Scenario 3: DISTINCT Semantics**
   ```sql
   -- Using DISTINCT Semantics
   MATCH (a:person) -> (b) | (a:person) -> (c) WHERE SAME(a.age > 25 AND a.name = 'marko') (b) | (a:person) -> (c) WHERE SAME(a.age > 25) DISTINCT
   ```
   
   **After optimization**:
   1. Union with DISTINCT: `(a:person) -> (b)` ∪ `(a:person) -> (c)` (de-duplication)
   2. Filter: `a.age > 25`
   
   ### 5. **Specific Benefits of the Optimization**
   
   #### **Performance Optimization**:
   - **Predicate Pushdown**: Conditions can be pushed down before the union, reducing the amount of data.
   - **Index Utilization**: Existing indexes can be leveraged to accelerate condition filtering.
   - **Parallel Execution**: Union operations can execute two paths in parallel.
   
   #### **Code Simplification**:
   - **Executor Simplification**: No special SAME predicate execution logic is required.
   - **Maintainability Improvement**: Standard union and filter operators are used.
   - **Test Simplification**: Existing union and filter test cases can be reused.
   
   ### 6. **Optimization Rule Triggering Conditions**
   
   ```java
   // This rule only applies to nodes of the MatchSamePredicate type.
   super(operand(MatchSamePredicate.class, any()));
   ```
   
   **Triggering Time**:
   - Query optimization phase
   - When the execution plan contains a `MatchSamePredicate` node
   - Automatically applied, no manual intervention required
   
   ### 7. **Cooperation with Other Optimization Rules**
   
   #### **MatchFilterMergeRule**:
   ```java
   // Can further merge multiple filters.
   MatchFilter(MatchFilter(input, condition1), condition2)
   ↓
   MatchFilter(input, condition1 AND condition2)
   ```
   
   #### **Predicate Pushdown Rule**:
   - Can push filter conditions further down to the data source.
   - Reduces the amount of intermediate results.
   
   ### 8. **Actual Execution Performance Comparison**
   
   #### **Before optimization**:
   ```
   Execution plan: MatchSamePredicate
   - Requires special execution logic
   - Difficult to leverage indexes
   - Complex execution path
   ```
   
   #### **After optimization**:
   ```
   Execution plan: MatchFilter -> MatchUnion
   - Uses standard operators
   - Can leverage index optimization
   - Clear execution path
   - Supports further optimization
   ```
   
   This optimization rule embodies the core concept of the **Query Optimizer**: transforming complex semantics into simple, optimizable combinations of standard operators.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@geaflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@geaflow.apache.org
For additional commands, e-mail: issues-help@geaflow.apache.org