Skip to content

Commit 7af480a

Browse files
deal with arrays with the same name in BI tools
1 parent e6ff22e commit 7af480a

File tree

5 files changed

+105
-33
lines changed

5 files changed

+105
-33
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ apply plugin: 'project-report'
2828

2929

3030
group 'io.tiledb'
31-
version '0.3.3-SNAPSHOT'
31+
version '0.3.4-SNAPSHOT'
3232

3333
repositories {
3434
mavenCentral()

src/main/java/io/tiledb/TileDBCloudColumnsResultSet.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.tiledb.cloud.rest_api.ApiException;
77
import io.tiledb.cloud.rest_api.api.ArrayApi;
88
import io.tiledb.cloud.rest_api.model.*;
9+
import io.tiledb.util.Util;
910
import java.io.InputStream;
1011
import java.io.Reader;
1112
import java.math.BigDecimal;
@@ -31,11 +32,11 @@ public class TileDBCloudColumnsResultSet implements ResultSet {
3132

3233
public TileDBCloudColumnsResultSet(String completeURI, ArrayApi arrayApi) {
3334
this.columnCounter = -1;
34-
// the complete URI contains both the name and the UUID
35+
// the complete URI contains both the name and the short-UUID
3536
this.completeURI = completeURI;
3637
this.nullable = columnNoNulls;
37-
38-
String[] split = completeURI.split("/");
38+
String withoutUUID = Util.removeUUID(completeURI);
39+
String[] split = withoutUUID.split("/");
3940
String arrayUUIDClean = split[split.length - 1];
4041
String arrayNamespaceClean = split[split.length - 2];
4142

src/main/java/io/tiledb/TileDBCloudStatement.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.tiledb.cloud.rest_api.model.ResultFormat;
77
import io.tiledb.cloud.rest_api.model.SQLParameters;
88
import io.tiledb.java.api.Pair;
9+
import io.tiledb.util.Util;
910
import java.sql.*;
1011
import java.util.ArrayList;
1112
import org.apache.arrow.vector.ValueVector;
@@ -38,7 +39,8 @@ public class TileDBCloudStatement implements Statement {
3839
public ResultSet executeQuery(String s) throws SQLException {
3940
// create SQL parameters
4041
SQLParameters sqlParameters = new SQLParameters();
41-
sqlParameters.setQuery(s);
42+
String query = Util.useTileDBUris(s);
43+
sqlParameters.setQuery(query);
4244
// get results in arrow format
4345
sqlParameters.setResultFormat(ResultFormat.ARROW);
4446

src/main/java/io/tiledb/TileDBCloudTablesResultSet.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.logging.Logger;
1414

1515
public class TileDBCloudTablesResultSet implements ResultSet {
16+
public static HashMap<String, String> uris = new HashMap<>();
1617
private List<ArrayInfo> arraysOwned = new ArrayList<ArrayInfo>();
1718
private List<ArrayInfo> arraysShared = new ArrayList<ArrayInfo>();
1819
private List<ArrayInfo> arraysPublic = new ArrayList<ArrayInfo>();
@@ -37,6 +38,31 @@ public TileDBCloudTablesResultSet(
3738

3839
this.numberOfArrays =
3940
this.arraysOwned.size() + this.arraysShared.size() + this.arraysPublic.size();
41+
populateURIs();
42+
}
43+
44+
private void populateURIs() {
45+
46+
// Iterate through arraysOwned and add entries to the HashMap
47+
for (ArrayInfo arrayInfo : arraysOwned) {
48+
String key = Util.getUUIDStart(arrayInfo.getTiledbUri());
49+
String value = arrayInfo.getTiledbUri();
50+
uris.put(key, value);
51+
}
52+
53+
// Iterate through arraysShared and add entries to the HashMap
54+
for (ArrayInfo arrayInfo : arraysShared) {
55+
String key = Util.getUUIDStart(arrayInfo.getTiledbUri());
56+
String value = arrayInfo.getTiledbUri();
57+
uris.put(key, value);
58+
}
59+
60+
// Iterate through arraysPublic and add entries to the HashMap
61+
for (ArrayInfo arrayInfo : arraysPublic) {
62+
String key = Util.getUUIDStart(arrayInfo.getTiledbUri());
63+
String value = arrayInfo.getTiledbUri();
64+
uris.put(key, value);
65+
}
4066
}
4167

4268
public TileDBCloudTablesResultSet() {
@@ -78,7 +104,13 @@ public boolean wasNull() throws SQLException {
78104

79105
@Override
80106
public String getString(int columnIndex) throws SQLException {
81-
return "tiledb://" + currentArray.getNamespace() + "/" + currentArray.getName();
107+
return "[tiledb://"
108+
+ currentArray.getNamespace()
109+
+ "/"
110+
+ currentArray.getName()
111+
+ "]["
112+
+ Util.getUUIDStart(currentArray.getTiledbUri())
113+
+ "]";
82114
}
83115

84116
@Override
@@ -176,7 +208,13 @@ public String getString(String columnLabel) throws SQLException {
176208

177209
switch (columnLabel) {
178210
case "TABLE_NAME":
179-
return "tiledb://" + currentArray.getNamespace() + "/" + currentArray.getName();
211+
return "[tiledb://"
212+
+ currentArray.getNamespace()
213+
+ "/"
214+
+ currentArray.getName()
215+
+ "]["
216+
+ Util.getUUIDStart(currentArray.getTiledbUri())
217+
+ "]";
180218
case "REMARKS":
181219
return ownership + " TileDB URI: " + currentArray.getTiledbUri();
182220
case "TABLE_TYPE":
Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.tiledb.util;
22

3+
import io.tiledb.TileDBCloudTablesResultSet;
34
import java.util.regex.Matcher;
45
import java.util.regex.Pattern;
56

@@ -15,41 +16,71 @@ public class Util {
1516
public static String SCHEMA_NAME = "All TileDB arrays";
1617

1718
/**
18-
* Replaces all array names with the corresponding UUID if the input string is of a specific
19-
* pattern
19+
* Takes as input a complete query that might or might not contain references to arrays of the
20+
* form [tiledb://../..][a09uhugr] and returns a query in which all these references which are
21+
* specific to this JDBC driver are translated to queries that contain the tiledbURis. This method
22+
* is mainly used when the query is created by a BI tool rather than a pure Java user. BI tools
23+
* display the arrays and when selected use their names to query them. Since the array names are
24+
* not unique in TileDB-Cloud we display the names accompanied with one small part of the original
25+
* UUID. We then map this UUID to the tileDBURI and modify the query so that it only uses
26+
* tileDBURIs. If no array references of the above-mentioned form are found the query is returned
27+
* intact.
2028
*
21-
* @param completeURI If the input string is of the form "tiledb://TileDB-Inc/orders ~
22-
* tiledb://TileDB-Inc/120f0518-dd8d-467f-8c1b-23aa49929465" this method will modify the input
23-
* string to only use the TileDB URI with the UUID. If an input string does not match the
24-
* regex pattern specified in the Pattern.compile method, the Matcher will not find any
25-
* matches, and the code will simply return the original input string as it is without making
26-
* any modifications.
29+
* @param query The complete query
2730
* @return
2831
*/
29-
public static String replaceArrayNamesWithUUIDs(String completeURI) {
30-
// Define the regular expression pattern for the TileDB URI pattern with spaces around "~"
31-
String regex = "(tiledb://[^~]+) ~ ([^\\s]+)";
32-
Pattern pattern = Pattern.compile(regex);
32+
public static String useTileDBUris(String query) {
33+
// Regular expression pattern to match the specified format
34+
Pattern pattern = Pattern.compile("\\[tiledb://([^/]+)/([^\\]]+)\\]\\[(\\w+)\\]");
3335

34-
Matcher matcher = pattern.matcher(completeURI);
35-
StringBuilder modifiedText = new StringBuilder();
36+
// Matcher to find and replace occurrences of the pattern in the input string
37+
Matcher matcher = pattern.matcher(query);
3638

37-
// Find and remove the first part in TileDB URIs
38-
int lastEnd = 0;
39-
while (matcher.find()) {
40-
// Append the text before the match and a space
41-
modifiedText.append(completeURI.substring(lastEnd, matcher.start(1)));
39+
// StringBuffer to hold the modified input string
40+
StringBuffer result = new StringBuffer();
4241

43-
// Append the text after "~" and a space
44-
modifiedText.append(matcher.group(2));
42+
// Find and replace occurrences of the pattern in the input string
43+
while (matcher.find()) {
44+
String key = matcher.group(3);
4545

46-
// Update the lastEnd to the end of this match
47-
lastEnd = matcher.end();
46+
String replacement =
47+
TileDBCloudTablesResultSet.uris.getOrDefault(key, ""); // Get replacement from the map
48+
if (!replacement.equals(""))
49+
matcher.appendReplacement(
50+
result, replacement); // Replace the match with the corresponding value
4851
}
52+
matcher.appendTail(result); // Append the remaining part of the input string
53+
// Output the modified string
54+
return result.toString();
55+
}
4956

50-
// Append any remaining text after the last match
51-
modifiedText.append(completeURI.substring(lastEnd));
57+
/**
58+
* Takes as input a TileDBURI and returns the first 8 characters of the UUID.
59+
*
60+
* @param tiledbUri the TileDBURI
61+
* @return the first 8 chars of the UUID
62+
*/
63+
public static String getUUIDStart(String tiledbUri) {
64+
// Split the input URI by '/'
65+
String[] uriParts = tiledbUri.split("/");
5266

53-
return modifiedText.toString().trim();
67+
// Extract the desired string and return its first 8 characters
68+
String targetString = uriParts[3];
69+
return targetString.substring(0, Math.min(targetString.length(), 8));
70+
}
71+
72+
public static String removeUUID(String arrayName) {
73+
// Regular expression pattern to match the specified format
74+
Pattern pattern = Pattern.compile("\\[tiledb://([^\\]]+)\\].*");
75+
76+
// Matcher to find the pattern in the input string
77+
Matcher matcher = pattern.matcher(arrayName);
78+
79+
// Check if the pattern is found and extract the desired part
80+
if (matcher.matches()) {
81+
return "tiledb://" + matcher.group(1);
82+
} else {
83+
return arrayName;
84+
}
5485
}
5586
}

0 commit comments

Comments
 (0)