Skip to content

Adding Struct Support #73

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

Merged
merged 4 commits into from
May 10, 2023
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
2 changes: 1 addition & 1 deletion src/main/java/org/opensearch/jdbc/ConnectionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ public Array createArrayOf(String typeName, Object[] elements) throws SQLExcepti

@Override
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
throw new SQLFeatureNotSupportedException("Struct is not supported.");
return new StructImpl(typeName, attributes);
}

@Override
Expand Down
98 changes: 98 additions & 0 deletions src/main/java/org/opensearch/jdbc/StructImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.jdbc;

import java.sql.SQLException;
import java.sql.Struct;
import java.util.Arrays;
import java.util.Map;
import java.util.List;


/**
* This class implements the {@link java.sql.Struct} interface.
* <p>
* {@code StructImpl} provides a simple implementation of a struct data type.
* </p>
*/
public class StructImpl implements Struct {
private final String typeName;
private final Object[] attributes;

/**
* Constructs a new {@code StructImpl} object with the specified parameter values.
*
* @param typeName the SQL type name of the struct
* @param attributes the attributes of the struct, each attribute is a {@code Map.Entry<K, V>}(key-value pair)
*/
public StructImpl(String typeName, Object[] attributes) {
this.typeName = typeName;
this.attributes = attributes;
}

/**
* Returns the SQL type name of the struct.
*
* @return the SQL type name of the struct
* @throws SQLException if a database access error occurs
*/
@Override
public String getSQLTypeName() throws SQLException {
return this.typeName;
}

/**
* Returns an array containing the attributes of the struct.
*
* @return an array containing the attribute values of the struct
* @throws SQLException if a database access error occurs
*/
@Override
public Object[] getAttributes() throws SQLException {
return attributes;
}

/**
* @throws java.lang.UnsupportedOperationException because functionality is not supported yet
*/
@Override
public Object[] getAttributes(Map<String,Class<?>> map) throws SQLException {
throw new java.lang.UnsupportedOperationException("Not supported yet.");
}

/**
* Compares this StructImpl object with the specified object for equality.
*
* <p>
* Two StructImpl objects are considered equal if they have the same typeName, same number of attributes,
* and contain the same attributes.
* </p>
*
* @param obj the object to compare with this StructImpl object for equality.
* @return {@code true} if the specified object is equal to this StructImpl object, {@code false} otherwise.
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Struct)) {
return false;
}
if (obj == this) {
return true;
}
Struct other = (Struct) obj;
try {
if (!typeName.equals(other.getSQLTypeName()) || attributes.length != other.getAttributes().length) {
return false;
}
List otherAttributes = Arrays.asList(other.getAttributes());
return otherAttributes.containsAll(Arrays.asList(attributes));
}
catch (SQLException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.sql.Date;
import java.sql.SQLException;
import java.sql.Struct;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.HashMap;
Expand Down Expand Up @@ -35,6 +36,8 @@ public abstract class BaseTypeConverter implements TypeConverter {
typeHandlerMap.put(Date.class, DateType.INSTANCE);
typeHandlerMap.put(Time.class, TimeType.INSTANCE);

typeHandlerMap.put(Struct.class, StructType.INSTANCE);

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public enum OpenSearchType {
jdbcTypeToOpenSearchTypeMap.put(JDBCType.TIME, TIME);
jdbcTypeToOpenSearchTypeMap.put(JDBCType.DATE, DATE);
jdbcTypeToOpenSearchTypeMap.put(JDBCType.VARBINARY, BINARY);
jdbcTypeToOpenSearchTypeMap.put(JDBCType.STRUCT, OBJECT);
}

/**
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/org/opensearch/jdbc/types/StructType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.jdbc.types;

import java.sql.Struct;
import java.util.Map;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.LinkedHashMap;
import java.util.Collections;



import org.opensearch.jdbc.StructImpl;

public class StructType implements TypeHelper<Struct> {

public static final StructType INSTANCE = new StructType();

private StructType() {

}

@Override
public String getTypeName() {
return "Struct";
}

@Override
public Struct fromValue(Object value, Map<String, Object> conversionParams) {
if (value == null || !(value instanceof Map<?, ?>)) {
return null;
}
Map<String, Object> structKeyValues = (Map<String, Object>) value;
return new StructImpl(getTypeName(), structKeyValues.entrySet().toArray());
}
}
23 changes: 23 additions & 0 deletions src/main/java/org/opensearch/jdbc/types/TypeConverters.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.sql.Date;
import java.sql.JDBCType;
import java.sql.SQLException;
import java.sql.Struct;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Arrays;
Expand Down Expand Up @@ -56,12 +57,34 @@ public class TypeConverters {
tcMap.put(JDBCType.BINARY, new BinaryTypeConverter());

tcMap.put(JDBCType.NULL, new NullTypeConverter());

// Adding Struct Support
tcMap.put(JDBCType.STRUCT, new StructTypeConverter());
}

public static TypeConverter getInstance(JDBCType jdbcType) {
return tcMap.get(jdbcType);
}

public static class StructTypeConverter extends BaseTypeConverter {

private static final Set<Class> supportedJavaClasses = Collections.singleton(Struct.class);

StructTypeConverter() {

}

@Override
public Class getDefaultJavaClass() {
return Struct.class;
}

@Override
public Set<Class> getSupportedJavaClasses() {
return supportedJavaClasses;
}
}

public static class TimestampTypeConverter extends BaseTypeConverter {

private static final Set<Class> supportedJavaClasses = Collections.unmodifiableSet(
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/org/opensearch/jdbc/ResultSetTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.opensearch.jdbc.test.TestResources;
import org.opensearch.jdbc.test.mocks.MockOpenSearch;
import org.opensearch.jdbc.types.OpenSearchType;
import org.opensearch.jdbc.types.StructType;
import org.opensearch.jdbc.test.PerTestWireMockServerExtension;
import org.opensearch.jdbc.test.WireMockServerHelpers;
import org.opensearch.jdbc.test.mocks.MockResultSet;
Expand All @@ -32,6 +33,8 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
Expand Down Expand Up @@ -176,6 +179,18 @@ void testNullableFieldsQuery(WireMockServer mockServer) throws SQLException, IOE
Statement st = con.createStatement();
ResultSet rs = assertDoesNotThrow(() -> st.executeQuery(queryMock.getSql()));

Map<String, Object> attributes = new HashMap<String, Object>() {{
put("attribute1", "value1");
put("attribute2", 2);
put("attribute3", 15.0);
}};

Map<String, Object> nestedAttributes = new HashMap<String, Object>() {{
put("struct", attributes);
put("string", "hello");
put("int", 1);
}};

assertNotNull(rs);

MockResultSetMetaData mockResultSetMetaData = MockResultSetMetaData.builder()
Expand All @@ -191,6 +206,7 @@ void testNullableFieldsQuery(WireMockServer mockServer) throws SQLException, IOE
.column("testKeyword", OpenSearchType.KEYWORD)
.column("testText", OpenSearchType.TEXT)
.column("testDouble", OpenSearchType.DOUBLE)
.column("testStruct", OpenSearchType.OBJECT)
.build();

MockResultSetRows mockResultSetRows = MockResultSetRows.builder()
Expand All @@ -207,6 +223,21 @@ void testNullableFieldsQuery(WireMockServer mockServer) throws SQLException, IOE
.column("Test String", false)
.column("document3", false)
.column((double) 0, true)
.column(StructType.INSTANCE.fromValue(attributes, null), false)
.row()
.column(true, false)
.column("1", false)
.column((byte) 126, false)
.column((float) 0, true)
.column((long) 32000320003200030L, false)
.column((short) 29000, false)
.column((float) 0, true)
.column(null, true)
.column((double) 0, true)
.column(null, true)
.column(null, true)
.column((double) 22.312423148903218, false)
.column(null, true)
.row()
.column(true, false)
.column("1", false)
Expand All @@ -220,6 +251,7 @@ void testNullableFieldsQuery(WireMockServer mockServer) throws SQLException, IOE
.column(null, true)
.column(null, true)
.column((double) 22.312423148903218, false)
.column(StructType.INSTANCE.fromValue(nestedAttributes, null), false)
.build();

MockResultSet mockResultSet = new MockResultSet(mockResultSetMetaData, mockResultSetRows);
Expand Down
57 changes: 57 additions & 0 deletions src/test/java/org/opensearch/jdbc/types/StructTypeTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.jdbc.types;

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.opensearch.jdbc.types.StructType;
import org.opensearch.jdbc.StructImpl;

import java.sql.Struct;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Map;
import java.util.HashMap;

public class StructTypeTests {

@Test
public void testStructTypeFromValue() throws SQLException {
Map<String, Object> attributes = new HashMap<String, Object>() {{
put("attribute1", "value1");
put("attribute2", 2);
put("attribute3", 15.0);
}};

Map<String, Object> attributes2 = new HashMap<String, Object>() {{
put("attribute1", "value1");
put("attribute2", 2);
put("attribute3", 15.0);
put("attribute4", "value4");
}};

Struct actualStruct = StructType.INSTANCE.fromValue(attributes, null);
Struct actualStruct2 = StructType.INSTANCE.fromValue(attributes2, null);

assertTrue(Arrays.equals(actualStruct.getAttributes(), attributes.entrySet().toArray()));
assertEquals(actualStruct.getAttributes().length, 3);
assertEquals(actualStruct2.getAttributes().length, 4);
assertEquals(actualStruct, new StructImpl(StructType.INSTANCE.getTypeName(), attributes.entrySet().toArray()));
assertNotEquals(actualStruct, actualStruct2);

Map<String, Object> nestedAttributes = new HashMap<String, Object>() {{
put("struct", attributes);
put("string", "hello");
put("int", 1);
}};

Struct actualNestedStruct = StructType.INSTANCE.fromValue(nestedAttributes, null);
assertTrue(Arrays.equals(actualNestedStruct.getAttributes(), nestedAttributes.entrySet().toArray()));
assertEquals(actualNestedStruct, new StructImpl(StructType.INSTANCE.getTypeName(), nestedAttributes.entrySet().toArray()));
assertNotEquals(actualStruct, actualNestedStruct);
}
}
Loading