Skip to content

value_numeric support for scientific notation #381

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 7 additions & 4 deletions src/xian/services/bds/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def create_state():
value JSONB,
value_numeric NUMERIC GENERATED ALWAYS AS (
CASE
WHEN value::text ~ '^"*[0-9]+(\.[0-9]+)?"*$'
WHEN value::text ~ '^"*-?[0-9]+(\\.[0-9]+)?([eE][+-]?[0-9]+)?"*$'
THEN (trim(both '"' from value::text))::NUMERIC
ELSE NULL
END
Expand All @@ -34,7 +34,7 @@ def create_state():
);
CREATE INDEX IF NOT EXISTS idx_state_value_numeric ON state(value_numeric);
"""


def create_state_changes():
return """
Expand All @@ -45,13 +45,16 @@ def create_state_changes():
value JSONB,
value_numeric NUMERIC GENERATED ALWAYS AS (
CASE
WHEN value::text ~ '^"*[0-9]+(\.[0-9]+)?"*$'
-- Match numeric values, including integers, decimals, and scientific notation.
-- Examples of supported formats: 42, -3.14, "1e10", "-2.5E-3", "0.001", "1.23e-4".
-- Refer to test cases for additional examples and edge cases.
WHEN value::text ~ '^"*-?[0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)?"*$'
THEN (trim(both '"' from value::text))::NUMERIC
ELSE NULL
END
) STORED,
created TIMESTAMP NOT NULL
)
);
"""


Expand Down