Search in sources :

Example 1 with CreateIndexNode

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)));
}
Also used : CreateIndexNode(io.prestosql.sql.planner.plan.CreateIndexNode) CreateIndexMetadata(io.prestosql.spi.connector.CreateIndexMetadata) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Properties(java.util.Properties) Field(io.prestosql.sql.analyzer.Field) FrameBoundType(io.prestosql.spi.sql.expression.Types.FrameBoundType) Type(io.prestosql.spi.type.Type) WindowFrameType(io.prestosql.spi.sql.expression.Types.WindowFrameType) RelationType(io.prestosql.sql.analyzer.RelationType) CreateIndex(io.prestosql.sql.tree.CreateIndex) Property(io.prestosql.sql.tree.Property)

Aggregations

CreateIndexMetadata (io.prestosql.spi.connector.CreateIndexMetadata)1 FrameBoundType (io.prestosql.spi.sql.expression.Types.FrameBoundType)1 WindowFrameType (io.prestosql.spi.sql.expression.Types.WindowFrameType)1 Type (io.prestosql.spi.type.Type)1 Field (io.prestosql.sql.analyzer.Field)1 RelationType (io.prestosql.sql.analyzer.RelationType)1 CreateIndexNode (io.prestosql.sql.planner.plan.CreateIndexNode)1 CreateIndex (io.prestosql.sql.tree.CreateIndex)1 Property (io.prestosql.sql.tree.Property)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Properties (java.util.Properties)1