Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CALCITE-1221] Implement DESCRIBE DATABASE, CATALOG, STATEMENT #4219

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
30 changes: 15 additions & 15 deletions core/src/main/codegen/templates/Parser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ import org.apache.calcite.sql.SqlCollation;
import org.apache.calcite.sql.SqlCollectionTypeNameSpec;
import org.apache.calcite.sql.SqlDataTypeSpec;
import org.apache.calcite.sql.SqlDelete;
import org.apache.calcite.sql.SqlDescribeCatalog;
import org.apache.calcite.sql.SqlDescribeDatabase;
import org.apache.calcite.sql.SqlDescribeSchema;
import org.apache.calcite.sql.SqlDescribeStatement;
import org.apache.calcite.sql.SqlDescribeTable;
import org.apache.calcite.sql.SqlDynamicParam;
import org.apache.calcite.sql.SqlExplain;
Expand Down Expand Up @@ -1518,11 +1521,18 @@ SqlNode SqlDescribe() :
{
<DESCRIBE> { s = span(); }
(
LOOKAHEAD(2) (<DATABASE> | <CATALOG> | <SCHEMA>)
<DATABASE>
id = CompoundIdentifier() {
return new SqlDescribeDatabase(s.end(id), id);
}
|
<CATALOG>
id = CompoundIdentifier() {
return new SqlDescribeCatalog(s.end(id), id);
}
|
<SCHEMA>
id = CompoundIdentifier() {
// DESCRIBE DATABASE and DESCRIBE CATALOG currently do the same as
// DESCRIBE SCHEMA but should be different. See
// [CALCITE-1221] Implement DESCRIBE DATABASE, CATALOG, STATEMENT
return new SqlDescribeSchema(s.end(id), id);
}
|
Expand All @@ -1545,17 +1555,7 @@ SqlNode SqlDescribe() :
|
(LOOKAHEAD(1) <STATEMENT>)?
stmt = SqlQueryOrDml() {
// DESCRIBE STATEMENT currently does the same as EXPLAIN. See
// [CALCITE-1221] Implement DESCRIBE DATABASE, CATALOG, STATEMENT
final SqlExplainLevel detailLevel = SqlExplainLevel.EXPPLAN_ATTRIBUTES;
final SqlExplain.Depth depth = SqlExplain.Depth.PHYSICAL;
final SqlExplainFormat format = SqlExplainFormat.TEXT;
return new SqlExplain(s.end(stmt),
stmt,
detailLevel.symbol(SqlParserPos.ZERO),
depth.symbol(SqlParserPos.ZERO),
format.symbol(SqlParserPos.ZERO),
nDynamicParams);
return new SqlDescribeStatement(s.end(stmt), stmt);
}
)
}
Expand Down
77 changes: 77 additions & 0 deletions core/src/main/java/org/apache/calcite/sql/SqlDescribeCatalog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.util.ImmutableNullableList;

import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.List;

/**
* A <code>SqlDescribeCatalog</code> is a node of a parse tree that represents a
* {@code DESCRIBE CATALOG} statement.
*/
public class SqlDescribeCatalog extends SqlCall {

public static final SqlSpecialOperator OPERATOR =
new SqlSpecialOperator("DESCRIBE_CATALOG", SqlKind.DESCRIBE_CATALOG) {
@SuppressWarnings("argument.type.incompatible")
@Override public SqlCall createCall(@Nullable SqlLiteral functionQualifier,
SqlParserPos pos, @Nullable SqlNode... operands) {
return new SqlDescribeSchema(pos, (SqlIdentifier) operands[0]);
}
};

SqlIdentifier catalog;

/** Creates a SqlDescribeCatalog. */
public SqlDescribeCatalog(SqlParserPos pos, SqlIdentifier catalog) {
super(pos);
this.catalog = catalog;
}

@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
writer.keyword("DESCRIBE");
writer.keyword("CATALOG");
catalog.unparse(writer, leftPrec, rightPrec);
}

@SuppressWarnings("assignment.type.incompatible")
@Override public void setOperand(int i, @Nullable SqlNode operand) {
switch (i) {
case 0:
catalog = (SqlIdentifier) operand;
break;
default:
throw new AssertionError(i);
}
}

@Override public SqlOperator getOperator() {
return OPERATOR;
}

@Override public List<SqlNode> getOperandList() {
return ImmutableNullableList.of(catalog);
}

public SqlIdentifier getCatalog() {
return catalog;
}
}
77 changes: 77 additions & 0 deletions core/src/main/java/org/apache/calcite/sql/SqlDescribeDatabase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.util.ImmutableNullableList;

import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.List;

/**
* A <code>SqlDescribeDatabase</code> is a node of a parse tree that represents a
* {@code DESCRIBE DATABASE} statement.
*/
public class SqlDescribeDatabase extends SqlCall {

public static final SqlSpecialOperator OPERATOR =
new SqlSpecialOperator("DESCRIBE_DATABASE", SqlKind.DESCRIBE_DATABASE) {
@SuppressWarnings("argument.type.incompatible")
@Override public SqlCall createCall(@Nullable SqlLiteral functionQualifier,
SqlParserPos pos, @Nullable SqlNode... operands) {
return new SqlDescribeSchema(pos, (SqlIdentifier) operands[0]);
}
};

SqlIdentifier database;

/** Creates a SqlDescribeDatabase. */
public SqlDescribeDatabase(SqlParserPos pos, SqlIdentifier database) {
super(pos);
this.database = database;
}

@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
writer.keyword("DESCRIBE");
writer.keyword("DATABASE");
database.unparse(writer, leftPrec, rightPrec);
}

@SuppressWarnings("assignment.type.incompatible")
@Override public void setOperand(int i, @Nullable SqlNode operand) {
switch (i) {
case 0:
database = (SqlIdentifier) operand;
break;
default:
throw new AssertionError(i);
}
}

@Override public SqlOperator getOperator() {
return OPERATOR;
}

@Override public List<SqlNode> getOperandList() {
return ImmutableNullableList.of(database);
}

public SqlIdentifier getDatabase() {
return database;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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.util.ImmutableNullableList;

import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.List;

/**
* A <code>SqlDescribeStatement</code> is a node of a parse tree that represents a
* {@code DESCRIBE STATEMENT} statement.
*/
public class SqlDescribeStatement extends SqlCall {

public static final SqlSpecialOperator OPERATOR =
new SqlSpecialOperator("DESCRIBE_STATEMENT", SqlKind.DESCRIBE_STATEMENT) {
@SuppressWarnings("argument.type.incompatible")
@Override public SqlCall createCall(@Nullable SqlLiteral functionQualifier,
SqlParserPos pos, @Nullable SqlNode... operands) {
return new SqlDescribeSchema(pos, (SqlIdentifier) operands[0]);
}
};

SqlNode statement;

/** Creates a SqlDescribeStatement. */
public SqlDescribeStatement(SqlParserPos pos, SqlNode statement) {
super(pos);
this.statement = statement;
}

@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
writer.keyword("DESCRIBE");
writer.keyword("STATEMENT");
writer.newlineAndIndent();
statement.unparse(
writer, getOperator().getLeftPrec(), getOperator().getRightPrec());
}

@SuppressWarnings("assignment.type.incompatible")
@Override public void setOperand(int i, @Nullable SqlNode operand) {
switch (i) {
case 0:
statement = operand;
break;
default:
throw new AssertionError(i);
}
}

@Override public SqlOperator getOperator() {
return OPERATOR;
}

@Override public List<SqlNode> getOperandList() {
return ImmutableNullableList.of(statement);
}

public SqlNode getStatement() {
return statement;
}
}
9 changes: 9 additions & 0 deletions core/src/main/java/org/apache/calcite/sql/SqlKind.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,21 @@ public enum SqlKind {
/** EXPLAIN statement. */
EXPLAIN,

/** DESCRIBE DATABASE statement. */
DESCRIBE_DATABASE,

/** DESCRIBE CATALOG statement. */
DESCRIBE_CATALOG,

/** DESCRIBE SCHEMA statement. */
DESCRIBE_SCHEMA,

/** DESCRIBE TABLE statement. */
DESCRIBE_TABLE,

/** DESCRIBE STATEMENT statement. */
DESCRIBE_STATEMENT,

/** INSERT statement. */
INSERT,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4857,12 +4857,10 @@ void checkPeriodPredicate(Checker checker) {
@Test void testDescribeSchema() {
sql("describe schema A")
.ok("DESCRIBE SCHEMA `A`");
// Currently DESCRIBE DATABASE, DESCRIBE CATALOG become DESCRIBE SCHEMA.
// See [CALCITE-1221] Implement DESCRIBE DATABASE, CATALOG, STATEMENT
sql("describe database A")
.ok("DESCRIBE SCHEMA `A`");
.ok("DESCRIBE DATABASE `A`");
sql("describe catalog A")
.ok("DESCRIBE SCHEMA `A`");
.ok("DESCRIBE CATALOG `A`");
}

@Test void testDescribeTable() {
Expand Down Expand Up @@ -4897,15 +4895,13 @@ void checkPeriodPredicate(Checker checker) {
}

@Test void testDescribeStatement() {
// Currently DESCRIBE STATEMENT becomes EXPLAIN.
// See [CALCITE-1221] Implement DESCRIBE DATABASE, CATALOG, STATEMENT
final String expected0 = ""
+ "EXPLAIN PLAN INCLUDING ATTRIBUTES WITH IMPLEMENTATION FOR\n"
+ "DESCRIBE STATEMENT\n"
+ "SELECT *\n"
+ "FROM `EMPS`";
sql("describe statement select * from emps").ok(expected0);
final String expected1 = ""
+ "EXPLAIN PLAN INCLUDING ATTRIBUTES WITH IMPLEMENTATION FOR\n"
+ "DESCRIBE STATEMENT\n"
+ "(SELECT *\n"
+ "FROM `EMPS`\n"
+ "ORDER BY 2)";
Expand All @@ -4914,15 +4910,15 @@ void checkPeriodPredicate(Checker checker) {
sql("describe (select * from emps)").ok(expected0);
sql("describe statement (select * from emps)").ok(expected0);
final String expected2 = ""
+ "EXPLAIN PLAN INCLUDING ATTRIBUTES WITH IMPLEMENTATION FOR\n"
+ "DESCRIBE STATEMENT\n"
+ "SELECT `DEPTNO`\n"
+ "FROM `EMPS`\n"
+ "UNION\n"
+ "SELECT `DEPTNO`\n"
+ "FROM `DEPTS`";
sql("describe select deptno from emps union select deptno from depts").ok(expected2);
final String expected3 = ""
+ "EXPLAIN PLAN INCLUDING ATTRIBUTES WITH IMPLEMENTATION FOR\n"
+ "DESCRIBE STATEMENT\n"
+ "INSERT INTO `EMPS`\n"
+ "VALUES (ROW(1, 'a'))";
sql("describe insert into emps values (1, 'a')").ok(expected3);
Expand Down