Skip to content

Commit

Permalink
[dingo-calcite] Add show-related syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
jwcxs-m authored and JYcz committed May 19, 2023
1 parent 2b3bc21 commit 2bd7b4c
Show file tree
Hide file tree
Showing 18 changed files with 436 additions and 45 deletions.
2 changes: 2 additions & 0 deletions dingo-calcite/src/main/codegen/config.fmpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ data: {
"io.dingodb.calcite.grammar.dql.SqlShow"
"io.dingodb.calcite.grammar.dql.SqlShowColumns"
"io.dingodb.calcite.grammar.dql.SqlShowCreateTable"
"io.dingodb.calcite.grammar.dql.SqlShowCreateUser"
"io.dingodb.calcite.grammar.dql.SqlShowDatabases"
"io.dingodb.calcite.grammar.dql.SqlShowGrants"
"io.dingodb.calcite.grammar.dql.SqlShowTables"
"io.dingodb.calcite.grammar.dql.SqlShowTableDistribution"
"io.dingodb.calcite.grammar.dql.SqlShowFullTables"
"io.dingodb.calcite.grammar.dql.SqlShowVariables"
"io.dingodb.calcite.grammar.dql.SqlShowWarnings"
Expand Down
33 changes: 27 additions & 6 deletions dingo-calcite/src/main/codegen/includes/parserImpls.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -883,27 +883,48 @@ SqlShow SqlShow(): {
|
show = SqlShowGlobalVariables(s)
|
show = SqlShowCreateTable(s)
show = SqlShowCreate(s)
|
show = SqlShowColumns(s)
|
show = SqlShowTableDistribution(s)
)
{
return show;
}
}
SqlShow SqlShowTableDistribution(Span s): {
String tableName = null;
} {
<TABLE> (<QUOTED_STRING> | <IDENTIFIER>) { tableName = token.image.toUpperCase(); } <DISTRIBUTION>
{ return new SqlShowTableDistribution(s.end(this), tableName); }
}

SqlShow SqlShowColumns(Span s): {
String tableName = null;
String pattern = null;
} {
<COLUMNS> (<QUOTED_STRING> | <IDENTIFIER>) { tableName = token.image.toUpperCase(); }
{ return new SqlShowColumns(s.end(this), tableName); }
<COLUMNS> <FROM> (<QUOTED_STRING> | <IDENTIFIER>) { tableName = token.image.toUpperCase(); }
[ <LIKE> <QUOTED_STRING> { pattern = token.image.toUpperCase().replace("'", ""); } ]
{ return new SqlShowColumns(s.end(this), tableName, pattern); }
}

SqlShow SqlShowCreateTable(Span s): {
SqlShow SqlShowCreate(Span s): {
String tableName = null;
String userName = null;
String host = "%";
} {
<CREATE> <TABLE> (<QUOTED_STRING> | <IDENTIFIER>) { tableName = token.image.toUpperCase(); }
{ return new SqlShowCreateTable(s.end(this), tableName); }
<CREATE>
(
<TABLE>
( <QUOTED_STRING> | <IDENTIFIER> ) { tableName = token.image.toUpperCase(); }
{ return new SqlShowCreateTable(s.end(this), tableName); }
|
<USER>
<QUOTED_STRING> { userName = token.image.replace("'", ""); }
[<AT_SPLIT> <QUOTED_STRING> { host = token.image.replace("'", "");} ]
{ return new SqlShowCreateUser(s.end(this), userName, host); }
)
}

SqlShow SqlShowDatabases(Span s): {
Expand Down
1 change: 1 addition & 0 deletions dingo-calcite/src/main/codegen/templates/Parser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -7802,6 +7802,7 @@ SqlPostfixOperator PostfixRowOperator() :
| < DISCONNECT: "DISCONNECT" >
| < DISPATCH: "DISPATCH" >
| < DISTINCT: "DISTINCT" >
| < DISTRIBUTION: "DISTRIBUTION" >
| < DOMAIN: "DOMAIN" >
| < DOT_FORMAT: "DOT" >
| < DOUBLE: "DOUBLE" >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class SqlShowColumns extends SqlShow {

public String tableName;

public String sqlLikePattern;

private static final SqlOperator OPERATOR =
new SqlSpecialOperator("SHOW COLUMNS FROM TABLE", SqlKind.SELECT);

Expand All @@ -34,9 +36,10 @@ public class SqlShowColumns extends SqlShow {
*
* @param pos pos
*/
public SqlShowColumns(SqlParserPos pos, String tableName) {
public SqlShowColumns(SqlParserPos pos, String tableName, String sqlLikePattern) {
super(OPERATOR, pos);
this.tableName = tableName;
this.sqlLikePattern = sqlLikePattern;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2021 DataCanvas
*
* Licensed 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 io.dingodb.calcite.grammar.dql;

import org.apache.calcite.sql.SqlKind;
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;

public class SqlShowCreateUser extends SqlShow {

public String userName;

public String host;

private static final SqlOperator OPERATOR = new SqlSpecialOperator("SHOW CREATE USER", SqlKind.SELECT);

/**
* Creates a sqlDql.
*
* @param pos pos
*/
public SqlShowCreateUser(SqlParserPos pos, String userName, String host) {
super(OPERATOR, pos);
this.userName = userName;
this.host = host;
}

@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
writer.keyword("SHOW CREATE USER ");
writer.keyword(userName);
writer.keyword("@");
writer.keyword(host);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2021 DataCanvas
*
* Licensed 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 io.dingodb.calcite.grammar.dql;

import org.apache.calcite.sql.SqlKind;
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;

public class SqlShowTableDistribution extends SqlShow {

public String tableName;

private static final SqlOperator OPERATOR = new SqlSpecialOperator("SHOW TABLE DISTRIBUTION", SqlKind.SELECT);

/**
* Creates a sqlDql.
*
* @param pos pos
*/
public SqlShowTableDistribution(SqlParserPos pos, String tableName) {
super(OPERATOR, pos);
this.tableName = tableName;
}

@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
writer.keyword("SHOW TABLE");
writer.keyword(tableName);
writer.keyword("DISTRIBUTION");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package io.dingodb.calcite.operation;

import io.dingodb.calcite.grammar.dql.SqlShowColumns;
import io.dingodb.calcite.utils.MetaServiceUtils;
import io.dingodb.common.table.ColumnDefinition;
import io.dingodb.common.table.TableDefinition;
import io.dingodb.common.util.SqlLikeUtils;
import io.dingodb.meta.MetaService;
import lombok.Setter;
import org.apache.calcite.sql.SqlNode;
Expand All @@ -30,54 +32,68 @@

public class ShowColumnsOperation implements QueryOperation {

private static final String SCHEMA_NAME = "DINGO";

@Setter
public SqlNode sqlNode;

private MetaService metaService;

private String tableName;

private String sqlLikePattern;

public ShowColumnsOperation(SqlNode sqlNode) {
SqlShowColumns showCreateTable = (SqlShowColumns) sqlNode;
metaService = MetaService.root().getSubMetaService(getSchemaName(showCreateTable.tableName));
tableName = showCreateTable.tableName;
SqlShowColumns showColumns = (SqlShowColumns) sqlNode;
this.metaService = MetaService.root().getSubMetaService(MetaServiceUtils.getSchemaName(showColumns.tableName));
this.tableName = showColumns.tableName;
this.sqlLikePattern = showColumns.sqlLikePattern;
}

@Override
public Iterator getIterator() {
List<Object[]> createTable = new ArrayList<>();
String createTableStatement = getCreateTableStatement();
if (StringUtils.isNotBlank(createTableStatement)) {
Object[] tuples = new Object[]{createTableStatement};
createTable.add(tuples);
List<Object[]> tuples = new ArrayList<>();
List<List<String>> columnList = getColumnFields();
for (List<String> values : columnList) {
Object[] tuple = values.toArray();
tuples.add(tuple);
}
return createTable.iterator();
return tuples.iterator();
}

@Override
public List<String> columns() {
List<String> columns = new ArrayList<>();
columns.add("create table statement");
columns.add("Field");
columns.add("Type");
columns.add("Null");
columns.add("Key");
columns.add("Default");
return columns;
}

private String getSchemaName(String tableName) {
if (tableName.contains("\\.")) {
return tableName.split("\\.")[0];
}
return SCHEMA_NAME;
}

private String getCreateTableStatement() {
private List<List<String>> getColumnFields() {
TableDefinition tableDefinition = metaService.getTableDefinition(tableName);
if (tableDefinition == null) {
return "";
throw new RuntimeException("Table " + tableName + " doesn't exist");
}

List<ColumnDefinition> columns = tableDefinition.getColumns();
List<List<String>> columnList = new ArrayList<>();
boolean haveLike = !StringUtils.isBlank(sqlLikePattern);
for (ColumnDefinition column : columns) {
List<String> columnValues = new ArrayList<>();

String columnName = column.getName();
if (haveLike && !SqlLikeUtils.like(columnName, sqlLikePattern)) {
continue;
}
columnValues.add(columnName);
columnValues.add(column.getType().toString());
columnValues.add(column.isNullable() ? "YES" : "NO");
columnValues.add(column.isPrimary() ? "PRI" : "");
columnValues.add(column.getDefaultValue() != null ? column.getDefaultValue() : "NULL");

return "";
columnList.add(columnValues);
}
return columnList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.dingodb.calcite.operation;

import io.dingodb.calcite.grammar.dql.SqlShowCreateTable;
import io.dingodb.calcite.utils.MetaServiceUtils;
import io.dingodb.common.table.TableDefinition;
import io.dingodb.meta.MetaService;
import lombok.Setter;
Expand All @@ -29,8 +30,6 @@

public class ShowCreateTableOperation implements QueryOperation {

private static final String SCHEMA_NAME = "DINGO";

@Setter
public SqlNode sqlNode;

Expand All @@ -40,7 +39,7 @@ public class ShowCreateTableOperation implements QueryOperation {

public ShowCreateTableOperation(SqlNode sqlNode) {
SqlShowCreateTable showCreateTable = (SqlShowCreateTable) sqlNode;
metaService = MetaService.root().getSubMetaService(getSchemaName(showCreateTable.tableName));
metaService = MetaService.root().getSubMetaService(MetaServiceUtils.getSchemaName(showCreateTable.tableName));
tableName = showCreateTable.tableName;
}

Expand All @@ -64,13 +63,6 @@ public List<String> columns() {
return columns;
}

private String getSchemaName(String tableName) {
if (tableName.contains("\\.")) {
return tableName.split("\\.")[0];
}
return SCHEMA_NAME;
}

private String getCreateTable() {
TableDefinition tableDefinition = metaService.getTableDefinition(tableName);
if (tableDefinition == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,54 @@

package io.dingodb.calcite.operation;

import io.dingodb.common.privilege.UserDefinition;
import io.dingodb.verify.service.UserService;
import io.dingodb.verify.service.UserServiceProvider;
import lombok.Setter;
import org.apache.calcite.sql.SqlNode;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ShowCreateUserOperation implements QueryOperation {

static UserService userService = UserServiceProvider.getRoot();

@Setter
public SqlNode sqlNode;

private String userName;

private String host;

public ShowCreateUserOperation(SqlNode sqlNode, String userName, String host) {
this.sqlNode = sqlNode;
this.userName = userName;
this.host = host;
}

@Override
public Iterator getIterator() {
return null;
List<Object[]> createUserList = new ArrayList<>();
String createUser = getCreateUser();
Object[] tuples = new Object[]{createUser};
createUserList.add(tuples);
return createUserList.iterator();
}

@Override
public List<String> columns() {
return null;
List<String> columns = new ArrayList<>();
columns.add("Create User");
return columns;
}

private String getCreateUser() {
UserDefinition userDefinition = userService.getUserDefinition(this.userName, this.host);
if (userDefinition == null) {
throw new RuntimeException("Unknown user '" + this.userName + "'@'" + this.host + "'");
}
return "Create user '" + this.userName + "' identified by ******";
}
}
Loading

0 comments on commit 2bd7b4c

Please sign in to comment.