Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[master] Bug 463042: Concurrency issue with Case expression operator #1359

Merged
merged 1 commit into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 1998, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021 IBM Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -84,6 +85,158 @@ public void _testIsLogicalOperatorTest() {
}
}

public void _testDefaultCaseOperatorDatabaseStringsTest() {
ExpressionOperator caseOp = ExpressionOperator.caseStatement();

String[] databaseStrings = caseOp.getDatabaseStrings(0);
String[] expectedStrings = new String[] {"CASE ", " END"};
if(!(Arrays.equals(expectedStrings, databaseStrings))) {
throw new TestErrorException("Expected " + Arrays.toString(expectedStrings) +
" but was " + Arrays.toString(databaseStrings));
}

databaseStrings = caseOp.getDatabaseStrings(1);
expectedStrings = new String[] {"CASE ", " END"};
if(!(Arrays.equals(expectedStrings, databaseStrings))) {
throw new TestErrorException("Expected " + Arrays.toString(expectedStrings) +
" but was " + Arrays.toString(databaseStrings));
}

databaseStrings = caseOp.getDatabaseStrings(2);
expectedStrings = new String[] {"CASE ", " ELSE ", " END"};
if(!(Arrays.equals(expectedStrings, databaseStrings))) {
throw new TestErrorException("Expected " + Arrays.toString(expectedStrings) +
" but was " + Arrays.toString(databaseStrings));
}

databaseStrings = caseOp.getDatabaseStrings(3);
expectedStrings = new String[] {"CASE ", " WHEN ", " THEN ", " END"};
if(!(Arrays.equals(expectedStrings, databaseStrings))) {
throw new TestErrorException("Expected " + Arrays.toString(expectedStrings) +
" but was " + Arrays.toString(databaseStrings));
}

databaseStrings = caseOp.getDatabaseStrings(4);
expectedStrings = new String[] {"CASE ", " WHEN ", " THEN ", " ELSE ", " END"};
if(!(Arrays.equals(expectedStrings, databaseStrings))) {
throw new TestErrorException("Expected " + Arrays.toString(expectedStrings) +
" but was " + Arrays.toString(databaseStrings));
}

databaseStrings = caseOp.getDatabaseStrings(5);
expectedStrings = new String[] {"CASE ", " WHEN ", " THEN ", " WHEN ", " THEN ", " END"};
if(!(Arrays.equals(expectedStrings, databaseStrings))) {
throw new TestErrorException("Expected " + Arrays.toString(expectedStrings) +
" but was " + Arrays.toString(databaseStrings));
}

databaseStrings = caseOp.getDatabaseStrings(6);
expectedStrings = new String[] {"CASE ", " WHEN ", " THEN ", " WHEN ", " THEN ", " ELSE ", " END"};
if(!(Arrays.equals(expectedStrings, databaseStrings))) {
throw new TestErrorException("Expected " + Arrays.toString(expectedStrings) +
" but was " + Arrays.toString(databaseStrings));
}
}

public void _testDefaultCaseConditionOperatorDatabaseStringsTest() {
ExpressionOperator caseConditionOp = ExpressionOperator.caseConditionStatement();

String[] databaseStrings = caseConditionOp.getDatabaseStrings(0);
String[] expectedStrings = new String[] {"CASE WHEN ", " END "};
if(!(Arrays.equals(expectedStrings, databaseStrings))) {
throw new TestErrorException("Expected " + Arrays.toString(expectedStrings) +
" but was " + Arrays.toString(databaseStrings));
}

databaseStrings = caseConditionOp.getDatabaseStrings(1);
expectedStrings = new String[] {"CASE WHEN ", " END "};
if(!(Arrays.equals(expectedStrings, databaseStrings))) {
throw new TestErrorException("Expected " + Arrays.toString(expectedStrings) +
" but was " + Arrays.toString(databaseStrings));
}

databaseStrings = caseConditionOp.getDatabaseStrings(2);
expectedStrings = new String[] {"CASE WHEN ", " THEN ", " END "};
if(!(Arrays.equals(expectedStrings, databaseStrings))) {
throw new TestErrorException("Expected " + Arrays.toString(expectedStrings) +
" but was " + Arrays.toString(databaseStrings));
}

databaseStrings = caseConditionOp.getDatabaseStrings(3);
expectedStrings = new String[] {"CASE WHEN ", " THEN ", " ELSE ", " END "};
if(!(Arrays.equals(expectedStrings, databaseStrings))) {
throw new TestErrorException("Expected " + Arrays.toString(expectedStrings) +
" but was " + Arrays.toString(databaseStrings));
}

databaseStrings = caseConditionOp.getDatabaseStrings(4);
expectedStrings = new String[] {"CASE WHEN ", " THEN ", " WHEN ", " THEN ", " END "};
if(!(Arrays.equals(expectedStrings, databaseStrings))) {
throw new TestErrorException("Expected " + Arrays.toString(expectedStrings) +
" but was " + Arrays.toString(databaseStrings));
}

databaseStrings = caseConditionOp.getDatabaseStrings(5);
expectedStrings = new String[] {"CASE WHEN ", " THEN ", " WHEN ", " THEN ", " ELSE ", " END "};
if(!(Arrays.equals(expectedStrings, databaseStrings))) {
throw new TestErrorException("Expected " + Arrays.toString(expectedStrings) +
" but was " + Arrays.toString(databaseStrings));
}

databaseStrings = caseConditionOp.getDatabaseStrings(6);
expectedStrings = new String[] {"CASE WHEN ", " THEN ", " WHEN ", " THEN ", " WHEN ", " THEN ", " END "};
if(!(Arrays.equals(expectedStrings, databaseStrings))) {
throw new TestErrorException("Expected " + Arrays.toString(expectedStrings) +
" but was " + Arrays.toString(databaseStrings));
}
}

public void _testDefaultCoalesceOperatorDatabaseStringsTest() {
ExpressionOperator coalesceOp = ExpressionOperator.coalesce();

String[] databaseStrings = coalesceOp.getDatabaseStrings(0);
String[] expectedStrings = new String[] {"COALESCE(", ")"};
if(!(Arrays.equals(expectedStrings, databaseStrings))) {
throw new TestErrorException("Expected " + Arrays.toString(expectedStrings) +
" but was " + Arrays.toString(databaseStrings));
}

databaseStrings = coalesceOp.getDatabaseStrings(1);
expectedStrings = new String[] {"COALESCE(", ")"};
if(!(Arrays.equals(expectedStrings, databaseStrings))) {
throw new TestErrorException("Expected " + Arrays.toString(expectedStrings) +
" but was " + Arrays.toString(databaseStrings));
}

databaseStrings = coalesceOp.getDatabaseStrings(2);
expectedStrings = new String[] {"COALESCE(", ", ", ")"};
if(!(Arrays.equals(expectedStrings, databaseStrings))) {
throw new TestErrorException("Expected " + Arrays.toString(expectedStrings) +
" but was " + Arrays.toString(databaseStrings));
}

databaseStrings = coalesceOp.getDatabaseStrings(3);
expectedStrings = new String[] {"COALESCE(", ", ", ", ", ")"};
if(!(Arrays.equals(expectedStrings, databaseStrings))) {
throw new TestErrorException("Expected " + Arrays.toString(expectedStrings) +
" but was " + Arrays.toString(databaseStrings));
}

databaseStrings = coalesceOp.getDatabaseStrings(4);
expectedStrings = new String[] {"COALESCE(", ", ", ", ", ", ", ")"};
if(!(Arrays.equals(expectedStrings, databaseStrings))) {
throw new TestErrorException("Expected " + Arrays.toString(expectedStrings) +
" but was " + Arrays.toString(databaseStrings));
}

databaseStrings = coalesceOp.getDatabaseStrings(5);
expectedStrings = new String[] {"COALESCE(", ", ", ", ", ", ", ", ", ")"};
if(!(Arrays.equals(expectedStrings, databaseStrings))) {
throw new TestErrorException("Expected " + Arrays.toString(expectedStrings) +
" but was " + Arrays.toString(databaseStrings));
}
}

@Override
public void addTests() {
setManager(PopulationManager.getDefaultManager());
Expand All @@ -95,5 +248,9 @@ public void addTests() {
addTest(new UnitTestCase("IsComparisonOperatorTest"));
addTest(new UnitTestCase("IsFunctionOperatorTest"));
addTest(new UnitTestCase("IsLogicalOperatorTest"));

addTest(new UnitTestCase("DefaultCaseOperatorDatabaseStringsTest"));
addTest(new UnitTestCase("DefaultCaseConditionOperatorDatabaseStringsTest"));
addTest(new UnitTestCase("DefaultCoalesceOperatorDatabaseStringsTest"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ public class ExpressionOperator implements Serializable {
static final long serialVersionUID = -7066100204792043980L;
protected int selector;
protected String name;
protected String[] databaseStrings;

// ListExpressionOperator uses its own start/separator/terminator strings
private String[] databaseStrings;

protected boolean isPrefix = false;
protected boolean isRepeating = false;
protected Class<?> nodeClass;
Expand Down Expand Up @@ -317,7 +320,7 @@ public boolean equals(Object object) {
}
ExpressionOperator operator = (ExpressionOperator) object;
if (getSelector() == 0) {
return Arrays.equals(getDatabaseStrings(), operator.getDatabaseStrings());
return Arrays.equals(getDatabaseStrings(0), operator.getDatabaseStrings(0));
} else {
return getSelector() == operator.getSelector();
}
Expand Down Expand Up @@ -1229,6 +1232,13 @@ public String[] getDatabaseStrings() {
return databaseStrings;
}

/**
* INTERNAL:
*/
public String[] getDatabaseStrings(int arguments) {
return databaseStrings;
}

/**
* INTERNAL:
*/
Expand Down Expand Up @@ -2166,16 +2176,11 @@ public void printCollection(List items, ExpressionSQLPrinter printer) {
if (printer.getPlatform().isDynamicSQLRequiredForFunctions() && !isBindingSupported()) {
printer.getCall().setUsesBinding(false);
}

int dbStringIndex = 0;
try {
if (isPrefix()) {
printer.getWriter().write(getDatabaseStrings()[0]);
dbStringIndex = 1;
} else {
dbStringIndex = 0;
}
} catch (IOException e) {
e.printStackTrace();
if (isPrefix()) {
printer.printString(getDatabaseStrings()[0]);
dbStringIndex = 1;
}

if (argumentIndices == null) {
Expand All @@ -2185,6 +2190,7 @@ public void printCollection(List items, ExpressionSQLPrinter printer) {
}
}

String[] dbStrings = getDatabaseStrings(items.size());
for (final int index : argumentIndices) {
Expression item = (Expression)items.get(index);
if ((this.selector == Ref) || ((this.selector == Deref) && (item.isObjectExpression()))) {
Expand All @@ -2195,8 +2201,8 @@ public void printCollection(List items, ExpressionSQLPrinter printer) {
} else {
item.printSQL(printer);
}
if (dbStringIndex < getDatabaseStrings().length) {
printer.printString(getDatabaseStrings()[dbStringIndex++]);
if (dbStringIndex < dbStrings.length) {
printer.printString(dbStrings[dbStringIndex++]);
}
}
}
Expand Down Expand Up @@ -2225,12 +2231,11 @@ public void printDuo(Expression first, Expression second, ExpressionSQLPrinter p
if (printer.getPlatform().isDynamicSQLRequiredForFunctions() && !isBindingSupported()) {
printer.getCall().setUsesBinding(false);
}
int dbStringIndex;

int dbStringIndex = 0;
if (isPrefix()) {
printer.printString(getDatabaseStrings()[0]);
dbStringIndex = 1;
} else {
dbStringIndex = 0;
}

first.printSQL(printer);
Expand Down Expand Up @@ -2996,11 +3001,12 @@ public static ExpressionOperator toNumber() {
*/
@Override
public String toString() {
if ((getDatabaseStrings() == null) || (getDatabaseStrings().length == 0)) {
String[] dbStrings = getDatabaseStrings();
if ((dbStrings == null) || (dbStrings.length == 0)) {
//CR#... Print a useful name for the missing platform operator.
return "platform operator - " + getPlatformOperatorName(this.selector);
} else {
return "operator " + Arrays.asList(getDatabaseStrings());
return "operator " + Arrays.asList(dbStrings);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 1998, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021 IBM Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -14,6 +15,8 @@
// tware - initial API and implementation from for JPA 2.0 criteria API
package org.eclipse.persistence.expressions;

import java.util.List;

import org.eclipse.persistence.internal.helper.Helper;

/**
Expand All @@ -39,7 +42,6 @@ public class ListExpressionOperator extends ExpressionOperator {
protected String[] startStrings = null;
protected String[] separators = null;
protected String[] terminationStrings = null;
protected int numberOfItems = 0;
protected boolean isComplete = false;

@Override
Expand All @@ -61,35 +63,50 @@ public void copyTo(ExpressionOperator operator){
*/
@Override
public String[] getDatabaseStrings() {
databaseStrings = new String[numberOfItems + 1];
return getDatabaseStrings(0);
}

/**
* Returns an array of Strings that expects a query argument between each String in the array to form the Expression.
* The array is built from the defined startStrings, separators, and terminationStrings.
* Start strings and termination strings take precedence over separator strings.
*
* The first defined start string will be added to the array.
* All subsequent start strings are optional, meaning they will only be added to the array if there are argument spaces available.
*
* The defined set of separator strings will be repeated, as a complete set, as long as there are argument spaces available.
*
* The last defined termination string will be added to the array.
* All antecedent termination strings are optional, meaning they will only be added to the array if there are argument spaces available.
*/
@Override
public String[] getDatabaseStrings(int arguments) {
int i = 0;
while (i < startStrings.length){
databaseStrings[i] = startStrings[i];
String[] databaseStrings = new String[(arguments == 0) ? 2 : arguments + 1];

int start = (arguments < (startStrings.length)) ? databaseStrings.length - 1 : startStrings.length;
for (int j = 0; j < start; j++) {
databaseStrings[i] = startStrings[j];
i++;
}
while (i < numberOfItems - (terminationStrings.length - 1)){
for (int j=0;j<separators.length;j++){
databaseStrings[i] = separators[j];

// '- 1' to save a spot for the guaranteed 1 terminator
int separ = ((databaseStrings.length - start - 1) / separators.length);
for (int j = 0; j < separ; j++) {
for (int k = 0; k < separators.length; k++) {
databaseStrings[i] = separators[k];
i++;
}
}
while (i <= numberOfItems){
for (int j=0;j<terminationStrings.length;j++){
databaseStrings[i] = terminationStrings[j];
i++;
}

int termi = databaseStrings.length - (start + (separ * separators.length));
for (int j = (terminationStrings.length - termi); j < terminationStrings.length; j++) {
databaseStrings[i] = terminationStrings[j];
i++;
}
return databaseStrings;
}

public int getNumberOfItems(){
return numberOfItems;
}

public void setNumberOfItems(int numberOfItems){
this.numberOfItems = numberOfItems;
}

public String[] getStartStrings() {
return startStrings;
}
Expand Down Expand Up @@ -126,10 +143,6 @@ public void setTerminationStrings(String[] terminationStrings){
this.terminationStrings = terminationStrings;
}

public void incrementNumberOfItems(){
numberOfItems++;
}

public void setIsComplete(boolean isComplete){
this.isComplete = isComplete;
}
Expand All @@ -138,6 +151,4 @@ public void setIsComplete(boolean isComplete){
public boolean isComplete() {
return isComplete;
}


}
Loading