|
| 1 | +/* |
| 2 | + * Copyright 2025 Snowflake Inc. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "postgres.h" |
| 19 | +#include "miscadmin.h" |
| 20 | +#include "foreign/foreign.h" |
| 21 | +#include "catalog/pg_foreign_table.h" |
| 22 | + |
| 23 | + |
| 24 | +#include "pg_lake/copy/copy_format.h" |
| 25 | +#include "pg_lake/parsetree/options.h" |
| 26 | +#include "pg_lake/util/catalog_type.h" |
| 27 | +#include "pg_lake/util/rel_utils.h" |
| 28 | + |
| 29 | + |
| 30 | +/* |
| 31 | + * GetIcebergCatalogType returns the IcebergCatalogType for the given |
| 32 | + * relation ID. |
| 33 | + */ |
| 34 | +IcebergCatalogType |
| 35 | +GetIcebergCatalogType(Oid relationId) |
| 36 | +{ |
| 37 | + if (!IsPgLakeIcebergForeignTableById(relationId)) |
| 38 | + return NONE_CATALOG; |
| 39 | + |
| 40 | + ForeignTable *foreignTable = GetForeignTable(relationId); |
| 41 | + List *options = foreignTable->options; |
| 42 | + |
| 43 | + bool hasRestCatalogOption = HasRestCatalogTableOption(options); |
| 44 | + bool hasObjectStoreCatalogOption = HasObjectStoreCatalogTableOption(options); |
| 45 | + bool hasReadOnlyOption = HasReadOnlyOption(options); |
| 46 | + |
| 47 | + if (hasRestCatalogOption && hasReadOnlyOption) |
| 48 | + { |
| 49 | + return REST_CATALOG_READ_ONLY; |
| 50 | + } |
| 51 | + else if (hasRestCatalogOption && !hasReadOnlyOption) |
| 52 | + { |
| 53 | + return REST_CATALOG_READ_WRITE; |
| 54 | + } |
| 55 | + else if (hasObjectStoreCatalogOption && hasReadOnlyOption) |
| 56 | + { |
| 57 | + return OBJECT_STORE_READ_ONLY; |
| 58 | + } |
| 59 | + else if (hasObjectStoreCatalogOption && !hasReadOnlyOption) |
| 60 | + { |
| 61 | + return OBJECT_STORE_READ_WRITE; |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + return POSTGRES_CATALOG; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +/* |
| 71 | + * HasRestCatalogTableOption returns true if the options contain |
| 72 | + * catalog='rest'. |
| 73 | + */ |
| 74 | +bool |
| 75 | +HasRestCatalogTableOption(List *options) |
| 76 | +{ |
| 77 | + char *catalog = GetStringOption(options, "catalog", false); |
| 78 | + |
| 79 | + return catalog ? strncasecmp(catalog, "rest", strlen("rest")) == 0 : false; |
| 80 | +} |
| 81 | + |
| 82 | + |
| 83 | +/* |
| 84 | + * HasObjectStoreCatalogTableOption returns true if the options contain |
| 85 | + * catalog='object_store'. |
| 86 | + */ |
| 87 | +bool |
| 88 | +HasObjectStoreCatalogTableOption(List *options) |
| 89 | +{ |
| 90 | + char *catalog = GetStringOption(options, "catalog", false); |
| 91 | + |
| 92 | + return catalog ? strncasecmp(catalog, "object_store", strlen("object_store")) == 0 : false; |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +/* |
| 97 | + * HasReadOnlyOption returns true if the options contain |
| 98 | + * catalog='read_only'. |
| 99 | + */ |
| 100 | +bool |
| 101 | +HasReadOnlyOption(List *options) |
| 102 | +{ |
| 103 | + char *readOnly = GetStringOption(options, "read_only", false); |
| 104 | + |
| 105 | + return readOnly ? strncasecmp(readOnly, "true", strlen("true")) == 0 : false; |
| 106 | +} |
0 commit comments