Java Code Examples for org.apache.calcite.sql.type.SqlTypeName#BIGINT

The following examples show how to use org.apache.calcite.sql.type.SqlTypeName#BIGINT . 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: JethroDataSqlDialect.java    From Bats with Apache License 2.0 6 votes vote down vote up
private SqlTypeName parse(String strType) {
  switch (strType.toLowerCase(Locale.ROOT)) {
  case "bigint":
  case "long":
    return SqlTypeName.BIGINT;
  case "integer":
  case "int":
    return SqlTypeName.INTEGER;
  case "double":
    return SqlTypeName.DOUBLE;
  case "float":
    return SqlTypeName.FLOAT;
  case "string":
    return SqlTypeName.VARCHAR;
  case "timestamp":
    return SqlTypeName.TIMESTAMP;
  default:
    return SqlTypeName.ANY;
  }
}
 
Example 2
Source File: SqlNumericLiteral.java    From Bats with Apache License 2.0 6 votes vote down vote up
public RelDataType createSqlType(RelDataTypeFactory typeFactory) {
  if (isExact) {
    int scaleValue = scale.intValue();
    if (0 == scaleValue) {
      BigDecimal bd = (BigDecimal) value;
      SqlTypeName result;
      long l = bd.longValue();
      if ((l >= Integer.MIN_VALUE) && (l <= Integer.MAX_VALUE)) {
        result = SqlTypeName.INTEGER;
      } else {
        result = SqlTypeName.BIGINT;
      }
      return typeFactory.createSqlType(result);
    }

    // else we have a decimal
    return typeFactory.createSqlType(
        SqlTypeName.DECIMAL,
        prec.intValue(),
        scaleValue);
  }

  // else we have a a float, real or double.  make them all double for
  // now.
  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
}
 
Example 3
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * For Extract and date_part functions, infer the return types based on timeUnit
 */
public static SqlTypeName getSqlTypeNameForTimeUnit(String timeUnitStr) {
  TimeUnit timeUnit = TimeUnit.valueOf(timeUnitStr);
  switch (timeUnit) {
    case YEAR:
    case MONTH:
    case DAY:
    case HOUR:
    case MINUTE:
      return SqlTypeName.BIGINT;
    case SECOND:
      return SqlTypeName.DOUBLE;
    default:
      throw UserException
          .functionError()
          .message("extract function supports the following time units: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND")
          .build(logger);
  }
}
 
Example 4
Source File: TypeInferenceUtils.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
/**
 * For Extract and date_part functions, infer the return types based on timeUnit
 */
public static SqlTypeName getSqlTypeNameForTimeUnit(String timeUnit) {
  switch (timeUnit.toUpperCase()){
    case "YEAR":
    case "MONTH":
    case "DAY":
    case "HOUR":
    case "MINUTE":
    case "SECOND":
    case "CENTURY":
    case "DECADE":
    case "DOW":
    case "DOY":
    case "MILLENNIUM":
    case "QUARTER":
    case "WEEK":
    case "EPOCH":
      return SqlTypeName.BIGINT;
    default:
      throw UserException
          .functionError()
          .message("extract function supports the following time units: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND")
          .build(logger);
  }
}
 
Example 5
Source File: JethroDataSqlDialect.java    From calcite with Apache License 2.0 6 votes vote down vote up
private SqlTypeName parse(String strType) {
  switch (strType.toLowerCase(Locale.ROOT)) {
  case "bigint":
  case "long":
    return SqlTypeName.BIGINT;
  case "integer":
  case "int":
    return SqlTypeName.INTEGER;
  case "double":
    return SqlTypeName.DOUBLE;
  case "float":
    return SqlTypeName.FLOAT;
  case "string":
    return SqlTypeName.VARCHAR;
  case "timestamp":
    return SqlTypeName.TIMESTAMP;
  default:
    return SqlTypeName.ANY;
  }
}
 
Example 6
Source File: SqlNumericLiteral.java    From calcite with Apache License 2.0 6 votes vote down vote up
public RelDataType createSqlType(RelDataTypeFactory typeFactory) {
  if (isExact) {
    int scaleValue = scale.intValue();
    if (0 == scaleValue) {
      BigDecimal bd = (BigDecimal) value;
      SqlTypeName result;
      long l = bd.longValue();
      if ((l >= Integer.MIN_VALUE) && (l <= Integer.MAX_VALUE)) {
        result = SqlTypeName.INTEGER;
      } else {
        result = SqlTypeName.BIGINT;
      }
      return typeFactory.createSqlType(result);
    }

    // else we have a decimal
    return typeFactory.createSqlType(
        SqlTypeName.DECIMAL,
        prec.intValue(),
        scaleValue);
  }

  // else we have a a float, real or double.  make them all double for
  // now.
  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
}
 
Example 7
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  final SqlTypeName type = SqlTypeName.BIGINT;
  return createCalciteTypeWithNullability(
      factory,
      type,
      false);
}
 
Example 8
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  final SqlTypeName sqlTypeName = SqlTypeName.BIGINT;

  // We need to check only the first argument because
  // the second one is used to represent encoding type
  final boolean isNullable = opBinding.getOperandType(0).isNullable();
  return createCalciteTypeWithNullability(
      factory,
      sqlTypeName,
      isNullable);
}
 
Example 9
Source File: JoinTranslator.java    From samza with Apache License 2.0 5 votes vote down vote up
private void validateJoinKeyType(RexInputRef ref) {
  SqlTypeName sqlTypeName = ref.getType().getSqlTypeName();

  // Primitive types and ANY (for the record key) are supported in the key
  if (sqlTypeName != SqlTypeName.BOOLEAN && sqlTypeName != SqlTypeName.TINYINT && sqlTypeName != SqlTypeName.SMALLINT
      && sqlTypeName != SqlTypeName.INTEGER && sqlTypeName != SqlTypeName.CHAR && sqlTypeName != SqlTypeName.BIGINT
      && sqlTypeName != SqlTypeName.VARCHAR && sqlTypeName != SqlTypeName.DOUBLE && sqlTypeName != SqlTypeName.FLOAT
      && sqlTypeName != SqlTypeName.ANY && sqlTypeName != SqlTypeName.OTHER) {
    log.error("Unsupported key type " + sqlTypeName + " used in join condition.");
    throw new SamzaException("Unsupported key type used in join condition.");
  }
}
 
Example 10
Source File: SamzaSqlValidator.java    From samza with Apache License 2.0 5 votes vote down vote up
private boolean compareFieldTypes(RelDataType outputFieldType, SqlFieldSchema sqlFieldSchema,
    RelDataType selectQueryFieldType, RelSchemaProvider outputRelSchemaProvider) {

  SqlTypeName outputSqlType = outputFieldType.getSqlTypeName();
  SqlTypeName projectSqlType = selectQueryFieldType.getSqlTypeName();

  if (projectSqlType == SqlTypeName.ANY || outputSqlType == SqlTypeName.ANY) {
    return true;
  } else if (outputSqlType != SqlTypeName.ROW && outputSqlType == projectSqlType) {
    return true;
  }

  switch (outputSqlType) {
    case CHAR:
      return projectSqlType == SqlTypeName.VARCHAR;
    case VARCHAR:
      return projectSqlType == SqlTypeName.CHAR;
    case BIGINT:
      return projectSqlType == SqlTypeName.INTEGER;
    case INTEGER:
      return projectSqlType == SqlTypeName.BIGINT;
    case FLOAT:
      return projectSqlType == SqlTypeName.DOUBLE;
    case DOUBLE:
      return projectSqlType == SqlTypeName.FLOAT;
    case ROW:
      try {
        validateOutputRecords(sqlFieldSchema.getRowSchema(), (RelRecordType) outputFieldType,
            (RelRecordType) selectQueryFieldType, outputRelSchemaProvider);
      } catch (SamzaSqlValidatorException e) {
        LOG.error("A field in select query does not match with the output schema.", e);
        return false;
      }
      return true;
    default:
      return false;
  }
}
 
Example 11
Source File: Checker.java    From samza with Apache License 2.0 5 votes vote down vote up
/**
 * Converts the {@link SamzaSqlFieldType} to the calcite {@link SqlTypeName}.
 * @param samzaSqlFieldType the samza sql field type.
 * @return the converted calcite SqlTypeName.
 */
@VisibleForTesting
static SqlTypeName toCalciteSqlType(SamzaSqlFieldType samzaSqlFieldType) {
  switch (samzaSqlFieldType) {
    case ANY:
    case ROW:
      return SqlTypeName.ANY;
    case MAP:
      return SqlTypeName.MAP;
    case ARRAY:
      return SqlTypeName.ARRAY;
    case REAL:
      return SqlTypeName.REAL;
    case DOUBLE:
      return SqlTypeName.DOUBLE;
    case STRING:
      return SqlTypeName.VARCHAR;
    case INT16:
    case INT32:
      return SqlTypeName.INTEGER;
    case FLOAT:
      return SqlTypeName.FLOAT;
    case INT64:
      return SqlTypeName.BIGINT;
    case BOOLEAN:
      return SqlTypeName.BOOLEAN;
    case BYTES:
      return SqlTypeName.VARBINARY;
    default:
      String msg = String.format("Field Type %s is not supported", samzaSqlFieldType);
      LOG.error(msg);
      throw new SamzaException(msg);
  }
}
 
Example 12
Source File: StandardConvertletTable.java    From Bats with Apache License 2.0 4 votes vote down vote up
public RexNode convertCall(SqlRexContext cx, SqlCall call) {
  // TIMESTAMPDIFF(unit, t1, t2)
  //    => (t2 - t1) UNIT
  final RexBuilder rexBuilder = cx.getRexBuilder();
  final SqlLiteral unitLiteral = call.operand(0);
  TimeUnit unit = unitLiteral.symbolValue(TimeUnit.class);
  BigDecimal multiplier = BigDecimal.ONE;
  BigDecimal divider = BigDecimal.ONE;
  SqlTypeName sqlTypeName = unit == TimeUnit.NANOSECOND
      ? SqlTypeName.BIGINT
      : SqlTypeName.INTEGER;
  switch (unit) {
  case MICROSECOND:
  case MILLISECOND:
  case NANOSECOND:
  case WEEK:
    multiplier = BigDecimal.valueOf(DateTimeUtils.MILLIS_PER_SECOND);
    divider = unit.multiplier;
    unit = TimeUnit.SECOND;
    break;
  case QUARTER:
    divider = unit.multiplier;
    unit = TimeUnit.MONTH;
    break;
  }
  final SqlIntervalQualifier qualifier =
      new SqlIntervalQualifier(unit, null, SqlParserPos.ZERO);
  final RexNode op2 = cx.convertExpression(call.operand(2));
  final RexNode op1 = cx.convertExpression(call.operand(1));
  final RelDataType intervalType =
      cx.getTypeFactory().createTypeWithNullability(
          cx.getTypeFactory().createSqlIntervalType(qualifier),
          op1.getType().isNullable() || op2.getType().isNullable());
  final RexCall rexCall = (RexCall) rexBuilder.makeCall(
      intervalType, SqlStdOperatorTable.MINUS_DATE,
      ImmutableList.of(op2, op1));
  final RelDataType intType =
      cx.getTypeFactory().createTypeWithNullability(
          cx.getTypeFactory().createSqlType(sqlTypeName),
          SqlTypeUtil.containsNullable(rexCall.getType()));
  RexNode e = rexBuilder.makeCast(intType, rexCall);
  return rexBuilder.multiplyDivide(e, multiplier, divider);
}
 
Example 13
Source File: MycatCalciteMySqlNodeVisitor.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
public boolean visit(SQLCastExpr x) {
    SqlLiteral functionQualifier = null;

    SqlNode sqlNode = convertToSqlNode(x.getExpr());

    SQLDataType dataType = x.getDataType();
    String typeName = dataType.getName().toUpperCase();
    if (dataType.nameHashCode64() == FnvHash.Constants.INT) {
        typeName = "INTEGER";
    } else if (dataType.nameHashCode64() == FnvHash.Constants.NUMERIC) {
        typeName = "DECIMAL";
    }

    SqlIdentifier dataTypeNode = (SqlIdentifier)convertToSqlNode(
            new SQLIdentifierExpr(typeName));

    int scale = -1;
    int precision = -1;

    List<SQLExpr> arguments = dataType.getArguments();
    if (arguments != null && !arguments.isEmpty()) {
        scale = ((SQLNumericLiteralExpr)arguments.get(0)).getNumber().intValue();
        if (arguments.size() > 1) {
            precision = ((SQLNumericLiteralExpr) arguments.get(1)).getNumber().intValue();
        }
    }
    SqlNode sqlDataTypeSpec;
    if (typeName.equalsIgnoreCase("SIGNED")){
        sqlDataTypeSpec =  new SqlDataTypeSpec( new SqlBasicTypeNameSpec(SqlTypeName.BIGINT, precision, scale, null, SqlParserPos.ZERO),SqlParserPos.ZERO);
    }else {
        SqlTypeName sqlTypeName = SqlTypeName.valueOf(typeName);
        SqlBasicTypeNameSpec sqlBasicTypeNameSpec = new SqlBasicTypeNameSpec(sqlTypeName, precision, scale, null, SqlParserPos.ZERO);

        sqlDataTypeSpec = new SqlDataTypeSpec(sqlBasicTypeNameSpec, SqlParserPos.ZERO);
    }

    SqlOperator sqlOperator = new SqlCastFunction();

    this.sqlNode = new CalciteSqlBasicCall(sqlOperator, new SqlNode[]{ sqlNode, sqlDataTypeSpec}, SqlParserPos.ZERO,
                                           false, functionQualifier);
    return false;
}
 
Example 14
Source File: TypeInferenceUtils.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  SqlTypeName typeToCastTo = null;
  if (opBinding instanceof SqlCallBinding) {
    SqlCallBinding sqlCallBinding = (SqlCallBinding) opBinding;
    if (sqlCallBinding.operand(1).getKind() == SqlKind.LITERAL) {
      String type = null;
      try {
        SqlLiteral sqlLiteral = (SqlLiteral) sqlCallBinding.operand(1);
        type = ((NlsString) sqlLiteral.getValue()).getValue();
        switch(type) {
          case "JSON":
            typeToCastTo = SqlTypeName.ANY;
            break;
          case "UTF8":
          case "UTF16":
            typeToCastTo = SqlTypeName.VARCHAR;
            break;
          case "BOOLEAN_BYTE":
            typeToCastTo = SqlTypeName.BOOLEAN;
            break;
          case "TINYINT_BE":
          case "TINYINT":
            typeToCastTo = SqlTypeName.TINYINT;
            break;
          case "SMALLINT_BE":
          case "SMALLINT":
            typeToCastTo = SqlTypeName.SMALLINT;
            break;
          case "INT_BE":
          case "INT":
          case "INT_HADOOPV":
            typeToCastTo = SqlTypeName.INTEGER;
            break;
          case "BIGINT_BE":
          case "BIGINT":
          case "BIGINT_HADOOPV":
            typeToCastTo = SqlTypeName.BIGINT;
            break;
          case "FLOAT":
            typeToCastTo = SqlTypeName.FLOAT;
            break;
          case "DOUBLE":
            typeToCastTo = SqlTypeName.DOUBLE;
            break;
          case "DATE_EPOCH_BE":
          case "DATE_EPOCH":
            typeToCastTo = SqlTypeName.DATE;
            break;
          case "TIME_EPOCH_BE":
          case "TIME_EPOCH":
            typeToCastTo = SqlTypeName.TIME;
            break;
          case "TIMESTAMP_EPOCH":
          case "TIMESTAMP_IMPALA":
            typeToCastTo = SqlTypeName.TIMESTAMP;
            break;
          default:
            typeToCastTo = SqlTypeName.ANY;
            break;
        }
      } catch (final ClassCastException e) {
        logger.debug("Failed to parse string for convert_from()");
      }
    }
  }

  if (typeToCastTo == null) {
    typeToCastTo = SqlTypeName.ANY;
  }
  return factory.createTypeWithNullability(
      factory.createSqlType(typeToCastTo),
      true);
}
 
Example 15
Source File: StandardConvertletTable.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RexNode convertCall(SqlRexContext cx, SqlCall call) {
  // TIMESTAMPDIFF(unit, t1, t2)
  //    => (t2 - t1) UNIT
  final RexBuilder rexBuilder = cx.getRexBuilder();
  final SqlLiteral unitLiteral = call.operand(0);
  TimeUnit unit = unitLiteral.symbolValue(TimeUnit.class);
  BigDecimal multiplier = BigDecimal.ONE;
  BigDecimal divider = BigDecimal.ONE;
  SqlTypeName sqlTypeName = unit == TimeUnit.NANOSECOND
      ? SqlTypeName.BIGINT
      : SqlTypeName.INTEGER;
  switch (unit) {
  case MICROSECOND:
  case MILLISECOND:
  case NANOSECOND:
  case WEEK:
    multiplier = BigDecimal.valueOf(DateTimeUtils.MILLIS_PER_SECOND);
    divider = unit.multiplier;
    unit = TimeUnit.SECOND;
    break;
  case QUARTER:
    divider = unit.multiplier;
    unit = TimeUnit.MONTH;
    break;
  }
  final SqlIntervalQualifier qualifier =
      new SqlIntervalQualifier(unit, null, SqlParserPos.ZERO);
  final RexNode op2 = cx.convertExpression(call.operand(2));
  final RexNode op1 = cx.convertExpression(call.operand(1));
  final RelDataType intervalType =
      cx.getTypeFactory().createTypeWithNullability(
          cx.getTypeFactory().createSqlIntervalType(qualifier),
          op1.getType().isNullable() || op2.getType().isNullable());
  final RexCall rexCall = (RexCall) rexBuilder.makeCall(
      intervalType, SqlStdOperatorTable.MINUS_DATE,
      ImmutableList.of(op2, op1));
  final RelDataType intType =
      cx.getTypeFactory().createTypeWithNullability(
          cx.getTypeFactory().createSqlType(sqlTypeName),
          SqlTypeUtil.containsNullable(rexCall.getType()));
  RexNode e = rexBuilder.makeCast(intType, rexCall);
  return rexBuilder.multiplyDivide(e, multiplier, divider);
}