Skip to content

Commit af0a63a

Browse files
committed
Re-work ordering clause support in query builder behind a single OrderingClause abstraction. This allows for
custom impls to define their own logic for managing "ORDER BY" clauses. patch by Bret McGuire; reviewed by Bret McGuire and Lukasz Antoniak reference: #2047
1 parent 855cd0b commit af0a63a

5 files changed

Lines changed: 208 additions & 97 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package com.datastax.oss.driver.api.querybuilder.select;
19+
20+
import com.datastax.oss.driver.api.core.CqlIdentifier;
21+
import com.datastax.oss.driver.api.core.data.CqlVector;
22+
import com.datastax.oss.driver.api.querybuilder.QueryBuilder;
23+
import edu.umd.cs.findbugs.annotations.NonNull;
24+
25+
/**
26+
* Concrete implementation of {@link OrderingClause} which supports ordering by
27+
* the adjacent nearest-neighbor (ANN) calculation. This usage is primarily used
28+
* for vector calculations.
29+
*/
30+
public class AnnOrderingClause extends OrderingClause {
31+
32+
private final CqlIdentifier identifier;
33+
private final CqlVector<?> vector;
34+
35+
AnnOrderingClause(CqlIdentifier identifier, CqlVector<?> vector) {
36+
37+
this.identifier = identifier;
38+
this.vector = vector;
39+
}
40+
41+
public static AnnOrderingClause create(CqlIdentifier identifier, CqlVector<?> vector) {
42+
return new AnnOrderingClause(identifier, vector);
43+
}
44+
45+
@Override
46+
public void appendTo(@NonNull StringBuilder builder) {
47+
builder.append(" ORDER BY ").append(this.identifier.asCql(true)).append(" ANN OF ");
48+
QueryBuilder.literal(this.vector).appendTo(builder);
49+
}
50+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package com.datastax.oss.driver.api.querybuilder.select;
19+
20+
import com.datastax.oss.driver.api.core.CqlIdentifier;
21+
import com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder;
22+
import com.datastax.oss.driver.internal.querybuilder.ImmutableCollections;
23+
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap;
24+
import edu.umd.cs.findbugs.annotations.NonNull;
25+
import java.util.Map;
26+
27+
/**
28+
* Concrete implementation of {@link OrderingClause} which supports ordering by
29+
* specified columns. This usages is the default ORDER BY syntax for Apache Cassandra.
30+
*/
31+
public class ColumnsOrderingClause extends OrderingClause {
32+
33+
private final ImmutableMap<CqlIdentifier, ClusteringOrder> orderings;
34+
35+
ColumnsOrderingClause(ImmutableMap<CqlIdentifier, ClusteringOrder> orderings) {
36+
37+
this.orderings = orderings;
38+
}
39+
40+
public static ColumnsOrderingClause create() {
41+
return new ColumnsOrderingClause(ImmutableMap.of());
42+
}
43+
44+
public ColumnsOrderingClause add(
45+
@NonNull CqlIdentifier identifier, @NonNull ClusteringOrder order) {
46+
return new ColumnsOrderingClause(
47+
ImmutableCollections.append(this.orderings, identifier, order));
48+
}
49+
50+
public ColumnsOrderingClause add(@NonNull Map<CqlIdentifier, ClusteringOrder> orderMap) {
51+
return new ColumnsOrderingClause(ImmutableCollections.concat(this.orderings, orderMap));
52+
}
53+
54+
@Override
55+
public void appendTo(@NonNull StringBuilder builder) {
56+
57+
boolean first = true;
58+
for (Map.Entry<CqlIdentifier, ClusteringOrder> entry : orderings.entrySet()) {
59+
if (first) {
60+
builder.append(" ORDER BY ");
61+
first = false;
62+
} else {
63+
builder.append(",");
64+
}
65+
builder.append(entry.getKey().asCql(true)).append(" ").append(entry.getValue().name());
66+
}
67+
}
68+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package com.datastax.oss.driver.api.querybuilder.select;
19+
20+
import com.datastax.oss.driver.api.querybuilder.CqlSnippet;
21+
22+
/**
23+
* Abstract representation of an ordering clause (i.e. ORDER BY) in a CQL statement.
24+
* Alternate implementations may be provided if servers wind up implementing customized
25+
* orderings.
26+
*/
27+
public abstract class OrderingClause implements CqlSnippet {}

0 commit comments

Comments
 (0)