use of io.crate.sql.tree.ColumnPolicy 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.sql.tree.ColumnPolicy in project crate by crate.
the class DocTableInfo method getDynamic.
@Nullable
public DynamicReference getDynamic(ColumnIdent ident, boolean forWrite, boolean errorOnUnknownObjectKey) {
boolean parentIsIgnored = false;
ColumnPolicy parentPolicy = columnPolicy();
int position = 0;
if (!ident.isTopLevel()) {
// see if parent is strict object
ColumnIdent parentIdent = ident.getParent();
Reference parentInfo = null;
while (parentIdent != null) {
parentInfo = getReference(parentIdent);
if (parentInfo != null) {
break;
}
parentIdent = parentIdent.getParent();
}
if (parentInfo != null) {
parentPolicy = parentInfo.columnPolicy();
position = parentInfo.position();
}
}
switch(parentPolicy) {
case DYNAMIC:
if (!forWrite) {
if (!errorOnUnknownObjectKey) {
return new VoidReference(new ReferenceIdent(ident(), ident), rowGranularity(), position);
}
return null;
}
break;
case STRICT:
if (forWrite) {
throw new ColumnUnknownException(ident.sqlFqn(), ident());
}
return null;
case IGNORED:
parentIsIgnored = true;
break;
default:
break;
}
if (parentIsIgnored) {
return new DynamicReference(new ReferenceIdent(ident(), ident), rowGranularity(), ColumnPolicy.IGNORED, position);
}
return new DynamicReference(new ReferenceIdent(ident(), ident), rowGranularity(), position);
}
Aggregations