Search in sources :

Example 1 with QualifiedNameReference

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

the class AstBuilder method visitInsert.

@Override
public Node visitInsert(SqlBaseParser.InsertContext context) {
    List<String> columns = identsToStrings(context.ident());
    Table table;
    try {
        table = (Table) visit(context.table());
    } catch (ClassCastException e) {
        TableFunction tf = (TableFunction) visit(context.table());
        for (Expression ex : tf.functionCall().getArguments()) {
            if (!(ex instanceof QualifiedNameReference)) {
                throw new IllegalArgumentException(String.format(Locale.ENGLISH, "invalid table column reference %s", ex.toString()));
            }
        }
        throw e;
    }
    return new Insert<>(table, (Query) visit(context.insertSource().query()), columns, getReturningItems(context.returning()), createDuplicateKeyContext(context));
}
Also used : DropBlobTable(io.crate.sql.tree.DropBlobTable) ShowCreateTable(io.crate.sql.tree.ShowCreateTable) SwapTable(io.crate.sql.tree.SwapTable) CreateBlobTable(io.crate.sql.tree.CreateBlobTable) Table(io.crate.sql.tree.Table) AlterTable(io.crate.sql.tree.AlterTable) AlterBlobTable(io.crate.sql.tree.AlterBlobTable) CreateTable(io.crate.sql.tree.CreateTable) DropTable(io.crate.sql.tree.DropTable) 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) TableFunction(io.crate.sql.tree.TableFunction) BitString(io.crate.sql.tree.BitString) Insert(io.crate.sql.tree.Insert) QualifiedNameReference(io.crate.sql.tree.QualifiedNameReference)

Example 2 with QualifiedNameReference

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

the class InsertAnalyzer method verifyOnConflictTargets.

private static void verifyOnConflictTargets(CoordinatorTxnCtx txnCtx, ExpressionAnalyzer expressionAnalyzer, DocTableInfo docTable, Insert.DuplicateKeyContext<Expression> duplicateKeyContext) {
    List<Expression> constraintColumns = duplicateKeyContext.getConstraintColumns();
    if (constraintColumns.isEmpty()) {
        return;
    }
    List<ColumnIdent> pkColumnIdents = docTable.primaryKey();
    if (constraintColumns.size() != pkColumnIdents.size()) {
        throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Number of conflict targets (%s) did not match the number of primary key columns (%s)", constraintColumns, pkColumnIdents));
    }
    ExpressionAnalysisContext ctx = new ExpressionAnalysisContext(txnCtx.sessionContext());
    List<Symbol> conflictTargets = Lists2.map(constraintColumns, x -> {
        try {
            return expressionAnalyzer.convert(x, ctx);
        } catch (ColumnUnknownException e) {
            // Going through ExpressionAnalyzer again to still have a "column must exist" validation
            if (x instanceof QualifiedNameReference) {
                QualifiedName name = ((QualifiedNameReference) x).getName();
                Expression subscriptExpression = MetadataToASTNodeResolver.expressionFromColumn(ColumnIdent.fromPath(name.toString()));
                return expressionAnalyzer.convert(subscriptExpression, ctx);
            }
            throw e;
        }
    });
    for (Symbol conflictTarget : conflictTargets) {
        if (!pkColumnIdents.contains(Symbols.pathFromSymbol(conflictTarget))) {
            throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Conflict target (%s) did not match the primary key columns (%s)", conflictTarget, pkColumnIdents));
        }
    }
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Expression(io.crate.sql.tree.Expression) ColumnUnknownException(io.crate.exceptions.ColumnUnknownException) Symbol(io.crate.expression.symbol.Symbol) QualifiedName(io.crate.sql.tree.QualifiedName) QualifiedNameReference(io.crate.sql.tree.QualifiedNameReference)

Example 3 with QualifiedNameReference

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

the class DeallocateAnalyzer method analyze.

public static AnalyzedDeallocate analyze(DeallocateStatement deallocateStatement) {
    Expression preparedStmtExpression = deallocateStatement.preparedStmt();
    String preparedStmt = null;
    if (preparedStmtExpression != null) {
        if (preparedStmtExpression instanceof StringLiteral) {
            preparedStmt = ((StringLiteral) preparedStmtExpression).getValue();
        } else if (preparedStmtExpression instanceof QualifiedNameReference) {
            preparedStmt = ((QualifiedNameReference) preparedStmtExpression).getName().toString();
        } else {
            throw new AssertionError("Expression " + preparedStmtExpression.toString() + " not supported as " + "preparedStmt expression for DEALLOCATE");
        }
    }
    return new AnalyzedDeallocate(preparedStmt);
}
Also used : StringLiteral(io.crate.sql.tree.StringLiteral) Expression(io.crate.sql.tree.Expression) QualifiedNameReference(io.crate.sql.tree.QualifiedNameReference)

Example 4 with QualifiedNameReference

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

the class OrderByAnalyzerTest method analyzeSortItems.

@Test
public void analyzeSortItems() {
    List<SortItem> sortItems = new ArrayList<>(2);
    QualifiedName tx = QualifiedName.of("t", "x");
    SortItem firstSort = new SortItem(new QualifiedNameReference(tx), SortItem.Ordering.ASCENDING, SortItem.NullOrdering.FIRST);
    sortItems.add(firstSort);
    QualifiedName ty = QualifiedName.of("t", "y");
    SortItem second = new SortItem(new QualifiedNameReference(ty), SortItem.Ordering.DESCENDING, SortItem.NullOrdering.LAST);
    sortItems.add(second);
    OrderBy orderBy = OrderyByAnalyzer.analyzeSortItems(sortItems, e -> Literal.of(((QualifiedNameReference) e).getName().toString()));
    assertThat(orderBy, is(notNullValue()));
    List<Symbol> orderBySymbols = orderBy.orderBySymbols();
    assertThat(orderBySymbols.size(), is(2));
    assertThat(orderBySymbols.get(0), SymbolMatchers.isLiteral("t.x"));
    assertThat(orderBySymbols.get(1), SymbolMatchers.isLiteral("t.y"));
    boolean[] reverseFlags = orderBy.reverseFlags();
    assertThat(reverseFlags.length, is(2));
    assertThat(reverseFlags[0], is(false));
    assertThat(reverseFlags[1], is(true));
    boolean[] nullsFirst = orderBy.nullsFirst();
    assertThat(nullsFirst.length, is(2));
    assertThat(nullsFirst[0], is(true));
    assertThat(nullsFirst[1], is(false));
}
Also used : OrderBy(io.crate.analyze.OrderBy) SortItem(io.crate.sql.tree.SortItem) Symbol(io.crate.expression.symbol.Symbol) QualifiedName(io.crate.sql.tree.QualifiedName) ArrayList(java.util.ArrayList) QualifiedNameReference(io.crate.sql.tree.QualifiedNameReference) Test(org.junit.Test)

Example 5 with QualifiedNameReference

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

the class ExpressionToColumnIdentVisitor method visitSubscriptExpression.

@Override
protected ColumnIdent visitSubscriptExpression(SubscriptExpression node, List<String> context) {
    if (node.index() instanceof QualifiedNameReference) {
        throw new IllegalArgumentException("Key of subscript must not be a reference");
    }
    if (context == null) {
        context = new ArrayList<>();
    }
    ColumnIdent colIdent = node.base().accept(this, context);
    node.index().accept(this, context);
    return colIdent;
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) QualifiedNameReference(io.crate.sql.tree.QualifiedNameReference)

Aggregations

QualifiedNameReference (io.crate.sql.tree.QualifiedNameReference)6 Expression (io.crate.sql.tree.Expression)4 QualifiedName (io.crate.sql.tree.QualifiedName)3 Symbol (io.crate.expression.symbol.Symbol)2 ColumnIdent (io.crate.metadata.ColumnIdent)2 ArrayComparisonExpression (io.crate.sql.tree.ArrayComparisonExpression)2 ComparisonExpression (io.crate.sql.tree.ComparisonExpression)2 NegativeExpression (io.crate.sql.tree.NegativeExpression)2 ParameterExpression (io.crate.sql.tree.ParameterExpression)2 StringLiteral (io.crate.sql.tree.StringLiteral)2 SubqueryExpression (io.crate.sql.tree.SubqueryExpression)2 SubscriptExpression (io.crate.sql.tree.SubscriptExpression)2 Test (org.junit.Test)2 OrderBy (io.crate.analyze.OrderBy)1 ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)1 ColumnUnknownException (io.crate.exceptions.ColumnUnknownException)1 AlterBlobTable (io.crate.sql.tree.AlterBlobTable)1 AlterTable (io.crate.sql.tree.AlterTable)1 ArithmeticExpression (io.crate.sql.tree.ArithmeticExpression)1 ArraySliceExpression (io.crate.sql.tree.ArraySliceExpression)1