use of io.crate.analyze.expressions.ExpressionAnalyzer in project crate by crate.
the class DocIndexMetaData method initializeGeneratedExpressions.
private void initializeGeneratedExpressions() {
if (generatedColumnReferences.isEmpty()) {
return;
}
Collection<Reference> references = this.references.values();
TableReferenceResolver tableReferenceResolver = new TableReferenceResolver(references);
ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, SessionContext.SYSTEM_SESSION, ParamTypeHints.EMPTY, tableReferenceResolver, null);
ExpressionAnalysisContext context = new ExpressionAnalysisContext();
for (Reference reference : generatedColumnReferences) {
GeneratedReference generatedReference = (GeneratedReference) reference;
Expression expression = SqlParser.createExpression(generatedReference.formattedGeneratedExpression());
generatedReference.generatedExpression(expressionAnalyzer.convert(expression, context));
generatedReference.referencedReferences(ImmutableList.copyOf(tableReferenceResolver.references()));
tableReferenceResolver.references().clear();
}
}
use of io.crate.analyze.expressions.ExpressionAnalyzer in project crate by crate.
the class DeleteAnalyzer method analyze.
public AnalyzedStatement analyze(Delete node, Analysis analysis) {
int numNested = 1;
Function<ParameterExpression, Symbol> convertParamFunction = analysis.parameterContext();
StatementAnalysisContext statementAnalysisContext = new StatementAnalysisContext(analysis.sessionContext(), convertParamFunction, Operation.DELETE, analysis.transactionContext());
RelationAnalysisContext relationAnalysisContext = statementAnalysisContext.startRelation();
AnalyzedRelation analyzedRelation = relationAnalyzer.analyze(node.getRelation(), statementAnalysisContext);
assert analyzedRelation instanceof DocTableRelation : "analyzedRelation must be DocTableRelation";
DocTableRelation docTableRelation = (DocTableRelation) analyzedRelation;
EvaluatingNormalizer normalizer = new EvaluatingNormalizer(functions, RowGranularity.CLUSTER, ReplaceMode.MUTATE, null, docTableRelation);
DeleteAnalyzedStatement deleteAnalyzedStatement = new DeleteAnalyzedStatement(docTableRelation);
ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, analysis.sessionContext(), convertParamFunction, new FullQualifedNameFieldProvider(relationAnalysisContext.sources()), null);
ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext();
WhereClauseAnalyzer whereClauseAnalyzer = new WhereClauseAnalyzer(functions, deleteAnalyzedStatement.analyzedRelation());
if (analysis.parameterContext().hasBulkParams()) {
numNested = analysis.parameterContext().numBulkParams();
}
TransactionContext transactionContext = analysis.transactionContext();
for (int i = 0; i < numNested; i++) {
analysis.parameterContext().setBulkIdx(i);
Symbol query = expressionAnalyzer.generateQuerySymbol(node.getWhere(), expressionAnalysisContext);
WhereClause whereClause = new WhereClause(normalizer.normalize(query, transactionContext));
whereClause = validate(whereClauseAnalyzer.analyze(whereClause, transactionContext));
deleteAnalyzedStatement.whereClauses.add(whereClause);
}
statementAnalysisContext.endRelation();
return deleteAnalyzedStatement;
}
use of io.crate.analyze.expressions.ExpressionAnalyzer in project crate by crate.
the class UpdateAnalyzer method analyze.
public AnalyzedStatement analyze(Update node, Analysis analysis) {
StatementAnalysisContext statementAnalysisContext = new StatementAnalysisContext(analysis.sessionContext(), analysis.parameterContext(), Operation.UPDATE, analysis.transactionContext());
RelationAnalysisContext currentRelationContext = statementAnalysisContext.startRelation();
AnalyzedRelation analyzedRelation = relationAnalyzer.analyze(node.relation(), statementAnalysisContext);
FieldResolver fieldResolver = (FieldResolver) analyzedRelation;
EvaluatingNormalizer normalizer = new EvaluatingNormalizer(functions, RowGranularity.CLUSTER, ReplaceMode.MUTATE, null, fieldResolver);
FieldProvider columnFieldProvider = new NameFieldProvider(analyzedRelation);
ExpressionAnalyzer columnExpressionAnalyzer = new ExpressionAnalyzer(functions, analysis.sessionContext(), analysis.parameterContext(), columnFieldProvider, null);
columnExpressionAnalyzer.setResolveFieldsOperation(Operation.UPDATE);
assert Iterables.getOnlyElement(currentRelationContext.sources().values()) == analyzedRelation : "currentRelationContext.sources().values() must have one element and equal to analyzedRelation";
ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, analysis.sessionContext(), analysis.parameterContext(), new FullQualifedNameFieldProvider(currentRelationContext.sources()), null);
ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext();
int numNested = 1;
if (analysis.parameterContext().numBulkParams() > 0) {
numNested = analysis.parameterContext().numBulkParams();
}
WhereClauseAnalyzer whereClauseAnalyzer = null;
if (analyzedRelation instanceof DocTableRelation) {
whereClauseAnalyzer = new WhereClauseAnalyzer(functions, ((DocTableRelation) analyzedRelation));
}
TableInfo tableInfo = ((AbstractTableRelation) analyzedRelation).tableInfo();
List<UpdateAnalyzedStatement.NestedAnalyzedStatement> nestedAnalyzedStatements = new ArrayList<>(numNested);
for (int i = 0; i < numNested; i++) {
analysis.parameterContext().setBulkIdx(i);
Symbol querySymbol = expressionAnalyzer.generateQuerySymbol(node.whereClause(), expressionAnalysisContext);
WhereClause whereClause = new WhereClause(normalizer.normalize(querySymbol, analysis.transactionContext()));
if (whereClauseAnalyzer != null) {
whereClause = whereClauseAnalyzer.analyze(whereClause, analysis.transactionContext());
}
if (!whereClause.docKeys().isPresent() && Symbols.containsColumn(whereClause.query(), DocSysColumns.VERSION)) {
throw VERSION_SEARCH_EX;
}
UpdateAnalyzedStatement.NestedAnalyzedStatement nestedAnalyzedStatement = new UpdateAnalyzedStatement.NestedAnalyzedStatement(whereClause);
for (Assignment assignment : node.assignements()) {
analyzeAssignment(assignment, nestedAnalyzedStatement, tableInfo, normalizer, expressionAnalyzer, columnExpressionAnalyzer, expressionAnalysisContext, analysis.transactionContext());
}
nestedAnalyzedStatements.add(nestedAnalyzedStatement);
}
statementAnalysisContext.endRelation();
return new UpdateAnalyzedStatement(analyzedRelation, nestedAnalyzedStatements);
}
use of io.crate.analyze.expressions.ExpressionAnalyzer in project crate by crate.
the class InsertFromValuesAnalyzer method analyze.
public AnalyzedStatement analyze(InsertFromValues node, Analysis analysis) {
DocTableInfo tableInfo = schemas.getWritableTable(TableIdent.of(node.table(), analysis.sessionContext().defaultSchema()));
Operation.blockedRaiseException(tableInfo, Operation.INSERT);
DocTableRelation tableRelation = new DocTableRelation(tableInfo);
FieldProvider fieldProvider = new NameFieldProvider(tableRelation);
Function<ParameterExpression, Symbol> convertParamFunction = analysis.parameterContext();
ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, analysis.sessionContext(), convertParamFunction, fieldProvider, null);
ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext();
expressionAnalyzer.setResolveFieldsOperation(Operation.INSERT);
ValuesResolver valuesResolver = new ValuesResolver(tableRelation);
ExpressionAnalyzer valuesAwareExpressionAnalyzer = new ValuesAwareExpressionAnalyzer(functions, analysis.sessionContext(), convertParamFunction, fieldProvider, valuesResolver);
InsertFromValuesAnalyzedStatement statement = new InsertFromValuesAnalyzedStatement(tableInfo, analysis.parameterContext().numBulkParams());
handleInsertColumns(node, node.maxValuesLength(), statement);
Set<Reference> allReferencedReferences = new HashSet<>();
for (GeneratedReference reference : tableInfo.generatedColumns()) {
allReferencedReferences.addAll(reference.referencedReferences());
}
ReferenceToLiteralConverter.Context referenceToLiteralContext = new ReferenceToLiteralConverter.Context(statement.columns(), allReferencedReferences);
ValueNormalizer valuesNormalizer = new ValueNormalizer(schemas);
EvaluatingNormalizer normalizer = new EvaluatingNormalizer(functions, RowGranularity.CLUSTER, ReplaceMode.COPY, null, tableRelation);
analyzeColumns(statement.tableInfo(), statement.columns());
for (ValuesList valuesList : node.valuesLists()) {
analyzeValues(tableRelation, valuesNormalizer, normalizer, expressionAnalyzer, expressionAnalysisContext, analysis.transactionContext(), valuesResolver, valuesAwareExpressionAnalyzer, valuesList, node.onDuplicateKeyAssignments(), statement, analysis.parameterContext(), referenceToLiteralContext);
}
return statement;
}
use of io.crate.analyze.expressions.ExpressionAnalyzer in project crate by crate.
the class AnalyzedTableElements method validateGeneratedColumns.
private void validateGeneratedColumns(TableIdent tableIdent, Collection<? extends Reference> existingColumns, Functions functions, ParameterContext parameterContext, SessionContext sessionContext) {
List<Reference> tableReferences = new ArrayList<>();
for (AnalyzedColumnDefinition columnDefinition : columns) {
buildReference(tableIdent, columnDefinition, tableReferences);
}
tableReferences.addAll(existingColumns);
TableReferenceResolver tableReferenceResolver = new TableReferenceResolver(tableReferences);
ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, sessionContext, parameterContext, tableReferenceResolver, null);
SymbolPrinter printer = new SymbolPrinter(functions);
ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext();
for (AnalyzedColumnDefinition columnDefinition : columns) {
if (columnDefinition.generatedExpression() != null) {
processGeneratedExpression(expressionAnalyzer, printer, columnDefinition, expressionAnalysisContext);
}
}
}
Aggregations