Search in sources :

Example 71 with DataType

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

the class PlannerTest method testAllocateRouting.

@Test
public void testAllocateRouting() throws Exception {
    TableIdent custom = new TableIdent("custom", "t1");
    TableInfo tableInfo1 = TestingTableInfo.builder(custom, shardRouting("t1")).add("id", DataTypes.INTEGER, null).build();
    TableInfo tableInfo2 = TestingTableInfo.builder(custom, shardRoutingForReplicas("t1")).add("id", DataTypes.INTEGER, null).build();
    Planner.Context plannerContext = new Planner.Context(e.planner, clusterService, UUID.randomUUID(), null, normalizer, new TransactionContext(SessionContext.SYSTEM_SESSION), 0, 0);
    WhereClause whereClause = new WhereClause(new Function(new FunctionInfo(new FunctionIdent(EqOperator.NAME, Arrays.<DataType>asList(DataTypes.INTEGER, DataTypes.INTEGER)), DataTypes.BOOLEAN), Arrays.asList(tableInfo1.getReference(new ColumnIdent("id")), Literal.of(2))));
    plannerContext.allocateRouting(tableInfo1, WhereClause.MATCH_ALL, null);
    plannerContext.allocateRouting(tableInfo2, whereClause, null);
    // 2 routing allocations with different where clause must result in 2 allocated routings
    java.lang.reflect.Field tableRoutings = Planner.Context.class.getDeclaredField("tableRoutings");
    tableRoutings.setAccessible(true);
    Multimap<TableIdent, Planner.TableRouting> routing = (Multimap<TableIdent, Planner.TableRouting>) tableRoutings.get(plannerContext);
    assertThat(routing.size(), is(2));
    // The routings must be the same after merging the locations
    Iterator<Planner.TableRouting> iterator = routing.values().iterator();
    Routing routing1 = iterator.next().routing;
    Routing routing2 = iterator.next().routing;
    assertThat(routing1, is(routing2));
}
Also used : SessionContext(io.crate.action.sql.SessionContext) WhereClause(io.crate.analyze.WhereClause) TableDefinitions.shardRouting(io.crate.analyze.TableDefinitions.shardRouting) Function(io.crate.analyze.symbol.Function) Multimap(com.google.common.collect.Multimap) DataType(io.crate.types.DataType) TableInfo(io.crate.metadata.table.TableInfo) TestingTableInfo(io.crate.metadata.table.TestingTableInfo) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 72 with DataType

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

the class Literal method typeMatchesValue.

private static boolean typeMatchesValue(DataType type, Object value) {
    if (value == null) {
        return true;
    }
    if (type.equals(DataTypes.STRING) && (value instanceof BytesRef || value instanceof String)) {
        return true;
    }
    if (type instanceof ArrayType) {
        DataType innerType = ((ArrayType) type).innerType();
        while (innerType instanceof ArrayType && value.getClass().isArray()) {
            type = innerType;
            innerType = ((ArrayType) innerType).innerType();
            value = ((Object[]) value)[0];
        }
        if (innerType.equals(DataTypes.STRING)) {
            for (Object o : ((Object[]) value)) {
                if (o != null && !(o instanceof String || o instanceof BytesRef)) {
                    return false;
                }
            }
            return true;
        } else {
            return Arrays.equals((Object[]) value, ((ArrayType) type).value(value));
        }
    }
    // types like GeoPoint are represented as arrays
    if (value.getClass().isArray() && Arrays.equals((Object[]) value, (Object[]) type.value(value))) {
        return true;
    }
    return type.value(value).equals(value);
}
Also used : ArrayType(io.crate.types.ArrayType) DataType(io.crate.types.DataType) BytesRef(org.apache.lucene.util.BytesRef)

Example 73 with DataType

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

the class ConnectionContext method handleParseMessage.

/**
     * Parse Message
     * header:
     * | 'P' | int32 len
     * <p>
     * body:
     * | string statementName | string query | int16 numParamTypes |
     * foreach param:
     * | int32 type_oid (zero = unspecified)
     */
private void handleParseMessage(ChannelBuffer buffer, final Channel channel) {
    String statementName = readCString(buffer);
    final String query = readCString(buffer);
    short numParams = buffer.readShort();
    List<DataType> paramTypes = new ArrayList<>(numParams);
    for (int i = 0; i < numParams; i++) {
        int oid = buffer.readInt();
        DataType dataType = PGTypes.fromOID(oid);
        if (dataType == null) {
            throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Can't map PGType with oid=%d to Crate type", oid));
        }
        paramTypes.add(dataType);
    }
    session.parse(statementName, query, paramTypes);
    Messages.sendParseComplete(channel);
}
Also used : DataType(io.crate.types.DataType)

Example 74 with DataType

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

the class ConnectionContext method handleBindMessage.

/**
     * Bind Message
     * Header:
     * | 'B' | int32 len
     * <p>
     * Body:
     * <pre>
     * | string portalName | string statementName
     * | int16 numFormatCodes
     *      foreach
     *      | int16 formatCode
     * | int16 numParams
     *      foreach
     *      | int32 valueLength
     *      | byteN value
     * | int16 numResultColumnFormatCodes
     *      foreach
     *      | int16 formatCode
     * </pre>
     */
private void handleBindMessage(ChannelBuffer buffer, Channel channel) {
    String portalName = readCString(buffer);
    String statementName = readCString(buffer);
    FormatCodes.FormatCode[] formatCodes = FormatCodes.fromBuffer(buffer);
    short numParams = buffer.readShort();
    List<Object> params = createList(numParams);
    for (int i = 0; i < numParams; i++) {
        int valueLength = buffer.readInt();
        if (valueLength == -1) {
            params.add(null);
        } else {
            DataType paramType = session.getParamType(statementName, i);
            PGType pgType = PGTypes.get(paramType);
            FormatCodes.FormatCode formatCode = getFormatCode(formatCodes, i);
            switch(formatCode) {
                case TEXT:
                    params.add(pgType.readTextValue(buffer, valueLength));
                    break;
                case BINARY:
                    params.add(pgType.readBinaryValue(buffer, valueLength));
                    break;
                default:
                    Messages.sendErrorResponse(channel, new UnsupportedOperationException(String.format(Locale.ENGLISH, "Unsupported format code '%d' for param '%s'", formatCode.ordinal(), paramType.getName())));
                    return;
            }
        }
    }
    FormatCodes.FormatCode[] resultFormatCodes = FormatCodes.fromBuffer(buffer);
    session.bind(portalName, statementName, params, resultFormatCodes);
    Messages.sendBindComplete(channel);
}
Also used : FormatCodes.getFormatCode(io.crate.protocols.postgres.FormatCodes.getFormatCode) PGType(io.crate.protocols.postgres.types.PGType) DataType(io.crate.types.DataType)

Example 75 with DataType

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

the class WindowProjector method createComputeEndFrameBoundary.

static ComputeFrameBoundary<Object[]> createComputeEndFrameBoundary(int numCellsInSourceRow, TransactionContext txnCtx, NodeContext nodeCtx, WindowDefinition windowDefinition, Comparator<Object[]> cmpOrderBy) {
    var frameDefinition = windowDefinition.windowFrameDefinition();
    var frameBoundEnd = frameDefinition.end();
    var framingMode = frameDefinition.mode();
    DataType offsetType = frameBoundEnd.value().valueType();
    Object offsetValue = evaluateWithoutParams(txnCtx, nodeCtx, frameBoundEnd.value());
    Object[] endProbeValues = new Object[numCellsInSourceRow];
    BiFunction<Object[], Object[], Object[]> updateProbeValues;
    if (offsetValue != null && framingMode == WindowFrame.Mode.RANGE) {
        updateProbeValues = createUpdateProbeValueFunction(windowDefinition, ArithmeticOperatorsFactory::getAddFunction, offsetValue, offsetType);
    } else {
        updateProbeValues = (currentRow, x) -> x;
    }
    return (partitionStart, partitionEnd, currentIndex, sortedRows) -> frameBoundEnd.type().getEnd(framingMode, partitionStart, partitionEnd, currentIndex, offsetValue, updateProbeValues.apply(sortedRows.get(currentIndex), endProbeValues), cmpOrderBy, sortedRows);
}
Also used : Input(io.crate.data.Input) TransactionContext(io.crate.metadata.TransactionContext) InputColumn(io.crate.expression.symbol.InputColumn) WindowFrame(io.crate.sql.tree.WindowFrame) BiFunction(java.util.function.BiFunction) RowAccountingWithEstimators(io.crate.breaker.RowAccountingWithEstimators) CollectExpression(io.crate.execution.engine.collect.CollectExpression) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Projector(io.crate.data.Projector) IntervalType(io.crate.types.IntervalType) Symbols(io.crate.expression.symbol.Symbols) Nullable(javax.annotation.Nullable) IntSupplier(java.util.function.IntSupplier) NodeContext(io.crate.metadata.NodeContext) Executor(java.util.concurrent.Executor) AggregationFunction(io.crate.execution.engine.aggregation.AggregationFunction) SymbolType(io.crate.expression.symbol.SymbolType) Comparators.createComparator(io.crate.execution.engine.sort.Comparators.createComparator) DataType(io.crate.types.DataType) SymbolEvaluator.evaluateWithoutParams(io.crate.analyze.SymbolEvaluator.evaluateWithoutParams) MemoryManager(io.crate.memory.MemoryManager) RamAccounting(io.crate.breaker.RamAccounting) ExpressionsInput(io.crate.expression.ExpressionsInput) List(java.util.List) OrderBy(io.crate.analyze.OrderBy) Version(org.elasticsearch.Version) Row(io.crate.data.Row) Literal(io.crate.expression.symbol.Literal) WindowDefinition(io.crate.analyze.WindowDefinition) WindowAggProjection(io.crate.execution.dsl.projection.WindowAggProjection) Symbol(io.crate.expression.symbol.Symbol) FunctionImplementation(io.crate.metadata.FunctionImplementation) InputFactory(io.crate.expression.InputFactory) Comparator(java.util.Comparator) DataType(io.crate.types.DataType)

Aggregations

DataType (io.crate.types.DataType)95 ArrayType (io.crate.types.ArrayType)35 Test (org.junit.Test)33 ArrayList (java.util.ArrayList)17 Map (java.util.Map)17 CrateUnitTest (io.crate.test.integration.CrateUnitTest)14 List (java.util.List)12 Symbol (io.crate.expression.symbol.Symbol)11 Literal (io.crate.expression.symbol.Literal)9 ColumnIdent (io.crate.metadata.ColumnIdent)9 HashMap (java.util.HashMap)9 FunctionIdent (io.crate.metadata.FunctionIdent)8 NodeContext (io.crate.metadata.NodeContext)8 Reference (io.crate.metadata.Reference)8 Row (io.crate.data.Row)7 Symbols (io.crate.expression.symbol.Symbols)7 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)7 Locale (java.util.Locale)7 Lists2 (io.crate.common.collections.Lists2)6 FunctionInfo (io.crate.metadata.FunctionInfo)6