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));
}
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);
}
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);
}
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);
}
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);
}
Aggregations