Java Code Examples for org.apache.calcite.sql.SqlDataTypeSpec

The following examples show how to use org.apache.calcite.sql.SqlDataTypeSpec. These examples are extracted from open source projects. 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 Project: Bats   Source File: SqlValidatorUtil.java    License: 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 2
Source Project: Bats   Source File: PostgresqlSqlDialect.java    License: 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 Project: Bats   Source File: RexSqlStandardConvertletTable.java    License: 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 Project: calcite   Source File: OracleSqlDialect.java    License: 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 5
Source Project: calcite   Source File: RexSqlStandardConvertletTable.java    License: 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 6
Source Project: calcite   Source File: PostgresqlSqlDialect.java    License: 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 7
Source Project: flink   Source File: SqlRowType.java    License: 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 8
Source Project: kylin-on-parquet-v2   Source File: ExpressionComparator.java    License: 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 9
Source Project: kylin-on-parquet-v2   Source File: ConvSqlWriter.java    License: 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 10
Source Project: kylin-on-parquet-v2   Source File: ConvMaster.java    License: 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 11
Source Project: kylin   Source File: ExpressionComparator.java    License: 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 12
Source Project: kylin   Source File: ConvSqlWriter.java    License: 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 13
Source Project: flink   Source File: ExtendedHiveStructTypeNameSpec.java    License: 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 14
Source Project: calcite   Source File: ServerDdlExecutor.java    License: 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 15
Source Project: calcite   Source File: SqlValidatorUtil.java    License: 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 16
Source Project: calcite   Source File: SqlAttributeDefinition.java    License: 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 Project: calcite   Source File: SqlCreateType.java    License: 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 18
Source Project: Flink-CEPplus   Source File: SqlValidatorImpl.java    License: Apache License 2.0 5 votes vote down vote up
public RelDataType visit(SqlDataTypeSpec dataType) {
	// Q. How can a data type have a type?
	// A. When it appears in an expression. (Say as the 2nd arg to the
	//    CAST operator.)
	validateDataType(dataType);
	return dataType.deriveType(SqlValidatorImpl.this);
}
 
Example 19
Source Project: flink   Source File: SqlValidatorImpl.java    License: Apache License 2.0 5 votes vote down vote up
public RelDataType visit(SqlDataTypeSpec dataType) {
	// Q. How can a data type have a type?
	// A. When it appears in an expression. (Say as the 2nd arg to the
	//    CAST operator.)
	validateDataType(dataType);
	return dataType.deriveType(SqlValidatorImpl.this);
}
 
Example 20
Source Project: flink   Source File: SqlTableColumn.java    License: Apache License 2.0 5 votes vote down vote up
public SqlTableColumn(SqlIdentifier name,
		SqlDataTypeSpec type,
		SqlCharStringLiteral comment,
		SqlParserPos pos) {
	super(pos);
	this.name = requireNonNull(name, "Column name should not be null");
	this.type = requireNonNull(type, "Column type should not be null");
	this.comment = comment;
}
 
Example 21
Source Project: flink   Source File: ExtendedSqlType.java    License: 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 22
Source Project: flink   Source File: SqlRowType.java    License: 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 23
Source Project: dremio-oss   Source File: SqlColumnDeclaration.java    License: 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 24
Source Project: dremio-oss   Source File: DremioSqlDialect.java    License: 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);
}
 
Example 25
Source Project: streamline   Source File: CompilerUtil.java    License: 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 26
Source Project: quark   Source File: RelToSqlConverter.java    License: 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 27
Source Project: calcite   Source File: SqlCreateTable.java    License: 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 28
Source Project: calcite   Source File: HiveSqlDialect.java    License: 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 29
Source Project: calcite   Source File: ClickHouseSqlDialect.java    License: 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 30
Source Project: flink   Source File: ExtendedHiveStructTypeNameSpec.java    License: 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");
	}
}