@@ -21,7 +21,8 @@ import org.apache.spark.sql.catalyst.analysis.UnresolvedTable
21
21
import org .apache .spark .sql .catalyst .expressions .PrimaryKeyConstraint
22
22
import org .apache .spark .sql .catalyst .parser .CatalystSqlParser .parsePlan
23
23
import org .apache .spark .sql .catalyst .parser .ParseException
24
- import org .apache .spark .sql .catalyst .plans .logical .AddConstraint
24
+ import org .apache .spark .sql .catalyst .plans .logical .{AddConstraint , ColumnDefinition }
25
+ import org .apache .spark .sql .types .StringType
25
26
26
27
class PrimaryKeyConstraintParseSuite extends ConstraintParseSuiteBase {
27
28
override val validConstraintCharacteristics =
@@ -34,7 +35,7 @@ class PrimaryKeyConstraintParseSuite extends ConstraintParseSuiteBase {
34
35
tableName = " t" ,
35
36
userProvidedName = null )
36
37
val constraints = Seq (constraint)
37
- verifyConstraints(sql, constraints)
38
+ verifyConstraints(sql, constraints, columnANullable = false )
38
39
}
39
40
40
41
test(" Create table with named primary key - table level" ) {
@@ -45,7 +46,7 @@ class PrimaryKeyConstraintParseSuite extends ConstraintParseSuiteBase {
45
46
userProvidedName = " pk1"
46
47
)
47
48
val constraints = Seq (constraint)
48
- verifyConstraints(sql, constraints)
49
+ verifyConstraints(sql, constraints, columnANullable = false )
49
50
}
50
51
51
52
test(" Create table with composite primary key - table level" ) {
@@ -55,7 +56,24 @@ class PrimaryKeyConstraintParseSuite extends ConstraintParseSuiteBase {
55
56
tableName = " t" ,
56
57
userProvidedName = null )
57
58
val constraints = Seq (constraint)
58
- verifyConstraints(sql, constraints)
59
+ verifyConstraints(sql, constraints, columnANullable = false , columnBNullable = false )
60
+ }
61
+
62
+ test(" Create table with composite primary key - case insensitivity" ) {
63
+ val sql = " CREATE TABLE t (FirstName STRING, LastName STRING," +
64
+ " PRIMARY KEY (firstName, LASTNAME)) USING parquet"
65
+ val constraint = PrimaryKeyConstraint (
66
+ columns = Seq (" firstName" , " LASTNAME" ),
67
+ tableName = " t" ,
68
+ userProvidedName = null )
69
+ val expectedPlan = createExpectedPlan(
70
+ columns = Seq (
71
+ ColumnDefinition (" FirstName" , StringType , nullable = false ),
72
+ ColumnDefinition (" LastName" , StringType , nullable = false )),
73
+ tableConstraints = Seq (constraint),
74
+ isCreateTable = true )
75
+ val parsed = parsePlan(sql)
76
+ comparePlans(parsed, expectedPlan)
59
77
}
60
78
61
79
test(" Create table with primary key - column level" ) {
@@ -65,7 +83,7 @@ class PrimaryKeyConstraintParseSuite extends ConstraintParseSuiteBase {
65
83
tableName = " t" ,
66
84
userProvidedName = null )
67
85
val constraints = Seq (constraint)
68
- verifyConstraints(sql, constraints)
86
+ verifyConstraints(sql, constraints, columnANullable = false )
69
87
}
70
88
71
89
test(" Create table with named primary key - column level" ) {
@@ -76,7 +94,7 @@ class PrimaryKeyConstraintParseSuite extends ConstraintParseSuiteBase {
76
94
userProvidedName = " pk1"
77
95
)
78
96
val constraints = Seq (constraint)
79
- verifyConstraints(sql, constraints)
97
+ verifyConstraints(sql, constraints, columnANullable = false )
80
98
}
81
99
82
100
test(" Create table with multiple primary keys should fail" ) {
@@ -101,7 +119,7 @@ class PrimaryKeyConstraintParseSuite extends ConstraintParseSuiteBase {
101
119
tableName = " t" ,
102
120
userProvidedName = null )
103
121
val constraints = Seq (constraint)
104
- verifyConstraints(sql, constraints, isCreateTable = false )
122
+ verifyConstraints(sql, constraints, isCreateTable = false , columnANullable = false )
105
123
}
106
124
107
125
test(" Replace table with named primary key - table level" ) {
@@ -112,7 +130,7 @@ class PrimaryKeyConstraintParseSuite extends ConstraintParseSuiteBase {
112
130
userProvidedName = " pk1"
113
131
)
114
132
val constraints = Seq (constraint)
115
- verifyConstraints(sql, constraints, isCreateTable = false )
133
+ verifyConstraints(sql, constraints, isCreateTable = false , columnANullable = false )
116
134
}
117
135
118
136
test(" Replace table with composite primary key - table level" ) {
@@ -122,7 +140,25 @@ class PrimaryKeyConstraintParseSuite extends ConstraintParseSuiteBase {
122
140
tableName = " t" ,
123
141
userProvidedName = null )
124
142
val constraints = Seq (constraint)
125
- verifyConstraints(sql, constraints, isCreateTable = false )
143
+ verifyConstraints(sql, constraints, isCreateTable = false , columnANullable = false ,
144
+ columnBNullable = false )
145
+ }
146
+
147
+ test(" Replace table with composite primary key - case insensitivity" ) {
148
+ val sql = " REPLACE TABLE t (FirstName STRING, LastName STRING," +
149
+ " PRIMARY KEY (firstName, LASTNAME)) USING parquet"
150
+ val constraint = PrimaryKeyConstraint (
151
+ columns = Seq (" firstName" , " LASTNAME" ),
152
+ tableName = " t" ,
153
+ userProvidedName = null )
154
+ val expectedPlan = createExpectedPlan(
155
+ columns = Seq (
156
+ ColumnDefinition (" FirstName" , StringType , nullable = false ),
157
+ ColumnDefinition (" LastName" , StringType , nullable = false )),
158
+ tableConstraints = Seq (constraint),
159
+ isCreateTable = false )
160
+ val parsed = parsePlan(sql)
161
+ comparePlans(parsed, expectedPlan)
126
162
}
127
163
128
164
test(" Replace table with primary key - column level" ) {
@@ -132,7 +168,7 @@ class PrimaryKeyConstraintParseSuite extends ConstraintParseSuiteBase {
132
168
tableName = " t" ,
133
169
userProvidedName = null )
134
170
val constraints = Seq (constraint)
135
- verifyConstraints(sql, constraints, isCreateTable = false )
171
+ verifyConstraints(sql, constraints, isCreateTable = false , columnANullable = false )
136
172
}
137
173
138
174
test(" Replace table with named primary key - column level" ) {
@@ -143,7 +179,7 @@ class PrimaryKeyConstraintParseSuite extends ConstraintParseSuiteBase {
143
179
userProvidedName = " pk1"
144
180
)
145
181
val constraints = Seq (constraint)
146
- verifyConstraints(sql, constraints, isCreateTable = false )
182
+ verifyConstraints(sql, constraints, isCreateTable = false , columnANullable = false )
147
183
}
148
184
149
185
test(" Replace table with multiple primary keys should fail" ) {
0 commit comments