Skip to content

Commit 4facd1a

Browse files
authored
Merge pull request #1853 from joto/test-flex-indexes
Test flex indexes
2 parents 8de2822 + e862650 commit 4facd1a

File tree

5 files changed

+409
-32
lines changed

5 files changed

+409
-32
lines changed

src/flex-lua-index.cpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,18 @@ static void check_and_add_columns(flex_table_t const &table,
4949

5050
void flex_lua_setup_index(lua_State *lua_state, flex_table_t *table)
5151
{
52+
// get method
5253
char const *const method =
5354
luaX_get_table_string(lua_state, "method", -1, "Index definition");
5455
if (!has_index_method(method)) {
5556
throw std::runtime_error{"Unknown index method '{}'."_format(method)};
5657
}
57-
58+
lua_pop(lua_state, 1);
5859
auto &index = table->add_index(method);
5960

61+
// get columns
6062
std::vector<std::string> columns;
61-
lua_getfield(lua_state, -2, "column");
63+
lua_getfield(lua_state, -1, "column");
6264
if (lua_isstring(lua_state, -1)) {
6365
check_and_add_column(*table, &columns, lua_tostring(lua_state, -1));
6466
index.set_columns(columns);
@@ -75,19 +77,21 @@ void flex_lua_setup_index(lua_State *lua_state, flex_table_t *table)
7577
"The 'column' field in an index definition must contain a "
7678
"string or an array."};
7779
}
80+
lua_pop(lua_state, 1);
7881

82+
// get expression
7983
std::string const expression = luaX_get_table_string(
80-
lua_state, "expression", -3, "Index definition", "");
81-
82-
index.set_expression(expression);
83-
84+
lua_state, "expression", -1, "Index definition", "");
85+
lua_pop(lua_state, 1);
8486
if (expression.empty() == columns.empty()) {
8587
throw std::runtime_error{"You must set either the 'column' or the "
8688
"'expression' field in index definition."};
8789
}
90+
index.set_expression(expression);
8891

92+
// get include columns
8993
std::vector<std::string> include_columns;
90-
lua_getfield(lua_state, -4, "include");
94+
lua_getfield(lua_state, -1, "include");
9195
if (get_database_version() >= 110000) {
9296
if (lua_isstring(lua_state, -1)) {
9397
check_and_add_column(*table, &include_columns,
@@ -105,23 +109,26 @@ void flex_lua_setup_index(lua_State *lua_state, flex_table_t *table)
105109
"Database version ({}) doesn't support"
106110
" include columns in indexes."_format(get_database_version())};
107111
}
112+
lua_pop(lua_state, 1);
108113

114+
// get tablespace
109115
std::string const tablespace = luaX_get_table_string(
110-
lua_state, "tablespace", -5, "Index definition", "");
116+
lua_state, "tablespace", -1, "Index definition", "");
117+
lua_pop(lua_state, 1);
111118
check_identifier(tablespace, "tablespace");
112119
if (!has_tablespace(tablespace)) {
113120
throw std::runtime_error{"Unknown tablespace '{}'."_format(tablespace)};
114121
}
115122
index.set_tablespace(tablespace.empty() ? table->index_tablespace()
116123
: tablespace);
117124

118-
index.set_is_unique(luaX_get_table_bool(lua_state, "unique", -6,
125+
// get unique
126+
index.set_is_unique(luaX_get_table_bool(lua_state, "unique", -1,
119127
"Index definition", false));
128+
lua_pop(lua_state, 1);
120129

130+
// get where condition
121131
index.set_where_condition(
122-
luaX_get_table_string(lua_state, "where", -7, "Index definition", ""));
123-
124-
// stack has: "where", "unique", "tablespace", "includes", "expression",
125-
// "column", "method"
126-
lua_pop(lua_state, 7);
132+
luaX_get_table_string(lua_state, "where", -1, "Index definition", ""));
133+
lua_pop(lua_state, 1);
127134
}

src/pgsql-capabilities-int.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef OSM2PGSQL_PGSQL_CAPABILITIES_INT_HPP
2+
#define OSM2PGSQL_PGSQL_CAPABILITIES_INT_HPP
3+
4+
/**
5+
* SPDX-License-Identifier: GPL-2.0-or-later
6+
*
7+
* This file is part of osm2pgsql (https://osm2pgsql.org/).
8+
*
9+
* Copyright (C) 2006-2022 by the osm2pgsql developer community.
10+
* For a full list of authors see the git log.
11+
*/
12+
13+
#include "pgsql-capabilities.hpp"
14+
15+
#include <map>
16+
#include <set>
17+
#include <string>
18+
19+
struct database_capabilities_t
20+
{
21+
std::map<std::string, std::string> settings;
22+
23+
std::set<std::string> extensions;
24+
std::set<std::string> schemas;
25+
std::set<std::string> tablespaces;
26+
std::set<std::string> index_methods;
27+
28+
std::string database_name;
29+
30+
uint32_t database_version = 0;
31+
postgis_version postgis{};
32+
};
33+
34+
database_capabilities_t &database_capabilities_for_testing() noexcept;
35+
36+
#endif // OSM2PGSQL_PGSQL_CAPABILITIES_INT_HPP

src/pgsql-capabilities.cpp

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,23 @@
1010
#include "format.hpp"
1111
#include "logging.hpp"
1212
#include "pgsql-capabilities.hpp"
13+
#include "pgsql-capabilities-int.hpp"
1314
#include "pgsql.hpp"
1415
#include "version.hpp"
1516

16-
#include <map>
17-
#include <set>
1817
#include <stdexcept>
19-
#include <string>
20-
21-
struct database_capabilities_t
22-
{
23-
std::map<std::string, std::string> settings;
24-
25-
std::set<std::string> extensions;
26-
std::set<std::string> schemas;
27-
std::set<std::string> tablespaces;
28-
std::set<std::string> index_methods;
29-
30-
std::string database_name;
31-
32-
uint32_t database_version = 0;
33-
postgis_version postgis{};
34-
};
3518

3619
static database_capabilities_t &capabilities() noexcept
3720
{
3821
static database_capabilities_t c;
3922
return c;
4023
}
4124

25+
database_capabilities_t &database_capabilities_for_testing() noexcept
26+
{
27+
return capabilities();
28+
}
29+
4230
static void init_set_from_query(std::set<std::string> *set,
4331
pg_conn_t const &db_connection,
4432
char const *table, char const *column,

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ set_test(test-wkb LABELS NoDB)
8484

8585
# these tests require LUA support
8686
if (WITH_LUA)
87+
set_test(test-flex-indexes)
8788
set_test(test-output-flex)
8889
set_test(test-output-flex-multi-input)
8990
set_test(test-output-flex-nodes)

0 commit comments

Comments
 (0)