No match found for function signature when NullHandling set to INTERNAL - apache-drill

I'm trying to implement an user defined function for Apache Drill.
The function takes float arguments (decimals do not work) and they have to be nullable in order to return zeroes.
However, when I use NullHandling.Internal and set the parameters as nullable types, the function can be no longer invoked.
SELECT tetsting_udf(1.23,4.56);
VALIDATION ERROR: (...): No match found for function signature TESTING_UDF(<DECIMAL>, <DECIMAL>)
SELECT tetsting_udf(cast(1.23 as float), cast(4.56 as float));
VALIDATION ERROR: (...): No match found for function signature TESTING_UDF(<FLOAT>, <FLOAT>)
When Float8Holders and NullHandling.NULL_IF_NULL is used, both calls above are working.
What I'm doing wrong?
#FunctionTemplate(
name = "testing_udf",
scope = FunctionTemplate.FunctionScope.SIMPLE,
nulls = FunctionTemplate.NullHandling.INTERNAL
)
public class TestingFunction implements DrillSimpleFunc {
#Param
NullableFloat8Holder numberA;
#Param
NullableFloat8Holder numberB;
#Output
Float8Holder out;
public void setup() {
}
public void eval() {
// Whatever
}
}

For the case when FunctionTemplate.NullHandling.INTERNAL is specified, UDFs implementations with all combinations of nullability should be specified. For your case, you should specify UDFs which accepts (Float8Holder and Float8Holder), (NullableFloat8Holder and NullableFloat8Holder), (Float8Holder and NullableFloat8Holder), (NullableFloat8Holder and Float8Holder).

Related

any() vs any(Class.class) Mockito

I am not able to understand why below two tests are not giving the same result.
#Service
public class SomeManager{
private final SomeDependency someDependency;
#Autowired
public SomeManager(SomeDependency someDependency){
this.someDependency = someDependency;
}
public List<returnType> methodToTest(Arg arg){
List<JsonObject> jo = someDependency.search(arg);
return jo.stream().map(returnType::parse).collect(Collectors.toList());
}
}
Test with any(). This Test pass.
#RunWith(SpringJUnit4ClassRunner.class)
public class TestMethodToTest(){
#Test
public void TestMethod(){
SomeDependency someDependency = mock(SomeDependency.class);
List<JsonObject> expected := \some valid list of JsonObject\
// Here I used any() method.
when(someDependency.search(any())).thenReturn(expected);
SomeManager someManager = new SomeManager(someDependency);
List<returnType> actual = someManager.methodToTest(any(Arg.class));
assertArrayEquals(acutal.toArray(), expected.stream().map(returnType::parse).toArray());
}
}
But since search(Arg arg) method of SomeDependency takes parameter of class Arg so I changed above test like this:
#RunWith(SpringJUnit4ClassRunner.class)
public class TestMethodToTest(){
#Test
public void TestMethod(){
SomeDependency someDependency = mock(SomeDependency.class);
List<JsonObject> expected := \some valid list of JsonObject\
// Here I used any(Arg.class) method.
when(someDependency.search(any(Arg.class))).thenReturn(expected);
SomeManager someManager = new SomeManager(someDependency);
List<returnType> actual = someManager.methodToTest(any(Arg.class));
assertArrayEquals(acutal.toArray(), expected.stream().map(returnType::parse).toArray());
}
}
This second test fails with output java.lang.AssertionError: array lengths differed, expected.length=1 actual.length=0.What's the possible reason behind this?
Note: The value expected.length=1 in output depends on what value is provided by the user as valid list of json objects in the test.
The difference stems from the fact that any matches null, while anyClass does not match null. See ArgumentMatchers javadoc:
any() Matches anything, including nulls and varargs.
any​(Class<T> type) Matches any object of given type, excluding nulls.
You are passing null to your method under test here:
List<returnType> actual = someManager.methodToTest(any(Arg.class));
any() returns null which you pass to method under test.
Note that using argument matchers this way is illegal - you should only call them inside calls to when and verify. You should pass a real instance of Arg to method under test.
See Mockito javadoc
Matcher methods like any(), eq() do not return matchers. Internally, they record a matcher on a stack and return a dummy value (usually null). This implementation is due to static type safety imposed by the java compiler. The consequence is that you cannot use any(), eq() methods outside of verified/stubbed method.

AS3 variable declared as a null function

I have encountered an AS3 function that is declared as a null variable, as in:
public var edgeWeights:Function = null;
I am not sure how to use this function to change null to another value (e.g., a number like 2 or 3). I thought something like cs.edgeWeights = 2 might work, but that creates a compile error, as does cs.edgeWeights(2);
I believe these are anonymous functions in AS3 and I did do some research on them, but could not find a resolution to this situation.
public var edgeWeights:Function = null;
This notation means declaring variable edgeWeights of type Function. In Actionscript Function is an object and can be set to null.
To use it you need to set this variable to some function. For example:
edgeWeights = function(a:int,b:int):int { return a+b } or edgeWeights = Math.sin.
What function you should set there depends on your particular case.
If you assume that the Class that declares edgeWeights is Widget:
protected var widget:Widget;
protected function createWidget():void {
widget = new Widget();
widget.edgeWeights = widgetCallback;
}
//signature would need to match what the Widget
//actually expects this callback to do
protected function widgetCallback():void {
trace('hi from widget callback');
}
Note that it's probably bad practice to have a public callback variable and not provide a default implementation, so if you have access to the source code, you should probably fix that.
Given any function:
public function someFunction()
{
...
}
You can create a "pointer" with this: this.edgeWeights = someFunction; (yes, without ())
Later you just use: this.edgeWeights(); and you'll be calling someFunction().

Responder callback methods

I need to create a Responder object, the constructor documentation says:
Parameters
result:Function — The function invoked if the call to the
server succeeds and returns a result.
status:Function (default = null) — The function invoked if the server returns an error.
What is the parameter of the status function? it says the signature is function(default = null), but it doesn't actually explain what is default.
What type is default?
What might it contain?
Here function(default = null) means that the default value for the second parameter is null rather than the signature if the status handler.
As for the signature of the status handler it depends on your client<->server protocol. For example look at the MessageResponder class that inherits the Responder that are used in the flex remoting. It has the strongly typing serialization of AMF directly to the IMessage:
public function MessageResponder(agent:MessageAgent, message:IMessage,
channel:Channel = null)
{
super(result, status);
...
}
...
final public function result(message:IMessage):void {...}
final public function status(message:IMessage):void {...}
In general you can pass the functions with the single Object argument:
public function status(message:Object):void {}
public function result(message:Object):void {}

what is the point of void in AS3

Simple question here, when void follows a function in AS3 what is it doing?
public function sayGoodbye():void { trace("Goodbye from MySubClass");}
void type indicates to the compiler that the function you have written will not return any value, in the other side if you indicate other type T than void the compiler expect that you return T.
Ex:
function foo(a:int):int { // here the compiler expect that somewhere
// in your function you return an int
return a;
}
void means that it has no return value. I.e., you can't use it in an expression.
void specifies that the function will return no value, or, to be more exact, the special undefined value type. Note that the function return can be used in an expression and it is the unique value of the undefined type.
In actionscript 3 to conform to the strict mode you need to specify variable types and function return types in order for the compiler to know what types to expect and to optimize your application.

Proper usage of "this." keyword in C#?

I'm working through the book Head First C# (and it's going well so far), but I'm having a lot of trouble wrapping my head around the syntax involved with using the "this." keyword.
Conceptually, I get that I'm supposed to use it to avoid having a parameter mask a field of the same name, but I'm having trouble actually tracking it through their examples (also, they don't seem to have a section dedicated to that particular keyword, they just explain it and start using it in their examples).
Does anyone have any good rules of thumb they follow when applying "this."? Or any tutorials online that explain it in a different way that Head First C#?
Thanks!
Personally I only use it when I have to which is:
Constructor chaining:
public Foo(int x) : this(x, null)
{
}
public Foo(int x, string name)
{
...
}
Copying from a parameter name into a field (not as common in C# as in Java, as you'd usually use a property - but common in constructors)
public void SetName(string name)
{
// Just "name = name" would be no-op; within this method,
// "name" refers to the parameter, not the field
this.name = name;
}
Referring to this object without any members involved:
Console.WriteLine(this);
Declaring an extension method:
public static TimeSpan Days(this int days)
{
return TimeSpan.FromDays(days);
}
Some other people always use it (e.g. for other method calls) - personally I find that clutters things up a bit.
StyleCop's default coding style enforces the following rule:
A1101: The call to {method or property
name} must begin with the 'this.'
prefix to indicate that the item is a
member of the class.
Which means that every method, field, property that belongs to the current class will be prefixed by this. I was initially resistant to this rule, which makes your code more verbose, but it has grown on me since, as it makes the code pretty clear. This thread discusses the question.
I write this. if and only if it enhances readability, for example, when implementing a Comparable interface (Java, but the idea is the same):
public void compareTo(MyClass other) {
if (this.someField > other.someField) return 1;
if (this.someField < other.someField) return -1;
return 0;
}
As to parameter shadowing (e.g. in constructors): I usually give those a shorter name of the corresponding field, such as:
class Rect {
private int width, height;
public Rect(int w, int h) {
width = w;
height = h;
}
}
Basically, this gives you a reference to the current object. You can use it to access members on the object, or to pass the current object as parameters into other methods.
It is entirely unnecessary in almost all cases to place it before accessing member variables or method calls, although some style guidelines recommend it for various reasons.
Personally, I make sure I name my member variables to be clearly different from my parameters to avoid ever having to use 'this.'. For example:
private String _someData;
public String SomeData
{
get{return _someData;}
set{_someData = value;}
}
It's very much an individual preference though, and some people will recommend that you name the property and member variable the same (just case difference - 'someData' and 'SomeData') and use the this keyword when accessing the private member to indicate the difference.
So as for a rule of thumb - Avoid using it. If you find yourself using it to distinguish between local/parameters variables and member variables then rename one of them so you don't have to use 'this'.
The cases where I would use it are multiple constructors, passing a reference to other methods and in extension methods. (See Jon's answer for examples)
If you have a method inside a class which uses same class's fields, you can use this.
public class FullName
{
public string fn { set; get; }
public string sn { set; get; }
//overriding Equals method
public override bool Equals(object obj)
{
if (!(obj is FullName))
return false;
if (obj == null)
return false;
return this.fn == ((FullName)obj).fn &&
this.sn == ((FullName)obj).sn;
}
//overriding GetHashCode
public override int GetHashCode()
{
return this.fn.GetHashCode() ^ this.sn.GetHashCode();
}
}