Jugen commented on code in PR #605: URL: https://github.com/apache/cayenne/pull/605#discussion_r1516017214
########## 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<DbAttribute> 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: @stariy95 Have submitted PR #610 (ready for review) against [CAY-2847](https://issues.apache.org/jira/browse/CAY-2847) to address this. -- 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