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: insert/values does not convert Number to String #3282

Closed
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 @@ -58,6 +58,10 @@ public <T> Optional<T> coerce(final Object value, final SqlType targetType) {
return Optional.empty();
}

if (targetType.baseType() == SqlBaseType.STRING) {
return optional(String.valueOf(value));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if we should coerce all types - what if it's a struct, or an array (I know we don't support these yet, but it might make sense to just explicitly convert the types that we what we want to)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition is called after the value has been checked for a Number type (see the previous if). So at this point, we're only converting Number types to String. Or am I missing something?

}

final Number result = UPCASTER.get(targetType.baseType()).apply((Number) value);
return optional(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

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.SqlTypes;
import io.confluent.ksql.util.KsqlException;
import java.math.BigDecimal;
Expand Down Expand Up @@ -140,6 +141,17 @@ public void shouldCoerceStringToDecimal() {
assertThat(coercer.coerce(val, SqlTypes.decimal(2, 1)), is(Optional.of(new BigDecimal("1.0"))));
}

@Test
public void shouldCoerceNumbersToString() {
ImmutableSet.of(
1,
2L,
3.3
).forEach(number -> {
assertThat(coercer.coerce(number, SqlTypes.STRING), is(Optional.of(String.valueOf(number))));
});
}

@Test
public void shouldThrowIfInvalidCoercionString() {
// Given:
Expand Down