Search in sources :

Example 11 with CoordinatorTxnCtx

use of io.crate.metadata.CoordinatorTxnCtx in project crate by crate.

the class DocIndexMetadata method build.

public DocIndexMetadata build() {
    notNullColumns = getNotNullColumns();
    columnPolicy = getColumnPolicy();
    createColumnDefinitions();
    indices = createIndexDefinitions();
    references = new LinkedHashMap<>();
    DocSysColumns.forTable(ident, references::put);
    columns.sort(SORT_REFS_BY_POSTITON_THEN_NAME);
    nestedColumns.sort(SORT_REFS_BY_POSTITON_THEN_NAME);
    for (Reference ref : columns) {
        references.put(ref.column(), ref);
        for (Reference nestedColumn : nestedColumns) {
            if (nestedColumn.column().getRoot().equals(ref.column())) {
                references.put(nestedColumn.column(), nestedColumn);
            }
        }
    }
    // Order of the partitionedByColumns is important; Must be the same order as `partitionedBy` is in.
    partitionedByColumns = Lists2.map(partitionedBy, references::get);
    generatedColumnReferences = List.copyOf(generatedColumnReferencesBuilder);
    primaryKey = getPrimaryKey();
    routingCol = getRoutingCol();
    Collection<Reference> references = this.references.values();
    TableReferenceResolver tableReferenceResolver = new TableReferenceResolver(references, ident);
    CoordinatorTxnCtx txnCtx = CoordinatorTxnCtx.systemTransactionContext();
    ExpressionAnalyzer exprAnalyzer = new ExpressionAnalyzer(txnCtx, nodeCtx, ParamTypeHints.EMPTY, tableReferenceResolver, null);
    ExpressionAnalysisContext analysisCtx = new ExpressionAnalysisContext(txnCtx.sessionContext());
    ArrayList<CheckConstraint<Symbol>> checkConstraintsBuilder = null;
    Map<String, Object> metaMap = Maps.get(mappingMap, "_meta");
    if (metaMap != null) {
        Map<String, String> checkConstraintsMap = Maps.get(metaMap, "check_constraints");
        if (checkConstraintsMap != null) {
            checkConstraintsBuilder = new ArrayList<>();
            for (Map.Entry<String, String> entry : checkConstraintsMap.entrySet()) {
                String name = entry.getKey();
                String expressionStr = entry.getValue();
                Expression expr = SqlParser.createExpression(expressionStr);
                Symbol analyzedExpr = exprAnalyzer.convert(expr, analysisCtx);
                checkConstraintsBuilder.add(new CheckConstraint<>(name, null, analyzedExpr, expressionStr));
            }
        }
    }
    checkConstraints = checkConstraintsBuilder != null ? List.copyOf(checkConstraintsBuilder) : List.of();
    for (Reference reference : generatedColumnReferences) {
        GeneratedReference generatedReference = (GeneratedReference) reference;
        Expression expression = SqlParser.createExpression(generatedReference.formattedGeneratedExpression());
        generatedReference.generatedExpression(exprAnalyzer.convert(expression, analysisCtx));
        generatedReference.referencedReferences(List.copyOf(tableReferenceResolver.references()));
        tableReferenceResolver.references().clear();
    }
    return this;
}
Also used : CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) GeneratedReference(io.crate.metadata.GeneratedReference) GeneratedReference(io.crate.metadata.GeneratedReference) GeoReference(io.crate.metadata.GeoReference) IndexReference(io.crate.metadata.IndexReference) Reference(io.crate.metadata.Reference) Symbol(io.crate.expression.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) Expression(io.crate.sql.tree.Expression) TableReferenceResolver(io.crate.analyze.expressions.TableReferenceResolver) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CheckConstraint(io.crate.sql.tree.CheckConstraint)

Example 12 with CoordinatorTxnCtx

use of io.crate.metadata.CoordinatorTxnCtx in project crate by crate.

the class ViewAnalyzer method analyze.

public CreateViewStmt analyze(CreateView createView, CoordinatorTxnCtx txnCtx) {
    RelationName name = RelationName.of(createView.name(), txnCtx.sessionContext().searchPath().currentSchema());
    name.ensureValidForRelationCreation();
    if (BlobSchemaInfo.NAME.equals(name.schema())) {
        throw new UnsupportedOperationException("Creating a view in the \"blob\" schema is not supported");
    }
    AnalyzedRelation query;
    String formattedQuery;
    try {
        formattedQuery = SqlFormatter.formatSql(createView.query());
    } catch (Exception e) {
        throw new UnsupportedOperationException("Invalid query used in CREATE VIEW. Query: " + createView.query());
    }
    try {
        // Analyze the formatted Query to make sure the formatting didn't mess it up in any way.
        query = relationAnalyzer.analyze((Query) SqlParser.createStatement(formattedQuery), txnCtx, ParamTypeHints.EMPTY);
    } catch (Exception e) {
        throw new UnsupportedOperationException("Invalid query used in CREATE VIEW. " + e.getMessage() + ". Query: " + formattedQuery);
    }
    if (query.outputs().stream().map(f -> Symbols.pathFromSymbol(f).sqlFqn()).distinct().count() != query.outputs().size()) {
        throw new IllegalArgumentException("Query in CREATE VIEW must not have duplicate column names");
    }
    return new CreateViewStmt(name, query, createView.query(), createView.replaceExisting(), txnCtx.sessionContext().sessionUser());
}
Also used : RelationName(io.crate.metadata.RelationName) SqlFormatter(io.crate.sql.SqlFormatter) DropView(io.crate.sql.tree.DropView) BlobSchemaInfo(io.crate.metadata.blob.BlobSchemaInfo) ArrayList(java.util.ArrayList) CreateView(io.crate.sql.tree.CreateView) RelationsUnknown(io.crate.exceptions.RelationsUnknown) Symbols(io.crate.expression.symbol.Symbols) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) RelationAnalyzer(io.crate.analyze.relations.RelationAnalyzer) SqlParser(io.crate.sql.parser.SqlParser) Schemas(io.crate.metadata.Schemas) Query(io.crate.sql.tree.Query) RelationUnknown(io.crate.exceptions.RelationUnknown) QualifiedName(io.crate.sql.tree.QualifiedName) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) Query(io.crate.sql.tree.Query) RelationName(io.crate.metadata.RelationName) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation)

Example 13 with CoordinatorTxnCtx

use of io.crate.metadata.CoordinatorTxnCtx in project crate by crate.

the class CreateSnapshotAnalyzer method analyze.

public AnalyzedCreateSnapshot analyze(CreateSnapshot<Expression> createSnapshot, ParamTypeHints paramTypeHints, CoordinatorTxnCtx txnCtx) {
    String repositoryName = createSnapshot.name().getPrefix().map(name -> {
        validateRepository(name);
        return name.toString();
    }).orElseThrow(() -> new IllegalArgumentException("Snapshot must be specified by \"<repository_name>\".\"<snapshot_name>\""));
    String snapshotName = createSnapshot.name().getSuffix();
    Snapshot snapshot = new Snapshot(repositoryName, new SnapshotId(snapshotName, UUIDs.dirtyUUID().toString()));
    var exprCtx = new ExpressionAnalysisContext(txnCtx.sessionContext());
    var exprAnalyzerWithoutFields = new ExpressionAnalyzer(txnCtx, nodeCtx, paramTypeHints, FieldProvider.UNSUPPORTED, null);
    var exprAnalyzerWithFieldsAsString = new ExpressionAnalyzer(txnCtx, nodeCtx, paramTypeHints, FieldProvider.FIELDS_AS_LITERAL, null);
    List<Table<Symbol>> tables = Lists2.map(createSnapshot.tables(), (table) -> table.map(x -> exprAnalyzerWithFieldsAsString.convert(x, exprCtx)));
    GenericProperties<Symbol> properties = createSnapshot.properties().map(x -> exprAnalyzerWithoutFields.convert(x, exprCtx));
    return new AnalyzedCreateSnapshot(snapshot, tables, properties);
}
Also used : ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) NodeContext(io.crate.metadata.NodeContext) SnapshotId(org.elasticsearch.snapshots.SnapshotId) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) RepositoryService(io.crate.execution.ddl.RepositoryService) FieldProvider(io.crate.analyze.relations.FieldProvider) CreateSnapshot(io.crate.sql.tree.CreateSnapshot) UUIDs(org.elasticsearch.common.UUIDs) Table(io.crate.sql.tree.Table) GenericProperties(io.crate.sql.tree.GenericProperties) Lists2(io.crate.common.collections.Lists2) List(java.util.List) Symbol(io.crate.expression.symbol.Symbol) Locale(java.util.Locale) QualifiedName(io.crate.sql.tree.QualifiedName) Snapshot(org.elasticsearch.snapshots.Snapshot) Expression(io.crate.sql.tree.Expression) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Table(io.crate.sql.tree.Table) Symbol(io.crate.expression.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) CreateSnapshot(io.crate.sql.tree.CreateSnapshot) Snapshot(org.elasticsearch.snapshots.Snapshot) SnapshotId(org.elasticsearch.snapshots.SnapshotId)

Example 14 with CoordinatorTxnCtx

use of io.crate.metadata.CoordinatorTxnCtx in project crate by crate.

the class DocKeysTest method testDocKeySequeceAndTerm.

@Test
public void testDocKeySequeceAndTerm() {
    DocKeys docKeys = new DocKeys(List.of(List.of(Literal.of(1), Literal.of(22), Literal.of(5))), false, true, 1, null);
    DocKeys.DocKey key = docKeys.getOnlyKey();
    CoordinatorTxnCtx txnCtx = CoordinatorTxnCtx.systemTransactionContext();
    Optional<Long> sequenceNo = key.sequenceNo(txnCtx, nodeCtx, Row.EMPTY, SubQueryResults.EMPTY);
    assertThat(sequenceNo.isPresent(), is(true));
    assertThat(sequenceNo.get(), is(22L));
    Optional<Long> primaryTerm = key.primaryTerm(txnCtx, nodeCtx, Row.EMPTY, SubQueryResults.EMPTY);
    assertThat(primaryTerm.isPresent(), is(true));
    assertThat(primaryTerm.get(), is(5L));
}
Also used : CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) Test(org.junit.Test)

Example 15 with CoordinatorTxnCtx

use of io.crate.metadata.CoordinatorTxnCtx in project crate by crate.

the class SQLExecutor method asSymbol.

/**
 * Convert a expression to a symbol
 * If tables are used here they must also be registered in the SQLExecutor having used {@link Builder#addTable(String)}
 */
public Symbol asSymbol(String expression) {
    MapBuilder<RelationName, AnalyzedRelation> sources = MapBuilder.newMapBuilder();
    for (SchemaInfo schemaInfo : schemas) {
        for (TableInfo tableInfo : schemaInfo.getTables()) {
            if (tableInfo instanceof DocTableInfo) {
                RelationName relationName = tableInfo.ident();
                sources.put(relationName, new DocTableRelation(schemas.getTableInfo(relationName)));
            }
        }
    }
    CoordinatorTxnCtx coordinatorTxnCtx = new CoordinatorTxnCtx(sessionContext);
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(coordinatorTxnCtx, nodeCtx, ParamTypeHints.EMPTY, new FullQualifiedNameFieldProvider(sources.immutableMap(), ParentRelations.NO_PARENTS, sessionContext.searchPath().currentSchema()), new SubqueryAnalyzer(relAnalyzer, new StatementAnalysisContext(ParamTypeHints.EMPTY, Operation.READ, coordinatorTxnCtx)));
    ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext(coordinatorTxnCtx.sessionContext());
    return expressionAnalyzer.convert(SqlParser.createExpression(expression), expressionAnalysisContext);
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) StatementAnalysisContext(io.crate.analyze.relations.StatementAnalysisContext) RelationName(io.crate.metadata.RelationName) DocTableRelation(io.crate.analyze.relations.DocTableRelation) TableInfo(io.crate.metadata.table.TableInfo) DocTableInfo(io.crate.metadata.doc.DocTableInfo) SubqueryAnalyzer(io.crate.analyze.expressions.SubqueryAnalyzer) PgCatalogSchemaInfo(io.crate.metadata.pgcatalog.PgCatalogSchemaInfo) SysSchemaInfo(io.crate.metadata.sys.SysSchemaInfo) BlobSchemaInfo(io.crate.metadata.blob.BlobSchemaInfo) InformationSchemaInfo(io.crate.metadata.information.InformationSchemaInfo) SchemaInfo(io.crate.metadata.table.SchemaInfo) FullQualifiedNameFieldProvider(io.crate.analyze.relations.FullQualifiedNameFieldProvider)

Aggregations

CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)44 Symbol (io.crate.expression.symbol.Symbol)27 NodeContext (io.crate.metadata.NodeContext)22 List (java.util.List)16 PlannerContext (io.crate.planner.PlannerContext)15 ArrayList (java.util.ArrayList)15 Map (java.util.Map)14 Lists2 (io.crate.common.collections.Lists2)13 Row (io.crate.data.Row)13 RelationName (io.crate.metadata.RelationName)13 Plan (io.crate.planner.Plan)13 SubQueryResults (io.crate.planner.operators.SubQueryResults)13 Function (java.util.function.Function)13 VisibleForTesting (io.crate.common.annotations.VisibleForTesting)12 RowConsumer (io.crate.data.RowConsumer)12 DependencyCarrier (io.crate.planner.DependencyCarrier)12 HashMap (java.util.HashMap)12 Test (org.junit.Test)12 Row1 (io.crate.data.Row1)11 DocTableInfo (io.crate.metadata.doc.DocTableInfo)11