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

feat(hog): add isNull/isNotNull to hog #26973

Merged
merged 5 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion hogvm/__tests__/__snapshots__/stl.hoge
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@
29, 2, "notEmpty", 1, 2, "print", 1, 35, 30, 2, "notEmpty", 1, 2, "print", 1, 35, 32, "", 2, "print", 1, 35, 32,
"-- replaceAll, replaceOne --", 2, "print", 1, 35, 32, "hello world", 32, "l", 32, "L", 2, "replaceAll", 3, 2, "print",
1, 35, 32, "hello world", 32, "l", 32, "L", 2, "replaceOne", 3, 2, "print", 1, 35, 32, "", 2, "print", 1, 35, 32,
"-- generateUUIDv4 --", 2, "print", 1, 35, 2, "generateUUIDv4", 0, 2, "length", 1, 2, "print", 1, 35]
"-- generateUUIDv4 --", 2, "print", 1, 35, 2, "generateUUIDv4", 0, 2, "length", 1, 2, "print", 1, 35, 32, "", 2,
"print", 1, 35, 32, "-- isNull, isNotNull --", 2, "print", 1, 35, 31, 2, "isNull", 1, 31, 2, "isNotNull", 1, 2, "print",
2, 35, 29, 2, "isNull", 1, 29, 2, "isNotNull", 1, 2, "print", 2, 35, 32, "banana", 2, "isNull", 1, 32, "banana", 2,
"isNotNull", 1, 2, "print", 2, 35, 30, 2, "isNull", 1, 30, 2, "isNotNull", 1, 2, "print", 2, 35, 33, 0, 2, "isNull", 1,
33, 0, 2, "isNotNull", 1, 2, "print", 2, 35, 33, 1, 2, "isNull", 1, 33, 1, 2, "isNotNull", 1, 2, "print", 2, 35]
10 changes: 10 additions & 0 deletions hogvm/__tests__/__snapshots__/stl.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ function print (...args) { console.log(...args.map(__printHogStringOutput)) }
function notEmpty (value) { return !empty(value) }
function lower (value) { return value.toLowerCase() }
function length (value) { return value.length }
function isNull (value) { return value === null || value === undefined }
function isNotNull (value) { return value !== null && value !== undefined }
function generateUUIDv4 () { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { const r = (Math.random() * 16) | 0; const v = c === 'x' ? r : (r & 0x3) | 0x8; return v.toString(16) })}
function encodeURLComponent (str) { return encodeURIComponent(str) }
function empty (value) {
Expand Down Expand Up @@ -119,3 +121,11 @@ print(replaceOne("hello world", "l", "L"));
print("");
print("-- generateUUIDv4 --");
print(length(generateUUIDv4()));
print("");
print("-- isNull, isNotNull --");
print(isNull(null), isNotNull(null));
print(isNull(true), isNotNull(true));
print(isNull("banana"), isNotNull("banana"));
print(isNull(false), isNotNull(false));
print(isNull(0), isNotNull(0));
print(isNull(1), isNotNull(1));
8 changes: 8 additions & 0 deletions hogvm/__tests__/__snapshots__/stl.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,11 @@ heLlo world

-- generateUUIDv4 --
36

-- isNull, isNotNull --
true false
false true
false true
false true
false true
false true
9 changes: 8 additions & 1 deletion hogvm/__tests__/stl.hog
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,11 @@ print(replaceOne('hello world', 'l', 'L'))
print('')
print('-- generateUUIDv4 --')
print(length(generateUUIDv4()))

print('')
print('-- isNull, isNotNull --')
print(isNull(null), isNotNull(null))
print(isNull(true), isNotNull(true))
print(isNull('banana'), isNotNull('banana'))
print(isNull(false), isNotNull(false))
print(isNull(0), isNotNull(0))
print(isNull(1), isNotNull(1))
2 changes: 2 additions & 0 deletions hogvm/python/stl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,8 @@ def _typeof(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]]
"toInt": STLFunction(fn=toInt, minArgs=1, maxArgs=1),
"toFloat": STLFunction(fn=toFloat, minArgs=1, maxArgs=1),
"ifNull": STLFunction(fn=ifNull, minArgs=2, maxArgs=2),
"isNull": STLFunction(fn=lambda args, team, stdout, timeout: args[0] is None, minArgs=1, maxArgs=1),
"isNotNull": STLFunction(fn=lambda args, team, stdout, timeout: args[0] is not None, minArgs=1, maxArgs=1),
"length": STLFunction(fn=lambda args, team, stdout, timeout: len(args[0]), minArgs=1, maxArgs=1),
"empty": STLFunction(fn=empty, minArgs=1, maxArgs=1),
"notEmpty": STLFunction(
Expand Down
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.64",
"version": "1.0.65",
"description": "PostHog Hog Virtual Machine",
"types": "dist/index.d.ts",
"source": "src/index.ts",
Expand Down
14 changes: 14 additions & 0 deletions hogvm/typescript/src/stl/stl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ export const STL: Record<string, STLFunction> = {
minArgs: 2,
maxArgs: 2,
},
isNull: {
fn: (args) => {
return args[0] === null || args[0] === undefined
},
minArgs: 1,
maxArgs: 1,
},
isNotNull: {
fn: (args) => {
return args[0] !== null && args[0] !== undefined
},
minArgs: 1,
maxArgs: 1,
},
length: {
fn: (args) => {
return args[0].length
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"@microlink/react-json-view": "^1.21.3",
"@microsoft/fetch-event-source": "^2.0.1",
"@monaco-editor/react": "4.6.0",
"@posthog/hogvm": "^1.0.64",
"@posthog/hogvm": "^1.0.65",
"@posthog/icons": "0.9.2",
"@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 @@ -54,7 +54,7 @@
"@maxmind/geoip2-node": "^3.4.0",
"@posthog/clickhouse": "^1.7.0",
"@posthog/cyclotron": "file:../rust/cyclotron-node",
"@posthog/hogvm": "^1.0.64",
"@posthog/hogvm": "^1.0.65",
"@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.

8 changes: 4 additions & 4 deletions pnpm-lock.yaml

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

8 changes: 8 additions & 0 deletions posthog/hogql/compiler/javascript_stl.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@
"function ifNull (value, defaultValue) { return value !== null ? value : defaultValue } ",
[],
],
"isNull": [
"function isNull (value) { return value === null || value === undefined }",
[],
],
"isNotNull": [
"function isNotNull (value) { return value !== null && value !== undefined }",
[],
],
"length": [
"function length (value) { return value.length }",
[],
Expand Down
33 changes: 33 additions & 0 deletions posthog/management/commands/print_hog_stl_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Print compatibility table
from django.core.management.base import BaseCommand


class Command(BaseCommand):
help = "Print a Hog/HogQL STL compatibility table"

def handle(self, *args, **options):
from posthog.hogql.functions.mapping import HOGQL_CLICKHOUSE_FUNCTIONS, HOGQL_COMPARISON_MAPPING

hogql_functions = set(HOGQL_COMPARISON_MAPPING.keys()).union(set(HOGQL_CLICKHOUSE_FUNCTIONS.keys()))

from hogvm.python.stl import STL
from hogvm.python.stl.bytecode import BYTECODE_STL

hog_functions = set(STL.keys()).union(set(BYTECODE_STL.keys()))

hogql_functions = {
fn
if fn not in {f.lower() for f in hog_functions}
else next((f for f in hog_functions if f.lower() == fn), fn)
for fn in hogql_functions
}

all_functions = sorted(hog_functions.union(hogql_functions))
max_length = max(len(fn) for fn in all_functions)

for fn in all_functions:
print( # noqa: T201
fn.ljust(max_length),
"HogQL" if fn in hogql_functions else " ",
"Hog" if fn in hog_functions else " ",
)
Loading