blerer commented on code in PR #1892:
URL: https://github.com/apache/cassandra/pull/1892#discussion_r1052043394


##########
src/java/org/apache/cassandra/cql3/functions/FunctionResolver.java:
##########
@@ -99,6 +100,7 @@ private static Collection<Function> collectCandidates(String 
keyspace,
             candidates.addAll(NativeFunctions.instance.getFunctions(name));
             
candidates.addAll(NativeFunctions.instance.getFactories(name).stream()
                                             .map(f -> 
f.getOrCreateFunction(providedArgs, receiverType, receiverKs, receiverCf))
+                                            .filter(Objects::nonNull)

Review Comment:
   Nit: The javadoc of 'getOrCreateFunction' should specify that the method can 
return null value. The annotation is here but it would better to have it 
specified in the the javadoc. I read the doc but did not pay attention to the 
annotation so I guess that others could do the same. 



##########
src/java/org/apache/cassandra/cql3/functions/masking/NullMaskingFunction.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.cql3.functions.masking;
+
+import java.nio.ByteBuffer;
+import java.util.List;
+
+import org.apache.cassandra.cql3.functions.FunctionFactory;
+import org.apache.cassandra.cql3.functions.FunctionName;
+import org.apache.cassandra.cql3.functions.FunctionParameter;
+import org.apache.cassandra.cql3.functions.NativeFunction;
+import org.apache.cassandra.db.marshal.AbstractType;
+import org.apache.cassandra.transport.ProtocolVersion;
+
+/**
+ * A {@link MaskingFunction} that always returns a {@code null} of the same 
type as its single argument.
+ * <p>
+ * For example, given a text column named "username", {@code 
mask_null(username)} will always return {@code null},
+ * independently of the actual value of that column.
+ */
+public class NullMaskingFunction extends MaskingFunction
+{
+    public static final String NAME = "null";
+
+    private NullMaskingFunction(FunctionName name, AbstractType<?> inputType)
+    {
+        super(name, inputType, inputType);
+    }
+
+    @Override
+    public final ByteBuffer execute(ProtocolVersion protocolVersion, 
List<ByteBuffer> parameters)
+    {
+        return null;

Review Comment:
   Some types like `smallint` for example represent a `null` value as an empty 
`ByteBuffer`. Other types like `text`do not seem to support `null` inputs. 
Returning a null here seems to be the equivalent of saying that the column do 
not exist. Is it really what we want?   



##########
src/java/org/apache/cassandra/cql3/functions/masking/HashMaskingFunction.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.cql3.functions.masking;
+
+import java.nio.ByteBuffer;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.List;
+import java.util.function.Supplier;
+import javax.annotation.Nullable;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Suppliers;
+
+import org.apache.cassandra.cql3.functions.FunctionFactory;
+import org.apache.cassandra.cql3.functions.FunctionName;
+import org.apache.cassandra.cql3.functions.FunctionParameter;
+import org.apache.cassandra.cql3.functions.NativeFunction;
+import org.apache.cassandra.db.marshal.AbstractType;
+import org.apache.cassandra.db.marshal.BytesType;
+import org.apache.cassandra.db.marshal.StringType;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.exceptions.InvalidRequestException;
+import org.apache.cassandra.transport.ProtocolVersion;
+
+/**
+ * A {@link MaskingFunction} that replaces the specified column value by its 
hash according to the specified
+ * algorithm. The available algorithms are those defined by the registered 
security {@link java.security.Provider}s.
+ * If no algorithm is passed to the function, the {@link #DEFAULT_ALGORITHM} 
will be used.
+ */
+public class HashMaskingFunction extends MaskingFunction
+{
+    public static final String NAME = "hash";
+
+    /** The default hashing algorithm to be used if no other algorithm is 
specified in the call to the function. */
+    public static final String DEFAULT_ALGORITHM = "SHA-256";
+
+    // The default message digest is lazily built to prevent a failure during 
server startup if the algorithm is not
+    // available. That way, if the algorithm is not found only the calls to 
the function will fail.
+    private static final Supplier<MessageDigest> DEFAULT_DIGEST = 
Suppliers.memoize(() -> messageDigest(DEFAULT_ALGORITHM));
+    private static final AbstractType<?>[] DEFAULT_ARGUMENTS = {};
+
+    /** The type of the supplied algorithm argument, {@code null} if that 
argument isn't supplied. */
+    @Nullable
+    private final StringType algorithmArgumentType;
+
+    private HashMaskingFunction(FunctionName name, AbstractType<?> inputType, 
@Nullable StringType algorithmArgumentType)
+    {
+        super(name, BytesType.instance, inputType, 
argumentsType(algorithmArgumentType));
+        this.algorithmArgumentType = algorithmArgumentType;
+    }
+
+    private static AbstractType<?>[] argumentsType(@Nullable StringType 
algorithmArgumentType)
+    {
+        // the algorithm argument is optional, so we will have different 
signatures depending on whether that argument
+        // is supplied or not
+        return algorithmArgumentType == null
+               ? DEFAULT_ARGUMENTS
+               : new AbstractType<?>[]{ algorithmArgumentType };
+    }
+
+    @Override
+    public final ByteBuffer execute(ProtocolVersion protocolVersion, 
List<ByteBuffer> parameters)
+    {
+        MessageDigest digest;
+        if (algorithmArgumentType == null || parameters.get(1) == null)
+        {
+            digest = DEFAULT_DIGEST.get();
+        }
+        else
+        {
+            String algorithm = 
algorithmArgumentType.compose(parameters.get(1));
+            digest = messageDigest(algorithm);
+        }
+
+        return hash(digest, parameters.get(0));
+    }
+
+    @VisibleForTesting
+    @Nullable
+    static ByteBuffer hash(MessageDigest digest, ByteBuffer value)
+    {
+        if (value == null)
+            return null;
+
+        byte[] hash = digest.digest(value.array());
+        return BytesType.instance.compose(ByteBuffer.wrap(hash));

Review Comment:
   Thinking of selector chaining. Is it safe to change the column type on the 
flight? Do we also change the Resultset metadata?



-- 
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]

Reply via email to