|
18 | 18 |
|
19 | 19 | package org.apache.flink.connector.jdbc.table;
|
20 | 20 |
|
| 21 | +import org.apache.flink.api.dag.Transformation; |
21 | 22 | import org.apache.flink.configuration.Configuration;
|
22 | 23 | import org.apache.flink.connector.jdbc.testutils.DatabaseTest;
|
23 | 24 | import org.apache.flink.connector.jdbc.testutils.TableManaged;
|
24 | 25 | import org.apache.flink.connector.jdbc.testutils.tables.TableRow;
|
25 | 26 | import org.apache.flink.runtime.testutils.MiniClusterResourceConfiguration;
|
26 | 27 | import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
|
27 |
| -import org.apache.flink.table.api.DataTypes; |
28 |
| -import org.apache.flink.table.api.TableEnvironment; |
| 28 | +import org.apache.flink.table.api.*; |
| 29 | +import org.apache.flink.table.api.bridge.internal.AbstractStreamTableEnvironmentImpl; |
29 | 30 | import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
|
| 31 | +import org.apache.flink.table.catalog.*; |
30 | 32 | import org.apache.flink.table.connector.source.lookup.cache.LookupCache;
|
31 | 33 | import org.apache.flink.table.data.DecimalData;
|
32 | 34 | import org.apache.flink.table.data.GenericRowData;
|
33 | 35 | import org.apache.flink.table.data.RowData;
|
34 | 36 | import org.apache.flink.table.data.TimestampData;
|
| 37 | +import org.apache.flink.table.delegation.Executor; |
| 38 | +import org.apache.flink.table.delegation.Planner; |
| 39 | +import org.apache.flink.table.factories.CatalogStoreFactory; |
| 40 | +import org.apache.flink.table.factories.PlannerFactoryUtil; |
| 41 | +import org.apache.flink.table.factories.TableFactoryUtil; |
| 42 | +import org.apache.flink.table.module.ModuleManager; |
| 43 | +import org.apache.flink.table.operations.ModifyOperation; |
35 | 44 | import org.apache.flink.table.planner.factories.TestValuesTableFactory;
|
36 | 45 | import org.apache.flink.table.planner.runtime.utils.StreamTestSink;
|
| 46 | +import org.apache.flink.table.resource.ResourceManager; |
37 | 47 | import org.apache.flink.table.runtime.functions.table.lookup.LookupCacheManager;
|
38 | 48 | import org.apache.flink.table.test.lookup.cache.LookupCacheAssert;
|
39 | 49 | import org.apache.flink.test.junit5.MiniClusterExtension;
|
40 | 50 | import org.apache.flink.types.Row;
|
41 | 51 | import org.apache.flink.util.CollectionUtil;
|
| 52 | +import org.apache.flink.util.FlinkUserCodeClassLoaders; |
| 53 | +import org.apache.flink.util.MutableURLClassLoader; |
42 | 54 |
|
43 | 55 | import org.junit.jupiter.api.AfterEach;
|
44 | 56 | import org.junit.jupiter.api.BeforeEach;
|
|
48 | 60 | import org.junit.jupiter.params.provider.EnumSource;
|
49 | 61 |
|
50 | 62 | import java.math.BigDecimal;
|
| 63 | +import java.net.URL; |
51 | 64 | import java.sql.Connection;
|
52 | 65 | import java.sql.SQLException;
|
53 | 66 | import java.time.LocalDateTime;
|
54 | 67 | import java.time.temporal.ChronoUnit;
|
55 | 68 | import java.time.temporal.TemporalUnit;
|
56 |
| -import java.util.Arrays; |
57 |
| -import java.util.Collection; |
58 |
| -import java.util.Collections; |
59 |
| -import java.util.HashMap; |
60 |
| -import java.util.List; |
61 |
| -import java.util.Map; |
| 69 | +import java.util.*; |
62 | 70 | import java.util.stream.Collectors;
|
63 | 71 |
|
64 | 72 | import static org.apache.flink.connector.jdbc.testutils.tables.TableBuilder.field;
|
@@ -140,6 +148,24 @@ void afterEach() {
|
140 | 148 | StreamTestSink.clear();
|
141 | 149 | }
|
142 | 150 |
|
| 151 | + @Test |
| 152 | + void testJdbcSourceName() { |
| 153 | + String testTable = "testTable"; |
| 154 | + final List<Transformation<?>> transformationCollector = new ArrayList<>(); |
| 155 | + tEnv = |
| 156 | + createTestingTableEnv( |
| 157 | + StreamExecutionEnvironment.getExecutionEnvironment(), |
| 158 | + EnvironmentSettings.newInstance().build(), |
| 159 | + transformationCollector); |
| 160 | + tEnv.executeSql(inputTable.getCreateQueryForFlink(getMetadata(), testTable)); |
| 161 | + tEnv.executeSql("SELECT * FROM " + testTable); |
| 162 | + assertThat(transformationCollector).hasSize(1); |
| 163 | + assertThat(transformationCollector.get(0).getName()) |
| 164 | + .isNotEqualTo( |
| 165 | + JdbcDynamicTableSource.getDynamicJdbcTableSourceName( |
| 166 | + "default_catalog.default_database." + testTable)); |
| 167 | + } |
| 168 | + |
143 | 169 | @Test
|
144 | 170 | void testJdbcSource() {
|
145 | 171 | String testTable = "testTable";
|
@@ -585,6 +611,91 @@ protected TemporalUnit timestampPrecision() {
|
585 | 611 | return ChronoUnit.MICROS;
|
586 | 612 | }
|
587 | 613 |
|
| 614 | + private TableEnvironment createTestingTableEnv( |
| 615 | + StreamExecutionEnvironment executionEnvironment, |
| 616 | + EnvironmentSettings settings, |
| 617 | + final List<Transformation<?>> transformationCollector) { |
| 618 | + |
| 619 | + final MutableURLClassLoader userClassLoader = |
| 620 | + FlinkUserCodeClassLoaders.create( |
| 621 | + new URL[0], settings.getUserClassLoader(), settings.getConfiguration()); |
| 622 | + final Executor executor = |
| 623 | + AbstractStreamTableEnvironmentImpl.lookupExecutor( |
| 624 | + userClassLoader, executionEnvironment); |
| 625 | + |
| 626 | + final TableConfig tableConfig = TableConfig.getDefault(); |
| 627 | + tableConfig.setRootConfiguration(executor.getConfiguration()); |
| 628 | + tableConfig.addConfiguration(settings.getConfiguration()); |
| 629 | + |
| 630 | + final ResourceManager resourceManager = |
| 631 | + new ResourceManager(settings.getConfiguration(), userClassLoader); |
| 632 | + final ModuleManager moduleManager = new ModuleManager(); |
| 633 | + |
| 634 | + final CatalogStoreFactory catalogStoreFactory = |
| 635 | + TableFactoryUtil.findAndCreateCatalogStoreFactory( |
| 636 | + settings.getConfiguration(), userClassLoader); |
| 637 | + final CatalogStoreFactory.Context catalogStoreFactoryContext = |
| 638 | + TableFactoryUtil.buildCatalogStoreFactoryContext( |
| 639 | + settings.getConfiguration(), userClassLoader); |
| 640 | + catalogStoreFactory.open(catalogStoreFactoryContext); |
| 641 | + final CatalogStore catalogStore = |
| 642 | + settings.getCatalogStore() != null |
| 643 | + ? settings.getCatalogStore() |
| 644 | + : catalogStoreFactory.createCatalogStore(); |
| 645 | + |
| 646 | + final CatalogManager catalogManager = |
| 647 | + CatalogManager.newBuilder() |
| 648 | + .classLoader(userClassLoader) |
| 649 | + .config(tableConfig) |
| 650 | + .defaultCatalog( |
| 651 | + settings.getBuiltInCatalogName(), |
| 652 | + new GenericInMemoryCatalog( |
| 653 | + settings.getBuiltInCatalogName(), |
| 654 | + settings.getBuiltInDatabaseName())) |
| 655 | + .executionConfig(executionEnvironment.getConfig()) |
| 656 | + .catalogModificationListeners( |
| 657 | + TableFactoryUtil.findCatalogModificationListenerList( |
| 658 | + settings.getConfiguration(), userClassLoader)) |
| 659 | + .catalogStoreHolder( |
| 660 | + CatalogStoreHolder.newBuilder() |
| 661 | + .classloader(userClassLoader) |
| 662 | + .config(tableConfig) |
| 663 | + .catalogStore(catalogStore) |
| 664 | + .factory(catalogStoreFactory) |
| 665 | + .build()) |
| 666 | + .build(); |
| 667 | + |
| 668 | + final FunctionCatalog functionCatalog = |
| 669 | + new FunctionCatalog(tableConfig, resourceManager, catalogManager, moduleManager); |
| 670 | + |
| 671 | + final Planner planner = |
| 672 | + PlannerFactoryUtil.createPlanner( |
| 673 | + executor, |
| 674 | + tableConfig, |
| 675 | + userClassLoader, |
| 676 | + moduleManager, |
| 677 | + catalogManager, |
| 678 | + functionCatalog); |
| 679 | + |
| 680 | + return new AbstractStreamTableEnvironmentImpl( |
| 681 | + catalogManager, |
| 682 | + moduleManager, |
| 683 | + resourceManager, |
| 684 | + tableConfig, |
| 685 | + executor, |
| 686 | + functionCatalog, |
| 687 | + planner, |
| 688 | + settings.isStreamingMode(), |
| 689 | + executionEnvironment) { |
| 690 | + @Override |
| 691 | + protected List<Transformation<?>> translate(List<ModifyOperation> modifyOperations) { |
| 692 | + List<Transformation<?>> transformations = super.translate(modifyOperations); |
| 693 | + transformationCollector.addAll(transformations); |
| 694 | + return transformations; |
| 695 | + } |
| 696 | + }; |
| 697 | + } |
| 698 | + |
588 | 699 | private LocalDateTime truncateTime(LocalDateTime value) {
|
589 | 700 | return value.truncatedTo(timestampPrecision());
|
590 | 701 | }
|
|
0 commit comments