Skip to content

Commit 8b0e85b

Browse files
committed
new denormalized fields
1 parent eb18e7c commit 8b0e85b

File tree

4 files changed

+86
-7
lines changed

4 files changed

+86
-7
lines changed

packages/ast/deploy/schemas/ast_helpers/procedures/denormalized_fields.sql

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,14 @@ BEGIN
5656

5757
body = trim(format('
5858
BEGIN
59-
%1s
60-
INTO %2s;
59+
IF (NEW.%1s IS NOT NULL) THEN
60+
%2s
61+
INTO %3s;
62+
END IF;
6163
RETURN NEW;
6264
END;
6365
',
66+
v_table_field,
6467
deparser.deparse(ast_expr),
6568
deparser.list(to_jsonb(set_fields), E',\n')
6669
));

packages/ast/sql/ast--0.0.1.sql

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6043,6 +6043,7 @@ BEGIN
60436043
IF ((context->'bool')::bool IS TRUE) THEN
60446044
fmt_str = '(%s)';
60456045
END IF;
6046+
60466047
ctx = jsonb_set(context, '{bool}', to_jsonb(TRUE));
60476048

60486049
IF (boolop = 'AND_EXPR') THEN
@@ -6327,6 +6328,7 @@ CREATE FUNCTION deparser.boolean_test ( node jsonb, context jsonb DEFAULT '{}'::
63276328
DECLARE
63286329
output text[];
63296330
booltesttype text;
6331+
ctx jsonb;
63306332
BEGIN
63316333

63326334
IF (node->'BooleanTest') IS NULL THEN
@@ -6345,7 +6347,9 @@ BEGIN
63456347

63466348
booltesttype = node->>'booltesttype';
63476349

6348-
output = array_append(output, deparser.expression(node->'arg'));
6350+
ctx = jsonb_set(context, '{bool}', to_jsonb(TRUE));
6351+
6352+
output = array_append(output, deparser.expression(node->'arg', ctx));
63496353

63506354
output = array_append(output, (CASE
63516355
WHEN booltesttype = 'IS_TRUE' THEN 'IS TRUE'
@@ -6957,6 +6961,31 @@ BEGIN
69576961
END;
69586962
$EOFCODE$ LANGUAGE plpgsql IMMUTABLE;
69596963

6964+
CREATE FUNCTION deparser.alter_seq_stmt ( node jsonb, context jsonb DEFAULT '{}'::jsonb ) RETURNS text AS $EOFCODE$
6965+
DECLARE
6966+
output text[];
6967+
BEGIN
6968+
IF (node->'AlterSeqStmt') IS NULL THEN
6969+
RAISE EXCEPTION 'BAD_EXPRESSION %', 'AlterSeqStmt';
6970+
END IF;
6971+
6972+
IF (node->'AlterSeqStmt'->'sequence') IS NULL THEN
6973+
RAISE EXCEPTION 'BAD_EXPRESSION %', 'AlterSeqStmt';
6974+
END IF;
6975+
6976+
node = node->'AlterSeqStmt';
6977+
6978+
output = array_append(output, 'ALTER SEQUENCE');
6979+
output = array_append(output, deparser.range_var(node->'sequence'));
6980+
6981+
IF (node->'options' IS NOT NULL AND jsonb_array_length(node->'options') > 0) THEN
6982+
output = array_append(output, deparser.list(node->'options', ' ', jsonb_set(context, '{sequence}', to_jsonb(TRUE))));
6983+
END IF;
6984+
6985+
RETURN array_to_string(output, ' ');
6986+
END;
6987+
$EOFCODE$ LANGUAGE plpgsql IMMUTABLE;
6988+
69606989
CREATE FUNCTION deparser.do_stmt ( node jsonb, context jsonb DEFAULT '{}'::jsonb ) RETURNS text AS $EOFCODE$
69616990
DECLARE
69626991
output text[];
@@ -7095,6 +7124,7 @@ $EOFCODE$ LANGUAGE plpgsql IMMUTABLE;
70957124
CREATE FUNCTION deparser.def_elem ( node jsonb, context jsonb DEFAULT '{}'::jsonb ) RETURNS text AS $EOFCODE$
70967125
DECLARE
70977126
defname text;
7127+
output text[];
70987128
BEGIN
70997129
IF (node->'DefElem') IS NULL THEN
71007130
RAISE EXCEPTION 'BAD_EXPRESSION %', 'DefElem';
@@ -7162,6 +7192,33 @@ BEGIN
71627192
ELSE
71637193
RETURN defname || ' ' || deparser.expression(node->'arg', jsonb_set(context, '{simple}', to_jsonb(TRUE)));
71647194
END IF;
7195+
ELSIF (defname = 'owned_by') THEN
7196+
output = ARRAY['OWNED BY']::text;
7197+
7198+
IF (node->'arg' IS NOT NULL AND jsonb_array_length(node->'arg') > 0) THEN
7199+
output = array_append(output, deparser.list_quotes(node->'arg', '.', jsonb_set(context, '{sequence}', to_jsonb(TRUE))));
7200+
END IF;
7201+
7202+
RETURN array_to_string(output, ' ');
7203+
7204+
ELSIF (defname = 'start') THEN
7205+
7206+
output = ARRAY['START WITH']::text;
7207+
output = array_append(output, deparser.expression(node->'arg', jsonb_set(context, '{sequence}', to_jsonb(TRUE))));
7208+
7209+
RETURN array_to_string(output, ' ');
7210+
7211+
ELSIF (defname = 'restart') THEN
7212+
7213+
IF (node->'arg' IS NOT NULL) THEN
7214+
output = ARRAY['RESTART WITH']::text;
7215+
output = array_append(output, deparser.expression(node->'arg', jsonb_set(context, '{sequence}', to_jsonb(TRUE))));
7216+
ELSE
7217+
output = ARRAY['RESTART']::text;
7218+
END IF;
7219+
7220+
RETURN array_to_string(output, ' ');
7221+
71657222
ELSIF (node->'arg' IS NOT NULL) THEN
71667223
RETURN defname || ' ' || deparser.expression(node->'arg', jsonb_set(context, '{simple}', to_jsonb(TRUE)));
71677224
END IF;
@@ -9166,6 +9223,18 @@ BEGIN
91669223
output = array_append(output, 'TO');
91679224
output = array_append(output, quote_ident(node->>'newname'));
91689225

9226+
ELSEIF ( renameType = 'OBJECT_SCHEMA' ) THEN
9227+
9228+
output = array_append(output, 'ALTER');
9229+
output = array_append(output, 'SCHEMA');
9230+
IF ((node->'missing_ok')::bool is TRUE) THEN
9231+
output = array_append(output, 'IF EXISTS');
9232+
END IF;
9233+
output = array_append(output, quote_ident(node->>'subname'));
9234+
output = array_append(output, 'RENAME');
9235+
output = array_append(output, 'TO');
9236+
output = array_append(output, quote_ident(node->>'newname'));
9237+
91699238
ELSEIF ( renameType = 'OBJECT_DOMCONSTRAINT' ) THEN
91709239

91719240
output = array_append(output, 'ALTER');
@@ -9944,6 +10013,8 @@ BEGIN
994410013
RETURN deparser.alter_enum_stmt(expr, context);
994510014
ELSEIF (expr->>'AlterPolicyStmt') IS NOT NULL THEN
994610015
RETURN deparser.alter_policy_stmt(expr, context);
10016+
ELSEIF (expr->>'AlterSeqStmt') IS NOT NULL THEN
10017+
RETURN deparser.alter_seq_stmt(expr, context);
994710018
ELSEIF (expr->>'AlterTableCmd') IS NOT NULL THEN
994810019
RETURN deparser.alter_table_cmd(expr, context);
994910020
ELSEIF (expr->>'AlterTableStmt') IS NOT NULL THEN
@@ -10175,11 +10246,14 @@ BEGIN
1017510246

1017610247
body = trim(format('
1017710248
BEGIN
10178-
%1s
10179-
INTO %2s;
10249+
IF (NEW.%1s IS NOT NULL) THEN
10250+
%2s
10251+
INTO %3s;
10252+
END IF;
1018010253
RETURN NEW;
1018110254
END;
1018210255
',
10256+
v_table_field,
1018310257
deparser.deparse(ast_expr),
1018410258
deparser.list(to_jsonb(set_fields), E',\n')
1018510259
));

packages/ast/test/__tests__/sandbox/__snapshots__/ast.test.js.snap renamed to packages/ast/test/__tests__/sandbox/__snapshots__/denormalized-fields.test.js.snap

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
exports[`test 1`] = `
44
"
55
BEGIN
6-
SELECT ref.a,
6+
IF (NEW.v_table_field IS NOT NULL) THEN
7+
SELECT ref.a,
78
ref.b,
89
ref.c FROM v_schema_name.v_table_name AS ref WHERE ref.v_ref_field = new.v_table_field
9-
INTO new.d,
10+
INTO new.d,
1011
new.e,
1112
new.f;
13+
END IF;
1214
RETURN NEW;
1315
END;
1416
"

0 commit comments

Comments
 (0)