use of io.crate.analyze.relations.DocTableRelation 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.DocTableRelation in project crate by crate.
the class CopyAnalyzer method analyzeCopyTo.
AnalyzedCopyTo analyzeCopyTo(CopyTo<Expression> node, ParamTypeHints paramTypeHints, CoordinatorTxnCtx txnCtx) {
if (!node.directoryUri()) {
throw new UnsupportedOperationException("Using COPY TO without specifying a DIRECTORY is not supported");
}
TableInfo tableInfo = schemas.resolveTableInfo(node.table().getName(), Operation.COPY_TO, txnCtx.sessionContext().sessionUser(), txnCtx.sessionContext().searchPath());
Operation.blockedRaiseException(tableInfo, Operation.READ);
DocTableRelation tableRelation = new DocTableRelation((DocTableInfo) tableInfo);
EvaluatingNormalizer normalizer = new EvaluatingNormalizer(nodeCtx, RowGranularity.CLUSTER, null, tableRelation);
var exprCtx = new ExpressionAnalysisContext(txnCtx.sessionContext());
var expressionAnalyzer = new ExpressionAnalyzer(txnCtx, nodeCtx, paramTypeHints, new NameFieldProvider(tableRelation), null);
var exprAnalyzerWithFieldsAsString = new ExpressionAnalyzer(txnCtx, nodeCtx, paramTypeHints, FieldProvider.FIELDS_AS_LITERAL, null);
var uri = expressionAnalyzer.convert(node.targetUri(), exprCtx);
var table = node.table().map(x -> exprAnalyzerWithFieldsAsString.convert(x, exprCtx));
var properties = node.properties().map(x -> expressionAnalyzer.convert(x, exprCtx));
var columns = Lists2.map(node.columns(), c -> normalizer.normalize(expressionAnalyzer.convert(c, exprCtx), txnCtx));
var whereClause = node.whereClause().map(w -> normalizer.normalize(expressionAnalyzer.convert(w, exprCtx), txnCtx)).orElse(null);
return new AnalyzedCopyTo(tableInfo, table, normalizer.normalize(uri, txnCtx), properties, columns, whereClause);
}
use of io.crate.analyze.relations.DocTableRelation in project crate by crate.
the class UpdatePlanner method plan.
public static Plan plan(AnalyzedUpdateStatement update, PlannerContext plannerCtx, SubqueryPlanner subqueryPlanner) {
if (update.outputs() != null && !plannerCtx.clusterState().getNodes().getMinNodeVersion().onOrAfter(Version.V_4_2_0)) {
throw new UnsupportedFeatureException(RETURNING_VERSION_ERROR_MSG);
}
AbstractTableRelation<?> table = update.table();
Plan plan;
if (table instanceof DocTableRelation) {
DocTableRelation docTable = (DocTableRelation) table;
plan = plan(docTable, update.assignmentByTargetCol(), update.query(), plannerCtx, update.outputs());
} else {
plan = new Update((plannerContext, params, subQueryValues) -> sysUpdate(plannerContext, (TableRelation) table, update.assignmentByTargetCol(), update.query(), params, subQueryValues, update.outputs()));
}
Map<LogicalPlan, SelectSymbol> subQueries = subqueryPlanner.planSubQueries(update);
return MultiPhasePlan.createIfNeeded(plan, subQueries);
}
use of io.crate.analyze.relations.DocTableRelation in project crate by crate.
the class WhereClauseAnalyzerTest method analyzeSelectWhere.
private WhereClause analyzeSelectWhere(String stmt) {
AnalyzedRelation rel = e.analyze(stmt);
if (rel instanceof QueriedSelectRelation) {
QueriedSelectRelation queriedRelation = ((QueriedSelectRelation) rel);
if (queriedRelation.from().get(0) instanceof DocTableRelation) {
DocTableRelation docTableRelation = (DocTableRelation) queriedRelation.from().get(0);
WhereClauseOptimizer.DetailedQuery detailedQuery = WhereClauseOptimizer.optimize(new EvaluatingNormalizer(e.nodeCtx, RowGranularity.CLUSTER, null, docTableRelation), queriedRelation.where(), docTableRelation.tableInfo(), coordinatorTxnCtx, e.nodeCtx);
return detailedQuery.toBoundWhereClause(docTableRelation.tableInfo(), Row.EMPTY, SubQueryResults.EMPTY, coordinatorTxnCtx, e.nodeCtx);
}
return new WhereClause(queriedRelation.where());
} else {
return WhereClause.MATCH_ALL;
}
}
use of io.crate.analyze.relations.DocTableRelation in project crate by crate.
the class T3 method sources.
public static Map<RelationName, AnalyzedRelation> sources(Iterable<RelationName> relations, ClusterService clusterService) {
SQLExecutor.Builder executorBuilder = SQLExecutor.builder(clusterService);
relations.forEach(rn -> {
String tableDefinition = RELATION_DEFINITIONS.get(rn);
if (tableDefinition == null) {
throw new RuntimeException("Unknown relation " + rn);
}
try {
executorBuilder.addTable(tableDefinition);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
SQLExecutor executor = executorBuilder.build();
Schemas schemas = executor.schemas();
MapBuilder<RelationName, AnalyzedRelation> builder = MapBuilder.newMapBuilder();
for (RelationName relationName : relations) {
builder.put(relationName, new DocTableRelation(schemas.getTableInfo(relationName)));
}
return builder.immutableMap();
}
Aggregations