You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by "zoudan (via GitHub)" <gi...@apache.org> on 2023/03/13 03:01:51 UTC

[GitHub] [calcite] zoudan commented on a diff in pull request #2986: [CALCITE-5403] Babel parser should parse PostgreSQL's SET, RESET, BEGIN, SHOW, ROLLBACK, COMMIT commands

zoudan commented on code in PR #2986:
URL: https://github.com/apache/calcite/pull/2986#discussion_r1130311239


##########
babel/src/main/java/org/apache/calcite/sql/babel/postgresql/SqlBegin.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.babel.postgresql;
+
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlLiteral;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.SqlSpecialOperator;
+import org.apache.calcite.sql.SqlWriter;
+import org.apache.calcite.sql.Symbolizable;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.type.ReturnTypes;
+
+import com.google.common.collect.ImmutableList;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.util.List;
+
+/**
+ * Parse tree node representing a {@code BEGIN} clause.
+ * @see <a href="https://www.postgresql.org/docs/current/sql-begin.html">BEGIN specification</a>
+ */
+public class SqlBegin extends SqlCall {
+  public static final SqlSpecialOperator OPERATOR =

Review Comment:
   could be private?



##########
babel/src/main/java/org/apache/calcite/sql/babel/postgresql/SqlCommit.java:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.babel.postgresql;
+
+import org.apache.calcite.sql.SqlCall;
+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.sql.type.ReturnTypes;
+
+import com.google.common.collect.ImmutableList;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.util.List;
+
+/**
+ * Parse tree node representing a {@code COMMIT} clause.
+ * @see <a href="https://www.postgresql.org/docs/current/sql-commit.html">COMMIT specification</a>
+ */
+public class SqlCommit extends SqlCall {
+
+  public static final SqlSpecialOperator OPERATOR =

Review Comment:
   do it too



##########
babel/src/main/java/org/apache/calcite/sql/babel/postgresql/SqlShow.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.babel.postgresql;
+
+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.sql.type.ReturnTypes;
+
+import com.google.common.collect.ImmutableList;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.util.List;
+
+/**
+ * Parse tree node representing a {@code SHOW} clause.
+ * @see <a href="https://www.postgresql.org/docs/current/sql-show.html">SHOW specification</a>
+ */
+public class SqlShow extends SqlCall {
+  public static final SqlSpecialOperator OPERATOR =

Review Comment:
   do it too



##########
babel/src/main/codegen/config.fmpp:
##########
@@ -30,23 +30,40 @@ data: {
       "org.apache.calcite.sql.SqlCreate",
       "org.apache.calcite.sql.babel.SqlBabelCreateTable",
       "org.apache.calcite.sql.babel.TableCollectionType",
+      "org.apache.calcite.sql.babel.postgresql.AndChain",
+      "org.apache.calcite.sql.babel.postgresql.SqlBegin",
+      "org.apache.calcite.sql.babel.postgresql.SqlCommit",
+      "org.apache.calcite.sql.babel.postgresql.SqlDiscard",
+      "org.apache.calcite.sql.babel.postgresql.SqlRollback",
+      "org.apache.calcite.sql.babel.postgresql.SqlShow",
       "org.apache.calcite.sql.ddl.SqlDdlNodes",
     ]
 
     # List of new keywords. Example: "DATABASES", "TABLES". If the keyword is
     # not a reserved keyword, add it to the 'nonReservedKeywords' section.
     keywords: [
+      "DISCARD",

Review Comment:
   this comma could be removed?



##########
babel/src/main/codegen/includes/parserPostgresqlImpls.ftl:
##########
@@ -0,0 +1,260 @@
+<#--
+// 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.
+-->
+
+// SHOW (<TRANSACTION ISOLATION LEVEL> | name)
+SqlNode PostgresqlSqlShow() :
+{
+    final SqlIdentifier parameter;
+    final Span s;
+}
+{
+    <SHOW> { s = span(); }
+  (
+      parameter = PostgresqlTransactionIsolationLevel()
+|
+      <IDENTIFIER> {
+          parameter = new SqlIdentifier(token.image.toLowerCase(Locale.ROOT), getPos());
+      }
+  ) { return SqlShow.OPERATOR.createCall(null, s.end(this), parameter); }
+}
+
+SqlIdentifier PostgresqlTransactionIsolationLevel() :
+{
+    final Span s;
+}
+{
+  { s = span(); } <TRANSACTION> <ISOLATION> <LEVEL> {
+    return new SqlIdentifier("transaction_isolation", s.end(this));
+  }
+}
+
+// SET [ SESSION | LOCAL ] configuration_parameter { TO | = } { value | 'value' | DEFAULT }

Review Comment:
   use `/** ... */` for multiline comments



##########
babel/src/main/codegen/config.fmpp:
##########
@@ -30,23 +30,40 @@ data: {
       "org.apache.calcite.sql.SqlCreate",
       "org.apache.calcite.sql.babel.SqlBabelCreateTable",
       "org.apache.calcite.sql.babel.TableCollectionType",
+      "org.apache.calcite.sql.babel.postgresql.AndChain",
+      "org.apache.calcite.sql.babel.postgresql.SqlBegin",
+      "org.apache.calcite.sql.babel.postgresql.SqlCommit",
+      "org.apache.calcite.sql.babel.postgresql.SqlDiscard",
+      "org.apache.calcite.sql.babel.postgresql.SqlRollback",
+      "org.apache.calcite.sql.babel.postgresql.SqlShow",
       "org.apache.calcite.sql.ddl.SqlDdlNodes",
     ]
 
     # List of new keywords. Example: "DATABASES", "TABLES". If the keyword is
     # not a reserved keyword, add it to the 'nonReservedKeywords' section.
     keywords: [
+      "DISCARD",
       "IF"
+      "PLANS"
+      "SEED"
       "SEMI"
+      "SEQUENCES"
+      "TEMP"
       "VOLATILE"
     ]
 
     # List of non-reserved keywords to add;
     # items in this list become non-reserved
     nonReservedKeywordsToAdd: [
       # not in core, added in babel
+      "DISCARD",

Review Comment:
   do it too



##########
babel/src/main/java/org/apache/calcite/sql/babel/postgresql/SqlDiscard.java:
##########
@@ -0,0 +1,70 @@
+/*
+ * 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.babel.postgresql;
+
+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.sql.type.ReturnTypes;
+
+import com.google.common.collect.ImmutableList;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.util.List;
+
+/**
+ * Parse tree node representing a {@code DISCARD} clause.
+ * @see <a href="https://www.postgresql.org/docs/current/sql-discard.html">DISCARD specification</a>
+ */
+public class SqlDiscard extends SqlCall {
+  public static final SqlSpecialOperator OPERATOR =

Review Comment:
   do it too



##########
babel/src/main/java/org/apache/calcite/sql/babel/postgresql/SqlRollback.java:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.babel.postgresql;
+
+import org.apache.calcite.sql.SqlCall;
+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.sql.type.ReturnTypes;
+
+import com.google.common.collect.ImmutableList;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.util.List;
+
+/**
+ * Parse tree node representing a {@code ROLLBACK} clause.
+ * @see <a href="https://www.postgresql.org/docs/current/sql-rollback.html">ROLLBACK specification</a>
+ */
+public class SqlRollback extends SqlCall {
+
+  public static final SqlSpecialOperator OPERATOR =

Review Comment:
   do it too



##########
babel/src/main/codegen/config.fmpp:
##########
@@ -30,23 +30,40 @@ data: {
       "org.apache.calcite.sql.SqlCreate",
       "org.apache.calcite.sql.babel.SqlBabelCreateTable",
       "org.apache.calcite.sql.babel.TableCollectionType",
+      "org.apache.calcite.sql.babel.postgresql.AndChain",
+      "org.apache.calcite.sql.babel.postgresql.SqlBegin",
+      "org.apache.calcite.sql.babel.postgresql.SqlCommit",
+      "org.apache.calcite.sql.babel.postgresql.SqlDiscard",
+      "org.apache.calcite.sql.babel.postgresql.SqlRollback",
+      "org.apache.calcite.sql.babel.postgresql.SqlShow",
       "org.apache.calcite.sql.ddl.SqlDdlNodes",
     ]
 
     # List of new keywords. Example: "DATABASES", "TABLES". If the keyword is
     # not a reserved keyword, add it to the 'nonReservedKeywords' section.
     keywords: [
+      "DISCARD",
       "IF"
+      "PLANS"
+      "SEED"
       "SEMI"
+      "SEQUENCES"
+      "TEMP"
       "VOLATILE"
     ]
 
     # List of non-reserved keywords to add;
     # items in this list become non-reserved
     nonReservedKeywordsToAdd: [
       # not in core, added in babel
+      "DISCARD",
       "IF"
+      "PLANS"
+      "SEED"
       "SEMI"
+      "SEQUENCES"
+      "TEMP"
+      "VOLATILE"

Review Comment:
   should we add 'VOLATILE' as a non-reserved keyword?



##########
babel/src/main/codegen/includes/parserPostgresqlImpls.ftl:
##########
@@ -0,0 +1,260 @@
+<#--
+// 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.
+-->
+
+// SHOW (<TRANSACTION ISOLATION LEVEL> | name)
+SqlNode PostgresqlSqlShow() :
+{
+    final SqlIdentifier parameter;
+    final Span s;
+}
+{
+    <SHOW> { s = span(); }
+  (
+      parameter = PostgresqlTransactionIsolationLevel()
+|
+      <IDENTIFIER> {
+          parameter = new SqlIdentifier(token.image.toLowerCase(Locale.ROOT), getPos());
+      }
+  ) { return SqlShow.OPERATOR.createCall(null, s.end(this), parameter); }
+}
+
+SqlIdentifier PostgresqlTransactionIsolationLevel() :
+{
+    final Span s;
+}
+{
+  { s = span(); } <TRANSACTION> <ISOLATION> <LEVEL> {
+    return new SqlIdentifier("transaction_isolation", s.end(this));
+  }
+}
+
+// SET [ SESSION | LOCAL ] configuration_parameter { TO | = } { value | 'value' | DEFAULT }
+// SET [ SESSION | LOCAL ] TIME ZONE { value | 'value' | LOCAL | DEFAULT }
+// SET [ SESSION | LOCAL ] (SCHEMA | NAMES | SEED) value
+// value - Values can be specified as string constants, identifiers, numbers, or comma-separated lists of these
+SqlNode PostgresqlSqlSetOption() :
+{
+    SqlIdentifier name;
+    final SqlNode val;
+    String scope = null;
+    Span s;
+}
+{
+    { s = span(); }
+    (
+        <SET> {
+            s.add(this);
+        }
+        [ scope = PostgresqlOptionScope() ]
+        (
+          <TIME> <ZONE> { name = new SqlIdentifier("timezone", getPos()); }
+          (
+            val = Default()
+          |
+            <LOCAL> { val = SqlLiteral.createCharString("LOCAL", getPos()); }
+          |
+            val = Literal()
+          )
+        |
+          (
+              <SCHEMA> { name = new SqlIdentifier("search_path", getPos()); }
+            |
+              <NAMES> { name = new SqlIdentifier("client_encoding", getPos()); }
+            |
+              <SEED> { name = new SqlIdentifier("seed", getPos()); }
+            |
+              name = CompoundIdentifier()
+              ( <EQ> | <TO> )
+          )
+          val = PostgresqlSqlOptionValues()
+        ){
+          return new SqlSetOption(s.end(val), scope, name, val);
+        }
+
+    |
+        <RESET> {
+            s.add(this);
+        }
+        (
+            name = CompoundIdentifier()
+        |
+            <ALL> {
+                name = new SqlIdentifier(token.image.toUpperCase(Locale.ROOT),
+                    getPos());
+            }
+        )
+        {
+            return new SqlSetOption(s.end(name), scope, name, null);
+        }
+    )
+}
+
+String PostgresqlOptionScope() :
+{
+}
+{
+    ( <LOCAL> | <SESSION> ) { return token.image.toUpperCase(Locale.ROOT); }
+}
+
+SqlNode PostgresqlSqlOptionValues():
+{
+  final List<SqlNode> list;
+  Span s;
+  SqlNode e;
+}
+{
+   e = PostgresqlSqlOptionValue() { s = span(); list = startList(e); }
+   ( <COMMA> e = PostgresqlSqlOptionValue() { list.add(e); } )*
+   { 
+      return list.size() > 1 ? new SqlNodeList(list, s.end(this)) : e; 
+   }
+}
+
+SqlNode PostgresqlSqlOptionValue():
+{
+  final SqlNode val;
+}
+{
+  (
+      val = Default()
+  |
+      val = Literal()
+  |
+      val = SimpleIdentifier()
+  ) {
+    return val;
+  }
+}
+
+// DISCARD { ALL | PLANS | SEQUENCES | TEMPORARY | TEMP }
+SqlNode PostgresqlSqlDiscard() :
+{
+  final Span s;
+}
+{
+  { s = span(); }
+   <DISCARD> ( <ALL> | <PLANS> | <SEQUENCES> | <TEMPORARY> | <TEMP> ) {
+      return SqlDiscard.OPERATOR.createCall(s.end(this), new SqlIdentifier(
+         token.image.toUpperCase(Locale.ROOT), getPos()));
+   }
+}
+
+// BEGIN [ WORK | TRANSACTION ] [ transaction_mode [, ...] ]

Review Comment:
   do it too



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org