Skip to content

Commit 45a890e

Browse files
committed
CREATE TABLE WITH
1 parent 4d89805 commit 45a890e

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ set(SOURCES
1010
chunk_index.c
1111
chunk_scan.c
1212
constraint.c
13+
create_table_with_clause.c
1314
cross_module_fn.c
1415
copy.c
1516
compression_with_clause.c

src/create_table_with_clause.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This file and its contents are licensed under the Apache License 2.0.
3+
* Please see the included NOTICE for copyright information and
4+
* LICENSE-APACHE for a copy of the license.
5+
*/
6+
7+
#include <postgres.h>
8+
#include <access/htup_details.h>
9+
#include <catalog/dependency.h>
10+
#include <catalog/namespace.h>
11+
#include <catalog/pg_trigger.h>
12+
#include <catalog/pg_type.h>
13+
#include <commands/trigger.h>
14+
#include <fmgr.h>
15+
#include <parser/parser.h>
16+
#include <storage/lmgr.h>
17+
#include <utils/builtins.h>
18+
#include <utils/lsyscache.h>
19+
#include <utils/typcache.h>
20+
21+
#include "compat/compat.h"
22+
#include "create_table_with_clause.h"
23+
#include "with_clause_parser.h"
24+
25+
static const WithClauseDefinition create_table_with_clauses_def[] = {
26+
[CreateTableFlagHypertable] = {.arg_names = {"hypertable", NULL}, .type_id = BOOLOID,},
27+
[CreateTableFlagTimeColumn] = {.arg_names = {"time_column", NULL}, .type_id = TEXTOID,},
28+
};
29+
30+
WithClauseResult *
31+
ts_create_table_with_clause_parse(const List *defelems)
32+
{
33+
return ts_with_clauses_parse(defelems,
34+
create_table_with_clauses_def,
35+
TS_ARRAY_LEN(create_table_with_clauses_def));
36+
}

src/create_table_with_clause.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* This file and its contents are licensed under the Apache License 2.0.
3+
* Please see the included NOTICE for copyright information and
4+
* LICENSE-APACHE for a copy of the license.
5+
*/
6+
#pragma once
7+
8+
#include <postgres.h>
9+
#include <catalog/pg_type.h>
10+
11+
#include "with_clause_parser.h"
12+
13+
typedef enum CreateTableFlags
14+
{
15+
CreateTableFlagHypertable = 0,
16+
CreateTableFlagTimeColumn,
17+
} CreateTableFlags;
18+
19+
WithClauseResult *ts_create_table_with_clause_parse(const List *defelems);

src/process_utility.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include "chunk_index.h"
5959
#include "compression_with_clause.h"
6060
#include "copy.h"
61+
#include "create_table_with_clause.h"
6162
#include "cross_module_fn.h"
6263
#include "debug_assert.h"
6364
#include "debug_point.h"
@@ -101,6 +102,19 @@ static ProcessUtilityContext last_process_utility_context = PROCESS_UTILITY_TOPL
101102
static DDLResult process_altertable_set_options(AlterTableCmd *cmd, Hypertable *ht);
102103
static DDLResult process_altertable_reset_options(AlterTableCmd *cmd, Hypertable *ht);
103104

105+
static Oid
106+
get_sizing_func_oid()
107+
{
108+
const char *sizing_func_name = "calculate_chunk_interval";
109+
const int sizing_func_nargs = 3;
110+
static Oid sizing_func_arg_types[] = { INT4OID, INT8OID, INT8OID };
111+
112+
return ts_get_function_oid(sizing_func_name,
113+
INTERNAL_SCHEMA_NAME,
114+
sizing_func_nargs,
115+
sizing_func_arg_types);
116+
}
117+
104118
/* Call the default ProcessUtility and handle PostgreSQL version differences */
105119
static void
106120
prev_ProcessUtility(ProcessUtilityArgs *args)
@@ -3665,6 +3679,13 @@ process_cluster_start(ProcessUtilityArgs *args)
36653679
return result;
36663680
}
36673681

3682+
typedef struct CreateTableInfo
3683+
{
3684+
bool hypertable;
3685+
NameData time_column;
3686+
} CreateTableInfo;
3687+
3688+
static CreateTableInfo create_table_info = { 0 };
36683689
/*
36693690
* Process create table statements.
36703691
*
@@ -3709,6 +3730,34 @@ process_create_table_end(Node *parsetree)
37093730
break;
37103731
}
37113732
}
3733+
3734+
if (true)
3735+
{
3736+
Oid table_relid = RangeVarGetRelid(stmt->relation, NoLock, true);
3737+
3738+
DimensionInfo *open_dim_info =
3739+
ts_dimension_info_create_open(table_relid,
3740+
&create_table_info.time_column, /* column name */
3741+
-1, /* interval */
3742+
InvalidOid, /* interval type */
3743+
InvalidOid /* partitioning func */
3744+
);
3745+
3746+
ChunkSizingInfo chunk_sizing_info = {
3747+
.table_relid = table_relid,
3748+
.func = get_sizing_func_oid(),
3749+
.colname = NameStr(create_table_info.time_column),
3750+
};
3751+
3752+
ts_hypertable_create_from_info(table_relid,
3753+
INVALID_HYPERTABLE_ID,
3754+
0, /* flags */
3755+
open_dim_info, /* open_dim_info */
3756+
NULL, /* closed_dim_info */
3757+
NULL, /* associated_schema_name */
3758+
NULL, /* associated_table_prefix */
3759+
&chunk_sizing_info);
3760+
}
37123761
}
37133762

37143763
static inline const char *
@@ -4912,6 +4961,7 @@ process_create_stmt(ProcessUtilityArgs *args)
49124961
{
49134962
CreateStmt *stmt = castNode(CreateStmt, args->parsetree);
49144963

4964+
#if PG15_GE
49154965
if (stmt->accessMethod && strcmp(stmt->accessMethod, TS_HYPERCORE_TAM_NAME) == 0)
49164966
ereport(ERROR,
49174967
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -4929,6 +4979,22 @@ process_create_stmt(ProcessUtilityArgs *args)
49294979
errhint("It does not make sense to set the default access method for all "
49304980
"tables to \"%s\" since it is only supported for hypertables.",
49314981
TS_HYPERCORE_TAM_NAME));
4982+
#endif
4983+
4984+
List *pg_options = NIL, *hypertable_options = NIL;
4985+
ts_with_clause_filter(stmt->options, &hypertable_options, &pg_options);
4986+
stmt->options = pg_options;
4987+
4988+
if (hypertable_options)
4989+
{
4990+
WithClauseResult *parsed_with_clauses =
4991+
ts_create_table_with_clause_parse(hypertable_options);
4992+
}
4993+
4994+
create_table_info.hypertable =
4995+
DatumGetBool(parsed_with_clauses[CreateTableFlagHypertable].parsed);
4996+
namestrcpy(&create_table_info.time_column,
4997+
TextDatumGetCString(parsed_with_clauses[CreateTableFlagTimeColumn].parsed));
49324998

49334999
return DDL_CONTINUE;
49345000
}

0 commit comments

Comments
 (0)