From c5740116c05d2d8fb41cbb3633d891a525d0073f Mon Sep 17 00:00:00 2001 From: longfengpili <398745129@qq.com> Date: Mon, 8 Apr 2024 13:54:49 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=96=B0=E5=A2=9E]trino=E6=94=AF=E6=8C=81tran?= =?UTF-8?q?sactional?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pydbapi/api/trino.py | 48 +++++++++++++++++++++++++++++++++----------- setup.py | 4 ++-- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/pydbapi/api/trino.py b/pydbapi/api/trino.py index 3dbe49f..190d186 100644 --- a/pydbapi/api/trino.py +++ b/pydbapi/api/trino.py @@ -2,7 +2,7 @@ # @Author: longfengpili # @Date: 2023-06-02 15:27:41 # @Last Modified by: longfengpili -# @Last Modified time: 2024-03-04 18:28:25 +# @Last Modified time: 2024-04-08 13:52:55 # @github: https://github.com/longfengpili @@ -42,21 +42,45 @@ def create_partition(self, partition): partition = f"with (partitioned_by = ARRAY['{partition.newname}'])" return partition - def create(self, columns, partition=None): - partition_sql = None - if partition: - partition_key = columns.get_column_by_name(partition) - if not partition_key: - raise ValueError(f"<{partition}> not in {columns}") + def table_properties(self, partition: str, format: str = 'ORC', transactional: str = 'true'): + ''' + 查看支持的属性 + SELECT * FROM system.metadata.table_properties; + ''' + coltype = partition.coltype + if not (coltype.startswith('varchar') or coltype == 'date'): + raise TypeError(f"{partition} only support varchar, date !") + + base_properties = f"format = '{format}',\n transactional = {transactional}" + if partition: + partition_property = f"partitioned_by = ARRAY['{partition.newname}']," + table_properties = f''' + with ( + {partition_property} + {base_properties} + ) + ''' + else: + table_properties = f''' + with ( + {base_properties} + ) + ''' + + return table_properties.strip() + + def create(self, columns, partition: str, transactional: str = 'true'): + partition_col = columns.get_column_by_name(partition) + if partition_col: columns.remove(partition) - columns.append(partition_key) - partition_sql = self.create_partition(partition_key) + columns.append(partition_col) + else: + raise ValueError(f"<{partition}> not in {columns}") sql = self.create_nonindex(columns) - - if partition_sql: - sql = sql.replace(';', f'\n{partition_sql};') + table_properties = self.table_properties(partition=partition_col, transactional=transactional) + sql = sql.replace(';', f'\n{table_properties};') return sql diff --git a/setup.py b/setup.py index 7541b51..e45085c 100644 --- a/setup.py +++ b/setup.py @@ -2,13 +2,13 @@ # @Author: longfengpili # @Date: 2023-06-02 15:27:41 # @Last Modified by: longfengpili -# @Last Modified time: 2024-03-01 11:43:49 +# @Last Modified time: 2024-04-08 13:54:17 # @github: https://github.com/longfengpili import setuptools -VERSION = '0.0.120' +VERSION = '0.0.121' PROJECT_NAME = 'pydbapi' with open('README.md', 'r', encoding='utf-8') as f: