vvysotskyi commented on a change in pull request #1615: DRILL-6964: Implement 
CREATE / DROP TABLE SCHEMA commands
URL: https://github.com/apache/drill/pull/1615#discussion_r249442649
 
 

 ##########
 File path: 
exec/java-exec/src/main/java/org/apache/drill/exec/record/metadata/schema/parser/TableSchemaVisitor.java
 ##########
 @@ -0,0 +1,247 @@
+/*
+ * 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.drill.exec.record.metadata.schema.parser;
+
+import org.antlr.v4.runtime.tree.TerminalNode;
+import org.apache.drill.common.types.TypeProtos;
+import org.apache.drill.common.types.Types;
+import org.apache.drill.exec.record.MaterializedField;
+import org.apache.drill.exec.record.metadata.ColumnMetadata;
+import org.apache.drill.exec.record.metadata.MapBuilder;
+import org.apache.drill.exec.record.metadata.MetadataUtils;
+import org.apache.drill.exec.record.metadata.RepeatedListBuilder;
+import org.apache.drill.exec.record.metadata.TupleMetadata;
+import org.apache.drill.exec.record.metadata.TupleSchema;
+
+import java.util.List;
+
+/**
+ * Visits table schema and stores metadata about its columns into {@link 
TupleMetadata} class.
+ */
+public class TableSchemaVisitor extends 
TableSchemaParserBaseVisitor<TupleMetadata> {
+
+  @Override
+  public TupleMetadata visitSchema(TableSchemaParser.SchemaContext ctx) {
+    return visitColumns(ctx.columns());
+  }
+
+  @Override
+  public TupleMetadata visitColumns(TableSchemaParser.ColumnsContext ctx) {
+    TupleMetadata schema = new TupleSchema();
+    ColumnVisitor columnVisitor = new ColumnVisitor();
+    ctx.column().forEach(
+      c -> schema.addColumn(c.accept(columnVisitor))
+    );
+    return schema;
+  }
+
+  /**
+   * Visits various types of columns (primitive, map, array) and stores their 
metadata
+   * into {@link ColumnMetadata} class.
+   */
+  public static class ColumnVisitor extends 
TableSchemaParserBaseVisitor<ColumnMetadata> {
+
+    @Override
+    public ColumnMetadata 
visitPrimitive_column(TableSchemaParser.Primitive_columnContext ctx) {
+      String name = ctx.column_id().accept(new IdVisitor());
+      TypeProtos.DataMode mode = ctx.nullability() == null ? 
TypeProtos.DataMode.OPTIONAL : TypeProtos.DataMode.REQUIRED;
+      return ctx.simple_type().accept(new TypeVisitor(name, mode));
+    }
+
+    @Override
+    public ColumnMetadata 
visitSimple_array_column(TableSchemaParser.Simple_array_columnContext ctx) {
+      String name = ctx.column_id().accept(new IdVisitor());
+      return ctx.simple_array_type().accept(new ArrayTypeVisitor(name));
+    }
+
+    @Override
+    public ColumnMetadata visitMap_column(TableSchemaParser.Map_columnContext 
ctx) {
+      String name = ctx.column_id().accept(new IdVisitor());
+      // Drill does not distinguish between nullable and not null map, by 
default they are not null
+      return ctx.map_type().accept(new TypeVisitor(name, 
TypeProtos.DataMode.REQUIRED));
+    }
+
+    @Override
+    public ColumnMetadata 
visitComplex_array_column(TableSchemaParser.Complex_array_columnContext ctx) {
+      String name = ctx.column_id().accept(new IdVisitor());
+      ColumnMetadata child = 
ctx.complex_array_type().complex_type().accept(new ArrayTypeVisitor(name));
+      RepeatedListBuilder builder = new RepeatedListBuilder(null, name);
+      builder.addColumn(child);
+      return builder.buildColumn();
+    }
+
+  }
+
+  /**
+   * Visits ID and QUOTED_ID, returning their string representation.
+   */
+  private static class IdVisitor extends TableSchemaParserBaseVisitor<String> {
+
+    @Override
+    public String visitId(TableSchemaParser.IdContext ctx) {
+      return ctx.ID().getText();
+    }
+
+    @Override
+    public String visitQuoted_id(TableSchemaParser.Quoted_idContext ctx) {
+      String text = ctx.QUOTED_ID().getText();
+      // first substring first and last symbols (backticks)
+      // then find all chars that are preceding with the backslash and remove 
the backslash
+      return text.substring(1, text.length() -1).replaceAll("\\\\(.)", "$1");
+    }
+  }
+
+  /**
+   * Visits simple and map types, storing their metadata into {@link 
ColumnMetadata} holder.
+   */
+  private static class TypeVisitor extends 
TableSchemaParserBaseVisitor<ColumnMetadata> {
+
+    private final String name;
+    private final TypeProtos.DataMode mode;
+
+    TypeVisitor(String name, TypeProtos.DataMode mode) {
+      this.name = name;
+      this.mode = mode;
+    }
+
+    @Override
+    public ColumnMetadata visitInt(TableSchemaParser.IntContext ctx) {
+      return constructColumn(Types.withMode(TypeProtos.MinorType.INT, mode));
+    }
+
+    @Override
+    public ColumnMetadata visitBigint(TableSchemaParser.BigintContext ctx) {
+      return constructColumn(Types.withMode(TypeProtos.MinorType.BIGINT, 
mode));
+    }
+
+    @Override
+    public ColumnMetadata visitFloat(TableSchemaParser.FloatContext ctx) {
+      return constructColumn(Types.withMode(TypeProtos.MinorType.FLOAT4, 
mode));
+    }
+
+    @Override
+    public ColumnMetadata visitDouble(TableSchemaParser.DoubleContext ctx) {
+      return constructColumn(Types.withMode(TypeProtos.MinorType.FLOAT8, 
mode));
+    }
+
+    @Override
+    public ColumnMetadata visitDecimal(TableSchemaParser.DecimalContext ctx) {
+      TypeProtos.MajorType type = 
Types.withMode(TypeProtos.MinorType.VARDECIMAL, mode);
+
+      List<TerminalNode> numbers = ctx.NUMBER();
+      if (!numbers.isEmpty()) {
+        int precision = Integer.parseInt(numbers.get(0).getText());
+        int scale = numbers.size() == 2 ? 
Integer.parseInt(numbers.get(1).getText()) : 0;
+        type = 
type.toBuilder().setPrecision(precision).setScale(scale).build();
+      }
+
+      return constructColumn(type);
+    }
+
+    @Override
+    public ColumnMetadata visitBoolean(TableSchemaParser.BooleanContext ctx) {
+      return constructColumn(Types.withMode(TypeProtos.MinorType.BIT, mode));
+    }
+
+    @Override
+    public ColumnMetadata visitVarchar(TableSchemaParser.VarcharContext ctx) {
+      TypeProtos.MajorType type = Types.withMode(TypeProtos.MinorType.VARCHAR, 
mode);
+
+      if (ctx.NUMBER() != null) {
+        type = 
type.toBuilder().setPrecision(Integer.parseInt(ctx.NUMBER().getText())).build();
+      }
+
+      return constructColumn(type);
+    }
+
+    @Override
+    public ColumnMetadata visitBinary(TableSchemaParser.BinaryContext ctx) {
+      TypeProtos.MajorType type = 
Types.withMode(TypeProtos.MinorType.VARBINARY, mode);
+
+      if (ctx.NUMBER() != null) {
+        type = 
type.toBuilder().setPrecision(Integer.parseInt(ctx.NUMBER().getText())).build();
+      }
+
+      return constructColumn(type);
+    }
+
+    @Override
+    public ColumnMetadata visitTime(TableSchemaParser.TimeContext ctx) {
+      return constructColumn(Types.withMode(TypeProtos.MinorType.TIME, mode));
 
 Review comment:
   Precision for TIME and TIMESTAMP

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to