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

Fix bug with INSERT INTO...SELECT #33

Merged
merged 1 commit into from
May 3, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/planner.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ static set_rel_pathlist_hook_type prev_set_rel_pathlist_hook;
typedef struct ChangeTableNameCtx
{
Query *parse;
CmdType commandType;
Cache *hcache;
Hypertable *hentry;
} ChangeTableNameCtx;
Expand Down Expand Up @@ -57,7 +58,7 @@ change_table_name_walker(Node *node, void *context)
ChangeTableNameCtx *ctx = (ChangeTableNameCtx *) context;

if (rangeTableEntry->rtekind == RTE_RELATION && rangeTableEntry->inh
&& ctx->parse->commandType != CMD_INSERT
&& ctx->commandType != CMD_INSERT
)
{
Hypertable *hentry = hypertable_cache_get_entry(ctx->hcache, rangeTableEntry->relid);
Expand All @@ -74,8 +75,20 @@ change_table_name_walker(Node *node, void *context)

if (IsA(node, Query))
{
return query_tree_walker((Query *) node, change_table_name_walker,
bool result;
ChangeTableNameCtx *ctx = (ChangeTableNameCtx *) context;
CmdType old = ctx->commandType;

/* adjust context */
ctx->commandType = ((Query *) node)->commandType;

result = query_tree_walker((Query *) node, change_table_name_walker,
context, QTW_EXAMINE_RTES);

/* restore context */
ctx->commandType = old;

return result;
}

return expression_tree_walker(node, change_table_name_walker, context);
Expand Down Expand Up @@ -280,6 +293,7 @@ timescaledb_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
/* replace call to main table with call to the replica table */
context.hcache = hypertable_cache_pin();
context.parse = parse;
context.commandType = parse->commandType;
context.hentry = NULL;
change_table_name_walker((Node *) parse, &context);
/* note assumes 1 hypertable per query */
Expand Down
22 changes: 22 additions & 0 deletions test/expected/insert_single.out
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,25 @@ SELECT * FROM "1dim";
Fri Jan 20 09:00:47 2017 | 25.1
(3 rows)

CREATE TABLE regular_table (time timestamp, temp float);
INSERT INTO regular_table SELECT * FROM "1dim";
SELECT * FROM regular_table;
time | temp
--------------------------+------
Fri Jan 20 09:00:01 2017 | 22.5
Fri Jan 20 09:00:21 2017 | 21.2
Fri Jan 20 09:00:47 2017 | 25.1
(3 rows)

TRUNCATE TABLE regular_table;
INSERT INTO regular_table VALUES('2017-01-20T09:00:59', 29.2);
INSERT INTO "1dim" SELECT * FROM regular_table;
SELECT * FROM "1dim";
time | temp
--------------------------+------
Fri Jan 20 09:00:01 2017 | 22.5
Fri Jan 20 09:00:21 2017 | 21.2
Fri Jan 20 09:00:47 2017 | 25.1
Fri Jan 20 09:00:59 2017 | 29.2
(4 rows)

10 changes: 10 additions & 0 deletions test/sql/insert_single.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,13 @@ INSERT INTO "1dim" VALUES('2017-01-20T09:00:01', 22.5);
INSERT INTO "1dim" VALUES('2017-01-20T09:00:21', 21.2);
INSERT INTO "1dim" VALUES('2017-01-20T09:00:47', 25.1);
SELECT * FROM "1dim";

CREATE TABLE regular_table (time timestamp, temp float);
INSERT INTO regular_table SELECT * FROM "1dim";
SELECT * FROM regular_table;

TRUNCATE TABLE regular_table;
INSERT INTO regular_table VALUES('2017-01-20T09:00:59', 29.2);
INSERT INTO "1dim" SELECT * FROM regular_table;
SELECT * FROM "1dim";