github.com/apache/beam/sdks/v2@v2.48.2/java/extensions/sql/src/main/codegen/includes/parserImpls.ftl (about)

     1  <#-- Licensed to the Apache Software Foundation (ASF) under one or more contributor
     2    license agreements. See the NOTICE file distributed with this work for additional
     3    information regarding copyright ownership. The ASF licenses this file to
     4    You under the Apache License, Version 2.0 (the "License"); you may not use
     5    this file except in compliance with the License. You may obtain a copy of
     6    the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
     7    by applicable law or agreed to in writing, software distributed under the
     8    License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
     9    OF ANY KIND, either express or implied. See the License for the specific
    10    language governing permissions and limitations under the License. -->
    11  
    12  boolean IfNotExistsOpt() :
    13  {
    14  }
    15  {
    16      <IF> <NOT> <EXISTS> { return true; }
    17  |
    18      { return false; }
    19  }
    20  
    21  boolean IfExistsOpt() :
    22  {
    23  }
    24  {
    25      <IF> <EXISTS> { return true; }
    26  |
    27      { return false; }
    28  }
    29  
    30  SqlNodeList Options() :
    31  {
    32      final Span s;
    33      final List<SqlNode> list = new ArrayList<SqlNode>();
    34  }
    35  {
    36      <OPTIONS> { s = span(); } <LPAREN>
    37      [
    38          Option(list)
    39          (
    40              <COMMA>
    41              Option(list)
    42          )*
    43      ]
    44      <RPAREN> {
    45          return new SqlNodeList(list, s.end(this));
    46      }
    47  }
    48  
    49  void Option(List<SqlNode> list) :
    50  {
    51      final SqlIdentifier id;
    52      final SqlNode value;
    53  }
    54  {
    55      id = SimpleIdentifier()
    56      value = Literal() {
    57          list.add(id);
    58          list.add(value);
    59      }
    60  }
    61  
    62  List<Schema.Field> FieldListParens() :
    63  {
    64      final List<Schema.Field> fields;
    65  }
    66  {
    67      <LPAREN>
    68          fields = FieldListBody()
    69      <RPAREN>
    70      {
    71          return fields;
    72      }
    73  }
    74  
    75  List<Schema.Field> FieldListAngular() :
    76  {
    77      final List<Schema.Field> fields;
    78  }
    79  {
    80      <LT>
    81          fields = FieldListBody()
    82      <GT>
    83      {
    84          return fields;
    85      }
    86  }
    87  
    88  List<Schema.Field> FieldListBody() :
    89  {
    90      final List<Schema.Field> fields = new ArrayList<Schema.Field>();
    91      Schema.Field field = null;
    92  }
    93  {
    94      field = Field() { fields.add(field); }
    95      (
    96          <COMMA> field = Field() { fields.add(field); }
    97      )*
    98      {
    99          return fields;
   100      }
   101  }
   102  
   103  Schema.Field Field() :
   104  {
   105      final String name;
   106      final Schema.FieldType type;
   107      final boolean nullable;
   108      Schema.Field field = null;
   109      SqlNode comment = null;
   110  }
   111  {
   112      name = Identifier()
   113      type = FieldType()
   114      {
   115          field = Schema.Field.of(name, type);
   116      }
   117      (
   118          <NULL> { field = field.withNullable(true); }
   119      |
   120          <NOT> <NULL> { field = field.withNullable(false); }
   121      |
   122          { field = field.withNullable(true); }
   123      )
   124      [
   125          <COMMENT> comment = StringLiteral()
   126          {
   127              if (comment != null) {
   128                  String commentString =
   129                      ((NlsString) SqlLiteral.value(comment)).getValue();
   130                  field = field.withDescription(commentString);
   131              }
   132          }
   133      ]
   134      {
   135          return field;
   136      }
   137  }
   138  
   139  /**
   140   * Note: This example is probably out of sync with the code.
   141   *
   142   * CREATE TABLE ( IF NOT EXISTS )?
   143   *   ( database_name '.' )? table_name '(' column_def ( ',' column_def )* ')'
   144   *   TYPE type_name
   145   *   ( COMMENT comment_string )?
   146   *   ( LOCATION location_string )?
   147   *   ( TBLPROPERTIES tbl_properties )?
   148   */
   149  SqlCreate SqlCreateExternalTable(Span s, boolean replace) :
   150  {
   151      final boolean ifNotExists;
   152      final SqlIdentifier id;
   153      List<Schema.Field> fieldList = null;
   154      final SqlNode type;
   155      SqlNode comment = null;
   156      SqlNode location = null;
   157      SqlNode tblProperties = null;
   158  }
   159  {
   160  
   161      <EXTERNAL> <TABLE> {
   162          s.add(this);
   163      }
   164  
   165      ifNotExists = IfNotExistsOpt()
   166      id = CompoundIdentifier()
   167      fieldList = FieldListParens()
   168      <TYPE>
   169      (
   170          type = StringLiteral()
   171      |
   172          type = SimpleIdentifier()
   173      )
   174      [ <COMMENT> comment = StringLiteral() ]
   175      [ <LOCATION> location = StringLiteral() ]
   176      [ <TBLPROPERTIES> tblProperties = StringLiteral() ]
   177      {
   178          return
   179              new SqlCreateExternalTable(
   180                  s.end(this),
   181                  replace,
   182                  ifNotExists,
   183                  id,
   184                  fieldList,
   185                  type,
   186                  comment,
   187                  location,
   188                  tblProperties);
   189      }
   190  }
   191  
   192  SqlCreate SqlCreateFunction(Span s, boolean replace) :
   193  {
   194      boolean isAggregate = false;
   195      final SqlIdentifier name;
   196      final SqlNode jarName;
   197  }
   198  {
   199      (
   200          <AGGREGATE> {
   201              isAggregate = true;
   202          }
   203      )?
   204      <FUNCTION> {
   205          s.add(this);
   206      }
   207      name = CompoundIdentifier()
   208      <USING> <JAR>
   209          jarName = StringLiteral()
   210      {
   211          return
   212              new SqlCreateFunction(
   213                  s.end(this),
   214                  replace,
   215                  name,
   216                  jarName,
   217                  isAggregate);
   218      }
   219  }
   220  
   221  SqlCreate SqlCreateTableNotSupportedMessage(Span s, boolean replace) :
   222  {
   223  }
   224  {
   225    <TABLE>
   226    {
   227      throw new ParseException("'CREATE TABLE' is not supported in SQL. You can use "
   228      + "'CREATE EXTERNAL TABLE' to register an external data source to SQL. For more details, "
   229      + "please check: https://beam.apache.org/documentation/dsls/sql/create-external-table");
   230    }
   231  }
   232  
   233  SqlDrop SqlDropTable(Span s, boolean replace) :
   234  {
   235      final boolean ifExists;
   236      final SqlIdentifier id;
   237  }
   238  {
   239      <TABLE> ifExists = IfExistsOpt() id = CompoundIdentifier() {
   240          return SqlDdlNodes.dropTable(s.end(this), ifExists, id);
   241      }
   242  }
   243  
   244  Schema.FieldType FieldType() :
   245  {
   246      final SqlTypeName collectionTypeName;
   247      Schema.FieldType fieldType;
   248      final Span s = Span.of();
   249  }
   250  {
   251      (
   252          fieldType = Map()
   253      |
   254          fieldType = Array()
   255      |
   256          fieldType = Row()
   257      |
   258          fieldType = SimpleType()
   259      )
   260      {
   261          return fieldType;
   262      }
   263  }
   264  
   265  Schema.FieldType Array() :
   266  {
   267      final Schema.FieldType arrayElementType;
   268  }
   269  {
   270      <ARRAY> <LT> arrayElementType = FieldType() <GT>
   271      {
   272          return Schema.FieldType.array(arrayElementType);
   273      }
   274  
   275  }
   276  
   277  Schema.FieldType Map() :
   278  {
   279      final Schema.FieldType mapKeyType;
   280      final Schema.FieldType mapValueType;
   281  }
   282  {
   283      <MAP>
   284          <LT>
   285              mapKeyType = SimpleType()
   286          <COMMA>
   287              mapValueType = FieldType()
   288          <GT>
   289      {
   290          return Schema.FieldType.map(mapKeyType, mapValueType);
   291      }
   292  }
   293  
   294  Schema.FieldType Row() :
   295  {
   296      final List<Schema.Field> fields;
   297  }
   298  {
   299      <ROW> fields = RowFields()
   300      {
   301          Schema rowSchema = Schema.builder().addFields(fields).build();
   302          return Schema.FieldType.row(rowSchema);
   303      }
   304  }
   305  
   306  List<Schema.Field> RowFields() :
   307  {
   308      final List<Schema.Field> fields;
   309  }
   310  {
   311      (
   312          fields = FieldListParens()
   313      |
   314          fields = FieldListAngular()
   315      )
   316      {
   317          return fields;
   318      }
   319  }
   320  
   321  Schema.FieldType SimpleType() :
   322  {
   323      final Span s = Span.of();
   324      final SqlTypeNameSpec simpleTypeName;
   325  }
   326  {
   327      simpleTypeName = SqlTypeName(s)
   328      {
   329          s.end(this);
   330          return CalciteUtils.toFieldType(simpleTypeName);
   331      }
   332  }
   333  
   334  SqlSetOptionBeam SqlSetOptionBeam(Span s, String scope) :
   335  {
   336      SqlIdentifier name;
   337      final SqlNode val;
   338  }
   339  {
   340      (
   341          <SET> {
   342              s.add(this);
   343          }
   344          name = CompoundIdentifier()
   345          <EQ>
   346          (
   347              val = Literal()
   348          |
   349              val = SimpleIdentifier()
   350          |
   351              <ON> {
   352                  // OFF is handled by SimpleIdentifier, ON handled here.
   353                  val = new SqlIdentifier(token.image.toUpperCase(Locale.ROOT),
   354                      getPos());
   355              }
   356          )
   357          {
   358              return new SqlSetOptionBeam(s.end(val), scope, name, val);
   359          }
   360      |
   361          <RESET> {
   362              s.add(this);
   363          }
   364          (
   365              name = CompoundIdentifier()
   366          |
   367              <ALL> {
   368                  name = new SqlIdentifier(token.image.toUpperCase(Locale.ROOT),
   369                      getPos());
   370              }
   371          )
   372          {
   373              return new SqlSetOptionBeam(s.end(name), scope, name, null);
   374          }
   375      )
   376  }
   377  
   378  // End parserImpls.ftl