Search in sources :

Example 1 with org.apache.cassandra.cql3.functions

use of org.apache.cassandra.cql3.functions in project cassandra by apache.

the class SchemaKeyspace method createUDAFromRow.

private static UDAggregate createUDAFromRow(UntypedResultSet.Row row, Functions functions, Types types) {
    String ksName = row.getString("keyspace_name");
    String functionName = row.getString("aggregate_name");
    FunctionName name = new FunctionName(ksName, functionName);
    List<AbstractType<?>> argTypes = row.getFrozenList("argument_types", UTF8Type.instance).stream().map(t -> CQLTypeParser.parse(ksName, t, types)).collect(toList());
    AbstractType<?> returnType = CQLTypeParser.parse(ksName, row.getString("return_type"), types);
    FunctionName stateFunc = new FunctionName(ksName, (row.getString("state_func")));
    FunctionName finalFunc = row.has("final_func") ? new FunctionName(ksName, row.getString("final_func")) : null;
    AbstractType<?> stateType = row.has("state_type") ? CQLTypeParser.parse(ksName, row.getString("state_type"), types) : null;
    ByteBuffer initcond = row.has("initcond") ? Terms.asBytes(ksName, row.getString("initcond"), stateType) : null;
    try {
        return UDAggregate.create(functions, name, argTypes, returnType, stateFunc, finalFunc, stateType, initcond);
    } catch (InvalidRequestException reason) {
        return UDAggregate.createBroken(name, argTypes, returnType, initcond, reason);
    }
}
Also used : org.apache.cassandra.config(org.apache.cassandra.config) java.util(java.util) CharacterCodingException(java.nio.charset.CharacterCodingException) org.apache.cassandra.db.marshal(org.apache.cassandra.db.marshal) MessageDigest(java.security.MessageDigest) LoggerFactory(org.slf4j.LoggerFactory) org.apache.cassandra.db(org.apache.cassandra.db) ByteBuffer(java.nio.ByteBuffer) org.apache.cassandra.cql3(org.apache.cassandra.cql3) org.apache.cassandra.db.rows(org.apache.cassandra.db.rows) View(org.apache.cassandra.db.view.View) ImmutableList(com.google.common.collect.ImmutableList) SelectStatement(org.apache.cassandra.cql3.statements.SelectStatement) ColumnFilter(org.apache.cassandra.db.filter.ColumnFilter) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) org.apache.cassandra.cql3.functions(org.apache.cassandra.cql3.functions) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) org.apache.cassandra.db.partitions(org.apache.cassandra.db.partitions) Collectors.toSet(java.util.stream.Collectors.toSet) ClusteringOrder(org.apache.cassandra.schema.ColumnMetadata.ClusteringOrder) Logger(org.slf4j.Logger) FBUtilities(org.apache.cassandra.utils.FBUtilities) ByteBufferUtil(org.apache.cassandra.utils.ByteBufferUtil) QueryProcessor.executeInternal(org.apache.cassandra.cql3.QueryProcessor.executeInternal) Maps(com.google.common.collect.Maps) String.format(java.lang.String.format) QueryProcessor.executeOnceInternal(org.apache.cassandra.cql3.QueryProcessor.executeOnceInternal) TimeUnit(java.util.concurrent.TimeUnit) MapDifference(com.google.common.collect.MapDifference) Collectors.toList(java.util.stream.Collectors.toList) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CreateTableStatement(org.apache.cassandra.cql3.statements.CreateTableStatement) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) ByteBuffer(java.nio.ByteBuffer)

Example 2 with org.apache.cassandra.cql3.functions

use of org.apache.cassandra.cql3.functions in project cassandra by apache.

the class SchemaKeyspace method createUDAFromRow.

private static UDAggregate createUDAFromRow(UntypedResultSet.Row row, Collection<UDFunction> functions, Types types) {
    String ksName = row.getString("keyspace_name");
    String functionName = row.getString("aggregate_name");
    FunctionName name = new FunctionName(ksName, functionName);
    List<AbstractType<?>> argTypes = row.getFrozenList("argument_types", UTF8Type.instance).stream().map(t -> CQLTypeParser.parse(ksName, t, types)).collect(toList());
    AbstractType<?> returnType = CQLTypeParser.parse(ksName, row.getString("return_type"), types);
    FunctionName stateFunc = new FunctionName(ksName, (row.getString("state_func")));
    FunctionName finalFunc = row.has("final_func") ? new FunctionName(ksName, row.getString("final_func")) : null;
    AbstractType<?> stateType = row.has("state_type") ? CQLTypeParser.parse(ksName, row.getString("state_type"), types) : null;
    ByteBuffer initcond = row.has("initcond") ? Terms.asBytes(ksName, row.getString("initcond"), stateType) : null;
    return UDAggregate.create(functions, name, argTypes, returnType, stateFunc, finalFunc, stateType, initcond);
}
Also used : org.apache.cassandra.config(org.apache.cassandra.config) java.util(java.util) CharacterCodingException(java.nio.charset.CharacterCodingException) org.apache.cassandra.db.marshal(org.apache.cassandra.db.marshal) LoggerFactory(org.slf4j.LoggerFactory) org.apache.cassandra.db(org.apache.cassandra.db) ByteBuffer(java.nio.ByteBuffer) org.apache.cassandra.cql3(org.apache.cassandra.cql3) org.apache.cassandra.db.rows(org.apache.cassandra.db.rows) SchemaKeyspaceTables(org.apache.cassandra.schema.SchemaKeyspaceTables) RecognitionException(org.antlr.runtime.RecognitionException) Simulate(org.apache.cassandra.utils.Simulate) ColumnFilter(org.apache.cassandra.db.filter.ColumnFilter) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) org.apache.cassandra.cql3.functions(org.apache.cassandra.cql3.functions) KeyspacesDiff(org.apache.cassandra.schema.Keyspaces.KeyspacesDiff) com.google.common.collect(com.google.common.collect) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) SpeculativeRetryPolicy(org.apache.cassandra.service.reads.SpeculativeRetryPolicy) org.apache.cassandra.db.partitions(org.apache.cassandra.db.partitions) Collectors.toSet(java.util.stream.Collectors.toSet) ClusteringOrder(org.apache.cassandra.schema.ColumnMetadata.ClusteringOrder) Logger(org.slf4j.Logger) FBUtilities(org.apache.cassandra.utils.FBUtilities) ByteBufferUtil(org.apache.cassandra.utils.ByteBufferUtil) QueryProcessor.executeInternal(org.apache.cassandra.cql3.QueryProcessor.executeInternal) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) ReadRepairStrategy(org.apache.cassandra.service.reads.repair.ReadRepairStrategy) String.format(java.lang.String.format) QueryProcessor.executeOnceInternal(org.apache.cassandra.cql3.QueryProcessor.executeOnceInternal) CreateTableStatement(org.apache.cassandra.cql3.statements.schema.CreateTableStatement) TimeUnit(java.util.concurrent.TimeUnit) GLOBAL_CLOCK(org.apache.cassandra.utils.Simulate.With.GLOBAL_CLOCK) Collectors.toList(java.util.stream.Collectors.toList) VisibleForTesting(com.google.common.annotations.VisibleForTesting) NotThreadSafe(javax.annotation.concurrent.NotThreadSafe) ByteBuffer(java.nio.ByteBuffer)

Example 3 with org.apache.cassandra.cql3.functions

use of org.apache.cassandra.cql3.functions in project cassandra by apache.

the class Terms method ofListMarker.

/**
 * Creates a {@code Terms} for the specified list marker.
 *
 * @param marker the list  marker
 * @param type the element type
 * @return a {@code Terms} for the specified list marker
 */
public static Terms ofListMarker(final Lists.Marker marker, final AbstractType<?> type) {
    return new Terms() {

        @Override
        public void addFunctionsTo(List<Function> functions) {
        }

        @Override
        public void collectMarkerSpecification(VariableSpecifications boundNames) {
            marker.collectMarkerSpecification(boundNames);
        }

        @Override
        public List<ByteBuffer> bindAndGet(QueryOptions options) {
            Terminal terminal = marker.bind(options);
            if (terminal == null)
                return null;
            if (terminal == Constants.UNSET_VALUE)
                return UNSET_LIST;
            return ((MultiItemTerminal) terminal).getElements();
        }

        @Override
        public List<Terminal> bind(QueryOptions options) {
            Terminal terminal = marker.bind(options);
            if (terminal == null)
                return null;
            if (terminal == Constants.UNSET_VALUE)
                return UNSET_LIST;
            java.util.function.Function<ByteBuffer, Term.Terminal> deserializer = deserializer(options.getProtocolVersion());
            List<ByteBuffer> boundValues = ((MultiItemTerminal) terminal).getElements();
            List<Term.Terminal> values = new ArrayList<>(boundValues.size());
            for (int i = 0, m = boundValues.size(); i < m; i++) {
                ByteBuffer buffer = boundValues.get(i);
                Term.Terminal value = buffer == null ? null : deserializer.apply(buffer);
                values.add(value);
            }
            return values;
        }

        public java.util.function.Function<ByteBuffer, Term.Terminal> deserializer(ProtocolVersion version) {
            if (type.isCollection()) {
                switch(((CollectionType<?>) type).kind) {
                    case LIST:
                        return e -> Lists.Value.fromSerialized(e, (ListType<?>) type, version);
                    case SET:
                        return e -> Sets.Value.fromSerialized(e, (SetType<?>) type, version);
                    case MAP:
                        return e -> Maps.Value.fromSerialized(e, (MapType<?, ?>) type, version);
                }
                throw new AssertionError();
            }
            return e -> new Constants.Value(e);
        }
    };
}
Also used : MultiItemTerminal(org.apache.cassandra.cql3.Term.MultiItemTerminal) Terminal(org.apache.cassandra.cql3.Term.Terminal) java.util(java.util) Function(org.apache.cassandra.cql3.functions.Function) org.apache.cassandra.db.marshal(org.apache.cassandra.db.marshal) MultiItemTerminal(org.apache.cassandra.cql3.Term.MultiItemTerminal) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) ByteBuffer(java.nio.ByteBuffer) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) ByteBuffer(java.nio.ByteBuffer) Terminal(org.apache.cassandra.cql3.Term.Terminal) MultiItemTerminal(org.apache.cassandra.cql3.Term.MultiItemTerminal) Terminal(org.apache.cassandra.cql3.Term.Terminal)

Example 4 with org.apache.cassandra.cql3.functions

use of org.apache.cassandra.cql3.functions in project cassandra by apache.

the class AggregateFcts method all.

public static Collection<AggregateFunction> all() {
    Collection<AggregateFunction> functions = new ArrayList<>();
    functions.add(countRowsFunction);
    // sum for primitives
    functions.add(sumFunctionForByte);
    functions.add(sumFunctionForShort);
    functions.add(sumFunctionForInt32);
    functions.add(sumFunctionForLong);
    functions.add(sumFunctionForFloat);
    functions.add(sumFunctionForDouble);
    functions.add(sumFunctionForDecimal);
    functions.add(sumFunctionForVarint);
    functions.add(sumFunctionForCounter);
    // avg for primitives
    functions.add(avgFunctionForByte);
    functions.add(avgFunctionForShort);
    functions.add(avgFunctionForInt32);
    functions.add(avgFunctionForLong);
    functions.add(avgFunctionForFloat);
    functions.add(avgFunctionForDouble);
    functions.add(avgFunctionForDecimal);
    functions.add(avgFunctionForVarint);
    functions.add(avgFunctionForCounter);
    // count, max, and min for all standard types
    for (CQL3Type type : CQL3Type.Native.values()) {
        if (// varchar and text both mapping to UTF8Type
        type != CQL3Type.Native.VARCHAR) {
            functions.add(AggregateFcts.makeCountFunction(type.getType()));
            if (type != CQL3Type.Native.COUNTER) {
                functions.add(AggregateFcts.makeMaxFunction(type.getType()));
                functions.add(AggregateFcts.makeMinFunction(type.getType()));
            } else {
                functions.add(AggregateFcts.maxFunctionForCounter);
                functions.add(AggregateFcts.minFunctionForCounter);
            }
        }
    }
    return functions;
}
Also used : CQL3Type(org.apache.cassandra.cql3.CQL3Type) ArrayList(java.util.ArrayList)

Example 5 with org.apache.cassandra.cql3.functions

use of org.apache.cassandra.cql3.functions in project cassandra by apache.

the class UFAuthTest method functionResource.

private FunctionResource functionResource(String functionName) {
    // Note that this is somewhat brittle as it assumes that function names are
    // truly unique. As such, it will break in the face of overloading.
    // It is here to avoid having to duplicate the functionality of CqlParser
    // for transforming cql types into AbstractTypes
    FunctionName fn = parseFunctionName(functionName);
    Collection<Function> functions = Schema.instance.getFunctions(fn);
    assertEquals(String.format("Expected a single function definition for %s, but found %s", functionName, functions.size()), 1, functions.size());
    return FunctionResource.function(fn.keyspace, fn.name, functions.iterator().next().argTypes());
}
Also used : FunctionName(org.apache.cassandra.cql3.functions.FunctionName) Function(org.apache.cassandra.cql3.functions.Function)

Aggregations

ByteBuffer (java.nio.ByteBuffer)4 java.util (java.util)3 ArrayList (java.util.ArrayList)3 Function (org.apache.cassandra.cql3.functions.Function)3 org.apache.cassandra.db.marshal (org.apache.cassandra.db.marshal)3 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)3 ProtocolVersion (org.apache.cassandra.transport.ProtocolVersion)3 Maps (com.google.common.collect.Maps)2 String.format (java.lang.String.format)2 CharacterCodingException (java.nio.charset.CharacterCodingException)2 List (java.util.List)2 TimeUnit (java.util.concurrent.TimeUnit)2 Collectors (java.util.stream.Collectors)2 Collectors.toList (java.util.stream.Collectors.toList)2 Collectors.toSet (java.util.stream.Collectors.toSet)2 org.apache.cassandra.config (org.apache.cassandra.config)2 org.apache.cassandra.cql3 (org.apache.cassandra.cql3)2 QueryProcessor.executeInternal (org.apache.cassandra.cql3.QueryProcessor.executeInternal)2 QueryProcessor.executeOnceInternal (org.apache.cassandra.cql3.QueryProcessor.executeOnceInternal)2 org.apache.cassandra.cql3.functions (org.apache.cassandra.cql3.functions)2