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

fix: schema converters should handle List and Map impl (MINOR) #3290

Merged
merged 1 commit into from
Sep 3, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Function;
import org.apache.kafka.connect.data.Schema;
import org.apache.kafka.connect.data.SchemaBuilder;
Expand Down Expand Up @@ -285,12 +286,11 @@ private static class JavaToSqlConverter implements JavaToSqlTypeConverter {

@Override
public SqlBaseType toSqlType(final Class<?> javaType) {
final SqlBaseType sqlType = JAVA_TO_SQL.get(javaType);
if (sqlType == null) {
throw new KsqlException("Unexpected java type: " + javaType);
}

return sqlType;
return JAVA_TO_SQL.entrySet().stream()
.filter(e -> e.getKey().isAssignableFrom(javaType))
.map(Entry::getValue)
.findAny()
.orElseThrow(() -> new KsqlException("Unexpected java type: " + javaType));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import io.confluent.ksql.schema.ksql.types.SqlArray;
import io.confluent.ksql.schema.ksql.types.SqlDecimal;
Expand All @@ -31,6 +33,8 @@
import io.confluent.ksql.util.DecimalUtil;
import io.confluent.ksql.util.KsqlException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -177,6 +181,26 @@ public void shouldGetSqlTypeForAllJavaTypes() {
});
}

@Test
public void shouldGetSqArrayForImplementationsOfJavaList() {
ImmutableList.<Class<?>>of(
ArrayList.class,
ImmutableList.class
).forEach(javaType -> {
assertThat(SchemaConverters.javaToSqlConverter().toSqlType(javaType), is(SqlBaseType.ARRAY));
});
}

@Test
public void shouldGetSqlMapForImplementationsOfJavaMap() {
ImmutableList.<Class<?>>of(
HashMap.class,
ImmutableMap.class
).forEach(javaType -> {
assertThat(SchemaConverters.javaToSqlConverter().toSqlType(javaType), is(SqlBaseType.MAP));
});
}

@Test
public void shouldConvertNestedComplexToSql() {
assertThat(SchemaConverters.connectToSqlConverter().toSqlType(NESTED_LOGICAL_TYPE), is(NESTED_SQL_TYPE));
Expand Down