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

FMWK-302 Fix query by primary key written with sendKey false #66

Merged
merged 1 commit into from
Jan 1, 2024
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
10 changes: 10 additions & 0 deletions src/main/java/com/aerospike/jdbc/model/AerospikeQuery.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.aerospike.jdbc.model;

import com.aerospike.client.exp.Exp;
import com.aerospike.client.exp.Expression;
import com.aerospike.jdbc.predicate.QueryPredicate;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -176,6 +178,14 @@ public boolean isIndexable() {
return Objects.nonNull(predicate) && predicate.isIndexable() && Objects.isNull(offset);
}

public Expression toFilterExpression(boolean withPrimaryKey) {
if (predicate == null) {
return null;
}
Exp exp = predicate.toFilterExpression(withPrimaryKey);
return exp == null ? null : Exp.build(exp);
}

@Override
public String toString() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public interface QueryPredicate {

Exp toFilterExpression();
Exp toFilterExpression(boolean withPrimaryKey);

Optional<Filter> toFilter(String binName);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.aerospike.jdbc.predicate;

import com.aerospike.client.exp.Exp;
import com.aerospike.jdbc.util.Constants;

import java.util.Collections;
import java.util.List;

import static com.aerospike.jdbc.util.Constants.PRIMARY_KEY_COLUMN_NAME;

public abstract class QueryPredicateBase implements QueryPredicate {

protected final String binName;
Expand Down Expand Up @@ -64,8 +65,12 @@ protected Exp getValueExp(Object value) {
}
}

protected boolean isPrimaryKeyPredicate() {
return binName.equals(PRIMARY_KEY_COLUMN_NAME);
}

protected Exp buildLeftExp() {
return binName.equals(Constants.PRIMARY_KEY_COLUMN_NAME)
return isPrimaryKeyPredicate()
? Exp.key(valueType)
: Exp.bin(binName, valueType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import java.util.Collections;
import java.util.Optional;

import static com.aerospike.jdbc.util.Constants.PRIMARY_KEY_COLUMN_NAME;

public class QueryPredicateBinary extends QueryPredicateBase {

private final Operator operator;
Expand All @@ -21,7 +19,10 @@ public QueryPredicateBinary(String binName, Operator operator, Object value) {
}

@Override
public Exp toFilterExpression() {
public Exp toFilterExpression(boolean withPrimaryKey) {
if (isPrimaryKeyPredicate() && !withPrimaryKey) {
return null;
}
return operator.exp(buildLeftExp(), getValueExp(value));
}

Expand All @@ -39,7 +40,7 @@ public Optional<Filter> toFilter(String binName) {

@Override
public Collection<Object> getPrimaryKeys() {
if (binName.equals(PRIMARY_KEY_COLUMN_NAME) && operator == OperatorBinary.EQ) {
if (isPrimaryKeyPredicate() && operator == OperatorBinary.EQ) {
return Collections.singletonList(value);
}
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,19 @@ public QueryPredicateBoolean(
this.right = right;
}

@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
private static <T> Optional<T> or(Optional<T> optional, Optional<T> fallback) {
return optional.isPresent() ? optional : fallback;
}

@Override
public Exp toFilterExpression() {
return operator.exp(left.toFilterExpression(), right.toFilterExpression());
public Exp toFilterExpression(boolean withPrimaryKey) {
Exp leftExp = left.toFilterExpression(withPrimaryKey);
Exp rightExp = right.toFilterExpression(withPrimaryKey);
if (leftExp == null || rightExp == null) {
return leftExp == null ? rightExp : leftExp;
}
return operator.exp(leftExp, rightExp);
}

@Override
Expand Down Expand Up @@ -65,9 +75,4 @@ public Collection<Object> getPrimaryKeys() {
}
return Collections.emptyList();
}

@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
private static <T> Optional<T> or(Optional<T> optional, Optional<T> fallback) {
return optional.isPresent() ? optional : fallback;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public QueryPredicateIsNotNull(String binName) {
}

@Override
public Exp toFilterExpression() {
public Exp toFilterExpression(boolean withPrimaryKey) {
return binName.equals(PRIMARY_KEY_COLUMN_NAME)
? Exp.keyExists()
: Exp.binExists(binName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public QueryPredicateIsNull(String binName) {
}

@Override
public Exp toFilterExpression() {
public Exp toFilterExpression(boolean withPrimaryKey) {
return Exp.not(
binName.equals(PRIMARY_KEY_COLUMN_NAME)
? Exp.keyExists()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public QueryPredicateLike(String binName, String expression) {
}

@Override
public Exp toFilterExpression() {
public Exp toFilterExpression(boolean withPrimaryKey) {
return Exp.regexCompare(
expression,
RegexFlag.ICASE | RegexFlag.NEWLINE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import java.util.Collections;
import java.util.Optional;

import static com.aerospike.jdbc.util.Constants.PRIMARY_KEY_COLUMN_NAME;

public class QueryPredicateList extends QueryPredicateBase {

private final Operator operator;
Expand All @@ -22,7 +20,10 @@ public QueryPredicateList(String binName, Operator operator, Object[] values) {
}

@Override
public Exp toFilterExpression() {
public Exp toFilterExpression(boolean withPrimaryKey) {
if (isPrimaryKeyPredicate() && !withPrimaryKey) {
return null;
}
return operator.exp(
Arrays.stream(values)
.map(v -> Exp.eq(buildLeftExp(), getValueExp(v)))
Expand All @@ -42,7 +43,7 @@ public boolean isIndexable() {

@Override
public Collection<Object> getPrimaryKeys() {
if (binName.equals(PRIMARY_KEY_COLUMN_NAME)) {
if (isPrimaryKeyPredicate()) {
return Arrays.asList(values);
}
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ public QueryPredicatePrefix(
}

@Override
public Exp toFilterExpression() {
return operator.exp(right.toFilterExpression());
public Exp toFilterExpression(boolean withPrimaryKey) {
Exp rightExp = right.toFilterExpression(withPrimaryKey);
if (rightExp == null) {
return null;
}
return operator.exp(rightExp);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public QueryPredicateRange(
}

@Override
public Exp toFilterExpression() {
public Exp toFilterExpression(boolean withPrimaryKey) {
return Exp.and(
Exp.ge(buildLeftExp(), getValueExp(lowValue)),
Exp.lt(buildLeftExp(), getValueExp(highValue))
Expand Down
13 changes: 4 additions & 9 deletions src/main/java/com/aerospike/jdbc/query/PolicyBuilder.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.aerospike.jdbc.query;

import com.aerospike.client.IAerospikeClient;
import com.aerospike.client.exp.Exp;
import com.aerospike.client.policy.BatchReadPolicy;
import com.aerospike.client.policy.BatchWritePolicy;
import com.aerospike.client.policy.QueryPolicy;
Expand All @@ -23,15 +22,13 @@ public PolicyBuilder(IAerospikeClient client) {
public ScanPolicy buildScanPolicy(AerospikeQuery query) {
ScanPolicy scanPolicy = new ScanPolicy(client.getScanPolicyDefault());
scanPolicy.maxRecords = Objects.isNull(query.getLimit()) ? 0 : query.getLimit();
scanPolicy.filterExp = Objects.isNull(query.getPredicate())
? null : Exp.build(query.getPredicate().toFilterExpression());
scanPolicy.filterExp = query.toFilterExpression(true);
return scanPolicy;
}

public QueryPolicy buildQueryPolicy(AerospikeQuery query) {
QueryPolicy queryPolicy = new QueryPolicy(client.getQueryPolicyDefault());
queryPolicy.filterExp = Objects.isNull(query.getPredicate())
? null : Exp.build(query.getPredicate().toFilterExpression());
queryPolicy.filterExp = query.toFilterExpression(true);
return queryPolicy;
}

Expand All @@ -43,8 +40,7 @@ public ScanPolicy buildScanNoBinDataPolicy(AerospikeQuery query) {

public WritePolicy buildWritePolicy(AerospikeQuery query) {
WritePolicy writePolicy = new WritePolicy(client.getWritePolicyDefault());
writePolicy.filterExp = Objects.isNull(query.getPredicate())
? null : Exp.build(query.getPredicate().toFilterExpression());
writePolicy.filterExp = query.toFilterExpression(true);
return writePolicy;
}

Expand All @@ -56,8 +52,7 @@ public WritePolicy buildDeleteWritePolicy() {

public BatchReadPolicy buildBatchReadPolicy(AerospikeQuery query) {
BatchReadPolicy batchReadPolicy = new BatchReadPolicy();
batchReadPolicy.filterExp = Objects.isNull(query.getPredicate())
? null : Exp.build(query.getPredicate().toFilterExpression());
batchReadPolicy.filterExp = query.toFilterExpression(false);
return batchReadPolicy;
}

Expand Down
18 changes: 13 additions & 5 deletions src/test/java/com/aerospike/jdbc/DatabaseMetadataTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.aerospike.jdbc;

import com.aerospike.jdbc.util.TestRecord;
import com.aerospike.jdbc.util.TestUtil;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
Expand All @@ -11,6 +12,8 @@
import java.sql.SQLException;
import java.util.Objects;

import static com.aerospike.jdbc.util.TestConfig.NAMESPACE;
import static com.aerospike.jdbc.util.TestConfig.TABLE_NAME;
import static com.aerospike.jdbc.util.TestUtil.closeQuietly;
import static java.lang.String.format;
import static org.testng.Assert.assertEquals;
Expand All @@ -19,13 +22,18 @@

public class DatabaseMetadataTest extends JdbcBaseTest {

private final TestRecord testRecord;

DatabaseMetadataTest() {
testRecord = new TestRecord("key1", true, 11100, 1, "bar");
}

@BeforeClass
public void setUp() throws SQLException {
Objects.requireNonNull(connection, "connection is null");
PreparedStatement statement = null;
int count;
String query = format("insert into %s (bin1, int1, str1, bool1) values (11100, 1, \"bar\", true)",
tableName);
String query = testRecord.toInsertQuery();
try {
statement = connection.prepareStatement(query);
count = statement.executeUpdate();
Expand All @@ -39,7 +47,7 @@ public void setUp() throws SQLException {
public void tearDown() throws SQLException {
Objects.requireNonNull(connection, "connection is null");
PreparedStatement statement = null;
String query = format("delete from %s", tableName);
String query = format("delete from %s", TABLE_NAME);
try {
statement = connection.prepareStatement(query);
boolean result = statement.execute();
Expand All @@ -53,10 +61,10 @@ public void tearDown() throws SQLException {
@Test
public void testGetTables() throws SQLException {
DatabaseMetaData databaseMetaData = connection.getMetaData();
ResultSet tables = databaseMetaData.getTables(namespace, "", tableName, null);
ResultSet tables = databaseMetaData.getTables(NAMESPACE, "", TABLE_NAME, null);

if (tables.next()) {
assertEquals(tables.getString("TABLE_NAME"), tableName);
assertEquals(tables.getString("TABLE_NAME"), TABLE_NAME);
assertFalse(tables.next());
}
TestUtil.closeQuietly(tables);
Expand Down
Loading
Loading