In Hive, what does the 'positive' function do? - hive

I'm working on an item-based recommender using data stored in Hive tables and stumbled across a similar scenario in Sagar Prasad's blog. I notice that he uses the POSITIVE function on the hashes of the user/product.
I'm a bit confused as to why this function exists. The documentation states that this function takes an int or double and returns that value:
"positive(INT/DOUBLE a) returns a"
For example:
hive> select positive(-1);
-1
Does that mean that the function doesn't do anything? Or am I missing some subtle nuance?

Under the hood this function is GenericUDFOPPositive.java which extends GenericUDFBaseUnary.java. positive can also be called simply as +.
For numeric types, this function does indeed do effectively nothing. For text types, it appears to attempt to do a conversion to double, since GenericUDFBaseUnary.java contains the following:
private PrimitiveTypeInfo deriveResultTypeInfo(PrimitiveTypeInfo typeInfo) {
switch(typeInfo.getPrimitiveCategory()) {
case STRING:
case VARCHAR:
case CHAR:
return TypeInfoFactory.doubleTypeInfo;
default:
return typeInfo;
}
}
However this doesn't seem to actually work, as calling positive on a string simply returns the string. This implies that at the least, the type signature of this function in the documentation is wrong, as it accepts more than just double and int.
Don't worry about being confused, having read the source code to try and work out what this function is for, I'm also now confused about why it exists!
https://github.com/apache/hive/blob/release-0.14.0/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFOPPositive.java
https://github.com/apache/hive/blob/release-0.14.0/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBaseUnary.java

Related

PHP 7.4 Typed property with floats / doubles

I apologise in advance if this is a lame question, Its my first.
I am building a small framework for a university project and I wanted to enforce types as much as possible, Great to see now that PHP 7.4 has strict types for properties,
But it is not being properly enforced even with declare strict_types.
Also on a side note, I know people say in PHP there is no difference between doubles and floats, but with typed properties, PHP does not recognise double as a data type.
See the simple code test below:
class FloatTest
{
private float $float;
private int $int;
function __construct()
{
}
public function setFloat(float $float):void
{
$this->float = $float;
}
public function getFloat()
{
return $this->float;
}
public function setInt(int $int):void
{
$this->int = $int;
}
public function getInt():int
{
return $this->int;
}
}
$ft = new FloatTest();
$ft->setFloat(8);//No error is thrown, converts to float but no decimals
$ft->getFloat();// Returns number 8 as float, but there is no enforcing of decimal point?
var_dump(is_float(8));//returns false
//Argument 1 passed to FloatTest::setInt() must be of the type int, float given
$ft->setInt(8.2);//Works as expected, great!
class DoubleTest
{
private double $double;
function __construct()
{
# code...
}
public function setDouble(double $double):void
{
$this->double = $double;
}
public function getDouble():double
{
return $this->double;
}
}
$dt = new DoubleTest();
//Argument 1 passed to DoubleTest::setDouble() must be an instance of double, int given:
$dt->setDouble(8);
$double = $dt->getDouble();
var_dump(is_double(8)); returns false
Based on this very simple test I have a few points which I find strange:
Why is PHP correctly enforcing the int type but not the float type?
Why is it that when I check with is_float() it returns false but the function accepts an integer?
Why is the integer type perfectly enforced but not the float?
Even though PHP has a valid double data type, why does it assume double is an instance?
double is definitely a primitive data type within PHP, as the is_double() function works perfectly,
See exception thrown above.
Basically what would be the best, cleanest work around to enforcing decimal numbers in PHP?
Why is PHP correctly enforcing the int type but not the float type?
Why is it that when I check with is_float() it returns false but the
function accepts an integer?
Because float range can contain integers without losing any data. In other words, float is like a superset of integers, so you can't say I only want floats i don't want integers even in strongly typed languages and even if your code is in strict_types mode.
Why is the integer type perfectly enforced but not the float?
PHP type declaration comes with coercive mode as default mode, so it's possible to change the declared type and no TypeError will be thrown only if it's possible for PHP to coerce values of the wrong type into the expected ones.
In your case you are passing 8(integer) to a method that expects a float, this is totally fine and PHP will not complain about it because coercing 8 to a float will not change anything/lose anything.
So what does strict_types do then ?
It will change the behavior of PHP from coercive to strict. That means PHP will not be tolerant when an involved operation could lead to data loss.
By taking your example when we set declare(strict_types=1) the following line will be a problem
$ft->setInt(8.2);//Works as expected, great!
Because we are trying to pass a float number to an int parameter which means a data loss (8.2 becomes 8 ) PHP will throw an exception
Fatal error: Uncaught TypeError: Argument 1 passed to
FloatTest::setInt() must be of the type int, float given
Even though PHP has a valid double data type, why does it assume
double is an instance? double is definitely a primitive data type
within PHP, as the is_double() function works perfectly
PHP has no double data type and is_double is just an alias of is_float()
Basically what would be the best, cleanest work around to enforcing
decimal numbers in PHP?
What you did is the best and cleanest work :)

Could someone, please, explain me the implementation of the following "Kotlin Literal high order function"?

I am a newbie in Kotlin, I just started to learn it,
I get the following code example about literal/high order function:
fun myHigherOrderFun(functionArg: (Int)->String) = functionArg(5)
println ( myHigherOrderFun { "The Number is $it" })
prints "The Number is 5"
Which I have difficulty to understand: the function myHigherOrderFun get a lambda function as parameter but i can't understand, where is the (Int) input parameter? I see is passed in functionArg(5)... but i can't realize how is possible that?
Thanks in advance.
To start from the beginning, in Kotlin functions are first-class types, just like numbers and Strings and stuff.  So a function can take another function as a parameter, and/or return a function as its result.  A function which does this is called a ‘higher-order function’.
And that's what you have in your example!  The line:
fun myHigherOrderFun(functionArg: (Int)->String) = functionArg(5)
defines myHigherOrderFun() as a function which takes one parameter, which is itself a function taking a single Int parameter and returning a String.  (myHigherOrderFun() doesn't specify an explicit return type, so it's inferred to be a String too.)
The next line is probably where things are less clear:
println(myHigherOrderFun{ "The Number is $it" })
The first non-obvious thing is that it's calling myHigherOrderFun() with a parameter.  Because that parameter is a lambda, Kotlin lets you omit the usual (…), and use only the braces.
The other non-obvious thing is the lambda itself: { "The Number is $it" }. This is a literal function taking one parameter (of unspecified type).
Normally, you'd have to specify any parameters explicitly, e.g.: { a: Char, b: Int -> /* … */ }.  But if there's exactly one parameter, and you aren't specifying its type, then you can skip that and just refer to the parameter as it.  That's what's happening here.
(If the lambda didn't reference it, then it would be a function taking no parameters at all.)
And because the lambda is being passed to something expecting a function taking an Int parameter, Kotlin knows that it must be an Int, which is why we can get away without specifying that.
So, Kotlin passes that lambda to the myHigherOrderFun(), which executes the lambda, passing 5 as it.  That interpolates it into a string, which it returns as the argument to println().
Many lambdas take a single parameter, so it gets used quite a lot in Kotlin; it's more concise (and usually more readable) than the alternative.  See the docs for more info.

How to construct a Complex from a String using Python's C-API?

How to use the Python C-API for the Complex class (documented here) to:
convert a general PyObject (which might be a String, Long, Float, Complex) into a Complex PyObject?
convert a Complex PyObject into String PyObject?
Python has a complex() function, documented here):
Return a complex number with the value real + imag*j or convert a
string or number to a complex number. If the first parameter is a
string, it will be interpreted as a complex number and the function
must be called without a second parameter. The second parameter can
never be a string. Each argument may be any numeric type (including
complex). If imag is omitted, it defaults to zero and the function
serves as a numeric conversion function like int(), long() and
float(). If both arguments are omitted, returns 0j.
However, it isn't obvious which API function (if any) is backing it.
It would appear none of them, is the above paragraph talks about two PyObject* parameters, and none of the API functions listed match that signature.
When in doubt, do what Python does: call the constructor.
PyObject *c1 = PyObject_CallFunction(PyComplex_Type, "s", "1+2j");
If (!c1)
return NULL;

Passing parameters of varying Types

I am using a procedure which involves parameter passing and the parameter being passed is a variable. Because I have explicitly declared the data type of another parameter, I need to do the same for this one. What data type do I declare the parameter as if it is a variable?
Thanks
An example of what you are doing and what Types you are dealing with would have been nice. You can implement Overloading to provide for different parameter Types:
Friend Function FooBar(n As Integer) As Integer
Friend Function FooBar(n As Int64) As Integer
Friend Function FooBar(n As Short) As Integer
The compiler will pick the function which matches the data type being passed. Internally, they might do whatever based on the Type passed, then call another procedure to perform any stuff common to them all.
There is probably a finite number of types you need it to work with. For instance Font, Point and Rectangle probably make no sense. Even Date is dubious because you cannot do stuff to a date in the same way as with an Int or Long. String is also not likely needed because you can always pass it as FooBar(CInt(someString)) provided it does contain a valid integer or whatever.
You can also use a generic to tell the function what you are passing:
Private Function FooBar(Of T)(parm As T) As Integer
' called as:
ziggy = FooBar(Of Int32)(n)
zoey = FooBar(Of String)(str)
This might even be Private Function FooBar(Of T)(parm As T) As T if the function return varies depending on the parameter Type passed. There are many uses for this (one of which is to avoid passing a param as Object), but as a general purpose way of passing any type you want it is not a good idea: internally you will likely have to have a big If/Else to handle the different types their own way.
Turning off Option Strict is never advisable since all sorts of unwanted type conversions can take place.
In VB.NET, you can use Object as the type but with Option Strict Off. You can pass any kind of parameter in that case.
for more information, refer : https://stackoverflow.com/a/2890023/3660930

Exclamation operator?

I'm learning D and have seen a lot of code like this:
ushort x = to!ushort(args[1]);
I assume this casts args[1] to ushort, but what's the difference between this and cast(ushort)?
EDIT: And what other uses does the exclamation mark operator have?
In D,
to!ushort(args[1])
is shorthand for the template instantiation
to!(ushort)(args[1])
and is similar to
to<ushort>(args[1])
in languages like C++/Java/C#.
The exclamation point is to note the fact that it's not a regular argument, but a template argument.
The notation does not use angle brackets because those are ridiculously difficult to parse correctly for a compiler (they make the grammar very context-sensitive), which makes it that much more difficult to implement a correct compiler. See here for more info.
The only other use I know about is just the unary 'not' operation (e.g. false == !true)... I can't think of any other uses at the moment.
Regarding the cast:
cast(ushort) is an unchecked cast, so it won't throw an exception if the value is out of range.
to!ushort() is a checked cast, so it throws an exception if the value is out of range.
The exclamation mark here is not an operator, it is just a token part of the explicit template instantiation syntax (described in detail here).
std.conv.to (docs) is a function template for converting between arbitrary types. It is implemented entirely in the library and has no special support in the language. It has a broader and different scope compared to the cast operator.
The to template takes two type parameters; a "to" type and a "from" type, in that order. In your example, the template is explicitly instantiated with the single type argument ushort for the "to" parameter, and a second type argument string (assuming args comes from the first parameter to main) is automatically inferred from the regular function argument passed to the function (args[1]) as the "from" parameter.
The resulting function takes a string parameter and returns a ushort parsed from that string, or throws an exception if it failed. The cast operator will not attempt this kind of high-level conversion.
Note that if there is more than one explicit template parameter, or that parameter has more than one token in it (ushort is a single keyword token), you must wrap the template parameter list in parentheses:
ushort result;
result = to!(typeof(result))(args[1]);
In this example, typeof, (, result and ) are four separate tokens and the parentheses are thus required.
To answer your last question, the ! token is also used for the unary not operator, unrelated to template instantiations:
bool yes = true;
bool no = !yes; // 'no' is false
You already got two excellent answers by jA_cOp and Merhdad. I just want answer directly to the OP question (what's the difference between this and cast(ushort)?) - The difference is that cast(ushort)args[1] will not work (you cannot cast from a string to an uint just like that), while the to!(type)(param) template knows what to do with the string and how to convert it to the primitive type.