Skip to content

Commit

Permalink
Support alias in SQL API
Browse files Browse the repository at this point in the history
  • Loading branch information
brfrn169 committed Apr 4, 2022
1 parent 6bf9d72 commit 32b09b8
Show file tree
Hide file tree
Showing 14 changed files with 468 additions and 136 deletions.
24 changes: 15 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,13 @@ public ResultSet visit(SelectStatement statement, TransactionCrudOperable transa

Selection selection = convertSelectStatementToSelection(statement, metadata);

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

try {
if (selection instanceof Get) {
Expand All @@ -70,11 +73,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 +139,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 +165,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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,34 @@
public class ResultIteratorResultSet implements ResultSet {

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

ResultIteratorResultSet(Iterator<Result> iterator, ImmutableList<String> projectedColumnNames) {
ResultIteratorResultSet(Iterator<Result> iterator, ImmutableList<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 ImmutableList<Projection> projections;

public ResultIterator(Iterator<Result> iterator, ImmutableList<String> projectedColumnNames) {
public ResultIterator(Iterator<Result> iterator, ImmutableList<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

0 comments on commit 32b09b8

Please sign in to comment.