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

[CALCITE-6624] SqlParser should parse MySQL DATETIME type #4002

Open
wants to merge 1 commit into
base: main
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions babel/src/main/codegen/config.fmpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ data: {
"CYCLE"
"DATA"
# "DATE"
"DATETIME"
"DATETIME_DIFF"
"DAY"
"DEALLOCATE"
Expand Down
14 changes: 14 additions & 0 deletions babel/src/test/java/org/apache/calcite/test/BabelQuidemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ public static void main(String[] args) throws Exception {
typeFactory.createSqlType(
SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE)))
.connect();
case "scott-mysql":
return CalciteAssert.that()
.with(CalciteAssert.SchemaSpec.SCOTT)
.with(CalciteConnectionProperty.FUN, "standard,mysql")
.with(CalciteConnectionProperty.LEX, Lex.MYSQL)
.with(CalciteConnectionProperty.PARSER_FACTORY,
BabelDdlExecutor.class.getName() + "#PARSER_FACTORY")
.with(CalciteConnectionProperty.CONFORMANCE,
SqlConformanceEnum.BABEL)
.with(CalciteConnectionProperty.LENIENT_OPERATOR_LOOKUP, true)
.with(
ConnectionFactories.addType("DATETIME", typeFactory ->
typeFactory.createSqlType(SqlTypeName.TIMESTAMP)))
.connect();
case "scott-postgresql":
return CalciteAssert.that()
.with(CalciteAssert.SchemaSpec.SCOTT)
Expand Down
39 changes: 39 additions & 0 deletions babel/src/test/resources/sql/mysql.iq
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# mysql.iq - Babel test for MySQL dialect of SQL
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to you under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.
#
!use scott-mysql
!set outputformat mysql

SELECT cast(cast(TIMESTAMP'1000-01-01 00:00:00' as DATETIME) as DATETIME);
+---------------------+
| EXPR$0 |
+---------------------+
| 1000-01-01 00:00:00 |
+---------------------+
(1 row)

!ok

SELECT cast(cast(TIMESTAMP'9999-12-31 23:59:59' as DATETIME) as DATETIME);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also write DATETIME '1000-01-01 00:00:00'?
Are you supposed to?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MySQL syntax is kind of messy

  1. Time literals can only be defined using DATE 'str', TIME 'str', or TIMESTAMP 'str'. There is no syntax like DATETIME 'str'.

  2. The TIMESTAMP syntax produces a DATETIME value in MySQL because DATETIME has a range that more closely corresponds to the standard SQL TIMESTAMP type, which has a year range from 0001 to 9999. (The MySQL TIMESTAMP year range is 1970 to 2038.)

Currently, SELECT DATETIME '1000-01-01 00:00:00' works in Calcite when using the MySQL dialect. Ideally, it should throw an exception. However, I’m not sure if anyone actually needs this behavior or if it’s worth spending time on.

+---------------------+
| EXPR$0 |
+---------------------+
| 9999-12-31 23:59:59 |
+---------------------+
(1 row)

!ok
Loading