use of io.prestosql.sql.planner.plan.CreateIndexNode in project hetu-core by openlookeng.
the class QueryPlanner method createIndex.
/**
* CREATE INDEX statements are rewritten as SELECT statements,
* if the original statement was CREATE INDEX, create the necessary plan nodes
* to create the index
*/
private PlanBuilder createIndex(PlanBuilder subPlan, Statement originalStatement) {
if (!(originalStatement instanceof CreateIndex)) {
return subPlan;
}
// rewrite sub queries
CreateIndex createIndex = (CreateIndex) originalStatement;
String tableName = MetadataUtil.createQualifiedObjectName(session, originalStatement, createIndex.getTableName()).toString();
List<String> partitions = new ArrayList<>();
if (createIndex.getExpression().isPresent()) {
partitions = HeuristicIndexUtils.extractPartitions(createIndex.getExpression().get());
}
Map<String, Type> columnTypes = new HashMap<>();
for (Field field : analysis.getRootScope().getRelationType().getAllFields()) {
if (INDEX_SUPPORTED_TYPES.get(createIndex.getIndexType().toLowerCase(Locale.ENGLISH)).stream().noneMatch(supportType -> field.getType().getDisplayName().contains(supportType))) {
throw new UnsupportedOperationException("Index creation on " + field.getType().getDisplayName() + " column is not supported");
}
columnTypes.put(field.getOriginColumnName().get(), field.getType());
}
Properties indexProperties = new Properties();
CreateIndexMetadata.Level indexCreationLevel = CreateIndexMetadata.Level.UNDEFINED;
indexProperties.setProperty(LEVEL_PROP_KEY, indexCreationLevel.toString());
boolean autoLoadFound = false;
for (Property property : createIndex.getProperties()) {
String key = extractPropertyValue(property.getName());
String val = extractPropertyValue(property.getValue()).toUpperCase(Locale.ENGLISH);
if (key.equals(LEVEL_PROP_KEY)) {
indexCreationLevel = CreateIndexMetadata.Level.valueOf(val);
continue;
}
if (key.equals(AUTOLOAD_PROP_KEY)) {
autoLoadFound = true;
String valInLowerCase = val.toLowerCase(Locale.ROOT);
if (valInLowerCase.equals("true") || valInLowerCase.equals("false")) {
indexProperties.setProperty(key, valInLowerCase);
} else {
throw new IllegalArgumentException("Unrecognized value for key '" + AUTOLOAD_PROP_KEY + "', only 'true' or 'false' are allowed");
}
continue;
}
indexProperties.setProperty(key, val);
}
if (!autoLoadFound) {
boolean defaultAutoloadProp = PropertyService.getBooleanProperty(HetuConstant.FILTER_CACHE_AUTOLOAD_DEFAULT);
indexProperties.setProperty(AUTOLOAD_PROP_KEY, String.valueOf(defaultAutoloadProp));
}
return subPlan.withNewRoot(new CreateIndexNode(idAllocator.getNextId(), ExchangeNode.gatheringExchange(idAllocator.getNextId(), ExchangeNode.Scope.REMOTE, subPlan.getRoot()), new CreateIndexMetadata(createIndex.getIndexName().toString(), tableName, createIndex.getIndexType(), 0L, createIndex.getColumnAliases().stream().map(identifier -> new Pair<>(identifier.toString(), columnTypes.get(identifier.toString().toLowerCase(Locale.ROOT)))).collect(Collectors.toList()), partitions, indexProperties, session.getUser(), indexCreationLevel)));
}
Aggregations