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

[xh]Add date support for oracle destination #4646

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
clean_column_name,
convert_column_to_type,
convert_column_type,
convert_datetime,
)
from mage_integrations.destinations.sql.base import Destination, main
from mage_integrations.destinations.sql.utils import (
Expand Down Expand Up @@ -161,6 +162,7 @@ def build_insert_commands(
columns=columns,
records=records,
convert_column_to_type_func=convert_column_to_type,
convert_datetime_func=convert_datetime,
string_parse_func=lambda x, y: x.replace("'", "''").replace('\\', '\\\\')
if COLUMN_TYPE_OBJECT == y['type'] else x,
use_lowercase=self.use_lowercase,
Expand All @@ -176,9 +178,27 @@ def build_insert_commands(
if unique_constraints and unique_conflict_method:
if UNIQUE_CONFLICT_METHOD_UPDATE == unique_conflict_method:
# Build update command
# Convert insert_value from format:
# CAST('1' AS NUMBER), TO_DATE('2023-09-10', 'yyyy-mm-dd'),
# to_nclob('{\"customer\": \"John Doe\", \"items\": {\"product\": \"Beer\"}'),

# to format:
# ["col1 = CAST('1' AS NUMBER)", "col2 = TO_DATE('2023-09-10', 'yyyy-mm-dd')",
# 'col3 = to_nclob(\'{"customer": "John Doe", "items":
# {"product": "Beer"}}\')']

# Steps:
# - First remove unnecessary punctuation
# (1) ( at beginning (2) ) at the end (3), at the end
# - split by "),"
# - Add back ) at the end of each value
insert_value_without_left_parenthesis = re.sub(r"^\(", "", insert_value)
insert_value_strip = re.sub(r"\)$", "", insert_value_without_left_parenthesis)
updated_values = insert_value_strip.split(',')
insert_value_strip = re.sub(r"\,$", "", insert_value_strip)
splitted_values = insert_value_strip.split('),')
updated_values = [s + ")" if i < len(splitted_values) - 1 else s
matrixstone marked this conversation as resolved.
Show resolved Hide resolved
for i, s in enumerate(splitted_values)]

updated_command = ', '.join([f'{col} = {updated_values[idx].strip()}'
for idx, col in enumerate(columns_cleaned)])
update_command_constraint = ""
Expand Down
24 changes: 22 additions & 2 deletions mage_integrations/mage_integrations/destinations/oracledb/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from typing import Dict, List

from mage_integrations.destinations.sql.constants import SQL_RESERVED_WORDS
Expand Down Expand Up @@ -54,6 +55,24 @@ def convert_column_to_type(value, column_type) -> str:
return f"CAST('{value}' AS {column_type})"


def is_valid_datetime_format(date_string, date_format):
try:
datetime.strptime(date_string, date_format)
return True
except ValueError:
return False


def convert_datetime(value: str, column_type_dict: Dict) -> str:
if is_valid_datetime_format(value, "%Y-%m-%d %H:%M:%S.%f"):
# Oracle stores only the fractions up to second in a DATE field.
# Use TO_TIMESTAMP for milliseconds and microseconds.
return f"TO_TIMESTAMP('{value}', 'yyyy-mm-dd hh24:mi:ss.ff6')"
if is_valid_datetime_format(value, "%Y-%m-%d"):
return f"TO_DATE('{value}', 'yyyy-mm-dd')"
return 'CHAR(255)'


def build_create_table_command(
column_type_mapping: Dict,
columns: List[str],
Expand Down Expand Up @@ -108,7 +127,8 @@ def convert_column_type(column_type: str, column_settings: Dict, **kwargs) -> st
return 'NCLOB'
elif COLUMN_TYPE_STRING == column_type:
if COLUMN_FORMAT_DATETIME == column_settings.get('format'):
# Twice as long as the number of characters in ISO date format
return 'CHAR(52)'
return 'DATE'
elif COLUMN_FORMAT_DATETIME == column_type:
return 'DATE'

return 'CHAR(255)'