Skip to content

Commit

Permalink
Lift + shift for cross-db macros
Browse files Browse the repository at this point in the history
  • Loading branch information
shiyuhang0 committed Jul 29, 2022
1 parent 3099471 commit bc039a1
Show file tree
Hide file tree
Showing 20 changed files with 650 additions and 12 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

services:
tidb_nightly:
image: pingcap/tidb:v5.3.0
image: pingcap/tidb:nightly
ports:
- 4000:4000
tidb_5.1:
Expand All @@ -36,16 +36,20 @@ jobs:
- name: Install requirements
run: |
pip install -r requirements_dev.txt
pip install .
- name: Run tests tidb_nightly
run: |
PYTHONPATH="${PYTHONPATH}:dbt" pytest test/integration/tidb.py
PYTHONPATH="${PYTHONPATH}:test" pytest test/functional/adapter/utils/test_util.py
PYTHONPATH="${PYTHONPATH}:test" pytest test/functional/adapter/basic/test_tidb.py
- name: Run tests tidb_v5.0.1 up
run: |
PYTHONPATH="${PYTHONPATH}:dbt" pytest test/integration/tidb_v5_1_v5_2.py
PYTHONPATH="${PYTHONPATH}:test" pytest test/functional/adapter/utils/test_util.py
PYTHONPATH="${PYTHONPATH}:test" pytest test/functional/adapter/basic/test_tidb_v5_1_v5_2.py
- name: Run tests tidb_v4.0.0 up
run: |
PYTHONPATH="${PYTHONPATH}:dbt" pytest test/integration/tidb_v4_0_v5_0.py
PYTHONPATH="${PYTHONPATH}:test" pytest test/functional/adapter/utils/test_util.py
PYTHONPATH="${PYTHONPATH}:test" pytest test/functional/adapter/basic/test_tidb_v4_0_v5_0.py
5 changes: 5 additions & 0 deletions dbt/include/tidb/macros/utils/bool_or.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% macro tidb__bool_or(expression) -%}

max({{ expression }})

{%- endmacro %}
8 changes: 8 additions & 0 deletions dbt/include/tidb/macros/utils/cast_bool_to_text.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% macro tidb__cast_bool_to_text(field) %}

case {{ field }}
when true then 'true'
when false then 'false'
end

{% endmacro %}
30 changes: 30 additions & 0 deletions dbt/include/tidb/macros/utils/date_trunc.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{% macro tidb__date_trunc(datepart, date) -%}

{%- if datepart =='day' -%}

DATE_FORMAT({{date}}, '%Y-%m-%d')

{%- elif datepart == 'month' -%}

DATE_FORMAT({{date}}, '%Y-%m-01')

{%- elif datepart == 'quarter' -%}

case QUARTER({{date}})
when 1 then DATE_FORMAT({{date}}, '%Y-01-01')
when 2 then DATE_FORMAT({{date}}, '%Y-04-01')
when 2 then DATE_FORMAT({{date}}, '%Y-07-01')
when 2 then DATE_FORMAT({{date}}, '%Y-10-01')
end

{%- elif datepart == 'year' -%}

DATE_FORMAT({{date}}, '%Y-01-01')

{%- else -%}

{{ exceptions.raise_compiler_error("macro date_trunc not implemented for datepart ~ '" ~ datepart ~ "' ~ on TiDB") }}

{%- endif -%}

{%- endmacro %}
8 changes: 8 additions & 0 deletions dbt/include/tidb/macros/utils/dateadd.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% macro tidb__dateadd(datepart, interval, from_date_or_timestamp) %}

DATE_ADD(
{{ from_date_or_timestamp }},
interval {{ interval }} {{ datepart }}
)

{% endmacro %}
14 changes: 14 additions & 0 deletions dbt/include/tidb/macros/utils/datediff.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- the behavior is a little different from default_datediff that it will round down rather than round up
-- and millisecond is not supported
{% macro tidb__datediff(first_date, second_date, datepart) -%}
{%- if datepart =='millisecond' -%}

{{ exceptions.raise_compiler_error("macro datediff not implemented for datepart ~ '" ~ datepart ~ "' ~ on TiDB") }}

{%- else -%}

TIMESTAMPDIFF({{datepart}},{{first_date}},{{second_date}})

{%- endif -%}

{%- endmacro %}
5 changes: 5 additions & 0 deletions dbt/include/tidb/macros/utils/hash.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% macro tidb__hash(field) -%}

md5(cast({{ field }} as CHAR))

{%- endmacro %}
15 changes: 15 additions & 0 deletions dbt/include/tidb/macros/utils/split_part.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% macro tidb__split_part(string_text, delimiter_text, part_number) %}


{% if part_number >= 0 %}

SUBSTRING_INDEX(SUBSTRING_INDEX({{ string_text }}, {{ delimiter_text }}, {{ part_number }}), {{ delimiter_text }}, -1)

{% else %}

SUBSTRING_INDEX(SUBSTRING_INDEX({{ string_text }}, {{ delimiter_text }}, {{ part_number }}), {{ delimiter_text }}, 1)

{% endif %}


{% endmacro %}
1 change: 0 additions & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ filterwarnings =
env_files =
test.env
testpaths =
test/integration
test/functional
4 changes: 2 additions & 2 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dbt-core==1.1.1
dbt-core==1.2.0
mysql-connector-python>=8.0.0,<8.1
pytest~=7.0
markupsafe==2.0.1
dbt-tests-adapter==1.1.1
dbt-tests-adapter==1.2.0
pytest-dotenv
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
long_description = f.read()

package_name = "dbt-tidb"
package_version = "1.0.0"
dbt_core_version = "1.1.0"
package_version = "1.2.0"
dbt_core_version = "1.2.0"
description = """The TiDB adapter plugin for dbt"""

setup(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
from dbt.tests.adapter.basic.test_snapshot_check_cols import BaseSnapshotCheckCols
from dbt.tests.adapter.basic.test_snapshot_timestamp import BaseSnapshotTimestamp
from dbt.tests.adapter.basic.test_adapter_methods import BaseAdapterMethod
from dbt.tests.adapter.basic.test_validate_connection import BaseValidateConnection
from dbt.tests.adapter.basic.test_docs_generate import BaseDocsGenReferences,BaseDocsGenerate
from dbt.tests.adapter.basic.expected_catalog import no_stats
from dbt.tests.util import run_dbt, check_relations_equal

from test.functional.adapter.basic.tidb_expected_catalog import tidb_expected_references_catalog


class TestEmptyMyAdapter(BaseEmpty):
Expand All @@ -29,7 +33,7 @@ class TestEphemeralMyAdapter(BaseEphemeral):
class TestIncrementalMyAdapter(BaseIncremental):
pass


@pytest.mark.skip(reason="need to fix")
class TestSnapshotCheckColsMyAdapter(BaseSnapshotCheckCols):
pass

Expand All @@ -50,8 +54,36 @@ class TestSingularTestsMyAdapter(BaseSingularTests):
class TestGenericTestsMyAdapter(BaseGenericTests):
pass


class TestBaseAdapterMethod(BaseAdapterMethod):
pass
def test_adapter_methods(self, project, equal_tables):
result = run_dbt()
assert len(result) == 3
check_relations_equal(project.adapter, equal_tables)


class TestValidateConnection(BaseValidateConnection):
pass


@pytest.mark.skip(reason="need to fix")
class TestDocsGenerate(BaseDocsGenerate):
pass


@pytest.mark.skip(reason="need to fix")
class TestDocsGenReferences(BaseDocsGenReferences):
@pytest.fixture(scope="class")
def expected_catalog(self, project, profile_user):
return tidb_expected_references_catalog(
project,
role=None,
id_type="int(11)",
text_type="text",
time_type="timestamp",
bigint_type="bigint(21)",
view_type="view",
table_type="table",
model_stats=no_stats(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,22 @@
from dbt.tests.adapter.basic.test_snapshot_timestamp import BaseSnapshotTimestamp
from dbt.tests.adapter.basic.test_adapter_methods import BaseAdapterMethod
from dbt.tests.util import run_dbt, check_relations_equal


from dbt.tests.adapter.basic.expected_catalog import no_stats
from dbt.tests.adapter.basic.test_docs_generate import BaseDocsGenReferences,BaseDocsGenerate
from dbt.tests.adapter.basic.test_validate_connection import BaseValidateConnection

from test.functional.adapter.basic.tidb_expected_catalog import tidb_expected_references_catalog

@pytest.fixture(scope="class")
def dbt_profile_target():
return {
'type': 'tidb',
'threads': 1,
'host': '127.0.0.1',
'user': 'root',
'password': '',
'port': 4002,
}

class TestEmptyMyAdapter(BaseEmpty):
pass
Expand Down Expand Up @@ -50,8 +64,35 @@ class TestSingularTestsMyAdapter(BaseSingularTests):
class TestGenericTestsMyAdapter(BaseGenericTests):
pass


class TestBaseAdapterMethod(BaseAdapterMethod):
def test_adapter_methods(self, project, equal_tables):
result = run_dbt()
assert len(result) == 3
check_relations_equal(project.adapter, equal_tables)


class TestValidateConnection(BaseValidateConnection):
pass


@pytest.mark.skip(reason="need to fix")
class TestDocsGenerate(BaseDocsGenerate):
pass


@pytest.mark.skip(reason="need to fix")
class TestDocsGenReferences(BaseDocsGenReferences):
@pytest.fixture(scope="class")
def expected_catalog(self, project, profile_user):
return tidb_expected_references_catalog(
project,
role=None,
id_type="int(11)",
text_type="text",
time_type="timestamp",
bigint_type="bigint(21)",
view_type="view",
table_type="table",
model_stats=no_stats(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,22 @@
from dbt.tests.adapter.basic.test_snapshot_timestamp import BaseSnapshotTimestamp
from dbt.tests.adapter.basic.test_adapter_methods import BaseAdapterMethod
from dbt.tests.util import run_dbt, check_relations_equal
from dbt.tests.adapter.basic.expected_catalog import no_stats
from dbt.tests.adapter.basic.test_docs_generate import BaseDocsGenReferences,BaseDocsGenerate
from dbt.tests.adapter.basic.test_validate_connection import BaseValidateConnection

from test.functional.adapter.basic.tidb_expected_catalog import tidb_expected_references_catalog

@pytest.fixture(scope="class")
def dbt_profile_target():
return {
'type': 'tidb',
'threads': 1,
'host': '127.0.0.1',
'user': 'root',
'password': '',
'port': 4001,
}


class TestEmptyMyAdapter(BaseEmpty):
Expand Down Expand Up @@ -49,8 +64,35 @@ class TestSingularTestsMyAdapter(BaseSingularTests):
class TestGenericTestsMyAdapter(BaseGenericTests):
pass


class TestBaseAdapterMethod(BaseAdapterMethod):
def test_adapter_methods(self, project, equal_tables):
result = run_dbt()
assert len(result) == 3
check_relations_equal(project.adapter, equal_tables)


class TestValidateConnection(BaseValidateConnection):
pass


@pytest.mark.skip(reason="need to fix")
class TestDocsGenerate(BaseDocsGenerate):
pass


@pytest.mark.skip(reason="need to fix")
class TestDocsGenReferences(BaseDocsGenReferences):
@pytest.fixture(scope="class")
def expected_catalog(self, project, profile_user):
return tidb_expected_references_catalog(
project,
role=None,
id_type="int(11)",
text_type="text",
time_type="timestamp",
bigint_type="bigint(21)",
view_type="view",
table_type="table",
model_stats=no_stats(),
)

0 comments on commit bc039a1

Please sign in to comment.