use of io.crate.analyze.QueriedSelectRelation in project crate by crate.
the class RelationAnalyzer method visitQuery.
@Override
protected AnalyzedRelation visitQuery(Query node, StatementAnalysisContext statementContext) {
AnalyzedRelation childRelation = node.getQueryBody().accept(this, statementContext);
if (node.getOrderBy().isEmpty() && node.getLimit().isEmpty() && node.getOffset().isEmpty()) {
return childRelation;
}
// In case of Set Operation (UNION, INTERSECT EXCEPT) or VALUES clause,
// the `node` contains the ORDER BY and/or LIMIT and/or OFFSET and wraps the
// actual operation (eg: UNION) which is parsed into the `queryBody` of the `node`.
// Use child relation to process expressions of the "root" Query node
statementContext.startRelation();
RelationAnalysisContext relationAnalysisContext = statementContext.currentRelationContext();
relationAnalysisContext.addSourceRelation(childRelation);
statementContext.endRelation();
List<Symbol> childRelationFields = childRelation.outputs();
var coordinatorTxnCtx = statementContext.transactionContext();
ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(coordinatorTxnCtx, nodeCtx, statementContext.paramTyeHints(), new FullQualifiedNameFieldProvider(relationAnalysisContext.sources(), relationAnalysisContext.parentSources(), coordinatorTxnCtx.sessionContext().searchPath().currentSchema()), new SubqueryAnalyzer(this, statementContext));
ExpressionAnalysisContext expressionAnalysisContext = relationAnalysisContext.expressionAnalysisContext();
SelectAnalysis selectAnalysis = new SelectAnalysis(childRelationFields.size(), relationAnalysisContext.sources(), expressionAnalyzer, expressionAnalysisContext);
for (Symbol field : childRelationFields) {
selectAnalysis.add(Symbols.pathFromSymbol(field), field);
}
var normalizer = EvaluatingNormalizer.functionOnlyNormalizer(nodeCtx, f -> expressionAnalysisContext.isEagerNormalizationAllowed() && f.isDeterministic());
return new QueriedSelectRelation(false, List.of(childRelation), List.of(), selectAnalysis.outputSymbols(), Literal.BOOLEAN_TRUE, List.of(), null, analyzeOrderBy(selectAnalysis, node.getOrderBy(), expressionAnalyzer, expressionAnalysisContext, false, false), longSymbolOrNull(node.getLimit(), expressionAnalyzer, expressionAnalysisContext, normalizer, coordinatorTxnCtx), longSymbolOrNull(node.getOffset(), expressionAnalyzer, expressionAnalysisContext, normalizer, coordinatorTxnCtx));
}
use of io.crate.analyze.QueriedSelectRelation in project crate by crate.
the class LogicalPlanner method tryOptimizeForInSubquery.
// In case the subselect is inside an IN() or = ANY() apply a "natural" OrderBy to optimize
// the building of TermInSetQuery which does a sort on the collection of values.
// See issue https://github.com/crate/crate/issues/6755
// If the output values are already sorted (even in desc order) no optimization is needed
private LogicalPlan tryOptimizeForInSubquery(SelectSymbol selectSymbol, AnalyzedRelation relation, LogicalPlan planBuilder) {
if (selectSymbol.getResultType() == SelectSymbol.ResultType.SINGLE_COLUMN_MULTIPLE_VALUES && relation instanceof QueriedSelectRelation) {
QueriedSelectRelation queriedRelation = (QueriedSelectRelation) relation;
OrderBy relationOrderBy = queriedRelation.orderBy();
Symbol firstOutput = queriedRelation.outputs().get(0);
if ((relationOrderBy == null || relationOrderBy.orderBySymbols().get(0).equals(firstOutput) == false) && DataTypes.isPrimitive(firstOutput.valueType())) {
return Order.create(planBuilder, new OrderBy(Collections.singletonList(firstOutput)));
}
}
return planBuilder;
}
use of io.crate.analyze.QueriedSelectRelation in project crate by crate.
the class GeneratedColumnsTest method testSubscriptExpressionThatReturnsAnArray.
@Test
public void testSubscriptExpressionThatReturnsAnArray() throws Exception {
SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table t (obj object as (arr array(integer)), arr as obj['arr'])").build();
QueriedSelectRelation query = e.analyze("select obj, arr from t");
DocTableInfo table = ((DocTableRelation) query.from().get(0)).tableInfo();
GeneratedColumns<Doc> generatedColumns = new GeneratedColumns<>(new InputFactory(e.nodeCtx), CoordinatorTxnCtx.systemTransactionContext(), GeneratedColumns.Validation.NONE, new DocRefResolver(Collections.emptyList()), Collections.emptyList(), table.generatedColumns());
BytesReference bytes = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().startObject("obj").startArray("arr").value(10).value(20).endArray().endObject().endObject());
generatedColumns.setNextRow(new Doc(1, table.concreteIndices()[0], "1", 1, 1, 1, XContentHelper.convertToMap(bytes, false, XContentType.JSON).v2(), bytes::utf8ToString));
Map.Entry<Reference, Input<?>> generatedColumn = generatedColumns.generatedToInject().iterator().next();
assertThat((List<Object>) generatedColumn.getValue().value(), contains(10, 20));
}
use of io.crate.analyze.QueriedSelectRelation in project crate by crate.
the class SplitPointsTest method testScalarIsNotCollectedEarly.
@Test
public void testScalarIsNotCollectedEarly() throws Exception {
QueriedSelectRelation relation = e.analyze("select x + 1 from t1 group by x");
SplitPoints splitPoints = SplitPointsBuilder.create(relation);
assertThat(splitPoints.toCollect(), contains(isReference("x")));
assertThat(splitPoints.aggregates(), Matchers.emptyIterable());
}
use of io.crate.analyze.QueriedSelectRelation in project crate by crate.
the class SplitPointsTest method test_split_points_creation_with_filter_in_aggregate_fo_window_function_call.
@Test
public void test_split_points_creation_with_filter_in_aggregate_fo_window_function_call() {
QueriedSelectRelation relation = e.analyze("select sum(i) filter (where x > 1) over(order by i) from t1");
SplitPoints splitPoints = SplitPointsBuilder.create(relation);
assertThat(splitPoints.toCollect(), contains(isReference("i"), isFunction("op_>", isReference("x"), isLiteral(1))));
assertThat(splitPoints.windowFunctions(), contains(isFunction("sum")));
assertThat(splitPoints.aggregates(), is(empty()));
}
Aggregations