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

GSoC-2021 Week-7: vrp_vroom #183

Merged
merged 6 commits into from
Jul 24, 2021
Merged
Show file tree
Hide file tree
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
216 changes: 216 additions & 0 deletions pgtap/vroom/inner_query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
BEGIN;

SELECT plan(166);

/*
SELECT * FROM vrp_vroom(
$$SELECT id, location_index, service, delivery, pickup, skills, priority, time_windows_sql FROM vroom_jobs$$,
$$SELECT p_id, p_location_index, p_service, p_time_windows_sql, d_id, d_location_index, d_service, d_time_windows_sql, amount, skills, priority FROM vroom_shipments$$,
$$SELECT id, start_index, end_index, capacity, skills, tw_open, tw_close, breaks_sql FROM vroom_vehicles$$,
$$SELECT start_vid, end_vid, agg_cost FROM vroom_matrix$$
)
*/

CREATE OR REPLACE FUNCTION test_value(fn TEXT, inner_query_table TEXT, start_sql TEXT, rest_sql TEXT, params TEXT[], parameter TEXT, accept TEXT[], reject TEXT[])
RETURNS SETOF TEXT AS
$BODY$
DECLARE
end_sql TEXT;
query TEXT;
p TEXT;
type_name TEXT;
BEGIN
start_sql = 'SELECT * FROM ' || fn || '(' || start_sql || '$$ SELECT ';
FOREACH p IN ARRAY params
LOOP
IF p = parameter THEN CONTINUE;
END IF;
start_sql = start_sql || p || ', ';
END LOOP;
end_sql = ' FROM ' || inner_query_table || '$$' || rest_sql;

FOREACH type_name IN ARRAY accept
LOOP
query := start_sql || parameter || '::' || type_name || end_sql;
RETURN query SELECT lives_ok(query);
END LOOP;

FOREACH type_name IN ARRAY reject
LOOP
query := start_sql || parameter || '::' || type_name || end_sql;
RETURN query SELECT throws_ok(query);
END LOOP;
END;
$BODY$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION test_anyInteger(fn TEXT, inner_query_table TEXT, start_sql TEXT, rest_sql TEXT, params TEXT[], parameter TEXT)
RETURNS SETOF TEXT AS
$BODY$
DECLARE
accept TEXT[] := ARRAY['SMALLINT', 'INTEGER', 'BIGINT'];
reject TEXT[] := ARRAY['REAL', 'FLOAT8', 'NUMERIC'];
BEGIN
RETURN query SELECT test_value(fn, inner_query_table, start_sql, rest_sql, params, parameter, accept, reject);
END;
$BODY$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION test_Integer(fn TEXT, inner_query_table TEXT, start_sql TEXT, rest_sql TEXT, params TEXT[], parameter TEXT)
RETURNS SETOF TEXT AS
$BODY$
DECLARE
accept TEXT[] := ARRAY['SMALLINT', 'INTEGER'];
reject TEXT[] := ARRAY['BIGINT', 'REAL', 'FLOAT8', 'NUMERIC'];
BEGIN
RETURN query SELECT test_value(fn, inner_query_table, start_sql, rest_sql, params, parameter, accept, reject);
END;
$BODY$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION test_anyArrayInteger(fn TEXT, inner_query_table TEXT, start_sql TEXT, rest_sql TEXT, params TEXT[], parameter TEXT)
RETURNS SETOF TEXT AS
$BODY$
DECLARE
accept TEXT[] := ARRAY['SMALLINT[]', 'INTEGER[]', 'BIGINT[]'];
reject TEXT[] := ARRAY['REAL[]', 'FLOAT8[]', 'NUMERIC[]'];
BEGIN
RETURN query SELECT test_value(fn, inner_query_table, start_sql, rest_sql, params, parameter, accept, reject);
END;
$BODY$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION test_arrayInteger(fn TEXT, inner_query_table TEXT, start_sql TEXT, rest_sql TEXT, params TEXT[], parameter TEXT)
RETURNS SETOF TEXT AS
$BODY$
DECLARE
accept TEXT[] := ARRAY['SMALLINT[]', 'INTEGER[]'];
reject TEXT[] := ARRAY['BIGINT[]', 'REAL[]', 'FLOAT8[]', 'NUMERIC[]'];
BEGIN
RETURN query SELECT test_value(fn, inner_query_table, start_sql, rest_sql, params, parameter, accept, reject);
END;
$BODY$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION test_anyNumerical(fn TEXT, inner_query_table TEXT, start_sql TEXT, rest_sql TEXT, params TEXT[], parameter TEXT)
RETURNS SETOF TEXT AS
$BODY$
DECLARE
accept TEXT[] := ARRAY['SMALLINT', 'INTEGER', 'BIGINT', 'REAL', 'FLOAT8', 'NUMERIC'];
reject TEXT[] := ARRAY[]::TEXT[];
BEGIN
RETURN query SELECT test_value(fn, inner_query_table, start_sql, rest_sql, params, parameter, accept, reject);
END;
$BODY$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION test_Text(fn TEXT, inner_query_table TEXT, start_sql TEXT, rest_sql TEXT, params TEXT[], parameter TEXT)
RETURNS SETOF TEXT AS
$BODY$
DECLARE
accept TEXT[] := ARRAY['TEXT'];
reject TEXT[] := ARRAY[]::TEXT[];
BEGIN
RETURN query SELECT test_value(fn, inner_query_table, start_sql, rest_sql, params, parameter, accept, reject);
END;
$BODY$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION inner_query_jobs()
RETURNS SETOF TEXT AS
$BODY$
DECLARE
fn TEXT := 'vrp_vroom';
inner_query_table TEXT := 'vroom_jobs';
start_sql TEXT := '';
rest_sql TEXT := ', $$SELECT * FROM vroom_shipments$$, $$SELECT * FROM vroom_vehicles$$, $$SELECT * FROM vroom_matrix$$)';
params TEXT[] := ARRAY['id', 'location_index', 'service', 'delivery', 'pickup', 'skills', 'priority', 'time_windows_sql'];
BEGIN
RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'id');
RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'location_index');
RETURN QUERY SELECT test_Integer(fn, inner_query_table, start_sql, rest_sql, params, 'service');
RETURN QUERY SELECT test_anyArrayInteger(fn, inner_query_table, start_sql, rest_sql, params, 'delivery');
RETURN QUERY SELECT test_anyArrayInteger(fn, inner_query_table, start_sql, rest_sql, params, 'pickup');
RETURN QUERY SELECT test_arrayInteger(fn, inner_query_table, start_sql, rest_sql, params, 'skills');
RETURN QUERY SELECT test_Integer(fn, inner_query_table, start_sql, rest_sql, params, 'priority');
RETURN QUERY SELECT test_Text(fn, inner_query_table, start_sql, rest_sql, params, 'time_windows_sql');
END;
$BODY$
LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION inner_query_shipments()
RETURNS SETOF TEXT AS
$BODY$
DECLARE
fn TEXT := 'vrp_vroom';
inner_query_table TEXT := 'vroom_shipments';
start_sql TEXT := '$$SELECT * FROM vroom_jobs$$, ';
rest_sql TEXT := ', $$SELECT * FROM vroom_vehicles$$, $$SELECT * FROM vroom_matrix$$)';
params TEXT[] := ARRAY['p_id', 'p_location_index', 'p_service', 'p_time_windows_sql', 'd_id', 'd_location_index', 'd_service', 'd_time_windows_sql', 'amount', 'skills', 'priority'];
BEGIN
RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'p_id');
RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'd_id');
RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'p_location_index');
RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'd_location_index');
RETURN QUERY SELECT test_Integer(fn, inner_query_table, start_sql, rest_sql, params, 'p_service');
RETURN QUERY SELECT test_Integer(fn, inner_query_table, start_sql, rest_sql, params, 'd_service');
RETURN QUERY SELECT test_Text(fn, inner_query_table, start_sql, rest_sql, params, 'p_time_windows_sql');
RETURN QUERY SELECT test_Text(fn, inner_query_table, start_sql, rest_sql, params, 'd_time_windows_sql');
RETURN QUERY SELECT test_anyArrayInteger(fn, inner_query_table, start_sql, rest_sql, params, 'amount');
RETURN QUERY SELECT test_arrayInteger(fn, inner_query_table, start_sql, rest_sql, params, 'skills');
RETURN QUERY SELECT test_Integer(fn, inner_query_table, start_sql, rest_sql, params, 'priority');
END;
$BODY$
LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION inner_query_vehicles()
RETURNS SETOF TEXT AS
$BODY$
DECLARE
fn TEXT := 'vrp_vroom';
inner_query_table TEXT := 'vroom_vehicles';
start_sql TEXT := '$$SELECT * FROM vroom_jobs$$, $$SELECT * FROM vroom_shipments$$, ';
rest_sql TEXT := ', $$SELECT * FROM vroom_matrix$$)';
params TEXT[] := ARRAY['id', 'start_index', 'end_index', 'capacity', 'skills', 'tw_open', 'tw_close', 'breaks_sql', 'speed_factor'];
BEGIN
RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'id');
RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'start_index');
RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'end_index');
RETURN QUERY SELECT test_anyArrayInteger(fn, inner_query_table, start_sql, rest_sql, params, 'capacity');
RETURN QUERY SELECT test_arrayInteger(fn, inner_query_table, start_sql, rest_sql, params, 'skills');
RETURN QUERY SELECT test_Integer(fn, inner_query_table, start_sql, rest_sql, params, 'tw_open');
RETURN QUERY SELECT test_Integer(fn, inner_query_table, start_sql, rest_sql, params, 'tw_close');
RETURN QUERY SELECT test_Text(fn, inner_query_table, start_sql, rest_sql, params, 'breaks_sql');
RETURN QUERY SELECT test_anyNumerical(fn, inner_query_table, start_sql, rest_sql, params, 'speed_factor');
END;
$BODY$
LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION inner_query_matrix()
RETURNS SETOF TEXT AS
$BODY$
DECLARE
fn TEXT := 'vrp_vroom';
inner_query_table TEXT := 'vroom_matrix';
start_sql TEXT := '$$SELECT * FROM vroom_jobs$$, $$SELECT * FROM vroom_shipments$$, $$SELECT * FROM vroom_vehicles$$, ';
rest_sql TEXT := ')';
params TEXT[] := ARRAY['start_vid', 'end_vid', 'agg_cost'];
BEGIN
RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'start_vid');
RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'end_vid');
RETURN QUERY SELECT test_Integer(fn, inner_query_table, start_sql, rest_sql, params, 'agg_cost');
END;
$BODY$
LANGUAGE plpgsql;


SELECT inner_query_jobs();
SELECT inner_query_shipments();
SELECT inner_query_vehicles();
SELECT inner_query_matrix();

SELECT finish();
ROLLBACK;
76 changes: 76 additions & 0 deletions pgtap/vroom/no_crash_test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
BEGIN;

select plan(30);

PREPARE jobs AS
SELECT * FROM vroom_jobs;

PREPARE shipments AS
SELECT * FROM vroom_shipments;

PREPARE vehicles AS
SELECT * FROM vroom_vehicles;

PREPARE matrix AS
SELECT * FROM vroom_matrix;

CREATE OR REPLACE FUNCTION no_crash()
RETURNS SETOF TEXT AS
$BODY$
DECLARE
params TEXT[];
subs TEXT[];
BEGIN

RETURN QUERY
SELECT isnt_empty('jobs', 'Should be not empty to tests be meaningful');
RETURN QUERY
SELECT isnt_empty('shipments', 'Should be not empty to tests be meaningful');
RETURN QUERY
SELECT isnt_empty('vehicles', 'Should be not empty to tests be meaningful');
RETURN QUERY
SELECT isnt_empty('matrix', 'Should be not empty to tests be meaningful');

params = ARRAY[
'$$jobs$$',
'$$shipments$$',
'$$vehicles$$',
'$$matrix$$'
]::TEXT[];
subs = ARRAY[
'NULL',
'NULL',
'NULL',
'NULL'
]::TEXT[];

RETURN query SELECT * FROM no_crash_test('vrp_vroom', params, subs);

params = ARRAY[
'$$jobs$$',
'$$vehicles$$',
'$$matrix$$'
]::TEXT[];
subs = ARRAY[
'NULL',
'NULL',
'NULL'
]::TEXT[];

RETURN query SELECT * FROM no_crash_test('vrp_vroomJobs', params, subs);

params = ARRAY[
'$$shipments$$',
'$$vehicles$$',
'$$matrix$$'
]::TEXT[];
RETURN query SELECT * FROM no_crash_test('vrp_vroomShipments', params, subs);

END
$BODY$
LANGUAGE plpgsql VOLATILE;


SELECT * FROM no_crash();

ROLLBACK;
88 changes: 88 additions & 0 deletions pgtap/vroom/types_check.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
BEGIN;

SELECT plan(15);

CREATE OR REPLACE FUNCTION types_check()
RETURNS SETOF TEXT AS
$BODY$
BEGIN

RETURN QUERY
SELECT has_function('vrp_vroom');
RETURN QUERY
SELECT has_function('vrp_vroom', ARRAY['text', 'text', 'text', 'text']);
RETURN QUERY
SELECT function_returns('vrp_vroom', ARRAY['text', 'text', 'text', 'text'], 'setof record');

-- parameter names
RETURN QUERY
SELECT bag_has(
$$SELECT proargnames from pg_proc where proname = 'vrp_vroom'$$,
$$SELECT '{"","","","","seq","vehicle_seq","vehicle_id","step_seq","step_type",'
'"task_id","arrival","travel_time","service_time","waiting_time","load"}'::TEXT[]$$
);

-- parameter types
RETURN QUERY
SELECT set_eq(
$$SELECT proallargtypes from pg_proc where proname = 'vrp_vroom'$$,
$$VALUES
('{25,25,25,25,20,20,20,20,23,20,23,23,23,23,1016}'::OID[])
$$
);

RETURN QUERY
SELECT has_function('vrp_vroomjobs');
RETURN QUERY
SELECT has_function('vrp_vroomjobs', ARRAY['text', 'text', 'text']);
RETURN QUERY
SELECT function_returns('vrp_vroomjobs', ARRAY['text', 'text', 'text'], 'setof record');

-- parameter names
RETURN QUERY
SELECT bag_has(
$$SELECT proargnames from pg_proc where proname = 'vrp_vroomjobs'$$,
$$SELECT '{"","","","seq","vehicle_seq","vehicle_id","step_seq","step_type",'
'"task_id","arrival","travel_time","service_time","waiting_time","load"}'::TEXT[]$$
);

-- parameter types
RETURN QUERY
SELECT set_eq(
$$SELECT proallargtypes from pg_proc where proname = 'vrp_vroomjobs'$$,
$$VALUES
('{25,25,25,20,20,20,20,23,20,23,23,23,23,1016}'::OID[])
$$
);

RETURN QUERY
SELECT has_function('vrp_vroomshipments');
RETURN QUERY
SELECT has_function('vrp_vroomshipments', ARRAY['text', 'text', 'text']);
RETURN QUERY
SELECT function_returns('vrp_vroomshipments', ARRAY['text', 'text', 'text'], 'setof record');

-- parameter names
RETURN QUERY
SELECT bag_has(
$$SELECT proargnames from pg_proc where proname = 'vrp_vroomshipments'$$,
$$SELECT '{"","","","seq","vehicle_seq","vehicle_id","step_seq","step_type",'
'"task_id","arrival","travel_time","service_time","waiting_time","load"}'::TEXT[]$$
);

-- parameter types
RETURN QUERY
SELECT set_eq(
$$SELECT proallargtypes from pg_proc where proname = 'vrp_vroomshipments'$$,
$$VALUES
('{25,25,25,20,20,20,20,23,20,23,23,23,23,1016}'::OID[])
$$
);
END;
$BODY$
LANGUAGE plpgsql;

SELECT types_check();

SELECT * FROM finish();
ROLLBACK;
6 changes: 5 additions & 1 deletion src/common/get_check_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,11 @@ spi_getCoordinate(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, Coor
*/
char*
spi_getText(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) {
return DatumGetCString(SPI_getvalue(*tuple, *tupdesc, info.colNumber));
char *val = DatumGetCString(SPI_getvalue(*tuple, *tupdesc, info.colNumber));
if (!val) {
elog(ERROR, "Unexpected Null value in column %s", info.name);
}
return val;
}

/**
Expand Down
Loading