Skip to content

Commit 648ae29

Browse files
authored
Add support of TTL expression in column definition (#136)
1 parent 06269a7 commit 648ae29

File tree

5 files changed

+262
-6
lines changed

5 files changed

+262
-6
lines changed

parser/ast.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3220,7 +3220,7 @@ type ColumnDef struct {
32203220
AliasExpr Expr
32213221

32223222
Codec *CompressionCodec
3223-
TTL Expr
3223+
TTL *TTLClause
32243224

32253225
Comment *StringLiteral
32263226
CompressionCodec *Ident

parser/parser_table.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,14 @@ func (p *Parser) parseTableColumnExpr(pos Pos) (*ColumnDef, error) {
482482
if codec != nil {
483483
columnEnd = codec.End()
484484
}
485+
ttl, err := p.tryParseTTLClause(p.Pos(), false)
486+
if err != nil {
487+
return nil, err
488+
}
489+
if ttl != nil {
490+
columnEnd = ttl.End()
491+
}
492+
column.TTL = ttl
485493

486494
column.ColumnEnd = columnEnd
487495
column.Comment = comment
@@ -702,13 +710,13 @@ func (p *Parser) parseOrderExpr(pos Pos) (*OrderExpr, error) {
702710
}, nil
703711
}
704712

705-
func (p *Parser) tryParseTTLClause(pos Pos) (*TTLClause, error) {
713+
func (p *Parser) tryParseTTLClause(pos Pos, allowMultiValues bool) (*TTLClause, error) {
706714
if p.tryConsumeKeyword(KeywordTtl) == nil {
707715
return nil, nil // nolint
708716
}
709717
ttlExprList := &TTLClause{TTLPos: pos, ListEnd: pos}
710718
// accept the TTL keyword
711-
items, err := p.parseTTLClause(pos)
719+
items, err := p.parseTTLClause(pos, allowMultiValues)
712720
if err != nil {
713721
return nil, err
714722
}
@@ -719,14 +727,16 @@ func (p *Parser) tryParseTTLClause(pos Pos) (*TTLClause, error) {
719727
return ttlExprList, nil
720728
}
721729

722-
func (p *Parser) parseTTLClause(pos Pos) ([]*TTLExpr, error) {
730+
// parseTTLClause parses the TTL clause.
731+
// allowMultiValues is used to determine whether to allow multiple TTL values.
732+
func (p *Parser) parseTTLClause(pos Pos, allowMultiValues bool) ([]*TTLExpr, error) {
723733
items := make([]*TTLExpr, 0)
724734
expr, err := p.parseTTLExpr(pos)
725735
if err != nil {
726736
return nil, err
727737
}
728738
items = append(items, expr)
729-
for !p.lexer.isEOF() && p.tryConsumeTokenKind(TokenKindComma) != nil {
739+
for allowMultiValues && !p.lexer.isEOF() && p.tryConsumeTokenKind(TokenKindComma) != nil {
730740
expr, err = p.parseTTLExpr(pos)
731741
if err != nil {
732742
return nil, err
@@ -932,7 +942,7 @@ func (p *Parser) parseEngineExpr(pos Pos) (*EngineExpr, error) {
932942
engineExpr.SampleBy = sampleBy
933943
engineEnd = sampleBy.End()
934944
case p.matchKeyword(KeywordTtl):
935-
ttl, err := p.tryParseTTLClause(p.Pos())
945+
ttl, err := p.tryParseTTLClause(p.Pos(), true)
936946
if err != nil {
937947
return nil, err
938948
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE TABLE example1 (
2+
timestamp DateTime,
3+
x UInt32 TTL timestamp + INTERVAL 1 MONTH,
4+
y UInt32 TTL timestamp + INTERVAL 1 WEEK
5+
)
6+
ENGINE = MergeTree
7+
ORDER BY tuple()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- Origin SQL:
2+
CREATE TABLE example1 (
3+
timestamp DateTime,
4+
x UInt32 TTL timestamp + INTERVAL 1 MONTH,
5+
y UInt32 TTL timestamp + INTERVAL 1 WEEK
6+
)
7+
ENGINE = MergeTree
8+
ORDER BY tuple()
9+
10+
-- Format SQL:
11+
CREATE TABLE example1 (timestamp DateTime, x UInt32 TTL timestamp + INTERVAL 1 MONTH, y UInt32 TTL timestamp + INTERVAL 1 WEEK) ENGINE = MergeTree ORDER BY tuple();
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
[
2+
{
3+
"CreatePos": 0,
4+
"StatementEnd": 176,
5+
"Name": {
6+
"Database": null,
7+
"Table": {
8+
"Name": "example1",
9+
"QuoteType": 1,
10+
"NamePos": 13,
11+
"NameEnd": 21
12+
}
13+
},
14+
"IfNotExists": false,
15+
"UUID": null,
16+
"OnCluster": null,
17+
"TableSchema": {
18+
"SchemaPos": 22,
19+
"SchemaEnd": 140,
20+
"Columns": [
21+
{
22+
"NamePos": 28,
23+
"ColumnEnd": 46,
24+
"Name": {
25+
"Ident": {
26+
"Name": "timestamp",
27+
"QuoteType": 1,
28+
"NamePos": 28,
29+
"NameEnd": 37
30+
},
31+
"DotIdent": null
32+
},
33+
"Type": {
34+
"Name": {
35+
"Name": "DateTime",
36+
"QuoteType": 1,
37+
"NamePos": 38,
38+
"NameEnd": 46
39+
}
40+
},
41+
"NotNull": null,
42+
"Nullable": null,
43+
"DefaultExpr": null,
44+
"MaterializedExpr": null,
45+
"AliasExpr": null,
46+
"Codec": null,
47+
"TTL": null,
48+
"Comment": null,
49+
"CompressionCodec": null
50+
},
51+
{
52+
"NamePos": 52,
53+
"ColumnEnd": 93,
54+
"Name": {
55+
"Ident": {
56+
"Name": "x",
57+
"QuoteType": 1,
58+
"NamePos": 52,
59+
"NameEnd": 53
60+
},
61+
"DotIdent": null
62+
},
63+
"Type": {
64+
"Name": {
65+
"Name": "UInt32",
66+
"QuoteType": 1,
67+
"NamePos": 54,
68+
"NameEnd": 60
69+
}
70+
},
71+
"NotNull": null,
72+
"Nullable": null,
73+
"DefaultExpr": null,
74+
"MaterializedExpr": null,
75+
"AliasExpr": null,
76+
"Codec": null,
77+
"TTL": {
78+
"TTLPos": 61,
79+
"ListEnd": 93,
80+
"Items": [
81+
{
82+
"TTLPos": 61,
83+
"Expr": {
84+
"LeftExpr": {
85+
"Name": "timestamp",
86+
"QuoteType": 1,
87+
"NamePos": 65,
88+
"NameEnd": 74
89+
},
90+
"Operation": "+",
91+
"RightExpr": {
92+
"IntervalPos": 77,
93+
"Expr": {
94+
"NumPos": 86,
95+
"NumEnd": 87,
96+
"Literal": "1",
97+
"Base": 10
98+
},
99+
"Unit": {
100+
"Name": "MONTH",
101+
"QuoteType": 1,
102+
"NamePos": 88,
103+
"NameEnd": 93
104+
}
105+
},
106+
"HasGlobal": false,
107+
"HasNot": false
108+
}
109+
}
110+
]
111+
},
112+
"Comment": null,
113+
"CompressionCodec": null
114+
},
115+
{
116+
"NamePos": 99,
117+
"ColumnEnd": 139,
118+
"Name": {
119+
"Ident": {
120+
"Name": "y",
121+
"QuoteType": 1,
122+
"NamePos": 99,
123+
"NameEnd": 100
124+
},
125+
"DotIdent": null
126+
},
127+
"Type": {
128+
"Name": {
129+
"Name": "UInt32",
130+
"QuoteType": 1,
131+
"NamePos": 101,
132+
"NameEnd": 107
133+
}
134+
},
135+
"NotNull": null,
136+
"Nullable": null,
137+
"DefaultExpr": null,
138+
"MaterializedExpr": null,
139+
"AliasExpr": null,
140+
"Codec": null,
141+
"TTL": {
142+
"TTLPos": 108,
143+
"ListEnd": 139,
144+
"Items": [
145+
{
146+
"TTLPos": 108,
147+
"Expr": {
148+
"LeftExpr": {
149+
"Name": "timestamp",
150+
"QuoteType": 1,
151+
"NamePos": 112,
152+
"NameEnd": 121
153+
},
154+
"Operation": "+",
155+
"RightExpr": {
156+
"IntervalPos": 124,
157+
"Expr": {
158+
"NumPos": 133,
159+
"NumEnd": 134,
160+
"Literal": "1",
161+
"Base": 10
162+
},
163+
"Unit": {
164+
"Name": "WEEK",
165+
"QuoteType": 1,
166+
"NamePos": 135,
167+
"NameEnd": 139
168+
}
169+
},
170+
"HasGlobal": false,
171+
"HasNot": false
172+
}
173+
}
174+
]
175+
},
176+
"Comment": null,
177+
"CompressionCodec": null
178+
}
179+
],
180+
"AliasTable": null,
181+
"TableFunction": null
182+
},
183+
"Engine": {
184+
"EnginePos": 142,
185+
"EngineEnd": 176,
186+
"Name": "MergeTree",
187+
"Params": null,
188+
"PrimaryKey": null,
189+
"PartitionBy": null,
190+
"SampleBy": null,
191+
"TTL": null,
192+
"Settings": null,
193+
"OrderBy": {
194+
"OrderPos": 161,
195+
"ListEnd": 176,
196+
"Items": [
197+
{
198+
"OrderPos": 161,
199+
"Expr": {
200+
"Name": {
201+
"Name": "tuple",
202+
"QuoteType": 1,
203+
"NamePos": 170,
204+
"NameEnd": 175
205+
},
206+
"Params": {
207+
"LeftParenPos": 175,
208+
"RightParenPos": 176,
209+
"Items": {
210+
"ListPos": 176,
211+
"ListEnd": 176,
212+
"HasDistinct": false,
213+
"Items": []
214+
},
215+
"ColumnArgList": null
216+
}
217+
},
218+
"Alias": null,
219+
"Direction": "None"
220+
}
221+
]
222+
}
223+
},
224+
"SubQuery": null,
225+
"HasTemporary": false,
226+
"Comment": null
227+
}
228+
]

0 commit comments

Comments
 (0)