From 825e87155b3b5ae7bba39cb26abbea396fb50ca0 Mon Sep 17 00:00:00 2001 From: yrizhkov Date: Mon, 3 Apr 2023 10:27:12 +0300 Subject: [PATCH 1/4] FMWK-149 Fix support for null set queries --- .../java/com/aerospike/jdbc/async/ScanQueryHandler.java | 6 +++--- .../java/com/aerospike/jdbc/model/AerospikeQuery.java | 9 +++++++++ .../com/aerospike/jdbc/query/DeleteQueryHandler.java | 4 ++-- .../com/aerospike/jdbc/query/InsertQueryHandler.java | 4 ++-- .../com/aerospike/jdbc/query/SelectQueryHandler.java | 2 +- .../com/aerospike/jdbc/query/TruncateQueryHandler.java | 2 +- .../com/aerospike/jdbc/query/UpdateQueryHandler.java | 4 ++-- 7 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/aerospike/jdbc/async/ScanQueryHandler.java b/src/main/java/com/aerospike/jdbc/async/ScanQueryHandler.java index 22778b6..2a4b112 100644 --- a/src/main/java/com/aerospike/jdbc/async/ScanQueryHandler.java +++ b/src/main/java/com/aerospike/jdbc/async/ScanQueryHandler.java @@ -46,7 +46,7 @@ public RecordSet execute(ScanPolicy scanPolicy, AerospikeQuery query) { long maxRecords = scanPolicy.maxRecords; PartitionFilter filter = getPartitionFilter(query); while (isScanRequired(maxRecords)) { - client.scanPartitions(scanPolicy, filter, query.getSchema(), query.getTable(), + client.scanPartitions(scanPolicy, filter, query.getSchema(), query.getSetName(), callback, query.getBinNames()); scanPolicy.maxRecords = maxRecords > 0 ? maxRecords - count : maxRecords; filter = PartitionFilter.id(++currentPartition); @@ -54,13 +54,13 @@ public RecordSet execute(ScanPolicy scanPolicy, AerospikeQuery query) { listener.onSuccess(); } else { client.scanAll(EventLoopProvider.getEventLoop(), listener, scanPolicy, query.getSchema(), - query.getTable(), query.getBinNames()); + query.getSetName(), query.getBinNames()); } return listener.getRecordSet(); } private PartitionFilter getPartitionFilter(AerospikeQuery query) { - Key key = new Key(query.getSchema(), query.getTable(), query.getOffset()); + Key key = new Key(query.getSchema(), query.getSetName(), query.getOffset()); currentPartition = Partition.getPartitionId(key.digest); return PartitionFilter.after(key); } diff --git a/src/main/java/com/aerospike/jdbc/model/AerospikeQuery.java b/src/main/java/com/aerospike/jdbc/model/AerospikeQuery.java index 6011ec7..469dcec 100644 --- a/src/main/java/com/aerospike/jdbc/model/AerospikeQuery.java +++ b/src/main/java/com/aerospike/jdbc/model/AerospikeQuery.java @@ -15,6 +15,8 @@ import java.util.List; import java.util.Map; +import static com.aerospike.jdbc.util.Constants.defaultSchemaName; + public class AerospikeQuery { @VisibleForTesting @@ -88,6 +90,13 @@ public void setTable(String table) { } } + public String getSetName() { + if (table.equals(defaultSchemaName)) { + return null; + } + return table; + } + public SchemaTableName getSchemaTable() { return new SchemaTableName(schema, table); } diff --git a/src/main/java/com/aerospike/jdbc/query/DeleteQueryHandler.java b/src/main/java/com/aerospike/jdbc/query/DeleteQueryHandler.java index b44307f..19ab3e3 100644 --- a/src/main/java/com/aerospike/jdbc/query/DeleteQueryHandler.java +++ b/src/main/java/com/aerospike/jdbc/query/DeleteQueryHandler.java @@ -38,7 +38,7 @@ public Pair execute(AerospikeQuery query) { logger.info("DELETE primary key"); FutureDeleteListener listener = new FutureDeleteListener(keyObjects.size()); for (Object keyObject : keyObjects) { - Key key = new Key(query.getSchema(), query.getTable(), Value.get(keyObject)); + Key key = new Key(query.getSchema(), query.getSetName(), Value.get(keyObject)); try { client.delete(EventLoopProvider.getEventLoop(), listener, writePolicy, key); } catch (AerospikeException e) { @@ -57,7 +57,7 @@ public Pair execute(AerospikeQuery query) { ScanPolicy scanPolicy = buildScanPolicy(query); scanPolicy.includeBinData = false; client.scanAll(EventLoopProvider.getEventLoop(), listener, scanPolicy, query.getSchema(), - query.getTable()); + query.getSetName()); final AtomicInteger count = new AtomicInteger(); listener.getRecordSet().forEach(r -> { diff --git a/src/main/java/com/aerospike/jdbc/query/InsertQueryHandler.java b/src/main/java/com/aerospike/jdbc/query/InsertQueryHandler.java index 5d733db..22e3463 100644 --- a/src/main/java/com/aerospike/jdbc/query/InsertQueryHandler.java +++ b/src/main/java/com/aerospike/jdbc/query/InsertQueryHandler.java @@ -53,7 +53,7 @@ public Pair putConsecutively(AerospikeQuery query) { @SuppressWarnings("unchecked") List values = (List) record; Value recordKey = extractInsertKey(query, values); - Key key = new Key(query.getSchema(), query.getTable(), recordKey); + Key key = new Key(query.getSchema(), query.getSetName(), recordKey); Bin[] bins = buildBinArray(binNames, values); try { @@ -85,7 +85,7 @@ public Pair putBatch(AerospikeQuery query) { @SuppressWarnings("unchecked") List values = (List) record; Value recordKey = extractInsertKey(query, values); - Key key = new Key(query.getSchema(), query.getTable(), recordKey); + Key key = new Key(query.getSchema(), query.getSetName(), recordKey); batchRecords.add( new BatchWrite( batchWritePolicy, diff --git a/src/main/java/com/aerospike/jdbc/query/SelectQueryHandler.java b/src/main/java/com/aerospike/jdbc/query/SelectQueryHandler.java index 864f7dd..b86f88c 100644 --- a/src/main/java/com/aerospike/jdbc/query/SelectQueryHandler.java +++ b/src/main/java/com/aerospike/jdbc/query/SelectQueryHandler.java @@ -95,7 +95,7 @@ private Pair executeSelectByPrimaryKey(AerospikeQuery query, logger.info(() -> "SELECT primary key"); final BatchReadPolicy policy = buildBatchReadPolicy(query); List batchReadList = keyObjects.stream() - .map(k -> new BatchRead(policy, new Key(query.getSchema(), query.getTable(), Value.get(k)), true)) + .map(k -> new BatchRead(policy, new Key(query.getSchema(), query.getSetName(), Value.get(k)), true)) .collect(Collectors.toList()); RecordSetBatchSequenceListener listener = new RecordSetBatchSequenceListener(); diff --git a/src/main/java/com/aerospike/jdbc/query/TruncateQueryHandler.java b/src/main/java/com/aerospike/jdbc/query/TruncateQueryHandler.java index 609d0f3..fcd1563 100644 --- a/src/main/java/com/aerospike/jdbc/query/TruncateQueryHandler.java +++ b/src/main/java/com/aerospike/jdbc/query/TruncateQueryHandler.java @@ -19,7 +19,7 @@ public TruncateQueryHandler(IAerospikeClient client, Statement statement) { @Override public Pair execute(AerospikeQuery query) { logger.info("TRUNCATE/DROP statement"); - client.truncate(null, query.getSchema(), query.getTable(), null); + client.truncate(null, query.getSchema(), query.getSetName(), null); return new Pair<>(emptyRecordSet(query), 1); } diff --git a/src/main/java/com/aerospike/jdbc/query/UpdateQueryHandler.java b/src/main/java/com/aerospike/jdbc/query/UpdateQueryHandler.java index ff130a5..8784028 100644 --- a/src/main/java/com/aerospike/jdbc/query/UpdateQueryHandler.java +++ b/src/main/java/com/aerospike/jdbc/query/UpdateQueryHandler.java @@ -40,7 +40,7 @@ public Pair execute(AerospikeQuery query) { logger.info("UPDATE primary key"); FutureWriteListener listener = new FutureWriteListener(keyObjects.size()); for (Object keyObject : keyObjects) { - Key key = new Key(query.getSchema(), query.getTable(), Value.get(keyObject)); + Key key = new Key(query.getSchema(), query.getSetName(), Value.get(keyObject)); try { client.put(EventLoopProvider.getEventLoop(), listener, writePolicy, key, bins); } catch (AerospikeException e) { @@ -59,7 +59,7 @@ public Pair execute(AerospikeQuery query) { ScanPolicy scanPolicy = buildScanPolicy(query); scanPolicy.includeBinData = false; client.scanAll(EventLoopProvider.getEventLoop(), listener, scanPolicy, query.getSchema(), - query.getTable()); + query.getSetName()); final AtomicInteger count = new AtomicInteger(); listener.getRecordSet().forEach(r -> { From 7c17f44c6151b11799ee7a6e26ebcbcc8f7d5b3e Mon Sep 17 00:00:00 2001 From: yrizhkov Date: Mon, 3 Apr 2023 10:30:13 +0300 Subject: [PATCH 2/4] FMWK-118 Replace null return values with empty string in metadata --- .../java/com/aerospike/jdbc/AerospikeDatabaseMetadata.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/aerospike/jdbc/AerospikeDatabaseMetadata.java b/src/main/java/com/aerospike/jdbc/AerospikeDatabaseMetadata.java index e894887..eb9642c 100644 --- a/src/main/java/com/aerospike/jdbc/AerospikeDatabaseMetadata.java +++ b/src/main/java/com/aerospike/jdbc/AerospikeDatabaseMetadata.java @@ -245,17 +245,17 @@ public String getNumericFunctions() { @Override public String getStringFunctions() { - return null; + return ""; } @Override public String getSystemFunctions() { - return null; + return ""; } @Override public String getTimeDateFunctions() { - return null; + return ""; } @Override From 9d027010ae3e7f5e4c91af16f1e25355c889972a Mon Sep 17 00:00:00 2001 From: yrizhkov Date: Mon, 3 Apr 2023 12:05:19 +0300 Subject: [PATCH 3/4] FMWK-171 Return only relevant secondary indexes in metadata --- .../jdbc/AerospikeDatabaseMetadata.java | 15 ++++---- .../aerospike/jdbc/PreparedQueriesTest.java | 34 +++++++++++++++++++ .../com/aerospike/jdbc/SimpleQueriesTest.java | 34 +++++++++++++++++++ 3 files changed, 77 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/aerospike/jdbc/AerospikeDatabaseMetadata.java b/src/main/java/com/aerospike/jdbc/AerospikeDatabaseMetadata.java index eb9642c..2d82063 100644 --- a/src/main/java/com/aerospike/jdbc/AerospikeDatabaseMetadata.java +++ b/src/main/java/com/aerospike/jdbc/AerospikeDatabaseMetadata.java @@ -32,6 +32,7 @@ import static com.aerospike.jdbc.util.AerospikeUtils.getIndexBinValuesRatio; import static com.aerospike.jdbc.util.Constants.defaultKeyName; +import static com.aerospike.jdbc.util.Constants.defaultSchemaName; import static com.aerospike.jdbc.util.Constants.schemaScanRecords; import static java.lang.String.format; import static java.sql.Connection.TRANSACTION_NONE; @@ -78,7 +79,7 @@ public AerospikeDatabaseMetadata(String url, IAerospikeClient client, Connection namespaces.addAll(asList(getOrDefault(r, "namespaces", "").split(";"))); streamOfSubProperties(r, "sets").forEach(p -> tables.computeIfAbsent(p.getProperty("ns"), s -> new HashSet<>()) - .add(p.getProperty("set")) + .addAll(Arrays.asList(p.getProperty("set"), defaultSchemaName)) ); streamOfSubProperties(r, "sindex") .filter(AerospikeUtils::isSupportedIndexType) @@ -949,14 +950,16 @@ public ResultSet getTypeInfo() { @Override public ResultSet getIndexInfo(String catalog, String schema, String table, boolean unique, boolean approximate) { - final Iterable> indicesData; + Stream secondaryIndexStream; if (catalog == null) { - indicesData = indices.entrySet().stream().flatMap(p -> p.getValue().stream()) - .map(this::indexInfoAsList).collect(Collectors.toList()); + secondaryIndexStream = indices.entrySet().stream().flatMap(p -> p.getValue().stream()); } else { - indicesData = getOrDefault(indices, catalog, Collections.emptyList()).stream() - .map(this::indexInfoAsList).collect(Collectors.toList()); + secondaryIndexStream = getOrDefault(indices, catalog, Collections.emptyList()).stream(); } + final Iterable> indicesData = secondaryIndexStream + .filter(i -> i.getNamespace().equals(schema) && i.getSet().equals(table)) + .map(this::indexInfoAsList) + .collect(Collectors.toList()); String[] columns = new String[]{"TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", "NON_UNIQUE", "INDEX_QUALIFIER", "INDEX_NAME", "TYPE", "ORDINAL_POSITION", "COLUMN_NAME", "ASC_OR_DESC", "CARDINALITY", diff --git a/src/test/java/com/aerospike/jdbc/PreparedQueriesTest.java b/src/test/java/com/aerospike/jdbc/PreparedQueriesTest.java index e08b9c6..05908a2 100644 --- a/src/test/java/com/aerospike/jdbc/PreparedQueriesTest.java +++ b/src/test/java/com/aerospike/jdbc/PreparedQueriesTest.java @@ -84,6 +84,40 @@ public void testSelectQuery() throws SQLException { } } + @Test + public void testSelectByPrimaryKeyQuery() throws SQLException { + PreparedStatement statement = null; + ResultSet resultSet = null; + String query = String.format( + "insert into %s (__key, bin1, int1, str1, bool1) values (\"key1\", 11101, 2, \"bar\", true)", + tableName + ); + try { + statement = connection.prepareStatement(query); + statement.executeUpdate(); + } finally { + closeQuietly(statement); + } + query = String.format("select * from %s where __key='%s'", tableName, "key1"); + int total = 0; + try { + statement = connection.prepareStatement(query); + resultSet = statement.executeQuery(); + while (resultSet.next()) { + assertEquals(resultSet.getInt("bin1"), 11101); + assertEquals(resultSet.getInt("int1"), 2); + assertEquals(resultSet.getString("str1"), "bar"); + assertEquals(resultSet.getInt("bool1"), 1); + + total++; + } + assertEquals(total, 1); + } finally { + closeQuietly(statement); + closeQuietly(resultSet); + } + } + @Test public void testInsertQuery() throws SQLException { PreparedStatement statement = null; diff --git a/src/test/java/com/aerospike/jdbc/SimpleQueriesTest.java b/src/test/java/com/aerospike/jdbc/SimpleQueriesTest.java index fea6649..fe81f05 100644 --- a/src/test/java/com/aerospike/jdbc/SimpleQueriesTest.java +++ b/src/test/java/com/aerospike/jdbc/SimpleQueriesTest.java @@ -75,6 +75,40 @@ public void testSelectQuery() throws SQLException { } } + @Test + public void testSelectByPrimaryKeyQuery() throws SQLException { + Statement statement = null; + ResultSet resultSet = null; + String query = String.format( + "insert into %s (__key, bin1, int1, str1, bool1) values (\"key1\", 11101, 2, \"bar\", true)", + tableName + ); + try { + statement = connection.createStatement(); + statement.executeUpdate(query); + } finally { + closeQuietly(statement); + } + query = String.format("select * from %s where __key='%s'", tableName, "key1"); + int total = 0; + try { + statement = connection.createStatement(); + resultSet = statement.executeQuery(query); + while (resultSet.next()) { + assertEquals(resultSet.getInt("bin1"), 11101); + assertEquals(resultSet.getInt("int1"), 2); + assertEquals(resultSet.getString("str1"), "bar"); + assertEquals(resultSet.getInt("bool1"), 1); + + total++; + } + assertEquals(total, 1); + } finally { + closeQuietly(statement); + closeQuietly(resultSet); + } + } + @Test public void testInsertQuery() throws SQLException { Statement statement = null; From 38b77eee9ac6a6ecd58d64d2aa55b0727e7ab5c4 Mon Sep 17 00:00:00 2001 From: yrizhkov Date: Mon, 3 Apr 2023 12:07:22 +0300 Subject: [PATCH 4/4] FMWK-170 Upgrade dependencies --- pom.xml | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index 9d795d7..bdf27cd 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ aerospike-jdbc A JDBC driver for the Aerospike database https://github.com/aerospike/aerospike-jdbc - 1.7.1 + 1.7.2 false @@ -26,11 +26,15 @@ 3.4.0 1.6.13 - 6.1.6 - 4.1.87.Final + 6.1.8 + 4.1.90.Final 2.14.2 - 1.32.0 + 1.34.0 4.5.14 + + 3.24.2 + 7.5 + 3.37.1 @@ -121,21 +125,21 @@ org.assertj assertj-core - 3.23.1 + ${assertj.version} test org.testng testng - 7.5 + ${testng.version} test org.jdbi jdbi3-core - 3.33.0 + ${jdbi.version} test