Skip to content

Commit

Permalink
[CALCITE-6624] SqlParser should parse MySQL DATETIME type
Browse files Browse the repository at this point in the history
  • Loading branch information
dssysolyatin authored and dssysolyatin committed Oct 15, 2024
1 parent df2328b commit 492a2ae
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
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(DATETIME '1000-01-01 00:00:00' as DATETIME) as DATETIME);
+---------------------+
| EXPR$0 |
+---------------------+
| 1000-01-01 00:00:00 |
+---------------------+
(1 row)

!ok

SELECT cast(cast(DATETIME '9999-12-31 23:59:59' as DATETIME) as DATETIME);
+---------------------+
| EXPR$0 |
+---------------------+
| 9999-12-31 23:59:59 |
+---------------------+
(1 row)

!ok
8 changes: 7 additions & 1 deletion core/src/main/codegen/templates/Parser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -6154,7 +6154,7 @@ SqlTypeNameSpec CharacterTypeName(Span s) :
}

/**
* Parse datetime types: date, time, timestamp.
* Parse datetime types: date, time, timestamp, datetime.
*/
SqlTypeNameSpec DateTimeTypeName() :
{
Expand Down Expand Up @@ -6182,6 +6182,12 @@ SqlTypeNameSpec DateTimeTypeName() :
{
return new SqlBasicTypeNameSpec(typeName, precision, s.end(this));
}
|
<DATETIME> { s = span(); }
precision = PrecisionOpt()
{
return new SqlBasicTypeNameSpec(SqlTypeName.TIMESTAMP, precision, s.end(this));
}
}

// Parse an optional data type precision, default is -1.
Expand Down

0 comments on commit 492a2ae

Please sign in to comment.