Search in sources :

Example 91 with DataType

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

the class MapComparator method compareMaps.

public static <K, V> int compareMaps(Map<K, V> m1, Map<K, V> m2) {
    Objects.requireNonNull(m1, "map is null");
    Objects.requireNonNull(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 == null) {
            if (otherValue != null) {
                return 1;
            } else {
                continue;
            }
        }
        if (!thisValue.equals(otherValue)) {
            if (otherValue == null) {
                return -1;
            }
            if (!thisValue.getClass().equals(otherValue.getClass())) {
                DataType leftType = DataTypes.guessType(thisValue);
                int cmp = leftType.compare(thisValue, leftType.implicitCast(otherValue));
                if (cmp == 0) {
                    continue;
                }
                return cmp;
            }
            return 1;
        }
    }
    return 0;
}
Also used : DataType(io.crate.types.DataType) Map(java.util.Map)

Example 92 with DataType

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

the class Literal method typeMatchesValue.

private static <T> boolean typeMatchesValue(DataType<T> type, T value) {
    if (value == null) {
        return true;
    }
    if (type.id() == ObjectType.ID) {
        // noinspection unchecked
        Map<String, Object> mapValue = (Map<String, Object>) value;
        ObjectType objectType = ((ObjectType) type);
        for (String key : mapValue.keySet()) {
            DataType<?> innerType = objectType.innerType(key);
            // noinspection unchecked
            if (typeMatchesValue((DataType<Object>) innerType, mapValue.get(key)) == false) {
                return false;
            }
        }
        // lets do the expensive "deep" map value conversion only after everything else succeeded
        Map<String, Object> safeValue = objectType.sanitizeValue(value);
        return safeValue.size() == mapValue.size();
    }
    return Objects.equals(type.sanitizeValue(value), value);
}
Also used : ObjectType(io.crate.types.ObjectType) DataType(io.crate.types.DataType) Map(java.util.Map)

Example 93 with DataType

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

the class ArrayMinFunctionTest method test_array_returns_min_element.

@Test
public void test_array_returns_min_element() {
    List<DataType> typesToTest = new ArrayList(DataTypes.PRIMITIVE_TYPES);
    typesToTest.add(DataTypes.NUMERIC);
    for (DataType type : typesToTest) {
        var valuesToTest = TestingHelpers.getRandomsOfType(2, 10, type);
        var optional = valuesToTest.stream().filter(o -> o != null).min((o1, o2) -> type.compare(o1, o2));
        var expected = optional.orElse(null);
        String expression = String.format(Locale.ENGLISH, "array_min(?::%s[])", type.getName());
        assertEvaluate(expression, expected, Literal.of(valuesToTest, new ArrayType<>(type)));
    }
}
Also used : Asserts.assertThrowsMatches(io.crate.testing.Asserts.assertThrowsMatches) List(java.util.List) TestingHelpers(io.crate.testing.TestingHelpers) Literal(io.crate.expression.symbol.Literal) DataTypes(io.crate.types.DataTypes) Locale(java.util.Locale) DataType(io.crate.types.DataType) Test(org.junit.Test) ArrayType(io.crate.types.ArrayType) ArrayList(java.util.ArrayList) ArrayType(io.crate.types.ArrayType) ArrayList(java.util.ArrayList) DataType(io.crate.types.DataType) Test(org.junit.Test)

Example 94 with DataType

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

the class ArraySumFunctionTest method test_array_returns_sum_of_elements.

@Test
public void test_array_returns_sum_of_elements() {
    // This test picks up random numbers but controls that overflow will not happen (overflow case is checked in another test).
    List<DataType> typesToTest = new ArrayList(DataTypes.NUMERIC_PRIMITIVE_TYPES);
    typesToTest.add(DataTypes.NUMERIC);
    for (DataType type : typesToTest) {
        var valuesToTest = TestingHelpers.getRandomsOfType(1, 10, type);
        DataType inputDependantOutputType = DataTypes.LONG;
        if (type == DataTypes.FLOAT || type == DataTypes.DOUBLE || type == DataTypes.NUMERIC) {
            inputDependantOutputType = type;
        } else {
            // check potential overflow and get rid of numbers causing overflow
            long sum = 0;
            for (int i = 0; i < valuesToTest.size(); i++) {
                if (valuesToTest.get(i) != null) {
                    long nextNum = ((Number) valuesToTest.get(i)).longValue();
                    try {
                        sum = Math.addExact(sum, nextNum);
                    } catch (ArithmeticException e) {
                        // excluding i
                        valuesToTest = valuesToTest.subList(0, i);
                        break;
                    }
                }
            }
        }
        KahanSummationForDouble kahanSummationForDouble = new KahanSummationForDouble();
        var optional = valuesToTest.stream().filter(Objects::nonNull).reduce((o1, o2) -> {
            if (o1 instanceof BigDecimal) {
                return ((BigDecimal) o1).add((BigDecimal) o2);
            } else if (o1 instanceof Double || o1 instanceof Float) {
                return kahanSummationForDouble.sum(((Number) o1).doubleValue(), ((Number) o2).doubleValue());
            } else {
                return DataTypes.LONG.implicitCast(o1) + DataTypes.LONG.implicitCast(o2);
            }
        });
        var expected = inputDependantOutputType.implicitCast(optional.orElse(null));
        String expression = String.format(Locale.ENGLISH, "array_sum(?::%s[])", type.getName());
        assertEvaluate(expression, expected, Literal.of(valuesToTest, new ArrayType<>(type)));
    }
}
Also used : ArrayList(java.util.ArrayList) KahanSummationForDouble(io.crate.execution.engine.aggregation.impl.util.KahanSummationForDouble) KahanSummationForDouble(io.crate.execution.engine.aggregation.impl.util.KahanSummationForDouble) BigDecimal(java.math.BigDecimal) ArrayType(io.crate.types.ArrayType) DataType(io.crate.types.DataType) Test(org.junit.Test)

Example 95 with DataType

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

the class SQLIntegrationTestCase method assertFunctionIsCreatedOnAll.

public void assertFunctionIsCreatedOnAll(String schema, String name, List<DataType<?>> argTypes) throws Exception {
    SearchPath searchPath = SearchPath.pathWithPGCatalogAndDoc();
    assertBusy(() -> {
        Iterable<Functions> functions = internalCluster().getInstances(Functions.class);
        for (Functions function : functions) {
            FunctionImplementation func = function.get(schema, name, Lists2.map(argTypes, t -> Literal.of(t, null)), searchPath);
            assertThat(func, is(not(nullValue())));
            assertThat(func.info().ident().argumentTypes(), is(equalTo(argTypes)));
        }
    }, 20L, TimeUnit.SECONDS);
}
Also used : ParamTypeHints(io.crate.analyze.ParamTypeHints) SessionContext(io.crate.action.sql.SessionContext) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) SessionSettings(io.crate.metadata.settings.SessionSettings) Matchers.not(org.hamcrest.Matchers.not) Inherited(java.lang.annotation.Inherited) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Analyzer(io.crate.analyze.Analyzer) NodeLimits(io.crate.execution.jobs.NodeLimits) DependencyCarrier(io.crate.planner.DependencyCarrier) Functions(io.crate.metadata.Functions) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Map(java.util.Map) SQLTransportExecutor(io.crate.testing.SQLTransportExecutor) RandomStrings(com.carrotsearch.randomizedtesting.generators.RandomStrings) TableInfo(io.crate.metadata.table.TableInfo) RandomizedContext(com.carrotsearch.randomizedtesting.RandomizedContext) TasksService(io.crate.execution.jobs.TasksService) Logger(org.apache.logging.log4j.Logger) Row(io.crate.data.Row) SubQueryResults(io.crate.planner.operators.SubQueryResults) ClusterStateRequest(org.elasticsearch.action.admin.cluster.state.ClusterStateRequest) Matchers.is(org.hamcrest.Matchers.is) XContentFactory(org.elasticsearch.common.xcontent.XContentFactory) ClusterService(org.elasticsearch.cluster.service.ClusterService) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) Metadata(org.elasticsearch.cluster.metadata.Metadata) TestName(org.junit.rules.TestName) Timeout(org.junit.rules.Timeout) IndicesService(org.elasticsearch.indices.IndicesService) Nullable(javax.annotation.Nullable) PSQL_PORT_SETTING(io.crate.protocols.postgres.PostgresNetty.PSQL_PORT_SETTING) Before(org.junit.Before) Identifiers(io.crate.sql.Identifiers) Client(org.elasticsearch.client.Client) IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) IOException(java.io.IOException) Planner(io.crate.planner.Planner) Field(java.lang.reflect.Field) RoutingProvider(io.crate.metadata.RoutingProvider) Literal(io.crate.expression.symbol.Literal) PlannerContext(io.crate.planner.PlannerContext) Matcher(org.hamcrest.Matcher) Plan(io.crate.planner.Plan) PostgresNetty(io.crate.protocols.postgres.PostgresNetty) KillableCallable(io.crate.execution.jobs.kill.KillableCallable) Schemas(io.crate.metadata.Schemas) UseJdbc(io.crate.testing.UseJdbc) Randomness(org.elasticsearch.common.Randomness) TransportShardAction(io.crate.execution.dml.TransportShardAction) TransportShardUpsertAction(io.crate.execution.dml.upsert.TransportShardUpsertAction) RelationName(io.crate.metadata.RelationName) UseRandomizedSchema(io.crate.testing.UseRandomizedSchema) Random(java.util.Random) RootTask(io.crate.execution.jobs.RootTask) UseHashJoins(io.crate.testing.UseHashJoins) Settings(org.elasticsearch.common.settings.Settings) TestGroup(com.carrotsearch.randomizedtesting.annotations.TestGroup) Locale(java.util.Locale) After(org.junit.After) Documented(java.lang.annotation.Documented) ThreadPool(org.elasticsearch.threadpool.ThreadPool) Method(java.lang.reflect.Method) SQLOperations(io.crate.action.sql.SQLOperations) NodeContext(io.crate.metadata.NodeContext) User(io.crate.user.User) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UUID(java.util.UUID) Netty4Plugin(org.elasticsearch.transport.Netty4Plugin) InetSocketAddress(java.net.InetSocketAddress) Lists2(io.crate.common.collections.Lists2) List(java.util.List) Session(io.crate.action.sql.Session) ESIntegTestCase(org.elasticsearch.test.ESIntegTestCase) Symbol(io.crate.expression.symbol.Symbol) FunctionImplementation(io.crate.metadata.FunctionImplementation) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Annotation(java.lang.annotation.Annotation) TimeValue(io.crate.common.unit.TimeValue) Paging(io.crate.data.Paging) TestingRowConsumer(io.crate.testing.TestingRowConsumer) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) UserLookup(io.crate.user.UserLookup) SQLResponse(io.crate.testing.SQLResponse) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) TransportShardDeleteAction(io.crate.execution.dml.delete.TransportShardDeleteAction) SearchPath(io.crate.metadata.SearchPath) Index(org.elasticsearch.index.Index) SETTING_HTTP_COMPRESSION(org.elasticsearch.http.HttpTransportSettings.SETTING_HTTP_COMPRESSION) TaskId(org.elasticsearch.tasks.TaskId) Retention(java.lang.annotation.Retention) Strings(org.elasticsearch.common.Strings) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) Constants(io.crate.Constants) Symbols(io.crate.expression.symbol.Symbols) SqlParser(io.crate.sql.parser.SqlParser) Requests(org.elasticsearch.client.Requests) Listeners(com.carrotsearch.randomizedtesting.annotations.Listeners) TestExecutionConfig(io.crate.testing.TestExecutionConfig) ColumnIdent(io.crate.metadata.ColumnIdent) Plugin(org.elasticsearch.plugins.Plugin) Matchers(org.hamcrest.Matchers) DataType(io.crate.types.DataType) TimeUnit(java.util.concurrent.TimeUnit) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) Rule(org.junit.Rule) SystemPropsTestLoggingListener(io.crate.test.integration.SystemPropsTestLoggingListener) LogManager(org.apache.logging.log4j.LogManager) ConfigurationException(org.elasticsearch.common.inject.ConfigurationException) RetentionPolicy(java.lang.annotation.RetentionPolicy) SearchPath(io.crate.metadata.SearchPath) Functions(io.crate.metadata.Functions) FunctionImplementation(io.crate.metadata.FunctionImplementation)

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