Search in sources :

Example 1 with FetchReference

use of io.crate.expression.symbol.FetchReference in project crate by crate.

the class ReaderBucketsTest method test_reader_bucket_accounts_memory_for_added_rows.

@Test
public void test_reader_bucket_accounts_memory_for_added_rows() throws Exception {
    var e = SQLExecutor.builder(clusterService).addTable("create table t1 (x text)").build();
    var t1 = e.resolveTableInfo("t1");
    var x = (Reference) e.asSymbol("x");
    var fetchSource = new FetchSource();
    fetchSource.addFetchIdColumn(new InputColumn(0, DataTypes.LONG));
    fetchSource.addRefToFetch(x);
    var fetchRows = FetchRows.create(CoordinatorTxnCtx.systemTransactionContext(), TestingHelpers.createNodeContext(), Map.of(t1.ident(), fetchSource), List.of(new FetchReference(new InputColumn(0, DataTypes.LONG), x), new InputColumn(1, DataTypes.INTEGER)));
    var bytesAccounted = new AtomicLong();
    var ramAccounting = new BlockBasedRamAccounting(bytes -> bytesAccounted.addAndGet(bytes), 1024);
    int readerId = 1;
    var readerBuckets = new ReaderBuckets(fetchRows, reader -> fetchSource, new EstimateCellsSize(List.of(DataTypes.LONG, DataTypes.INTEGER)), ramAccounting);
    long fetchId = FetchId.encode(readerId, 1);
    readerBuckets.add(new RowN(fetchId, 42));
    assertThat(bytesAccounted.get(), is(1024L));
    assertThat(readerBuckets.ramBytesUsed(), is(40L));
    IntObjectHashMap<Bucket> bucketsByReader = new IntObjectHashMap<>();
    bucketsByReader.put(readerId, new CollectionBucket(List.<Object[]>of(new Object[] { "I eat memory for breakfast" })));
    IntHashSet readerIds = new IntHashSet(2);
    readerIds.add(readerId);
    readerBuckets.generateToFetch(readerIds);
    try (var outputRows = readerBuckets.getOutputRows(List.of(bucketsByReader))) {
        assertThat(bytesAccounted.get(), is(1024L));
        assertThat(readerBuckets.ramBytesUsed(), is(136L));
    }
    assertThat("After outputRows are closed the readerBuckets are released", readerBuckets.ramBytesUsed(), is(0L));
}
Also used : FetchSource(io.crate.planner.node.fetch.FetchSource) EstimateCellsSize(io.crate.breaker.EstimateCellsSize) Reference(io.crate.metadata.Reference) FetchReference(io.crate.expression.symbol.FetchReference) IntObjectHashMap(com.carrotsearch.hppc.IntObjectHashMap) IntHashSet(com.carrotsearch.hppc.IntHashSet) AtomicLong(java.util.concurrent.atomic.AtomicLong) BlockBasedRamAccounting(io.crate.breaker.BlockBasedRamAccounting) RowN(io.crate.data.RowN) Bucket(io.crate.data.Bucket) CollectionBucket(io.crate.data.CollectionBucket) InputColumn(io.crate.expression.symbol.InputColumn) FetchReference(io.crate.expression.symbol.FetchReference) CollectionBucket(io.crate.data.CollectionBucket) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 2 with FetchReference

use of io.crate.expression.symbol.FetchReference in project crate by crate.

the class FetchRows method create.

public static FetchRows create(TransactionContext txnCtx, NodeContext nodeCtx, Map<RelationName, FetchSource> fetchSourceByTable, List<Symbol> outputSymbols) {
    IntArrayList fetchIdPositions = new IntArrayList();
    ArrayList<Object[]> nullRows = new ArrayList<>();
    IntObjectHashMap<UnsafeArrayRow> fetchedRows = new IntObjectHashMap<>();
    for (var fetchSource : fetchSourceByTable.values()) {
        Object[] nullRow = new Object[fetchSource.references().size()];
        for (InputColumn ic : fetchSource.fetchIdCols()) {
            fetchIdPositions.add(ic.index());
            nullRows.add(nullRow);
            fetchedRows.put(ic.index(), new UnsafeArrayRow());
        }
    }
    final UnsafeArrayRow inputRow = new UnsafeArrayRow();
    var visitor = new BaseImplementationSymbolVisitor<Void>(txnCtx, nodeCtx) {

        @Override
        public Input<?> visitInputColumn(final InputColumn inputColumn, final Void context) {
            final int idx = inputColumn.index();
            return () -> inputRow.get(idx);
        }

        @Override
        public Input<?> visitFetchReference(final FetchReference fetchReference, final Void context) {
            var ref = fetchReference.ref();
            UnsafeArrayRow row = fetchedRows.get(fetchReference.fetchId().index());
            int posInFetchedRow = fetchSourceByTable.get(ref.ident().tableIdent()).references().indexOf(ref);
            return () -> row.get(posInFetchedRow);
        }
    };
    List<Input<?>> outputExpressions = Lists2.map(outputSymbols, x -> x.accept(visitor, null));
    return new FetchRows(fetchIdPositions, outputExpressions, inputRow, fetchedRows, nullRows);
}
Also used : IntObjectHashMap(com.carrotsearch.hppc.IntObjectHashMap) ArrayList(java.util.ArrayList) IntArrayList(com.carrotsearch.hppc.IntArrayList) BaseImplementationSymbolVisitor(io.crate.expression.BaseImplementationSymbolVisitor) Input(io.crate.data.Input) UnsafeArrayRow(io.crate.data.UnsafeArrayRow) InputColumn(io.crate.expression.symbol.InputColumn) FetchReference(io.crate.expression.symbol.FetchReference) IntArrayList(com.carrotsearch.hppc.IntArrayList)

Example 3 with FetchReference

use of io.crate.expression.symbol.FetchReference in project crate by crate.

the class SymbolPrinterTest method testPrintFetchRefs.

@Test
public void testPrintFetchRefs() throws Exception {
    Symbol field = sqlExpressions.asSymbol("bar");
    assertThat(field, isReference("bar"));
    Reference ref = (Reference) field;
    FetchReference fetchRef = new FetchReference(new InputColumn(0, field.valueType()), ref);
    assertPrint(fetchRef, "FETCH(INPUT(0), doc.formatter.bar)");
}
Also used : Symbol(io.crate.expression.symbol.Symbol) DynamicReference(io.crate.expression.symbol.DynamicReference) FetchReference(io.crate.expression.symbol.FetchReference) Reference(io.crate.metadata.Reference) SymbolMatchers.isReference(io.crate.testing.SymbolMatchers.isReference) VoidReference(io.crate.expression.symbol.VoidReference) InputColumn(io.crate.expression.symbol.InputColumn) FetchReference(io.crate.expression.symbol.FetchReference) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 4 with FetchReference

use of io.crate.expression.symbol.FetchReference in project crate by crate.

the class FetchRowsTest method test_fetch_rows_can_map_inputs_and_buckets_to_outputs.

@Test
public void test_fetch_rows_can_map_inputs_and_buckets_to_outputs() throws Exception {
    var e = SQLExecutor.builder(clusterService).addTable("create table t1 (x text)").addTable("create table t2 (y text, z int)").build();
    var t1 = e.resolveTableInfo("t1");
    var x = (Reference) e.asSymbol("x");
    var fetchSource1 = new FetchSource();
    fetchSource1.addFetchIdColumn(new InputColumn(0, DataTypes.LONG));
    fetchSource1.addRefToFetch(x);
    var t2 = e.resolveTableInfo("t2");
    var y = (Reference) e.asSymbol("y");
    var fetchSource2 = new FetchSource();
    fetchSource2.addFetchIdColumn(new InputColumn(1, DataTypes.LONG));
    fetchSource2.addRefToFetch(y);
    var fetchSources = Map.of(t1.ident(), fetchSource1, t2.ident(), fetchSource2);
    var fetchRows = FetchRows.create(CoordinatorTxnCtx.systemTransactionContext(), createNodeContext(), fetchSources, List.of(new FetchReference(new InputColumn(0, DataTypes.LONG), x), new FetchReference(new InputColumn(1, DataTypes.LONG), y), new InputColumn(2, DataTypes.INTEGER)));
    long fetchIdRel1 = FetchId.encode(1, 1);
    long fetchIdRel2 = FetchId.encode(2, 1);
    var readerBuckets = new ReaderBuckets(fetchRows, reader -> reader == 1 ? fetchSource1 : fetchSource2, cells -> 0, RamAccounting.NO_ACCOUNTING);
    IntHashSet readerIds = new IntHashSet(2);
    readerIds.add(1);
    readerIds.add(2);
    readerBuckets.add(new RowN(fetchIdRel1, fetchIdRel2, 42));
    readerBuckets.generateToFetch(readerIds);
    IntObjectHashMap<Bucket> results = new IntObjectHashMap<>();
    results.put(1, new ArrayBucket($$($("Arthur"))));
    results.put(2, new ArrayBucket($$($("Trillian"))));
    var it = readerBuckets.getOutputRows(List.of(results));
    assertThat(it.hasNext(), is(true));
    var outputRow = it.next();
    assertThat(outputRow.get(0), is("Arthur"));
    assertThat(outputRow.get(1), is("Trillian"));
    assertThat(outputRow.get(2), is(42));
}
Also used : ArrayBucket(io.crate.data.ArrayBucket) FetchSource(io.crate.planner.node.fetch.FetchSource) Reference(io.crate.metadata.Reference) FetchReference(io.crate.expression.symbol.FetchReference) IntObjectHashMap(com.carrotsearch.hppc.IntObjectHashMap) IntHashSet(com.carrotsearch.hppc.IntHashSet) RowN(io.crate.data.RowN) Bucket(io.crate.data.Bucket) ArrayBucket(io.crate.data.ArrayBucket) InputColumn(io.crate.expression.symbol.InputColumn) FetchReference(io.crate.expression.symbol.FetchReference) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 5 with FetchReference

use of io.crate.expression.symbol.FetchReference in project crate by crate.

the class InputColumns method visitFetchStub.

@Override
public Symbol visitFetchStub(FetchStub fetchStub, SourceSymbols sourceSymbols) {
    FetchMarker fetchMarker = fetchStub.fetchMarker();
    InputColumn fetchId = sourceSymbols.inputs.get(fetchMarker);
    if (fetchId == null) {
        throw new IllegalArgumentException("Could not find fetchMarker " + fetchMarker + " in sources: " + sourceSymbols);
    }
    return new FetchReference(fetchId, fetchStub.ref());
}
Also used : FetchMarker(io.crate.expression.symbol.FetchMarker) InputColumn(io.crate.expression.symbol.InputColumn) FetchReference(io.crate.expression.symbol.FetchReference)

Aggregations

FetchReference (io.crate.expression.symbol.FetchReference)5 InputColumn (io.crate.expression.symbol.InputColumn)5 IntObjectHashMap (com.carrotsearch.hppc.IntObjectHashMap)3 Reference (io.crate.metadata.Reference)3 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)3 Test (org.junit.Test)3 IntHashSet (com.carrotsearch.hppc.IntHashSet)2 Bucket (io.crate.data.Bucket)2 RowN (io.crate.data.RowN)2 FetchSource (io.crate.planner.node.fetch.FetchSource)2 IntArrayList (com.carrotsearch.hppc.IntArrayList)1 BlockBasedRamAccounting (io.crate.breaker.BlockBasedRamAccounting)1 EstimateCellsSize (io.crate.breaker.EstimateCellsSize)1 ArrayBucket (io.crate.data.ArrayBucket)1 CollectionBucket (io.crate.data.CollectionBucket)1 Input (io.crate.data.Input)1 UnsafeArrayRow (io.crate.data.UnsafeArrayRow)1 BaseImplementationSymbolVisitor (io.crate.expression.BaseImplementationSymbolVisitor)1 DynamicReference (io.crate.expression.symbol.DynamicReference)1 FetchMarker (io.crate.expression.symbol.FetchMarker)1