Search in sources :

Example 6 with TableInfo

use of io.crate.metadata.table.TableInfo in project crate by crate.

the class RoutedCollectPhase method forQueriedTable.

public static RoutedCollectPhase forQueriedTable(Planner.Context plannerContext, QueriedTableRelation table, List<Symbol> toCollect, List<Projection> projections) {
    TableInfo tableInfo = table.tableRelation().tableInfo();
    WhereClause where = table.querySpec().where();
    if (table.tableRelation() instanceof TableFunctionRelation) {
        TableFunctionRelation tableFunctionRelation = (TableFunctionRelation) table.tableRelation();
        plannerContext.normalizer().normalizeInplace(tableFunctionRelation.function().arguments(), plannerContext.transactionContext());
        return new TableFunctionCollectPhase(plannerContext.jobId(), plannerContext.nextExecutionPhaseId(), plannerContext.allocateRouting(tableInfo, where, null), tableFunctionRelation, projections, toCollect, where);
    }
    return new RoutedCollectPhase(plannerContext.jobId(), plannerContext.nextExecutionPhaseId(), "collect", plannerContext.allocateRouting(tableInfo, where, null), tableInfo.rowGranularity(), toCollect, projections, where, DistributionInfo.DEFAULT_BROADCAST);
}
Also used : WhereClause(io.crate.analyze.WhereClause) TableInfo(io.crate.metadata.table.TableInfo) TableFunctionRelation(io.crate.analyze.relations.TableFunctionRelation)

Example 7 with TableInfo

use of io.crate.metadata.table.TableInfo in project crate by crate.

the class CreateSnapshotAnalyzer method analyze.

public CreateSnapshotAnalyzedStatement analyze(CreateSnapshot node, Analysis analysis) {
    Optional<QualifiedName> repositoryName = node.name().getPrefix();
    // validate repository
    Preconditions.checkArgument(repositoryName.isPresent(), "Snapshot must be specified by \"<repository_name>\".\"<snapshot_name>\"");
    Preconditions.checkArgument(repositoryName.get().getParts().size() == 1, String.format(Locale.ENGLISH, "Invalid repository name '%s'", repositoryName.get()));
    repositoryService.failIfRepositoryDoesNotExist(repositoryName.get().toString());
    // snapshot existence in repo is validated upon execution
    String snapshotName = node.name().getSuffix();
    SnapshotId snapshotId = new SnapshotId(repositoryName.get().toString(), snapshotName);
    // validate and extract settings
    Settings settings = GenericPropertiesConverter.settingsFromProperties(node.properties(), analysis.parameterContext(), SETTINGS).build();
    boolean ignoreUnavailable = settings.getAsBoolean(IGNORE_UNAVAILABLE.name(), IGNORE_UNAVAILABLE.defaultValue());
    // iterate tables
    if (node.tableList().isPresent()) {
        List<Table> tableList = node.tableList().get();
        Set<String> snapshotIndices = new HashSet<>(tableList.size());
        for (Table table : tableList) {
            TableInfo tableInfo;
            try {
                tableInfo = schemas.getTableInfo(TableIdent.of(table, analysis.sessionContext().defaultSchema()));
            } catch (ResourceUnknownException e) {
                if (ignoreUnavailable) {
                    LOGGER.info("ignoring: {}", e.getMessage());
                    continue;
                } else {
                    throw e;
                }
            }
            if (!(tableInfo instanceof DocTableInfo)) {
                throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Cannot create snapshot of tables in schema '%s'", tableInfo.ident().schema()));
            }
            Operation.blockedRaiseException(tableInfo, Operation.READ);
            DocTableInfo docTableInfo = (DocTableInfo) tableInfo;
            if (table.partitionProperties().isEmpty()) {
                snapshotIndices.addAll(Arrays.asList(docTableInfo.concreteIndices()));
            } else {
                PartitionName partitionName = PartitionPropertiesAnalyzer.toPartitionName(docTableInfo, table.partitionProperties(), analysis.parameterContext().parameters());
                if (!docTableInfo.partitions().contains(partitionName)) {
                    if (!ignoreUnavailable) {
                        throw new PartitionUnknownException(tableInfo.ident().fqn(), partitionName.ident());
                    } else {
                        LOGGER.info("ignoring unknown partition of table '{}' with ident '{}'", partitionName.tableIdent(), partitionName.ident());
                    }
                } else {
                    snapshotIndices.add(partitionName.asIndexName());
                }
            }
        }
        /**
             * For now, we always (in case there are indices to restore) include the globalMetaData,
             * not only if one of the tables in the table list is partitioned.
             * Previously we only included it in snapshots of full partitioned tables.
             * However, to make restoring of shapshots of single partitions work
             * we also need to include the global metadata (index templates).
             */
        return CreateSnapshotAnalyzedStatement.forTables(snapshotId, settings, ImmutableList.copyOf(snapshotIndices), !snapshotIndices.isEmpty());
    } else {
        for (SchemaInfo schemaInfo : schemas) {
            for (TableInfo tableInfo : schemaInfo) {
                // only check for user generated tables
                if (tableInfo instanceof DocTableInfo) {
                    Operation.blockedRaiseException(tableInfo, Operation.READ);
                }
            }
        }
        return CreateSnapshotAnalyzedStatement.all(snapshotId, settings);
    }
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) PartitionUnknownException(io.crate.exceptions.PartitionUnknownException) Table(io.crate.sql.tree.Table) QualifiedName(io.crate.sql.tree.QualifiedName) ResourceUnknownException(io.crate.exceptions.ResourceUnknownException) PartitionName(io.crate.metadata.PartitionName) SnapshotId(org.elasticsearch.cluster.metadata.SnapshotId) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) Settings(org.elasticsearch.common.settings.Settings) SchemaInfo(io.crate.metadata.table.SchemaInfo)

Example 8 with TableInfo

use of io.crate.metadata.table.TableInfo in project crate by crate.

the class UpdateAnalyzer method analyze.

public AnalyzedStatement analyze(Update node, Analysis analysis) {
    StatementAnalysisContext statementAnalysisContext = new StatementAnalysisContext(analysis.sessionContext(), analysis.parameterContext(), Operation.UPDATE, analysis.transactionContext());
    RelationAnalysisContext currentRelationContext = statementAnalysisContext.startRelation();
    AnalyzedRelation analyzedRelation = relationAnalyzer.analyze(node.relation(), statementAnalysisContext);
    FieldResolver fieldResolver = (FieldResolver) analyzedRelation;
    EvaluatingNormalizer normalizer = new EvaluatingNormalizer(functions, RowGranularity.CLUSTER, ReplaceMode.MUTATE, null, fieldResolver);
    FieldProvider columnFieldProvider = new NameFieldProvider(analyzedRelation);
    ExpressionAnalyzer columnExpressionAnalyzer = new ExpressionAnalyzer(functions, analysis.sessionContext(), analysis.parameterContext(), columnFieldProvider, null);
    columnExpressionAnalyzer.setResolveFieldsOperation(Operation.UPDATE);
    assert Iterables.getOnlyElement(currentRelationContext.sources().values()) == analyzedRelation : "currentRelationContext.sources().values() must have one element and equal to analyzedRelation";
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, analysis.sessionContext(), analysis.parameterContext(), new FullQualifedNameFieldProvider(currentRelationContext.sources()), null);
    ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext();
    int numNested = 1;
    if (analysis.parameterContext().numBulkParams() > 0) {
        numNested = analysis.parameterContext().numBulkParams();
    }
    WhereClauseAnalyzer whereClauseAnalyzer = null;
    if (analyzedRelation instanceof DocTableRelation) {
        whereClauseAnalyzer = new WhereClauseAnalyzer(functions, ((DocTableRelation) analyzedRelation));
    }
    TableInfo tableInfo = ((AbstractTableRelation) analyzedRelation).tableInfo();
    List<UpdateAnalyzedStatement.NestedAnalyzedStatement> nestedAnalyzedStatements = new ArrayList<>(numNested);
    for (int i = 0; i < numNested; i++) {
        analysis.parameterContext().setBulkIdx(i);
        Symbol querySymbol = expressionAnalyzer.generateQuerySymbol(node.whereClause(), expressionAnalysisContext);
        WhereClause whereClause = new WhereClause(normalizer.normalize(querySymbol, analysis.transactionContext()));
        if (whereClauseAnalyzer != null) {
            whereClause = whereClauseAnalyzer.analyze(whereClause, analysis.transactionContext());
        }
        if (!whereClause.docKeys().isPresent() && Symbols.containsColumn(whereClause.query(), DocSysColumns.VERSION)) {
            throw VERSION_SEARCH_EX;
        }
        UpdateAnalyzedStatement.NestedAnalyzedStatement nestedAnalyzedStatement = new UpdateAnalyzedStatement.NestedAnalyzedStatement(whereClause);
        for (Assignment assignment : node.assignements()) {
            analyzeAssignment(assignment, nestedAnalyzedStatement, tableInfo, normalizer, expressionAnalyzer, columnExpressionAnalyzer, expressionAnalysisContext, analysis.transactionContext());
        }
        nestedAnalyzedStatements.add(nestedAnalyzedStatement);
    }
    statementAnalysisContext.endRelation();
    return new UpdateAnalyzedStatement(analyzedRelation, nestedAnalyzedStatements);
}
Also used : ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Symbol(io.crate.analyze.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) ArrayList(java.util.ArrayList) Assignment(io.crate.sql.tree.Assignment) WhereClauseAnalyzer(io.crate.analyze.where.WhereClauseAnalyzer) TableInfo(io.crate.metadata.table.TableInfo)

Example 9 with TableInfo

use of io.crate.metadata.table.TableInfo in project crate by crate.

the class RestoreSnapshotAnalyzer method analyze.

public RestoreSnapshotAnalyzedStatement analyze(RestoreSnapshot node, Analysis analysis) {
    List<String> nameParts = node.name().getParts();
    Preconditions.checkArgument(nameParts.size() == 2, "Snapshot name not supported, only <repository>.<snapshot> works.");
    String repositoryName = nameParts.get(0);
    repositoryService.failIfRepositoryDoesNotExist(repositoryName);
    // validate and extract settings
    Settings settings = GenericPropertiesConverter.settingsFromProperties(node.properties(), analysis.parameterContext(), SETTINGS).build();
    if (node.tableList().isPresent()) {
        List<Table> tableList = node.tableList().get();
        Set<RestoreSnapshotAnalyzedStatement.RestoreTableInfo> restoreTables = new HashSet<>(tableList.size());
        for (Table table : tableList) {
            TableIdent tableIdent = TableIdent.of(table, analysis.sessionContext().defaultSchema());
            boolean tableExists = schemas.tableExists(tableIdent);
            if (tableExists) {
                if (table.partitionProperties().isEmpty()) {
                    throw new TableAlreadyExistsException(tableIdent);
                }
                TableInfo tableInfo = schemas.getTableInfo(tableIdent);
                if (!(tableInfo instanceof DocTableInfo)) {
                    throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Cannot restore snapshot of tables in schema '%s'", tableInfo.ident().schema()));
                }
                DocTableInfo docTableInfo = ((DocTableInfo) tableInfo);
                PartitionName partitionName = PartitionPropertiesAnalyzer.toPartitionName(tableIdent, docTableInfo, table.partitionProperties(), analysis.parameterContext().parameters());
                if (docTableInfo.partitions().contains(partitionName)) {
                    throw new PartitionAlreadyExistsException(partitionName);
                }
                restoreTables.add(new RestoreSnapshotAnalyzedStatement.RestoreTableInfo(tableIdent, partitionName));
            } else {
                if (table.partitionProperties().isEmpty()) {
                    restoreTables.add(new RestoreSnapshotAnalyzedStatement.RestoreTableInfo(tableIdent, null));
                } else {
                    PartitionName partitionName = PartitionPropertiesAnalyzer.toPartitionName(tableIdent, null, table.partitionProperties(), analysis.parameterContext().parameters());
                    restoreTables.add(new RestoreSnapshotAnalyzedStatement.RestoreTableInfo(tableIdent, partitionName));
                }
            }
        }
        return RestoreSnapshotAnalyzedStatement.forTables(nameParts.get(1), repositoryName, settings, ImmutableList.copyOf(restoreTables));
    } else {
        return RestoreSnapshotAnalyzedStatement.all(nameParts.get(1), repositoryName, settings);
    }
}
Also used : TableAlreadyExistsException(io.crate.exceptions.TableAlreadyExistsException) DocTableInfo(io.crate.metadata.doc.DocTableInfo) Table(io.crate.sql.tree.Table) TableIdent(io.crate.metadata.TableIdent) PartitionName(io.crate.metadata.PartitionName) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) Settings(org.elasticsearch.common.settings.Settings) PartitionAlreadyExistsException(io.crate.exceptions.PartitionAlreadyExistsException) HashSet(java.util.HashSet)

Example 10 with TableInfo

use of io.crate.metadata.table.TableInfo in project crate by crate.

the class RelationAnalyzer method visitTable.

@Override
protected AnalyzedRelation visitTable(Table node, StatementAnalysisContext context) {
    TableInfo tableInfo = schemas.getTableInfo(TableIdent.of(node, context.sessionContext().defaultSchema()));
    Operation.blockedRaiseException(tableInfo, context.currentOperation());
    AnalyzedRelation tableRelation;
    // Dispatching of doc relations is based on the returned class of the schema information.
    if (tableInfo instanceof DocTableInfo) {
        tableRelation = new DocTableRelation((DocTableInfo) tableInfo);
    } else {
        tableRelation = new TableRelation(tableInfo);
    }
    context.currentRelationContext().addSourceRelation(tableInfo.ident().schema(), tableInfo.ident().name(), tableRelation);
    return tableRelation;
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) SysClusterTableInfo(io.crate.metadata.sys.SysClusterTableInfo)

Aggregations

TableInfo (io.crate.metadata.table.TableInfo)30 Test (org.junit.Test)16 DocTableInfo (io.crate.metadata.doc.DocTableInfo)15 CrateUnitTest (io.crate.test.integration.CrateUnitTest)8 SQLTransportIntegrationTest (io.crate.integrationtests.SQLTransportIntegrationTest)7 Symbol (io.crate.analyze.symbol.Symbol)6 WhereClause (io.crate.analyze.WhereClause)5 Function (io.crate.analyze.symbol.Function)5 SysClusterTableInfo (io.crate.metadata.sys.SysClusterTableInfo)5 InformationSchemaInfo (io.crate.metadata.information.InformationSchemaInfo)4 SchemaInfo (io.crate.metadata.table.SchemaInfo)4 ArrayList (java.util.ArrayList)4 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)3 Bucket (io.crate.data.Bucket)3 CollectionBucket (io.crate.data.CollectionBucket)3 Table (io.crate.sql.tree.Table)3 SessionContext (io.crate.action.sql.SessionContext)2 ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)2 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)2 DocTableRelation (io.crate.analyze.relations.DocTableRelation)2