Skip to content

Commit 584fceb

Browse files
committed
Refactor CREATE MATERIALIZED VIEW ... WITH clause
1 parent 8489390 commit 584fceb

File tree

8 files changed

+166
-133
lines changed

8 files changed

+166
-133
lines changed

src/process_utility.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
#include "tss_callbacks.h"
8686
#include "utils.h"
8787
#include "with_clause/alter_table_with_clause.h"
88+
#include "with_clause/create_materialized_view_with_clause.h"
8889
#include "with_clause/create_table_with_clause.h"
8990
#include "with_clause/with_clause_parser.h"
9091

@@ -4297,7 +4298,7 @@ process_altercontinuousagg_set_with(ContinuousAgg *cagg, Oid view_relid, const L
42974298

42984299
if (list_length(cagg_options) > 0)
42994300
{
4300-
parse_results = ts_continuous_agg_with_clause_parse(cagg_options);
4301+
parse_results = ts_create_materialized_view_with_clause_parse(cagg_options);
43014302
ts_cm_functions->continuous_agg_update_options(cagg, parse_results);
43024303
}
43034304
}
@@ -4978,8 +4979,8 @@ process_create_table_as(ProcessUtilityArgs *args)
49784979

49794980
if (cagg_options)
49804981
{
4981-
parse_results = ts_continuous_agg_with_clause_parse(cagg_options);
4982-
is_cagg = DatumGetBool(parse_results[ContinuousEnabled].parsed);
4982+
parse_results = ts_create_materialized_view_with_clause_parse(cagg_options);
4983+
is_cagg = DatumGetBool(parse_results[CreateMaterializedViewFlagContinuous].parsed);
49834984
}
49844985

49854986
if (!is_cagg)

src/ts_catalog/continuous_agg.c

-95
Original file line numberDiff line numberDiff line change
@@ -46,101 +46,6 @@
4646
#define BUCKET_FUNCTION_SERIALIZE_VERSION 1
4747
#define CHECK_NAME_MATCH(name1, name2) (namestrcmp(name1, name2) == 0)
4848

49-
static const WithClauseDefinition continuous_aggregate_with_clause_def[] = {
50-
[ContinuousEnabled] = {
51-
.arg_names = {"continuous", NULL},
52-
.type_id = BOOLOID,
53-
.default_val = (Datum)false,
54-
},
55-
[ContinuousViewOptionCreateGroupIndex] = {
56-
.arg_names = {"create_group_indexes", NULL},
57-
.type_id = BOOLOID,
58-
.default_val = (Datum)true,
59-
},
60-
[ContinuousViewOptionMaterializedOnly] = {
61-
.arg_names = {"materialized_only", NULL},
62-
.type_id = BOOLOID,
63-
.default_val = (Datum)true,
64-
},
65-
[ContinuousViewOptionCompress] = {
66-
.arg_names = {"enable_columnstore", "compress", NULL},
67-
.type_id = BOOLOID,
68-
},
69-
[ContinuousViewOptionFinalized] = {
70-
.arg_names = {"finalized", NULL},
71-
.type_id = BOOLOID,
72-
.default_val = (Datum)true,
73-
},
74-
[ContinuousViewOptionChunkTimeInterval] = {
75-
.arg_names = {"chunk_time_interval", NULL},
76-
.type_id = INTERVALOID,
77-
},
78-
[ContinuousViewOptionCompressSegmentBy] = {
79-
.arg_names = {"segmentby", "compress_segmentby", NULL},
80-
.type_id = TEXTOID,
81-
},
82-
[ContinuousViewOptionCompressOrderBy] = {
83-
.arg_names = {"orderby", "compress_orderby", NULL},
84-
.type_id = TEXTOID,
85-
},
86-
[ContinuousViewOptionCompressChunkTimeInterval] = {
87-
.arg_names = {"compress_chunk_time_interval", NULL},
88-
.type_id = INTERVALOID,
89-
},
90-
};
91-
92-
WithClauseResult *
93-
ts_continuous_agg_with_clause_parse(const List *defelems)
94-
{
95-
return ts_with_clauses_parse(defelems,
96-
continuous_aggregate_with_clause_def,
97-
TS_ARRAY_LEN(continuous_aggregate_with_clause_def));
98-
}
99-
100-
List *
101-
ts_continuous_agg_get_compression_defelems(const WithClauseResult *with_clauses)
102-
{
103-
List *ret = NIL;
104-
105-
for (int i = 0; i < AlterTableFlagsMax; i++)
106-
{
107-
int option_index = 0;
108-
switch (i)
109-
{
110-
case AlterTableFlagCompressEnabled:
111-
option_index = ContinuousViewOptionCompress;
112-
break;
113-
case AlterTableFlagCompressSegmentBy:
114-
option_index = ContinuousViewOptionCompressSegmentBy;
115-
break;
116-
case AlterTableFlagCompressOrderBy:
117-
option_index = ContinuousViewOptionCompressOrderBy;
118-
break;
119-
case AlterTableFlagCompressChunkTimeInterval:
120-
option_index = ContinuousViewOptionCompressChunkTimeInterval;
121-
break;
122-
default:
123-
elog(ERROR, "Unhandled compression option");
124-
break;
125-
}
126-
127-
const WithClauseResult *input = &with_clauses[option_index];
128-
WithClauseDefinition def = continuous_aggregate_with_clause_def[option_index];
129-
130-
if (!input->is_default)
131-
{
132-
Node *value = (Node *) makeString(ts_with_clause_result_deparse_value(input));
133-
DefElem *elem = makeDefElemExtended(EXTENSION_NAMESPACE,
134-
(char *) def.arg_names[0],
135-
value,
136-
DEFELEM_UNSPEC,
137-
-1);
138-
ret = lappend(ret, elem);
139-
}
140-
}
141-
return ret;
142-
}
143-
14449
static void
14550
init_scan_by_mat_hypertable_id(ScanIterator *iterator, const int32 mat_hypertable_id)
14651
{

src/ts_catalog/continuous_agg.h

-19
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,6 @@
4646
((funcinfo->origin == ORIGIN_TIMESCALE_EXPERIMENTAL) && \
4747
(strcmp("time_bucket_ng", funcinfo->funcname) == 0))
4848

49-
typedef enum ContinuousAggViewOption
50-
{
51-
ContinuousEnabled = 0,
52-
ContinuousViewOptionCreateGroupIndex,
53-
ContinuousViewOptionMaterializedOnly,
54-
ContinuousViewOptionCompress,
55-
ContinuousViewOptionFinalized,
56-
ContinuousViewOptionChunkTimeInterval,
57-
ContinuousViewOptionCompressSegmentBy,
58-
ContinuousViewOptionCompressOrderBy,
59-
ContinuousViewOptionCompressChunkTimeInterval,
60-
ContinuousViewOptionMax
61-
} ContinuousAggViewOption;
62-
6349
typedef enum ContinuousAggViewType
6450
{
6551
ContinuousAggUserView = 0,
@@ -68,11 +54,6 @@ typedef enum ContinuousAggViewType
6854
ContinuousAggAnyView
6955
} ContinuousAggViewType;
7056

71-
extern TSDLLEXPORT WithClauseResult *ts_continuous_agg_with_clause_parse(const List *defelems);
72-
73-
extern TSDLLEXPORT List *
74-
ts_continuous_agg_get_compression_defelems(const WithClauseResult *with_clauses);
75-
7657
/*
7758
* Information about the bucketing function.
7859
*/

src/with_clause/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
set(SOURCES
22
${CMAKE_CURRENT_SOURCE_DIR}/alter_table_with_clause.c
33
${CMAKE_CURRENT_SOURCE_DIR}/create_table_with_clause.c
4+
${CMAKE_CURRENT_SOURCE_DIR}/create_materialized_view_with_clause.c
45
${CMAKE_CURRENT_SOURCE_DIR}/with_clause_parser.c)
56

67
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
9+
#include <catalog/pg_type.h>
10+
#include <fmgr.h>
11+
#include <nodes/makefuncs.h>
12+
13+
#include "compat/compat.h"
14+
15+
#include "alter_table_with_clause.h"
16+
#include "create_materialized_view_with_clause.h"
17+
#include "cross_module_fn.h"
18+
#include "ts_catalog/continuous_agg.h"
19+
#include "with_clause_parser.h"
20+
21+
static const WithClauseDefinition continuous_aggregate_with_clause_def[] = {
22+
[CreateMaterializedViewFlagContinuous] = {
23+
.arg_names = {"continuous", NULL},
24+
.type_id = BOOLOID,
25+
.default_val = (Datum)false,
26+
},
27+
[CreateMaterializedViewFlagCreateGroupIndexes] = {
28+
.arg_names = {"create_group_indexes", NULL},
29+
.type_id = BOOLOID,
30+
.default_val = (Datum)true,
31+
},
32+
[CreateMaterializedViewFlagMaterializedOnly] = {
33+
.arg_names = {"materialized_only", NULL},
34+
.type_id = BOOLOID,
35+
.default_val = (Datum)true,
36+
},
37+
[CreateMaterializedViewFlagCompress] = {
38+
.arg_names = {"columnstore", "enable_columnstore", "compress", NULL},
39+
.type_id = BOOLOID,
40+
},
41+
[CreateMaterializedViewFlagFinalized] = {
42+
.arg_names = {"finalized", NULL},
43+
.type_id = BOOLOID,
44+
.default_val = (Datum)true,
45+
},
46+
[CreateMaterializedViewFlagChunkTimeInterval] = {
47+
.arg_names = {"chunk_time_interval", NULL},
48+
.type_id = INTERVALOID,
49+
},
50+
[CreateMaterializedViewFlagCompressSegmentBy] = {
51+
.arg_names = {"segmentby", "compress_segmentby", NULL},
52+
.type_id = TEXTOID,
53+
},
54+
[CreateMaterializedViewFlagCompressOrderBy] = {
55+
.arg_names = {"orderby", "compress_orderby", NULL},
56+
.type_id = TEXTOID,
57+
},
58+
[CreateMaterializedViewFlagCompressChunkTimeInterval] = {
59+
.arg_names = {"compress_chunk_time_interval", NULL},
60+
.type_id = INTERVALOID,
61+
},
62+
};
63+
64+
WithClauseResult *
65+
ts_create_materialized_view_with_clause_parse(const List *defelems)
66+
{
67+
return ts_with_clauses_parse(defelems,
68+
continuous_aggregate_with_clause_def,
69+
TS_ARRAY_LEN(continuous_aggregate_with_clause_def));
70+
}
71+
72+
List *
73+
ts_continuous_agg_get_compression_defelems(const WithClauseResult *with_clauses)
74+
{
75+
List *ret = NIL;
76+
77+
for (int i = 0; i < AlterTableFlagsMax; i++)
78+
{
79+
int option_index = 0;
80+
switch (i)
81+
{
82+
case AlterTableFlagCompressEnabled:
83+
option_index = CreateMaterializedViewFlagCompress;
84+
break;
85+
case AlterTableFlagCompressSegmentBy:
86+
option_index = CreateMaterializedViewFlagCompressSegmentBy;
87+
break;
88+
case AlterTableFlagCompressOrderBy:
89+
option_index = CreateMaterializedViewFlagCompressOrderBy;
90+
break;
91+
case AlterTableFlagCompressChunkTimeInterval:
92+
option_index = CreateMaterializedViewFlagCompressChunkTimeInterval;
93+
break;
94+
default:
95+
elog(ERROR, "Unhandled compression option");
96+
break;
97+
}
98+
99+
const WithClauseResult *input = &with_clauses[option_index];
100+
WithClauseDefinition def = continuous_aggregate_with_clause_def[option_index];
101+
102+
if (!input->is_default)
103+
{
104+
Node *value = (Node *) makeString(ts_with_clause_result_deparse_value(input));
105+
DefElem *elem = makeDefElemExtended(EXTENSION_NAMESPACE,
106+
(char *) def.arg_names[0],
107+
value,
108+
DEFELEM_UNSPEC,
109+
-1);
110+
ret = lappend(ret, elem);
111+
}
112+
}
113+
return ret;
114+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
10+
#include "with_clause_parser.h"
11+
12+
typedef enum CreateMaterializedViewFlags
13+
{
14+
CreateMaterializedViewFlagContinuous = 0,
15+
CreateMaterializedViewFlagCreateGroupIndexes,
16+
CreateMaterializedViewFlagMaterializedOnly,
17+
CreateMaterializedViewFlagCompress,
18+
CreateMaterializedViewFlagFinalized,
19+
CreateMaterializedViewFlagChunkTimeInterval,
20+
CreateMaterializedViewFlagCompressSegmentBy,
21+
CreateMaterializedViewFlagCompressOrderBy,
22+
CreateMaterializedViewFlagCompressChunkTimeInterval
23+
} CreateMaterializedViewFlags;
24+
25+
extern TSDLLEXPORT WithClauseResult *
26+
ts_create_materialized_view_with_clause_parse(const List *defelems);
27+
28+
extern TSDLLEXPORT List *
29+
ts_continuous_agg_get_compression_defelems(const WithClauseResult *with_clauses);

tsl/src/continuous_aggs/create.c

+9-8
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
#include "ts_catalog/continuous_agg.h"
7676
#include "ts_catalog/continuous_aggs_watermark.h"
7777
#include "utils.h"
78+
#include "with_clause/create_materialized_view_with_clause.h"
7879

7980
static void create_cagg_catalog_entry(int32 matht_id, int32 rawht_id, const char *user_schema,
8081
const char *user_view, const char *partial_schema,
@@ -668,14 +669,14 @@ cagg_create(const CreateTableAsStmt *create_stmt, ViewStmt *stmt, Query *panquer
668669
RangeVar *part_rel = NULL, *mat_rel = NULL, *dum_rel = NULL;
669670
int32 materialize_hypertable_id;
670671
bool materialized_only =
671-
DatumGetBool(with_clause_options[ContinuousViewOptionMaterializedOnly].parsed);
672-
bool finalized = DatumGetBool(with_clause_options[ContinuousViewOptionFinalized].parsed);
672+
DatumGetBool(with_clause_options[CreateMaterializedViewFlagMaterializedOnly].parsed);
673+
bool finalized = DatumGetBool(with_clause_options[CreateMaterializedViewFlagFinalized].parsed);
673674

674675
int64 matpartcol_interval = 0;
675-
if (!with_clause_options[ContinuousViewOptionChunkTimeInterval].is_default)
676+
if (!with_clause_options[CreateMaterializedViewFlagChunkTimeInterval].is_default)
676677
{
677-
matpartcol_interval = interval_to_usec(
678-
DatumGetIntervalP(with_clause_options[ContinuousViewOptionChunkTimeInterval].parsed));
678+
matpartcol_interval = interval_to_usec(DatumGetIntervalP(
679+
with_clause_options[CreateMaterializedViewFlagChunkTimeInterval].parsed));
679680
}
680681
else
681682
{
@@ -714,7 +715,7 @@ cagg_create(const CreateTableAsStmt *create_stmt, ViewStmt *stmt, Query *panquer
714715
makeMaterializedTableName(relnamebuf, "_materialized_hypertable_%d", materialize_hypertable_id);
715716
mat_rel = makeRangeVar(pstrdup(INTERNAL_SCHEMA_NAME), pstrdup(relnamebuf), -1);
716717
is_create_mattbl_index =
717-
DatumGetBool(with_clause_options[ContinuousViewOptionCreateGroupIndex].parsed);
718+
DatumGetBool(with_clause_options[CreateMaterializedViewFlagCreateGroupIndexes].parsed);
718719
mattablecolumninfo_create_materialization_table(&mattblinfo,
719720
materialize_hypertable_id,
720721
mat_rel,
@@ -843,7 +844,7 @@ tsl_process_continuous_agg_viewstmt(Node *node, const char *query_string, void *
843844
const CreateTableAsStmt *stmt = castNode(CreateTableAsStmt, node);
844845
CAggTimebucketInfo timebucket_exprinfo;
845846
Oid nspid;
846-
bool finalized = with_clause_options[ContinuousViewOptionFinalized].parsed;
847+
bool finalized = with_clause_options[CreateMaterializedViewFlagFinalized].parsed;
847848
ViewStmt viewstmt = {
848849
.type = T_ViewStmt,
849850
.view = stmt->into->rel,
@@ -881,7 +882,7 @@ tsl_process_continuous_agg_viewstmt(Node *node, const char *query_string, void *
881882
}
882883
}
883884

884-
if (!with_clause_options[ContinuousViewOptionCompress].is_default)
885+
if (!with_clause_options[CreateMaterializedViewFlagCompress].is_default)
885886
{
886887
ereport(ERROR,
887888
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),

0 commit comments

Comments
 (0)