Search in sources :

Example 1 with Relation

use of io.crate.sql.tree.Relation in project crate by crate.

the class AstBuilder method visitJoinRelation.

// From clause
@Override
public Node visitJoinRelation(SqlBaseParser.JoinRelationContext context) {
    Relation left = (Relation) visit(context.left);
    Relation right;
    if (context.CROSS() != null) {
        right = (Relation) visit(context.right);
        return new Join(Join.Type.CROSS, left, right, Optional.empty());
    }
    JoinCriteria criteria;
    if (context.NATURAL() != null) {
        right = (Relation) visit(context.right);
        criteria = new NaturalJoin();
    } else {
        right = (Relation) visit(context.rightRelation);
        if (context.joinCriteria().ON() != null) {
            criteria = new JoinOn((Expression) visit(context.joinCriteria().booleanExpression()));
        } else if (context.joinCriteria().USING() != null) {
            List<String> columns = identsToStrings(context.joinCriteria().ident());
            criteria = new JoinUsing(columns);
        } else {
            throw new IllegalArgumentException("Unsupported join criteria");
        }
    }
    return new Join(getJoinType(context.joinType()), left, right, Optional.of(criteria));
}
Also used : AliasedRelation(io.crate.sql.tree.AliasedRelation) Relation(io.crate.sql.tree.Relation) SubscriptExpression(io.crate.sql.tree.SubscriptExpression) IfExpression(io.crate.sql.tree.IfExpression) NotExpression(io.crate.sql.tree.NotExpression) SearchedCaseExpression(io.crate.sql.tree.SearchedCaseExpression) LogicalBinaryExpression(io.crate.sql.tree.LogicalBinaryExpression) ArraySliceExpression(io.crate.sql.tree.ArraySliceExpression) ArraySubQueryExpression(io.crate.sql.tree.ArraySubQueryExpression) SimpleCaseExpression(io.crate.sql.tree.SimpleCaseExpression) SubqueryExpression(io.crate.sql.tree.SubqueryExpression) InListExpression(io.crate.sql.tree.InListExpression) ParameterExpression(io.crate.sql.tree.ParameterExpression) ArrayComparisonExpression(io.crate.sql.tree.ArrayComparisonExpression) Expression(io.crate.sql.tree.Expression) ComparisonExpression(io.crate.sql.tree.ComparisonExpression) ArithmeticExpression(io.crate.sql.tree.ArithmeticExpression) NegativeExpression(io.crate.sql.tree.NegativeExpression) JoinCriteria(io.crate.sql.tree.JoinCriteria) Join(io.crate.sql.tree.Join) NaturalJoin(io.crate.sql.tree.NaturalJoin) ValuesList(io.crate.sql.tree.ValuesList) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) JoinUsing(io.crate.sql.tree.JoinUsing) NaturalJoin(io.crate.sql.tree.NaturalJoin) JoinOn(io.crate.sql.tree.JoinOn)

Example 2 with Relation

use of io.crate.sql.tree.Relation in project crate by crate.

the class RelationAnalyzer method visitQuerySpecification.

@Override
protected AnalyzedRelation visitQuerySpecification(QuerySpecification node, StatementAnalysisContext statementContext) {
    List<Relation> from = node.getFrom().isEmpty() ? EMPTY_ROW_TABLE_RELATION : node.getFrom();
    RelationAnalysisContext currentRelationContext = statementContext.startRelation();
    for (Relation relation : from) {
        // different from relations have to be isolated from each other
        RelationAnalysisContext innerContext = statementContext.startRelation();
        relation.accept(this, statementContext);
        statementContext.endRelation();
        for (Map.Entry<RelationName, AnalyzedRelation> entry : innerContext.sources().entrySet()) {
            currentRelationContext.addSourceRelation(entry.getValue());
        }
        for (JoinPair joinPair : innerContext.joinPairs()) {
            currentRelationContext.addJoinPair(joinPair);
        }
    }
    RelationAnalysisContext context = statementContext.currentRelationContext();
    CoordinatorTxnCtx coordinatorTxnCtx = statementContext.transactionContext();
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(coordinatorTxnCtx, nodeCtx, statementContext.paramTyeHints(), new FullQualifiedNameFieldProvider(context.sources(), context.parentSources(), coordinatorTxnCtx.sessionContext().searchPath().currentSchema()), new SubqueryAnalyzer(this, statementContext));
    ExpressionAnalysisContext expressionAnalysisContext = context.expressionAnalysisContext();
    expressionAnalysisContext.windows(node.getWindows());
    SelectAnalysis selectAnalysis = SelectAnalyzer.analyzeSelectItems(node.getSelect().getSelectItems(), context.sources(), expressionAnalyzer, expressionAnalysisContext);
    List<Symbol> groupBy = analyzeGroupBy(selectAnalysis, node.getGroupBy(), expressionAnalyzer, expressionAnalysisContext);
    if (!node.getGroupBy().isEmpty() || expressionAnalysisContext.hasAggregates()) {
        GroupAndAggregateSemantics.validate(selectAnalysis.outputSymbols(), groupBy);
    }
    boolean isDistinct = node.getSelect().isDistinct();
    Symbol where = expressionAnalyzer.generateQuerySymbol(node.getWhere(), expressionAnalysisContext);
    WhereClauseValidator.validate(where);
    var normalizer = EvaluatingNormalizer.functionOnlyNormalizer(nodeCtx, f -> expressionAnalysisContext.isEagerNormalizationAllowed() && f.isDeterministic());
    QueriedSelectRelation relation = new QueriedSelectRelation(isDistinct, List.copyOf(context.sources().values()), context.joinPairs(), selectAnalysis.outputSymbols(), where, groupBy, analyzeHaving(node.getHaving(), groupBy, expressionAnalyzer, context.expressionAnalysisContext()), analyzeOrderBy(selectAnalysis, node.getOrderBy(), expressionAnalyzer, expressionAnalysisContext, expressionAnalysisContext.hasAggregates() || !groupBy.isEmpty(), isDistinct), longSymbolOrNull(node.getLimit(), expressionAnalyzer, expressionAnalysisContext, normalizer, coordinatorTxnCtx), longSymbolOrNull(node.getOffset(), expressionAnalyzer, expressionAnalysisContext, normalizer, coordinatorTxnCtx));
    statementContext.endRelation();
    return relation;
}
Also used : SelectAnalysis(io.crate.analyze.relations.select.SelectAnalysis) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Symbol(io.crate.expression.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) Relation(io.crate.sql.tree.Relation) QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) AliasedRelation(io.crate.sql.tree.AliasedRelation) QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) RelationName(io.crate.metadata.RelationName) SubqueryAnalyzer(io.crate.analyze.expressions.SubqueryAnalyzer) Map(java.util.Map)

Example 3 with Relation

use of io.crate.sql.tree.Relation in project crate by crate.

the class RelationAnalyzer method visitValues.

@Override
public AnalyzedRelation visitValues(Values values, StatementAnalysisContext context) {
    var expressionAnalyzer = new ExpressionAnalyzer(context.transactionContext(), nodeCtx, context.paramTyeHints(), FieldProvider.UNSUPPORTED, new SubqueryAnalyzer(this, context));
    var expressionAnalysisContext = new ExpressionAnalysisContext(context.sessionContext());
    // prevent normalization of the values array, otherwise the value literals are converted to an array literal
    // and a special per-value-literal casting logic won't be executed (e.g. FloatLiteral.cast())
    expressionAnalysisContext.allowEagerNormalize(false);
    java.util.function.Function<Expression, Symbol> expressionToSymbol = e -> expressionAnalyzer.convert(e, expressionAnalysisContext);
    // There is a first pass to convert expressions from row oriented format:
    // `[[1, a], [2, b]]` to columns `[[1, 2], [a, b]]`
    // 
    // At the same time we determine the column type with the highest precedence,
    // so that we don't fail with slight type miss-matches (long vs. int)
    List<ValuesList> rows = values.rows();
    assert rows.size() > 0 : "Parser grammar enforces at least 1 row";
    ValuesList firstRow = rows.get(0);
    int numColumns = firstRow.values().size();
    ArrayList<List<Symbol>> columns = new ArrayList<>();
    ArrayList<DataType<?>> targetTypes = new ArrayList<>(numColumns);
    var parentOutputColumns = context.parentOutputColumns();
    for (int c = 0; c < numColumns; c++) {
        ArrayList<Symbol> columnValues = new ArrayList<>();
        DataType<?> targetType;
        boolean usePrecedence = true;
        if (parentOutputColumns.size() > c) {
            targetType = parentOutputColumns.get(c).valueType();
            usePrecedence = false;
        } else {
            targetType = DataTypes.UNDEFINED;
        }
        for (int r = 0; r < rows.size(); r++) {
            List<Expression> row = rows.get(r).values();
            if (row.size() != numColumns) {
                throw new IllegalArgumentException("VALUES lists must all be the same length. " + "Found row with " + numColumns + " items and another with " + columns.size() + " items");
            }
            Symbol cell = expressionToSymbol.apply(row.get(c));
            columnValues.add(cell);
            var cellType = cell.valueType();
            if (// skip first cell, we don't have to check for self-conversion
            r > 0 && !cellType.isConvertableTo(targetType, false) && targetType.id() != DataTypes.UNDEFINED.id()) {
                throw new IllegalArgumentException("The types of the columns within VALUES lists must match. " + "Found `" + targetType + "` and `" + cellType + "` at position: " + c);
            }
            if (usePrecedence && cellType.precedes(targetType)) {
                targetType = cellType;
            } else if (targetType == DataTypes.UNDEFINED) {
                targetType = cellType;
            }
        }
        targetTypes.add(targetType);
        columns.add(columnValues);
    }
    var normalizer = EvaluatingNormalizer.functionOnlyNormalizer(nodeCtx, f -> f.isDeterministic());
    ArrayList<Symbol> arrays = new ArrayList<>(columns.size());
    for (int c = 0; c < numColumns; c++) {
        DataType<?> targetType = targetTypes.get(c);
        ArrayType<?> arrayType = new ArrayType<>(targetType);
        List<Symbol> columnValues = Lists2.map(columns.get(c), s -> normalizer.normalize(s.cast(targetType), context.transactionContext()));
        arrays.add(new Function(ArrayFunction.SIGNATURE, columnValues, arrayType));
    }
    FunctionImplementation implementation = nodeCtx.functions().getQualified(ValuesFunction.SIGNATURE, Symbols.typeView(arrays), RowType.EMPTY);
    Function function = new Function(implementation.signature(), arrays, RowType.EMPTY);
    TableFunctionImplementation<?> tableFunc = TableFunctionFactory.from(implementation);
    TableFunctionRelation relation = new TableFunctionRelation(tableFunc, function);
    context.startRelation();
    context.currentRelationContext().addSourceRelation(relation);
    context.endRelation();
    return relation;
}
Also used : ParamTypeHints(io.crate.analyze.ParamTypeHints) JoinCriteria(io.crate.sql.tree.JoinCriteria) UnsupportedFeatureException(io.crate.exceptions.UnsupportedFeatureException) RelationName(io.crate.metadata.RelationName) RowType(io.crate.types.RowType) DefaultTraversalVisitor(io.crate.sql.tree.DefaultTraversalVisitor) EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) Node(io.crate.sql.tree.Node) ArrayType(io.crate.types.ArrayType) JoinType(io.crate.planner.node.dql.join.JoinType) JoinOn(io.crate.sql.tree.JoinOn) Locale(java.util.Locale) Map(java.util.Map) SelectAnalysis(io.crate.analyze.relations.select.SelectAnalysis) ValuesList(io.crate.sql.tree.ValuesList) Union(io.crate.sql.tree.Union) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) NodeContext(io.crate.metadata.NodeContext) SortItem(io.crate.sql.tree.SortItem) Collection(java.util.Collection) QuerySpecification(io.crate.sql.tree.QuerySpecification) TableSubquery(io.crate.sql.tree.TableSubquery) Set(java.util.Set) SelectAnalyzer(io.crate.analyze.relations.select.SelectAnalyzer) Intersect(io.crate.sql.tree.Intersect) WhereClauseValidator(io.crate.analyze.where.WhereClauseValidator) Iterables(io.crate.common.collections.Iterables) Lists2(io.crate.common.collections.Lists2) Relation(io.crate.sql.tree.Relation) List(java.util.List) OrderBy(io.crate.analyze.OrderBy) Symbol(io.crate.expression.symbol.Symbol) FunctionImplementation(io.crate.metadata.FunctionImplementation) DataTypes(io.crate.types.DataTypes) Singleton(org.elasticsearch.common.inject.Singleton) Values(io.crate.sql.tree.Values) Optional(java.util.Optional) Expression(io.crate.sql.tree.Expression) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) SemanticSortValidator(io.crate.analyze.validator.SemanticSortValidator) AmbiguousColumnAliasException(io.crate.exceptions.AmbiguousColumnAliasException) Tuple(io.crate.common.collections.Tuple) SubqueryAnalyzer(io.crate.analyze.expressions.SubqueryAnalyzer) FunctionCall(io.crate.sql.tree.FunctionCall) QualifiedNameReference(io.crate.sql.tree.QualifiedNameReference) QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) SearchPath(io.crate.metadata.SearchPath) Operation(io.crate.metadata.table.Operation) HavingSymbolValidator(io.crate.analyze.validator.HavingSymbolValidator) AliasedRelation(io.crate.sql.tree.AliasedRelation) Inject(org.elasticsearch.common.inject.Inject) ArrayList(java.util.ArrayList) ValuesFunction(io.crate.expression.tablefunctions.ValuesFunction) Join(io.crate.sql.tree.Join) Symbols(io.crate.expression.symbol.Symbols) SqlParser(io.crate.sql.parser.SqlParser) Query(io.crate.sql.tree.Query) QualifiedName(io.crate.sql.tree.QualifiedName) TableFunction(io.crate.sql.tree.TableFunction) Nullable(javax.annotation.Nullable) RelationValidationException(io.crate.exceptions.RelationValidationException) ArrayFunction(io.crate.expression.scalar.arithmetic.ArrayFunction) OrderByWithAggregationValidator(io.crate.planner.consumer.OrderByWithAggregationValidator) ViewMetadata(io.crate.metadata.view.ViewMetadata) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) TableFunctionFactory(io.crate.expression.tablefunctions.TableFunctionFactory) Table(io.crate.sql.tree.Table) DataType(io.crate.types.DataType) Function(io.crate.expression.symbol.Function) GroupAndAggregateSemantics(io.crate.expression.symbol.GroupAndAggregateSemantics) JoinUsing(io.crate.sql.tree.JoinUsing) TableFunctionImplementation(io.crate.metadata.tablefunctions.TableFunctionImplementation) Except(io.crate.sql.tree.Except) Literal(io.crate.expression.symbol.Literal) GroupBySymbolValidator(io.crate.analyze.validator.GroupBySymbolValidator) ColumnUnknownException(io.crate.exceptions.ColumnUnknownException) Schemas(io.crate.metadata.Schemas) RelationUnknown(io.crate.exceptions.RelationUnknown) Collections(java.util.Collections) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Symbol(io.crate.expression.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) ArrayList(java.util.ArrayList) ArrayType(io.crate.types.ArrayType) ValuesFunction(io.crate.expression.tablefunctions.ValuesFunction) TableFunction(io.crate.sql.tree.TableFunction) ArrayFunction(io.crate.expression.scalar.arithmetic.ArrayFunction) Function(io.crate.expression.symbol.Function) DataType(io.crate.types.DataType) ValuesList(io.crate.sql.tree.ValuesList) List(java.util.List) ArrayList(java.util.ArrayList) FunctionImplementation(io.crate.metadata.FunctionImplementation) TableFunctionImplementation(io.crate.metadata.tablefunctions.TableFunctionImplementation) ValuesList(io.crate.sql.tree.ValuesList) Expression(io.crate.sql.tree.Expression) SubqueryAnalyzer(io.crate.analyze.expressions.SubqueryAnalyzer)

Aggregations

AliasedRelation (io.crate.sql.tree.AliasedRelation)3 Relation (io.crate.sql.tree.Relation)3 QueriedSelectRelation (io.crate.analyze.QueriedSelectRelation)2 ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)2 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)2 SubqueryAnalyzer (io.crate.analyze.expressions.SubqueryAnalyzer)2 SelectAnalysis (io.crate.analyze.relations.select.SelectAnalysis)2 Symbol (io.crate.expression.symbol.Symbol)2 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)2 RelationName (io.crate.metadata.RelationName)2 Map (java.util.Map)2 OrderBy (io.crate.analyze.OrderBy)1 ParamTypeHints (io.crate.analyze.ParamTypeHints)1 SelectAnalyzer (io.crate.analyze.relations.select.SelectAnalyzer)1 GroupBySymbolValidator (io.crate.analyze.validator.GroupBySymbolValidator)1 HavingSymbolValidator (io.crate.analyze.validator.HavingSymbolValidator)1 SemanticSortValidator (io.crate.analyze.validator.SemanticSortValidator)1 WhereClauseValidator (io.crate.analyze.where.WhereClauseValidator)1 Iterables (io.crate.common.collections.Iterables)1 Lists2 (io.crate.common.collections.Lists2)1