Search in sources :

Example 6 with Symbols

use of io.crate.expression.symbol.Symbols in project crate by crate.

the class SymbolToColumnDefinitionConverterTest method testScopedNameToColumnDefinition.

@Test
public void testScopedNameToColumnDefinition() 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 A.col_default_object['col_nested_integer'], " + "   A.col_default_object['col_nested_object']['col_nested_timestamp_with_time_zone'], " + "   A.col_default_object['col_nested_object'] " + "from " + "   (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) as A";
    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 7 with Symbols

use of io.crate.expression.symbol.Symbols 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 8 with Symbols

use of io.crate.expression.symbol.Symbols 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 9 with Symbols

use of io.crate.expression.symbol.Symbols 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 10 with Symbols

use of io.crate.expression.symbol.Symbols in project crate by crate.

the class AnalyzedTableElements method validateAndFormatExpression.

private static void validateAndFormatExpression(Symbol function, AnalyzedColumnDefinition<Symbol> columnDefinitionWithExpressionSymbols, AnalyzedColumnDefinition<Object> columnDefinitionEvaluated, Consumer<String> formattedExpressionConsumer) {
    String formattedExpression;
    DataType<?> valueType = function.valueType();
    DataType<?> definedType = columnDefinitionWithExpressionSymbols.dataType();
    if (SymbolVisitors.any(Symbols::isAggregate, function)) {
        throw new UnsupportedOperationException("Aggregation functions are not allowed in generated columns: " + function);
    }
    // check for optional defined type and add `cast` to expression if possible
    if (definedType != null && !definedType.equals(valueType)) {
        final DataType<?> columnDataType;
        if (ArrayType.NAME.equals(columnDefinitionWithExpressionSymbols.collectionType())) {
            columnDataType = new ArrayType<>(definedType);
        } else {
            columnDataType = definedType;
        }
        if (!valueType.isConvertableTo(columnDataType, false)) {
            throw new IllegalArgumentException(String.format(Locale.ENGLISH, "expression value type '%s' not supported for conversion to '%s'", valueType, columnDataType.getName()));
        }
        Symbol castFunction = CastFunctionResolver.generateCastFunction(function, columnDataType);
        formattedExpression = castFunction.toString(Style.UNQUALIFIED);
    } else {
        if (valueType instanceof ArrayType) {
            columnDefinitionEvaluated.collectionType(ArrayType.NAME);
            columnDefinitionEvaluated.dataType(ArrayType.unnest(valueType).getName());
        } else {
            columnDefinitionEvaluated.dataType(valueType.getName());
        }
        formattedExpression = function.toString(Style.UNQUALIFIED);
    }
    formattedExpressionConsumer.accept(formattedExpression);
}
Also used : ArrayType(io.crate.types.ArrayType) Symbols(io.crate.expression.symbol.Symbols) Symbol(io.crate.expression.symbol.Symbol)

Aggregations

Symbols (io.crate.expression.symbol.Symbols)11 SQLExecutor (io.crate.testing.SQLExecutor)7 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)6 Test (org.junit.Test)6 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)3 Symbol (io.crate.expression.symbol.Symbol)3 RelationName (io.crate.metadata.RelationName)2 OrderBy (io.crate.analyze.OrderBy)1 StatementAnalysisContext (io.crate.analyze.relations.StatementAnalysisContext)1 Lists2 (io.crate.common.collections.Lists2)1 Paging (io.crate.data.Paging)1 Projection (io.crate.execution.dsl.projection.Projection)1 EvaluatingNormalizer (io.crate.expression.eval.EvaluatingNormalizer)1 ScopedSymbol (io.crate.expression.symbol.ScopedSymbol)1 SelectSymbol (io.crate.expression.symbol.SelectSymbol)1 SymbolVisitors (io.crate.expression.symbol.SymbolVisitors)1 Routing (io.crate.metadata.Routing)1 RowGranularity (io.crate.metadata.RowGranularity)1 TransactionContext (io.crate.metadata.TransactionContext)1 DistributionInfo (io.crate.planner.distribution.DistributionInfo)1