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);
}
}
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);
}
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);
}
};
}
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;
}
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());
}
Aggregations