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

2024-02-16 Thread via GitHub


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

   @Jugen Yes, just to keep everything neat


-- 
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-16 Thread via GitHub


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

   So do I squash my side and then FORCE push ?


-- 
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-16 Thread via GitHub


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

   @Jugen I think we are finally ready to merge it to 5.0 branch. We could fine 
tune it after if needed. Could you please squash commits here?


-- 
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-16 Thread via GitHub


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


##
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:
   Ok, so it's a bit fragile but should hold for now. 



-- 
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-16 Thread via GitHub


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


##
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:
   Actually I'm wrong wrt my last example because Cayenne wraps the expression 
in brackets like so:
   `SELECT DISTINCT ( column + 1 ) FROM table ORDER BY column` so we then get:
   ```
   "( column + 1 ) ".startsWith("column ") -> false
   ```
   and the resulting SQL will be: `SELECT DISTINCT ( column + 1 ), column FROM 
table ORDER BY column`



-- 
This is an automated message from the Apache Git Service.

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

2024-02-16 Thread via GitHub


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


##
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:
   But reversing your example will fail for: `SELECT DISTINCT column + 1 FROM 
table ORDER BY column` we would get:
   ```
   "column + 1 ".startsWith("column ") -> true
   ```
   and so produces the following SQL: `SELECT DISTINCT column + 1 FROM table 
ORDER BY column`
   which fails !



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

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

2024-02-16 Thread via GitHub


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


##
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:
   Yes this case should be ok. But I suppose the opposite case will be 
incorrect. I.e. `SELECT DISTINCT column + 1 FROM table ORDER BY column` will 
result in `"column + 1 ".startsWith("column ") -> true`
   



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

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

2024-02-16 Thread via GitHub


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


##
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:
   So for `SELECT DISTINCT column FROM table ORDER BY column + 1` we would get:
   ```
   "column ".startsWith("column + 1 ") -> false
   ```
   and so produces the following SQL: `SELECT DISTINCT column, column + 1 FROM 
table ORDER BY column + 1`
   which is correct isn't ?



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

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

2024-02-16 Thread via GitHub


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


##
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:
   Yeah, and `DbAttribute` filter should help too. Probably we could still get 
wrong results with complex orderings. Like `SELECT column FROM table ORDER BY 
column + 1`



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

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

2024-02-16 Thread via GitHub


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


##
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:
   Note that in `getSqlString` a trailing space is appended in order to prevent 
false positives like 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:

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

2024-02-16 Thread via GitHub


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


##
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 wonder if this check could return false positive result in case of some 
similar column names, like `item` and `item_code`?



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