Search in sources :

Example 31 with RelationName

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

the class TransportAnalyzeAction method fetchSamplesThenGenerateAndPublishStats.

@SuppressWarnings("unchecked")
public CompletableFuture<AcknowledgedResponse> fetchSamplesThenGenerateAndPublishStats() {
    ArrayList<CompletableFuture<Map.Entry<RelationName, Stats>>> futures = new ArrayList<>();
    for (SchemaInfo schema : schemas) {
        if (!(schema instanceof DocSchemaInfo)) {
            continue;
        }
        for (TableInfo table : schema.getTables()) {
            List<Reference> primitiveColumns = StreamSupport.stream(table.spliterator(), false).filter(x -> !x.column().isSystemColumn()).filter(x -> DataTypes.isPrimitive(x.valueType())).map(x -> table.getReadReference(x.column())).collect(Collectors.toList());
            futures.add(fetchSamples(table.ident(), primitiveColumns).thenApply(samples -> Map.entry(table.ident(), createTableStats(samples, primitiveColumns))));
        }
    }
    return CompletableFutures.allAsList(futures).thenCompose(entries -> publishTableStats(Map.ofEntries(entries.toArray(new Map.Entry[0]))));
}
Also used : Arrays(java.util.Arrays) RelationName(io.crate.metadata.RelationName) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) ClusterService(org.elasticsearch.cluster.service.ClusterService) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Inject(org.elasticsearch.common.inject.Inject) ArrayList(java.util.ArrayList) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ActionListenerResponseHandler(org.elasticsearch.action.ActionListenerResponseHandler) Symbols(io.crate.expression.symbol.Symbols) Map(java.util.Map) ThreadPool(org.elasticsearch.threadpool.ThreadPool) StreamSupport(java.util.stream.StreamSupport) TransportService(org.elasticsearch.transport.TransportService) FutureActionListener(io.crate.action.FutureActionListener) SchemaInfo(io.crate.metadata.table.SchemaInfo) TableInfo(io.crate.metadata.table.TableInfo) Streamer(io.crate.Streamer) NodeActionRequestHandler(io.crate.execution.support.NodeActionRequestHandler) ColumnIdent(io.crate.metadata.ColumnIdent) Reference(io.crate.metadata.Reference) DataType(io.crate.types.DataType) CompletableFutures(io.crate.concurrent.CompletableFutures) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) Collectors(java.util.stream.Collectors) MultiActionListener(io.crate.execution.support.MultiActionListener) List(java.util.List) Version(org.elasticsearch.Version) Row(io.crate.data.Row) AnalyzeRequest(io.crate.execution.ddl.AnalyzeRequest) DocSchemaInfo(io.crate.metadata.doc.DocSchemaInfo) DataTypes(io.crate.types.DataTypes) Singleton(org.elasticsearch.common.inject.Singleton) Schemas(io.crate.metadata.Schemas) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) Reference(io.crate.metadata.Reference) ArrayList(java.util.ArrayList) CompletableFuture(java.util.concurrent.CompletableFuture) DocSchemaInfo(io.crate.metadata.doc.DocSchemaInfo) RelationName(io.crate.metadata.RelationName) TableInfo(io.crate.metadata.table.TableInfo) HashMap(java.util.HashMap) Map(java.util.Map) SchemaInfo(io.crate.metadata.table.SchemaInfo) DocSchemaInfo(io.crate.metadata.doc.DocSchemaInfo)

Example 32 with RelationName

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

the class UpdateSourceGenTest method testSetXBasedOnXAndPartitionedColumn.

@Test
public void testSetXBasedOnXAndPartitionedColumn() throws Exception {
    SQLExecutor e = SQLExecutor.builder(clusterService).addPartitionedTable("create table t (x int, p int) partitioned by (p)", new PartitionName(new RelationName("doc", "t"), Collections.singletonList("1")).asIndexName()).build();
    AnalyzedUpdateStatement update = e.analyze("update t set x = x + p");
    Assignments assignments = Assignments.convert(update.assignmentByTargetCol(), e.nodeCtx);
    DocTableInfo table = (DocTableInfo) update.table().tableInfo();
    UpdateSourceGen updateSourceGen = new UpdateSourceGen(txnCtx, e.nodeCtx, table, assignments.targetNames());
    Map<String, Object> source = singletonMap("x", 1);
    Map<String, Object> updatedSource = updateSourceGen.generateSource(new Doc(1, table.concreteIndices()[0], "1", 1, 1, 1, source, () -> {
        try {
            return Strings.toString(XContentFactory.jsonBuilder().map(source));
        } catch (IOException e1) {
            throw new RuntimeException(e1);
        }
    }), assignments.sources(), new Object[0]);
    assertThat(updatedSource, is(Map.of("x", 2)));
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) Assignments(io.crate.expression.symbol.Assignments) IOException(java.io.IOException) PartitionName(io.crate.metadata.PartitionName) SQLExecutor(io.crate.testing.SQLExecutor) AnalyzedUpdateStatement(io.crate.analyze.AnalyzedUpdateStatement) RelationName(io.crate.metadata.RelationName) Doc(io.crate.expression.reference.Doc) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 33 with RelationName

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

the class InputColumnsTest method prepare.

@Before
public void prepare() throws Exception {
    Map<RelationName, AnalyzedRelation> sources = T3.sources(List.of(T3.T1), clusterService);
    DocTableRelation tr1 = (DocTableRelation) sources.get(T3.T1);
    sqlExpressions = new SqlExpressions(sources, tr1);
}
Also used : RelationName(io.crate.metadata.RelationName) DocTableRelation(io.crate.analyze.relations.DocTableRelation) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) SqlExpressions(io.crate.testing.SqlExpressions) Before(org.junit.Before)

Example 34 with RelationName

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

the class SubSelectIntegrationTest method testSingleRowSubSelectWorksWithJoins.

@Test
public void testSingleRowSubSelectWorksWithJoins() throws Exception {
    execute("create table t (x long primary key)");
    ensureYellow();
    execute("insert into t (x) values (1), (2)");
    execute("refresh table t");
    for (TableStats tableStats : internalCluster().getInstances(TableStats.class)) {
        Map<RelationName, Stats> newStats = new HashMap<>();
        newStats.put(new RelationName(sqlExecutor.getCurrentSchema(), "t"), new Stats(100, 64, Map.of()));
        tableStats.updateTableStats(newStats);
    }
    // Left table is expected to be one row, due to the single row subselect in the where clause.
    execute("select * from t as t1, t as t2 where t1.x = (select 1) order by t2.x");
    assertThat(printedTable(response.rows()), is("1| 1\n1| 2\n"));
    // Left table is expected to be bigger due to the table stats stating it being 100 rows
    execute("select * from t as t2, t as t1 where t1.x = (select 1) order by t2.x");
    assertThat(printedTable(response.rows()), is("1| 1\n2| 1\n"));
}
Also used : HashMap(java.util.HashMap) Stats(io.crate.statistics.Stats) TableStats(io.crate.statistics.TableStats) RelationName(io.crate.metadata.RelationName) TableStats(io.crate.statistics.TableStats) Test(org.junit.Test)

Example 35 with RelationName

use of io.crate.metadata.RelationName 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

RelationName (io.crate.metadata.RelationName)180 Test (org.junit.Test)100 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)55 PartitionName (io.crate.metadata.PartitionName)47 Symbol (io.crate.expression.symbol.Symbol)42 Reference (io.crate.metadata.Reference)37 ColumnIdent (io.crate.metadata.ColumnIdent)31 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)21 ReferenceIdent (io.crate.metadata.ReferenceIdent)21 DocTableInfo (io.crate.metadata.doc.DocTableInfo)21 Map (java.util.Map)20 HashMap (java.util.HashMap)19 SqlExpressions (io.crate.testing.SqlExpressions)18 ArrayList (java.util.ArrayList)18 List (java.util.List)17 Before (org.junit.Before)17 DocTableRelation (io.crate.analyze.relations.DocTableRelation)13 SQLExecutor (io.crate.testing.SQLExecutor)11 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)10 IndexTemplateMetadata (org.elasticsearch.cluster.metadata.IndexTemplateMetadata)10