Skip to content

Commit

Permalink
feat(hog): loose comparisons (#24775)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra authored Sep 4, 2024
1 parent a9d32a4 commit 1646a08
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 26 deletions.
8 changes: 7 additions & 1 deletion hogvm/__tests__/__snapshots__/operations.hoge
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@
35, 29, 2, "toString", 1, 36, 0, 54, 1, 35, 31, 2, "toString", 1, 36, 0, 54, 1, 35, 32, "string", 2, "toString", 1, 36,
0, 54, 1, 35, 32, "1", 2, "toInt", 1, 36, 0, 54, 1, 35, 32, "bla", 2, "toInt", 1, 36, 0, 54, 1, 35, 32, "1.2", 2,
"toFloat", 1, 36, 0, 54, 1, 35, 32, "bla", 2, "toFloat", 1, 36, 0, 54, 1, 35, 32, "asd", 2, "toUUID", 1, 36, 0, 54, 1,
35, 31, 33, 1, 11, 36, 0, 54, 1, 35, 31, 33, 1, 12, 36, 0, 54, 1, 35, 35]
35, 31, 33, 1, 11, 36, 0, 54, 1, 35, 31, 33, 1, 12, 36, 0, 54, 1, 35, 33, 1, 32, "1", 11, 36, 0, 54, 1, 35, 32, "1", 33,
1, 11, 36, 0, 54, 1, 35, 29, 33, 1, 11, 36, 0, 54, 1, 35, 29, 33, 0, 11, 36, 0, 54, 1, 35, 29, 33, 2, 11, 36, 0, 54, 1,
35, 30, 33, 1, 12, 36, 0, 54, 1, 35, 32, "2", 33, 1, 11, 36, 0, 54, 1, 35, 32, "2", 33, 1, 11, 36, 0, 54, 1, 35, 32,
"2", 33, 1, 12, 36, 0, 54, 1, 35, 32, "2", 33, 1, 15, 36, 0, 54, 1, 35, 32, "2", 33, 1, 16, 36, 0, 54, 1, 35, 32, "2",
33, 1, 13, 36, 0, 54, 1, 35, 32, "2", 33, 1, 14, 36, 0, 54, 1, 35, 33, 2, 32, "1", 11, 36, 0, 54, 1, 35, 33, 2, 32, "1",
11, 36, 0, 54, 1, 35, 33, 2, 32, "1", 12, 36, 0, 54, 1, 35, 33, 2, 32, "1", 15, 36, 0, 54, 1, 35, 33, 2, 32, "1", 16,
36, 0, 54, 1, 35, 33, 2, 32, "1", 13, 36, 0, 54, 1, 35, 33, 2, 32, "1", 14, 36, 0, 54, 1, 35, 35]
20 changes: 20 additions & 0 deletions hogvm/__tests__/__snapshots__/operations.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,23 @@ null
"asd"
false
true
true
true
true
false
false
true
false
false
true
true
true
false
false
false
false
true
true
true
false
false
20 changes: 20 additions & 0 deletions hogvm/__tests__/operations.hog
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,23 @@ test(toFloat('bla')) // null
test(toUUID('asd')) // 'asd'
test(1 == null) // false
test(1 != null) // true
test('1' = 1) // true
test(1 = '1') // true
test(1 == true) // true
test(0 == true) // false
test(2 == true) // false
test(1 != false) // true
test(1 = '2') // false
test(1 == '2') // false
test(1 != '2') // true
test(1 < '2') // true
test(1 <= '2') // true
test(1 > '2') // false
test(1 >= '2') // false
test('1' = 2) // false
test('1' == 2) // false
test('1' != 2) // true
test('1' < 2) // true
test('1' <= 2) // true
test('1' > 2) // false
test('1' >= 2) // false
19 changes: 13 additions & 6 deletions hogvm/python/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
like,
set_nested_value,
calculate_cost,
unify_comparison_types,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -201,17 +202,23 @@ def capture_upvalue(index) -> dict:
case Operation.MOD:
push_stack(pop_stack() % pop_stack())
case Operation.EQ:
push_stack(pop_stack() == pop_stack())
var1, var2 = unify_comparison_types(pop_stack(), pop_stack())
push_stack(var1 == var2)
case Operation.NOT_EQ:
push_stack(pop_stack() != pop_stack())
var1, var2 = unify_comparison_types(pop_stack(), pop_stack())
push_stack(var1 != var2)
case Operation.GT:
push_stack(pop_stack() > pop_stack())
var1, var2 = unify_comparison_types(pop_stack(), pop_stack())
push_stack(var1 > var2)
case Operation.GT_EQ:
push_stack(pop_stack() >= pop_stack())
var1, var2 = unify_comparison_types(pop_stack(), pop_stack())
push_stack(var1 >= var2)
case Operation.LT:
push_stack(pop_stack() < pop_stack())
var1, var2 = unify_comparison_types(pop_stack(), pop_stack())
push_stack(var1 < var2)
case Operation.LT_EQ:
push_stack(pop_stack() <= pop_stack())
var1, var2 = unify_comparison_types(pop_stack(), pop_stack())
push_stack(var1 <= var2)
case Operation.LIKE:
push_stack(like(pop_stack(), pop_stack()))
case Operation.ILIKE:
Expand Down
16 changes: 16 additions & 0 deletions hogvm/python/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,19 @@ def calculate_cost(object, marked: set | None = None) -> int:
elif isinstance(object, str):
return COST_PER_UNIT + len(object)
return COST_PER_UNIT


def unify_comparison_types(left, right):
if isinstance(left, int | float) and isinstance(right, str):
return left, float(right)
if isinstance(left, str) and isinstance(right, int | float):
return float(left), right
if isinstance(left, bool) and isinstance(right, str):
return left, bool(right)
if isinstance(left, str) and isinstance(right, bool):
return bool(left), right
if isinstance(left, int | float) and isinstance(right, bool):
return left, int(right)
if isinstance(left, bool) and isinstance(right, int | float):
return int(left), right
return left, right
2 changes: 1 addition & 1 deletion hogvm/typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@posthog/hogvm",
"version": "1.0.43",
"version": "1.0.44",
"description": "PostHog Hog Virtual Machine",
"types": "dist/index.d.ts",
"source": "src/index.ts",
Expand Down
19 changes: 13 additions & 6 deletions hogvm/typescript/src/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
like,
setNestedValue,
UncaughtHogVMException,
unifyComparisonTypes,
} from './utils'

export function execSync(bytecode: any[], options?: ExecOptions): any {
Expand Down Expand Up @@ -280,22 +281,28 @@ export function exec(code: any[] | VMState, options?: ExecOptions): ExecResult {
pushStack(Number(popStack()) % Number(popStack()))
break
case Operation.EQ:
pushStack(popStack() === popStack())
;[temp, temp2] = unifyComparisonTypes(popStack(), popStack())
pushStack(temp === temp2)
break
case Operation.NOT_EQ:
pushStack(popStack() !== popStack())
;[temp, temp2] = unifyComparisonTypes(popStack(), popStack())
pushStack(temp !== temp2)
break
case Operation.GT:
pushStack(popStack() > popStack())
;[temp, temp2] = unifyComparisonTypes(popStack(), popStack())
pushStack(temp > temp2)
break
case Operation.GT_EQ:
pushStack(popStack() >= popStack())
;[temp, temp2] = unifyComparisonTypes(popStack(), popStack())
pushStack(temp >= temp2)
break
case Operation.LT:
pushStack(popStack() < popStack())
;[temp, temp2] = unifyComparisonTypes(popStack(), popStack())
pushStack(temp < temp2)
break
case Operation.LT_EQ:
pushStack(popStack() <= popStack())
;[temp, temp2] = unifyComparisonTypes(popStack(), popStack())
pushStack(temp <= temp2)
break
case Operation.LIKE:
pushStack(like(popStack(), popStack()))
Expand Down
22 changes: 22 additions & 0 deletions hogvm/typescript/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,25 @@ export function calculateCost(object: any, marked: Set<any> | undefined = undefi
}
return COST_PER_UNIT
}

export function unifyComparisonTypes(left: any, right: any): [any, any] {
if (typeof left === 'number' && typeof right === 'string') {
return [left, Number(right)]
}
if (typeof left === 'string' && typeof right === 'number') {
return [Number(left), right]
}
if (typeof left === 'boolean' && typeof right === 'string') {
return [left, right === 'true']
}
if (typeof left === 'string' && typeof right === 'boolean') {
return [left === 'true', right]
}
if (typeof left === 'boolean' && typeof right === 'number') {
return [left ? 1 : 0, right]
}
if (typeof left === 'number' && typeof right === 'boolean') {
return [left, right ? 1 : 0]
}
return [left, right]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"@medv/finder": "^3.1.0",
"@microlink/react-json-view": "^1.21.3",
"@monaco-editor/react": "4.6.0",
"@posthog/hogvm": "^1.0.43",
"@posthog/hogvm": "^1.0.44",
"@posthog/icons": "0.7.3",
"@posthog/plugin-scaffold": "^1.4.4",
"@react-hook/size": "^2.1.2",
Expand Down
2 changes: 1 addition & 1 deletion plugin-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@maxmind/geoip2-node": "^3.4.0",
"@posthog/clickhouse": "^1.7.0",
"@posthog/cyclotron": "file:../rust/cyclotron-node",
"@posthog/hogvm": "^1.0.43",
"@posthog/hogvm": "^1.0.44",
"@posthog/plugin-scaffold": "1.4.4",
"@sentry/node": "^7.49.0",
"@sentry/profiling-node": "^0.3.0",
Expand Down
8 changes: 4 additions & 4 deletions plugin-server/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1646a08

Please sign in to comment.