Search in sources :

Example 36 with ArrayType

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

the class UpdateAnalyzerTest method testUpdateElementOfObjectArrayUsingParameterExpressionInsideFunctionResultsInCorrectlyTypedParameterSymbol.

@Test
public void testUpdateElementOfObjectArrayUsingParameterExpressionInsideFunctionResultsInCorrectlyTypedParameterSymbol() {
    AnalyzedUpdateStatement stmt = e.analyze("UPDATE bag SET ob = array_cat([?], [{obb=1}]) WHERE id = ?");
    assertThat(stmt.assignmentByTargetCol().keySet(), contains(isReference("ob", new ArrayType(DataTypes.UNTYPED_OBJECT))));
    assertThat(stmt.assignmentByTargetCol().values(), contains(isFunction("array_cat", isFunction("_array", singletonList(DataTypes.UNTYPED_OBJECT)), instanceOf(Literal.class))));
}
Also used : ArrayType(io.crate.types.ArrayType) Literal(io.crate.expression.symbol.Literal) SymbolMatchers.isLiteral(io.crate.testing.SymbolMatchers.isLiteral) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 37 with ArrayType

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

the class SelectStatementAnalyzerTest method testCastToNestedArrayCanBeUsed.

@Test
public void testCastToNestedArrayCanBeUsed() {
    var executor = SQLExecutor.builder(clusterService).build();
    AnalyzedRelation relation = executor.analyze("select [[1, 2, 3]]::array(array(int))");
    assertThat(relation.outputs().get(0).valueType(), is(new ArrayType<>(DataTypes.INTEGER_ARRAY)));
}
Also used : ArrayType(io.crate.types.ArrayType) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 38 with ArrayType

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

the class SelectStatementAnalyzerTest method testAnyOnObjectArrayField.

@Test
public void testAnyOnObjectArrayField() throws Exception {
    var executor = SQLExecutor.builder(clusterService).addTable(TableDefinitions.USER_TABLE_DEFINITION).build();
    QueriedSelectRelation relation = executor.analyze("select * from users where 5 = ANY (friends['id'])");
    Function anyFunction = (Function) relation.where();
    assertThat(anyFunction.name(), is(AnyEqOperator.NAME));
    assertThat(anyFunction.arguments().get(1), isReference("friends['id']", new ArrayType<>(DataTypes.LONG)));
    assertThat(anyFunction.arguments().get(0), isLiteral(5L));
}
Also used : ArrayType(io.crate.types.ArrayType) DistanceFunction(io.crate.expression.scalar.geo.DistanceFunction) SymbolMatchers.isFunction(io.crate.testing.SymbolMatchers.isFunction) TryCastFunction(io.crate.expression.scalar.cast.TryCastFunction) ExplicitCastFunction(io.crate.expression.scalar.cast.ExplicitCastFunction) Function(io.crate.expression.symbol.Function) SubscriptFunction(io.crate.expression.scalar.SubscriptFunction) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 39 with ArrayType

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

the class CreateFunctionAnalyzerTest method testCreateFunctionWithComplexComplexTypes.

@Test
public void testCreateFunctionWithComplexComplexTypes() {
    AnalyzedStatement analyzedStatement = e.analyze("CREATE FUNCTION" + "   bar(array(integer)," + "   object, ip," + "   timestamp with time zone" + ") RETURNS array(geo_point) LANGUAGE dummy_lang AS 'function() { return 1; }'");
    assertThat(analyzedStatement, instanceOf(AnalyzedCreateFunction.class));
    AnalyzedCreateFunction analysis = (AnalyzedCreateFunction) analyzedStatement;
    assertThat(analysis.name(), is("bar"));
    assertThat(analysis.replace(), is(false));
    assertThat(analysis.returnType(), is(new ArrayType(DataTypes.GEO_POINT)));
    assertThat(analysis.arguments().get(0), is(FunctionArgumentDefinition.of(new ArrayType(DataTypes.INTEGER))));
    assertThat(analysis.arguments().get(1), is(FunctionArgumentDefinition.of(DataTypes.UNTYPED_OBJECT)));
    assertThat(analysis.arguments().get(2), is(FunctionArgumentDefinition.of(DataTypes.IP)));
    assertThat(analysis.arguments().get(3), is(FunctionArgumentDefinition.of(DataTypes.TIMESTAMPZ)));
    assertThat(analysis.language(), is(Literal.of("dummy_lang")));
}
Also used : ArrayType(io.crate.types.ArrayType) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 40 with ArrayType

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

the class ArrayMaxFunctionTest method test_array_returns_max_element.

@Test
public void test_array_returns_max_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).max((o1, o2) -> type.compare(o1, o2));
        var expected = optional.orElse(null);
        String expression = String.format(Locale.ENGLISH, "array_max(?::%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)

Aggregations

ArrayType (io.crate.types.ArrayType)76 Test (org.junit.Test)53 DataType (io.crate.types.DataType)35 CrateUnitTest (io.crate.test.integration.CrateUnitTest)20 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)15 Symbol (io.crate.expression.symbol.Symbol)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 BytesRef (org.apache.lucene.util.BytesRef)8 DocTableInfo (io.crate.metadata.doc.DocTableInfo)7 Literal (io.crate.analyze.symbol.Literal)6 Literal (io.crate.expression.symbol.Literal)6 ColumnIdent (io.crate.metadata.ColumnIdent)6 Input (io.crate.data.Input)5 HashMap (java.util.HashMap)5 Function (io.crate.analyze.symbol.Function)4 Reference (io.crate.metadata.Reference)4 DataTypes (io.crate.types.DataTypes)4 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)3 TransactionContext (io.crate.metadata.TransactionContext)3