Skip to content

Commit

Permalink
opti macro
Browse files Browse the repository at this point in the history
  • Loading branch information
yhilmare committed Jun 18, 2024
1 parent 3f434a8 commit e36d907
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
14 changes: 14 additions & 0 deletions dbt/adapters/oceanbase_mysql/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,25 @@
# 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.
from typing import Dict

from dbt.adapters.base import Column


class OBMySQLColumn(Column):

_CAST_TYPE_ALIAS: Dict[str, str] = {
"int": "signed integer",
"bigint": "signed integer",
"nchar": "character",
"char": "character",
"varchar": "character",
}

@property
def quoted(self) -> str:
return "`{}`".format(self.column)

@classmethod
def translate_cast_type(cls, dtype: str) -> str:
return cls._CAST_TYPE_ALIAS.get(dtype, dtype)
4 changes: 4 additions & 0 deletions dbt/adapters/oceanbase_mysql/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,7 @@ def parse_index(self, raw_index: Dict[str, Any]) -> OBMySQLIndex:
return OBMySQLIndex.from_dict(raw_index)
except Exception as e:
raise DbtValidationError(f"Could not parse constraint: {raw_index}")

@available
def translate_cast_type(self, dtype: str) -> str:
return OBMySQLColumn.translate_cast_type(dtype)
6 changes: 1 addition & 5 deletions dbt/include/oceanbase_mysql/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,7 @@
{%- do col_err.append(col['name']) -%}
{%- endif -%}
{% set col_name = adapter.quote(col['name']) if col.get('quote') else col['name'] %}
{%- if col['data_type'].strip().lower() in ('int', 'bigint') -%}
cast(null as signed integer) as {{ col_name }}{{ ", " if not loop.last }}
{% else %}
cast(null as {{ col['data_type'] }}) as {{ col_name }}{{ ", " if not loop.last }}
{%- endif -%}
cast(null as {{ adapter.translate_cast_type(col['data_type']) }}) as {{ col_name }}{{ ", " if not loop.last }}
{%- endfor -%}
{%- if (col_err | length) > 0 -%}
{{ exceptions.column_type_missing(column_names=col_err) }}
Expand Down
2 changes: 1 addition & 1 deletion dbt/include/oceanbase_mysql/macros/relations/view/drop.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{% macro oceanbase_mysql__drop_view(relation) -%}
drop view id exists {{ relation.include(schema=False) }}
drop view if exists {{ relation.include(schema=False) }}
{%- endmacro %}

0 comments on commit e36d907

Please sign in to comment.