Skip to content

Commit

Permalink
#124: Added support for external tables. Added Redshift/Spectrum colu…
Browse files Browse the repository at this point in the history
…mn mappings.

# Conflicts:
#	jdbc-adapter/virtualschema-jdbc-adapter/src/main/java/com/exasol/adapter/jdbc/AbstractMetadataReader.java
#	jdbc-adapter/virtualschema-jdbc-adapter/src/test/java/com/exasol/adapter/dialects/redshift/RedshiftColumnMetadataReaderTest.java
#	jdbc-adapter/virtualschema-jdbc-adapter/src/test/java/com/exasol/adapter/dialects/redshift/RedshiftMetadataReaderTest.java
#	jdbc-adapter/virtualschema-jdbc-adapter/src/test/java/com/exasol/adapter/jdbc/BaseRemoteMetadataReaderTest.java
  • Loading branch information
redcatbear committed May 3, 2019
1 parent b04147b commit bfcc54e
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.sql.Connection;
import java.sql.Types;
import java.util.logging.Logger;

import com.exasol.adapter.AdapterProperties;
import com.exasol.adapter.dialects.IdentifierConverter;
Expand All @@ -13,6 +14,8 @@
* This class implements a Redshift-specific column metadata reader
*/
public class RedshiftColumnMetadataReader extends BaseColumnMetadataReader {
private static final Logger LOGGER = Logger.getLogger(RedshiftColumnMetadataReader.class.getName());

/**
* Create a new instance of a {@link RedshiftColumnMetadataReader}
*
Expand All @@ -27,10 +30,24 @@ public RedshiftColumnMetadataReader(final Connection connection, final AdapterPr

@Override
public DataType mapJdbcType(final JdbcTypeDescription jdbcTypeDescription) {
if (jdbcTypeDescription.getJdbcType() == Types.NUMERIC) {
switch (jdbcTypeDescription.getJdbcType()) {
case Types.NUMERIC:
return mapJdbcTypeNumericToDecimalWithFallbackToDouble(jdbcTypeDescription);
} else {
case Types.OTHER:
return mapJdbcTypeOther(jdbcTypeDescription);
default:
return super.mapJdbcType(jdbcTypeDescription);
}
}

protected DataType mapJdbcTypeOther(final JdbcTypeDescription jdbcTypeDescription) {
final String originalDataTypeName = jdbcTypeDescription.getTypeName();
if ("double".equals(originalDataTypeName)) {
return DataType.createDouble();
} else {
LOGGER.finer(() -> "Mapping JDBC type OTHER [" + jdbcTypeDescription.getTypeName()
+ "] to maximum size VARCHAR.");
}
return DataType.createMaximumSizeVarChar(DataType.ExaCharset.UTF8);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.exasol.adapter.dialects.redshift;

import java.sql.Connection;
import java.util.*;

import com.exasol.adapter.AdapterProperties;
import com.exasol.adapter.jdbc.*;
Expand Down Expand Up @@ -29,4 +30,10 @@ protected TableMetadataReader createTableMetadataReader() {
return new RedshiftTableMetadataReader(this.connection, getColumnMetadataReader(), this.properties,
getIdentifierConverter());
}

@Override
public Set<String> getSupportedTableTypes() {
return Collections
.unmodifiableSet(new HashSet<>(Arrays.asList("TABLE", "VIEW", "SYSTEM TABLE", "EXTERNAL TABLE")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* @see BaseColumnMetadataReader
*/
public abstract class AbstractMetadataReader implements MetadataReader {
static final String ANY_CATALOG_FILTER = null;
protected final AdapterProperties properties;
protected final Connection connection;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.exasol.adapter.jdbc;

import static com.exasol.adapter.jdbc.RemoteMetadataReaderConstants.ANY_TABLE;
import static com.exasol.adapter.jdbc.RemoteMetadataReaderConstants.SUPPORTED_TABLE_TYPES;

import java.sql.*;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.logging.Logger;

import com.exasol.adapter.AdapterProperties;
Expand Down Expand Up @@ -115,11 +113,16 @@ private List<TableMetadata> extractTableMetadata(final DatabaseMetaData remoteMe
final String schemaName = getSchemaNameFilter();
logTablesScan(catalogName, schemaName);
try (final ResultSet remoteTables = remoteMetadata.getTables(catalogName, schemaName, ANY_TABLE,
SUPPORTED_TABLE_TYPES.toArray(new String[0]))) {
getSupportedTableTypes().toArray(new String[0]))) {
return mapTables(remoteTables, selectedTables);
}
}

@Override
public Set<String> getSupportedTableTypes() {
return RemoteMetadataReaderConstants.DEFAULT_SUPPORTED_TABLE_TYPES;
}

protected void logTablesScan(final String catalogName, final String schemaName) {
LOGGER.fine(() -> {
final StringBuilder builder = new StringBuilder("Scanning \"");
Expand All @@ -133,11 +136,11 @@ protected void logTablesScan(final String catalogName, final String schemaName)
if (schemaName == null) {
builder.append("any schema ");
} else {
builder.append("\", schema \"");
builder.append("schema \"");
builder.append(schemaName);
builder.append("\" ");
}
builder.append(" for contained tables.");
builder.append("for contained tables.");
return builder.toString();
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.exasol.adapter.jdbc;

import java.util.List;
import java.util.Set;

import com.exasol.adapter.metadata.SchemaMetadata;

Expand Down Expand Up @@ -43,4 +44,11 @@ public interface RemoteMetadataReader extends MetadataReader {
* @return table metadata reader
*/
public TableMetadataReader getTableMetadataReader();

/**
* Get the table types the remote metadata reader supports
*
* @return set of table type names
*/
public Set<String> getSupportedTableTypes();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ private RemoteMetadataReaderConstants() {
public static final String ANY_CATALOG = null;
public static final String ANY_SCHEMA = null;
public static final String ANY_TABLE = "%";
public static final Set<String> SUPPORTED_TABLE_TYPES = Collections
.unmodifiableSet(new HashSet<>(Arrays.asList("TABLE", "VIEW", "SYSTEM TABLE")));
public static final String ANY_COLUMN = "%";
public static final String JDBC_TRUE = "yes";
public static final String JDBC_FALSE = "no";
public static final Set<String> DEFAULT_SUPPORTED_TABLE_TYPES = Collections
.unmodifiableSet(new HashSet<>(Arrays.asList("TABLE", "VIEW", "SYSTEM TABLE")));
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ void testMapJdbcTypeFallbackToParent() {
assertThat(this.columnMetadataReader.mapJdbcType(new JdbcTypeDescription(Types.BOOLEAN, 0, 0, 0, "")),
equalTo(DataType.createBool()));
}
}

@Test
void testMapJdbcTypeOtherDouble() {
assertThat(this.columnMetadataReader.mapJdbcType(new JdbcTypeDescription(Types.OTHER, 0, 0, 0, "double")),
equalTo(DataType.createDouble()));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.exasol.adapter.dialects.redshift;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

Expand Down Expand Up @@ -41,4 +40,10 @@ void testGetIdentifierConverter() {
() -> assertThat(converter.getUnquotedIdentifierHandling(),
equalTo(IdentifierCaseHandling.INTERPRET_AS_UPPER)));
}

@Test
void testGetSupportedTableTypes() {
assertThat(this.reader.getSupportedTableTypes(),
containsInAnyOrder("TABLE", "VIEW", "SYSTEM TABLE", "EXTERNAL TABLE"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ void testReadRemoteMetadataWithAdapterNotes() throws RemoteMetadataReaderExcepti
@Test
void testGetCatalogNameFilterDefaultsToAny() {
final BaseRemoteMetadataReader reader = new BaseRemoteMetadataReader(null, AdapterProperties.emptyProperties());
assertThat(reader.getCatalogNameFilter(), equalTo(AbstractMetadataReader.ANY_CATALOG_FILTER));
assertThat(reader.getCatalogNameFilter(), equalTo(RemoteMetadataReaderConstants.ANY_CATALOG));
}

@Test
Expand All @@ -203,7 +203,7 @@ void testGetCatalogNameFilter() {
@Test
void testGetSchemaNameFilterDefaultsToAny() {
final BaseRemoteMetadataReader reader = new BaseRemoteMetadataReader(null, AdapterProperties.emptyProperties());
assertThat(reader.getSchemaNameFilter(), equalTo(AbstractMetadataReader.ANY_CATALOG_FILTER));
assertThat(reader.getSchemaNameFilter(), equalTo(RemoteMetadataReaderConstants.ANY_SCHEMA));
}

@Test
Expand Down Expand Up @@ -243,6 +243,11 @@ void testCreateIdentifierConverter() {
equalTo(IdentifierCaseHandling.INTERPRET_AS_UPPER)),
() -> assertThat(reader.getIdentifierConverter().getQuotedIdentifierHandling(),
equalTo(IdentifierCaseHandling.INTERPRET_CASE_SENSITIVE)));
}

@Test
void testGetSupportedTableTypes() {
assertThat(new BaseRemoteMetadataReader(null, AdapterProperties.emptyProperties()).getSupportedTableTypes(),
containsInAnyOrder("TABLE", "VIEW", "SYSTEM TABLE"));
}
}

0 comments on commit bfcc54e

Please sign in to comment.