dcapwell commented on PR #2310:
URL: https://github.com/apache/cassandra/pull/2310#issuecomment-1563377307
@jbellis it works just fine for me...
```
@Test
public void selectFloatVectorFunctions()
{
createTable(KEYSPACE, "CREATE TABLE %s (pk int primary key, value
vector<float, 2>)");
execute("INSERT INTO %s (pk, value) VALUES (0, ?)", vector(1f, 2f));
assertRows(execute("SELECT similarity_cosine(value, value) FROM %s
WHERe pk=0"), row(vector(1f, 2f)));
}
```
```
public class VectorFcts
{
private static boolean isFloatVector(AbstractType<?> type)
{
type = type.unwrap();
return type instanceof VectorType && ((VectorType<?>)
type).getElementsType() == FloatType.instance;
}
public static void addFunctionsTo(NativeFunctions functions)
{
functions.add(new FunctionFactory("similarity_cosine",
FunctionParameter.anyType(true), FunctionParameter.anyType(true))
{
@Nullable
@Override
protected NativeFunction
doGetOrCreateFunction(List<AbstractType<?>> argTypes, AbstractType<?>
receiverType)
{
if (argTypes.size() != 2)
return null;
AbstractType<?> outputType = receiverType == null ?
argTypes.get(0) : receiverType;
if (!(isFloatVector(outputType) &&
argTypes.stream().allMatch(VectorFcts::isFloatVector)))
return null;
if (!argTypes.stream().allMatch(t -> t.equals(outputType)))
return null;
VectorType<Float> type = (VectorType<Float>) outputType;
return makeSimilarityCosine(name.name, type);
}
});
}
private static NativeFunction makeSimilarityCosine(String name,
VectorType<Float> type)
{
return new NativeScalarFunction(name, type, type, type)
{
@Override
public ByteBuffer execute(ProtocolVersion protocolVersion,
List<ByteBuffer> parameters) throws InvalidRequestException
{
return parameters.get(0);
}
};
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]