Skip to content

Commit

Permalink
Update the error message if column doesn't exist in projection
Browse files Browse the repository at this point in the history
  • Loading branch information
Hazel-Datastax committed Dec 20, 2024
1 parent 1a3af31 commit 8b362f2
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public ProjectionException(ErrorInstance errorInstance) {
}

public enum Code implements ErrorCode<ProjectionException> {
UNSUPPORTED_COLUMN_TYPES;
UNSUPPORTED_COLUMN_TYPES,
UNKNOWN_TABLE_COLUMNS;

private final ErrorTemplate<ProjectionException> template;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,15 @@ public static <CmdT extends Projectable> TableProjection fromDefinition(

// TODO: A table can't be with empty columns. Think a redundant check.
if (columns.isEmpty()) {
throw ErrorCodeV1.UNSUPPORTED_PROJECTION_DEFINITION.toApiException(
"did not include any Table columns");
throw ProjectionException.Code.UNKNOWN_TABLE_COLUMNS.get(
errVars(
table,
map -> {
map.put("allColumns", errFmtApiColumnDef(table.apiTableDef().allColumns()));
map.put(
"unknownColumns",
command.tableProjectionDefinition().getColumnNames().toString());
}));
}

// result set has ColumnDefinitions not ColumnMetadata kind of weird
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ private TableProjectionDefinition(boolean inclusion, List<String> columnNames) {
this.columnNames = columnNames;
}

/**
* Return the column defined in the projection.
*
* @return a new {@code ArrayList} containing the column names.
*/
public List<String> getColumnNames() {
return new ArrayList<>(columnNames);
}

public static TableProjectionDefinition createFromDefinition(JsonNode projectionDefinition) {
// First special case: "simple" default projection; "include all"
if (projectionDefinition == null || projectionDefinition.isEmpty()) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/resources/errors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,17 @@ request-errors:
Resend the command using only supported column types.
- scope: PROJECTION
code: UNKNOWN_TABLE_COLUMNS
title: Columns in the projection are not defined in the table schema
body: |-
Only columns defined in the table schema can be included in the projection.
The table ${keyspace}.${table} defines the columns: ${allColumns}.
The projection included the following unknown columns: ${unknownColumns}.
${SNIPPET.RESEND_USING_ONLY_DEFINED_COLUMNS}
# ================================================================================================================
# Family: REQUEST Scope: SCHEMA
# ================================================================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.stargate.sgv2.jsonapi.api.model.command.CommandName;
import io.stargate.sgv2.jsonapi.api.v1.util.DataApiResponseValidator;
import io.stargate.sgv2.jsonapi.api.v1.util.scenarios.AllScalarTypesTableScenario;
import io.stargate.sgv2.jsonapi.exception.ProjectionException;
import io.stargate.sgv2.jsonapi.fixtures.types.ApiDataTypesForTesting;
import io.stargate.sgv2.jsonapi.service.schema.tables.ApiColumnDefContainer;
import io.stargate.sgv2.jsonapi.service.schema.tables.ApiDataTypeDefs;
Expand Down Expand Up @@ -143,6 +144,19 @@ public void findManyProjectionMissingColumns() {
.doesNotHaveProjectionSchemaWith("MISSING_COLUMN");
}

@Test
public void findProjectionUnknownColumns() {

// Select a column that does not exist, should not be in the projection schema
assertTableCommand(keyspaceName, TABLE_NAME)
.templated()
.find(Map.of("id", "row-1"), List.of("FAKE2", "FAKE1"), Map.of(), Map.of())
.hasSingleApiError(
ProjectionException.Code.UNKNOWN_TABLE_COLUMNS,
ProjectionException.class,
"The projection included the following unknown columns: [FAKE2, FAKE1]");
}

private DataApiResponseValidator assertProjectionSchema(
DataApiResponseValidator validator, ApiColumnDefContainer columns) {
columns.values().forEach(validator::hasProjectionSchemaWith);
Expand Down

0 comments on commit 8b362f2

Please sign in to comment.