Skip to content

Commit 99ae29b

Browse files
committed
Use FallbackSyntheticSourceBlockLoader for unsigned_long fields
1 parent 82da281 commit 99ae29b

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.index.mapper;
11+
12+
import org.elasticsearch.logsdb.datageneration.FieldType;
13+
14+
import java.util.List;
15+
import java.util.Map;
16+
import java.util.Objects;
17+
18+
public abstract class NumberFieldBlockLoaderTestCase<T extends Number> extends BlockLoaderTestCase {
19+
public NumberFieldBlockLoaderTestCase(FieldType fieldType) {
20+
super(fieldType);
21+
}
22+
23+
@Override
24+
@SuppressWarnings("unchecked")
25+
protected Object expected(Map<String, Object> fieldMapping, Object value, boolean syntheticSource) {
26+
var nullValue = fieldMapping.get("null_value") != null ? convertNullValue((Number) fieldMapping.get("null_value")) : null;
27+
28+
if (value instanceof List<?> == false) {
29+
return convert(value, nullValue);
30+
}
31+
32+
if ((boolean) fieldMapping.getOrDefault("doc_values", false)) {
33+
// Sorted and no duplicates
34+
var resultList = ((List<Object>) value).stream().map(v -> convert(v, nullValue)).filter(Objects::nonNull).sorted().toList();
35+
return maybeFoldList(resultList);
36+
}
37+
38+
// parsing from source
39+
var resultList = ((List<Object>) value).stream().map(v -> convert(v, nullValue)).filter(Objects::nonNull).toList();
40+
return maybeFoldList(resultList);
41+
}
42+
43+
@SuppressWarnings("unchecked")
44+
private T convert(Object value, T nullValue) {
45+
if (value == null) {
46+
return nullValue;
47+
}
48+
// String coercion is true by default
49+
if (value instanceof String s && s.isEmpty()) {
50+
return nullValue;
51+
}
52+
if (value instanceof Number n) {
53+
return convert(n);
54+
}
55+
56+
// Malformed values are excluded
57+
return null;
58+
}
59+
60+
protected T convertNullValue(Number nullValue) {
61+
return convert(nullValue);
62+
}
63+
64+
protected abstract T convert(Number value);
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.xpack.unsignedlong;
11+
12+
import org.elasticsearch.index.mapper.NumberFieldBlockLoaderTestCase;
13+
import org.elasticsearch.logsdb.datageneration.FieldType;
14+
import org.elasticsearch.plugins.Plugin;
15+
16+
import java.util.Collection;
17+
import java.util.List;
18+
19+
public class UnsignedLongFieldBlockLoaderTests extends NumberFieldBlockLoaderTestCase<Long> {
20+
private static final long MASK_2_63 = 0x8000000000000000L;
21+
22+
public UnsignedLongFieldBlockLoaderTests() {
23+
super(FieldType.UNSIGNED_LONG);
24+
}
25+
26+
@Override
27+
protected Long convert(Number value) {
28+
// Adjust values coming from source to the way they are stored in doc_values.
29+
// See mapper implementation.
30+
var unsigned = value.longValue();
31+
return unsigned ^ MASK_2_63;
32+
}
33+
34+
@Override
35+
protected Collection<? extends Plugin> getPlugins() {
36+
return List.of(new UnsignedLongMapperPlugin());
37+
}
38+
}

0 commit comments

Comments
 (0)