ClickHouse/dbms/tests/queries/0_stateless/00921_datetime64_compatibility.python

187 lines
5.4 KiB
Plaintext
Raw Normal View History

2019-09-07 14:22:22 +00:00
#!/usr/bin/env python
# encoding: utf-8
import re
import itertools
2019-10-04 13:49:34 +00:00
import sys
2019-09-07 14:22:22 +00:00
# Create SQL statement to verify dateTime64 is accepted as argument to functions taking DateTime.
functions="""
2019-10-04 13:49:34 +00:00
toTimeZone({datetime}, 'UTC')
2019-09-07 14:22:22 +00:00
toYear({datetime})
toQuarter({datetime})
toMonth({datetime})
toDayOfYear({datetime})
toDayOfMonth({datetime})
toDayOfWeek({datetime})
toHour({datetime})
toMinute({datetime})
toSecond({datetime})
toUnixTimestamp({datetime})
toStartOfYear({datetime})
toStartOfISOYear({datetime})
toStartOfQuarter({datetime})
toStartOfMonth({datetime})
toMonday({datetime})
2019-10-04 13:49:34 +00:00
toStartOfWeek({datetime})
2019-09-07 14:22:22 +00:00
toStartOfDay({datetime})
toStartOfHour({datetime})
toStartOfMinute({datetime})
toStartOfFiveMinute({datetime})
toStartOfTenMinutes({datetime})
toStartOfFifteenMinutes({datetime})
2019-10-04 13:49:34 +00:00
# Do not work with DateTime64
toStartOfInterval({datetime}, INTERVAL 1 year)
toStartOfInterval({datetime}, INTERVAL 1 month)
toStartOfInterval({datetime}, INTERVAL 1 day)
toStartOfInterval({datetime}, INTERVAL 15 minute)
2019-09-07 14:22:22 +00:00
toTime({datetime})
toRelativeYearNum({datetime})
toRelativeQuarterNum({datetime})
toRelativeMonthNum({datetime})
toRelativeWeekNum({datetime})
toRelativeDayNum({datetime})
toRelativeHourNum({datetime})
toRelativeMinuteNum({datetime})
toRelativeSecondNum({datetime})
toISOYear({datetime})
toISOWeek({datetime})
2019-10-04 13:49:34 +00:00
toWeek({datetime})
toYearWeek({datetime})
2019-09-07 14:22:22 +00:00
timeSlot({datetime})
toYYYYMM({datetime})
toYYYYMMDD({datetime})
toYYYYMMDDhhmmss({datetime})
# -- Illegal type DateTime64 of argument of function addYears
2019-10-04 13:49:34 +00:00
addYears({datetime}, 1)
addMonths({datetime}, 1)
addWeeks({datetime}, 1)
addDays({datetime}, 1)
addHours({datetime}, 1)
addMinutes({datetime}, 1)
addSeconds({datetime}, 1)
addQuarters({datetime}, 1)
2019-09-07 14:22:22 +00:00
# -- Illegal type DateTime64 of argument of function subtractYears.
2019-10-04 13:49:34 +00:00
subtractYears({datetime}, 1)
subtractMonths({datetime}, 1)
subtractWeeks({datetime}, 1)
subtractDays({datetime}, 1)
subtractHours({datetime}, 1)
subtractMinutes({datetime}, 1)
subtractSeconds({datetime}, 1)
subtractQuarters({datetime}, 1)
2019-09-07 14:22:22 +00:00
CAST({datetime} as DateTime)
CAST({datetime} as Date)
2019-09-07 14:22:22 +00:00
CAST({datetime} as UInt64)
2019-10-04 13:49:34 +00:00
CAST({datetime} as DateTime64(0))
CAST({datetime} as DateTime64(3))
CAST({datetime} as DateTime64(6))
CAST({datetime} as DateTime64(9))
CAST({datetime} as DateTime64(12))
CAST({datetime} as DateTime64(18))
2019-09-07 14:22:22 +00:00
formatDateTime({datetime}, '%C %d %D %e %F %H %I %j %m %M %n %p %R %S %t %T %u %V %w %y %Y %%')
""".splitlines()
# filter out empty lines and commented out lines
COMMENTED_OUT_LINE_RE = re.compile(r"^\s*#")
functions = list(filter(lambda f: len(f) != 0 and COMMENTED_OUT_LINE_RE.match(f) == None, functions))
# Expanded to cartesian product of all arguments.
# NOTE: {{datetime}} to be turned into {datetime} after str.format() for keys (format string), but not for list of values!
extra_ops =\
[
# With same type:
(
2019-10-04 13:49:34 +00:00
['{{datetime}} {op} {{datetime}}'],
2019-09-07 14:22:22 +00:00
{
'op':
[
'- ', # does not work, but should it?
'+ ', # does not work, but should it?
'!=', '==', # how do we expect this to work?
'< ',
'<=',
'> ',
'>='
]
}
),
# With other DateTime types:
(
2019-10-04 13:49:34 +00:00
[
'{{datetime}} {op} {arg}',
'{arg} {op} {{datetime}}'
],
2019-09-07 14:22:22 +00:00
{
'op':
[
'-', # does not work, but should it?
'!=', '==', # how do we expect this to work?
# these are naturally expected to work, but they don't:
'< ',
'<=',
'> ',
'>='
],
'arg': ['now()', 'toDate(now())'],
2019-09-07 14:22:22 +00:00
}
),
# With arithmetic types
(
2019-10-04 13:49:34 +00:00
[
'{{datetime}} {op} {arg}',
'{arg} {op} {{datetime}}'
],
2019-09-07 14:22:22 +00:00
{
'op':
[
'+ ',
'- ',
'==',
'!=',
'< ',
'<=',
'> ',
'>='
],
'arg':
[
'1',
'-1',
'toInt64(1)',
'toInt64(-1)'
],
},
),
]
# Expand extra_ops here
2019-10-04 13:49:34 +00:00
for funcs, args in extra_ops:
2019-09-07 14:22:22 +00:00
args_keys = args.keys()
2019-10-04 13:49:34 +00:00
for func in funcs:
for args_vals in itertools.product(*args.values()):
result_func = func.format(**dict(zip(args_keys, args_vals)))
functions.append(result_func)
2019-09-07 14:22:22 +00:00
2019-10-04 13:49:34 +00:00
datetime64_args = [ #'now64(0)', 'now64(3)', 'now64(6)', 'now64(9)', 'now64(17)']
'now64(3)']
if sys.version_info[0] > 2:
escape_string_codec = 'unicode_escape'
else:
escape_string_codec = 'string-escape'
def escape_string(s):
return s.encode(escape_string_codec).decode('utf-8')
2019-09-26 15:12:40 +00:00
# TODO: use string.Template here to allow lines that do not contain type, like: SELECT CAST(toDateTime64(1234567890), 'DateTime64')
2019-09-07 14:22:22 +00:00
for func in functions:
for dt in datetime64_args:
2019-10-04 13:49:34 +00:00
f32 = func.format(datetime='now()')
f64 = func.format(datetime=dt)
f = "({f32}) == ({f64})".format(f32=f32, f64=f64)
print("""SELECT '{f64}';""".format(f64=escape_string(f64)))
print("""SELECT {f32};""".format(f32=f32))
print("""SELECT {f64};""".format(f64=f64))
# print("""SELECT {f};""".format(f=f))
print("""SELECT '-----------------------------------------------------------------------------------';""")