Search in sources :

Example 46 with SQLExecutor

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

the class SymbolToColumnDefinitionConverterTest method testEntireObjectToColumDefinition.

@Test
public void testEntireObjectToColumDefinition() throws IOException {
    String createTableStmt = "create table tbl (" + "   col_default_object object as (" + "       col_nested_integer integer," + "       col_nested_object object as (" + "           col_nested_timestamp_with_time_zone timestamp with time zone" + "       )" + "   )" + ")";
    SQLExecutor e = SQLExecutor.builder(clusterService).addTable(createTableStmt).build();
    AnalyzedRelation analyzedRelation = e.analyze("select col_default_object from tbl");
    var actual = Lists2.map(analyzedRelation.outputs(), Symbols::toColumnDefinition);
    assertThat(actual.get(0), isColumnDefinition("col_default_object", isObjectColumnType(DataTypes.UNTYPED_OBJECT.getName(), isColumnPolicy(OBJECT_TYPE_DEFAULT_COLUMN_POLICY), containsInAnyOrder(isColumnDefinition("col_nested_integer", isColumnType(DataTypes.INTEGER.getName())), isColumnDefinition("col_nested_object", isObjectColumnType(DataTypes.UNTYPED_OBJECT.getName(), isColumnPolicy(OBJECT_TYPE_DEFAULT_COLUMN_POLICY), contains(isColumnDefinition("col_nested_timestamp_with_time_zone", isColumnType(DataTypes.TIMESTAMPZ.getName())))))))));
}
Also used : SQLExecutor(io.crate.testing.SQLExecutor) Symbols(io.crate.expression.symbol.Symbols) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 47 with SQLExecutor

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

the class SymbolToColumnDefinitionConverterTest method testSymbolFromViewToColumnDefinition.

@Test
public void testSymbolFromViewToColumnDefinition() throws IOException {
    String createTableStmt = "create table tbl (" + "   col_default_object object as (" + "       col_nested_integer integer," + "       col_nested_object object as (" + "           col_nested_timestamp_with_time_zone timestamp with time zone" + "       )" + "   )" + ")";
    SQLExecutor e = SQLExecutor.builder(clusterService).addTable(createTableStmt).addView(new RelationName("doc", "tbl_view"), "select * from doc.tbl").build();
    String selectStmt = "select " + "   col_default_object['col_nested_integer'] as col1, " + "   col_default_object['col_nested_object']['col_nested_timestamp_with_time_zone'] as col2, " + "   col_default_object['col_nested_object'] as col3 " + "from tbl_view";
    var analyzedRelation = e.analyze(selectStmt);
    var actual = Lists2.map(analyzedRelation.outputs(), Symbols::toColumnDefinition);
    assertThat(actual, containsInAnyOrder(isColumnDefinition("col1", isColumnType(DataTypes.INTEGER.getName())), isColumnDefinition("col2", isColumnType(DataTypes.TIMESTAMPZ.getName())), isColumnDefinition("col3", isObjectColumnType(DataTypes.UNTYPED_OBJECT.getName(), isColumnPolicy(OBJECT_TYPE_DEFAULT_COLUMN_POLICY), contains(isColumnDefinition("col_nested_timestamp_with_time_zone", isColumnType(DataTypes.TIMESTAMPZ.getName())))))));
}
Also used : SQLExecutor(io.crate.testing.SQLExecutor) Symbols(io.crate.expression.symbol.Symbols) RelationName(io.crate.metadata.RelationName) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 48 with SQLExecutor

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

the class SymbolToColumnDefinitionConverterTest method testSubFieldOfObjectTypeToColumnDefinition.

@Test
public void testSubFieldOfObjectTypeToColumnDefinition() throws IOException {
    String createTableStmt = "create table tbl (" + "   col_default_object object as (" + "       col_nested_integer integer," + "       col_nested_object object as (" + "           col_nested_timestamp_with_time_zone timestamp with time zone" + "       )" + "   )" + ")";
    SQLExecutor e = SQLExecutor.builder(clusterService).addTable(createTableStmt).build();
    String selectStmt = "select " + "   col_default_object['col_nested_integer'], " + "   col_default_object['col_nested_object']['col_nested_timestamp_with_time_zone'], " + "   col_default_object['col_nested_object']" + "from tbl";
    var analyzedRelation = e.analyze(selectStmt);
    var actual = Lists2.map(analyzedRelation.outputs(), Symbols::toColumnDefinition);
    assertThat(actual, containsInAnyOrder(isColumnDefinition("col_default_object['col_nested_integer']", isColumnType(DataTypes.INTEGER.getName())), isColumnDefinition("col_default_object['col_nested_object']['col_nested_timestamp_with_time_zone']", isColumnType(DataTypes.TIMESTAMPZ.getName())), isColumnDefinition("col_default_object['col_nested_object']", isObjectColumnType(DataTypes.UNTYPED_OBJECT.getName(), isColumnPolicy(OBJECT_TYPE_DEFAULT_COLUMN_POLICY), contains(isColumnDefinition("col_nested_timestamp_with_time_zone", isColumnType(DataTypes.TIMESTAMPZ.getName())))))));
}
Also used : SQLExecutor(io.crate.testing.SQLExecutor) Symbols(io.crate.expression.symbol.Symbols) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 49 with SQLExecutor

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

the class SelectStatementAnalyzerTest method testCustomSchemaSubSelectWithAccessToParentRelation.

@Test
public void testCustomSchemaSubSelectWithAccessToParentRelation() throws Exception {
    var executor = SQLExecutor.builder(clusterService).addTable(T3.T1_DEFINITION).build();
    SQLExecutor sqlExecutor2 = SQLExecutor.builder(clusterService).setSearchPath("foo").addTable("create table foo.t1 (id bigint primary key, name text)").build();
    expectedException.expect(UnsupportedOperationException.class);
    expectedException.expectMessage("Cannot use relation \"foo.t1\" in this context. It is only accessible in the parent context");
    sqlExecutor2.analyze("select * from t1 where id = (select 1 from t1 as x where x.id = t1.id)");
}
Also used : SQLExecutor(io.crate.testing.SQLExecutor) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 50 with SQLExecutor

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

the class SelectStatementAnalyzerTest method testQueryUsesSearchPath.

@Test
public void testQueryUsesSearchPath() throws IOException {
    SQLExecutor executor = SQLExecutor.builder(clusterService).setSearchPath("first", "second", "third").addTable("create table \"first\".t (id int)").addTable("create table third.t1 (id int)").build();
    QueriedSelectRelation queriedTable = executor.analyze("select * from t");
    assertThat(queriedTable.from(), contains(isDocTable(new RelationName("first", "t"))));
    queriedTable = executor.analyze("select * from t1");
    assertThat(queriedTable.from(), contains(isDocTable(new RelationName("third", "t1"))));
}
Also used : SQLExecutor(io.crate.testing.SQLExecutor) RelationName(io.crate.metadata.RelationName) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

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