io.crate.types.DataType Java Examples

The following examples show how to use io.crate.types.DataType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BytesRefUtils.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public static void ensureStringTypesAreStrings(DataType[] dataTypes, Object[][] rows) {
    if (rows.length == 0) {
        return;
    }

    // NOTE: currently BytesRef inside Maps aren't converted here because
    // if the map is coming from a ESSearchTask/EsGetTask they already contain strings
    // and we have no case in which another Task returns a Map with ByteRefs/Strings inside.
    final IntArrayList stringColumns = new IntArrayList();
    final IntArrayList stringCollectionColumns = new IntArrayList();
    int idx = 0;
    for (DataType dataType : dataTypes) {
        if (BYTES_REF_TYPES.contains(dataType)) {
            stringColumns.add(idx);
        } else if ((DataTypes.isCollectionType(dataType)
                && (BYTES_REF_TYPES.contains(((CollectionType)dataType).innerType())))) {
            stringCollectionColumns.add(idx);
        }
        idx++;
    }

    for (Object[] row : rows) {
        convertStringColumns(row, stringColumns);
        convertStringCollectionColumns(row, stringCollectionColumns);
    }
}
 
Example #2
Source File: ArrayDifferenceFunction.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public Scalar<List<Object>, List<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.boundSignature.getReturnType().createType()).innerType();
    List<Object> values = (List<Object>) inputValue;
    Set<Object> subtractSet;
    if (values == null) {
        subtractSet = Collections.emptySet();
    } else {
        subtractSet = new HashSet<>();
        for (Object element : values) {
            subtractSet.add(innerType.value(element));
        }
    }
    return new ArrayDifferenceFunction(signature, boundSignature, subtractSet);
}
 
Example #3
Source File: LiteralTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testNestedArrayLiteral() throws Exception {
    for (DataType<?> type : DataTypes.PRIMITIVE_TYPES) {
        DataType<?> nestedType = new ArrayType<>(new ArrayType<>(type));
        Object value;

        if (type.id() == BooleanType.ID) {
            value = true;
        } else if (type.id() == DataTypes.IP.id()) {
            value = type.value("123.34.243.23");
        } else if (type.id() == DataTypes.INTERVAL.id()) {
            value = type.value(new Period().withSeconds(100));
        } else {
            value = type.value("0");
        }
        var nestedValue = List.of(List.of(value));
        Literal nestedLiteral = Literal.ofUnchecked(nestedType, nestedValue);
        assertThat(nestedLiteral.valueType(), is(nestedType));
        assertThat(nestedLiteral.value(), is(nestedValue));
    }
}
 
Example #4
Source File: MapComparator.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public static <K, V> int compareMaps(Map<K, V> m1, Map<K, V> m2) {
    Preconditions.checkNotNull(m1, "map is null");
    Preconditions.checkNotNull(m2, "map is null");
    int sizeCompare = Integer.compare(m1.size(), m2.size());
    if (sizeCompare != 0)
        return sizeCompare;

    for (Map.Entry<K, V> entry : m1.entrySet()) {
        V thisValue = entry.getValue();
        V otherValue = m2.get(entry.getKey());
        if (!thisValue.equals(otherValue)) {
            if (!thisValue.getClass().equals(otherValue.getClass())) {
                DataType leftType = DataTypes.guessType(thisValue);
                int cmp = leftType.compareValueTo(thisValue, leftType.value(otherValue));
                if (cmp == 0) {
                    continue;
                }
                return cmp;
            }
            return 1;
        }
    }
    return 0;
}
 
Example #5
Source File: DocIndexMetaData.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private void add(ColumnIdent column, DataType type, ColumnPolicy columnPolicy,
                 ReferenceInfo.IndexType indexType, boolean partitioned) {
    ReferenceInfo info;
    String generatedExpression = generatedColumns.get(column.fqn());
    if (generatedExpression == null) {
        info = newInfo(column, type, columnPolicy, indexType);
    } else {
        info = newGeneratedColumnInfo(column, type, columnPolicy, indexType, generatedExpression);
    }

    // don't add it if there is a partitioned equivalent of this column
    if (partitioned || !(partitionedBy != null && partitionedBy.contains(column))) {
        if (info.ident().isColumn()) {
            columnsBuilder.add(info);
        }
        referencesBuilder.put(info.ident().columnIdent(), info);
        if (info instanceof GeneratedReferenceInfo) {
            generatedColumnReferencesBuilder.add((GeneratedReferenceInfo) info);
        }
    }
    if (partitioned) {
        partitionedByColumnsBuilder.add(info);
    }
}
 
Example #6
Source File: AnalyzedTableElements.java    From crate with Apache License 2.0 6 votes vote down vote up
private AnalyzedTableElements(List<AnalyzedColumnDefinition<T>> partitionedByColumns,
                              List<AnalyzedColumnDefinition<T>> columns,
                              Set<ColumnIdent> columnIdents,
                              Map<ColumnIdent, DataType> columnTypes,
                              Set<String> primaryKeys,
                              Set<String> notNullColumns,
                              Map<String, String> checkConstraints,
                              List<List<String>> partitionedBy,
                              int numGeneratedColumns,
                              List<T> additionalPrimaryKeys,
                              Map<T, Set<String>> copyToMap) {
    this.partitionedByColumns = partitionedByColumns;
    this.columns = columns;
    this.columnIdents = columnIdents;
    this.columnTypes = columnTypes;
    this.primaryKeys = primaryKeys;
    this.notNullColumns = notNullColumns;
    this.checkConstraints = checkConstraints;
    this.partitionedBy = partitionedBy;
    this.numGeneratedColumns = numGeneratedColumns;
    this.additionalPrimaryKeys = additionalPrimaryKeys;
    this.copyToMap = copyToMap;
}
 
Example #7
Source File: AnalyzedColumnDefinition.java    From crate with Apache License 2.0 6 votes vote down vote up
private static void applyAndValidateStorageSettings(Map<String, Object> mapping,
                                                    AnalyzedColumnDefinition<Object> definition) {
    if (definition.storageProperties == null) {
        return;
    }
    Settings storageSettings = GenericPropertiesConverter.genericPropertiesToSettings(definition.storageProperties);
    for (String property : storageSettings.names()) {
        if (property.equals(COLUMN_STORE_PROPERTY)) {
            DataType<?> dataType = definition.dataType();
            boolean val = storageSettings.getAsBoolean(property, true);
            if (val == false) {
                if (!DataTypes.isSameType(dataType, DataTypes.STRING)) {
                    throw new IllegalArgumentException(
                        String.format(Locale.ENGLISH, "Invalid storage option \"columnstore\" for data type \"%s\"",
                                      dataType.getName()));
                }

                mapping.put(DOC_VALUES, "false");
            }
        } else {
            throw new IllegalArgumentException(
                String.format(Locale.ENGLISH, "Invalid storage option \"%s\"", storageSettings.get(property)));
        }
    }
}
 
Example #8
Source File: DocIndexMetaData.java    From crate with Apache License 2.0 6 votes vote down vote up
private Reference newInfo(Integer position,
                          ColumnIdent column,
                          DataType type,
                          @Nullable String formattedDefaultExpression,
                          ColumnPolicy columnPolicy,
                          Reference.IndexType indexType,
                          boolean nullable,
                          boolean columnStoreEnabled) {
    Symbol defaultExpression = null;
    if (formattedDefaultExpression != null) {
        Expression expression = SqlParser.createExpression(formattedDefaultExpression);
        defaultExpression = expressionAnalyzer.convert(expression, new ExpressionAnalysisContext());
    }
    return new Reference(
        refIdent(column),
        granularity(column),
        type,
        columnPolicy,
        indexType,
        nullable,
        columnStoreEnabled,
        position,
        defaultExpression
    );
}
 
Example #9
Source File: Functions.java    From crate with Apache License 2.0 6 votes vote down vote up
@Nullable
private FunctionImplementation get(Signature signature,
                                   List<DataType<?>> actualArgumentTypes,
                                   DataType<?> actualReturnType,
                                   Function<FunctionName, List<FunctionProvider>> lookupFunction) {
    var candidates = lookupFunction.apply(signature.getName());
    if (candidates == null) {
        return null;
    }
    for (var candidate : candidates) {
        if (candidate.getSignature().equals(signature)) {
            var boundSignature = Signature.builder(signature)
                .argumentTypes(Lists2.map(actualArgumentTypes, DataType::getTypeSignature))
                .returnType(actualReturnType.getTypeSignature())
                .build();
            return candidate.getFactory().apply(signature, boundSignature);
        }
    }
    return null;
}
 
Example #10
Source File: DocIndexMetaData.java    From crate with Apache License 2.0 6 votes vote down vote up
private ImmutableMap<ColumnIdent, String> getAnalyzers(ColumnIdent columnIdent, Map<String, Object> propertiesMap) {
    ImmutableMap.Builder<ColumnIdent, String> builder = ImmutableMap.builder();
    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());
        columnProperties = furtherColumnProperties(columnProperties);
        if (columnDataType.id() == ObjectType.ID
            || (columnDataType.id() == ArrayType.ID
                && ((ArrayType) columnDataType).innerType().id() == ObjectType.ID)) {
            if (columnProperties.get("properties") != null) {
                builder.putAll(getAnalyzers(newIdent, (Map<String, Object>) columnProperties.get("properties")));
            }
        }
        String analyzer = (String) columnProperties.get("analyzer");
        if (analyzer != null) {
            builder.put(newIdent, analyzer);
        }
    }
    return builder.build();
}
 
Example #11
Source File: DocInputFactory.java    From crate with Apache License 2.0 6 votes vote down vote up
public InputFactory.Context<? extends LuceneCollectorExpression<?>> extractImplementations(TransactionContext txnCtx,
                                                                                           RoutedCollectPhase phase) {
    OrderBy orderBy = phase.orderBy();
    ReferenceResolver<? extends LuceneCollectorExpression<?>> refResolver;
    if (orderBy == null) {
        refResolver = referenceResolver;
    } else {
        refResolver = ref -> {
            if (orderBy.orderBySymbols().contains(ref)) {
                DataType<?> dataType = ref.valueType();
                return new OrderByCollectorExpression(ref, orderBy, dataType::value);
            }
            return referenceResolver.getImplementation(ref);
        };
    }
    InputFactory.Context<? extends LuceneCollectorExpression<?>> ctx = inputFactory.ctxForRefs(txnCtx, refResolver);
    ctx.add(phase.toCollect());
    return ctx;
}
 
Example #12
Source File: FunctionArgumentDefinition.java    From crate with Apache License 2.0 6 votes vote down vote up
public static FunctionArgumentDefinition fromXContent(XContentParser parser) throws IOException {
    if (parser.currentToken() != XContentParser.Token.START_OBJECT) {
        throw new IllegalArgumentException("Expected a START_OBJECT but got " + parser.currentToken());
    }
    String name = null;
    DataType type = DataTypes.UNDEFINED;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            if ("name".equals(parser.currentName())) {
                name = parseStringField(parser);
            } else if ("data_type".equals(parser.currentName())) {
                if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
                    throw new IllegalArgumentException("Expected a START_OBJECT but got " + parser.currentToken());
                }
                type = UserDefinedFunctionMetaData.DataTypeXContent.fromXContent(parser);
            } else {
                throw new IllegalArgumentException("Expected \"name\" or \"data_type\", but got " + parser.currentName());
            }
        } else {
            throw new IllegalArgumentException("Expected a FIELD_NAME but got " + parser.currentToken());
        }
    }
    return new FunctionArgumentDefinition(name, type);
}
 
Example #13
Source File: MatchPredicate.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public static String getMatchType(@Nullable  String matchType, DataType columnType) {
    if (matchType == null) {
        return defaultMatchType(columnType);
    }
    if (columnType.equals(DataTypes.STRING)) {
        try {
            MultiMatchQueryBuilder.Type.parse(matchType, ParseFieldMatcher.STRICT);
            return matchType;
        } catch (ElasticsearchParseException e) {
            throw new IllegalArgumentException(String.format(Locale.ENGLISH,
                    "invalid MATCH type '%s' for type '%s'", matchType, columnType), e);
        }
    } else if (columnType.equals(DataTypes.GEO_SHAPE)) {
        if (!SUPPORTED_GEO_MATCH_TYPES.contains(matchType)) {
            throw new IllegalArgumentException(String.format(Locale.ENGLISH,
                    "invalid MATCH type '%s' for type '%s', valid types are: [%s]",
                    matchType, columnType, Joiner.on(",").join(SUPPORTED_GEO_MATCH_TYPES)));
        }
        return matchType;
    }
    throw new IllegalArgumentException("No match type for dataType: " + columnType);
}
 
Example #14
Source File: SignatureBinder.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public SolverReturnStatus update(BoundVariables.Builder bindings) {
    if (!bindings.containsTypeVariable(typeParameter)) {
        bindings.setTypeVariable(typeParameter, actualType);
        return SolverReturnStatus.CHANGED;
    }
    DataType<?> originalType = bindings.getTypeVariable(typeParameter);
    DataType<?> commonType = getCommonType(originalType, actualType);
    if (commonType == null) {
        return SolverReturnStatus.UNSOLVABLE;
    }
    if (commonType.equals(originalType)) {
        return SolverReturnStatus.UNCHANGED_SATISFIED;
    }
    bindings.setTypeVariable(typeParameter, commonType);
    return SolverReturnStatus.CHANGED;
}
 
Example #15
Source File: UserDefinedFunctionMetaData.java    From crate with Apache License 2.0 5 votes vote down vote up
public UserDefinedFunctionMetaData(String schema,
                                   String name,
                                   List<FunctionArgumentDefinition> arguments,
                                   DataType<?> returnType,
                                   String language,
                                   String definition) {
    this.schema = schema;
    this.name = name;
    this.arguments = arguments;
    this.returnType = returnType;
    this.language = language;
    this.definition = definition;
    this.argumentTypes = argumentTypesFrom(arguments);
    this.specificName = specificName(name, argumentTypes);
}
 
Example #16
Source File: ParameterContext.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public io.crate.analyze.symbol.Literal getAsSymbol(int index) {
    try {
        Object value = parameters()[index];
        DataType type = guessTypeSafe(value);
        // use type.value because some types need conversion (String to BytesRef, List to Array)
        return newLiteral(type, type.value(value));
    } catch (ArrayIndexOutOfBoundsException e) {
        throw new IllegalArgumentException(String.format(Locale.ENGLISH,
                "Tried to resolve a parameter but the arguments provided with the " +
                        "SQLRequest don't contain a parameter at position %d", index), e);
    }
}
 
Example #17
Source File: SubtractFunction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public FunctionImplementation<Function> getForTypes(List<DataType> dataTypes) throws IllegalArgumentException {
    validateTypes(dataTypes);
    if (containsTypesWithDecimal(dataTypes)) {
        return new DoubleSubtractFunction(genDoubleInfo(NAME, dataTypes));
    }
    return new LongSubtractFunction(genLongInfo(NAME, dataTypes));
}
 
Example #18
Source File: ExpressionAnalyzer.java    From crate with Apache License 2.0 5 votes vote down vote up
private static void ensureResultTypesMatch(Collection<? extends Symbol> results) {
    HashSet<DataType<?>> resultTypes = new HashSet<>(results.size());
    for (Symbol result : results) {
        resultTypes.add(result.valueType());
    }
    if (resultTypes.size() == 2 && !resultTypes.contains(DataTypes.UNDEFINED) || resultTypes.size() > 2) {
        throw new UnsupportedOperationException(String.format(Locale.ENGLISH,
            "Data types of all result expressions of a CASE statement must be equal, found: %s",
            results.stream().map(Symbol::valueType).collect(toList())));
    }
}
 
Example #19
Source File: MaximumAggregation.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public static void register(AggregationImplModule mod) {
    for (final DataType dataType : DataTypes.PRIMITIVE_TYPES) {
        FunctionInfo functionInfo = new FunctionInfo(
                new FunctionIdent(NAME, ImmutableList.of(dataType)), dataType, FunctionInfo.Type.AGGREGATE);

        if (dataType instanceof FixedWidthType) {
            mod.register(new FixedMaximumAggregation(functionInfo));
        } else {
            mod.register(new VariableMaximumAggregation(functionInfo));
        }
    }
}
 
Example #20
Source File: InputFieldComparator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public InputFieldComparator(int numHits,
                            List<LuceneCollectorExpression> collectorExpressions,
                            Input input,
                            DataType valueType,
                            Object missingValue) {
    this.collectorExpressions = collectorExpressions;
    this.missingValue = missingValue;
    this.valueType = valueType;
    this.values = new Object[numHits];
    this.input = input;
}
 
Example #21
Source File: ArbitraryAggregation.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public static void register(AggregationImplModule mod) {
    for (final DataType t : DataTypes.PRIMITIVE_TYPES) {
        mod.register(new ArbitraryAggregation(
                new FunctionInfo(new FunctionIdent(NAME, ImmutableList.of(t)), t,
                                FunctionInfo.Type.AGGREGATE)));
    }
}
 
Example #22
Source File: ConversionException.java    From crate with Apache License 2.0 5 votes vote down vote up
public ConversionException(DataType<?> sourceType, DataType<?> targetType) {
    super(String.format(
        Locale.ENGLISH,
        "Cannot cast expressions from type `%s` to type `%s`",
        sourceType.getName(),
        targetType.getName()
    ));
}
 
Example #23
Source File: FieldsVisitorInputFieldComparator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public FieldsVisitorInputFieldComparator(int numHits,
                                         CollectorFieldsVisitor fieldsVisitor,
                                         List<LuceneCollectorExpression> collectorExpressions,
                                         Input input,
                                         DataType valueType,
                                         Object missingValue) {
    super(numHits, collectorExpressions, input, valueType, missingValue);
    this.fieldsVisitor = fieldsVisitor;
    assert fieldsVisitor.required() : "Use InputFieldComparator if FieldsVisitor is not required";
}
 
Example #24
Source File: StdDevAggregationTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testReturnType() throws Exception {
    for (DataType<?> type : Iterables.concat(DataTypes.NUMERIC_PRIMITIVE_TYPES, List.of(DataTypes.TIMESTAMPZ))) {
        // Return type is fixed to Double
        FunctionImplementation stddev = functions.get(
            null, "stddev", ImmutableList.of(Literal.of(type, null)), SearchPath.pathWithPGCatalogAndDoc());
        assertEquals(DataTypes.DOUBLE, stddev.info().returnType());
    }
}
 
Example #25
Source File: ArrayArgumentValidators.java    From crate with Apache License 2.0 5 votes vote down vote up
public static void ensureInnerTypeIsNotUndefined(List<DataType<?>> dataTypes, String functionName) {
    DataType<?> innerType = ((ArrayType<?>) dataTypes.get(0)).innerType();
    if (innerType.equals(DataTypes.UNDEFINED)) {
        throw new IllegalArgumentException(String.format(
            Locale.ENGLISH,
            "The inner type of the array argument `%s` function cannot be undefined",
            functionName));
    }
}
 
Example #26
Source File: TopNProjection.java    From crate with Apache License 2.0 5 votes vote down vote up
public TopNProjection(int limit, int offset, List<DataType<?>> outputTypes) {
    assert limit > TopN.NO_LIMIT : "limit of TopNProjection must not be negative/unlimited";

    this.limit = limit;
    this.offset = offset;
    this.outputs = InputColumn.mapToInputColumns(outputTypes);
}
 
Example #27
Source File: UserDefinedFunctionsMetaData.java    From crate with Apache License 2.0 5 votes vote down vote up
public boolean contains(String schema, String name, List<DataType<?>> types) {
    for (UserDefinedFunctionMetaData function : functionsMetaData) {
        if (function.sameSignature(schema, name, types)) {
            return true;
        }
    }
    return false;
}
 
Example #28
Source File: TypeGuessEstimateRowSize.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public long applyAsLong(Row row) {
    if (estimateRowSize == null) {
        ArrayList<DataType<?>> guessedTypes = new ArrayList<>(row.numColumns());
        for (int i = 0; i < row.numColumns(); i++) {
            guessedTypes.add(DataTypes.guessType(row.get(i)));
        }
        estimateRowSize = new EstimateRowSize(guessedTypes);
    }
    return estimateRowSize.applyAsLong(row);
}
 
Example #29
Source File: SizeEstimatorFactory.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> SizeEstimator<T> create(DataType type) {
    switch (type.id()) {
        case StringType.ID:
        case IpType.ID:
            return (SizeEstimator<T>)new BytesRefSizeEstimator();
        default:
            if (type instanceof FixedWidthType) {
                return (SizeEstimator<T>) new ConstSizeEstimator(((FixedWidthType) type).fixedSize());
            }
            throw new UnsupportedOperationException(String.format(Locale.ENGLISH, "Cannot get SizeEstimator for type %s", type));
    }
}
 
Example #30
Source File: SessionTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetParamType() {
    SQLExecutor sqlExecutor = SQLExecutor.builder(clusterService).build();

    DependencyCarrier executor = mock(DependencyCarrier.class);
    Session session = new Session(
        sqlExecutor.analyzer,
        sqlExecutor.planner,
        new JobsLogs(() -> false),
        false,
        executor,
        AccessControl.DISABLED,
        SessionContext.systemSessionContext());

    session.parse("S_1", "Select 1 + ? + ?;", Collections.emptyList());
    assertThat(session.getParamType("S_1", 0), is(DataTypes.UNDEFINED));
    assertThat(session.getParamType("S_1", 2), is(DataTypes.UNDEFINED));

    DescribeResult describe = session.describe('S', "S_1");
    assertThat(describe.getParameters(), equalTo(new DataType[] { DataTypes.INTEGER, DataTypes.INTEGER }));

    assertThat(session.getParamType("S_1", 0), is(DataTypes.INTEGER));
    assertThat(session.getParamType("S_1", 1), is(DataTypes.INTEGER));

    expectedException.expect(IllegalStateException.class);
    expectedException.expectMessage("Requested parameter index exceeds the number of parameters");
    assertThat(session.getParamType("S_1", 3), is(DataTypes.UNDEFINED));
}