|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#pragma once |
| 17 | + |
| 18 | +#include "velox/connectors/Connector.h" |
| 19 | +#include "velox/connectors/hive/FileHandle.h" |
| 20 | +#include "velox/connectors/hive/HiveConfig.h" |
| 21 | + |
| 22 | +namespace facebook::velox::connector::hive::iceberg { |
| 23 | + |
| 24 | +/// Provides Iceberg table format support. |
| 25 | +/// - Creates HiveDataSource instances that use IcebergSplitReader for reading. |
| 26 | +/// Iceberg tables with support for delete files and schema evolution. |
| 27 | +/// - Creates IcebergDataSink instances for writing data with Iceberg-specific |
| 28 | +/// partition transforms and commit metadata. |
| 29 | +class IcebergConnector final : public Connector { |
| 30 | + public: |
| 31 | + IcebergConnector( |
| 32 | + const std::string& id, |
| 33 | + std::shared_ptr<const config::ConfigBase> config, |
| 34 | + folly::Executor* ioExecutor); |
| 35 | + |
| 36 | + bool canAddDynamicFilter() const override { |
| 37 | + return true; |
| 38 | + } |
| 39 | + |
| 40 | + /// Creates a data source for reading Iceberg tables. Returns a HiveDataSource |
| 41 | + /// that will use IcebergSplitReader when processing splits marked with |
| 42 | + /// table_format="hive-iceberg" in customSplitInfo. |
| 43 | + std::unique_ptr<DataSource> createDataSource( |
| 44 | + const RowTypePtr& outputType, |
| 45 | + const ConnectorTableHandlePtr& tableHandle, |
| 46 | + const ColumnHandleMap& columnHandles, |
| 47 | + ConnectorQueryCtx* connectorQueryCtx) override; |
| 48 | + |
| 49 | + bool supportsSplitPreload() const override { |
| 50 | + return true; |
| 51 | + } |
| 52 | + |
| 53 | + /// Creates a data sink for writing to Iceberg tables. Always returns an |
| 54 | + /// IcebergDataSink that handles Iceberg-specific partition transforms and |
| 55 | + /// generates commit metadata in the format expected by Iceberg catalogs. |
| 56 | + /// |
| 57 | + /// @param inputType The schema of the input data to write |
| 58 | + /// @param connectorInsertTableHandle Must be an IcebergInsertTableHandle |
| 59 | + /// containing Iceberg-specific write configuration |
| 60 | + /// @param connectorQueryCtx Query context for the write operation |
| 61 | + /// @param commitStrategy Strategy for committing the write operation |
| 62 | + /// @return IcebergDataSink instance configured for the write operation |
| 63 | + /// @throws VeloxUserError if connectorInsertTableHandle is not an |
| 64 | + /// IcebergInsertTableHandle |
| 65 | + std::unique_ptr<DataSink> createDataSink( |
| 66 | + RowTypePtr inputType, |
| 67 | + ConnectorInsertTableHandlePtr connectorInsertTableHandle, |
| 68 | + ConnectorQueryCtx* connectorQueryCtx, |
| 69 | + CommitStrategy commitStrategy) override; |
| 70 | + |
| 71 | + folly::Executor* ioExecutor() const override { |
| 72 | + return ioExecutor_; |
| 73 | + } |
| 74 | + |
| 75 | + FileHandleCacheStats fileHandleCacheStats() { |
| 76 | + return fileHandleFactory_.cacheStats(); |
| 77 | + } |
| 78 | + |
| 79 | + // NOTE: this is to clear file handle cache which might affect performance, |
| 80 | + // and is only used for operational purposes. |
| 81 | + FileHandleCacheStats clearFileHandleCache() { |
| 82 | + return fileHandleFactory_.clearCache(); |
| 83 | + } |
| 84 | + |
| 85 | + private: |
| 86 | + const std::shared_ptr<HiveConfig> hiveConfig_; |
| 87 | + FileHandleFactory fileHandleFactory_; |
| 88 | + folly::Executor* ioExecutor_; |
| 89 | +}; |
| 90 | + |
| 91 | +/// Factory for creating IcebergConnector instances. |
| 92 | +class IcebergConnectorFactory final : public ConnectorFactory { |
| 93 | + public: |
| 94 | + static constexpr const char* kIcebergConnectorName = "iceberg"; |
| 95 | + |
| 96 | + IcebergConnectorFactory() : ConnectorFactory(kIcebergConnectorName) {} |
| 97 | + |
| 98 | + std::shared_ptr<Connector> newConnector( |
| 99 | + const std::string& id, |
| 100 | + std::shared_ptr<const config::ConfigBase> config, |
| 101 | + folly::Executor* ioExecutor = nullptr, |
| 102 | + folly::Executor* cpuExecutor = nullptr) override { |
| 103 | + return std::make_shared<IcebergConnector>(id, config, ioExecutor); |
| 104 | + } |
| 105 | +}; |
| 106 | + |
| 107 | +} // namespace facebook::velox::connector::hive::iceberg |
0 commit comments