Search in sources :

Example 21 with FunctionIdent

use of io.crate.metadata.FunctionIdent in project crate by crate.

the class CoordinateFunction method register.

private static void register(ScalarFunctionModule module, String name, Function<Object, Double> func) {
    for (DataType inputType : SUPPORTED_INPUT_TYPES) {
        FunctionIdent ident = new FunctionIdent(name, Collections.singletonList(inputType));
        module.register(new UnaryScalar<>(new FunctionInfo(ident, DataTypes.DOUBLE), func));
    }
}
Also used : FunctionIdent(io.crate.metadata.FunctionIdent) DataType(io.crate.types.DataType) FunctionInfo(io.crate.metadata.FunctionInfo)

Example 22 with FunctionIdent

use of io.crate.metadata.FunctionIdent in project crate by crate.

the class GeoHashFunction method register.

private static void register(ScalarFunctionModule module, String name, Function<Object, BytesRef> func) {
    for (DataType inputType : SUPPORTED_INPUT_TYPES) {
        FunctionIdent ident = new FunctionIdent(name, Collections.singletonList(inputType));
        module.register(new UnaryScalar<>(new FunctionInfo(ident, DataTypes.STRING), func));
    }
}
Also used : FunctionIdent(io.crate.metadata.FunctionIdent) DataType(io.crate.types.DataType) FunctionInfo(io.crate.metadata.FunctionInfo)

Example 23 with FunctionIdent

use of io.crate.metadata.FunctionIdent in project crate by crate.

the class GroupProjectionTest method testStreaming2.

@Test
public void testStreaming2() throws Exception {
    Reference nameRef = createReference("name", DataTypes.STRING);
    List<Symbol> keys = Collections.singletonList(nameRef);
    List<Aggregation> aggregations = Collections.singletonList(Aggregation.finalAggregation(new FunctionInfo(new FunctionIdent(CountAggregation.NAME, ImmutableList.of()), DataTypes.LONG), ImmutableList.of(), Aggregation.Step.PARTIAL));
    GroupProjection groupProjection = new GroupProjection(keys, aggregations, RowGranularity.CLUSTER);
    BytesStreamOutput out = new BytesStreamOutput();
    Projection.toStream(groupProjection, out);
    StreamInput in = StreamInput.wrap(out.bytes());
    GroupProjection p2 = (GroupProjection) Projection.fromStream(in);
    assertThat(p2.keys().size(), is(1));
    assertThat(p2.values().size(), is(1));
}
Also used : CountAggregation(io.crate.operation.aggregation.impl.CountAggregation) Aggregation(io.crate.analyze.symbol.Aggregation) FunctionIdent(io.crate.metadata.FunctionIdent) TestingHelpers.createReference(io.crate.testing.TestingHelpers.createReference) Reference(io.crate.metadata.Reference) Symbol(io.crate.analyze.symbol.Symbol) StreamInput(org.elasticsearch.common.io.stream.StreamInput) FunctionInfo(io.crate.metadata.FunctionInfo) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 24 with FunctionIdent

use of io.crate.metadata.FunctionIdent in project crate by crate.

the class MergeNodeTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    Reference nameRef = TestingHelpers.createReference("name", DataTypes.STRING);
    List<Symbol> keys = Collections.singletonList(nameRef);
    List<Aggregation> aggregations = Collections.singletonList(Aggregation.finalAggregation(new FunctionInfo(new FunctionIdent(CountAggregation.NAME, ImmutableList.of()), DataTypes.LONG), ImmutableList.of(), Aggregation.Step.PARTIAL));
    GroupProjection groupProjection = new GroupProjection(keys, aggregations, RowGranularity.CLUSTER);
    TopNProjection topNProjection = new TopNProjection(10, 0, InputColumn.numInputs(keys.size() + aggregations.size()));
    List<Projection> projections = Arrays.asList(groupProjection, topNProjection);
    MergePhase node = new MergePhase(UUID.randomUUID(), 0, "merge", 2, Collections.emptyList(), Arrays.<DataType>asList(DataTypes.UNDEFINED, DataTypes.STRING), projections, DistributionInfo.DEFAULT_BROADCAST, null);
    node.executionNodes(Sets.newHashSet("node1", "node2"));
    BytesStreamOutput output = new BytesStreamOutput();
    node.writeTo(output);
    StreamInput input = StreamInput.wrap(output.bytes());
    MergePhase node2 = MergePhase.FACTORY.create();
    node2.readFrom(input);
    assertThat(node.numUpstreams(), is(node2.numUpstreams()));
    assertThat(node.nodeIds(), is(node2.nodeIds()));
    assertThat(node.jobId(), is(node2.jobId()));
    assertEquals(node.inputTypes(), node2.inputTypes());
    assertThat(node.phaseId(), is(node2.phaseId()));
    assertThat(node.distributionInfo(), is(node2.distributionInfo()));
}
Also used : Reference(io.crate.metadata.Reference) Symbol(io.crate.analyze.symbol.Symbol) FunctionInfo(io.crate.metadata.FunctionInfo) TopNProjection(io.crate.planner.projection.TopNProjection) GroupProjection(io.crate.planner.projection.GroupProjection) Projection(io.crate.planner.projection.Projection) TopNProjection(io.crate.planner.projection.TopNProjection) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) CountAggregation(io.crate.operation.aggregation.impl.CountAggregation) Aggregation(io.crate.analyze.symbol.Aggregation) FunctionIdent(io.crate.metadata.FunctionIdent) MergePhase(io.crate.planner.node.dql.MergePhase) StreamInput(org.elasticsearch.common.io.stream.StreamInput) GroupProjection(io.crate.planner.projection.GroupProjection) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 25 with FunctionIdent

use of io.crate.metadata.FunctionIdent in project crate by crate.

the class ExpressionAnalyzer method convertFunctionCall.

protected Symbol convertFunctionCall(FunctionCall node, ExpressionAnalysisContext context) {
    List<Symbol> arguments = new ArrayList<>(node.getArguments().size());
    List<DataType> argumentTypes = new ArrayList<>(node.getArguments().size());
    for (Expression expression : node.getArguments()) {
        Symbol argSymbol = expression.accept(innerAnalyzer, context);
        argumentTypes.add(argSymbol.valueType());
        arguments.add(argSymbol);
    }
    FunctionInfo functionInfo;
    if (node.isDistinct()) {
        if (argumentTypes.size() > 1) {
            throw new UnsupportedOperationException(String.format(Locale.ENGLISH, "%s(DISTINCT x) does not accept more than one argument", node.getName()));
        }
        // define the inner function. use the arguments/argumentTypes from above
        FunctionIdent innerIdent = new FunctionIdent(CollectSetAggregation.NAME, argumentTypes);
        FunctionInfo innerInfo = getFunctionInfo(innerIdent);
        Symbol innerFunction = context.allocateFunction(innerInfo, arguments);
        // define the outer function which contains the inner function as argument.
        String nodeName = "collection_" + node.getName().toString();
        List<Symbol> outerArguments = Arrays.<Symbol>asList(innerFunction);
        ImmutableList<DataType> outerArgumentTypes = ImmutableList.<DataType>of(new SetType(argumentTypes.get(0)));
        FunctionIdent ident = new FunctionIdent(nodeName, outerArgumentTypes);
        try {
            functionInfo = getFunctionInfo(ident);
        } catch (UnsupportedOperationException ex) {
            throw new UnsupportedOperationException(String.format(Locale.ENGLISH, "unknown function %s(DISTINCT %s)", node.getName(), argumentTypes.get(0)), ex);
        }
        arguments = outerArguments;
    } else {
        FunctionIdent ident = new FunctionIdent(node.getName().toString(), argumentTypes);
        functionInfo = getFunctionInfo(ident);
    }
    return context.allocateFunction(functionInfo, arguments);
}
Also used : FunctionIdent(io.crate.metadata.FunctionIdent) FunctionInfo(io.crate.metadata.FunctionInfo)

Aggregations

FunctionIdent (io.crate.metadata.FunctionIdent)32 Test (org.junit.Test)19 FunctionInfo (io.crate.metadata.FunctionInfo)15 AggregationTest (io.crate.operation.aggregation.AggregationTest)12 DataType (io.crate.types.DataType)8 CrateUnitTest (io.crate.test.integration.CrateUnitTest)6 Aggregation (io.crate.analyze.symbol.Aggregation)4 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)4 AggregationFunction (io.crate.operation.aggregation.AggregationFunction)3 InputCollectExpression (io.crate.operation.collect.InputCollectExpression)3 StreamInput (org.elasticsearch.common.io.stream.StreamInput)3 Function (io.crate.analyze.symbol.Function)2 InputColumn (io.crate.analyze.symbol.InputColumn)2 Symbol (io.crate.analyze.symbol.Symbol)2 Reference (io.crate.metadata.Reference)2 Aggregator (io.crate.operation.aggregation.Aggregator)2 CountAggregation (io.crate.operation.aggregation.impl.CountAggregation)2 CollectExpression (io.crate.operation.collect.CollectExpression)2 SetType (io.crate.types.SetType)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1