Skip to content

Instantly share code, notes, and snippets.

@myui
Created July 17, 2015 06:23
Show Gist options
  • Save myui/a4b230ec633db8541aab to your computer and use it in GitHub Desktop.
Save myui/a4b230ec633db8541aab to your computer and use it in GitHub Desktop.
ArrayOverlapsUDF.java
/*
* Hivemall: Hive scalable Machine Learning Library
*
* Copyright (C) 2015 Makoto YUI
* Copyright (C) 2013-2015 National Institute of Advanced Industrial Science and Technology (AIST)
*
* Licensed 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 hivemall.tools.array;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.hive.ql.udf.UDFType;
import org.apache.hadoop.io.BooleanWritable;
@Description(name = "array_overlaps", value = "_FUNC_(ary1, ary2) - Returns true if have elements in common")
@UDFType(deterministic = true, stateful = false)
public final class ArrayOverlapsUDF extends UDF {
@Nonnull
public BooleanWritable evaluate(@Nullable final List<String> ary1, @Nullable final List<String> ary2) {
return evaluate(ary1, ary2, false);
}
@Nonnull
public BooleanWritable evaluate(@Nullable List<String> ary1, @Nullable List<String> ary2, final boolean sorted) {
if(ary1 == null) {
return ary2 == null ? new BooleanWritable(true) : new BooleanWritable(false);
} else if(ary2 == null) {
return new BooleanWritable(false);
}
if(sorted == false) {
Collections.sort(ary1);
}
for(String k : ary2) {
if(Collections.binarySearch(ary2, k) >= 0) {
return new BooleanWritable(true);
}
}
return new BooleanWritable(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment