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

add support for covering index (INCLUDE) on PG producer #131

Merged
merged 7 commits into from Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/SQL/Translator/Parser/PostgreSQL.pm
Expand Up @@ -236,7 +236,7 @@ create : CREATE temporary(?) TABLE table_id '(' create_definition(s? /,/) ')' ta
1;
}

create : CREATE unique(?) /(index|key)/i index_name /on/i table_id using_method(?) '(' field_name(s /,/) ')' where_predicate(?) ';'
create : CREATE unique(?) /(index|key)/i index_name /on/i table_id using_method(?) '(' field_name(s /,/) ')' include_covering(?) where_predicate(?) ';'
{
my $table_info = $item{'table_id'};
my $schema_name = $table_info->{'schema_name'};
Expand All @@ -249,6 +249,7 @@ create : CREATE unique(?) /(index|key)/i index_name /on/i table_id using_method(
fields => $item[9],
method => $item{'using_method(?)'}[0],
where => $item{'where_predicate(?)'}[0],
include => $item{'include_covering(?)'}[0]
}
;
}
Expand Down Expand Up @@ -302,6 +303,9 @@ using_method : /using/i WORD { $item[2] }

where_predicate : /where/i /[^;]+/

include_covering : /include/i '(' covering_field_name(s /,/) ')'
{ $item{'covering_field_name(s)'} }

create_definition : field
| table_constraint
| <error>
Expand Down Expand Up @@ -502,6 +506,8 @@ schema_name : NAME

field_name : NAME

covering_field_name : NAME

double_quote: /"/

index_name : NAME
Expand Down Expand Up @@ -1088,6 +1094,7 @@ sub parse {
my @options = ();
push @options, { using => $idata->{'method'} } if $idata->{method};
push @options, { where => $idata->{'where'} } if $idata->{where};
push @options, { include => $idata->{'include'} } if $idata->{include};
my $index = $table->add_index(
name => $idata->{'name'},
type => uc $idata->{'type'},
Expand Down
16 changes: 15 additions & 1 deletion t/14postgres-parser.t
Expand Up @@ -78,6 +78,7 @@ baz $foo$,
CREATE INDEX test_index1 ON t_test1 (f_varchar);
CREATE INDEX test_index2 ON t_test1 USING hash (f_char, f_bool);
CREATE INDEX test_index3 ON t_test1 USING hash (f_bigint, f_tz) WHERE f_bigint = '1' AND f_tz IS NULL;
CREATE INDEX test_index4 ON t_test1 USING hash (f_bigint, f_tz) include (f_bool) WHERE f_bigint = '1' AND f_tz IS NULL;

alter table t_test1 add f_fk2 integer;

Expand Down Expand Up @@ -406,7 +407,7 @@ is( $trigger->action, 'EXECUTE PROCEDURE foo()', "Correct action for trigger");

# test index
my @indices = $t1->get_indices;
is(scalar @indices, 3, 'got three indexes');
is(scalar @indices, 4, 'got three indexes');

my $t1_i1 = $indices[0];
is( $t1_i1->name, 'test_index1', 'First index is "test_index1"' );
Expand All @@ -427,4 +428,17 @@ is_deeply(
'Index is using hash method and has predicate right'
);

my $t1_i4 = $indices[3];
is( $t1_i4->name, 'test_index4', 'Fourth index is "test_index4"' );
is( join(',', $t1_i4->fields), 'f_bigint,f_tz', 'Index is on fields "f_bigint, f_tz"' );
is_deeply(
[ $t1_i4->options ],
[
{ using => 'hash' },
{ where => "f_bigint = '1' AND f_tz IS NULL" },
{ include => [ 'f_bool' ] }
],
'Index is using hash method and has predicate right and include INCLUDE'
);

done_testing;