public DataTypeExpr visitData_type()

in tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/SQLAnalyzer.java [1081:1222]


  public DataTypeExpr visitData_type(SQLParser.Data_typeContext ctx) {
    SQLParser.Predefined_typeContext predefined_type = ctx.predefined_type();

    DataTypeExpr typeDefinition = null;
    if (predefined_type.character_string_type() != null) {
      SQLParser.Character_string_typeContext character_string_type =
          predefined_type.character_string_type();

      if ((character_string_type.CHARACTER() != null || character_string_type.CHAR() != null) &&
          character_string_type.VARYING() == null) {

        typeDefinition = new DataTypeExpr(Type.CHAR.name());

        if (character_string_type.type_length() != null) {
          typeDefinition.setLengthOrPrecision(
              Integer.parseInt(character_string_type.type_length().NUMBER().getText()));
        }

      } else if (character_string_type.VARCHAR() != null
          || character_string_type.VARYING() != null) {

        typeDefinition = new DataTypeExpr(Type.VARCHAR.name());

        if (character_string_type.type_length() != null) {
          typeDefinition.setLengthOrPrecision(
              Integer.parseInt(character_string_type.type_length().NUMBER().getText()));
        }

      } else if (character_string_type.TEXT() != null) {
        typeDefinition =  new DataTypeExpr(Type.TEXT.name());
      }

    } else if (predefined_type.national_character_string_type() != null) {
      SQLParser.National_character_string_typeContext nchar_type =
          predefined_type.national_character_string_type();
      if ((nchar_type.CHAR() != null || nchar_type.CHARACTER() != null
          || nchar_type.NCHAR() != null) && nchar_type.VARYING() == null) {
        typeDefinition = new DataTypeExpr(Type.NCHAR.name());
      } else if (nchar_type.NVARCHAR() != null || nchar_type.VARYING() != null) {
        typeDefinition = new DataTypeExpr(Type.NVARCHAR.name());
      }

      if (nchar_type.type_length() != null) {
        typeDefinition.setLengthOrPrecision(
            Integer.parseInt(nchar_type.type_length().NUMBER().getText()));
      }

    } else if (predefined_type.binary_large_object_string_type() != null) {
      SQLParser.Binary_large_object_string_typeContext blob_type =
          predefined_type.binary_large_object_string_type();
      typeDefinition = new DataTypeExpr(Type.BLOB.name());
      if (blob_type.type_length() != null) {
        typeDefinition.setLengthOrPrecision(
            Integer.parseInt(blob_type.type_length().NUMBER().getText()));
      }
    } else if (predefined_type.numeric_type() != null) {
      // exact number
      if (predefined_type.numeric_type().exact_numeric_type() != null) {
        SQLParser.Exact_numeric_typeContext exactType =
            predefined_type.numeric_type().exact_numeric_type();
        if (exactType.TINYINT() != null || exactType.INT1() != null) {
          typeDefinition = new DataTypeExpr(Type.INT1.name());
        } else if (exactType.INT2() != null || exactType.SMALLINT() != null) {
          typeDefinition = new DataTypeExpr(Type.INT2.name());
        } else if (exactType.INT4() != null || exactType.INTEGER() != null ||
            exactType.INT() != null) {
          typeDefinition = new DataTypeExpr(Type.INT4.name());
        } else if (exactType.INT8() != null || exactType.BIGINT() != null) {
          typeDefinition = new DataTypeExpr(Type.INT8.name());
        } else if (exactType.NUMERIC() != null) {
          typeDefinition = new DataTypeExpr(Type.NUMERIC.name());
        } else if (exactType.DECIMAL() != null || exactType.DEC() != null) {
          typeDefinition = new DataTypeExpr(Type.DECIMAL.name());
        }

        if (typeDefinition.getTypeName().equals(Type.NUMERIC.name()) ||
            typeDefinition.getTypeName().equals(Type.DECIMAL.name())) {
          if (exactType.precision_param() != null) {
            if (exactType.precision_param().scale != null) {
              typeDefinition.setScale(
                  Integer.parseInt(exactType.precision_param().scale.getText()));
            }
            typeDefinition.setLengthOrPrecision(
                Integer.parseInt(exactType.precision_param().precision.getText()));
          }
        }
      } else { // approximate number
        SQLParser.Approximate_numeric_typeContext approximateType =
            predefined_type.numeric_type().approximate_numeric_type();
        if (approximateType.FLOAT() != null || approximateType.FLOAT4() != null
            || approximateType.REAL() != null) {
          typeDefinition = new DataTypeExpr(Type.FLOAT4.name());
        } else if (approximateType.FLOAT8() != null || approximateType.DOUBLE() != null) {
          typeDefinition = new DataTypeExpr(Type.FLOAT8.name());
        }
      }
    } else if (predefined_type.boolean_type() != null)  {
      typeDefinition = new DataTypeExpr(Type.BOOLEAN.name());
    } else if (predefined_type.datetime_type() != null) {
      SQLParser.Datetime_typeContext dateTimeType = predefined_type.datetime_type();
      if (dateTimeType.DATE() != null) {
        typeDefinition = new DataTypeExpr(Type.DATE.name());
      } else if (dateTimeType.TIME(0) != null && dateTimeType.ZONE() == null) {
        typeDefinition = new DataTypeExpr(Type.TIME.name());
      } else if ((dateTimeType.TIME(0) != null && dateTimeType.ZONE() != null) ||
          dateTimeType.TIMETZ() != null) {
        typeDefinition = new DataTypeExpr(Type.TIMEZ.name());
      } else if (dateTimeType.TIMESTAMP() != null && dateTimeType.ZONE() == null) {
        typeDefinition = new DataTypeExpr(Type.TIMESTAMP.name());
      } else if ((dateTimeType.TIMESTAMP() != null && dateTimeType.ZONE() != null) ||
          dateTimeType.TIMESTAMPTZ() != null) {
        typeDefinition = new DataTypeExpr(Type.TIMESTAMPZ.name());
      }
    } else if (predefined_type.bit_type() != null) {
      SQLParser.Bit_typeContext bitType = predefined_type.bit_type();
      if (bitType.VARBIT() != null || bitType.VARYING() != null) {
        typeDefinition = new DataTypeExpr(Type.VARBIT.name());
      } else {
        typeDefinition = new DataTypeExpr(Type.BIT.name());
      }
      if (bitType.type_length() != null) {
        typeDefinition.setLengthOrPrecision(
            Integer.parseInt(bitType.type_length().NUMBER().getText()));
      }
    } else if (predefined_type.binary_type() != null) {
      SQLParser.Binary_typeContext binaryType = predefined_type.binary_type();
      if (binaryType.VARBINARY() != null || binaryType.VARYING() != null) {
        typeDefinition = new DataTypeExpr(Type.VARBINARY.name());
      } else {
        typeDefinition = new DataTypeExpr(Type.BINARY.name());
      }

      if (binaryType.type_length() != null) {
        typeDefinition.setLengthOrPrecision(
            Integer.parseInt(binaryType.type_length().NUMBER().getText()));
      }
    } else if (predefined_type.network_type() != null) {
      typeDefinition = new DataTypeExpr(Type.INET4.name());
    }

    return typeDefinition;
  }