|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to you under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.flink.sql.parser; |
| 19 | + |
| 20 | +import org.apache.flink.sql.parser.impl.FlinkSqlParserImpl; |
| 21 | +import org.apache.flink.sql.parser.type.ExtendedSqlRowTypeNameSpec; |
| 22 | + |
| 23 | +import org.apache.calcite.avatica.util.Casing; |
| 24 | +import org.apache.calcite.avatica.util.Quoting; |
| 25 | +import org.apache.calcite.sql.SqlBasicTypeNameSpec; |
| 26 | +import org.apache.calcite.sql.SqlDataTypeSpec; |
| 27 | +import org.apache.calcite.sql.SqlDialect; |
| 28 | +import org.apache.calcite.sql.SqlIdentifier; |
| 29 | +import org.apache.calcite.sql.SqlWriter; |
| 30 | +import org.apache.calcite.sql.dialect.CalciteSqlDialect; |
| 31 | +import org.apache.calcite.sql.fun.SqlStdOperatorTable; |
| 32 | +import org.apache.calcite.sql.parser.SqlParser; |
| 33 | +import org.apache.calcite.sql.parser.SqlParserImplFactory; |
| 34 | +import org.apache.calcite.sql.parser.SqlParserPos; |
| 35 | +import org.apache.calcite.sql.pretty.SqlPrettyWriter; |
| 36 | +import org.apache.calcite.sql.type.SqlTypeName; |
| 37 | +import org.apache.calcite.sql.validate.SqlConformance; |
| 38 | +import org.apache.calcite.sql.validate.SqlConformanceEnum; |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | + |
| 41 | +import java.util.List; |
| 42 | +import java.util.Map; |
| 43 | + |
| 44 | +/** Tests for {@link ExtendedSqlRowTypeNameSpec}. */ |
| 45 | +class ExtendedSqlRowTypeNameSpecTest { |
| 46 | + @Test |
| 47 | + void testExtendedRowWithNoComments() { |
| 48 | + final ExtendedSqlRowTypeNameSpec spec = |
| 49 | + new ExtendedSqlRowTypeNameSpec( |
| 50 | + SqlParserPos.ZERO, |
| 51 | + List.of( |
| 52 | + new SqlIdentifier("t1", SqlParserPos.ZERO), |
| 53 | + new SqlIdentifier("t2", SqlParserPos.ZERO), |
| 54 | + new SqlIdentifier("t3", SqlParserPos.ZERO)), |
| 55 | + List.of( |
| 56 | + new SqlDataTypeSpec( |
| 57 | + new SqlBasicTypeNameSpec( |
| 58 | + SqlTypeName.INTEGER, SqlParserPos.ZERO), |
| 59 | + SqlParserPos.ZERO), |
| 60 | + new SqlDataTypeSpec( |
| 61 | + new SqlBasicTypeNameSpec( |
| 62 | + SqlTypeName.DATE, SqlParserPos.ZERO), |
| 63 | + SqlParserPos.ZERO), |
| 64 | + new SqlDataTypeSpec( |
| 65 | + new SqlBasicTypeNameSpec( |
| 66 | + SqlTypeName.TIME, SqlParserPos.ZERO), |
| 67 | + SqlParserPos.ZERO)), |
| 68 | + List.of(), |
| 69 | + false); |
| 70 | + SqlWriter writer = getSqlWriter(); |
| 71 | + spec.unparse(writer, 0, 0); |
| 72 | + } |
| 73 | + |
| 74 | + private SqlWriter getSqlWriter() { |
| 75 | + final Map<String, ?> options = |
| 76 | + Map.ofEntries( |
| 77 | + Map.entry("quoting", Quoting.BACK_TICK), |
| 78 | + Map.entry("quotedCasing", Casing.UNCHANGED), |
| 79 | + Map.entry("unquotedCasing", Casing.UNCHANGED), |
| 80 | + Map.entry("caseSensitive", true), |
| 81 | + Map.entry("enableTypeCoercion", false), |
| 82 | + Map.entry("conformance", SqlConformanceEnum.DEFAULT), |
| 83 | + Map.entry("operatorTable", SqlStdOperatorTable.instance()), |
| 84 | + Map.entry("parserFactory", FlinkSqlParserImpl.FACTORY)); |
| 85 | + final SqlParser.Config parserConfig = |
| 86 | + SqlParser.config() |
| 87 | + .withQuoting((Quoting) options.get("quoting")) |
| 88 | + .withUnquotedCasing((Casing) options.get("unquotedCasing")) |
| 89 | + .withQuotedCasing((Casing) options.get("quotedCasing")) |
| 90 | + .withConformance((SqlConformance) options.get("conformance")) |
| 91 | + .withCaseSensitive((boolean) options.get("caseSensitive")) |
| 92 | + .withParserFactory((SqlParserImplFactory) options.get("parserFactory")); |
| 93 | + |
| 94 | + return new SqlPrettyWriter( |
| 95 | + new CalciteSqlDialect( |
| 96 | + SqlDialect.EMPTY_CONTEXT |
| 97 | + .withQuotedCasing(parserConfig.unquotedCasing()) |
| 98 | + .withConformance(parserConfig.conformance()) |
| 99 | + .withUnquotedCasing(parserConfig.unquotedCasing()) |
| 100 | + .withIdentifierQuoteString(parserConfig.quoting().string)), |
| 101 | + false); |
| 102 | + } |
| 103 | +} |
0 commit comments