Re: [PR] CAY-2842 Prevent duplicate select columns when using distinct with order by [cayenne]

2024-02-19 Thread via GitHub


Jugen commented on code in PR #605:
URL: https://github.com/apache/cayenne/pull/605#discussion_r1494864476


##
cayenne/src/main/java/org/apache/cayenne/access/translator/select/OrderingAbstractStage.java:
##
@@ -0,0 +1,107 @@
+/*
+ *   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
+ *
+ *https://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.cayenne.access.translator.select;
+
+import static org.apache.cayenne.access.sqlbuilder.SQLBuilder.*;
+
+import java.util.function.Predicate;
+
+import org.apache.cayenne.access.sqlbuilder.NodeBuilder;
+import org.apache.cayenne.access.sqlbuilder.SQLGenerationVisitor;
+import org.apache.cayenne.access.sqlbuilder.StringBuilderAppendable;
+import org.apache.cayenne.access.sqlbuilder.sqltree.ColumnNode;
+import org.apache.cayenne.access.sqlbuilder.sqltree.FunctionNode;
+import org.apache.cayenne.access.sqlbuilder.sqltree.Node;
+import org.apache.cayenne.access.sqlbuilder.sqltree.NodeType;
+import org.apache.cayenne.access.sqlbuilder.sqltree.SelectResultNode;
+import org.apache.cayenne.access.sqlbuilder.sqltree.SimpleNodeTreeVisitor;
+import org.apache.cayenne.exp.Expression;
+import org.apache.cayenne.exp.parser.ASTAggregateFunctionCall;
+import org.apache.cayenne.map.DbAttribute;
+import org.apache.cayenne.query.Ordering;
+
+abstract class OrderingAbstractStage implements TranslationStage {
+
+protected void processOrdering(QualifierTranslator qualifierTranslator, 
TranslatorContext context, Ordering ordering) {
+Expression orderExp = ordering.getSortSpec();
+NodeBuilder nodeBuilder = 
node(qualifierTranslator.translate(orderExp));
+
+if(ordering.isCaseInsensitive()) {
+nodeBuilder = function("UPPER", nodeBuilder);
+}
+
+// If query is DISTINCT or GROUPING then we need to add the order 
column as a result column
+if (orderColumnAbsent(context, nodeBuilder)) {
+// deepCopy as some DB expect exactly the same expression in 
select and in ordering
+ResultNodeDescriptor descriptor = 
context.addResultNode(nodeBuilder.build().deepCopy());
+if(orderExp instanceof ASTAggregateFunctionCall) {
+descriptor.setAggregate(true);
+}
+}
+}
+
+private DbAttribute getOrderDbAttribute(Node translatedOrderNode)
+{
+DbAttribute[] orderDbAttribute = {null};
+translatedOrderNode.visit(new SimpleNodeTreeVisitor() {
+@Override
+public boolean onNodeStart(Node node) {
+if (node.getType() == NodeType.COLUMN) {
+orderDbAttribute[0] = ((ColumnNode) node).getAttribute();
+return false;
+}
+return true;
+}
+});
+return orderDbAttribute[0];
+}
+
+private boolean orderColumnAbsent(TranslatorContext context, NodeBuilder 
nodeBuilder)
+{
+var orderDbAttribute = getOrderDbAttribute(nodeBuilder.build());
+if (orderDbAttribute == null) return false; // Alias ?
+
+var orderEntity = orderDbAttribute.getEntity().getName();
+var orderColumn = orderDbAttribute.getName();
+
+Predicate columnAndEntity = dba -> dba != null
+&& orderColumn.equals(dba.getName())
+&& 
orderEntity.equals(dba.getEntity().getName());
+
+var orderStr = getSqlString(order(nodeBuilder));
+
+return context.getResultNodeList().stream()
+.filter( result -> columnAndEntity.test(result.getDbAttribute()) )
+.noneMatch( result -> 
getSqlString(node(result.getNode())).startsWith(orderStr) );

Review Comment:
   I think I'll give option 2 a shot first then. It'll take me a couple of days 
though (maybe next week) as I've got some other commitments .



-- 
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: commits-unsubscr...@cayenne.apache.org

For queries about this service, 

Re: [PR] CAY-2842 Prevent duplicate select columns when using distinct with order by [cayenne]

2024-02-19 Thread via GitHub


stariy95 commented on PR #605:
URL: https://github.com/apache/cayenne/pull/605#issuecomment-1952256950

   Thank you @Jugen for this huge effort! Will review everything for the final 
time and merge it soon.


-- 
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: commits-unsubscr...@cayenne.apache.org

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



Re: [PR] CAY-2842 Prevent duplicate select columns when using distinct with order by [cayenne]

2024-02-19 Thread via GitHub


stariy95 commented on code in PR #605:
URL: https://github.com/apache/cayenne/pull/605#discussion_r1494406514


##
cayenne/src/main/java/org/apache/cayenne/access/translator/select/OrderingAbstractStage.java:
##
@@ -0,0 +1,107 @@
+/*
+ *   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
+ *
+ *https://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.cayenne.access.translator.select;
+
+import static org.apache.cayenne.access.sqlbuilder.SQLBuilder.*;
+
+import java.util.function.Predicate;
+
+import org.apache.cayenne.access.sqlbuilder.NodeBuilder;
+import org.apache.cayenne.access.sqlbuilder.SQLGenerationVisitor;
+import org.apache.cayenne.access.sqlbuilder.StringBuilderAppendable;
+import org.apache.cayenne.access.sqlbuilder.sqltree.ColumnNode;
+import org.apache.cayenne.access.sqlbuilder.sqltree.FunctionNode;
+import org.apache.cayenne.access.sqlbuilder.sqltree.Node;
+import org.apache.cayenne.access.sqlbuilder.sqltree.NodeType;
+import org.apache.cayenne.access.sqlbuilder.sqltree.SelectResultNode;
+import org.apache.cayenne.access.sqlbuilder.sqltree.SimpleNodeTreeVisitor;
+import org.apache.cayenne.exp.Expression;
+import org.apache.cayenne.exp.parser.ASTAggregateFunctionCall;
+import org.apache.cayenne.map.DbAttribute;
+import org.apache.cayenne.query.Ordering;
+
+abstract class OrderingAbstractStage implements TranslationStage {
+
+protected void processOrdering(QualifierTranslator qualifierTranslator, 
TranslatorContext context, Ordering ordering) {
+Expression orderExp = ordering.getSortSpec();
+NodeBuilder nodeBuilder = 
node(qualifierTranslator.translate(orderExp));
+
+if(ordering.isCaseInsensitive()) {
+nodeBuilder = function("UPPER", nodeBuilder);
+}
+
+// If query is DISTINCT or GROUPING then we need to add the order 
column as a result column
+if (orderColumnAbsent(context, nodeBuilder)) {
+// deepCopy as some DB expect exactly the same expression in 
select and in ordering
+ResultNodeDescriptor descriptor = 
context.addResultNode(nodeBuilder.build().deepCopy());
+if(orderExp instanceof ASTAggregateFunctionCall) {
+descriptor.setAggregate(true);
+}
+}
+}
+
+private DbAttribute getOrderDbAttribute(Node translatedOrderNode)
+{
+DbAttribute[] orderDbAttribute = {null};
+translatedOrderNode.visit(new SimpleNodeTreeVisitor() {
+@Override
+public boolean onNodeStart(Node node) {
+if (node.getType() == NodeType.COLUMN) {
+orderDbAttribute[0] = ((ColumnNode) node).getAttribute();
+return false;
+}
+return true;
+}
+});
+return orderDbAttribute[0];
+}
+
+private boolean orderColumnAbsent(TranslatorContext context, NodeBuilder 
nodeBuilder)
+{
+var orderDbAttribute = getOrderDbAttribute(nodeBuilder.build());
+if (orderDbAttribute == null) return false; // Alias ?
+
+var orderEntity = orderDbAttribute.getEntity().getName();
+var orderColumn = orderDbAttribute.getName();
+
+Predicate columnAndEntity = dba -> dba != null
+&& orderColumn.equals(dba.getName())
+&& 
orderEntity.equals(dba.getEntity().getName());
+
+var orderStr = getSqlString(order(nodeBuilder));
+
+return context.getResultNodeList().stream()
+.filter( result -> columnAndEntity.test(result.getDbAttribute()) )
+.noneMatch( result -> 
getSqlString(node(result.getNode())).startsWith(orderStr) );

Review Comment:
   I'm ok with either 2 or 3, don't think that the first option is much better 
that the existing solution. 3 is the most universal thing, but 2 should be a 
bit easier, I think. So it's up to you. And again we could do it as a separate 
task.
   



-- 
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