Skip to content

Commit

Permalink
Fix View not found error classification
Browse files Browse the repository at this point in the history
View not found errors are currently classified as
GENERIC_INTERNAL_ERROR. This fix introduces a proper
error code VIEW_NOT_FOUND and classifies this as a USER_ERROR
  • Loading branch information
ajaygeorge committed Jul 8, 2024
1 parent 6a0b194 commit cd68435
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.facebook.presto.common.QualifiedObjectName;
import com.facebook.presto.spi.MaterializedViewDefinition;
import com.facebook.presto.spi.PrestoException;
import com.facebook.presto.spi.analyzer.ViewDefinition;

import java.util.Map;
Expand All @@ -23,8 +24,10 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import static com.facebook.presto.spi.StandardErrorCode.VIEW_NOT_FOUND;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Throwables.throwIfUnchecked;
import static java.lang.String.format;

public class MetadataHandle
{
Expand Down Expand Up @@ -60,7 +63,10 @@ public void addTableColumnMetadata(QualifiedObjectName tableName, Future<TableCo

public Optional<ViewDefinition> getViewDefinition(QualifiedObjectName viewName)
{
checkState(viewDefinitions.containsKey(viewName), "View " + viewName + " not found, the available view names are " + viewDefinitions.keySet());
if (!viewDefinitions.containsKey(viewName)) {
throw new PrestoException(VIEW_NOT_FOUND, format("View %s not found, the available view names are: %s", viewName, viewDefinitions.keySet()));
}

try {
return viewDefinitions.get(viewName).get();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.sql.analyzer;

import com.facebook.presto.common.QualifiedObjectName;
import com.facebook.presto.spi.PrestoException;
import com.facebook.presto.spi.analyzer.ViewDefinition;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.Futures;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.Optional;

import static com.facebook.presto.common.type.BigintType.BIGINT;
import static com.facebook.presto.spi.StandardErrorCode.VIEW_NOT_FOUND;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

public class MetadataHandleTest
{
private MetadataHandle metadataHandle;

@BeforeMethod
public void setUp()
{
metadataHandle = new MetadataHandle();
ViewDefinition viewDefinition = new ViewDefinition(
"select a from t1",
Optional.of("tpch"),
Optional.of("s1"),
ImmutableList.of(new ViewDefinition.ViewColumn("a", BIGINT)),
Optional.of("user"),
false);
metadataHandle.addViewDefinition(QualifiedObjectName.valueOf("tpch.s1.t1"), Futures.immediateFuture(Optional.of(viewDefinition)));
}

@Test
public void testGetViewDefinition()
{
Optional<ViewDefinition> viewDefinitionOptional = metadataHandle.getViewDefinition(QualifiedObjectName.valueOf("tpch.s1.t1"));
assertTrue(viewDefinitionOptional.isPresent());
ViewDefinition viewDefinition = viewDefinitionOptional.get();
assertEquals("tpch", viewDefinition.getCatalog().get());
assertEquals("s1", viewDefinition.getSchema().get());
assertEquals("user", viewDefinition.getOwner().get());
assertEquals("select a from t1", viewDefinition.getOriginalSql());
assertFalse(viewDefinition.isRunAsInvoker());
}

@Test
public void testGetViewDefinitionNotPresent()
{
try {
metadataHandle.getViewDefinition(QualifiedObjectName.valueOf("tpch.s1.t2"));
fail("PrestoException not thrown for invalid view");
}
catch (Exception e) {
assertTrue(e instanceof PrestoException);
PrestoException prestoException = (PrestoException) e;
assertEquals(prestoException.getErrorCode(), VIEW_NOT_FOUND.toErrorCode());
assertEquals(prestoException.getMessage(), "View tpch.s1.t2 not found, the available view names are: [tpch.s1.t1]");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public enum StandardErrorCode
INVALID_ARGUMENTS(0x0000_002D, USER_ERROR),
EXCEEDED_PLAN_NODE_LIMIT(0x0000_002E, USER_ERROR),
INVALID_TYPE_DEFINITION(0x0000_002F, USER_ERROR),
VIEW_NOT_FOUND(0x0000_0030, USER_ERROR),

GENERIC_INTERNAL_ERROR(0x0001_0000, INTERNAL_ERROR),
TOO_MANY_REQUESTS_FAILED(0x0001_0001, INTERNAL_ERROR, true),
Expand Down

0 comments on commit cd68435

Please sign in to comment.