Search in sources :

Example 66 with Reference

use of io.crate.metadata.Reference in project crate by crate.

the class LogFunctionTest method testNormalizeReference.

@Test
public void testNormalizeReference() throws Exception {
    Reference dB = createReference("dB", DataTypes.DOUBLE);
    LogFunction log10 = getFunction(LogFunction.NAME, DataTypes.DOUBLE);
    Function function = new Function(log10.info(), Arrays.<Symbol>asList(dB));
    Function normalized = (Function) log10.normalizeSymbol(function, transactionContext);
    assertThat(normalized, Matchers.sameInstance(function));
    LogFunction ln = getFunction(LogFunction.LnFunction.NAME, DataTypes.DOUBLE);
    function = new Function(ln.info(), Arrays.<Symbol>asList(dB));
    normalized = (Function) ln.normalizeSymbol(function, transactionContext);
    assertThat(normalized, Matchers.sameInstance(function));
    LogFunction logBase = getFunction(LogFunction.NAME, DataTypes.DOUBLE, DataTypes.LONG);
    function = new Function(logBase.info(), Arrays.<Symbol>asList(dB, Literal.of(10L)));
    normalized = (Function) logBase.normalizeSymbol(function, transactionContext);
    assertThat(normalized, Matchers.sameInstance(function));
    Reference base = createReference("base", DataTypes.INTEGER);
    function = new Function(logBase.info(), Arrays.<Symbol>asList(dB, base));
    normalized = (Function) logBase.normalizeSymbol(function, transactionContext);
    assertThat(normalized, Matchers.sameInstance(function));
}
Also used : Function(io.crate.analyze.symbol.Function) TestingHelpers.createReference(io.crate.testing.TestingHelpers.createReference) Reference(io.crate.metadata.Reference) Symbol(io.crate.analyze.symbol.Symbol) AbstractScalarFunctionsTest(io.crate.operation.scalar.AbstractScalarFunctionsTest) Test(org.junit.Test)

Example 67 with Reference

use of io.crate.metadata.Reference in project crate by crate.

the class FetchContext method innerPrepare.

@Override
public void innerPrepare() {
    HashMap<String, TableIdent> index2TableIdent = new HashMap<>();
    for (Map.Entry<TableIdent, Collection<String>> entry : phase.tableIndices().asMap().entrySet()) {
        for (String indexName : entry.getValue()) {
            index2TableIdent.put(indexName, entry.getKey());
        }
    }
    Set<TableIdent> tablesWithFetchRefs = new HashSet<>();
    for (Reference reference : phase.fetchRefs()) {
        tablesWithFetchRefs.add(reference.ident().tableIdent());
    }
    for (Routing routing : routingIterable) {
        Map<String, Map<String, List<Integer>>> locations = routing.locations();
        Map<String, List<Integer>> indexShards = locations.get(localNodeId);
        for (Map.Entry<String, List<Integer>> indexShardsEntry : indexShards.entrySet()) {
            String index = indexShardsEntry.getKey();
            Integer base = phase.bases().get(index);
            if (base == null) {
                continue;
            }
            TableIdent ident = index2TableIdent.get(index);
            assert ident != null : "no tableIdent found for index " + index;
            tableIdents.put(base, ident);
            toFetch.put(ident, new ArrayList<Reference>());
            for (Integer shard : indexShardsEntry.getValue()) {
                ShardId shardId = new ShardId(index, shard);
                int readerId = base + shardId.id();
                SharedShardContext shardContext = shardContexts.get(readerId);
                if (shardContext == null) {
                    shardContext = sharedShardContexts.createContext(shardId, readerId);
                    shardContexts.put(readerId, shardContext);
                    if (tablesWithFetchRefs.contains(ident)) {
                        try {
                            searchers.put(readerId, shardContext.acquireSearcher());
                        } catch (IndexNotFoundException e) {
                            if (!PartitionName.isPartition(index)) {
                                throw e;
                            }
                        }
                    }
                }
            }
        }
    }
    for (Reference reference : phase.fetchRefs()) {
        Collection<Reference> references = toFetch.get(reference.ident().tableIdent());
        if (references != null) {
            references.add(reference);
        }
    }
}
Also used : IntObjectHashMap(com.carrotsearch.hppc.IntObjectHashMap) Reference(io.crate.metadata.Reference) Routing(io.crate.metadata.Routing) TableIdent(io.crate.metadata.TableIdent) ShardId(org.elasticsearch.index.shard.ShardId) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IntObjectHashMap(com.carrotsearch.hppc.IntObjectHashMap) SharedShardContext(io.crate.action.job.SharedShardContext)

Example 68 with Reference

use of io.crate.metadata.Reference 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 69 with Reference

use of io.crate.metadata.Reference in project crate by crate.

the class UpdatePlannerTest method testUpdateByQueryPlan.

@Test
public void testUpdateByQueryPlan() throws Exception {
    Upsert plan = e.plan("update users set name='Vogon lyric fan'");
    assertThat(plan.nodes().size(), is(1));
    Merge merge = (Merge) plan.nodes().get(0);
    Collect collect = (Collect) merge.subPlan();
    RoutedCollectPhase collectPhase = ((RoutedCollectPhase) collect.collectPhase());
    assertThat(collectPhase.routing(), is(TableDefinitions.SHARD_ROUTING));
    assertFalse(collectPhase.whereClause().noMatch());
    assertFalse(collectPhase.whereClause().hasQuery());
    assertThat(collectPhase.projections().size(), is(1));
    assertThat(collectPhase.projections().get(0), instanceOf(UpdateProjection.class));
    assertThat(collectPhase.toCollect().size(), is(1));
    assertThat(collectPhase.toCollect().get(0), instanceOf(Reference.class));
    assertThat(((Reference) collectPhase.toCollect().get(0)).ident().columnIdent().fqn(), is("_id"));
    UpdateProjection updateProjection = (UpdateProjection) collectPhase.projections().get(0);
    assertThat(updateProjection.uidSymbol(), instanceOf(InputColumn.class));
    assertThat(updateProjection.assignmentsColumns()[0], is("name"));
    Symbol symbol = updateProjection.assignments()[0];
    assertThat(symbol, isLiteral("Vogon lyric fan", DataTypes.STRING));
    MergePhase mergePhase = merge.mergePhase();
    assertThat(mergePhase.projections().size(), is(1));
    assertThat(mergePhase.projections().get(0), instanceOf(MergeCountProjection.class));
    assertThat(mergePhase.outputTypes().size(), is(1));
}
Also used : MergeCountProjection(io.crate.planner.projection.MergeCountProjection) Upsert(io.crate.planner.node.dml.Upsert) MergePhase(io.crate.planner.node.dql.MergePhase) Collect(io.crate.planner.node.dql.Collect) Reference(io.crate.metadata.Reference) InputColumn(io.crate.analyze.symbol.InputColumn) Symbol(io.crate.analyze.symbol.Symbol) UpdateProjection(io.crate.planner.projection.UpdateProjection) RoutedCollectPhase(io.crate.planner.node.dql.RoutedCollectPhase) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 70 with Reference

use of io.crate.metadata.Reference in project crate by crate.

the class FetchPhaseTest method testStreaming.

@Test
public void testStreaming() throws Exception {
    TableIdent t1 = new TableIdent(null, "t1");
    TreeMap<String, Integer> bases = new TreeMap<String, Integer>();
    bases.put(t1.name(), 0);
    bases.put("i2", 1);
    Multimap<TableIdent, String> tableIndices = HashMultimap.create();
    tableIndices.put(t1, t1.name());
    tableIndices.put(new TableIdent(null, "i2"), "i2_s1");
    tableIndices.put(new TableIdent(null, "i2"), "i2_s2");
    ReferenceIdent nameIdent = new ReferenceIdent(t1, "name");
    Reference name = new Reference(nameIdent, RowGranularity.DOC, DataTypes.STRING);
    FetchPhase orig = new FetchPhase(1, ImmutableSet.<String>of("node1", "node2"), bases, tableIndices, ImmutableList.of(name));
    BytesStreamOutput out = new BytesStreamOutput();
    ExecutionPhases.toStream(out, orig);
    StreamInput in = StreamInput.wrap(out.bytes());
    FetchPhase streamed = (FetchPhase) ExecutionPhases.fromStream(in);
    assertThat(orig.phaseId(), is(streamed.phaseId()));
    assertThat(orig.nodeIds(), is(streamed.nodeIds()));
    assertThat(orig.fetchRefs(), is(streamed.fetchRefs()));
    assertThat(orig.bases(), is(streamed.bases()));
    assertThat(orig.tableIndices(), is(streamed.tableIndices()));
}
Also used : Reference(io.crate.metadata.Reference) StreamInput(org.elasticsearch.common.io.stream.StreamInput) TableIdent(io.crate.metadata.TableIdent) TreeMap(java.util.TreeMap) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) ReferenceIdent(io.crate.metadata.ReferenceIdent) Test(org.junit.Test)

Aggregations

Reference (io.crate.metadata.Reference)84 Test (org.junit.Test)57 CrateUnitTest (io.crate.test.integration.CrateUnitTest)55 TestingHelpers.mapToSortedString (io.crate.testing.TestingHelpers.mapToSortedString)27 HashMap (java.util.HashMap)20 Map (java.util.Map)20 NestedObjectExpression (io.crate.operation.reference.NestedObjectExpression)17 ColumnIdent (io.crate.metadata.ColumnIdent)15 Symbol (io.crate.analyze.symbol.Symbol)14 BytesRef (org.apache.lucene.util.BytesRef)11 TableIdent (io.crate.metadata.TableIdent)9 ReferenceIdent (io.crate.metadata.ReferenceIdent)8 InputColumn (io.crate.analyze.symbol.InputColumn)6 RoutedCollectPhase (io.crate.planner.node.dql.RoutedCollectPhase)6 ShardId (org.elasticsearch.index.shard.ShardId)5 Routing (io.crate.metadata.Routing)4 UUID (java.util.UUID)4 OrderBy (io.crate.analyze.OrderBy)3 ShardResponse (io.crate.executor.transport.ShardResponse)3 ShardUpsertRequest (io.crate.executor.transport.ShardUpsertRequest)3