use of io.crate.types.DataType in project crate by crate.
the class MapComparator method compareMaps.
public static <K, V> int compareMaps(Map<K, V> m1, Map<K, V> m2) {
Objects.requireNonNull(m1, "map is null");
Objects.requireNonNull(m2, "map is null");
int sizeCompare = Integer.compare(m1.size(), m2.size());
if (sizeCompare != 0) {
return sizeCompare;
}
for (Map.Entry<K, V> entry : m1.entrySet()) {
V thisValue = entry.getValue();
V otherValue = m2.get(entry.getKey());
if (thisValue == null) {
if (otherValue != null) {
return 1;
} else {
continue;
}
}
if (!thisValue.equals(otherValue)) {
if (otherValue == null) {
return -1;
}
if (!thisValue.getClass().equals(otherValue.getClass())) {
DataType leftType = DataTypes.guessType(thisValue);
int cmp = leftType.compare(thisValue, leftType.implicitCast(otherValue));
if (cmp == 0) {
continue;
}
return cmp;
}
return 1;
}
}
return 0;
}
use of io.crate.types.DataType in project crate by crate.
the class Literal method typeMatchesValue.
private static <T> boolean typeMatchesValue(DataType<T> type, T value) {
if (value == null) {
return true;
}
if (type.id() == ObjectType.ID) {
// noinspection unchecked
Map<String, Object> mapValue = (Map<String, Object>) value;
ObjectType objectType = ((ObjectType) type);
for (String key : mapValue.keySet()) {
DataType<?> innerType = objectType.innerType(key);
// noinspection unchecked
if (typeMatchesValue((DataType<Object>) innerType, mapValue.get(key)) == false) {
return false;
}
}
// lets do the expensive "deep" map value conversion only after everything else succeeded
Map<String, Object> safeValue = objectType.sanitizeValue(value);
return safeValue.size() == mapValue.size();
}
return Objects.equals(type.sanitizeValue(value), value);
}
use of io.crate.types.DataType in project crate by crate.
the class ArrayMinFunctionTest method test_array_returns_min_element.
@Test
public void test_array_returns_min_element() {
List<DataType> typesToTest = new ArrayList(DataTypes.PRIMITIVE_TYPES);
typesToTest.add(DataTypes.NUMERIC);
for (DataType type : typesToTest) {
var valuesToTest = TestingHelpers.getRandomsOfType(2, 10, type);
var optional = valuesToTest.stream().filter(o -> o != null).min((o1, o2) -> type.compare(o1, o2));
var expected = optional.orElse(null);
String expression = String.format(Locale.ENGLISH, "array_min(?::%s[])", type.getName());
assertEvaluate(expression, expected, Literal.of(valuesToTest, new ArrayType<>(type)));
}
}
use of io.crate.types.DataType in project crate by crate.
the class ArraySumFunctionTest method test_array_returns_sum_of_elements.
@Test
public void test_array_returns_sum_of_elements() {
// This test picks up random numbers but controls that overflow will not happen (overflow case is checked in another test).
List<DataType> typesToTest = new ArrayList(DataTypes.NUMERIC_PRIMITIVE_TYPES);
typesToTest.add(DataTypes.NUMERIC);
for (DataType type : typesToTest) {
var valuesToTest = TestingHelpers.getRandomsOfType(1, 10, type);
DataType inputDependantOutputType = DataTypes.LONG;
if (type == DataTypes.FLOAT || type == DataTypes.DOUBLE || type == DataTypes.NUMERIC) {
inputDependantOutputType = type;
} else {
// check potential overflow and get rid of numbers causing overflow
long sum = 0;
for (int i = 0; i < valuesToTest.size(); i++) {
if (valuesToTest.get(i) != null) {
long nextNum = ((Number) valuesToTest.get(i)).longValue();
try {
sum = Math.addExact(sum, nextNum);
} catch (ArithmeticException e) {
// excluding i
valuesToTest = valuesToTest.subList(0, i);
break;
}
}
}
}
KahanSummationForDouble kahanSummationForDouble = new KahanSummationForDouble();
var optional = valuesToTest.stream().filter(Objects::nonNull).reduce((o1, o2) -> {
if (o1 instanceof BigDecimal) {
return ((BigDecimal) o1).add((BigDecimal) o2);
} else if (o1 instanceof Double || o1 instanceof Float) {
return kahanSummationForDouble.sum(((Number) o1).doubleValue(), ((Number) o2).doubleValue());
} else {
return DataTypes.LONG.implicitCast(o1) + DataTypes.LONG.implicitCast(o2);
}
});
var expected = inputDependantOutputType.implicitCast(optional.orElse(null));
String expression = String.format(Locale.ENGLISH, "array_sum(?::%s[])", type.getName());
assertEvaluate(expression, expected, Literal.of(valuesToTest, new ArrayType<>(type)));
}
}
use of io.crate.types.DataType in project crate by crate.
the class SQLIntegrationTestCase method assertFunctionIsCreatedOnAll.
public void assertFunctionIsCreatedOnAll(String schema, String name, List<DataType<?>> argTypes) throws Exception {
SearchPath searchPath = SearchPath.pathWithPGCatalogAndDoc();
assertBusy(() -> {
Iterable<Functions> functions = internalCluster().getInstances(Functions.class);
for (Functions function : functions) {
FunctionImplementation func = function.get(schema, name, Lists2.map(argTypes, t -> Literal.of(t, null)), searchPath);
assertThat(func, is(not(nullValue())));
assertThat(func.info().ident().argumentTypes(), is(equalTo(argTypes)));
}
}, 20L, TimeUnit.SECONDS);
}
Aggregations