-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_test.go
124 lines (113 loc) · 3.33 KB
/
parse_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package hive_sql_parser
import (
"github.com/stretchr/testify/suite"
"testing"
)
type ParseTestSuit struct {
suite.Suite
}
func TestParse(t *testing.T) {
suite.Run(t, new(ParseTestSuit))
}
func (s *ParseTestSuit) TestParseCreateTableStmt() {
// no partition
parser := NewCreatTableStmtParser()
stmt := parser.Parse(`create table test1
(
c1 int COMMENT 'c1c1',
c2 string not null,
c3 boolean,
c4 double,
c5 date COMMENT 'date',
c6 timestamp,
c7 varchar(255)
) comment '3434'`)
tableStmt := stmt.(*CreateTableStmt)
s.Equal("test1", tableStmt.TableName)
s.Equal("3434", tableStmt.TableComment)
s.Equal(7, len(tableStmt.TableColumns))
s.Equal(0, len(tableStmt.PartitionColumns))
s.Equal("c1c1", tableStmt.TableColumns[0].Comment)
s.Equal(true, tableStmt.TableColumns[1].NotNull)
s.Equal(255, tableStmt.TableColumns[6].Length)
// with partition
stmt = parser.Parse(`create table test1
(
c1 int COMMENT 'c1c1',
c2 string not null,
c3 boolean,
c4 double,
c5 date COMMENT 'date',
c6 timestamp,
c7 varchar(255)
) comment '3434' partitioned by (c8 string comment 'c8 partition')`)
tableStmt = stmt.(*CreateTableStmt)
s.Equal("test1", tableStmt.TableName)
s.Equal("3434", tableStmt.TableComment)
s.Equal(7, len(tableStmt.TableColumns))
s.Equal(1, len(tableStmt.PartitionColumns))
s.Equal("c8 partition", tableStmt.PartitionColumns[0].Comment)
s.Equal("string", tableStmt.PartitionColumns[0].Type)
stmt = parser.Parse("create table test1\n\t(\n\t c1 int COMMENT 'c1c1'," +
"\n\t `type` string not null\n\t) comment '3434'")
tableStmt = stmt.(*CreateTableStmt)
s.Equal("test1", tableStmt.TableName)
s.Equal("3434", tableStmt.TableComment)
s.Equal(2, len(tableStmt.TableColumns))
s.Equal("type", tableStmt.TableColumns[1].Name)
stmt = parser.Parse(`create table test1
(
c1 int COMMENT 'c1c1',
c2 string not null,
c3 boolean,
c4 double,
c5 date COMMENT 'date',
c6 timestamp,
c7 varchar(255),
c8 decimal(14, 2),
c9 decimal(10, 0)
) comment '3434'`)
tableStmt = stmt.(*CreateTableStmt)
s.Equal("decimal", tableStmt.TableColumns[7].Type)
s.Equal(14, tableStmt.TableColumns[7].Precision)
s.Equal(2, tableStmt.TableColumns[7].Scale.Value)
s.Equal(10, tableStmt.TableColumns[8].Precision)
s.Equal(0, tableStmt.TableColumns[8].Scale.Value)
s.Equal(true, tableStmt.TableColumns[8].Scale.Valid)
stmt = parser.Parse(`create table dwd_air_iep_forecast_station_hour_detail (
oid SMALLSERIAL NULL,
code SERIAL NULL,
name BIGSERIAL NULL,
aqi SMALLINT NULL,
pm25 INTEGER NULL,
pm10 BIGINT NULL,
so2 CHAR(10) NULL,
no2 VARCHAR(10) NULL,
co CHARACTER(10) NULL,
o3 CHARACTER VARYING(10) NULL,
aqi_main TEXT NULL,
temperature REAL NULL,
humidity DOUBLE PRECISION NULL,
wind_speed BOOLEAN NULL,
wind_direction TIME NULL,
pressure TIMETZ NULL,
create_at TIMESTAMP NULL,
tim TIMESTAMPTZ NULL,
cid TIMESTAMP WITHOUT TIME ZONE(6) NULL,
cid_name TIMESTAMP WITH TIME ZONE(6) NULL,
type DATE NULL,
forecast_time GEOGRAPHY NULL,
aqi_max GEOMETRY NULL,
aqi_min1 JSON NULL,
aqi_min2 NUMERIC NULL,
aqi_min3 DECIMAL NULL,
)`)
tableStmt = stmt.(*CreateTableStmt)
s.T().Log(tableStmt)
}
func (s *ParseTestSuit) TestParseDropTableStmt() {
parser := NewDropTableStmtParser()
stmt := parser.Parse(`drop table test1`)
dropTableStmt := stmt.(*DropTableStmt)
s.Equal("test1", dropTableStmt.TableName)
}