use of io.crate.types.DataType in project crate by crate.
the class ArrayMaxFunctionTest method test_array_returns_max_element.
@Test
public void test_array_returns_max_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).max((o1, o2) -> type.compare(o1, o2));
var expected = optional.orElse(null);
String expression = String.format(Locale.ENGLISH, "array_max(?::%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 DocIndexMetadataTest method testCreateTableMappingGenerationAndParsingArrayInsideObject.
@Test
public void testCreateTableMappingGenerationAndParsingArrayInsideObject() throws Exception {
DocIndexMetadata md = getDocIndexMetadataFromStatement("create table t1 (" + "id int primary key," + "details object as (names array(string))" + ") with (number_of_replicas=0)");
DataType type = md.references().get(new ColumnIdent("details", "names")).valueType();
assertThat(type, Matchers.equalTo(new ArrayType(DataTypes.STRING)));
}
use of io.crate.types.DataType in project crate by crate.
the class InsertFromSubQueryAnalyzedStatement method rewriteNestedInputToSubscript.
private Symbol rewriteNestedInputToSubscript(ColumnIdent columnIdent, Symbol inputSymbol) {
Reference reference = tableInfo().getReference(columnIdent);
Symbol symbol = inputSymbol;
Iterator<String> pathIt = columnIdent.path().iterator();
while (pathIt.hasNext()) {
// rewrite object access to subscript scalar
String key = pathIt.next();
DataType returnType = DataTypes.OBJECT;
if (!pathIt.hasNext()) {
returnType = reference.valueType();
}
FunctionIdent functionIdent = new FunctionIdent(SubscriptObjectFunction.NAME, ImmutableList.<DataType>of(DataTypes.OBJECT, DataTypes.STRING));
symbol = new Function(new FunctionInfo(functionIdent, returnType), Arrays.asList(symbol, Literal.of(key)));
}
return symbol;
}
use of io.crate.types.DataType in project crate by crate.
the class DocIndexMetaData method getColumnDataType.
/**
* extract dataType from given columnProperties
*
* @param columnProperties map of String to Object containing column properties
* @return dataType of the column with columnProperties
*/
public static DataType getColumnDataType(Map<String, Object> columnProperties) {
DataType type;
String typeName = (String) columnProperties.get("type");
if (typeName == null) {
if (columnProperties.containsKey("properties")) {
type = DataTypes.OBJECT;
} else {
return DataTypes.NOT_SUPPORTED;
}
} else if (typeName.equalsIgnoreCase("array")) {
Map<String, Object> innerProperties = getNested(columnProperties, "inner");
DataType innerType = getColumnDataType(innerProperties);
type = new ArrayType(innerType);
} else {
typeName = typeName.toLowerCase(Locale.ENGLISH);
type = MoreObjects.firstNonNull(DataTypes.ofMappingName(typeName), DataTypes.NOT_SUPPORTED);
}
return type;
}
use of io.crate.types.DataType in project crate by crate.
the class DocIndexMetaData method internalExtractColumnDefinitions.
/**
* extracts index definitions as well
*/
@SuppressWarnings("unchecked")
private void internalExtractColumnDefinitions(@Nullable ColumnIdent columnIdent, @Nullable Map<String, Object> propertiesMap) {
if (propertiesMap == null) {
return;
}
for (Map.Entry<String, Object> columnEntry : propertiesMap.entrySet()) {
Map<String, Object> columnProperties = (Map) columnEntry.getValue();
DataType columnDataType = getColumnDataType(columnProperties);
ColumnIdent newIdent = childIdent(columnIdent, columnEntry.getKey());
boolean nullable = !notNullColumns.contains(newIdent);
columnProperties = furtherColumnProperties(columnProperties);
Reference.IndexType columnIndexType = getColumnIndexType(columnProperties);
if (columnDataType == DataTypes.GEO_SHAPE) {
String geoTree = (String) columnProperties.get("tree");
String precision = (String) columnProperties.get("precision");
Integer treeLevels = (Integer) columnProperties.get("tree_levels");
Double distanceErrorPct = (Double) columnProperties.get("distance_error_pct");
addGeoReference(newIdent, geoTree, precision, treeLevels, distanceErrorPct);
} else if (columnDataType == DataTypes.OBJECT || (columnDataType.id() == ArrayType.ID && ((ArrayType) columnDataType).innerType() == DataTypes.OBJECT)) {
ColumnPolicy columnPolicy = ColumnPolicy.of(columnProperties.get("dynamic"));
add(newIdent, columnDataType, columnPolicy, Reference.IndexType.NO, false, nullable);
if (columnProperties.get("properties") != null) {
// walk nested
internalExtractColumnDefinitions(newIdent, (Map<String, Object>) columnProperties.get("properties"));
}
} else if (columnDataType != DataTypes.NOT_SUPPORTED) {
List<String> copyToColumns = getNested(columnProperties, "copy_to");
// extract columns this column is copied to, needed for indices
if (copyToColumns != null) {
for (String copyToColumn : copyToColumns) {
ColumnIdent targetIdent = ColumnIdent.fromPath(copyToColumn);
IndexReference.Builder builder = getOrCreateIndexBuilder(targetIdent);
builder.addColumn(newInfo(newIdent, columnDataType, ColumnPolicy.DYNAMIC, columnIndexType, false));
}
}
// is it an index?
if (indicesMap.containsKey(newIdent.fqn())) {
IndexReference.Builder builder = getOrCreateIndexBuilder(newIdent);
builder.indexType(columnIndexType).analyzer((String) columnProperties.get("analyzer"));
} else {
add(newIdent, columnDataType, columnIndexType, nullable);
}
}
}
}
Aggregations