org.apache.calcite.sql.SqlDataTypeSpec Java Examples

The following examples show how to use org.apache.calcite.sql.SqlDataTypeSpec. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SqlValidatorUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Gets a list of extended columns with field indices to the underlying table.
 */
public static List<RelDataTypeField> getExtendedColumns(
    SqlValidator validator, SqlValidatorTable table, SqlNodeList extendedColumns) {
  final ImmutableList.Builder<RelDataTypeField> extendedFields =
      ImmutableList.builder();
  final ExtensibleTable extTable = table.unwrap(ExtensibleTable.class);
  int extendedFieldOffset =
      extTable == null
          ? table.getRowType().getFieldCount()
          : extTable.getExtendedColumnOffset();
  for (final Pair<SqlIdentifier, SqlDataTypeSpec> pair : pairs(extendedColumns)) {
    final SqlIdentifier identifier = pair.left;
    final SqlDataTypeSpec type = pair.right;
    extendedFields.add(
        new RelDataTypeFieldImpl(identifier.toString(),
            extendedFieldOffset++,
            type.deriveType(validator)));
  }
  return extendedFields.build();
}
 
Example #2
Source File: PostgresqlSqlDialect.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override public SqlNode getCastSpec(RelDataType type) {
  String castSpec;
  switch (type.getSqlTypeName()) {
  case TINYINT:
    // Postgres has no tinyint (1 byte), so instead cast to smallint (2 bytes)
    castSpec = "_smallint";
    break;
  case DOUBLE:
    // Postgres has a double type but it is named differently
    castSpec = "_double precision";
    break;
  default:
    return super.getCastSpec(type);
  }

  return new SqlDataTypeSpec(new SqlIdentifier(castSpec, SqlParserPos.ZERO),
      -1, -1, null, null, SqlParserPos.ZERO);
}
 
Example #3
Source File: RexSqlStandardConvertletTable.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Creates and registers a convertlet for an operator in which
 * the SQL representation needs the result type appended
 * as an extra argument (e.g. CAST).
 *
 * @param op operator instance
 */
private void registerTypeAppendOp(final SqlOperator op) {
  registerOp(
      op, (converter, call) -> {
        SqlNode[] operands =
            convertExpressionList(converter, call.getOperands());
        if (operands == null) {
          return null;
        }
        List<SqlNode> operandList =
            new ArrayList<>(Arrays.asList(operands));
        SqlDataTypeSpec typeSpec =
            SqlTypeUtil.convertTypeToSpec(call.getType());
        operandList.add(typeSpec);
        return new SqlBasicCall(
            op,
            operandList.toArray(new SqlNode[0]),
            SqlParserPos.ZERO);
      });
}
 
Example #4
Source File: SqlValidatorUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Gets a list of extended columns with field indices to the underlying table.
 */
public static List<RelDataTypeField> getExtendedColumns(
    RelDataTypeFactory typeFactory, SqlValidatorTable table, SqlNodeList extendedColumns) {
  final ImmutableList.Builder<RelDataTypeField> extendedFields =
      ImmutableList.builder();
  final ExtensibleTable extTable = table.unwrap(ExtensibleTable.class);
  int extendedFieldOffset =
      extTable == null
          ? table.getRowType().getFieldCount()
          : extTable.getExtendedColumnOffset();
  for (final Pair<SqlIdentifier, SqlDataTypeSpec> pair : pairs(extendedColumns)) {
    final SqlIdentifier identifier = pair.left;
    final SqlDataTypeSpec type = pair.right;
    extendedFields.add(
        new RelDataTypeFieldImpl(identifier.toString(),
            extendedFieldOffset++,
            type.deriveType(typeFactory)));
  }
  return extendedFields.build();
}
 
Example #5
Source File: OracleSqlDialect.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public SqlNode getCastSpec(RelDataType type) {
  String castSpec;
  switch (type.getSqlTypeName()) {
  case SMALLINT:
    castSpec = "NUMBER(5)";
    break;
  case INTEGER:
    castSpec = "NUMBER(10)";
    break;
  case BIGINT:
    castSpec = "NUMBER(19)";
    break;
  case DOUBLE:
    castSpec = "DOUBLE PRECISION";
    break;
  default:
    return super.getCastSpec(type);
  }

  return new SqlDataTypeSpec(
      new SqlAlienSystemTypeNameSpec(castSpec, type.getSqlTypeName(), SqlParserPos.ZERO),
      SqlParserPos.ZERO);
}
 
Example #6
Source File: RexSqlStandardConvertletTable.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Creates and registers a convertlet for an operator in which
 * the SQL representation needs the result type appended
 * as an extra argument (e.g. CAST).
 *
 * @param op operator instance
 */
private void registerTypeAppendOp(final SqlOperator op) {
  registerOp(
      op, (converter, call) -> {
        SqlNode[] operands =
            convertExpressionList(converter, call.operands);
        if (operands == null) {
          return null;
        }
        List<SqlNode> operandList =
            new ArrayList<>(Arrays.asList(operands));
        SqlDataTypeSpec typeSpec =
            SqlTypeUtil.convertTypeToSpec(call.getType());
        operandList.add(typeSpec);
        return new SqlBasicCall(
            op,
            operandList.toArray(new SqlNode[0]),
            SqlParserPos.ZERO);
      });
}
 
Example #7
Source File: PostgresqlSqlDialect.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public SqlNode getCastSpec(RelDataType type) {
  String castSpec;
  switch (type.getSqlTypeName()) {
  case TINYINT:
    // Postgres has no tinyint (1 byte), so instead cast to smallint (2 bytes)
    castSpec = "smallint";
    break;
  case DOUBLE:
    // Postgres has a double type but it is named differently
    castSpec = "double precision";
    break;
  default:
    return super.getCastSpec(type);
  }

  return new SqlDataTypeSpec(
      new SqlAlienSystemTypeNameSpec(castSpec, type.getSqlTypeName(), SqlParserPos.ZERO),
      SqlParserPos.ZERO);
}
 
Example #8
Source File: SqlRowType.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
	writer.print("ROW");
	if (getFieldNames().size() == 0) {
		writer.print("<>");
	} else {
		SqlWriter.Frame frame = writer.startList(SqlWriter.FrameTypeEnum.FUN_CALL, "<", ">");
		int i = 0;
		for (Pair<SqlIdentifier, SqlDataTypeSpec> p : Pair.zip(this.fieldNames, this.fieldTypes)) {
			writer.sep(",", false);
			p.left.unparse(writer, 0, 0);
			ExtendedSqlType.unparseType(p.right, writer, leftPrec, rightPrec);
			if (comments.get(i) != null) {
				comments.get(i).unparse(writer, leftPrec, rightPrec);
			}
			i += 1;
		}
		writer.endList(frame);
	}
}
 
Example #9
Source File: ExpressionComparator.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
protected boolean isSqlDataTypeSpecEqual(SqlDataTypeSpec querySqlDataTypeSpec,
        SqlDataTypeSpec exprSqlDataTypeSpec) {
    if (querySqlDataTypeSpec.getTypeName() == null
            || CollectionUtils.isEmpty(querySqlDataTypeSpec.getTypeName().names))
        return false;
    if (querySqlDataTypeSpec.getTypeName().names.size() != exprSqlDataTypeSpec.getTypeName().names.size())
        return false;

    for (int i = 0; i < querySqlDataTypeSpec.getTypeName().names.size(); i++) {
        String queryName = querySqlDataTypeSpec.getTypeName().names.get(i);
        if (!exprSqlDataTypeSpec.getTypeName().names.contains(queryName)) {
            return false;
        }
    }

    return querySqlDataTypeSpec.getScale() == exprSqlDataTypeSpec.getScale()
            && querySqlDataTypeSpec.getPrecision() == exprSqlDataTypeSpec.getPrecision();
}
 
Example #10
Source File: ConvSqlWriter.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public void userDefinedType(SqlDataTypeSpec typeSpec, int leftPrec, int rightPrec) {
    keyword(typeSpec.getTypeName().getSimple());

    // also print precision and scale for user-defined-type
    int precision = typeSpec.getPrecision();
    int scale = typeSpec.getScale();
    if (precision >= 0) {
        final SqlWriter.Frame frame = startList(SqlWriter.FrameTypeEnum.FUN_CALL, "(", ")");
        this.print(precision);
        if (scale >= 0) {
            this.sep(",", true);
            this.print(scale);
        }
        this.endList(frame);
    }
}
 
Example #11
Source File: ConvMaster.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
SqlDataTypeSpec findTargetSqlDataTypeSpec(SqlDataTypeSpec typeSpec) {
    if (sourceDS == null || targetDS == null || typeSpec == null)
        return null;

    List<TypeDef> validTypeDefs = sourceDS.getTypeDefsByName(typeSpec.getTypeName().toString());
    if (validTypeDefs != null) {
        for (TypeDef typeDef : validTypeDefs) {
            if (typeDef.getMaxPrecision() >= typeSpec.getPrecision()) {
                TypeDef targetType = targetDS.getTypeDef(typeDef.getId());
                return new SqlDataTypeSpec(new SqlIdentifier(targetType.getName(), typeSpec.getParserPosition()),
                        targetType.getDefaultPrecision() >= 0 ? targetType.getDefaultPrecision()
                                : typeSpec.getPrecision(),
                        targetType.getDefaultScale() >= 0 ? targetType.getDefaultScale() : typeSpec.getScale(),
                        typeSpec.getCharSetName(), typeSpec.getTimeZone(), typeSpec.getParserPosition());
            }
        }
    }
    return null;
}
 
Example #12
Source File: ExpressionComparator.java    From kylin with Apache License 2.0 6 votes vote down vote up
protected boolean isSqlDataTypeSpecEqual(SqlDataTypeSpec querySqlDataTypeSpec,
        SqlDataTypeSpec exprSqlDataTypeSpec) {
    if (querySqlDataTypeSpec.getTypeName() == null
            || CollectionUtils.isEmpty(querySqlDataTypeSpec.getTypeName().names))
        return false;
    if (querySqlDataTypeSpec.getTypeName().names.size() != exprSqlDataTypeSpec.getTypeName().names.size())
        return false;

    for (int i = 0; i < querySqlDataTypeSpec.getTypeName().names.size(); i++) {
        String queryName = querySqlDataTypeSpec.getTypeName().names.get(i);
        if (!exprSqlDataTypeSpec.getTypeName().names.contains(queryName)) {
            return false;
        }
    }

    return querySqlDataTypeSpec.getScale() == exprSqlDataTypeSpec.getScale()
            && querySqlDataTypeSpec.getPrecision() == exprSqlDataTypeSpec.getPrecision();
}
 
Example #13
Source File: ConvSqlWriter.java    From kylin with Apache License 2.0 6 votes vote down vote up
@Override
public void userDefinedType(SqlDataTypeSpec typeSpec, int leftPrec, int rightPrec) {
    keyword(typeSpec.getTypeName().getSimple());

    // also print precision and scale for user-defined-type
    int precision = typeSpec.getPrecision();
    int scale = typeSpec.getScale();
    if (precision >= 0) {
        final SqlWriter.Frame frame = startList(SqlWriter.FrameTypeEnum.FUN_CALL, "(", ")");
        this.print(precision);
        if (scale >= 0) {
            this.sep(",", true);
            this.print(scale);
        }
        this.endList(frame);
    }
}
 
Example #14
Source File: ExtendedHiveStructTypeNameSpec.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
	writer.print("STRUCT");
	SqlWriter.Frame frame = writer.startList(SqlWriter.FrameTypeEnum.FUN_CALL, "<", ">");
	int i = 0;
	for (Pair<SqlIdentifier, SqlDataTypeSpec> p : Pair.zip(getFieldNames(), getFieldTypes())) {
		writer.sep(",", false);
		p.left.unparse(writer, 0, 0);
		p.right.unparse(writer, leftPrec, rightPrec);
		if (!p.right.getNullable()) {
			writer.keyword("NOT NULL");
		}
		if (getComments().get(i) != null) {
			getComments().get(i).unparse(writer, leftPrec, rightPrec);
		}
		i += 1;
	}
	writer.endList(frame);
}
 
Example #15
Source File: ServerDdlExecutor.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Executes a {@code CREATE TYPE} command. */
public void execute(SqlCreateType create,
    CalcitePrepare.Context context) {
  final Pair<CalciteSchema, String> pair = schema(context, true, create.name);
  final SqlValidator validator = validator(context, false);
  pair.left.add(pair.right, typeFactory -> {
    if (create.dataType != null) {
      return create.dataType.deriveType(validator);
    } else {
      final RelDataTypeFactory.Builder builder = typeFactory.builder();
      for (SqlNode def : create.attributeDefs) {
        final SqlAttributeDefinition attributeDef =
            (SqlAttributeDefinition) def;
        final SqlDataTypeSpec typeSpec = attributeDef.dataType;
        final RelDataType type = typeSpec.deriveType(validator);
        builder.add(attributeDef.name.getSimple(), type);
      }
      return builder.build();
    }
  });
}
 
Example #16
Source File: SqlAttributeDefinition.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a SqlAttributeDefinition; use {@link SqlDdlNodes#attribute}. */
SqlAttributeDefinition(SqlParserPos pos, SqlIdentifier name,
    SqlDataTypeSpec dataType, SqlNode expression, SqlCollation collation) {
  super(pos);
  this.name = name;
  this.dataType = dataType;
  this.expression = expression;
  this.collation = collation;
}
 
Example #17
Source File: SqlCreateTable.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void validate() throws SqlValidateException {

	List<SqlTableConstraint> constraints = getFullConstraints().stream()
		.filter(SqlTableConstraint::isPrimaryKey)
		.collect(Collectors.toList());

	if (constraints.size() > 1) {
		throw new SqlValidateException(
			constraints.get(1).getParserPosition(),
			"Duplicate primary key definition");
	} else if (constraints.size() == 1) {
		Set<String> primaryKeyColumns = Arrays.stream(constraints.get(0).getColumnNames())
			.collect(Collectors.toSet());

		for (SqlNode column : columnList) {
			if (column instanceof SqlTableColumn) {
				SqlTableColumn tableColumn = (SqlTableColumn) column;
				if (primaryKeyColumns.contains(tableColumn.getName().getSimple())) {
					SqlDataTypeSpec notNullType = tableColumn.getType().withNullable(false);
					tableColumn.setType(notNullType);
				}
			}
		}
	}

	if (tableLike != null) {
		tableLike.validate();
	}
}
 
Example #18
Source File: ExtendedSqlType.java    From flink with Apache License 2.0 5 votes vote down vote up
static void unparseType(SqlDataTypeSpec type,
		SqlWriter writer,
		int leftPrec,
		int rightPrec) {
	if (type.getTypeName() instanceof ExtendedSqlType) {
		type.getTypeName().unparse(writer, leftPrec, rightPrec);
	} else {
		type.unparse(writer, leftPrec, rightPrec);
	}
}
 
Example #19
Source File: SqlColumnDeclaration.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a SqlColumnDeclaration; use {@link SqlDdlNodes#column}. */
SqlColumnDeclaration(SqlParserPos pos, SqlIdentifier name,
    SqlDataTypeSpec dataType, SqlNode expression,
    ColumnStrategy strategy) {
  super(pos);
  this.name = name;
  this.dataType = dataType;
  this.expression = expression;
  this.strategy = strategy;
}
 
Example #20
Source File: SqlRowType.java    From flink with Apache License 2.0 5 votes vote down vote up
public SqlRowType(SqlParserPos pos,
		List<SqlIdentifier> fieldNames,
		List<SqlDataTypeSpec> fieldTypes,
		List<SqlCharStringLiteral> comments) {
	super(SqlTypeName.ROW.getName(), pos);
	this.fieldNames = fieldNames;
	this.fieldTypes = fieldTypes;
	this.comments = comments;
}
 
Example #21
Source File: OperationConverterUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
private static TableColumn toTableColumn(SqlTableColumn sqlTableColumn, SqlValidator sqlValidator) {
	String name = sqlTableColumn.getName().getSimple();
	SqlDataTypeSpec typeSpec = sqlTableColumn.getType();
	LogicalType logicalType = FlinkTypeFactory.toLogicalType(
			typeSpec.deriveType(sqlValidator, typeSpec.getNullable()));
	DataType dataType = TypeConversions.fromLogicalToDataType(logicalType);
	return TableColumn.of(name, dataType);
}
 
Example #22
Source File: ExtendedHiveStructTypeNameSpec.java    From flink with Apache License 2.0 5 votes vote down vote up
public ExtendedHiveStructTypeNameSpec(
		SqlParserPos pos,
		List<SqlIdentifier> fieldNames,
		List<SqlDataTypeSpec> fieldTypes,
		List<SqlCharStringLiteral> comments) throws ParseException {
	super(pos, fieldNames, fieldTypes, comments, false);
	if (fieldNames.isEmpty()) {
		throw new ParseException("STRUCT with no fields is not allowed");
	}
}
 
Example #23
Source File: ClickHouseSqlDialect.java    From calcite with Apache License 2.0 5 votes vote down vote up
private SqlDataTypeSpec createSqlDataTypeSpecByName(String typeAlias, SqlTypeName typeName) {
  SqlBasicTypeNameSpec spec = new SqlBasicTypeNameSpec(typeName, SqlParserPos.ZERO) {
    @Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
      // unparse as an identifier to ensure that type names are cased correctly
      writer.identifier(typeAlias, true);
    }
  };
  return new SqlDataTypeSpec(spec, SqlParserPos.ZERO);
}
 
Example #24
Source File: HiveSqlDialect.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public SqlNode getCastSpec(final RelDataType type) {
  if (type instanceof BasicSqlType) {
    switch (type.getSqlTypeName()) {
    case INTEGER:
      SqlAlienSystemTypeNameSpec typeNameSpec = new SqlAlienSystemTypeNameSpec(
          "INT", type.getSqlTypeName(), SqlParserPos.ZERO);
      return new SqlDataTypeSpec(typeNameSpec, SqlParserPos.ZERO);
    }
  }
  return super.getCastSpec(type);
}
 
Example #25
Source File: SqlCreateTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Calls an action for each (name, type) pair from {@code columnList}, in which
 * they alternate. */
@SuppressWarnings({"unchecked"})
public void forEachNameType(BiConsumer<SqlIdentifier, SqlDataTypeSpec> consumer) {
  final List list = columnList.getList();
  Pair.forEach((List<SqlIdentifier>) Util.quotientList(list, 2, 0),
      Util.quotientList((List<SqlDataTypeSpec>) list, 2, 1), consumer);
}
 
Example #26
Source File: SqlCreateType.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a SqlCreateType. */
SqlCreateType(SqlParserPos pos, boolean replace, SqlIdentifier name,
    SqlNodeList attributeDefs, SqlDataTypeSpec dataType) {
  super(OPERATOR, pos, replace, false);
  this.name = Objects.requireNonNull(name);
  this.attributeDefs = attributeDefs; // may be null
  this.dataType = dataType; // may be null
}
 
Example #27
Source File: RelToSqlConverter.java    From quark with Apache License 2.0 5 votes vote down vote up
private SqlNode toSql(RelDataType type) {
  switch (dialect.getDatabaseProduct()) {
    case MYSQL:
      switch (type.getSqlTypeName()) {
        case VARCHAR:
          // MySQL doesn't have a VARCHAR type, only CHAR.
          return new SqlDataTypeSpec(new SqlIdentifier("CHAR", POS),
              type.getPrecision(), -1, null, null, POS);
        case INTEGER:
          return new SqlDataTypeSpec(new SqlIdentifier("_UNSIGNED", POS),
              type.getPrecision(), -1, null, null, POS);
      }
      break;
  }
  if (type instanceof BasicSqlType) {
    return new SqlDataTypeSpec(
        new SqlIdentifier(type.getSqlTypeName().name(), POS),
        type.getPrecision(),
        type.getScale(),
        type.getCharset() != null
            && dialect.supportsCharSet()
            ? type.getCharset().name()
            : null,
        null,
        POS);
  }

  return SqlTypeUtil.convertTypeToSpec(type);
  //throw new AssertionError(type); // TODO: implement
}
 
Example #28
Source File: CompilerUtil.java    From streamline with Apache License 2.0 5 votes vote down vote up
public TableBuilderInfo field(String name, SqlDataTypeSpec type, ColumnConstraint constraint) {
  RelDataType dataType = type.deriveType(typeFactory);
  if (constraint instanceof ColumnConstraint.PrimaryKey) {
    ColumnConstraint.PrimaryKey pk = (ColumnConstraint.PrimaryKey) constraint;
    Preconditions.checkState(primaryKey == -1, "There are more than one primary key in the table");
    primaryKey = fields.size();
    primaryKeyMonotonicity = pk.monotonicity();
  }
  fields.add(new FieldType(name, dataType));
  return this;
}
 
Example #29
Source File: SqlColumnDeclaration.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a SqlColumnDeclaration.
 */
public SqlColumnDeclaration(SqlParserPos pos, SqlIdentifier name,
                            SqlDataTypeSpec dataType, SqlNode expression) {
  super(pos);
  this.name = name;
  this.dataType = dataType;
  this.expression = expression;
  this.strategy = ColumnStrategy.NULLABLE;
}
 
Example #30
Source File: DremioSqlDialect.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
protected static SqlNode getVarcharWithPrecision(DremioSqlDialect dialect, RelDataType type, int precision) {
  return new SqlDataTypeSpec(
    new SqlIdentifier(type.getSqlTypeName().name(), SqlParserPos.ZERO),
    precision,
    type.getScale(),
    type.getCharset() != null && dialect.supportsCharSet()
      ? type.getCharset().name() : null,
    null,
    SqlParserPos.ZERO);
}