Skip to content

Commit

Permalink
Adjust "||" behavior to ECMAScript
Browse files Browse the repository at this point in the history
"x || y" returns the first not false value. If x is not false value,
"x || y" returns "x" itself not "true". If x is false value and y is
not false value, "x || y" returns "y" itself not "true".

If both "x" and "y" are false value, "x || y" returns "false". It is
not changed.

Before:

    null || "string value" -> true
    null || null           -> false

After:

    null || "string value" -> "string value" (changed)
    null || null           -> false          (not changed)
  • Loading branch information
kou committed Jan 26, 2014
1 parent cbfff72 commit c007a68
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
25 changes: 18 additions & 7 deletions lib/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -3155,17 +3155,28 @@ grn_expr_exec(grn_ctx *ctx, grn_obj *expr, int nargs)
{
grn_obj *x, *y;
unsigned int x_boolean, y_boolean;
int result;
grn_obj *result;
POP2ALLOC1(x, y, res);
GRN_TRUEP(ctx, x, x_boolean);
GRN_TRUEP(ctx, y, y_boolean);
if (x_boolean || y_boolean) {
result = 1;
if (x_boolean) {
result = x;
} else {
result = 0;
GRN_TRUEP(ctx, y, y_boolean);
if (y_boolean) {
result = y;
} else {
result = NULL;
}
}
if (result) {
if (res != result) {
grn_obj_reinit(ctx, res, result->header.domain, 0);
grn_obj_cast(ctx, result, res, GRN_FALSE);
}
} else {
grn_obj_reinit(ctx, res, GRN_DB_BOOL, 0);
GRN_BOOL_SET(ctx, res, GRN_FALSE);
}
grn_obj_reinit(ctx, res, GRN_DB_INT32, 0);
GRN_INT32_SET(ctx, res, result);
}
code++;
break;
Expand Down
11 changes: 11 additions & 0 deletions test/command/suite/select/filter/logical_operation/or.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
table_create Users TABLE_NO_KEY
[[0,0.0,0.0],true]
column_create Users name COLUMN_SCALAR ShortText
[[0,0.0,0.0],true]
load --table Users
[
{"name": "Alice"}
]
[[0,0.0,0.0],1]
select Users --output_columns '_id, null || name || "unknown"' --command_version 2
[[0,0.0,0.0],[[[1],[["_id","UInt32"],["","null"]],[1,"Alice"]]]]
11 changes: 11 additions & 0 deletions test/command/suite/select/filter/logical_operation/or.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
table_create Users TABLE_NO_KEY
column_create Users name COLUMN_SCALAR ShortText

load --table Users
[
{"name": "Alice"}
]

select Users \
--output_columns '_id, null || name || "unknown"' \
--command_version 2

0 comments on commit c007a68

Please sign in to comment.