Search in sources :

Example 6 with DocTableRelation

use of io.crate.analyze.relations.DocTableRelation in project crate by crate.

the class MultiSourceFetchPushDown method process.

void process() {
    remainingOutputs = statement.querySpec().outputs();
    statement.querySpec().outputs(new ArrayList<>());
    HashMap<Symbol, Symbol> topLevelOutputMap = new HashMap<>(statement.canBeFetched().size());
    HashMap<Symbol, Symbol> mssOutputMap = new HashMap<>(statement.querySpec().outputs().size() + 2);
    ArrayList<Symbol> mssOutputs = new ArrayList<>(statement.sources().size() + statement.requiredForQuery().size());
    for (Map.Entry<QualifiedName, RelationSource> entry : statement.sources().entrySet()) {
        RelationSource source = entry.getValue();
        if (!(source.relation() instanceof DocTableRelation)) {
            int index = 0;
            for (Symbol output : source.querySpec().outputs()) {
                RelationColumn rc = new RelationColumn(entry.getKey(), index++, output.valueType());
                mssOutputs.add(rc);
                mssOutputMap.put(output, rc);
                topLevelOutputMap.put(output, new InputColumn(mssOutputs.size() - 1, output.valueType()));
            }
            continue;
        }
        DocTableRelation rel = (DocTableRelation) source.relation();
        HashSet<Field> canBeFetched = filterByRelation(statement.canBeFetched(), rel);
        if (!canBeFetched.isEmpty()) {
            RelationColumn fetchIdColumn = new RelationColumn(entry.getKey(), 0, DataTypes.LONG);
            mssOutputs.add(fetchIdColumn);
            InputColumn fetchIdInput = new InputColumn(mssOutputs.size() - 1);
            ArrayList<Symbol> qtOutputs = new ArrayList<>(source.querySpec().outputs().size() - canBeFetched.size() + 1);
            Reference fetchId = rel.tableInfo().getReference(DocSysColumns.FETCHID);
            qtOutputs.add(fetchId);
            for (Symbol output : source.querySpec().outputs()) {
                if (!canBeFetched.contains(output)) {
                    qtOutputs.add(output);
                    RelationColumn rc = new RelationColumn(entry.getKey(), qtOutputs.size() - 1, output.valueType());
                    mssOutputs.add(rc);
                    mssOutputMap.put(output, rc);
                    topLevelOutputMap.put(output, new InputColumn(mssOutputs.size() - 1, output.valueType()));
                }
            }
            for (Field field : canBeFetched) {
                FetchReference fr = new FetchReference(fetchIdInput, DocReferenceConverter.toSourceLookup(rel.resolveField(field)));
                allocateFetchedReference(fr, rel);
                topLevelOutputMap.put(field, fr);
            }
            source.querySpec().outputs(qtOutputs);
        } else {
            int index = 0;
            for (Symbol output : source.querySpec().outputs()) {
                RelationColumn rc = new RelationColumn(entry.getKey(), index++, output.valueType());
                mssOutputs.add(rc);
                mssOutputMap.put(output, rc);
                topLevelOutputMap.put(output, new InputColumn(mssOutputs.size() - 1, output.valueType()));
            }
        }
    }
    statement.querySpec().outputs(mssOutputs);
    MappingSymbolVisitor.inPlace().processInplace(remainingOutputs, topLevelOutputMap);
    if (statement.querySpec().orderBy().isPresent()) {
        MappingSymbolVisitor.inPlace().processInplace(statement.querySpec().orderBy().get().orderBySymbols(), mssOutputMap);
    }
}
Also used : RelationSource(io.crate.analyze.RelationSource) Reference(io.crate.metadata.Reference) QualifiedName(io.crate.sql.tree.QualifiedName) DocTableRelation(io.crate.analyze.relations.DocTableRelation)

Example 7 with DocTableRelation

use of io.crate.analyze.relations.DocTableRelation in project crate by crate.

the class UpdateAnalyzerTest method testUpdateAssignments.

@Test
public void testUpdateAssignments() throws Exception {
    UpdateAnalyzedStatement statement = analyze("update users set name='Trillian'");
    UpdateAnalyzedStatement.NestedAnalyzedStatement statement1 = statement.nestedStatements().get(0);
    assertThat(statement1.assignments().size(), is(1));
    assertThat(((DocTableRelation) statement.sourceRelation()).tableInfo().ident(), is(new TableIdent(Schemas.DEFAULT_SCHEMA_NAME, "users")));
    Reference ref = statement1.assignments().keySet().iterator().next();
    assertThat(ref.ident().tableIdent().name(), is("users"));
    assertThat(ref.ident().columnIdent().name(), is("name"));
    assertTrue(statement1.assignments().containsKey(ref));
    Symbol value = statement1.assignments().entrySet().iterator().next().getValue();
    assertThat(value, isLiteral("Trillian"));
}
Also used : DynamicReference(io.crate.analyze.symbol.DynamicReference) Symbol(io.crate.analyze.symbol.Symbol) DocTableRelation(io.crate.analyze.relations.DocTableRelation) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 8 with DocTableRelation

use of io.crate.analyze.relations.DocTableRelation in project crate by crate.

the class DeleteStatementPlanner method planDelete.

public static Plan planDelete(DeleteAnalyzedStatement analyzedStatement, Planner.Context context) {
    DocTableRelation tableRelation = analyzedStatement.analyzedRelation();
    List<WhereClause> whereClauses = new ArrayList<>(analyzedStatement.whereClauses().size());
    List<DocKeys.DocKey> docKeys = new ArrayList<>(analyzedStatement.whereClauses().size());
    Map<Integer, Integer> itemToBulkIdx = new HashMap<>();
    int bulkIdx = -1;
    int itemIdx = 0;
    for (WhereClause whereClause : analyzedStatement.whereClauses()) {
        bulkIdx++;
        if (whereClause.noMatch()) {
            continue;
        }
        if (whereClause.docKeys().isPresent() && whereClause.docKeys().get().size() == 1) {
            DocKeys.DocKey docKey = whereClause.docKeys().get().getOnlyKey();
            if (docKey.id() != null) {
                docKeys.add(docKey);
                itemToBulkIdx.put(itemIdx, bulkIdx);
                itemIdx++;
            }
        } else if (!whereClause.noMatch()) {
            whereClauses.add(whereClause);
        }
    }
    if (!docKeys.isEmpty()) {
        return new ESDelete(context.jobId(), context.nextExecutionPhaseId(), tableRelation.tableInfo(), docKeys, itemToBulkIdx, analyzedStatement.whereClauses().size());
    } else if (!whereClauses.isEmpty()) {
        return deleteByQuery(tableRelation.tableInfo(), whereClauses, context);
    }
    return new NoopPlan(context.jobId());
}
Also used : NoopPlan(io.crate.planner.NoopPlan) DocKeys(io.crate.analyze.where.DocKeys) WhereClause(io.crate.analyze.WhereClause) ESDelete(io.crate.planner.node.dml.ESDelete) DocTableRelation(io.crate.analyze.relations.DocTableRelation)

Example 9 with DocTableRelation

use of io.crate.analyze.relations.DocTableRelation in project crate by crate.

the class DeleteAnalyzerTest method testDeleteWhere.

@Test
public void testDeleteWhere() throws Exception {
    DeleteAnalyzedStatement statement = e.analyze("delete from users where name='Trillian'");
    DocTableRelation tableRelation = statement.analyzedRelation;
    TableInfo tableInfo = tableRelation.tableInfo();
    assertThat(USER_TABLE_IDENT, equalTo(tableInfo.ident()));
    assertThat(tableInfo.rowGranularity(), is(RowGranularity.DOC));
    Function whereClause = (Function) statement.whereClauses.get(0).query();
    assertEquals(EqOperator.NAME, whereClause.info().ident().name());
    assertFalse(whereClause.info().type() == FunctionInfo.Type.AGGREGATE);
    assertThat(whereClause.arguments().get(0), isReference("name"));
    assertThat(whereClause.arguments().get(1), isLiteral("Trillian"));
}
Also used : Function(io.crate.analyze.symbol.Function) DocTableRelation(io.crate.analyze.relations.DocTableRelation) TableInfo(io.crate.metadata.table.TableInfo) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Aggregations

DocTableRelation (io.crate.analyze.relations.DocTableRelation)9 Symbol (io.crate.analyze.symbol.Symbol)4 DocTableInfo (io.crate.metadata.doc.DocTableInfo)4 ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)3 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)3 CrateUnitTest (io.crate.test.integration.CrateUnitTest)3 Test (org.junit.Test)3 TableInfo (io.crate.metadata.table.TableInfo)2 QualifiedName (io.crate.sql.tree.QualifiedName)2 Settings (org.elasticsearch.common.settings.Settings)2 DeleteAnalyzedStatement (io.crate.analyze.DeleteAnalyzedStatement)1 RelationSource (io.crate.analyze.RelationSource)1 WhereClause (io.crate.analyze.WhereClause)1 ValueNormalizer (io.crate.analyze.expressions.ValueNormalizer)1 FieldProvider (io.crate.analyze.relations.FieldProvider)1 NameFieldProvider (io.crate.analyze.relations.NameFieldProvider)1 QueriedDocTable (io.crate.analyze.relations.QueriedDocTable)1 DynamicReference (io.crate.analyze.symbol.DynamicReference)1 Function (io.crate.analyze.symbol.Function)1 DocKeys (io.crate.analyze.where.DocKeys)1