zstan commented on code in PR #13389:
URL: https://github.com/apache/ignite/pull/13389#discussion_r4423346850


##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/metadata/IgniteMdRowCount.java:
##########
@@ -157,4 +489,129 @@ public double getRowCount(IgniteLimit rel, 
RelMetadataQuery mq) {
     public double getRowCount(IgniteTableModify rel, RelMetadataQuery mq) {
         return rel.estimateRowCount(mq);
     }
+
+    /** */
+    private record TablesPair(RelOptTable left, RelOptTable right) { }
+
+    /** */
+    private static class JoinContext {
+        /** Used columns of primary key of table from left side. */
+        private final BitSet leftKeys;
+
+        /** Used columns of primary key of table from right side. */
+        private final BitSet rightKeys;
+
+        /**
+         * Used columns of primary key in both tables.
+         *
+         * <p>This bitset is initialized when PK of both tables has equal 
columns count, and
+         * bits are cleared when join pair contains columns with equal 
positions in PK of corresponding
+         * table. For example, having tables T1 and T2 with primary keys in 
both tables defined as
+         * CONSTRAINT PRIMARY KEY (a, b), in case of query
+         * {@code SELECT ... FROM t1 JOIN t2 ON t1.a = t2.a AND t1.b = t2.b} 
commonKeys will be initialized
+         * and cleared, but in case of query {@code SELECT ... FROM t1 JOIN t2 
ON t1.a = t2.b AND t1.b = t2.a}
+         * (mind the join condition, where column A of one table compared with 
column B of another), will be
+         * only initialized (since size of the primary keys are equal), but 
not cleared.
+         */
+        private final @Nullable BitSet commonKeys;
+
+        /** Whether primary key columns of both tables are known. */
+        private final boolean pkKnown;
+
+        /** */
+        JoinContext(int leftPkSize, int rightPkSize) {
+            pkKnown = leftPkSize > 0 && rightPkSize > 0;
+
+            leftKeys = new BitSet();
+            rightKeys = new BitSet();
+            commonKeys = leftPkSize == rightPkSize && leftPkSize != 0 ? new 
BitSet() : null;
+
+            leftKeys.set(0, leftPkSize);
+            rightKeys.set(0, rightPkSize);
+
+            if (commonKeys != null) {
+                assert leftPkSize == rightPkSize;
+
+                commonKeys.set(0, leftPkSize);
+            }
+        }
+
+        /** */
+        void countKeys(KeyColumnOrigin left, KeyColumnOrigin right) {
+            if (left.positionInKey >= 0) {
+                leftKeys.clear(left.positionInKey);
+            }
+
+            if (right.positionInKey >= 0) {
+                rightKeys.clear(right.positionInKey);
+            }
+
+            if (commonKeys != null && left.positionInKey == 
right.positionInKey && left.positionInKey >= 0) {
+                commonKeys.clear(left.positionInKey);
+            }
+        }
+
+        /** */
+        JoiningRelationType joinType() {
+            if (!pkKnown) {
+                return JoiningRelationType.UNKNOWN;
+            }
+
+            if (commonKeys != null && commonKeys.isEmpty()) {

Review Comment:
   done



##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/metadata/IgniteMdRowCount.java:
##########
@@ -157,4 +489,129 @@ public double getRowCount(IgniteLimit rel, 
RelMetadataQuery mq) {
     public double getRowCount(IgniteTableModify rel, RelMetadataQuery mq) {
         return rel.estimateRowCount(mq);
     }
+
+    /** */
+    private record TablesPair(RelOptTable left, RelOptTable right) { }
+
+    /** */
+    private static class JoinContext {
+        /** Used columns of primary key of table from left side. */
+        private final BitSet leftKeys;
+
+        /** Used columns of primary key of table from right side. */
+        private final BitSet rightKeys;
+
+        /**
+         * Used columns of primary key in both tables.
+         *
+         * <p>This bitset is initialized when PK of both tables has equal 
columns count, and
+         * bits are cleared when join pair contains columns with equal 
positions in PK of corresponding
+         * table. For example, having tables T1 and T2 with primary keys in 
both tables defined as
+         * CONSTRAINT PRIMARY KEY (a, b), in case of query
+         * {@code SELECT ... FROM t1 JOIN t2 ON t1.a = t2.a AND t1.b = t2.b} 
commonKeys will be initialized
+         * and cleared, but in case of query {@code SELECT ... FROM t1 JOIN t2 
ON t1.a = t2.b AND t1.b = t2.a}
+         * (mind the join condition, where column A of one table compared with 
column B of another), will be
+         * only initialized (since size of the primary keys are equal), but 
not cleared.
+         */
+        private final @Nullable BitSet commonKeys;
+
+        /** Whether primary key columns of both tables are known. */
+        private final boolean pkKnown;
+
+        /** */
+        JoinContext(int leftPkSize, int rightPkSize) {
+            pkKnown = leftPkSize > 0 && rightPkSize > 0;
+
+            leftKeys = new BitSet();
+            rightKeys = new BitSet();
+            commonKeys = leftPkSize == rightPkSize && leftPkSize != 0 ? new 
BitSet() : null;
+
+            leftKeys.set(0, leftPkSize);
+            rightKeys.set(0, rightPkSize);
+
+            if (commonKeys != null) {
+                assert leftPkSize == rightPkSize;
+
+                commonKeys.set(0, leftPkSize);
+            }
+        }
+
+        /** */
+        void countKeys(KeyColumnOrigin left, KeyColumnOrigin right) {
+            if (left.positionInKey >= 0) {
+                leftKeys.clear(left.positionInKey);
+            }
+
+            if (right.positionInKey >= 0) {
+                rightKeys.clear(right.positionInKey);
+            }
+
+            if (commonKeys != null && left.positionInKey == 
right.positionInKey && left.positionInKey >= 0) {
+                commonKeys.clear(left.positionInKey);
+            }
+        }
+
+        /** */
+        JoiningRelationType joinType() {
+            if (!pkKnown) {
+                return JoiningRelationType.UNKNOWN;
+            }
+
+            if (commonKeys != null && commonKeys.isEmpty()) {
+                return JoiningRelationType.PK_ON_PK;
+            }
+
+            if (rightKeys.isEmpty()) {

Review Comment:
   done



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to