Search in sources :

Example 31 with DataType

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

the class ArrayDifferenceFunction method compile.

@Override
public Scalar<Object[], Object> compile(List<Symbol> arguments) {
    Symbol symbol = arguments.get(1);
    if (!symbol.symbolType().isValueSymbol()) {
        // arguments are no values, we can't compile
        return this;
    }
    Input input = (Input) symbol;
    Object inputValue = input.value();
    DataType innerType = ((ArrayType) this.info().returnType()).innerType();
    Object[] array = (Object[]) inputValue;
    Set<Object> subtractSet = new HashSet<>();
    if (array.length > 0) {
        for (Object element : array) {
            subtractSet.add(innerType.value(element));
        }
    }
    return new ArrayDifferenceFunction(this.functionInfo, subtractSet);
}
Also used : ArrayType(io.crate.types.ArrayType) Input(io.crate.data.Input) Symbol(io.crate.analyze.symbol.Symbol) DataType(io.crate.types.DataType)

Example 32 with DataType

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

the class Messages method sendDataRow.

/**
     * Byte1('D')
     * Identifies the message as a data row.
     * <p>
     * Int32
     * Length of message contents in bytes, including self.
     * <p>
     * Int16
     * The number of column values that follow (possibly zero).
     * <p>
     * Next, the following pair of fields appear for each column:
     * <p>
     * Int32
     * The length of the column value, in bytes (this count does not include itself).
     * Can be zero. As a special case, -1 indicates a NULL column value. No value bytes follow in the NULL case.
     * <p>
     * ByteN
     * The value of the column, in the format indicated by the associated format code. n is the above length.
     */
static void sendDataRow(Channel channel, Row row, List<? extends DataType> columnTypes, @Nullable FormatCodes.FormatCode[] formatCodes) {
    int length = 4 + 2;
    assert columnTypes.size() == row.numColumns() : "Number of columns in the row must match number of columnTypes. Row: " + row + " types: " + columnTypes;
    ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
    buffer.writeByte('D');
    // will be set at the end
    buffer.writeInt(0);
    buffer.writeShort(row.numColumns());
    for (int i = 0; i < row.numColumns(); i++) {
        DataType dataType = columnTypes.get(i);
        PGType pgType = PGTypes.get(dataType);
        Object value = row.get(i);
        if (value == null) {
            buffer.writeInt(-1);
            length += 4;
        } else {
            FormatCodes.FormatCode formatCode = FormatCodes.getFormatCode(formatCodes, i);
            switch(formatCode) {
                case TEXT:
                    length += pgType.writeAsText(buffer, value);
                    break;
                case BINARY:
                    length += pgType.writeAsBinary(buffer, value);
                    break;
                default:
                    throw new AssertionError("Unrecognized formatCode: " + formatCode);
            }
        }
    }
    buffer.setInt(1, length);
    channel.write(buffer);
}
Also used : PGType(io.crate.protocols.postgres.types.PGType) DataType(io.crate.types.DataType) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 33 with DataType

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

the class InsertFromSubQueryAnalyzedStatement method rewriteNestedInputToSubscript.

private Symbol rewriteNestedInputToSubscript(ColumnIdent columnIdent, Symbol inputSymbol) {
    Reference reference = tableInfo().getReference(columnIdent);
    Symbol symbol = inputSymbol;
    Iterator<String> pathIt = columnIdent.path().iterator();
    while (pathIt.hasNext()) {
        // rewrite object access to subscript scalar
        String key = pathIt.next();
        DataType returnType = DataTypes.OBJECT;
        if (!pathIt.hasNext()) {
            returnType = reference.valueType();
        }
        FunctionIdent functionIdent = new FunctionIdent(SubscriptObjectFunction.NAME, ImmutableList.<DataType>of(DataTypes.OBJECT, DataTypes.STRING));
        symbol = new Function(new FunctionInfo(functionIdent, returnType), Arrays.asList(symbol, Literal.of(key)));
    }
    return symbol;
}
Also used : SubscriptObjectFunction(io.crate.operation.scalar.SubscriptObjectFunction) DataType(io.crate.types.DataType)

Example 34 with DataType

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

the class DocIndexMetaData method getColumnDataType.

/**
     * extract dataType from given columnProperties
     *
     * @param columnProperties map of String to Object containing column properties
     * @return dataType of the column with columnProperties
     */
public static DataType getColumnDataType(Map<String, Object> columnProperties) {
    DataType type;
    String typeName = (String) columnProperties.get("type");
    if (typeName == null) {
        if (columnProperties.containsKey("properties")) {
            type = DataTypes.OBJECT;
        } else {
            return DataTypes.NOT_SUPPORTED;
        }
    } else if (typeName.equalsIgnoreCase("array")) {
        Map<String, Object> innerProperties = getNested(columnProperties, "inner");
        DataType innerType = getColumnDataType(innerProperties);
        type = new ArrayType(innerType);
    } else {
        typeName = typeName.toLowerCase(Locale.ENGLISH);
        type = MoreObjects.firstNonNull(DataTypes.ofMappingName(typeName), DataTypes.NOT_SUPPORTED);
    }
    return type;
}
Also used : ArrayType(io.crate.types.ArrayType) DataType(io.crate.types.DataType)

Example 35 with DataType

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

the class DocIndexMetaData method internalExtractColumnDefinitions.

/**
     * extracts index definitions as well
     */
@SuppressWarnings("unchecked")
private void internalExtractColumnDefinitions(@Nullable ColumnIdent columnIdent, @Nullable Map<String, Object> propertiesMap) {
    if (propertiesMap == null) {
        return;
    }
    for (Map.Entry<String, Object> columnEntry : propertiesMap.entrySet()) {
        Map<String, Object> columnProperties = (Map) columnEntry.getValue();
        DataType columnDataType = getColumnDataType(columnProperties);
        ColumnIdent newIdent = childIdent(columnIdent, columnEntry.getKey());
        boolean nullable = !notNullColumns.contains(newIdent);
        columnProperties = furtherColumnProperties(columnProperties);
        Reference.IndexType columnIndexType = getColumnIndexType(columnProperties);
        if (columnDataType == DataTypes.GEO_SHAPE) {
            String geoTree = (String) columnProperties.get("tree");
            String precision = (String) columnProperties.get("precision");
            Integer treeLevels = (Integer) columnProperties.get("tree_levels");
            Double distanceErrorPct = (Double) columnProperties.get("distance_error_pct");
            addGeoReference(newIdent, geoTree, precision, treeLevels, distanceErrorPct);
        } else if (columnDataType == DataTypes.OBJECT || (columnDataType.id() == ArrayType.ID && ((ArrayType) columnDataType).innerType() == DataTypes.OBJECT)) {
            ColumnPolicy columnPolicy = ColumnPolicy.of(columnProperties.get("dynamic"));
            add(newIdent, columnDataType, columnPolicy, Reference.IndexType.NO, false, nullable);
            if (columnProperties.get("properties") != null) {
                // walk nested
                internalExtractColumnDefinitions(newIdent, (Map<String, Object>) columnProperties.get("properties"));
            }
        } else if (columnDataType != DataTypes.NOT_SUPPORTED) {
            List<String> copyToColumns = getNested(columnProperties, "copy_to");
            // extract columns this column is copied to, needed for indices
            if (copyToColumns != null) {
                for (String copyToColumn : copyToColumns) {
                    ColumnIdent targetIdent = ColumnIdent.fromPath(copyToColumn);
                    IndexReference.Builder builder = getOrCreateIndexBuilder(targetIdent);
                    builder.addColumn(newInfo(newIdent, columnDataType, ColumnPolicy.DYNAMIC, columnIndexType, false));
                }
            }
            // is it an index?
            if (indicesMap.containsKey(newIdent.fqn())) {
                IndexReference.Builder builder = getOrCreateIndexBuilder(newIdent);
                builder.indexType(columnIndexType).analyzer((String) columnProperties.get("analyzer"));
            } else {
                add(newIdent, columnDataType, columnIndexType, nullable);
            }
        }
    }
}
Also used : ColumnPolicy(io.crate.metadata.table.ColumnPolicy) DataType(io.crate.types.DataType)

Aggregations

DataType (io.crate.types.DataType)64 ArrayType (io.crate.types.ArrayType)30 Test (org.junit.Test)30 CrateUnitTest (io.crate.test.integration.CrateUnitTest)28 FunctionIdent (io.crate.metadata.FunctionIdent)8 FunctionInfo (io.crate.metadata.FunctionInfo)7 WhereClause (io.crate.analyze.WhereClause)6 Symbol (io.crate.analyze.symbol.Symbol)4 Function (io.crate.analyze.symbol.Function)3 Input (io.crate.data.Input)3 Map (java.util.Map)3 ColumnUnknownException (io.crate.exceptions.ColumnUnknownException)2 ColumnIdent (io.crate.metadata.ColumnIdent)2 Reference (io.crate.metadata.Reference)2 DocTableInfo (io.crate.metadata.doc.DocTableInfo)2 TableInfo (io.crate.metadata.table.TableInfo)2 SubscriptFunction (io.crate.operation.scalar.SubscriptFunction)2 AddFunction (io.crate.operation.scalar.arithmetic.AddFunction)2 DistanceFunction (io.crate.operation.scalar.geo.DistanceFunction)2 MatchesFunction (io.crate.operation.scalar.regex.MatchesFunction)2