Search in sources :

Example 6 with SQLExecutor

use of io.crate.testing.SQLExecutor in project crate by crate.

the class TransportSchemaUpdateActionTest method testTemplateMappingUpdateFailsIfTypeIsDifferent.

@Test
public void testTemplateMappingUpdateFailsIfTypeIsDifferent() throws Exception {
    SQLExecutor e = SQLExecutor.builder(clusterService).addPartitionedTable("create table t (p int) partitioned by (p)").build();
    ClusterState currentState = clusterService.state();
    PlannerContext plannerContext = e.getPlannerContext(currentState);
    BoundAddColumn addXLong = AlterTableAddColumnPlan.bind(e.analyze("alter table t add column x long"), plannerContext.transactionContext(), plannerContext.nodeContext(), Row.EMPTY, SubQueryResults.EMPTY, null);
    BoundAddColumn addXString = AlterTableAddColumnPlan.bind(e.analyze("alter table t add column x string"), plannerContext.transactionContext(), plannerContext.nodeContext(), Row.EMPTY, SubQueryResults.EMPTY, null);
    String templateName = templateName("doc", "t");
    IndexTemplateMetadata template = currentState.metadata().templates().get(templateName);
    ClusterState stateWithXLong = ClusterState.builder(currentState).metadata(Metadata.builder(currentState.metadata()).put(IndexTemplateMetadata.builder(templateName).patterns(template.patterns()).putMapping(Constants.DEFAULT_MAPPING_TYPE, Strings.toString(JsonXContent.contentBuilder().map(addXLong.mapping())))).build()).build();
    expectedException.expect(IllegalArgumentException.class);
    TransportSchemaUpdateAction.updateTemplate(NamedXContentRegistry.EMPTY, stateWithXLong, templateName, addXString.mapping());
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) BoundAddColumn(io.crate.analyze.BoundAddColumn) PlannerContext(io.crate.planner.PlannerContext) SQLExecutor(io.crate.testing.SQLExecutor) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 7 with SQLExecutor

use of io.crate.testing.SQLExecutor in project crate by crate.

the class DeletePartitionsTest method testIndexNameGeneration.

@Test
public void testIndexNameGeneration() throws Exception {
    SQLExecutor e = SQLExecutor.builder(clusterService).addPartitionedTable(TableDefinitions.PARTED_PKS_TABLE_DEFINITION, new PartitionName(new RelationName("doc", "parted_pks"), singletonList("1395874800000")).asIndexName(), new PartitionName(new RelationName("doc", "parted_pks"), singletonList("1395961200000")).asIndexName()).build();
    DeletePartitions plan = e.plan("delete from parted_pks where date = ?");
    Object[] args1 = { "1395874800000" };
    assertThat(plan.getIndices(txnCtx, e.nodeCtx, new RowN(args1), SubQueryResults.EMPTY), Matchers.containsInAnyOrder(".partitioned.parted_pks.04732cpp6ks3ed1o60o30c1g"));
    Object[] args2 = { "1395961200000" };
    assertThat(plan.getIndices(txnCtx, e.nodeCtx, new RowN(args2), SubQueryResults.EMPTY), Matchers.containsInAnyOrder(".partitioned.parted_pks.04732cpp6ksjcc9i60o30c1g"));
}
Also used : PartitionName(io.crate.metadata.PartitionName) RowN(io.crate.data.RowN) SQLExecutor(io.crate.testing.SQLExecutor) RelationName(io.crate.metadata.RelationName) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 8 with SQLExecutor

use of io.crate.testing.SQLExecutor in project crate by crate.

the class FetchRewriteTest method test_fetchrewrite_on_eval_with_nested_source_outputs.

@Test
public void test_fetchrewrite_on_eval_with_nested_source_outputs() throws Exception {
    SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table tbl (x int)").build();
    DocTableInfo tableInfo = e.resolveTableInfo("tbl");
    var x = new AliasSymbol("x_alias", e.asSymbol("x"));
    var relation = new DocTableRelation(tableInfo);
    var collect = new Collect(relation, List.of(x), WhereClause.MATCH_ALL, 1L, DataTypes.INTEGER.fixedSize());
    var eval = new Eval(collect, List.of(x));
    FetchRewrite fetchRewrite = eval.rewriteToFetch(new TableStats(), List.of());
    assertThat(fetchRewrite, Matchers.notNullValue());
    assertThat(fetchRewrite.newPlan(), isPlan("Collect[doc.tbl | [_fetchid] | true]"));
    assertThat(fetchRewrite.replacedOutputs(), Matchers.hasEntry(is(x), isAlias("x_alias", isFetchStub("_doc['x']"))));
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) AliasSymbol(io.crate.expression.symbol.AliasSymbol) SQLExecutor(io.crate.testing.SQLExecutor) DocTableRelation(io.crate.analyze.relations.DocTableRelation) TableStats(io.crate.statistics.TableStats) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 9 with SQLExecutor

use of io.crate.testing.SQLExecutor in project crate by crate.

the class FetchRewriteTest method test_fetch_rewrite_on_eval_removes_eval_and_extends_replaced_outputs.

@Test
public void test_fetch_rewrite_on_eval_removes_eval_and_extends_replaced_outputs() throws Exception {
    SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table tbl (x int)").build();
    DocTableInfo tableInfo = e.resolveTableInfo("tbl");
    var x = e.asSymbol("x");
    var relation = new DocTableRelation(tableInfo);
    var collect = new Collect(relation, List.of(x), WhereClause.MATCH_ALL, 1L, DataTypes.INTEGER.fixedSize());
    var eval = new Eval(collect, List.of(new Function(Signature.scalar("add", DataTypes.INTEGER.getTypeSignature(), DataTypes.INTEGER.getTypeSignature(), DataTypes.INTEGER.getTypeSignature()), List.of(x, x), DataTypes.INTEGER)));
    FetchRewrite fetchRewrite = eval.rewriteToFetch(new TableStats(), List.of());
    assertThat(fetchRewrite, Matchers.notNullValue());
    assertThat(fetchRewrite.newPlan(), isPlan("Collect[doc.tbl | [_fetchid] | true]"));
    assertThat(fetchRewrite.replacedOutputs(), Matchers.hasEntry(isFunction("add", isReference("x"), isReference("x")), isFunction("add", isFetchStub("_doc['x']"), isFetchStub("_doc['x']"))));
    assertThat(List.copyOf(fetchRewrite.replacedOutputs().keySet()), is(eval.outputs()));
}
Also used : SymbolMatchers.isFunction(io.crate.testing.SymbolMatchers.isFunction) Function(io.crate.expression.symbol.Function) DocTableInfo(io.crate.metadata.doc.DocTableInfo) SQLExecutor(io.crate.testing.SQLExecutor) DocTableRelation(io.crate.analyze.relations.DocTableRelation) TableStats(io.crate.statistics.TableStats) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 10 with SQLExecutor

use of io.crate.testing.SQLExecutor in project crate by crate.

the class MapBackedSymbolReplacerTest method test_convert_symbol_without_traversing.

@Test
public void test_convert_symbol_without_traversing() throws Exception {
    SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table tbl (x int)").build();
    Symbol x = e.asSymbol("x");
    Symbol x_source_lookup = e.asSymbol("_doc['x']");
    Symbol alias = new AliasSymbol("a_alias", x);
    Symbol alias_source_lookup = new AliasSymbol("a_alias", x_source_lookup);
    // Mapping contains the requested symbol directly, must not traverse alias symbols
    Map<Symbol, Symbol> mapping = Map.of(alias, alias_source_lookup);
    assertThat(MapBackedSymbolReplacer.convert(alias, mapping), is(alias_source_lookup));
}
Also used : AliasSymbol(io.crate.expression.symbol.AliasSymbol) SQLExecutor(io.crate.testing.SQLExecutor) AliasSymbol(io.crate.expression.symbol.AliasSymbol) Symbol(io.crate.expression.symbol.Symbol) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Aggregations

SQLExecutor (io.crate.testing.SQLExecutor)64 Test (org.junit.Test)55 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)54 DocTableInfo (io.crate.metadata.doc.DocTableInfo)24 RelationName (io.crate.metadata.RelationName)9 AnalyzedUpdateStatement (io.crate.analyze.AnalyzedUpdateStatement)8 Doc (io.crate.expression.reference.Doc)8 Assignments (io.crate.expression.symbol.Assignments)7 Symbols (io.crate.expression.symbol.Symbols)7 CreateTable (io.crate.sql.tree.CreateTable)7 TableStats (io.crate.statistics.TableStats)7 Before (org.junit.Before)7 Symbol (io.crate.expression.symbol.Symbol)6 ViewsMetadataTest (io.crate.metadata.view.ViewsMetadataTest)6 QualifiedName (io.crate.sql.tree.QualifiedName)6 DocTableRelation (io.crate.analyze.relations.DocTableRelation)5 SessionContext (io.crate.action.sql.SessionContext)4 AliasSymbol (io.crate.expression.symbol.AliasSymbol)4 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)3 PlannerContext (io.crate.planner.PlannerContext)3