Skip to content

Commit

Permalink
added nsw_gnb.addr_str fn
Browse files Browse the repository at this point in the history
it just creates a readable addresss
  • Loading branch information
AKST committed Feb 3, 2025
1 parent 42f38bf commit 43e2b62
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
17 changes: 11 additions & 6 deletions lib/tooling/schema/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ def get_prop(expr, Type):
es = expr.args['properties'].expressions
return next((e for e in es if isinstance(e, Type)))

def create_function(expr: Expression, command_e: str) -> Stmt.Op:
match = re.search(r"FUNCTION\s+(?:(\w+)\.)?(\w+)\s*\(", command_e, re.IGNORECASE)
if not match:
raise TypeError(f'unknown {repr(e)}')
s_name = match.group(1) if match.group(1) else None
t_name = match.group(2)
return Stmt.CreateFunction(expr, s_name, t_name)

def expr_as_op(expr: Expression) -> Optional[Stmt.Op]:
match expr:
case sql_expr.Create(kind="SCHEMA", this=schema_def):
Expand Down Expand Up @@ -171,12 +179,9 @@ def expr_as_op(expr: Expression) -> Optional[Stmt.Op]:
case sql_expr.Command(this="CREATE", expression=e):
match re.findall(f'\w+', e.lower()):
case ['function', *_]:
match = re.search(r"FUNCTION\s+(?:(\w+)\.)?(\w+)\s*\(", e, re.IGNORECASE)
if not match:
raise TypeError(f'unknown {repr(e)}')
s_name = match.group(1) if match.group(1) else None
t_name = match.group(2)
return Stmt.CreateFunction(expr, s_name, t_name)
return create_function(expr, e)
case ['or', 'replace', 'function', *_]:
return create_function(expr, e)
case ['type', s_name, t_name, 'as', *_]:
return Stmt.CreateType(expr, s_name, t_name)
case ['type', t_name, 'as', *_]:
Expand Down
10 changes: 9 additions & 1 deletion lib/tooling/schema/tests/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,22 @@ def test_expr_as_op(snapshot, sql: str):
operations = sql_as_operations(sql)
snapshot.assert_match(pformat(operations, width=150), 'schema')

def test_function():
def test_create_function():
sql = \
"CREATE FUNCTION a.c(input TEXT) RETURNS TEXT $$ BEGIN RETURN ''; END; $$ LANGUAGE plpgsql"
schema = sql_as_operations(sql)
create_function = next(o for o in schema.operations)
assert create_function.schema_name == 'a'
assert create_function.type_name == 'c'

def test_create_or_replace_function():
sql = \
"CREATE OR REPLACE FUNCTION a.c(input TEXT) RETURNS TEXT $$ BEGIN RETURN ''; END; $$ LANGUAGE plpgsql"
schema = sql_as_operations(sql)
create_function = next(o for o in schema.operations)
assert create_function.schema_name == 'a'
assert create_function.type_name == 'c'

def test_schema_for_parition():
sql = \
"CREATE TABLE a.c PARTITION OF b.a FOR VALUES WITH (MODULUS 8, REMAINDER 0)"
Expand Down
6 changes: 6 additions & 0 deletions sql/nsw_gnb/schema/001_APPLY_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,9 @@ SELECT DISTINCT ON (property_id, strata_lot_number)
LEFT JOIN nsw_gnb.street USING (street_id, locality_id)
LEFT JOIN nsw_gnb.locality USING (locality_id);

CREATE OR REPLACE FUNCTION nsw_gnb.addr_str(a nsw_gnb.full_property_address) RETURNS TEXT
LANGUAGE SQL IMMUTABLE PARALLEL SAFE AS $$
SELECT (a.street_number || ' ' || a.street_name || ' ' || a.locality_name || ' ' || a.postcode);
$$;


0 comments on commit 43e2b62

Please sign in to comment.