Skip to content

Commit

Permalink
Fix error message on row dereference
Browse files Browse the repository at this point in the history
  • Loading branch information
wendigo committed Sep 12, 2024
1 parent 47f75aa commit 325f50d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@
import static io.trino.sql.analyzer.ExpressionTreeUtils.extractWindowExpressions;
import static io.trino.sql.analyzer.PatternRecognitionAnalysis.NavigationAnchor.FIRST;
import static io.trino.sql.analyzer.PatternRecognitionAnalysis.NavigationAnchor.LAST;
import static io.trino.sql.analyzer.SemanticExceptions.invalidReferenceException;
import static io.trino.sql.analyzer.SemanticExceptions.missingAttributeException;
import static io.trino.sql.analyzer.SemanticExceptions.semanticException;
import static io.trino.sql.analyzer.TypeSignatureProvider.fromTypes;
Expand Down Expand Up @@ -852,7 +853,8 @@ protected Type visitDereferenceExpression(DereferenceExpression node, Context co
}

if (rowFieldType == null) {
throw missingAttributeException(node, qualifiedName);
throw invalidReferenceException(node, Optional.ofNullable(qualifiedName)
.orElseGet(() -> QualifiedName.of(fieldName)));
}

return setExpressionType(node, rowFieldType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import static io.trino.spi.StandardErrorCode.AMBIGUOUS_NAME;
import static io.trino.spi.StandardErrorCode.COLUMN_NOT_FOUND;
import static io.trino.spi.StandardErrorCode.INVALID_COLUMN_REFERENCE;
import static io.trino.sql.analyzer.ExpressionTreeUtils.extractLocation;
import static java.lang.String.format;

Expand All @@ -34,6 +35,11 @@ public static TrinoException missingAttributeException(Expression node, Qualifie
throw semanticException(COLUMN_NOT_FOUND, node, "Column '%s' cannot be resolved", name);
}

public static TrinoException invalidReferenceException(Expression node, QualifiedName name)
{
throw semanticException(INVALID_COLUMN_REFERENCE, node, "Column reference '%s' is invalid", name);
}

public static TrinoException ambiguousAttributeException(Expression node, QualifiedName name)
{
throw semanticException(AMBIGUOUS_NAME, node, "Column '%s' is ambiguous", name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,14 @@ public void testHavingReferencesOutputAlias()
.hasMessage("line 1:32: Column 'x' cannot be resolved");
}

@Test
public void testRowReferencesUnknownField()
{
assertFails("SELECT row('a', 'b', 'c').field")
.hasErrorCode(INVALID_COLUMN_REFERENCE)
.hasMessage("line 1:8: Column reference 'field' is invalid");
}

@Test
public void testSelectAllColumns()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@
import java.util.Map;
import java.util.stream.Stream;

import static io.trino.spi.StandardErrorCode.INVALID_COLUMN_REFERENCE;
import static io.trino.spi.type.DoubleType.DOUBLE;
import static io.trino.spi.type.VarcharType.VARCHAR;
import static io.trino.testing.MaterializedResult.resultBuilder;
import static io.trino.testing.TestingNames.randomNameSuffix;
import static io.trino.testing.assertions.TrinoExceptionAssert.assertTrinoExceptionThrownBy;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -1047,8 +1049,9 @@ public void testNestedVariants()
"SELECT a.b.c FROM nested_variants",
"VALUES 'value1', 'value2', 'value3', 'value4'");

assertThatThrownBy(() -> computeActual("SELECT a.\"b.c\" FROM nested_variants"))
.hasMessageContaining("Column 'a.b.c' cannot be resolved");
assertTrinoExceptionThrownBy(() -> computeActual("SELECT a.\"b.c\" FROM nested_variants"))
.hasErrorCode(INVALID_COLUMN_REFERENCE)
.hasMessageContaining("Column reference 'a.b.c' is invalid");
}

@Test
Expand Down

0 comments on commit 325f50d

Please sign in to comment.