Skip to content

Commit

Permalink
Merge pull request #4065 from mathesar-foundation/default_formatting_bug
Browse files Browse the repository at this point in the history
Execute default expr when not dynamic.
  • Loading branch information
Anish9901 authored Dec 13, 2024
2 parents 026921a + 091fbae commit b928de2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
55 changes: 41 additions & 14 deletions db/sql/00_msar.sql
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,46 @@ WHERE has_privilege;
$$ LANGUAGE SQL STABLE RETURNS NULL ON NULL INPUT;


CREATE OR REPLACE FUNCTION
msar.describe_column_default(tab_id regclass, col_id smallint) RETURNS jsonb AS $$/*
Return a JSONB object describing the default (if any) of the given column in the given table.
The returned JSON will have the form:
{
"value": <any>,
"is_dynamic": <bool>,
}
If the default is possibly dynamic, i.e., if "is_dynamic" is true, then "value" will be a text SQL
expression that generates the default value if evaluated. If it is not dynamic, then "value" is the
actual default value.
*/
DECLARE
def_expr text;
def_json jsonb;
BEGIN
def_expr = CASE
WHEN attidentity='' THEN pg_catalog.pg_get_expr(adbin, tab_id)
ELSE 'identity'
END
FROM pg_catalog.pg_attribute LEFT JOIN pg_catalog.pg_attrdef ON attrelid=adrelid AND attnum=adnum
WHERE attrelid=tab_id AND attnum=col_id;
IF def_expr IS NULL THEN
RETURN NULL;
ELSIF msar.is_default_possibly_dynamic(tab_id, col_id) THEN
EXECUTE format(
'SELECT jsonb_build_object(''value'', %L, ''is_dynamic'', true)', def_expr
) INTO def_json;
ELSE
EXECUTE format(
'SELECT jsonb_build_object(''value'', msar.format_data(%s), ''is_dynamic'', false)', def_expr
) INTO def_json;
END IF;
RETURN def_json;
END;
$$ LANGUAGE plpgsql RETURNS NULL ON NULL INPUT;


CREATE OR REPLACE FUNCTION msar.get_column_info(tab_id regclass) RETURNS jsonb AS $$/*
Given a table identifier, return an array of objects describing the columns of the table.
Expand Down Expand Up @@ -965,20 +1005,7 @@ SELECT jsonb_agg(
'type_options', msar.get_type_options(atttypid, atttypmod, attndims),
'nullable', NOT attnotnull,
'primary_key', COALESCE(pgi.indisprimary, false),
'default',
nullif(
jsonb_strip_nulls(
jsonb_build_object(
'value',
CASE
WHEN attidentity='' THEN pg_get_expr(adbin, tab_id)
ELSE 'identity'
END,
'is_dynamic', msar.is_default_possibly_dynamic(tab_id, attnum)
)
),
jsonb_build_object()
),
'default', msar.describe_column_default(tab_id, attnum),
'has_dependents', msar.has_dependents(tab_id, attnum),
'description', msar.col_description(tab_id, attnum),
'current_role_priv', msar.list_column_privileges_for_current_role(tab_id, attnum),
Expand Down
2 changes: 1 addition & 1 deletion db/sql/test_00_msar.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2662,7 +2662,7 @@ BEGIN
"name": "txt",
"type": "text",
"default": {
"value": "'abc'::text",
"value": "abc",
"is_dynamic": false
},
"nullable": true,
Expand Down

0 comments on commit b928de2

Please sign in to comment.