[Enhancement] Support read from catalog through source in dbt (#31641)

Signed-off-by: Astralidea <astralidea@163.com>
This commit is contained in:
Felix Li 2023-09-22 20:16:13 +08:00 committed by GitHub
parent e4485112d0
commit 580f276f7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 5 deletions

View File

@ -100,6 +100,31 @@ models:
```
For materialized view only support partition_by、buckets、distributed_by、properties、refresh_method configuration.
## Read From Catalog
First you need to add this catalog to starrocks. The following is an example of hive.
```mysql
CREATE EXTERNAL CATALOG `hive_catalog`
PROPERTIES (
"hive.metastore.uris" = "thrift://127.0.0.1:8087",
"type"="hive"
);
```
How to add other types of catalogs can be found in the documentation.
https://docs.starrocks.io/en-us/latest/data_source/catalog/catalog_overview
Then write the sources.yaml file.
```yaml
sources:
- name: external_example
schema: hive_catalog.hive_db
tables:
- name: hive_table_name
```
Finally, you might use below marco quote
```
{{ source('external_example', 'hive_table_name') }}
```
## Test Adapter
consult [the project](https://github.com/dbt-labs/dbt-adapter-tests)

View File

@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
version = "1.3.2"
version = "1.4.0"

View File

@ -33,6 +33,7 @@ logger = AdapterLogger("starrocks")
class StarRocksCredentials(Credentials):
host: Optional[str] = None
port: Optional[int] = None
catalog: Optional[str] = 'default_catalog'
database: Optional[str] = None
schema: Optional[str] = None
username: Optional[str] = None
@ -72,8 +73,8 @@ class StarRocksCredentials(Credentials):
return (
"host",
"port",
"database",
"schema",
"catalog",
"username",
)
@ -102,8 +103,8 @@ class StarRocksConnectionManager(SQLConnectionManager):
return connection
credentials = cls.get_credentials(connection.credentials)
kwargs = {"host": credentials.host, "username": credentials.username, "password": credentials.password,
"database": credentials.schema}
kwargs = {"host": credentials.host, "username": credentials.username,
"password": credentials.password, "database": credentials.catalog + "." + credentials.schema}
if credentials.port:
kwargs["port"] = credentials.port

View File

@ -46,6 +46,22 @@ class StarRocksRelation(BaseRelation):
quote_policy: StarRocksQuotePolicy = field(default_factory=lambda: StarRocksQuotePolicy())
quote_character: str = "`"
def quoted(self, identifier):
if '.' in identifier:
catalog_db = identifier.split('.')
catalog = catalog_db[0]
db = catalog_db[1]
return "{quote_char}{catalog}{quote_char}.{quote_char}{db}{quote_char}".format(
quote_char=self.quote_character,
catalog=catalog,
db=db
)
else:
return "{quote_char}{identifier}{quote_char}".format(
quote_char=self.quote_character,
identifier=identifier,
)
@property
def is_materialized_view(self) -> bool:
return self.type == StarRocksRelationType.MaterializedView

View File

@ -41,7 +41,7 @@ with open(os.path.join(this_directory, "README.md")) as f:
package_name = "dbt-starrocks"
# make sure this always matches dbt/adapters/starrocks/__version__.py
package_version = "1.3.2"
package_version = "1.4.0"
description = """The Starrocks adapter plugin for dbt"""