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())))))));
}
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())))))))));
}
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())))))));
}
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())))))));
}
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);
}
Aggregations