Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added proper handling of int and float for math op #4257

Merged
merged 9 commits into from
Nov 19, 2019

Conversation

harshil-goel
Copy link
Contributor

@harshil-goel harshil-goel commented Nov 8, 2019

Fixes #4132


This change is Reviewable

a.Tid = types.FloatID
*res = *a
return nil
} else {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if block ends with a return statement, so drop this else and outdent its block (from golint)

@harshil-goel harshil-goel marked this pull request as ready for review November 11, 2019 11:22
Copy link
Member

@mangalaman93 mangalaman93 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 8 files reviewed, 9 unresolved discussions (waiting on @harshil-goel, @mangalaman93, @manishrjain, and @pawanrawal)


gql/math.go, line 249 at r2 (raw file):

				continue
			}
			// We will try to parse it as an Int first, if that fails we move to float

Merge these two comments.


gql/math.go, line 365 at r2 (raw file):

		// Leaf node.
		if t.Const.Tid == types.FloatID {
			x.Check2(buf.WriteString(strconv.FormatFloat(t.Const.Value.(float64), 'E', -1, 64)))

You can assign in the if and then, call check2 just once.


query/aggregator.go, line 186 at r2 (raw file):

func ApplyFloatMod(a, b, c *types.Val) error {
	if b.Value.(float64) == 0 {
		return errors.Errorf("Division by zero")

Shouldn't this be module by zero error


query/aggregator.go, line 193 at r2 (raw file):

func ApplyOtherMod(a, b, c *types.Val) error {
	return errors.Errorf("Wrong type encountered for func %s", "%")

Errorf seems unnecessary


query/aggregator.go, line 264 at r2 (raw file):

func ApplyLnOther(a, res *types.Val) error {
	return errors.Errorf("Wrong type encountered for func ln")

These errors could be defined in one place for all functions and then reused.


query/aggregator.go, line 352 at r2 (raw file):

type MathBinaryFunction func(a, b, res *types.Val) error

var supportedUnaryFunctions = map[string][]MathUnaryFunction{

can use a value type as array instead of slice


query/aggregator.go, line 352 at r2 (raw file):

type MathBinaryFunction func(a, b, res *types.Val) error

var supportedUnaryFunctions = map[string][]MathUnaryFunction{

Could just call the variable unaryFuncs


query/aggregator.go, line 451 at r2 (raw file):

	}

	if function, ok := supportedBinaryFunctions[ag.name]; ok {

Why are we checking the presence of function in the map here but not above?

@mangalaman93 mangalaman93 self-requested a review November 11, 2019 12:01
Copy link
Contributor

@pawanrawal pawanrawal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice work :lgtm:

I have a comment about the structure of functions, please discuss with @mangalaman93 and see what he thinks about it.

Reviewable status: 0 of 8 files reviewed, 11 unresolved discussions (waiting on @harshil-goel, @mangalaman93, @manishrjain, and @pawanrawal)


query/aggregator.go, line 173 at r2 (raw file):

func ApplyOtherDiv(a, b, c *types.Val) error {
	return errors.Errorf("Wrong type encountered for func /")

Can you also please add the name of the type in the error message and the value to make it easier for the user to debug?


query/aggregator.go, line 362 at r2 (raw file):

}

var supportedBinaryFunctions = map[string][]MathBinaryFunction{

Interesting choice of defining a global variable here and defining separate for functions for different types Int, Float, Other.

I would have considered collecting all functions related to an operation in one function, so ApplyIntAdd, ApplyFloatAdd and ApplyOtherAdd all in one function in a switch case based on the type, since it would have led to lesser functions and can help us get rid of global variables. Most of these functions are one liners anyway.

Copy link
Contributor

@manishrjain manishrjain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm: Bunch of changes to do here. Reduce the number of funcs and corresponding changes to map. Do get someone to look again just before merging. @shekarm can do the final review.

Reviewed 1 of 3 files at r1, 7 of 7 files at r2.
Reviewable status: all files reviewed, 18 unresolved discussions (waiting on @harshil-goel)


query/aggregator.go, line 196 at r2 (raw file):

}

func ApplyIntPow(a, b, c *types.Val) error {

Please make all of them start with small case.


query/aggregator.go, line 297 at r2 (raw file):

func ApplySqrtInt(a, res *types.Val) error {
	res.Value = math.Sqrt(float64(a.Value.(int64)))

ApplySqrt()

switch case {
float:
int:
default:
}


Then, your map can just be map of string to func, instead of a map of list. Pass the vbase to the func, which it uses to switch case.


query/aggregator.go, line 349 at r2 (raw file):

}

type MathUnaryFunction func(a, res *types.Val) error

Internal as well?


query/aggregator.go, line 394 at r2 (raw file):

}

func (ag *aggregator) MatchType(vBase, vaBase *valType, v, va *types.Val) error {

Can be an internal func.


query/aggregator.go, line 431 at r2 (raw file):

	if isUnary(ag.name) {
		res.Tid = v.Tid
		err := supportedUnaryFunctions[ag.name][vBase](&v, &res)

What if ag.name is not present in the list. Should error out instead of panicking.


query/aggregator.go, line 447 at r2 (raw file):

	vaBase := getValType(&va)

	if err := ag.MatchType(&vBase, &vaBase, &v, &va); err != nil {

vaBase can be derived internally. 4 args is kinda pushing it.


query/query4_test.go, line 26 at r2 (raw file):

)

func TestBigMathValue(t *testing.T) {

I'd add a few more tests.

Copy link

@shekarm shekarm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 7 files at r2, 7 of 7 files at r3.
Reviewable status: all files reviewed, 18 unresolved discussions (waiting on @harshil-goel)

@harshil-goel harshil-goel merged commit dc02af5 into master Nov 19, 2019
@harshil-goel harshil-goel deleted the harshil-goel/math-big-op branch November 19, 2019 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Math operations for int values fail for big values
6 participants