Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ SqlCreate SqlCreateCatalog(Span s, boolean replace) :
SqlParserPos startPos;
SqlIdentifier catalogName;
SqlNodeList propertyList = SqlNodeList.EMPTY;
SqlNode comment = null;
SqlCharStringLiteral comment = null;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

so far there were different approaches for comments
the PR aligns them

boolean ifNotExists = false;
}
{
Expand All @@ -154,8 +154,8 @@ SqlCreate SqlCreateCatalog(Span s, boolean replace) :

catalogName = SimpleIdentifier()
[
<COMMENT>
comment = StringLiteral()
<COMMENT> <QUOTED_STRING>
{ comment = Comment(); }
]
[
<WITH>
Expand Down Expand Up @@ -195,7 +195,7 @@ SqlAlterCatalog SqlAlterCatalog() :
SqlParserPos startPos;
SqlIdentifier catalogName;
SqlNodeList propertyList = SqlNodeList.EMPTY;
SqlNode comment = null;
SqlCharStringLiteral comment = null;
}
{
<ALTER> <CATALOG> { startPos = getPos(); }
Expand All @@ -217,9 +217,9 @@ SqlAlterCatalog SqlAlterCatalog() :
propertyList);
}
|
<COMMENT>
comment = StringLiteral()
<COMMENT> <QUOTED_STRING>
{
comment = Comment();
return new SqlAlterCatalogComment(startPos.plus(getPos()),
catalogName,
comment);
Expand Down Expand Up @@ -314,8 +314,7 @@ SqlCreate SqlCreateDatabase(Span s, boolean replace) :
databaseName = CompoundIdentifier()
[ <COMMENT> <QUOTED_STRING>
{
String p = SqlParserUtil.parseString(token.image);
comment = SqlLiteral.createCharString(p, getPos());
comment = Comment();
}
]
[
Expand Down Expand Up @@ -1124,15 +1123,17 @@ SqlTableColumn ComputedColumn(TableCreationContext context) :
SqlIdentifier name;
SqlParserPos pos;
SqlNode expr;
SqlNode comment = null;
SqlCharStringLiteral comment = null;
}
{
name = SimpleIdentifier() {pos = getPos();}
<AS>
expr = Expression(ExprContext.ACCEPT_NON_QUERY)
[
<COMMENT>
comment = StringLiteral()
<COMMENT> <QUOTED_STRING>
{
comment = Comment();
}
]
{
SqlTableColumn computedColumn = new SqlTableColumn.SqlComputedColumn(
Expand All @@ -1150,7 +1151,7 @@ SqlTableColumn MetadataColumn(TableCreationContext context, SqlIdentifier name,
{
SqlNode metadataAlias = null;
boolean isVirtual = false;
SqlNode comment = null;
SqlCharStringLiteral comment = null;
}
{
<METADATA>
Expand All @@ -1164,8 +1165,10 @@ SqlTableColumn MetadataColumn(TableCreationContext context, SqlIdentifier name,
}
]
[
<COMMENT>
comment = StringLiteral()
<COMMENT> <QUOTED_STRING>
{
comment = Comment();
}
]
{
SqlTableColumn metadataColumn = new SqlTableColumn.SqlMetadataColumn(
Expand All @@ -1184,15 +1187,17 @@ SqlTableColumn MetadataColumn(TableCreationContext context, SqlIdentifier name,
SqlTableColumn RegularColumn(TableCreationContext context, SqlIdentifier name, SqlDataTypeSpec type) :
{
SqlTableConstraint constraint = null;
SqlNode comment = null;
SqlCharStringLiteral comment = null;
}
{
[
constraint = ColumnConstraint(name)
]
[
<COMMENT>
comment = StringLiteral()
<COMMENT> <QUOTED_STRING>
{
comment = Comment();
}
]
{
SqlTableColumn regularColumn = new SqlTableColumn.SqlRegularColumn(
Expand Down Expand Up @@ -1573,8 +1578,7 @@ SqlCreate SqlCreateTable(Span s, boolean replace, boolean isTemporary) :
<RPAREN>
]
[ <COMMENT> <QUOTED_STRING> {
String p = SqlParserUtil.parseString(token.image);
comment = SqlLiteral.createCharString(p, getPos());
comment = Comment();
}]
[
<DISTRIBUTED>
Expand Down Expand Up @@ -1907,8 +1911,7 @@ SqlCreate SqlCreateOrAlterMaterializedTable(Span s, boolean replace, boolean isT
[
<COMMENT> <QUOTED_STRING>
{
String p = SqlParserUtil.parseString(token.image);
comment = SqlLiteral.createCharString(p, getPos());
comment = Comment();
}
]
[
Expand Down Expand Up @@ -2236,8 +2239,7 @@ SqlCreate SqlCreateView(Span s, boolean replace, boolean isTemporary) : {
fieldList = ParenthesizedSimpleIdentifierList()
]
[ <COMMENT> <QUOTED_STRING> {
String p = SqlParserUtil.parseString(token.image);
comment = SqlLiteral.createCharString(p, getPos());
comment = Comment();
}
]
<AS>
Expand Down Expand Up @@ -3483,8 +3485,7 @@ SqlCreate SqlCreateModel(Span s, boolean isTemporary) :
]
[ <COMMENT> <QUOTED_STRING>
{
String p = SqlParserUtil.parseString(token.image);
comment = SqlLiteral.createCharString(p, getPos());
comment = Comment();
}
]
[
Expand Down Expand Up @@ -3517,3 +3518,13 @@ SqlCreate SqlCreateModel(Span s, boolean isTemporary) :
ifNotExists);
}
}

SqlCharStringLiteral Comment() :
{
}
{
{
String p = SqlParserUtil.parseString(token.image);
return SqlLiteral.createCharString(p, getPos());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,25 @@

package org.apache.flink.sql.parser;

import org.apache.flink.sql.parser.ddl.SqlTableOption;

import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlLiteral;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlNodeList;
import org.apache.calcite.util.NlsString;

import javax.annotation.Nullable;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/** Utils methods for partition DDLs. */
public class SqlPartitionUtils {
/** Utils methods for parsing DDLs. */
public class SqlParseUtils {

private SqlPartitionUtils() {}
private SqlParseUtils() {}

/**
* Get static partition key value pair as strings.
Expand All @@ -44,18 +52,49 @@ public static LinkedHashMap<String, String> getPartitionKVs(SqlNodeList partitio
return null;
}
LinkedHashMap<String, String> ret = new LinkedHashMap<>();
if (partitionSpec.size() == 0) {
if (partitionSpec.isEmpty()) {
return ret;
}
for (SqlNode node : partitionSpec.getList()) {
SqlProperty sqlProperty = (SqlProperty) node;
Comparable<?> comparable = SqlLiteral.value(sqlProperty.getValue());
String value =
comparable instanceof NlsString
? ((NlsString) comparable).getValue()
: comparable.toString();
String value = extractString(sqlProperty.getValue());
ret.put(sqlProperty.getKey().getSimple(), value);
}
return ret;
}

@Nullable
public static String extractString(@Nullable SqlLiteral literal) {
return literal == null ? null : literal.getValueAs(NlsString.class).getValue();
}

@Nullable
public static String extractString(@Nullable SqlNode node) {
if (node == null) {
return null;
}
final Comparable value = SqlLiteral.value(node);
if (value == null) {
return null;
}
return value instanceof NlsString ? ((NlsString) value).getValue() : value.toString();
}

public static Map<String, String> extractMap(@Nullable SqlNodeList propList) {
if (propList == null) {
return Map.of();
}
return propList.getList().stream()
.map(p -> (SqlTableOption) p)
.collect(Collectors.toMap(k -> k.getKeyString(), SqlTableOption::getValueString));
}

public static List<String> extractList(@Nullable SqlNodeList sqlNodeList) {
if (sqlNodeList == null) {
return List.of();
}
return sqlNodeList.getList().stream()
.map(p -> ((SqlIdentifier) p).getSimple())
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@
import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlLiteral;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.SqlSpecialOperator;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.util.ImmutableNullableList;
import org.apache.calcite.util.NlsString;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -69,7 +67,7 @@ public String getKeyString() {

@Nullable
public String getValueString() {
return value != null ? ((NlsString) SqlLiteral.value(value)).getValue() : null;
return SqlParseUtils.extractString(value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@
import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlLiteral;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.SqlSpecialOperator;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.util.ImmutableNullableList;
import org.apache.calcite.util.NlsString;

import java.util.List;

Expand Down Expand Up @@ -64,7 +62,7 @@ public String getKeyString() {
}

public String getValueString() {
return ((NlsString) SqlLiteral.value(value)).getValue();
return SqlParseUtils.extractString(value);
}

@Override
Expand Down
Loading