forked from apache/datafusion-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcatalog.py
More file actions
146 lines (109 loc) · 4.6 KB
/
catalog.py
File metadata and controls
146 lines (109 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
"""Data catalog providers."""
from __future__ import annotations
from typing import TYPE_CHECKING
import datafusion._internal as df_internal
if TYPE_CHECKING:
import pyarrow as pa
try:
from warnings import deprecated # Python 3.13+
except ImportError:
from typing_extensions import deprecated # Python 3.12
__all__ = [
"Catalog",
"Schema",
"Table",
]
class Catalog:
"""DataFusion data catalog."""
def __init__(self, catalog: df_internal.catalog.RawCatalog) -> None:
"""This constructor is not typically called by the end user."""
self.catalog = catalog
def __repr__(self) -> str:
"""Print a string representation of the catalog."""
return self.catalog.__repr__()
def names(self) -> set[str]:
"""This is an alias for `schema_names`."""
return self.schema_names()
def schema_names(self) -> set[str]:
"""Returns the list of schemas in this catalog."""
return self.catalog.schema_names()
def schema(self, name: str = "public") -> Schema:
"""Returns the database with the given ``name`` from this catalog."""
schema = self.catalog.schema(name)
return (
Schema(schema)
if isinstance(schema, df_internal.catalog.RawSchema)
else schema
)
@deprecated("Use `schema` instead.")
def database(self, name: str = "public") -> Schema:
"""Returns the database with the given ``name`` from this catalog."""
return self.schema(name)
def new_in_memory_schema(self, name: str) -> Schema:
"""Create a new schema in this catalog using an in-memory provider."""
self.catalog.new_in_memory_schema(name)
return self.schema(name)
def register_schema(self, name, schema) -> Schema | None:
"""Register a schema with this catalog."""
return self.catalog.register_schema(name, schema)
def deregister_schema(self, name: str, cascade: bool = True) -> Schema | None:
"""Deregister a schema from this catalog."""
return self.catalog.deregister_schema(name, cascade)
class Schema:
"""DataFusion Schema."""
def __init__(self, schema: df_internal.catalog.RawSchema) -> None:
"""This constructor is not typically called by the end user."""
self._raw_schema = schema
def __repr__(self) -> str:
"""Print a string representation of the schema."""
return self._raw_schema.__repr__()
def names(self) -> set[str]:
"""This is an alias for `table_names`."""
return self.table_names()
def table_names(self) -> set[str]:
"""Returns the list of all tables in this schema."""
return self._raw_schema.table_names
def table(self, name: str) -> Table:
"""Return the table with the given ``name`` from this schema."""
return Table(self._raw_schema.table(name))
def register_table(self, name, table) -> None:
"""Register a table provider in this schema."""
return self._raw_schema.register_table(name, table)
def deregister_table(self, name: str) -> None:
"""Deregister a table provider from this schema."""
return self._raw_schema.deregister_table(name)
@deprecated("Use `Schema` instead.")
class Database(Schema):
"""See `Schema`."""
class Table:
"""DataFusion table."""
def __init__(self, table: df_internal.catalog.RawTable) -> None:
"""This constructor is not typically called by the end user."""
self.table = table
def __repr__(self) -> str:
"""Print a string representation of the table."""
return self.table.__repr__()
@property
def schema(self) -> pa.Schema:
"""Returns the schema associated with this table."""
return self.table.schema
@property
def kind(self) -> str:
"""Returns the kind of table."""
return self.table.kind