Skip to content

Commit 49bfbde

Browse files
authored
[fix](fe) Avoid table handle lookups for external views (apache#69946)
### What problem does this PR solve? Related PR: apache#69836 Selecting an external Iceberg REST view can fail during query binding with `BadRequestException: ... is not an Iceberg compatible table`, even when the catalog supports reading the view definition. `BindRelation` loads statement snapshots before expanding the view SQL. The snapshot loader calls `PluginDrivenExternalTable.resolveConnectorTableHandle`, which unconditionally asks the connector for a table handle using the view name. A REST catalog can reject that table request with HTTP 400 instead of reporting a missing table. The Iceberg connector only converts `NoSuchTableException` to an empty handle, so the error escapes before the existing view-existence fallback can run. Resolve the object's view classification before requesting a table handle. Known views return no table handle and retain the live view-existence check, so a view deleted outside Doris still invalidates its cached lookup. Snapshot loading can then return an empty view pin and continue to view expansion; the referenced base tables keep their normal snapshot handling. Ordinary table handles and table-load errors retain their existing behavior. This addresses the shared handle-resolution boundary, including callers outside the snapshot loader. apache#69836 proposes skipping the snapshot-loader entry instead. ### Release note Fix external view queries failing when an Iceberg REST catalog rejects table metadata requests for view names. ### Check List (For Author) - Test - [ ] Regression test - [x] Unit Test - [ ] Manual test - [ ] No need to test or manual test. - Behavior changed: - [ ] No. - [x] Yes. External views no longer issue a table-handle request during resolution. - Does this need documentation? - [x] No. - [ ] Yes. Validation: - `run-fe-ut.sh --run` with `PluginDrivenExternalViewHandleTest`, `PluginDrivenExternalTableTest`, `PluginDrivenMvccExternalTableTest`, and `PluginDrivenMissingTableTest`: **164 tests passed**, with no failures, errors, or skips. - Before the fix, both view cases failed with the reproduced table-endpoint error; the ordinary table cases passed. - `mvn checkstyle:check -pl fe-core`: **0 violations**. - `git diff --check` passed. No live Databricks integration test was run.
1 parent 44dfa15 commit 49bfbde

2 files changed

Lines changed: 115 additions & 1 deletion

File tree

‎fe/fe-core/src/main/java/org/apache/doris/datasource/plugin/PluginDrivenExternalTable.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ public Optional<ConnectorTableHandle> resolveConnectorTableHandle(
140140
}
141141
});
142142
String dbName = db != null ? db.getRemoteName() : "";
143-
Optional<ConnectorTableHandle> handle = metadata.getTableHandle(session, dbName, getRemoteName());
143+
// Some catalogs reject table requests for views with non-404 errors. Classify views before
144+
// probing the table endpoint so snapshot loading can continue to view expansion.
145+
Optional<ConnectorTableHandle> handle = supportsView() && isView()
146+
? Optional.empty() : metadata.getTableHandle(session, dbName, getRemoteName());
144147
// A view has no table handle. Check live view existence before treating an empty handle as a
145148
// dropped table; the cached isView flag can outlive a remote view deletion.
146149
if (!handle.isPresent() && !metadata.viewExists(session, dbName, getRemoteName())) {
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.doris.datasource.plugin;
19+
20+
import org.apache.doris.common.jmockit.Deencapsulation;
21+
import org.apache.doris.connector.spi.Connector;
22+
import org.apache.doris.connector.spi.ConnectorCapability;
23+
import org.apache.doris.connector.spi.ConnectorMetadata;
24+
import org.apache.doris.connector.spi.ConnectorSession;
25+
import org.apache.doris.connector.spi.DorisConnectorException;
26+
import org.apache.doris.connector.spi.handle.ConnectorTableHandle;
27+
import org.apache.doris.datasource.ExternalDatabase;
28+
29+
import org.junit.jupiter.api.Assertions;
30+
import org.junit.jupiter.api.Test;
31+
import org.mockito.Mockito;
32+
33+
import java.util.Optional;
34+
import java.util.Set;
35+
36+
public class PluginDrivenExternalViewHandleTest {
37+
@Test
38+
public void viewDoesNotProbeTableEndpoint() {
39+
Fixture f = new Fixture(true);
40+
Mockito.when(f.metadata.viewExists(f.session, "remote_db", "remote_object")).thenReturn(true);
41+
Mockito.when(f.metadata.getTableHandle(f.session, "remote_db", "remote_object"))
42+
.thenThrow(new DorisConnectorException("Object is not an Iceberg compatible table"));
43+
44+
Assertions.assertTrue(f.table.resolveConnectorTableHandle(f.session, f.metadata).isEmpty());
45+
46+
Mockito.verify(f.metadata, Mockito.never()).getTableHandle(Mockito.any(), Mockito.any(), Mockito.any());
47+
Mockito.verifyNoInteractions(f.invalidate);
48+
}
49+
50+
@Test
51+
public void deletedViewStillInvalidatesLookup() {
52+
Fixture f = new Fixture(true);
53+
// The locally cached view classification can survive an out-of-band view deletion.
54+
Mockito.when(f.metadata.viewExists(f.session, "remote_db", "remote_object")).thenReturn(false);
55+
Mockito.when(f.metadata.getTableHandle(f.session, "remote_db", "remote_object"))
56+
.thenThrow(new DorisConnectorException("Object is not an Iceberg compatible table"));
57+
58+
Assertions.assertTrue(f.table.resolveConnectorTableHandle(f.session, f.metadata).isEmpty());
59+
60+
Mockito.verify(f.invalidate).run();
61+
Mockito.verify(f.metadata, Mockito.never()).getTableHandle(Mockito.any(), Mockito.any(), Mockito.any());
62+
}
63+
64+
@Test
65+
public void ordinaryTableStillResolvesHandle() {
66+
Fixture f = new Fixture(false);
67+
ConnectorTableHandle handle = Mockito.mock(ConnectorTableHandle.class);
68+
Mockito.when(f.metadata.getTableHandle(f.session, "remote_db", "remote_object"))
69+
.thenReturn(Optional.of(handle));
70+
71+
Assertions.assertSame(handle, f.table.resolveConnectorTableHandle(f.session, f.metadata).orElseThrow());
72+
73+
Mockito.verify(f.metadata, Mockito.never()).viewExists(Mockito.any(), Mockito.any(), Mockito.any());
74+
Mockito.verifyNoInteractions(f.invalidate);
75+
}
76+
77+
@Test
78+
public void ordinaryTableLoadFailureStillPropagates() {
79+
Fixture f = new Fixture(false);
80+
DorisConnectorException failure = new DorisConnectorException("Table metadata request failed");
81+
Mockito.when(f.metadata.getTableHandle(f.session, "remote_db", "remote_object")).thenThrow(failure);
82+
83+
Assertions.assertSame(failure, Assertions.assertThrows(DorisConnectorException.class,
84+
() -> f.table.resolveConnectorTableHandle(f.session, f.metadata)));
85+
86+
Mockito.verifyNoInteractions(f.invalidate);
87+
}
88+
89+
private static class Fixture {
90+
final ConnectorMetadata metadata = Mockito.mock(ConnectorMetadata.class);
91+
final ConnectorSession session = Mockito.mock(ConnectorSession.class);
92+
final Runnable invalidate = Mockito.mock(Runnable.class);
93+
final PluginDrivenExternalTable table =
94+
Mockito.mock(PluginDrivenExternalTable.class, Mockito.CALLS_REAL_METHODS);
95+
96+
Fixture(boolean view) {
97+
Connector connector = Mockito.mock(Connector.class);
98+
Mockito.when(connector.getCapabilities()).thenReturn(Set.of(ConnectorCapability.SUPPORTS_VIEW));
99+
PluginDrivenExternalCatalog catalog = Mockito.mock(PluginDrivenExternalCatalog.class);
100+
Mockito.when(catalog.getConnector()).thenReturn(connector);
101+
ExternalDatabase<?> database = Mockito.mock(ExternalDatabase.class);
102+
Mockito.when(database.getRemoteName()).thenReturn("remote_db");
103+
Mockito.when(database.observeTableLookup(Mockito.eq(table), Mockito.any()))
104+
.thenReturn(Optional.of(invalidate));
105+
Deencapsulation.setField(table, "catalog", catalog);
106+
Deencapsulation.setField(table, "db", database);
107+
Deencapsulation.setField(table, "remoteName", "remote_object");
108+
Mockito.doReturn(view).when(table).isView();
109+
}
110+
}
111+
}

0 commit comments

Comments
 (0)