Skip to content

Commit

Permalink
Use preprocessor to inline true and false
Browse files Browse the repository at this point in the history
  • Loading branch information
texodus committed Sep 26, 2021
1 parent 0448699 commit 2af96ad
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 68 deletions.
14 changes: 14 additions & 0 deletions packages/perspective/src/js/perspective.js
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,20 @@ export default function (Module) {
expression_alias = expression_string;
}

// Replace `true` and `false` reserved words with symbols
parsed_expression_string = parsed_expression_string.replace(
/([a-zA-Z_]+[a-zA-Z0-9_]*)/g,
(match) => {
if (match == "true") {
return "True";
} else if (match == "false") {
return "False";
} else {
return match;
}
}
);

parsed_expression_string = parsed_expression_string.replace(
/\"(.*?[^\\])\"/g,
(_, cname) => {
Expand Down
56 changes: 28 additions & 28 deletions packages/perspective/test/js/expressions/functionality.js
Original file line number Diff line number Diff line change
Expand Up @@ -837,16 +837,16 @@ module.exports = (perspective) => {
);
const view = await table.view({
expressions: [
`var x := True; var y := False; x and y ? 1 : 0`,
`var x := True; var y := False; x or y`,
`var x := true; var y := false; x and y ? 1 : 0`,
`var x := true; var y := false; x or y`,
],
});

const results = await view.to_columns();
expect(
results[`var x := True; var y := False; x and y ? 1 : 0`]
results[`var x := true; var y := false; x and y ? 1 : 0`]
).toEqual([0, 0, 0, 0]);
expect(results["var x := True; var y := False; x or y"]).toEqual([
expect(results["var x := true; var y := false; x or y"]).toEqual([
true,
true,
true,
Expand Down Expand Up @@ -1117,8 +1117,8 @@ module.exports = (perspective) => {
expressions: [
"0 and 1",
"0 or 1",
"True and True",
"False or True",
"true and true",
"false or true",
],
});
expect(await view.schema()).toEqual({
Expand All @@ -1128,14 +1128,14 @@ module.exports = (perspective) => {
z: "boolean",
"0 and 1": "boolean",
"0 or 1": "boolean",
"True and True": "boolean",
"False or True": "boolean",
"true and true": "boolean",
"false or true": "boolean",
});
const result = await view.to_columns();
expect(result["0 and 1"]).toEqual([false, false, false, false]);
expect(result["0 or 1"]).toEqual([true, true, true, true]);
expect(result["True and True"]).toEqual([true, true, true, true]);
expect(result["False or True"]).toEqual([true, true, true, true]);
expect(result["true and true"]).toEqual([true, true, true, true]);
expect(result["false or true"]).toEqual([true, true, true, true]);
view.delete();
table.delete();
});
Expand All @@ -1148,8 +1148,8 @@ module.exports = (perspective) => {
expressions: [
"0 and 1",
"0 or 1",
"True and True",
"False or True",
"true and true",
"false or true",
],
});
expect(await view.schema()).toEqual({
Expand All @@ -1159,14 +1159,14 @@ module.exports = (perspective) => {
z: "boolean",
"0 and 1": "boolean",
"0 or 1": "boolean",
"True and True": "boolean",
"False or True": "boolean",
"true and true": "boolean",
"false or true": "boolean",
});
const result = await view.to_columns();
expect(result["0 and 1"]).toEqual([false, false, false, false]);
expect(result["0 or 1"]).toEqual([true, true, true, true]);
expect(result["True and True"]).toEqual([true, true, true, true]);
expect(result["False or True"]).toEqual([true, true, true, true]);
expect(result["true and true"]).toEqual([true, true, true, true]);
expect(result["false or true"]).toEqual([true, true, true, true]);
view.delete();
table.delete();
});
Expand Down Expand Up @@ -1195,10 +1195,10 @@ module.exports = (perspective) => {
expressions: [
'"x" and 1',
'"x" or 1',
'"x" and True',
'"x" and False',
'"x" or True',
'"x" or False',
'"x" and true',
'"x" and false',
'"x" or true',
'"x" or false',
],
});
expect(await view.schema()).toEqual({
Expand All @@ -1208,23 +1208,23 @@ module.exports = (perspective) => {
z: "boolean",
'"x" and 1': "boolean",
'"x" or 1': "boolean",
'"x" and True': "boolean",
'"x" and False': "boolean",
'"x" or True': "boolean",
'"x" or False': "boolean",
'"x" and true': "boolean",
'"x" and false': "boolean",
'"x" or true': "boolean",
'"x" or false': "boolean",
});
const result = await view.to_columns();
expect(result['"x" and 1']).toEqual([true, true, true, true]);
expect(result['"x" or 1']).toEqual([true, true, true, true]);
expect(result['"x" and True']).toEqual([true, true, true, true]);
expect(result['"x" and False']).toEqual([
expect(result['"x" and true']).toEqual([true, true, true, true]);
expect(result['"x" and false']).toEqual([
false,
false,
false,
false,
]);
expect(result['"x" or True']).toEqual([true, true, true, true]);
expect(result['"x" or False']).toEqual([true, true, true, true]);
expect(result['"x" or true']).toEqual([true, true, true, true]);
expect(result['"x" or false']).toEqual([true, true, true, true]);
view.delete();
table.delete();
});
Expand Down
38 changes: 19 additions & 19 deletions packages/perspective/test/js/expressions/numeric.js
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,7 @@ module.exports = (perspective) => {
"0 and 0",
"1 and 1",
"1 and 100",
"True and False",
"true and false",
],
});
const result = await view.to_columns();
Expand All @@ -1201,7 +1201,7 @@ module.exports = (perspective) => {
expect(result["0 and 0"]).toEqual([false, false, false, false]);
expect(result["1 and 1"]).toEqual([true, true, true, true]);
expect(result["1 and 100"]).toEqual([true, true, true, true]);
expect(result["True and False"]).toEqual([
expect(result["true and false"]).toEqual([
false,
false,
false,
Expand All @@ -1216,7 +1216,7 @@ module.exports = (perspective) => {
const view = await table.view({
expressions: [
'mand("u" and "u", "u" and "z", "z" and "z")',
"mand(True, True, True, True)",
"mand(true, true, true, true)",
"mand(is_null(null), is_not_null(null))",
],
});
Expand All @@ -1230,7 +1230,7 @@ module.exports = (perspective) => {
result["mand(is_null(null), is_not_null(null))"]
).toEqual(Array(4).fill(false));

expect(result["mand(True, True, True, True)"]).toEqual(
expect(result["mand(true, true, true, true)"]).toEqual(
Array(4).fill(true)
);

Expand Down Expand Up @@ -1284,8 +1284,8 @@ module.exports = (perspective) => {
'"c" or "d"',
'"e" or "f"',
"0 or 1",
"True or False",
"False or False",
"true or false",
"false or false",
'// filtered\n"a" > 0.5 or "d" < 0.5',
],
});
Expand All @@ -1299,13 +1299,13 @@ module.exports = (perspective) => {
expect(result['"c" or "d"']).toEqual([true, true, true, true]);
expect(result['"e" or "f"']).toEqual([true, true, true, true]);
expect(result["0 or 1"]).toEqual([true, true, true, true]);
expect(result["True or False"]).toEqual([
expect(result["true or false"]).toEqual([
true,
true,
true,
true,
]);
expect(result["False or False"]).toEqual([
expect(result["false or false"]).toEqual([
false,
false,
false,
Expand All @@ -1321,24 +1321,24 @@ module.exports = (perspective) => {
const view = await table.view({
expressions: [
'mor("u" and "u", "u" and "z", "z" and "z")',
"mor(False, False, False)",
"mor(False, True, False)",
"mor(false, false, false)",
"mor(false, true, false)",
],
});
const result = await view.to_columns();
expect(
result['mor("u" and "u", "u" and "z", "z" and "z")']
).toEqual([true, true, true, true]);

// The boolean False is a False
expect(result["mor(False, False, False)"]).toEqual([
// The boolean false is a false
expect(result["mor(false, false, false)"]).toEqual([
false,
false,
false,
false,
]);

expect(result["mor(False, True, False)"]).toEqual([
expect(result["mor(false, true, false)"]).toEqual([
true,
true,
true,
Expand Down Expand Up @@ -1390,7 +1390,7 @@ module.exports = (perspective) => {
"0 nand 0",
"1 nand 1",
"1 nand 100",
"True nand True",
"true nand true",
],
});
const result = await view.to_columns();
Expand Down Expand Up @@ -1425,7 +1425,7 @@ module.exports = (perspective) => {
false,
false,
]);
expect(result["True nand True"]).toEqual([
expect(result["true nand true"]).toEqual([
false,
false,
false,
Expand All @@ -1450,7 +1450,7 @@ module.exports = (perspective) => {
'"c" nor "d"',
'"e" nor "f"',
"0 nor 1",
"False nor False",
"false nor false",
],
});
const result = await view.to_columns();
Expand All @@ -1473,7 +1473,7 @@ module.exports = (perspective) => {
false,
]);
expect(result["0 nor 1"]).toEqual([false, false, false, false]);
expect(result["False nor False"]).toEqual([
expect(result["false nor false"]).toEqual([
true,
true,
true,
Expand All @@ -1498,7 +1498,7 @@ module.exports = (perspective) => {
'"c" xor "d"',
'"e" xor "f"',
"0 xor 1",
"False xor False",
"false xor false",
],
});
const result = await view.to_columns();
Expand All @@ -1511,7 +1511,7 @@ module.exports = (perspective) => {
expect(result['"c" xor "d"']).toEqual([true, true, true, true]);
expect(result['"e" xor "f"']).toEqual([true, true, true, true]);
expect(result["0 xor 1"]).toEqual([true, true, true, true]);
expect(result["False xor False"]).toEqual([
expect(result["false xor false"]).toEqual([
false,
false,
false,
Expand Down
12 changes: 6 additions & 6 deletions packages/perspective/test/js/expressions/updates.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ module.exports = (perspective) => {
// don't work as of yet.
expressions: [
'if ("x" > 4) 10; else 100',
`"y" == 'A' ? True : False`,
`"y" == 'A' ? true : false`,
],
});
const before = await view.to_columns();
expect(before['if ("x" > 4) 10; else 100']).toEqual([
100, 100, 100, 10,
]);
expect(before[`"y" == 'A' ? True : False`]).toEqual([
expect(before[`"y" == 'A' ? true : false`]).toEqual([
true,
false,
false,
Expand All @@ -119,7 +119,7 @@ module.exports = (perspective) => {
expect(after['if ("x" > 4) 10; else 100']).toEqual([
100, 100, 100, 10, 10, 10, 10,
]);
expect(after[`"y" == 'A' ? True : False`]).toEqual([
expect(after[`"y" == 'A' ? true : false`]).toEqual([
true,
false,
false,
Expand Down Expand Up @@ -298,14 +298,14 @@ module.exports = (perspective) => {
const view = await table.view({
expressions: [
'if ("x" > 4) 10; else 100',
`"z" == 'a' ? True : False`,
`"z" == 'a' ? true : false`,
],
});
const before = await view.to_columns();
expect(before['if ("x" > 4) 10; else 100']).toEqual([
100, 100, 100, 10,
]);
expect(before[`"z" == 'a' ? True : False`]).toEqual([
expect(before[`"z" == 'a' ? true : false`]).toEqual([
true,
false,
false,
Expand All @@ -321,7 +321,7 @@ module.exports = (perspective) => {
expect(after['if ("x" > 4) 10; else 100']).toEqual([
10, 100, 10, 10,
]);
expect(after[`"z" == 'a' ? True : False`]).toEqual([
expect(after[`"z" == 'a' ? true : false`]).toEqual([
true,
false,
true,
Expand Down
9 changes: 9 additions & 0 deletions python/perspective/perspective/table/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
EXPRESSION_COLUMN_NAME_REGEX = re.compile(r"\"(.*?[^\\])\"")
STRING_LITERAL_REGEX = re.compile(r"'(.*?[^\\])'")
BUCKET_LITERAL_REGEX = re.compile(r"bucket\(.*?, (intern\(\'([smhDWMY])\'\))\)")
BOOLEAN_LITERAL_REGEX = re.compile(r"([a-zA-Z_]+[a-zA-Z0-9_]*)")


def _extract_type(type, typemap):
Expand Down Expand Up @@ -183,6 +184,14 @@ def _parse_expression_strings(expressions):
running_cidx,
)

parsed = re.sub(
BOOLEAN_LITERAL_REGEX,
lambda match: "true"
if match == "True"
else ("false" if match == "False" else match),
parsed,
)

parsed = re.sub(EXPRESSION_COLUMN_NAME_REGEX, replacer_fn, parsed)
parsed = re.sub(
STRING_LITERAL_REGEX,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_view_expression_schema_all_types(self):
'"a"',
'"b" * 0.5',
"'abcdefg'",
"True and False",
"true and false",
'float("a") > 2 ? null : 1',
"today()",
"now()",
Expand All @@ -43,7 +43,7 @@ def test_view_expression_schema_all_types(self):
'"a"': int,
'"b" * 0.5': float,
"'abcdefg'": str,
"True and False": bool,
"true and false": bool,
'float("a") > 2 ? null : 1': float,
"today()": date,
"now()": datetime,
Expand All @@ -59,7 +59,7 @@ def test_view_expression_schema_all_types(self):
'"a"': [1, 2, 3, 4],
'"b" * 0.5': [2.5, 3, 3.5, 4],
"'abcdefg'": ['abcdefg' for _ in range(4)],
"True and False": [False for _ in range(4)],
"true and false": [False for _ in range(4)],
'float("a") > 2 ? null : 1': [1, 1, None, None],
"today()": [today for _ in range(4)],
"length('abcd')": [4 for _ in range(4)]
Expand Down
Loading

0 comments on commit 2af96ad

Please sign in to comment.