matriv commented on a change in pull request #18029:
URL: https://github.com/apache/flink/pull/18029#discussion_r763097366
##########
File path:
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/catalog/SchemaTranslator.java
##########
@@ -113,19 +114,42 @@ public static ProducingResult createProducingResult(
ResolvedSchema inputSchema,
AbstractDataType<?> targetDataType) {
final List<String> inputFieldNames = inputSchema.getColumnNames();
+ final List<String> inputFieldNamesNormalized =
+ inputFieldNames.stream()
+ .map(n -> n.toUpperCase(Locale.ROOT))
+ .collect(Collectors.toList());
final DataType resolvedDataType =
dataTypeFactory.createDataType(targetDataType);
final List<String> targetFieldNames = flattenToNames(resolvedDataType);
+ final List<String> targetFieldNamesNormalized =
+ targetFieldNames.stream()
+ .map(n -> n.toUpperCase(Locale.ROOT))
+ .collect(Collectors.toList());
final List<DataType> targetFieldDataTypes =
flattenToDataTypes(resolvedDataType);
// help in reorder fields for POJOs if all field names are present but
out of order,
// otherwise let the sink validation fail later
- final List<String> projections;
- if (targetFieldNames.size() == inputFieldNames.size()
- && !targetFieldNames.equals(inputFieldNames)
- && targetFieldNames.containsAll(inputFieldNames)) {
- projections = targetFieldNames;
- } else {
- projections = null;
+ List<String> projections = null;
+ if (targetFieldNames.size() == inputFieldNames.size()) {
+ // reordering by name (case-sensitive)
+ if (targetFieldNames.containsAll(inputFieldNames)) {
+ projections = targetFieldNames;
+ }
+ // reordering by name (case-insensitive) but fields must be unique
+ else if
(targetFieldNamesNormalized.containsAll(inputFieldNamesNormalized)
+ && targetFieldNamesNormalized.stream().distinct().count()
+ == targetFieldNames.size()
+ && inputFieldNamesNormalized.stream().distinct().count()
+ == inputFieldNames.size()) {
+ projections =
+ targetFieldNamesNormalized.stream()
+ .map(
+ targetName -> {
+ final int inputFieldPos =
+
inputFieldNamesNormalized.indexOf(targetName);
Review comment:
This is not performance critical, but maybe we can create a `Map<String,
Integer>` where the values are the index positions and get them directly
without the O(n) of `indexOf`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]