use of io.crate.analyze.relations.FullQualifiedNameFieldProvider 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.FullQualifiedNameFieldProvider 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.FullQualifiedNameFieldProvider in project crate by crate.
the class ExpressionAnalyzerTest method testInSelfJoinCaseFunctionsThatLookTheSameMustNotReuseFunctionAllocation.
@Test
public void testInSelfJoinCaseFunctionsThatLookTheSameMustNotReuseFunctionAllocation() throws Exception {
TableInfo t1 = executor.resolveTableInfo("t1");
TableRelation relation = new TableRelation(t1);
RelationName a1 = new RelationName(null, "a1");
RelationName a2 = new RelationName(null, "a2");
Map<RelationName, AnalyzedRelation> sources = Map.of(a1, new AliasedAnalyzedRelation(relation, a1), a2, new AliasedAnalyzedRelation(relation, a2));
SessionContext sessionContext = SessionContext.systemSessionContext();
CoordinatorTxnCtx coordinatorTxnCtx = new CoordinatorTxnCtx(sessionContext);
ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(coordinatorTxnCtx, expressions.nodeCtx, paramTypeHints, new FullQualifiedNameFieldProvider(sources, ParentRelations.NO_PARENTS, sessionContext.searchPath().currentSchema()), null);
Function andFunction = (Function) expressionAnalyzer.convert(SqlParser.createExpression("not a1.x = 1 and not a2.x = 1"), context);
ScopedSymbol t1Id = ((ScopedSymbol) ((Function) ((Function) andFunction.arguments().get(0)).arguments().get(0)).arguments().get(0));
ScopedSymbol t2Id = ((ScopedSymbol) ((Function) ((Function) andFunction.arguments().get(1)).arguments().get(0)).arguments().get(0));
assertThat(t1Id.relation(), is(not(t2Id.relation())));
}
use of io.crate.analyze.relations.FullQualifiedNameFieldProvider 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.FullQualifiedNameFieldProvider 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);
}
Aggregations