-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[Transform] add support for unsigned_long data type #63940
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
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,30 +23,59 @@ | |
| import org.elasticsearch.xpack.core.ClientHelper; | ||
| import org.elasticsearch.xpack.core.transform.transforms.pivot.PivotConfig; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| public final class SchemaUtil { | ||
| private static final Logger logger = LogManager.getLogger(SchemaUtil.class); | ||
|
|
||
| // Full collection of numeric field type strings | ||
| private static final Set<String> NUMERIC_FIELD_MAPPER_TYPES; | ||
| // Full collection of numeric field type strings and whether they are floating point or not | ||
| private static final Map<String, Boolean> NUMERIC_FIELD_MAPPER_TYPES; | ||
| static { | ||
| Set<String> types = Stream.of(NumberFieldMapper.NumberType.values()) | ||
| .map(NumberFieldMapper.NumberType::typeName) | ||
| .collect(Collectors.toSet()); | ||
| types.add("scaled_float"); // have to add manually since scaled_float is in a module | ||
| Map<String, Boolean> types = Stream.of(NumberFieldMapper.NumberType.values()) | ||
| .collect(Collectors.toMap(t -> t.typeName(), t -> t.numericType().isFloatingPoint())); | ||
|
|
||
| // have to add manually since they are in a module | ||
| types.put("scaled_float", true); | ||
| types.put("unsigned_long", false); | ||
| NUMERIC_FIELD_MAPPER_TYPES = types; | ||
| } | ||
|
|
||
| private SchemaUtil() {} | ||
|
|
||
| public static boolean isNumericType(String type) { | ||
| return type != null && NUMERIC_FIELD_MAPPER_TYPES.contains(type); | ||
| return type != null && NUMERIC_FIELD_MAPPER_TYPES.containsKey(type); | ||
| } | ||
|
|
||
| /** | ||
| * Convert a numeric value to an integer if it's not a floating point number. | ||
| * | ||
| * Implementation decision: We do not care about the concrete type, but only if its floating point or not. | ||
| * Further checks (e.g. range) are done at indexing. | ||
| * | ||
| * If type is floating point and value could be an integer (ends with `.0`), we still preserve `.0` in case | ||
| * the destination index uses dynamic mappings as well as being json friendly. | ||
| * | ||
| * @param type the type of the value according to the schema we know | ||
| * @param value the value as double (aggs return double for everything) | ||
| * @return value if its floating point, a integer | ||
| */ | ||
| public static Object convertToIntegerTypeIfNeeded(String type, double value) { | ||
| if (NUMERIC_FIELD_MAPPER_TYPES.getOrDefault(type, true) == false) { | ||
| assert value % 1 == 0; | ||
| if (value < Long.MAX_VALUE) { | ||
| return (long) value; | ||
| } | ||
|
|
||
| // special case for unsigned long | ||
| return BigDecimal.valueOf(value).toBigInteger(); | ||
|
Comment on lines
+68
to
+75
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its weird to me that there is not a blanket This works in a pinch, I wonder if there is anyway to use But looking at it, there isn't a clean cut solution :/ |
||
| } | ||
|
|
||
| return value; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -188,8 +217,11 @@ private static Map<String, String> resolveMappings( | |
| } else if (destinationMapping != null) { | ||
| targetMapping.put(targetFieldName, destinationMapping); | ||
| } else { | ||
| logger.warn("Failed to deduce mapping for [{}], fall back to dynamic mapping. " + | ||
| "Create the destination index with complete mappings first to avoid deducing the mappings", targetFieldName); | ||
| logger.warn( | ||
| "Failed to deduce mapping for [{}], fall back to dynamic mapping. " | ||
| + "Create the destination index with complete mappings first to avoid deducing the mappings", | ||
| targetFieldName | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
|
|
@@ -199,8 +231,11 @@ private static Map<String, String> resolveMappings( | |
| if (destinationMapping != null) { | ||
| targetMapping.put(targetFieldName, destinationMapping); | ||
| } else { | ||
| logger.warn("Failed to deduce mapping for [{}], fall back to keyword. " + | ||
| "Create the destination index with complete mappings first to avoid deducing the mappings", targetFieldName); | ||
| logger.warn( | ||
| "Failed to deduce mapping for [{}], fall back to keyword. " | ||
| + "Create the destination index with complete mappings first to avoid deducing the mappings", | ||
| targetFieldName | ||
| ); | ||
| targetMapping.put(targetFieldName, KeywordFieldMapper.CONTENT_TYPE); | ||
| } | ||
| }); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or some better name as this is is not an
integerbut instead some possibly large discrete numeral (big int or a long).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
its referring to the mathematical definition of integer, that's why integer type, not integer. I can try to find a better name, maybe
dropFloatingPointComponentIfNeeded, but lets not over-complicate it, the doc string defines what it does.