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