Skip to content

Commit 1f59829

Browse files
skrulcikvkedia
authored andcommitted
---
yaml --- r: 8273 b: refs/heads/master c: be26690 h: refs/heads/master i: 8271: 16910a9
1 parent 0696daa commit 1f59829

3 files changed

Lines changed: 33 additions & 17 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: cec93a50abb6ebe241c9d61aee8d8835cbcf7d24
2+
refs/heads/master: be26690e2461ec271e16376a86afef64f3448b42
33
refs/heads/travis: 47e4fee4fd5af9b2a8ce46f23c72ec95f9b195b2
44
refs/heads/gh-pages: 983ad13eceefeee06f3300486563b47902cedb77
55
refs/tags/0.0.9: 22f1839238f66c39e67ed4dfdcd273b1ae2e8444

trunk/google-cloud-spanner/src/main/java/com/google/cloud/spanner/Mutation.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424

2525
import java.io.Serializable;
2626
import java.util.Collections;
27+
import java.util.HashSet;
2728
import java.util.LinkedHashMap;
2829
import java.util.List;
2930
import java.util.Map;
3031
import java.util.Objects;
32+
import java.util.Set;
3133
import javax.annotation.Nullable;
3234

3335
/**
@@ -188,9 +190,17 @@ public ValueBinder<WriteBuilder> set(String columnName) {
188190
return binder;
189191
}
190192

193+
/**
194+
* Returns a newly created {@code Mutation} based on the contents of the {@code Builder}.
195+
*
196+
* @throws IllegalStateException if any duplicate columns are present. Duplicate detection is
197+
* case-insensitive.
198+
*/
191199
public Mutation build() {
192200
checkBindingInProgress(false);
193-
return new Mutation(table, operation, columns.build(), values.build(), null);
201+
ImmutableList<String> columnNames = columns.build();
202+
checkDuplicateColumns(columnNames);
203+
return new Mutation(table, operation, columnNames, values.build(), null);
194204
}
195205

196206
private void checkBindingInProgress(boolean expectInProgress) {
@@ -200,6 +210,17 @@ private void checkBindingInProgress(boolean expectInProgress) {
200210
throw new IllegalStateException("Incomplete binding for column " + currentColumn);
201211
}
202212
}
213+
214+
private void checkDuplicateColumns(ImmutableList<String> columnNames) {
215+
Set<String> columnNameSet = new HashSet<>();
216+
for (String columnName : columnNames) {
217+
columnName = columnName.toLowerCase();
218+
if (columnNameSet.contains(columnName)) {
219+
throw new IllegalStateException("Duplicate column: " + columnName);
220+
}
221+
columnNameSet.add(columnName);
222+
}
223+
}
203224
}
204225

205226
/** Returns the name of the table that this mutation will affect. */
@@ -247,9 +268,6 @@ public Map<String, Value> asMap() {
247268
LinkedHashMap<String, Value> map = new LinkedHashMap<>();
248269
for (int i = 0; i < columns.size(); ++i) {
249270
Value existing = map.put(columns.get(i), values.get(i));
250-
if (existing != null) {
251-
throw new IllegalStateException("Duplicate column: " + columns.get(i));
252-
}
253271
}
254272
return Collections.unmodifiableMap(map);
255273
}

trunk/google-cloud-spanner/src/test/java/com/google/cloud/spanner/MutationTest.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,16 @@ public void replace() {
124124

125125
@Test
126126
public void duplicateColumn() {
127-
// The current implementation does not repeat validation performed by the server; duplicates
128-
// are permitted but will later cause an operation failure.
129-
Mutation m = Mutation.newInsertBuilder("T1").set("C1").to(true).set("C1").to(false).build();
130-
assertThat(m.getOperation()).isEqualTo(Mutation.Op.INSERT);
131-
assertThat(m.getColumns()).containsExactly("C1", "C1").inOrder();
132-
assertThat(m.getValues()).containsExactly(Value.bool(true), Value.bool(false)).inOrder();
133-
assertThat(m.toString()).isEqualTo("insert(T1{C1=true,C1=false})");
127+
expectedException.expect(IllegalStateException.class);
128+
expectedException.expectMessage("Duplicate column");
129+
Mutation.newInsertBuilder("T1").set("C1").to(true).set("C1").to(false).build();
130+
}
131+
132+
@Test
133+
public void duplicateColumnCaseInsensitive() {
134+
expectedException.expect(IllegalStateException.class);
135+
expectedException.expectMessage("Duplicate column");
136+
Mutation.newInsertBuilder("T1").set("C1").to(true).set("c1").to(false).build();
134137
}
135138

136139
@Test
@@ -141,11 +144,6 @@ public void asMap() {
141144
m = Mutation.newInsertBuilder("T").set("C1").to(true).set("C2").to(1234).build();
142145
assertThat(m.asMap())
143146
.isEqualTo(ImmutableMap.of("C1", Value.bool(true), "C2", Value.int64(1234)));
144-
145-
m = Mutation.newInsertBuilder("T").set("C1").to(true).set("C1").to(false).build();
146-
expectedException.expect(IllegalStateException.class);
147-
expectedException.expectMessage("Duplicate column");
148-
m.asMap();
149147
}
150148

151149
@Test

0 commit comments

Comments
 (0)