#2813 - Sometimes subquery use wrong alias in SQL

The reason for this is that as part of DefaultOrmQuery.copy() it uses
DefaultExpressionList.copy() and that assumed that expressions were
safe to share which is NOT the case for IN and EXISTS sub-query expressions
so InQueryExpression and ExistsQueryExpression

The effective fix for this is that DefaultExpressionList.copy() changes
to call SpiExpression.copy() and for InQueryExpression and ExistsQueryExpression
to implement that copy() by creating a copy of the sub-query.

A "side-fix" is that in DefaultOrmQuery.createExtraJoinsToSupportManyWhereClause()
it was creating an instance of ManyWhereJoins, then mutating it ... and if we
change that to only doing the assignment at the end (object assignment is atomic)
then racy access reading ManyWhereJoins would always get a fully completed non-mutating
instance of ManyWhereJoins. Noting this because it kind of points to where I think
the race condition is (in createExtraJoinsToSupportManyWhereClause()) but noting that
with the change to DefaultExpressionList.copy() this "side-fix" isn't required per say.
This commit is contained in:
Rob Bygrave committed 2022-08-31 17:24:55 +12:00
1 parent f07c7411fa
commit 29e2b81092
12 files changed
+110 -28

No files matched your search

@@ -50,7 +50,6 @@ public final class ManyWhereJoins implements Serializable {
* Add a many where join.
*/
public void add(ElPropertyDeploy elProp) {
String join = elProp.elPrefix();
BeanProperty p = elProp.beanProperty();
if (p instanceof BeanPropertyAssocMany<?>) {
@@ -111,7 +110,6 @@ public final class ManyWhereJoins implements Serializable {
* Return the set of property names for the many where joins.
*/
public TreeSet<String> getPropertyNames() {
TreeSet<String> propertyNames = new TreeSet<>();
for (PropertyJoin join : joins.values()) {
propertyNames.add(join.getProperty());
@@ -110,4 +110,11 @@ public interface SpiExpression extends Expression {
* Apply property prefix when filterMany expressions included into main query.
*/
void prefixProperty(String path);
/**
* Return a copy of the expression (as part of creating a query copy).
*/
default SpiExpression copy() {
return this;
}
}
@@ -76,11 +76,7 @@ public class DefaultExpressionList<T> implements SpiExpressionList<T> {
}
public DefaultExpressionList(Query<T> query, ExpressionList<T> parentExprList) {
this(query, query.getExpressionFactory(), parentExprList);
}
DefaultExpressionList(Query<T> query, ExpressionFactory expr, ExpressionList<T> parentExprList) {
this(query, expr, parentExprList, new ArrayList<>());
this(query, query.getExpressionFactory(), parentExprList, new ArrayList<>());
}
DefaultExpressionList(Query<T> query, ExpressionFactory expr, ExpressionList<T> parentExprList, List<SpiExpression> list) {
@@ -243,13 +239,12 @@ public class DefaultExpressionList<T> implements SpiExpressionList<T> {
/**
* Return a copy of the expression list.
* <p>
* Each of the expressions are expected to be immutable and safe to reference.
* </p>
*/
public DefaultExpressionList<T> copy(Query<T> query) {
DefaultExpressionList<T> copy = new DefaultExpressionList<>(query, expr, null);
copy.list.addAll(list);
DefaultExpressionList<T> copy = new DefaultExpressionList<>(query, expr, null, new ArrayList<>(list.size()));
for (SpiExpression expr : list) {
copy.list.add(expr.copy());
}
return copy;
}
@@ -27,6 +27,11 @@ final class ExistsQueryExpression implements SpiExpression, UnsupportedDocStoreE
this.subQuery = null;
}
@Override
public SpiExpression copy() {
return subQuery == null ? this : new ExistsQueryExpression(subQuery.copy(), not);
}
@Override
public void prefixProperty(String path) {
// do nothing
@@ -27,7 +27,7 @@ public final class FilterExpressionList<T> extends DefaultExpressionList<T> {
}
public FilterExpressionList(FilterExprPath pathPrefix, ExpressionFactory expr, Query<T> rootQuery) {
super(null, expr, null);
super(null, expr, null, new ArrayList<>());
this.pathPrefix = pathPrefix;
this.rootQuery = rootQuery;
}
@@ -31,6 +31,11 @@ final class InQueryExpression extends AbstractExpression implements UnsupportedD
this.bindParams = bindParams;
}
@Override
public SpiExpression copy() {
return subQuery == null ? this : new InQueryExpression(propName, subQuery.copy(), not);
}
@Override
public void simplify() {
// do nothing
@@ -43,7 +48,6 @@ final class InQueryExpression extends AbstractExpression implements UnsupportedD
@Override
public void prepareExpression(BeanQueryRequest<?> request) {
CQuery<?> subQuery = compileSubQuery(request);
this.bindParams = subQuery.predicates().whereExprBindValues();
this.sql = subQuery.generatedSql().replace('\n', ' ');
@@ -71,7 +75,6 @@ final class InQueryExpression extends AbstractExpression implements UnsupportedD
@Override
public void addSql(SpiExpressionRequest request) {
request.append(" (").append(propName).append(")");
if (not) {
request.append(" not");
@@ -83,7 +86,6 @@ final class InQueryExpression extends AbstractExpression implements UnsupportedD
@Override
public void addBindValues(SpiExpressionRequest request) {
for (Object bindParam : bindParams) {
request.addBindValue(bindParam);
}
@@ -412,21 +412,22 @@ public class DefaultOrmQuery<T> extends AbstractQuery implements SpiQuery<T> {
}
private void createExtraJoinsToSupportManyWhereClause() {
manyWhereJoins = new ManyWhereJoins();
final var manyWhere = new ManyWhereJoins();
if (whereExpressions != null) {
whereExpressions.containsMany(beanDescriptor, manyWhereJoins);
whereExpressions.containsMany(beanDescriptor, manyWhere);
}
if (havingExpressions != null) {
havingExpressions.containsMany(beanDescriptor, manyWhereJoins);
havingExpressions.containsMany(beanDescriptor, manyWhere);
}
if (orderBy != null) {
for (Property orderProperty : orderBy.getProperties()) {
ElPropertyDeploy elProp = beanDescriptor.elPropertyDeploy(orderProperty.getProperty());
if (elProp != null && elProp.containsFormulaWithJoin()) {
manyWhereJoins.addFormulaWithJoin(elProp.elPrefix(), elProp.name());
manyWhere.addFormulaWithJoin(elProp.elPrefix(), elProp.name());
}
}
}
manyWhereJoins = manyWhere;
}
/**