Skip to content

Commit ee2f4b0

Browse files
authored
Merge pull request #2471 from joto/timestamp
Add timestamp(tz) column types
2 parents 4061d56 + 684d8fd commit ee2f4b0

5 files changed

Lines changed: 76 additions & 5 deletions

File tree

src/flex-table-column.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ std::vector<column_type_lookup> const COLUMN_TYPES = {
4343
{"int8", table_column_type::int8},
4444
{"bigint", table_column_type::int8},
4545
{"real", table_column_type::real},
46+
{"timestamp", table_column_type::timestamp},
47+
{"timestamptz", table_column_type::timestamptz},
4648
{"hstore", table_column_type::hstore},
4749
{"json", table_column_type::json},
4850
{"jsonb", table_column_type::jsonb},
@@ -142,6 +144,10 @@ std::string flex_table_column_t::sql_type_name() const
142144
return "int8";
143145
case table_column_type::real:
144146
return "real";
147+
case table_column_type::timestamp:
148+
return "timestamp";
149+
case table_column_type::timestamptz:
150+
return "timestamptz";
145151
case table_column_type::hstore:
146152
return "hstore";
147153
case table_column_type::json:

src/flex-table-column.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ enum class table_column_type : uint8_t
3232

3333
real,
3434

35+
timestamp,
36+
timestamptz,
37+
3538
hstore,
3639
json,
3740
jsonb,

src/flex-write.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "lua-utils.hpp"
1515
#include "wkb.hpp"
1616

17+
#include <osmium/osm/timestamp.hpp>
18+
1719
#include <algorithm>
1820
#include <cmath>
1921
#include <cstdint>
@@ -358,6 +360,26 @@ void flex_write_column(lua_State *lua_state,
358360
throw fmt_error("Invalid type '{}' for real column.",
359361
lua_typename(lua_state, ltype));
360362
}
363+
} else if (column.type() == table_column_type::timestamp) {
364+
if (ltype == LUA_TNUMBER) {
365+
auto const ts = osmium::Timestamp{lua_tointeger(lua_state, -1)};
366+
copy_mgr->add_column(ts.to_iso());
367+
} else if (ltype == LUA_TSTRING) {
368+
copy_mgr->add_column(lua_tolstring(lua_state, -1, nullptr));
369+
} else {
370+
throw fmt_error("Invalid type '{}' for timestamp column.",
371+
lua_typename(lua_state, ltype));
372+
}
373+
} else if (column.type() == table_column_type::timestamptz) {
374+
if (ltype == LUA_TNUMBER) {
375+
auto const ts = osmium::Timestamp{lua_tointeger(lua_state, -1)};
376+
copy_mgr->add_column(ts.to_iso());
377+
} else if (ltype == LUA_TSTRING) {
378+
copy_mgr->add_column(lua_tolstring(lua_state, -1, nullptr));
379+
} else {
380+
throw fmt_error("Invalid type '{}' for timestamptz column.",
381+
lua_typename(lua_state, ltype));
382+
}
361383
} else if (column.type() == table_column_type::hstore) {
362384
if (ltype == LUA_TTABLE) {
363385
copy_mgr->new_hash();

tests/bdd/flex/extra-attributes.feature

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Feature: Tests for including extra attributes
1212
{ column = 'version', type = 'int4' },
1313
{ column = 'changeset', type = 'int4' },
1414
{ column = 'timestamp', type = 'int4' },
15+
{ column = 'timestamp_ts', type = 'timestamp' },
16+
{ column = 'timestamp_ts_tz', type = 'timestamptz' },
1517
{ column = 'uid', type = 'int4' },
1618
{ column = 'user', type = 'text' },
1719
{ column = 'geom', type = 'linestring', not_null = true },
@@ -20,7 +22,10 @@ Feature: Tests for including extra attributes
2022
2123
function osm2pgsql.process_way(object)
2224
object.geom = object:as_linestring()
23-
attr_table:insert(object)
25+
a = object
26+
a.timestamp_ts = a.timestamp
27+
a.timestamp_ts_tz = a.timestamp
28+
attr_table:insert(a)
2429
end
2530
"""
2631

@@ -36,8 +41,8 @@ Feature: Tests for including extra attributes
3641
| --slim |
3742

3843
Then table osm2pgsql_test_attr contains
39-
| type | way_id | tags->'highway' | tags->'osm_version' | version | changeset | timestamp | uid | "user" |
40-
| way | 20 | primary | NULL | 1 | 31 | 1578832496 | 17 | test |
44+
| type | way_id | tags->'highway' | tags->'osm_version' | version | changeset | timestamp | timestamp_ts::text | to_char(timestamp_ts_tz AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"') | uid | "user" |
45+
| way | 20 | primary | NULL | 1 | 31 | 1578832496 | 2020-01-12 12:34:56 | 2020-01-12T12:34:56Z | 17 | test |
4146

4247
Given the grid
4348
| | |
@@ -46,8 +51,8 @@ Feature: Tests for including extra attributes
4651
| --slim | --append |
4752

4853
Then table osm2pgsql_test_attr contains
49-
| type | way_id | tags->'highway' | tags->'osm_version' | version | changeset | timestamp | uid | "user" |
50-
| way | 20 | primary | NULL | NULL | NULL | NULL | NULL | NULL |
54+
| type | way_id | tags->'highway' | tags->'osm_version' | version | changeset | timestamp | timestamp_ts | timestamp_ts_tz | uid | "user" |
55+
| way | 20 | primary | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
5156

5257

5358
Scenario: Importing data with extra attributes

tests/bdd/flex/timestamp.feature

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Feature: Handling of timestamps
2+
3+
Scenario: Write tags in different forms to table
4+
Given the OSM data
5+
"""
6+
n10 v1 dV t2020-12-12T11:22:33Z Tts=20260102T123456Z x10.0 y10.0
7+
n11 v1 dV t2020-12-12T11:22:33Z Tts=2026-02-03T01:23:45Z x10.0 y10.0
8+
"""
9+
And the lua style
10+
"""
11+
local t = osm2pgsql.define_node_table('osm2pgsql_test', {
12+
{ column = 'ts', type = 'timestamp' },
13+
{ column = 'ts_tz', type = 'timestamptz' },
14+
})
15+
16+
function osm2pgsql.process_node(object)
17+
t:insert{
18+
ts = object.tags.ts,
19+
ts_tz = object.tags.ts,
20+
}
21+
t:insert{
22+
ts = object.timestamp,
23+
ts_tz = object.timestamp,
24+
}
25+
end
26+
"""
27+
When running osm2pgsql flex
28+
29+
Then table osm2pgsql_test contains exactly
30+
| node_id | ts::text | to_char(ts_tz AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"') |
31+
| 10 | 2026-01-02 12:34:56 | 2026-01-02T12:34:56Z |
32+
| 11 | 2026-02-03 01:23:45 | 2026-02-03T01:23:45Z |
33+
| 10 | 2020-12-12 11:22:33 | 2020-12-12T11:22:33Z |
34+
| 11 | 2020-12-12 11:22:33 | 2020-12-12T11:22:33Z |
35+

0 commit comments

Comments
 (0)