Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
snuyanzin committed Aug 18, 2024
1 parent be93cf6 commit 7c15788
Show file tree
Hide file tree
Showing 31 changed files with 1,219 additions and 1,447 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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.flink.sql.parser.dql;

import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlCharStringLiteral;
import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.parser.SqlParserPos;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

public abstract class SqlShowCall extends SqlCall {
private final String preposition;
private final SqlIdentifier sqlIdentifier;
// different like type such as like, ilike
private final String likeType;
private final SqlCharStringLiteral likeLiteral;
private final boolean notLike;

public SqlShowCall(
SqlParserPos pos,
String preposition,
SqlIdentifier databaseName,
String likeType,
SqlCharStringLiteral likeLiteral,
boolean notLike) {
super(pos);
this.preposition = preposition;
this.sqlIdentifier = preposition != null ? databaseName : null;
this.likeType = likeType;
this.likeLiteral = likeType == null ? null : likeLiteral;
this.notLike = likeType != null && notLike;
}

@Override
public abstract SqlOperator getOperator();

@Override
public List<SqlNode> getOperandList() {
return Objects.isNull(sqlIdentifier)
? Collections.emptyList()
: Collections.singletonList(sqlIdentifier);
}

@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
String keyword = getOperationName();
if (preposition == null) {
writer.keyword(keyword);
} else if (sqlIdentifier != null) {
writer.keyword(keyword + " " + preposition);
sqlIdentifier.unparse(writer, leftPrec, rightPrec);
}
if (isWithLike()) {
if (isNotLike()) {
writer.keyword(String.format("NOT %s '%s'", likeType, getLikeSqlPattern()));
} else {
writer.keyword(String.format("%s '%s'", likeType, getLikeSqlPattern()));
}
}
}

public String getPreposition() {
return preposition;
}

public List<String> getSqlIdentifierNameList() {
return Objects.isNull(this.sqlIdentifier) ? Collections.emptyList() : sqlIdentifier.names;
}

public boolean isWithLike() {
return likeType != null;
}

public String getLikeType() {
return likeType;
}

public String getLikeSqlPattern() {
return Objects.isNull(likeLiteral) ? null : likeLiteral.getValueAs(String.class);
}

public boolean isNotLike() {
return notLike;
}

abstract String getOperationName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,66 +18,37 @@

package org.apache.flink.sql.parser.dql;

import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlCharStringLiteral;
import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlKind;
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 java.util.Collections;
import java.util.List;
import java.util.Objects;

import static java.util.Objects.requireNonNull;

/** Show [[catalog.] dataBasesName.]sqlIdentifier sql call. Here we add Rich in */
public class SqlShowColumns extends SqlCall {
public class SqlShowColumns extends SqlShowCall {

public static final SqlSpecialOperator OPERATOR =
new SqlSpecialOperator("SHOW COLUMNS", SqlKind.OTHER);

protected final SqlIdentifier tableName;
protected final String preposition;
protected final boolean notLike;
protected final SqlCharStringLiteral likeLiteral;

public SqlShowColumns(
SqlParserPos pos,
String preposition,
SqlIdentifier tableName,
boolean notLike,
SqlCharStringLiteral likeLiteral) {
super(pos);
this.preposition =
requireNonNull(
preposition, "Preposition of 'SHOW COLUMNS' must be 'FROM' or 'IN'.");
this.tableName = requireNonNull(tableName, "tableName should not be null.");
this.notLike = notLike;
this.likeLiteral = likeLiteral;
}

public String getLikeSqlPattern() {
return Objects.isNull(this.likeLiteral) ? null : likeLiteral.getValueAs(String.class);
}

public boolean isNotLike() {
return notLike;
}

public SqlCharStringLiteral getLikeLiteral() {
return likeLiteral;
}

public boolean isWithLike() {
return Objects.nonNull(likeLiteral);
}

public String getPreposition() {
return preposition;
// only LIKE currently supported for SHOW COLUMNS
super(
pos,
preposition,
tableName,
likeLiteral == null ? null : "LIKE",
likeLiteral,
notLike);
requireNonNull(preposition, "Preposition of 'SHOW COLUMNS' must be 'FROM' or 'IN'.");
requireNonNull(tableName, "tableName should not be null.");
}

@Override
Expand All @@ -86,25 +57,7 @@ public SqlOperator getOperator() {
}

@Override
public List<SqlNode> getOperandList() {
return Collections.singletonList(tableName);
}

public String[] fullTableName() {
return tableName.names.toArray(new String[0]);
}

@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
writer.keyword("SHOW COLUMNS " + this.preposition);
tableName.unparse(writer, leftPrec, rightPrec);
if (isWithLike()) {
if (isNotLike()) {
writer.keyword(
String.format("NOT LIKE '%s'", likeLiteral.getValueAs(String.class)));
} else {
writer.keyword(String.format("LIKE '%s'", likeLiteral.getValueAs(String.class)));
}
}
String getOperationName() {
return "SHOW COLUMNS";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,13 @@

import org.apache.flink.sql.parser.impl.ParseException;

import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlCharStringLiteral;
import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlKind;
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 java.util.Collections;
import java.util.List;

import static java.util.Objects.requireNonNull;

/**
* SHOW Databases sql call. The full syntax for show databases is as followings:
*
Expand All @@ -43,23 +35,11 @@
* <sql_like_pattern> ] statement
* }</pre>
*/
public class SqlShowDatabases extends SqlCall {
public class SqlShowDatabases extends SqlShowCall {

public static final SqlSpecialOperator OPERATOR =
new SqlSpecialOperator("SHOW DATABASES", SqlKind.OTHER);

private final String preposition;
private final SqlIdentifier catalogName;
private final String likeType;
private final SqlCharStringLiteral likeLiteral;
private final boolean notLike;

public String[] getCatalog() {
return catalogName == null || catalogName.names.isEmpty()
? new String[] {}
: catalogName.names.toArray(new String[0]);
}

public SqlShowDatabases(
SqlParserPos pos,
String preposition,
Expand All @@ -68,29 +48,17 @@ public SqlShowDatabases(
SqlCharStringLiteral likeLiteral,
boolean notLike)
throws ParseException {
super(pos);
this.preposition = preposition;

this.catalogName =
preposition != null
? requireNonNull(catalogName, "Catalog name must not be null.")
: null;
if (this.catalogName != null && this.catalogName.names.size() > 1) {
super(pos, preposition, catalogName, likeType, likeLiteral, notLike);
if (catalogName != null && catalogName.names.size() > 1) {
throw new ParseException(
String.format(
"Show databases from/in identifier [ %s ] format error, catalog must be a single part identifier.",
String.join(".", this.catalogName.names)));
String.join(".", catalogName.names)));
}
}

if (likeType != null) {
this.likeType = likeType;
this.likeLiteral = requireNonNull(likeLiteral, "Like pattern must not be null");
this.notLike = notLike;
} else {
this.likeType = null;
this.likeLiteral = null;
this.notLike = false;
}
public String getCatalogName() {
return getSqlIdentifierNameList().isEmpty() ? null : getSqlIdentifierNameList().get(0);
}

@Override
Expand All @@ -99,40 +67,7 @@ public SqlOperator getOperator() {
}

@Override
public List<SqlNode> getOperandList() {
return catalogName == null
? Collections.emptyList()
: Collections.singletonList(catalogName);
}

@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
writer.keyword("SHOW DATABASES");
if (preposition != null) {
writer.keyword(preposition);
catalogName.unparse(writer, leftPrec, rightPrec);
}
if (likeType != null) {
writer.keyword(
isNotLike()
? String.format("NOT %s '%s'", likeType, getLikeSqlPattern())
: String.format("%s '%s'", likeType, getLikeSqlPattern()));
}
}

public String getLikeSqlPattern() {
return likeLiteral == null ? null : likeLiteral.getValueAs(String.class);
}

public boolean isNotLike() {
return notLike;
}

public String getPreposition() {
return preposition;
}

public String getLikeType() {
return likeType;
String getOperationName() {
return "SHOW DATABASES";
}
}
Loading

0 comments on commit 7c15788

Please sign in to comment.