use of io.crate.sql.tree.Assignment in project crate by crate.
the class PartitionPropertiesAnalyzerTest method testPartitionNameFromAssignmentWithBytesRef.
@Test
public void testPartitionNameFromAssignmentWithBytesRef() throws Exception {
DocTableInfo tableInfo = TestingTableInfo.builder(new TableIdent("doc", "users"), new Routing(ImmutableMap.<String, Map<String, List<Integer>>>of())).add("name", DataTypes.STRING, null, true).addPrimaryKey("name").build();
PartitionName partitionName = PartitionPropertiesAnalyzer.toPartitionName(tableInfo, Arrays.asList(new Assignment(new QualifiedNameReference(new QualifiedName("name")), new StringLiteral("foo"))), Row.EMPTY);
assertThat(partitionName.asIndexName(), is(".partitioned.users.0426crrf"));
}
use of io.crate.sql.tree.Assignment 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.sql.tree.Assignment in project crate by crate.
the class InsertFromSubQueryAnalyzer method processUpdateAssignments.
private Map<Reference, Symbol> processUpdateAssignments(DocTableRelation tableRelation, List<Reference> targetColumns, SessionContext sessionContext, ParameterContext parameterContext, TransactionContext transactionContext, FieldProvider fieldProvider, List<Assignment> assignments) {
ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, sessionContext, parameterContext, fieldProvider, null);
ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext();
EvaluatingNormalizer normalizer = new EvaluatingNormalizer(functions, RowGranularity.CLUSTER, ReplaceMode.COPY, null, tableRelation);
ValueNormalizer valuesNormalizer = new ValueNormalizer(schemas);
ValuesResolver valuesResolver = new ValuesResolver(tableRelation, targetColumns);
ValuesAwareExpressionAnalyzer valuesAwareExpressionAnalyzer = new ValuesAwareExpressionAnalyzer(functions, sessionContext, parameterContext, fieldProvider, valuesResolver);
Map<Reference, Symbol> updateAssignments = new HashMap<>(assignments.size());
for (Assignment assignment : assignments) {
Reference columnName = tableRelation.resolveField((Field) expressionAnalyzer.convert(assignment.columnName(), expressionAnalysisContext));
assert columnName != null : "columnName must not be null";
Symbol valueSymbol = normalizer.normalize(valuesAwareExpressionAnalyzer.convert(assignment.expression(), expressionAnalysisContext), transactionContext);
Symbol assignmentExpression = valuesNormalizer.normalizeInputForReference(valueSymbol, columnName);
updateAssignments.put(columnName, assignmentExpression);
}
return updateAssignments;
}
Aggregations