cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ntimof...@apache.org
Subject [cayenne] branch master updated: New select translator lost warning about a joint prefetch + limit
Date Mon, 18 May 2020 10:07:38 GMT
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/master by this push:
     new 90553be  New select translator lost warning about a joint prefetch + limit
90553be is described below

commit 90553be6dc187a82360e82c214cd8fe8a56dbd40
Author: Nikita Timofeev <stariy95@gmail.com>
AuthorDate: Mon May 18 13:07:25 2020 +0300

    New select translator lost warning about a joint prefetch + limit
---
 .../translator/select/PrefetchNodeStage.java       | 22 ++++++++++++++++++++--
 .../org/apache/cayenne/access/JointPrefetchIT.java |  2 +-
 2 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/translator/select/PrefetchNodeStage.java
b/cayenne-server/src/main/java/org/apache/cayenne/access/translator/select/PrefetchNodeStage.java
index 885c916..3aa795d 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/access/translator/select/PrefetchNodeStage.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/access/translator/select/PrefetchNodeStage.java
@@ -32,8 +32,11 @@ import org.apache.cayenne.map.ObjEntity;
 import org.apache.cayenne.map.ObjRelationship;
 import org.apache.cayenne.query.PrefetchSelectQuery;
 import org.apache.cayenne.query.PrefetchTreeNode;
+import org.apache.cayenne.query.QueryMetadata;
 import org.apache.cayenne.query.Select;
 import org.apache.cayenne.reflect.ClassDescriptor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import static org.apache.cayenne.access.sqlbuilder.SQLBuilder.table;
 
@@ -42,6 +45,8 @@ import static org.apache.cayenne.access.sqlbuilder.SQLBuilder.table;
  */
 class PrefetchNodeStage implements TranslationStage {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(SelectTranslator.class);
+
     @Override
     public void perform(TranslatorContext context) {
         updatePrefetchNodes(context);
@@ -62,12 +67,14 @@ class PrefetchNodeStage implements TranslationStage {
     }
 
     private void processJoint(TranslatorContext context) {
-        PrefetchTreeNode prefetch = context.getMetadata().getPrefetchTree();
+        QueryMetadata queryMetadata = context.getMetadata();
+        PrefetchTreeNode prefetch = queryMetadata.getPrefetchTree();
         if(prefetch == null) {
             return;
         }
 
-        ObjEntity objEntity = context.getMetadata().getObjEntity();
+        ObjEntity objEntity = queryMetadata.getObjEntity();
+        boolean warnPrefetchWithLimit = false;
 
         for(PrefetchTreeNode node : prefetch.adjacentJointNodes()) {
             Expression prefetchExp = ExpressionFactory.exp(node.getPath());
@@ -94,6 +101,17 @@ class PrefetchNodeStage implements TranslationStage {
 
             DescriptorColumnExtractor columnExtractor = new DescriptorColumnExtractor(context,
prefetchClassDescriptor);
             columnExtractor.extract("p:" + dbPath);
+
+            if(!warnPrefetchWithLimit && targetRel.isToMany()
+                    && (queryMetadata.getFetchLimit() > 0 || queryMetadata.getFetchOffset()
> 0)) {
+                warnPrefetchWithLimit = true;
+            }
+        }
+
+        // warn about a potentially faulty joint prefetch + limit combination
+        if(warnPrefetchWithLimit) {
+            LOGGER.warn("The query uses both limit/offset and a joint prefetch, this most
probably will lead to an incorrect result. " +
+                    "Either use disjointById prefetch or get a full result set.");
         }
     }
 
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/access/JointPrefetchIT.java b/cayenne-server/src/test/java/org/apache/cayenne/access/JointPrefetchIT.java
index 18ad1d8..1f34af5 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/access/JointPrefetchIT.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/access/JointPrefetchIT.java
@@ -130,7 +130,7 @@ public class JointPrefetchIT extends ServerCase {
 
         final List<Artist> objects = ObjectSelect.query(Artist.class)
                 .limit(2).offset(0)
-                .orderBy("db:ARTIST_ID", SortOrder.ASCENDING)
+                .orderBy(Artist.ARTIST_ID_PK_PROPERTY.asc())
                 .prefetch(Artist.PAINTING_ARRAY.joint())
                 .select(context);
 


Mime
View raw message