Skip to content

Commit 33bfa3b

Browse files
authored
Merge pull request #1894 from joto/create-index-always
Add option to always build id index
2 parents 781d1fe + 960ef0c commit 33bfa3b

File tree

5 files changed

+98
-5
lines changed

5 files changed

+98
-5
lines changed

src/flex-table.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,8 @@ void table_connection_t::stop(bool updateable, bool append)
359359
}
360360
}
361361

362-
if (updateable && table().has_id_column()) {
362+
if (table().always_build_id_index() ||
363+
(updateable && table().has_id_column())) {
363364
create_id_index();
364365
}
365366

src/flex-table.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ class flex_table_t
155155

156156
flex_index_t &add_index(std::string method);
157157

158+
void set_always_build_id_index() noexcept
159+
{
160+
m_always_build_id_index = true;
161+
}
162+
163+
bool always_build_id_index() const noexcept
164+
{
165+
return m_always_build_id_index;
166+
}
167+
158168
private:
159169
/// The name of the table
160170
std::string m_name;
@@ -200,6 +210,9 @@ class flex_table_t
200210
/// Does this table have more than one geometry column?
201211
bool m_has_multiple_geom_columns = false;
202212

213+
/// Always build the id index, not only when it is needed for updates?
214+
bool m_always_build_id_index = false;
215+
203216
}; // class flex_table_t
204217

205218
class table_connection_t

src/output-flex.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ void output_flex_t::setup_id_columns(flex_table_t *table)
534534

535535
std::string const type{
536536
luaX_get_table_string(lua_state(), "type", -1, "The ids field")};
537+
lua_pop(lua_state(), 1); // "type"
537538

538539
if (type == "node") {
539540
table->set_id_type(osmium::item_type::node);
@@ -545,7 +546,7 @@ void output_flex_t::setup_id_columns(flex_table_t *table)
545546
table->set_id_type(osmium::item_type::area);
546547
} else if (type == "any") {
547548
table->set_id_type(osmium::item_type::undefined);
548-
lua_getfield(lua_state(), -2, "type_column");
549+
lua_getfield(lua_state(), -1, "type_column");
549550
if (lua_isstring(lua_state(), -1)) {
550551
std::string const column_name =
551552
lua_tolstring(lua_state(), -1, nullptr);
@@ -555,18 +556,29 @@ void output_flex_t::setup_id_columns(flex_table_t *table)
555556
} else if (!lua_isnil(lua_state(), -1)) {
556557
throw std::runtime_error{"type_column must be a string or nil."};
557558
}
558-
lua_pop(lua_state(), 1); // type_column
559+
lua_pop(lua_state(), 1); // "type_column"
559560
} else {
560561
throw fmt_error("Unknown ids type: {}.", type);
561562
}
562563

563564
std::string const name =
564-
luaX_get_table_string(lua_state(), "id_column", -2, "The ids field");
565+
luaX_get_table_string(lua_state(), "id_column", -1, "The ids field");
566+
lua_pop(lua_state(), 1); // "id_column"
565567
check_identifier(name, "column names");
566568

569+
std::string const create_index = luaX_get_table_string(
570+
lua_state(), "create_index", -1, "The ids field", "auto");
571+
lua_pop(lua_state(), 1); // "create_index"
572+
if (create_index == "always") {
573+
table->set_always_build_id_index();
574+
} else if (create_index != "auto") {
575+
throw fmt_error("Unknown value '{}' for 'create_index' field of ids",
576+
create_index);
577+
}
578+
567579
auto &column = table->add_column(name, "id_num", "");
568580
column.set_not_null();
569-
lua_pop(lua_state(), 3); // id_column, type, ids
581+
lua_pop(lua_state(), 1); // "ids"
570582
}
571583

572584
void output_flex_t::setup_flex_table_columns(flex_table_t *table)

tests/bdd/flex/lua-index-definitions.feature

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,64 @@ Feature: Index definitions in Lua file
476476
Then SELECT schemaname, tablename FROM pg_catalog.pg_indexes WHERE tablename = 'mytable' AND indexdef LIKE '%USING btree (name)%' AND indexdef LIKE '%WHERE (name = lower(name))%'
477477
| schemaname | tablename |
478478
| public | mytable |
479+
480+
Scenario: Don't create id index if the configuration doesn't mention it
481+
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
482+
And the lua style
483+
"""
484+
local t = osm2pgsql.define_table({
485+
name = 'mytable',
486+
ids = { type = 'node', id_column = 'node_id' },
487+
columns = {
488+
{ column = 'name', type = 'text' },
489+
{ column = 'tags', type = 'jsonb' },
490+
{ column = 'geom', type = 'geometry' },
491+
}
492+
})
493+
"""
494+
When running osm2pgsql flex
495+
Then table pg_catalog.pg_indexes has 0 rows with condition
496+
"""
497+
schemaname = 'public' AND tablename = 'mytable' AND indexname LIKE '%node_id%'
498+
"""
499+
500+
Scenario: Don't create id index if the configuration doesn't says so
501+
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
502+
And the lua style
503+
"""
504+
local t = osm2pgsql.define_table({
505+
name = 'mytable',
506+
ids = { type = 'node', id_column = 'node_id', create_index = 'auto' },
507+
columns = {
508+
{ column = 'name', type = 'text' },
509+
{ column = 'tags', type = 'jsonb' },
510+
{ column = 'geom', type = 'geometry' },
511+
}
512+
})
513+
"""
514+
When running osm2pgsql flex
515+
Then table pg_catalog.pg_indexes has 0 rows with condition
516+
"""
517+
schemaname = 'public' AND tablename = 'mytable' AND indexname LIKE '%node_id%'
518+
"""
519+
520+
Scenario: Always create id index if the configuration says so
521+
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
522+
And the lua style
523+
"""
524+
local t = osm2pgsql.define_table({
525+
name = 'mytable',
526+
ids = { type = 'node', id_column = 'node_id', create_index = 'always' },
527+
columns = {
528+
{ column = 'name', type = 'text' },
529+
{ column = 'tags', type = 'jsonb' },
530+
{ column = 'geom', type = 'geometry' },
531+
}
532+
})
533+
"""
534+
When running osm2pgsql flex
535+
Then table pg_catalog.pg_indexes has 1 rows with condition
536+
"""
537+
schemaname = 'public' AND tablename = 'mytable' AND indexname LIKE '%node_id%'
538+
"""
539+

tests/bdd/steps/steps_db.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ def table_exists(conn, table):
121121
num = scalar(conn, """SELECT count(*) FROM pg_tables
122122
WHERE tablename = %s AND schemaname = %s""",
123123
(tablename, schema))
124+
if num == 1:
125+
return True
126+
127+
num = scalar(conn, """SELECT count(*) FROM pg_views
128+
WHERE viewname = %s AND schemaname = %s""",
129+
(tablename, schema))
124130
return num == 1
125131

126132

0 commit comments

Comments
 (0)