use of io.crate.expression.symbol.Symbols in project crate by crate.
the class RoutedCollectPhase method normalize.
/**
* normalizes the symbols of this node with the given normalizer
*
* @return a normalized node, if no changes occurred returns this
*/
public RoutedCollectPhase normalize(EvaluatingNormalizer normalizer, @Nonnull TransactionContext txnCtx) {
RoutedCollectPhase result = this;
Function<Symbol, Symbol> normalize = s -> normalizer.normalize(s, txnCtx);
List<Symbol> newToCollect = Lists2.map(toCollect, normalize);
boolean changed = !newToCollect.equals(toCollect);
Symbol newWhereClause = normalizer.normalize(where, txnCtx);
OrderBy orderBy = this.orderBy;
if (orderBy != null) {
orderBy = orderBy.map(normalize);
}
changed = changed || newWhereClause != where || orderBy != this.orderBy;
if (changed) {
result = new RoutedCollectPhase(jobId(), phaseId(), name(), routing, maxRowGranularity, newToCollect, projections, newWhereClause, distributionInfo);
result.nodePageSizeHint(nodePageSizeHint);
result.orderBy(orderBy);
}
return result;
}
use of io.crate.expression.symbol.Symbols in project crate by crate.
the class SymbolToColumnDefinitionConverterTest method testAliasedNameToColumnDefinition.
@Test
public void testAliasedNameToColumnDefinition() 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'] 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";
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 getAllColumnDefinitionsFrom.
private List<ColumnDefinition<Expression>> getAllColumnDefinitionsFrom(String createTableStmt) throws IOException {
SQLExecutor e = SQLExecutor.builder(clusterService).addTable(createTableStmt).build();
AnalyzedRelation analyzedRelation = e.analyze("select * from tbl");
return Lists2.map(analyzedRelation.outputs(), Symbols::toColumnDefinition);
}
use of io.crate.expression.symbol.Symbols in project crate by crate.
the class SymbolToColumnDefinitionConverterTest method testTypeCastedSymbolToColumnDefinition.
@Test
public void testTypeCastedSymbolToColumnDefinition() {
// check for naming of the target columns
String selectStmt = "select cast([0,1,5] as array(boolean)) AS active_threads, " + " cast(port['http']as boolean) from sys.nodes limit 1 ";
SQLExecutor e = SQLExecutor.builder(clusterService).build();
var analyzedRelation = e.analyze(selectStmt);
var actual = Lists2.map(analyzedRelation.outputs(), Symbols::toColumnDefinition);
assertThat(actual, containsInAnyOrder(isColumnDefinition("cast(port['http'] AS boolean)", isColumnType(DataTypes.BOOLEAN.getName())), isColumnDefinition("active_threads", isCollectionColumnType(ArrayType.NAME.toUpperCase(), isColumnType(DataTypes.BOOLEAN.getName())))));
}
use of io.crate.expression.symbol.Symbols in project crate by crate.
the class MoveFilterBeneathGroupBy method apply.
@Override
public LogicalPlan apply(Filter filter, Captures captures, TableStats tableStats, TransactionContext txnCtx, NodeContext nodeCtx) {
// Since something like `SELECT x, sum(y) FROM t GROUP BY x HAVING y > 10` is not valid
// (y would have to be declared as group key) any parts of a HAVING that is not an aggregation can be moved.
Symbol predicate = filter.query();
List<Symbol> parts = AndOperator.split(predicate);
ArrayList<Symbol> withAggregates = new ArrayList<>();
ArrayList<Symbol> withoutAggregates = new ArrayList<>();
for (Symbol part : parts) {
if (SymbolVisitors.any(Symbols::isAggregate, part)) {
withAggregates.add(part);
} else {
withoutAggregates.add(part);
}
}
if (withoutAggregates.isEmpty()) {
return null;
}
GroupHashAggregate groupBy = captures.get(groupByCapture);
if (withoutAggregates.size() == parts.size()) {
return transpose(filter, groupBy);
}
/* HAVING `count(*) > 1 AND x = 10`
* withAggregates: [count(*) > 1]
* withoutAggregates: [x = 10]
*
* Filter
* |
* GroupBy
*
* transforms to
*
* Filter (count(*) > 1)
* |
* GroupBy
* |
* Filter (x = 10)
*/
LogicalPlan newGroupBy = groupBy.replaceSources(List.of(new Filter(groupBy.source(), AndOperator.join(withoutAggregates))));
return new Filter(newGroupBy, AndOperator.join(withAggregates));
}
Aggregations