-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CALCITE-5424] Customize handling of literals based on type system
Literals introduced by the keyword DATE, TIME, DATETIME, TIMESTAMP, TIMESTAMP WITH LOCAL TIME ZONE are represented by the parser by new class SqlUnknownLiteral. Determining the actual type is deferred until validation time; the validator determines the actual type based on the type system's type alias map, and then validates the character string. Close #3036 Co-authored-by: Julian Hyde <[email protected]> Co-authored-by: Oliver Lee <[email protected]>
- Loading branch information
1 parent
d9eeba4
commit f8f8a51
Showing
19 changed files
with
268 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
core/src/main/java/org/apache/calcite/sql/SqlUnknownLiteral.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* 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. | ||
*/ | ||
package org.apache.calcite.sql; | ||
|
||
import org.apache.calcite.sql.parser.SqlParserPos; | ||
import org.apache.calcite.sql.parser.SqlParserUtil; | ||
import org.apache.calcite.sql.type.SqlTypeName; | ||
import org.apache.calcite.util.NlsString; | ||
import org.apache.calcite.util.Util; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* Literal whose type is not yet known. | ||
*/ | ||
public class SqlUnknownLiteral extends SqlLiteral { | ||
public final String tag; | ||
|
||
SqlUnknownLiteral(String tag, String value, SqlParserPos pos) { | ||
super(requireNonNull(value, "value"), SqlTypeName.UNKNOWN, pos); | ||
this.tag = requireNonNull(tag, "tag"); | ||
} | ||
|
||
@Override public String getValue() { | ||
return (String) requireNonNull(super.getValue(), "value"); | ||
} | ||
|
||
@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) { | ||
final NlsString nlsString = new NlsString(getValue(), null, null); | ||
writer.keyword(tag); | ||
writer.literal(nlsString.asSql(true, true, writer.getDialect())); | ||
} | ||
|
||
|
||
/** Converts this unknown literal to a literal of known type. */ | ||
public SqlLiteral resolve(SqlTypeName typeName) { | ||
switch (typeName) { | ||
case DATE: | ||
return SqlParserUtil.parseDateLiteral(getValue(), pos); | ||
case TIME: | ||
return SqlParserUtil.parseTimeLiteral(getValue(), pos); | ||
case TIMESTAMP: | ||
return SqlParserUtil.parseTimestampLiteral(getValue(), pos); | ||
case TIMESTAMP_WITH_LOCAL_TIME_ZONE: | ||
return SqlParserUtil.parseTimestampWithLocalTimeZoneLiteral(getValue(), pos); | ||
default: | ||
throw Util.unexpected(typeName); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.