use of io.crate.analyze.relations.StatementAnalysisContext in project crate by crate.
the class DeleteAnalyzer method analyze.
public AnalyzedDeleteStatement analyze(Delete delete, ParamTypeHints typeHints, CoordinatorTxnCtx txnContext) {
StatementAnalysisContext stmtCtx = new StatementAnalysisContext(typeHints, Operation.DELETE, txnContext);
final RelationAnalysisContext relationCtx = stmtCtx.startRelation();
AnalyzedRelation relation = relationAnalyzer.analyze(delete.getRelation(), stmtCtx);
stmtCtx.endRelation();
MaybeAliasedStatement maybeAliasedStatement = MaybeAliasedStatement.analyze(relation);
relation = maybeAliasedStatement.nonAliasedRelation();
if (!(relation instanceof DocTableRelation)) {
throw new UnsupportedOperationException("Cannot delete from relations other than base tables");
}
DocTableRelation table = (DocTableRelation) relation;
EvaluatingNormalizer normalizer = new EvaluatingNormalizer(nodeCtx, RowGranularity.CLUSTER, null, table);
ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(txnContext, nodeCtx, typeHints, new FullQualifiedNameFieldProvider(relationCtx.sources(), relationCtx.parentSources(), txnContext.sessionContext().searchPath().currentSchema()), new SubqueryAnalyzer(relationAnalyzer, new StatementAnalysisContext(typeHints, Operation.READ, txnContext)));
Symbol query = Objects.requireNonNullElse(expressionAnalyzer.generateQuerySymbol(delete.getWhere(), new ExpressionAnalysisContext(txnContext.sessionContext())), Literal.BOOLEAN_TRUE);
query = maybeAliasedStatement.maybeMapFields(query);
Symbol normalizedQuery = normalizer.normalize(query, txnContext);
return new AnalyzedDeleteStatement(table, normalizedQuery);
}
use of io.crate.analyze.relations.StatementAnalysisContext in project crate by crate.
the class SQLExecutor method asSymbol.
/**
* Convert a expression to a symbol
* If tables are used here they must also be registered in the SQLExecutor having used {@link Builder#addTable(String)}
*/
public Symbol asSymbol(String expression) {
MapBuilder<RelationName, AnalyzedRelation> sources = MapBuilder.newMapBuilder();
for (SchemaInfo schemaInfo : schemas) {
for (TableInfo tableInfo : schemaInfo.getTables()) {
if (tableInfo instanceof DocTableInfo) {
RelationName relationName = tableInfo.ident();
sources.put(relationName, new DocTableRelation(schemas.getTableInfo(relationName)));
}
}
}
CoordinatorTxnCtx coordinatorTxnCtx = new CoordinatorTxnCtx(sessionContext);
ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(coordinatorTxnCtx, nodeCtx, ParamTypeHints.EMPTY, new FullQualifiedNameFieldProvider(sources.immutableMap(), ParentRelations.NO_PARENTS, sessionContext.searchPath().currentSchema()), new SubqueryAnalyzer(relAnalyzer, new StatementAnalysisContext(ParamTypeHints.EMPTY, Operation.READ, coordinatorTxnCtx)));
ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext(coordinatorTxnCtx.sessionContext());
return expressionAnalyzer.convert(SqlParser.createExpression(expression), expressionAnalysisContext);
}
use of io.crate.analyze.relations.StatementAnalysisContext in project crate by crate.
the class UpdateAnalyzer method analyze.
public AnalyzedUpdateStatement analyze(Update update, ParamTypeHints typeHints, CoordinatorTxnCtx txnCtx) {
/* UPDATE t1 SET col1 = ?, col2 = ? WHERE id = ?`
* ^^^^^^^^^^^^^^^^^^ ^^^^^^
* assignments whereClause
*
* col1 = ?
* | |
* | source
* columnName/target
*/
StatementAnalysisContext stmtCtx = new StatementAnalysisContext(typeHints, Operation.UPDATE, txnCtx);
final RelationAnalysisContext relCtx = stmtCtx.startRelation();
AnalyzedRelation relation = relationAnalyzer.analyze(update.relation(), stmtCtx);
stmtCtx.endRelation();
MaybeAliasedStatement maybeAliasedStatement = MaybeAliasedStatement.analyze(relation);
relation = maybeAliasedStatement.nonAliasedRelation();
if (!(relation instanceof AbstractTableRelation)) {
throw new UnsupportedOperationException("UPDATE is only supported on base-tables");
}
AbstractTableRelation<?> table = (AbstractTableRelation<?>) relation;
EvaluatingNormalizer normalizer = new EvaluatingNormalizer(nodeCtx, RowGranularity.CLUSTER, null, table);
SubqueryAnalyzer subqueryAnalyzer = new SubqueryAnalyzer(relationAnalyzer, new StatementAnalysisContext(typeHints, Operation.READ, txnCtx));
ExpressionAnalyzer sourceExprAnalyzer = new ExpressionAnalyzer(txnCtx, nodeCtx, typeHints, new FullQualifiedNameFieldProvider(relCtx.sources(), relCtx.parentSources(), txnCtx.sessionContext().searchPath().currentSchema()), subqueryAnalyzer);
ExpressionAnalysisContext exprCtx = new ExpressionAnalysisContext(txnCtx.sessionContext());
Map<Reference, Symbol> assignmentByTargetCol = getAssignments(update.assignments(), typeHints, txnCtx, table, normalizer, subqueryAnalyzer, sourceExprAnalyzer, exprCtx);
Symbol query = Objects.requireNonNullElse(sourceExprAnalyzer.generateQuerySymbol(update.whereClause(), exprCtx), Literal.BOOLEAN_TRUE);
query = maybeAliasedStatement.maybeMapFields(query);
Symbol normalizedQuery = normalizer.normalize(query, txnCtx);
SelectAnalysis selectAnalysis = SelectAnalyzer.analyzeSelectItems(update.returningClause(), relCtx.sources(), sourceExprAnalyzer, exprCtx);
List<Symbol> outputSymbol = Lists2.map(selectAnalysis.outputSymbols(), x -> normalizer.normalize(x, txnCtx));
return new AnalyzedUpdateStatement(table, assignmentByTargetCol, normalizedQuery, outputSymbol.isEmpty() ? null : outputSymbol);
}
use of io.crate.analyze.relations.StatementAnalysisContext in project crate by crate.
the class InsertAnalyzer method analyze.
public AnalyzedInsertStatement analyze(Insert<Expression> insert, ParamTypeHints typeHints, CoordinatorTxnCtx txnCtx) {
DocTableInfo tableInfo = (DocTableInfo) schemas.resolveTableInfo(insert.table().getName(), Operation.INSERT, txnCtx.sessionContext().sessionUser(), txnCtx.sessionContext().searchPath());
List<Reference> targetColumns = new ArrayList<>(resolveTargetColumns(insert.columns(), tableInfo));
AnalyzedRelation subQueryRelation = relationAnalyzer.analyze(insert.insertSource(), new StatementAnalysisContext(typeHints, Operation.READ, txnCtx, targetColumns));
ensureClusteredByPresentOrNotRequired(targetColumns, tableInfo);
checkSourceAndTargetColsForLengthAndTypesCompatibility(targetColumns, subQueryRelation.outputs());
DocTableRelation tableRelation = new DocTableRelation(tableInfo);
NameFieldProvider fieldProvider = new NameFieldProvider(tableRelation);
ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(txnCtx, nodeCtx, typeHints, fieldProvider, null, Operation.READ);
verifyOnConflictTargets(txnCtx, expressionAnalyzer, tableInfo, insert.duplicateKeyContext());
Map<Reference, Symbol> onDuplicateKeyAssignments = processUpdateAssignments(tableRelation, targetColumns, typeHints, txnCtx, nodeCtx, fieldProvider, insert.duplicateKeyContext());
final boolean ignoreDuplicateKeys = insert.duplicateKeyContext().getType() == Insert.DuplicateKeyContext.Type.ON_CONFLICT_DO_NOTHING;
List<Symbol> returnValues;
if (insert.returningClause().isEmpty()) {
returnValues = null;
} else {
var exprCtx = new ExpressionAnalysisContext(txnCtx.sessionContext());
Map<RelationName, AnalyzedRelation> sources = Map.of(tableRelation.relationName(), tableRelation);
var sourceExprAnalyzer = new ExpressionAnalyzer(txnCtx, nodeCtx, typeHints, new FullQualifiedNameFieldProvider(sources, ParentRelations.NO_PARENTS, txnCtx.sessionContext().searchPath().currentSchema()), null);
var selectAnalysis = SelectAnalyzer.analyzeSelectItems(insert.returningClause(), sources, sourceExprAnalyzer, exprCtx);
returnValues = selectAnalysis.outputSymbols();
}
return new AnalyzedInsertStatement(subQueryRelation, tableInfo, targetColumns, ignoreDuplicateKeys, onDuplicateKeyAssignments, returnValues);
}
use of io.crate.analyze.relations.StatementAnalysisContext in project crate by crate.
the class CreateTableAsAnalyzer method analyze.
public AnalyzedCreateTableAs analyze(CreateTableAs createTableAs, ParamTypeHints paramTypeHints, CoordinatorTxnCtx txnCtx) {
RelationName relationName = RelationName.of(createTableAs.name().getName(), txnCtx.sessionContext().searchPath().currentSchema());
relationName.ensureValidForRelationCreation();
AnalyzedRelation analyzedSourceQuery = relationAnalyzer.analyze(createTableAs.query(), new StatementAnalysisContext(paramTypeHints, Operation.READ, txnCtx));
List<TableElement<Expression>> tableElements = Lists2.map(analyzedSourceQuery.outputs(), Symbols::toColumnDefinition);
CreateTable<Expression> createTable = new CreateTable<Expression>(createTableAs.name(), tableElements, Optional.empty(), Optional.empty(), GenericProperties.empty(), false);
// This is only a preliminary analysis to to have the source available for privilege checks.
// It will be analyzed again with the target columns from the target table once
// the table has been created.
AnalyzedRelation sourceRelation = relationAnalyzer.analyze(createTableAs.query(), new StatementAnalysisContext(paramTypeHints, Operation.READ, txnCtx));
// postponing the analysis of the insert statement, since the table has not been created yet.
Supplier<AnalyzedInsertStatement> postponedInsertAnalysis = () -> {
Insert<Expression> insert = new Insert<Expression>(createTableAs.name(), createTableAs.query(), Collections.emptyList(), Collections.emptyList(), Insert.DuplicateKeyContext.none());
return insertAnalyzer.analyze(insert, paramTypeHints, txnCtx);
};
return new AnalyzedCreateTableAs(createTableStatementAnalyzer.analyze(createTable, paramTypeHints, txnCtx), sourceRelation, postponedInsertAnalysis);
}
Aggregations