Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support alias in SQL API #542

Merged
merged 1 commit into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Support alias in SQL API
  • Loading branch information
brfrn169 committed Apr 5, 2022
commit 3ac08ddb5808eb1dcf5da7b161ad100efe7bf77f
23 changes: 14 additions & 9 deletions core/src/main/java/com/scalar/db/sql/DmlStatementExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ public ResultSet visit(SelectStatement statement, TransactionCrudOperable transa

Selection selection = convertSelectStatementToSelection(statement, metadata);

ImmutableList<String> projectedColumnNames =
statement.projectedColumnNames.isEmpty()
? ImmutableList.copyOf(metadata.getColumnNames())
: statement.projectedColumnNames;
List<Projection> projections =
statement.projections.isEmpty()
? metadata.getColumnNames().stream()
.map(Projection::column)
.collect(Collectors.toList())
: statement.projections;

try {
if (selection instanceof Get) {
Expand All @@ -70,11 +72,11 @@ public ResultSet visit(SelectStatement statement, TransactionCrudOperable transa
r ->
(ResultSet)
new ResultIteratorResultSet(
Collections.singletonList(r).iterator(), projectedColumnNames))
Collections.singletonList(r).iterator(), projections))
.orElse(EmptyResultSet.INSTANCE);
} else {
List<Result> results = transaction.scan((Scan) selection);
return new ResultIteratorResultSet(results.iterator(), projectedColumnNames);
return new ResultIteratorResultSet(results.iterator(), projections);
}
} catch (CrudConflictException e) {
throw new TransactionConflictException("Conflict happened during selecting a record", e);
Expand Down Expand Up @@ -136,13 +138,16 @@ private Selection convertSelectStatementToSelection(
ImmutableListMultimap<String, Predicate> predicatesMap =
Multimaps.index(statement.predicates, c -> c.columnName);

List<String> projectedColumnNames =
statement.projections.stream().map(p -> p.columnName).collect(Collectors.toList());

if (SqlUtils.isIndexScan(predicatesMap, metadata)) {
String indexColumnName = predicatesMap.keySet().iterator().next();
Scan scan =
new Scan(
createKeyFromPredicatesMap(
predicatesMap, Collections.singletonList(indexColumnName)))
.withProjections(statement.projectedColumnNames)
.withProjections(projectedColumnNames)
.forNamespace(statement.namespaceName)
.forTable(statement.tableName);
if (statement.limit > 0) {
Expand All @@ -159,13 +164,13 @@ private Selection convertSelectStatementToSelection(
clusteringKey = createKeyFromPredicatesMap(predicatesMap, metadata.getClusteringKeyNames());
}
return new Get(partitionKey, clusteringKey)
.withProjections(statement.projectedColumnNames)
.withProjections(projectedColumnNames)
.forNamespace(statement.namespaceName)
.forTable(statement.tableName);
} else {
Scan scan =
new Scan(partitionKey)
.withProjections(statement.projectedColumnNames)
.withProjections(projectedColumnNames)
.forNamespace(statement.namespaceName)
.forTable(statement.tableName);
setClusteringKeyRangeForScan(scan, predicatesMap, metadata);
Expand Down
51 changes: 51 additions & 0 deletions core/src/main/java/com/scalar/db/sql/Projection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.scalar.db.sql;

import com.google.common.base.MoreObjects;
import java.util.Objects;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

@Immutable
public class Projection {

public final String columnName;
@Nullable public final String alias;

private Projection(String columnName, @Nullable String alias) {
this.columnName = Objects.requireNonNull(columnName);
this.alias = alias;
}

public Projection as(String alias) {
return new Projection(columnName, alias);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("columnName", columnName)
.add("alias", alias)
.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Projection)) {
return false;
}
Projection that = (Projection) o;
return Objects.equals(columnName, that.columnName) && Objects.equals(alias, that.alias);
}

@Override
public int hashCode() {
return Objects.hash(columnName, alias);
}

public static Projection column(String columnName) {
return new Projection(columnName, null);
}
}
20 changes: 10 additions & 10 deletions core/src/main/java/com/scalar/db/sql/ResultIteratorResultSet.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.scalar.db.sql;

import com.google.common.collect.ImmutableList;
import com.scalar.db.api.Result;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import javax.annotation.concurrent.NotThreadSafe;
Expand All @@ -11,34 +11,34 @@
public class ResultIteratorResultSet implements ResultSet {

private final Iterator<Result> iterator;
private final ImmutableList<String> projectedColumnNames;
private final List<Projection> projections;

ResultIteratorResultSet(Iterator<Result> iterator, ImmutableList<String> projectedColumnNames) {
ResultIteratorResultSet(Iterator<Result> iterator, List<Projection> projections) {
this.iterator = Objects.requireNonNull(iterator);
this.projectedColumnNames = Objects.requireNonNull(projectedColumnNames);
this.projections = Objects.requireNonNull(projections);
}

@Override
public Optional<Record> one() {
if (iterator.hasNext()) {
return Optional.of(new ResultRecord(iterator.next(), projectedColumnNames));
return Optional.of(new ResultRecord(iterator.next(), projections));
}
return Optional.empty();
}

@Override
public Iterator<Record> iterator() {
return new ResultIterator(iterator, projectedColumnNames);
return new ResultIterator(iterator, projections);
}

private static class ResultIterator implements Iterator<Record> {

private final Iterator<Result> iterator;
private final ImmutableList<String> projectedColumnNames;
private final List<Projection> projections;

public ResultIterator(Iterator<Result> iterator, ImmutableList<String> projectedColumnNames) {
public ResultIterator(Iterator<Result> iterator, List<Projection> projections) {
this.iterator = iterator;
this.projectedColumnNames = projectedColumnNames;
this.projections = projections;
}

@Override
Expand All @@ -48,7 +48,7 @@ public boolean hasNext() {

@Override
public Record next() {
return new ResultRecord(iterator.next(), projectedColumnNames);
return new ResultRecord(iterator.next(), projections);
}
}
}
Loading