Search in sources :

Example 1 with Assignment

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"));
}
Also used : PartitionName(io.crate.metadata.PartitionName) Assignment(io.crate.sql.tree.Assignment) DocTableInfo(io.crate.metadata.doc.DocTableInfo) StringLiteral(io.crate.sql.tree.StringLiteral) QualifiedName(io.crate.sql.tree.QualifiedName) Routing(io.crate.metadata.Routing) TableIdent(io.crate.metadata.TableIdent) List(java.util.List) QualifiedNameReference(io.crate.sql.tree.QualifiedNameReference) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 2 with Assignment

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);
}
Also used : ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Symbol(io.crate.analyze.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) ArrayList(java.util.ArrayList) Assignment(io.crate.sql.tree.Assignment) WhereClauseAnalyzer(io.crate.analyze.where.WhereClauseAnalyzer) TableInfo(io.crate.metadata.table.TableInfo)

Example 3 with Assignment

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;
}
Also used : Assignment(io.crate.sql.tree.Assignment) ValueNormalizer(io.crate.analyze.expressions.ValueNormalizer) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) DynamicReference(io.crate.analyze.symbol.DynamicReference) Symbol(io.crate.analyze.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer)

Aggregations

Assignment (io.crate.sql.tree.Assignment)3 ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)2 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)2 Symbol (io.crate.analyze.symbol.Symbol)2 ValueNormalizer (io.crate.analyze.expressions.ValueNormalizer)1 DynamicReference (io.crate.analyze.symbol.DynamicReference)1 WhereClauseAnalyzer (io.crate.analyze.where.WhereClauseAnalyzer)1 PartitionName (io.crate.metadata.PartitionName)1 Routing (io.crate.metadata.Routing)1 TableIdent (io.crate.metadata.TableIdent)1 DocTableInfo (io.crate.metadata.doc.DocTableInfo)1 TableInfo (io.crate.metadata.table.TableInfo)1 QualifiedName (io.crate.sql.tree.QualifiedName)1 QualifiedNameReference (io.crate.sql.tree.QualifiedNameReference)1 StringLiteral (io.crate.sql.tree.StringLiteral)1 CrateUnitTest (io.crate.test.integration.CrateUnitTest)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Test (org.junit.Test)1