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

feat: native map/array constructors #4232

Merged
merged 3 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
fix: andys comments
  • Loading branch information
agavra committed Jan 17, 2020
commit 79dff2ffe287a7fff2893d6e584652a3baeec219
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ public Void visitCreateArrayExpression(
if (sqlTypes.size() == 0) {
throw new KsqlException("Cannot construct an array with all NULL elements "
+ "(see https://github.com/confluentinc/ksql/issues/4239). As a workaround, you may "
+ "cast the NULL value to a desired type.");
+ "cast a NULL value to the desired type.");
}

if (new HashSet<>(sqlTypes).size() != 1) {
Expand All @@ -383,7 +383,7 @@ public Void visitCreateMapExpression(
+ "value pair (see https://github.com/confluentinc/ksql/issues/4239).");
}

final List<SqlType> gkeyTypes = exp.getMap()
final List<SqlType> keyTypes = exp.getMap()
.keySet()
.stream()
.map(key -> {
Expand All @@ -392,11 +392,11 @@ public Void visitCreateMapExpression(
})
.collect(Collectors.toList());

if (gkeyTypes.stream().anyMatch(type -> !SqlTypes.STRING.equals(type))) {
throw new KsqlException("Only STRING keys are supported in maps but got: " + gkeyTypes);
if (keyTypes.stream().anyMatch(type -> !SqlTypes.STRING.equals(type))) {
throw new KsqlException("Only STRING keys are supported in maps but got: " + keyTypes);
}

final List<SqlType> sqlTypes = exp.getMap()
final List<SqlType> valueTypes = exp.getMap()
.values()
.stream()
.map(val -> {
Expand All @@ -406,19 +406,20 @@ public Void visitCreateMapExpression(
.distinct()
.collect(Collectors.toList());

if (sqlTypes.size() != 1) {
if (valueTypes.size() != 1) {
throw new KsqlException(
String.format(
"Cannot construct a map with mismatching value types (%s) from expression %s.",
sqlTypes,
valueTypes,
exp));
}

if (sqlTypes.get(0) == null) {
throw new KsqlException("Cannot construct a map with NULL values.");
if (valueTypes.get(0) == null) {
throw new KsqlException("Cannot construct MAP with NULL values. As a workaround, you "
+ "may cast a NULL value to the desired type.");
}

context.setSqlType(SqlMap.of(sqlTypes.get(0)));
context.setSqlType(SqlMap.of(valueTypes.get(0)));
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ public void shouldThrowOnMapOfNullValues() {

// Expect
expectedException.expect(KsqlException.class);
expectedException.expectMessage("Cannot construct a map with NULL values");
expectedException.expectMessage("Cannot construct MAP with NULL values");

// When:
expressionTypeManager.getExpressionSqlType(expression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
],
"expectedException": {
"type": "io.confluent.ksql.util.KsqlException",
"message": "Cannot construct an array with all NULL elements (see https://github.com/confluentinc/ksql/issues/4239). As a workaround, you may cast the NULL value to a desired type."
"message": "Cannot construct an array with all NULL elements (see https://github.com/confluentinc/ksql/issues/4239). As a workaround, you may cast a NULL value to the desired type."
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@
{
"name": "should handle arbitrary nested expressions",
"statements": [
"CREATE STREAM TEST (I INT, A ARRAY<ARRAY<INT>>) WITH (kafka_topic='test_topic', value_format='JSON');",
"CREATE STREAM TEST (I INT, A ARRAY<ARRAY<BIGINT>>) WITH (kafka_topic='test_topic', value_format='JSON');",
"INSERT INTO TEST (I, A) VALUES (-1, ARRAY[ARRAY[1]]);"
],
"inputs": [
Expand All @@ -249,7 +249,7 @@
{
"name": "should handle map expressions",
"statements": [
"CREATE STREAM TEST (I INT, A MAP<VARCHAR, INT>) WITH (kafka_topic='test_topic', value_format='JSON');",
"CREATE STREAM TEST (I INT, A MAP<VARCHAR, BIGINT>) WITH (kafka_topic='test_topic', value_format='JSON');",
"INSERT INTO TEST (I, A) VALUES (-1, MAP('a':=0, 'b':=1));"
],
"inputs": [
Expand Down