Search in sources :

Example 1 with RowType

use of io.crate.types.RowType in project crate by crate.

the class PGTypesTest method test_can_retrieve_pg_type_for_record_array.

@Test
public void test_can_retrieve_pg_type_for_record_array() throws Exception {
    var pgType = PGTypes.get(new ArrayType<>(new RowType(List.of(DataTypes.STRING))));
    assertThat(pgType.oid(), is(PGArray.EMPTY_RECORD_ARRAY.oid()));
    byte[] bytes = pgType.encodeAsUTF8Text(List.of(new Row1("foobar")));
    assertThat(new String(bytes, StandardCharsets.UTF_8), is("{\"(foobar)\"}"));
}
Also used : Row1(io.crate.data.Row1) RowType(io.crate.types.RowType) BitString(io.crate.sql.tree.BitString) Test(org.junit.Test)

Example 2 with RowType

use of io.crate.types.RowType in project crate by crate.

the class SubscriptFunctionsTest method test_subscript_can_be_created_for_accessing_field_within_nested_record.

@Test
public void test_subscript_can_be_created_for_accessing_field_within_nested_record() throws Exception {
    var rowType = new RowType(List.of(new RowType(List.of(DataTypes.INTEGER), List.of("y"))), List.of("x"));
    var rowExpression = Literal.of(rowType, new Row1(new Row1(10)));
    var subscript = SubscriptFunctions.tryCreateSubscript(rowExpression, List.of("x", "y"));
    assertThat(subscript, isFunction("_subscript_record", isFunction("_subscript_record"), isLiteral("y")));
}
Also used : Row1(io.crate.data.Row1) RowType(io.crate.types.RowType) Test(org.junit.Test)

Example 3 with RowType

use of io.crate.types.RowType in project crate by crate.

the class TableFunctionCollectSource method getIterator.

@Override
@SuppressWarnings("unchecked")
public CompletableFuture<BatchIterator<Row>> getIterator(TransactionContext txnCtx, CollectPhase collectPhase, CollectTask collectTask, boolean supportMoveToStart) {
    TableFunctionCollectPhase phase = (TableFunctionCollectPhase) collectPhase;
    TableFunctionImplementation<?> functionImplementation = phase.functionImplementation();
    RowType rowType = functionImplementation.returnType();
    // noinspection unchecked  Only literals can be passed to table functions. Anything else is invalid SQL
    List<Input<?>> inputs = (List<Input<?>>) (List<?>) phase.functionArguments();
    List<Input<?>> topLevelInputs = new ArrayList<>(phase.toCollect().size());
    List<String> columns = rowType.fieldNames();
    InputFactory.Context<InputCollectExpression> ctx = inputFactory.ctxForRefs(txnCtx, ref -> {
        for (int i = 0; i < columns.size(); i++) {
            String column = columns.get(i);
            if (ref.column().isTopLevel() && ref.column().name().equals(column)) {
                return new InputCollectExpression(i);
            }
        }
        throw new IllegalStateException("Column `" + ref + "` not found in " + functionImplementation.signature().getName().displayName());
    });
    for (Symbol symbol : phase.toCollect()) {
        topLevelInputs.add(ctx.add(symbol));
    }
    var inputRow = new ValueAndInputRow<>(topLevelInputs, ctx.expressions());
    Input<Boolean> condition = (Input<Boolean>) ctx.add(phase.where());
    Iterable<Row> rows = () -> StreamSupport.stream(functionImplementation.evaluate(txnCtx, nodeCtx, inputs.toArray(new Input[0])).spliterator(), false).map(inputRow).filter(InputCondition.asPredicate(condition)).iterator();
    return CompletableFuture.completedFuture(InMemoryBatchIterator.of(rows, SentinelRow.SENTINEL, functionImplementation.hasLazyResultSet()));
}
Also used : InputFactory(io.crate.expression.InputFactory) Symbol(io.crate.expression.symbol.Symbol) ValueAndInputRow(io.crate.execution.engine.collect.ValueAndInputRow) ArrayList(java.util.ArrayList) RowType(io.crate.types.RowType) Input(io.crate.data.Input) InputCollectExpression(io.crate.execution.engine.collect.InputCollectExpression) ArrayList(java.util.ArrayList) List(java.util.List) Row(io.crate.data.Row) ValueAndInputRow(io.crate.execution.engine.collect.ValueAndInputRow) SentinelRow(io.crate.data.SentinelRow) TableFunctionCollectPhase(io.crate.execution.dsl.phases.TableFunctionCollectPhase)

Example 4 with RowType

use of io.crate.types.RowType in project crate by crate.

the class SizeEstimatorFactoryTest method test_estimate_size_of_record.

@Test
public void test_estimate_size_of_record() throws Exception {
    var estimator = SizeEstimatorFactory.create(new RowType(List.of(DataTypes.LONG, DataTypes.INTEGER)));
    assertThat(estimator.estimateSize(new RowN(20L, 10)), is(40L));
}
Also used : RowN(io.crate.data.RowN) RowType(io.crate.types.RowType) Test(org.junit.Test)

Aggregations

RowType (io.crate.types.RowType)4 Test (org.junit.Test)3 Row1 (io.crate.data.Row1)2 Input (io.crate.data.Input)1 Row (io.crate.data.Row)1 RowN (io.crate.data.RowN)1 SentinelRow (io.crate.data.SentinelRow)1 TableFunctionCollectPhase (io.crate.execution.dsl.phases.TableFunctionCollectPhase)1 InputCollectExpression (io.crate.execution.engine.collect.InputCollectExpression)1 ValueAndInputRow (io.crate.execution.engine.collect.ValueAndInputRow)1 InputFactory (io.crate.expression.InputFactory)1 Symbol (io.crate.expression.symbol.Symbol)1 BitString (io.crate.sql.tree.BitString)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1