use of io.crate.types.ArrayType in project crate by crate.
the class SelectStatementAnalyzerTest method testTryCastExpression.
@Test
public void testTryCastExpression() throws Exception {
SelectAnalyzedStatement analysis = analyze("select try_cast(other_id as string) from users");
assertThat(analysis.relation().querySpec().outputs().get(0), isFunction(CastFunctionResolver.tryFunctionsMap().get(DataTypes.STRING), Arrays.<DataType>asList(DataTypes.LONG)));
analysis = analyze("select try_cast(1+1 as string) from users");
assertThat(analysis.relation().querySpec().outputs().get(0), isLiteral("2", DataTypes.STRING));
analysis = analyze("select try_cast(null as string) from users");
assertThat(analysis.relation().querySpec().outputs().get(0), isLiteral(null, DataTypes.STRING));
analysis = analyze("select try_cast(counters as array(boolean)) from users");
assertThat(analysis.relation().querySpec().outputs().get(0), isFunction(CastFunctionResolver.tryFunctionsMap().get(new ArrayType(DataTypes.BOOLEAN)), Arrays.<DataType>asList(new ArrayType(DataTypes.LONG))));
}
use of io.crate.types.ArrayType in project crate by crate.
the class ParameterContextTest method testBulkNestedNested.
@Test
public void testBulkNestedNested() throws Exception {
Object[][] bulkArgs = new Object[][] { new Object[] { new String[][] { new String[] { null } } }, new Object[] { new String[][] { new String[] { "foo" } } } };
ParameterContext ctx = new ParameterContext(Row.EMPTY, Rows.of(bulkArgs));
assertThat(ctx.getAsSymbol(0), isLiteral(bulkArgs[0][0], new ArrayType(new ArrayType(DataTypes.UNDEFINED))));
}
use of io.crate.types.ArrayType in project crate by crate.
the class Literal method typeMatchesValue.
private static boolean typeMatchesValue(DataType type, Object value) {
if (value == null) {
return true;
}
if (type.equals(DataTypes.STRING) && (value instanceof BytesRef || value instanceof String)) {
return true;
}
if (type instanceof ArrayType) {
DataType innerType = ((ArrayType) type).innerType();
while (innerType instanceof ArrayType && value.getClass().isArray()) {
type = innerType;
innerType = ((ArrayType) innerType).innerType();
value = ((Object[]) value)[0];
}
if (innerType.equals(DataTypes.STRING)) {
for (Object o : ((Object[]) value)) {
if (o != null && !(o instanceof String || o instanceof BytesRef)) {
return false;
}
}
return true;
} else {
return Arrays.equals((Object[]) value, ((ArrayType) type).value(value));
}
}
// types like GeoPoint are represented as arrays
if (value.getClass().isArray() && Arrays.equals((Object[]) value, (Object[]) type.value(value))) {
return true;
}
return type.value(value).equals(value);
}
use of io.crate.types.ArrayType in project crate by crate.
the class ESFieldExtractorTest method testNullInList.
@Test
public void testNullInList() throws Exception {
ESFieldExtractor.Source ex = new ESFieldExtractor.Source(new ColumnIdent("top", "child1"), new ArrayType(DataTypes.INTEGER));
// test null value in list
HashMap<String, Object> nullMap = new HashMap<String, Object>(1);
nullMap.put("child1", null);
ImmutableMap<String, Object> source = ImmutableMap.<String, Object>of("top", ImmutableList.of(nullMap, ImmutableMap.of("child1", 33)));
Object[] objects = (Object[]) ex.toValue(source);
assertThat(objects[0], nullValue());
assertThat(((Integer) objects[1]), is(33));
}
use of io.crate.types.ArrayType in project crate by crate.
the class AnyLikeOperatorTest method normalizeSymbol.
private static Symbol normalizeSymbol(String pattern, String... expressions) {
Literal patternLiteral = Literal.of(pattern);
Object[] value = new Object[expressions.length];
for (int i = 0; i < expressions.length; i++) {
value[i] = expressions[i] == null ? null : new BytesRef(expressions[i]);
}
Literal valuesLiteral = Literal.of(new ArrayType(DataTypes.STRING), value);
AnyLikeOperator impl = (AnyLikeOperator) new AnyLikeOperator.AnyLikeResolver().getForTypes(Arrays.asList(patternLiteral.valueType(), valuesLiteral.valueType()));
Function function = new Function(impl.info(), Arrays.<Symbol>asList(patternLiteral, valuesLiteral));
return impl.normalizeSymbol(function, new TransactionContext(SessionContext.SYSTEM_SESSION));
}
Aggregations