Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support dbt v1.2 #6

Merged
merged 31 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
f514198
support dbt v1.1
shiyuhang0 Jul 26, 2022
3099471
fix error
shiyuhang0 Jul 26, 2022
c40944c
Lift + shift for cross-db macros
shiyuhang0 Jul 28, 2022
fcf99ba
retry connection logical
shiyuhang0 Jul 29, 2022
ac9a784
fix github action fail
shiyuhang0 Jul 29, 2022
2b15a2e
test alter name
shiyuhang0 Jul 29, 2022
7d168fd
support grant
shiyuhang0 Jul 31, 2022
2c6fc2e
test python 3.10
shiyuhang0 Aug 2, 2022
b321e24
fix error test in test_tidb.py
shiyuhang0 Aug 1, 2022
7160510
fmt will be done in another pr
shiyuhang0 Aug 2, 2022
50b2aa1
add code annotation
shiyuhang0 Aug 2, 2022
945ffdc
doc
shiyuhang0 Aug 2, 2022
d071f22
fix doc
shiyuhang0 Aug 5, 2022
f3a38ea
Update dbt/include/tidb/macros/apply_grants.sql
zhangyangyu Aug 18, 2022
8e83b30
fix github action
shiyuhang0 Aug 18, 2022
3d1c446
Merge remote-tracking branch 'origin/dbt_1.2' into dbt_1.2
shiyuhang0 Aug 18, 2022
0cdb5d7
fix typo
shiyuhang0 Aug 18, 2022
c404104
fix port
shiyuhang0 Aug 18, 2022
1fbc02a
Apply suggestions from code review
zhangyangyu Aug 18, 2022
5e3df9b
fix port typo
shiyuhang0 Aug 18, 2022
dc412cb
Merge remote-tracking branch 'origin/dbt_1.2' into dbt_1.2
shiyuhang0 Aug 18, 2022
f2e83c0
test grant
shiyuhang0 Aug 18, 2022
84de1ef
fix tidb5_1
shiyuhang0 Aug 19, 2022
cbf0f65
fix typo
shiyuhang0 Aug 19, 2022
b0f175e
fix tidb4.x
shiyuhang0 Aug 19, 2022
64b0b21
add test for tidb 5.3
shiyuhang0 Aug 19, 2022
d64b591
fix typo
shiyuhang0 Aug 20, 2022
eca9828
Merge branch 'main' into dbt_1.2
shiyuhang0 Aug 22, 2022
ff4d04d
fmt
shiyuhang0 Aug 22, 2022
7caaae2
fix version
shiyuhang0 Aug 22, 2022
c1dad1b
link fix
shiyuhang0 Aug 22, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
156 changes: 126 additions & 30 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,142 @@ on:
branches: main

jobs:
build:
name: Python 3.8 | TiDB nightly | Ubuntu latest
shiyuhang0 marked this conversation as resolved.
Show resolved Hide resolved
tidb_nightly:
name: Python ${{ matrix.python-version }} | TiDB nightly
runs-on: ubuntu-latest

strategy:
matrix:
python-version: [ '3.8', '3.9', '3.10' ]

services:
tidb_nightly:
image: pingcap/tidb:nightly
ports:
- 4000:4000

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install requirements
run: |
pip install -r requirements_dev.txt

- name: Run tests
run: |
mysql -P4000 -uroot -h127.0.0.1 < tests/functional/adapter/tidb/grant/create_user.sql
export DBT_TEST_USER_1=user1
export DBT_TEST_USER_2=user2
export DBT_TEST_USER_3=user3
PYTHONPATH=. pytest tests/functional/adapter/tidb

tidb_5_3:
name: Python ${{ matrix.python-version }} | TiDB 5.3
runs-on: ubuntu-latest

strategy:
matrix:
python-version: [ '3.8', '3.9', '3.10' ]

services:
tidb_nightly:
image: pingcap/tidb:v5.3.2
ports:
- 4000:4000

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install requirements
run: |
pip install -r requirements_dev.txt

- name: Run tests
run: |
mysql -P4000 -uroot -h127.0.0.1 < tests/functional/adapter/tidb/grant/create_user.sql
export DBT_TEST_USER_1=user1
export DBT_TEST_USER_2=user2
export DBT_TEST_USER_3=user3
PYTHONPATH=. pytest tests/functional/adapter/tidb

tidb_5_1:
name: Python ${{ matrix.python-version }} | TiDB 5.1
runs-on: ubuntu-latest

strategy:
matrix:
python-version: [ '3.8', '3.9', '3.10' ]

services:
tidb_5.1:
image: pingcap/tidb:v5.1.0
image: pingcap/tidb:v5.1.4
ports:
- 4001:4000
- 4000:4000

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install requirements
run: |
pip install -r requirements_dev.txt

- name: Run tests
run: |
mysql -P4000 -uroot -h127.0.0.1 < tests/functional/adapter/tidb5_1/grant/create_user.sql
export DBT_TEST_USER_1=user1
export DBT_TEST_USER_2=user2
export DBT_TEST_USER_3=user3
PYTHONPATH=. pytest tests/functional/adapter/tidb5_1

tidb_4_0:
name: Python ${{ matrix.python-version }} | TiDB 4.0
runs-on: ubuntu-latest

strategy:
matrix:
python-version: [ '3.8', '3.9', '3.10' ]

services:
tidb_4.0:
image: pingcap/tidb:v4.0.0
image: pingcap/tidb:v4.0.16
ports:
- 4002:4000
- 4000:4000

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: 3.8

- name: Install requirements
run: |
pip install -r requirements_dev.txt

- name: Run tests tidb_nightly
run: |
PYTHONPATH="${PYTHONPATH}:dbt" pytest test/integration/tidb.dbtspec

- name: Run tests tidb_v5.0.1 up
run: |
PYTHONPATH="${PYTHONPATH}:dbt" pytest test/integration/tidb_v5.1-v5.2.dbtspec

- name: Run tests tidb_v4.0.0 up
run: |
PYTHONPATH="${PYTHONPATH}:dbt" pytest test/integration/tidb_v4.0-v5.0.dbtspec

- name: Checkout
uses: actions/checkout@v2

- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install requirements
run: |
pip install -r requirements_dev.txt

- name: Run tests
run: |
mysql -P4000 -uroot -h127.0.0.1 < tests/functional/adapter/tidb4_0/grant/create_user.sql
export DBT_TEST_USER_1=user1
export DBT_TEST_USER_2=user2
export DBT_TEST_USER_3=user3
PYTHONPATH=. pytest tests/functional/adapter/tidb4_0
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ dbt-integration-tests
test/integration/.user.yml
.DS_Store
.vscode
logs
80 changes: 55 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
The `dbt-tidb` package contains all of the code enabling [dbt](https://getdbt.com) to work with
[TiDB](https://en.pingcap.com/tidb/).

This repository is based on [dbt-mysql](https://github.com/dbeatty10/dbt-mysql).
This repository is partly based on [dbt-mysql](https://github.com/dbeatty10/dbt-mysql).
Thanks to them for their excellent work.

## Table of Contents
* [Installation](#installation)
* [Supported features](#supported-features)
* [Supported functions](#supported-functions)
* [Profile Configuration](#profile-configuration)
* [Database User Privileges](#database-user-privileges)
* [Running Tests](#running-tests)
Expand All @@ -35,17 +36,19 @@ $ pip install dbt-tidb
```
## Supported features

| TiDB 4.X | TiDB 5.0 ~ 5.2 | TiDB >= 5.3 | Feature |
|:--------------:|:--------------:|:----------------:|:------------------------------:|
| ✅ | ✅ | ✅ | Table materialization |
| ✅ | ✅ | ✅ | View materialization |
| ✅ | ✅ | ✅ | Incremental materialization |
| ❌ | ❌ | ✅ | Ephemeral materialization |
| ✅ | ✅ | ✅ | Seeds |
| ✅ | ✅ | ✅ | Sources |
| ✅ | ✅ | ✅ | Custom data tests |
| ✅ | ✅ | ✅ | Docs generate |
| ❌ | ❌ | ✅ | Snapshots |
| TiDB 4.X | TiDB 5.0 ~ 5.2 | TiDB >= 5.3 | Feature |
|:--------------:|:--------------:|:----------------:|:---------------------------:|
| ✅ | ✅ | ✅ | Table materialization |
| ✅ | ✅ | ✅ | View materialization |
| ❌ | ❌ | ✅ | Incremental materialization |
| ❌ | ✅ | ✅ | Ephemeral materialization |
| ✅ | ✅ | ✅ | Seeds |
| ✅ | ✅ | ✅ | Sources |
| ✅ | ✅ | ✅ | Custom data tests |
| ✅ | ✅ | ✅ | Docs generate |
| ❌ | ❌ | ✅ | Snapshots |
| ✅ | ✅ | ✅ | Connection retry |
| ✅ | ✅ | ✅ | Grant |

Note:

Expand All @@ -55,7 +58,32 @@ Note:
* TiDB 4.X does not support using SQL func in `CREATE VIEW`, avoid it in your SQL code.
You can find more detail [here](https://github.com/pingcap/tidb/pull/27252).

### Profile Configuration
## Supported functions

cross-db macros are moved from dbt-utils into dbt-core, so you can use the following functions directly, see [dbt-util](https://github.com/dbt-labs/dbt-utils) on how to use them.
- bool_or
- cast_bool_to_text
- dateadd
- datediff
- date_trunc
- hash
- safe_cast
- split_part
- last_day
- cast_bool_to_text
- concat
- escape_single_quotes
- except
- intersect
- length
- position
- replace
- right
- listagg (not support yet)

> pay attention that datediff is a little different from dbt-util that it will round down rather than round up.

## Profile Configuration

TiDB targets should be set up using the following configuration in your `profiles.yml` file.

Expand All @@ -72,18 +100,20 @@ your_profile_name:
schema: database_name
username: tidb_username
password: tidb_password
retries: 2
```

| Option | Description | Required? | Example |
| --------------- | ------------------------------------------------------ |-----------|--------------------------------|
| type | The specific adapter to use | Required | `tidb` |
| server | The server (hostname) to connect to | Required | `yourorg.tidb.com` |
| port | The port to use | Required | `4000` |
| schema | Specify the schema (database) to build models into | Required | `analytics` |
| username | The username to use to connect to the server | Required | `dbt_admin` |
| password | The password to use for authenticating to the server | Required | `correct-horse-battery-staple` |
| Option | Description | Required? | Example |
|------------------|-------------------------------------------------------|-----------|--------------------------------|
| type | The specific adapter to use | Required | `tidb` |
| server | The server (hostname) to connect to | Required | `yourorg.tidb.com` |
| port | The port to use | Required | `4000` |
| schema | Specify the schema (database) to build models into | Required | `analytics` |
| username | The username to use to connect to the server | Required | `dbt_admin` |
| password | The password to use for authenticating to the server | Required | `correct-horse-battery-staple` |
| retries | The retry times for connection to TiDB (1 in default) | Optional | `2` |

### Database User Privileges
## Database User Privileges

Your database user would be able to have some abilities to read or write, such as `SELECT`, `CREATE`, and so on.
You can find some help [here](https://docs.pingcap.com/tidb/v4.0/privilege-management) with TiDB privileges management.
Expand All @@ -100,14 +130,14 @@ You can find some help [here](https://docs.pingcap.com/tidb/v4.0/privilege-manag
| SHOW VIEW |
| SUPER |

### Running Tests
## Running Tests

See [test/README.md](test/README.md) for details on running the integration tests.

### Example
## Example

Click [here](https://github.com/pingcap/dbt-tidb/wiki/Primer-Tutorial-%7C-How-to-use-dbt-with-TiDB) to see a simple example about using dbt with dbt-tidb.

### Contributing
## Contributing

Welcome to contribute for dbt-tidb. See [Contributing Guide](CONTRIBUTING.md) for more information.
46 changes: 19 additions & 27 deletions dbt/adapters/tidb/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
@dataclass(init=False)
class TiDBCredentials(Credentials):
server: str
schema: str
port: Optional[int] = None
database: Optional[str] = None
schema: str
username: Optional[str] = None
password: Optional[str] = None
charset: Optional[str] = None
retries: int = 1

_ALIASES = {
"UID": "username",
Expand Down Expand Up @@ -85,36 +86,27 @@ def open(cls, connection):
kwargs["host"] = credentials.server
kwargs["user"] = credentials.username
kwargs["passwd"] = credentials.password
kwargs["buffered"] = True

if credentials.port:
kwargs["port"] = credentials.port

try:
connection.handle = mysql.connector.connect(**kwargs)
connection.state = 'open'
except mysql.connector.Error:

try:
logger.debug("Failed connection without supplying the `database`. "
"Trying again with `database` included.")

# Try again with the database included
kwargs["database"] = credentials.schema

connection.handle = mysql.connector.connect(**kwargs)
connection.state = 'open'
except mysql.connector.Error as e:

logger.debug("Got an error when attempting to open a tidb "
"connection: '{}'"
.format(e))

connection.handle = None
connection.state = 'fail'

raise dbt.exceptions.FailedToConnectException(str(e))

return connection
def connect():
handle = mysql.connector.connect(**kwargs)
return handle

# we just retry for any error now
retryable_exceptions = [
mysql.connector.Error
]

return cls.retry_connection(
connection,
connect=connect,
logger=logger,
retry_limit=credentials.retries,
retryable_exceptions=retryable_exceptions,
)

@classmethod
def get_credentials(cls, credentials):
Expand Down