use of io.crate.types.ArrayType in project crate by crate.
the class Function method castArrayElements.
private Symbol castArrayElements(DataType<?> newDataType, CastMode... modes) {
DataType<?> innerType = ((ArrayType<?>) newDataType).innerType();
ArrayList<Symbol> newArgs = new ArrayList<>(arguments.size());
for (Symbol arg : arguments) {
try {
newArgs.add(arg.cast(innerType, modes));
} catch (ConversionException e) {
throw new ConversionException(returnType, newDataType);
}
}
return new Function(signature, newArgs, newDataType, null);
}
use of io.crate.types.ArrayType in project crate by crate.
the class ArraySumFunction method register.
public static void register(ScalarFunctionModule module) {
module.register(Signature.scalar(NAME, new ArrayType(DataTypes.NUMERIC).getTypeSignature(), DataTypes.NUMERIC.getTypeSignature()), ArraySumFunction::new);
for (var supportedType : DataTypes.NUMERIC_PRIMITIVE_TYPES) {
DataType inputDependantOutputType = DataTypes.LONG;
if (supportedType == DataTypes.FLOAT || supportedType == DataTypes.DOUBLE) {
inputDependantOutputType = supportedType;
}
module.register(Signature.scalar(NAME, new ArrayType(supportedType).getTypeSignature(), inputDependantOutputType.getTypeSignature()), ArraySumFunction::new);
}
}
use of io.crate.types.ArrayType in project crate by crate.
the class ArrayAvgFunction method register.
public static void register(ScalarFunctionModule module) {
// All types except float and double have numeric average
// https://www.postgresql.org/docs/13/functions-aggregate.html
module.register(Signature.scalar(NAME, new ArrayType(DataTypes.NUMERIC).getTypeSignature(), DataTypes.NUMERIC.getTypeSignature()), (signature, boundSignature) -> new UnaryScalar<>(signature, boundSignature, new ArrayType(DataTypes.NUMERIC), Operations.NUMERIC.getFunction()));
module.register(Signature.scalar(NAME, new ArrayType(DataTypes.FLOAT).getTypeSignature(), DataTypes.FLOAT.getTypeSignature()), (signature, boundSignature) -> new UnaryScalar<>(signature, boundSignature, new ArrayType(DataTypes.FLOAT), Operations.FLOAT.getFunction()));
module.register(Signature.scalar(NAME, new ArrayType(DataTypes.DOUBLE).getTypeSignature(), DataTypes.DOUBLE.getTypeSignature()), (signature, boundSignature) -> new UnaryScalar<>(signature, boundSignature, new ArrayType(DataTypes.DOUBLE), Operations.DOUBLE.getFunction()));
for (var supportedType : DataTypes.NUMERIC_PRIMITIVE_TYPES) {
if (supportedType != DataTypes.FLOAT && supportedType != DataTypes.DOUBLE) {
module.register(Signature.scalar(NAME, new ArrayType(supportedType).getTypeSignature(), DataTypes.NUMERIC.getTypeSignature()), (signature, boundSignature) -> new UnaryScalar<>(signature, boundSignature, new ArrayType(supportedType), Operations.INTEGRAL_PRIMITIVE.getFunction()));
}
}
}
use of io.crate.types.ArrayType 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;
}
var columns = propertiesMap.entrySet().stream().sorted(SORT_BY_POSITION_THEN_NAME).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
for (Map.Entry<String, Object> columnEntry : columns.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);
int position = columnPosition((int) columnProperties.getOrDefault("position", 0));
String defaultExpression = (String) columnProperties.getOrDefault("default_expr", null);
Reference.IndexType columnIndexType = getColumnIndexType(columnProperties);
StorageSupport storageSupport = columnDataType.storageSupport();
assert storageSupport != null : "DataType used in table definition must have storage support: " + columnDataType;
boolean docValuesDefault = storageSupport.getComputedDocValuesDefault(columnIndexType);
boolean hasDocValues = Booleans.parseBoolean(columnProperties.getOrDefault(DOC_VALUES, docValuesDefault).toString());
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(position, newIdent, geoTree, precision, treeLevels, distanceErrorPct);
} else if (columnDataType.id() == ObjectType.ID || (columnDataType.id() == ArrayType.ID && ((ArrayType) columnDataType).innerType().id() == ObjectType.ID)) {
ColumnPolicy columnPolicy = ColumnPolicies.decodeMappingValue(columnProperties.get("dynamic"));
add(position, newIdent, columnDataType, defaultExpression, columnPolicy, Reference.IndexType.NONE, nullable, hasDocValues);
if (columnProperties.get("properties") != null) {
// walk nested
internalExtractColumnDefinitions(newIdent, (Map<String, Object>) columnProperties.get("properties"));
}
} else if (columnDataType != DataTypes.NOT_SUPPORTED) {
List<String> copyToColumns = Maps.get(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(position, newIdent, columnDataType, defaultExpression, ColumnPolicy.DYNAMIC, columnIndexType, false, hasDocValues));
}
}
// is it an index?
if (indicesMap.containsKey(newIdent.fqn())) {
IndexReference.Builder builder = getOrCreateIndexBuilder(newIdent);
builder.indexType(columnIndexType).analyzer((String) columnProperties.get("analyzer"));
} else {
add(position, newIdent, columnDataType, defaultExpression, ColumnPolicy.DYNAMIC, columnIndexType, nullable, hasDocValues);
}
}
}
}
use of io.crate.types.ArrayType 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 || ObjectType.NAME.equals(typeName)) {
Map<String, Object> innerProperties = (Map<String, Object>) columnProperties.get("properties");
if (innerProperties != null) {
ObjectType.Builder builder = ObjectType.builder();
for (Map.Entry<String, Object> entry : innerProperties.entrySet()) {
builder.setInnerType(entry.getKey(), getColumnDataType((Map<String, Object>) entry.getValue()));
}
type = builder.build();
} else {
type = Objects.requireNonNullElse(DataTypes.ofMappingName(typeName), DataTypes.NOT_SUPPORTED);
}
} else if (typeName.equalsIgnoreCase("array")) {
Map<String, Object> innerProperties = Maps.get(columnProperties, "inner");
DataType<?> innerType = getColumnDataType(innerProperties);
type = new ArrayType<>(innerType);
} else {
typeName = typeName.toLowerCase(Locale.ENGLISH);
switch(typeName) {
case DateFieldMapper.CONTENT_TYPE:
Boolean ignoreTimezone = (Boolean) columnProperties.get("ignore_timezone");
if (ignoreTimezone != null && ignoreTimezone) {
return DataTypes.TIMESTAMP;
} else {
return DataTypes.TIMESTAMPZ;
}
case KeywordFieldMapper.CONTENT_TYPE:
Integer lengthLimit = (Integer) columnProperties.get("length_limit");
return lengthLimit != null ? StringType.of(lengthLimit) : DataTypes.STRING;
case BitStringFieldMapper.CONTENT_TYPE:
Integer length = (Integer) columnProperties.get("length");
assert length != null : "Length is required for bit string type";
return new BitStringType(length);
default:
type = Objects.requireNonNullElse(DataTypes.ofMappingName(typeName), DataTypes.NOT_SUPPORTED);
}
}
return type;
}
Aggregations