Skip to content
This repository was archived by the owner on Apr 7, 2026. It is now read-only.

Commit a487b8e

Browse files
committed
add constant value encoding
1 parent feb3c7c commit a487b8e

3 files changed

Lines changed: 45 additions & 15 deletions

File tree

google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/KeyRecipe.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,17 @@ private static final class Part {
6868
private final com.google.spanner.v1.KeyRecipe.Part.NullOrder nullOrder; // if kind == VALUE
6969
private final String identifier; // if kind == VALUE
7070
private final List<Integer> structIdentifiers; // if kind == VALUE
71+
private final Value constantValue; // if kind == VALUE and value is set
7172
private final boolean random; // if kind == VALUE and random: true
7273

74+
private Value constantValue() {
75+
return constantValue;
76+
}
77+
78+
private boolean hasConstantValue() {
79+
return constantValue != null;
80+
}
81+
7382
private Part(
7483
Kind kind,
7584
int tag,
@@ -78,6 +87,7 @@ private Part(
7887
com.google.spanner.v1.KeyRecipe.Part.NullOrder nullOrder,
7988
String identifier,
8089
List<Integer> structIdentifiers,
90+
Value constantValue,
8191
boolean random) {
8292
this.kind = kind;
8393
this.tag = tag;
@@ -86,10 +96,14 @@ private Part(
8696
this.nullOrder = nullOrder;
8797
this.identifier = identifier;
8898
this.structIdentifiers = structIdentifiers;
99+
this.constantValue = constantValue;
89100
this.random = random;
90101
}
91102

92103
private ResolvedValue resolveValue(BiFunction<Integer, String, Value> valueFinder, int index) {
104+
if (hasConstantValue()) {
105+
return ResolvedValue.ofValue(constantValue());
106+
}
93107
Value value = valueFinder.apply(index, identifier == null ? "" : identifier);
94108
if (value == null) {
95109
return ResolvedValue.missing();
@@ -112,36 +126,38 @@ private ResolvedValue resolveValue(BiFunction<Integer, String, Value> valueFinde
112126
}
113127

114128
private boolean shouldConsumeValueIndex() {
115-
return !random;
129+
return !hasConstantValue() && !random;
116130
}
117131

118132
static Part fromProto(com.google.spanner.v1.KeyRecipe.Part partProto) {
119133
if (partProto.getTag() != 0) {
120134
if (partProto.getTag() < 0) {
121-
return new Part(Kind.INVALID, 0, null, null, null, null, null, false);
135+
return new Part(Kind.INVALID, 0, null, null, null, null, null, null, false);
122136
}
123-
return new Part(Kind.TAG, partProto.getTag(), null, null, null, null, null, false);
137+
return new Part(Kind.TAG, partProto.getTag(), null, null, null, null, null, null, false);
124138
}
125139
if (!partProto.hasType()) {
126-
return new Part(Kind.INVALID, 0, null, null, null, null, null, false);
140+
return new Part(Kind.INVALID, 0, null, null, null, null, null, null, false);
127141
}
128142
if (partProto.getOrder() != com.google.spanner.v1.KeyRecipe.Part.Order.ASCENDING
129143
&& partProto.getOrder() != com.google.spanner.v1.KeyRecipe.Part.Order.DESCENDING) {
130-
return new Part(Kind.INVALID, 0, null, null, null, null, null, false);
144+
return new Part(Kind.INVALID, 0, null, null, null, null, null, null, false);
131145
}
132146
if (partProto.getNullOrder() != com.google.spanner.v1.KeyRecipe.Part.NullOrder.NULLS_FIRST
133147
&& partProto.getNullOrder() != com.google.spanner.v1.KeyRecipe.Part.NullOrder.NULLS_LAST
134148
&& partProto.getNullOrder() != com.google.spanner.v1.KeyRecipe.Part.NullOrder.NOT_NULL) {
135-
return new Part(Kind.INVALID, 0, null, null, null, null, null, false);
149+
return new Part(Kind.INVALID, 0, null, null, null, null, null, null, false);
136150
}
137151
if (partProto.hasRandom()
138152
&& partProto.getType().getCode() != com.google.spanner.v1.TypeCode.INT64) {
139-
return new Part(Kind.INVALID, 0, null, null, null, null, null, false);
153+
return new Part(Kind.INVALID, 0, null, null, null, null, null, null, false);
140154
}
141155

142156
String identifier = partProto.hasIdentifier() ? partProto.getIdentifier() : null;
143157
List<Integer> structIdentifiers = new ArrayList<>(partProto.getStructIdentifiersList());
144158

159+
Value constantValue = partProto.hasValue() ? partProto.getValue() : null;
160+
145161
return new Part(
146162
Kind.VALUE,
147163
0,
@@ -150,6 +166,7 @@ static Part fromProto(com.google.spanner.v1.KeyRecipe.Part partProto) {
150166
partProto.getNullOrder(),
151167
identifier,
152168
structIdentifiers,
169+
constantValue,
153170
partProto.hasRandom());
154171
}
155172
}

google-cloud-spanner/src/test/java/com/google/cloud/spanner/spi/v1/KeyRecipeTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ public void queryParamsUsesStructIdentifiers() throws Exception {
5858
assertTrue(target.limit.isEmpty());
5959
}
6060

61+
@Test
62+
public void queryParamsUsesConstantValue() throws Exception {
63+
com.google.spanner.v1.KeyRecipe recipeProto =
64+
createRecipe(
65+
"part { tag: 1 }\n"
66+
+ "part {\n"
67+
+ " order: ASCENDING\n"
68+
+ " null_order: NULLS_FIRST\n"
69+
+ " type { code: STRING }\n"
70+
+ " value { string_value: \"const\" }\n"
71+
+ "}\n");
72+
73+
KeyRecipe recipe = KeyRecipe.create(recipeProto);
74+
TargetRange target = recipe.queryParamsToTargetRange(Struct.getDefaultInstance());
75+
assertEquals(expectedKey("const"), target.start);
76+
assertTrue(target.limit.isEmpty());
77+
}
78+
6179
private static com.google.spanner.v1.KeyRecipe createRecipe(String text)
6280
throws TextFormat.ParseException {
6381
com.google.spanner.v1.KeyRecipe.Builder builder = com.google.spanner.v1.KeyRecipe.newBuilder();

google-cloud-spanner/src/test/resources/finder_test.textproto

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4560,8 +4560,7 @@ test_case {
45604560
operation_uid: 1
45614561
database_id: 1099549376513
45624562
schema_generation: "\001\001"
4563-
key: "A\206\310\002"
4564-
limit_key: "A\206\310\003"
4563+
key: "A\206\310\002\234\2310\000x"
45654564
}
45664565
}
45674566
event {
@@ -5327,8 +5326,7 @@ test_case {
53275326
operation_uid: 1
53285327
database_id: 1099548327937
53295328
schema_generation: "\001\001"
5330-
key: "A\206\310\002"
5331-
limit_key: "A\206\310\003"
5329+
key: "A\206\310\002\234\2310\000x"
53325330
}
53335331
}
53345332
event {
@@ -8751,8 +8749,7 @@ test_case {
87518749
operation_uid: 1
87528750
database_id: 1099550425089
87538751
schema_generation: "\001\001"
8754-
key: "A\206\300\002"
8755-
limit_key: "A\206\300\003"
8752+
key: "A\206\300\002\221\000"
87568753
}
87578754
}
87588755
event {
@@ -10079,5 +10076,3 @@ test_case {
1007910076
}
1008010077
}
1008110078
}
10082-
10083-

0 commit comments

Comments
 (0)