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

The following examples show how to use org.apache.calcite.sql.type.SqlTypeName#VARCHAR . 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: TypeInferenceUtils.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Given a {@link SqlTypeName} and nullability, create a RelDataType from the RelDataTypeFactory
 *
 * @param typeFactory RelDataTypeFactory used to create the RelDataType
 * @param sqlTypeName the given SqlTypeName
 * @param isNullable  the nullability of the created RelDataType
 * @return RelDataType Type of call
 */
public static RelDataType createCalciteTypeWithNullability(RelDataTypeFactory typeFactory,
                                                           SqlTypeName sqlTypeName,
                                                           boolean isNullable) {
  RelDataType type;
  if (sqlTypeName.getFamily() == SqlTypeFamily.INTERVAL_DAY_TIME) {
    type = typeFactory.createSqlIntervalType(
        new SqlIntervalQualifier(
            TimeUnit.DAY,
            TimeUnit.MINUTE,
            SqlParserPos.ZERO));
  } else if (sqlTypeName.getFamily() == SqlTypeFamily.INTERVAL_YEAR_MONTH) {
    type = typeFactory.createSqlIntervalType(
        new SqlIntervalQualifier(
            TimeUnit.YEAR,
            TimeUnit.MONTH,
            SqlParserPos.ZERO));
  } else if (sqlTypeName == SqlTypeName.VARCHAR) {
    type = typeFactory.createSqlType(sqlTypeName, Types.MAX_VARCHAR_LENGTH);
  } else {
    type = typeFactory.createSqlType(sqlTypeName);
  }
  return typeFactory.createTypeWithNullability(type, isNullable);
}
 
Example 2
Source File: RexToTestCodeShuttle.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public String visitLiteral(RexLiteral literal) {
  RelDataType type = literal.getType();

  if (type.getSqlTypeName() == SqlTypeName.BOOLEAN) {
    if (literal.isNull()) {
      return "nullBool";
    }
    return literal.toString() + "Literal";
  }
  if (type.getSqlTypeName() == SqlTypeName.INTEGER) {
    if (literal.isNull()) {
      return "nullInt";
    }
    return "literal(" + literal.getValue() + ")";
  }
  if (type.getSqlTypeName() == SqlTypeName.VARCHAR) {
    if (literal.isNull()) {
      return "nullVarchar";
    }
  }
  return "/*" + literal.getTypeName().getName() + "*/" + literal.toString();
}
 
Example 3
Source File: CalciteConvertors.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
public static RelDataType getRelDataType(final List<SimpleColumnInfo> columnInfos, final RelDataTypeFactory factory) {
    final RelDataTypeFactory.Builder builder = new RelDataTypeFactory.Builder(factory);
    for (SimpleColumnInfo columnInfo : columnInfos) {
        final JDBCType columnType = columnInfo.getJdbcType();
        final RelDataType type;
        if (columnType == JDBCType.VARCHAR) {
            type = factory.createTypeWithCharsetAndCollation(
                    factory.createSqlType(SqlTypeName.VARCHAR),
                    Charset.defaultCharset(),
                    SqlCollation.IMPLICIT);
        } else if (columnType == JDBCType.LONGVARBINARY) {
            type = factory.createSqlType(SqlTypeName.VARBINARY);
        } else {
            SqlTypeName sqlTypeName = SqlTypeName.getNameForJdbcType(columnType.getVendorTypeNumber());
            if (sqlTypeName == null) {
                sqlTypeName = SqlTypeName.VARCHAR;
            }
            type = factory.createSqlType(sqlTypeName);
        }
        builder.add(columnInfo.getColumnName(), factory.createTypeWithNullability(type, columnInfo.isNullable()));
    }
    return builder.build();
}
 
Example 4
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 5
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 6
Source File: TypeInferenceUtils.java    From dremio-oss 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.VARCHAR;

  for(int i = 0; i < opBinding.getOperandCount(); ++i) {
    if(opBinding.getOperandType(i).isNullable()) {
      return createCalciteTypeWithNullability(factory, sqlTypeName, true, null);
    }
  }

  return createCalciteTypeWithNullability(factory, sqlTypeName, false, null);
}
 
Example 7
Source File: RexBuilderTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RelDataType createTypeWithNullability(
    final RelDataType type,
    final boolean nullable) {
  if (type.getSqlTypeName() == SqlTypeName.VARCHAR) {
    return new BasicSqlType(this.typeSystem, type.getSqlTypeName(),
        PRECISION);
  }
  return super.createTypeWithNullability(type, nullable);
}
 
Example 8
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 9
Source File: Checker.java    From samza with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) {
  if (!udfMetadataOptional.isPresent() || udfMetadataOptional.get().isDisableArgCheck() || !throwOnFailure) {
    return true;
  } else {
    // 1. Generate a mapping from argument index to parsed calcite-type for the sql UDF.
    Map<Integer, RelDataType> argumentIndexToCalciteType = IntStream.range(0, callBinding.getOperandCount())
        .boxed()
        .collect(Collectors.toMap(operandIndex -> operandIndex, callBinding::getOperandType, (a, b) -> b));

    UdfMetadata udfMetadata = udfMetadataOptional.get();
    List<SamzaSqlFieldType> udfArguments = udfMetadata.getArguments();

    // 2. Compare the argument type in samza-sql UDF against the RelType generated by the
    // calcite parser engine.
    for (int udfArgumentIndex = 0; udfArgumentIndex < udfArguments.size(); ++udfArgumentIndex) {
      SamzaSqlFieldType udfArgumentType = udfArguments.get(udfArgumentIndex);
      SqlTypeName udfArgumentAsSqlType = toCalciteSqlType(udfArgumentType);
      RelDataType parsedSqlArgType = argumentIndexToCalciteType.get(udfArgumentIndex);

      // 3(a). Special-case, where static strings used as method-arguments in udf-methods during invocation are parsed as the Char type by calcite.
      if (parsedSqlArgType.getSqlTypeName() == SqlTypeName.CHAR && udfArgumentAsSqlType == SqlTypeName.VARCHAR) {
        return true;
      } else if (!Objects.equals(parsedSqlArgType.getSqlTypeName(), udfArgumentAsSqlType)
              && !ANY_SQL_TYPE_NAMES.contains(parsedSqlArgType.getSqlTypeName()) && hasOneUdfMethod(udfMetadata)) {
        // 3(b). Throw up and fail on mismatch between the SamzaSqlType and CalciteType for any argument.
        String msg = String.format("Type mismatch in udf class: %s at argument index: %d." +
                        "Expected type: %s, actual type: %s.", udfMetadata.getName(),
                udfArgumentIndex, parsedSqlArgType.getSqlTypeName(), udfArgumentAsSqlType);
        LOG.error(msg);
        throw new SamzaSqlValidatorException(msg);
      }
    }
  }
  // 4. The SamzaSqlFieldType and CalciteType has matched for all the arguments in the UDF.
  return true;
}
 
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: 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 12
Source File: TypeInferenceUtils.java    From dremio-oss 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.VARCHAR;

  for(int i = 0; i < opBinding.getOperandCount(); ++i) {
    if(opBinding.getOperandType(i).isNullable()) {
      return createCalciteTypeWithNullability(factory, sqlTypeName, true, null);
    }
  }

  return createCalciteTypeWithNullability(factory, sqlTypeName, false, 65536);
}
 
Example 13
Source File: SqlDatePartOperator.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkSingleOperandType(SqlCallBinding callBinding, SqlNode node,
    int iFormalOperand, boolean throwOnFailure) {

  // check that the input is a literal.
  if(!super.checkSingleOperandType(callBinding, node, iFormalOperand, throwOnFailure)) {
    return false;
  }

  final RelDataType type = callBinding.getValidator().deriveType(callBinding.getScope(), node);
  final SqlTypeName typeName = type.getSqlTypeName();

  // Pass type checking for operators if it's of type 'ANY'.
  if (typeName.getFamily() == SqlTypeFamily.ANY) {
    return true;
  }

  if(!(typeName == SqlTypeName.CHAR || typeName == SqlTypeName.VARCHAR)) {
    if(throwOnFailure) {
      throw callBinding.newValidationSignatureError();
    }
    return false;
  }

  final SqlLiteral literal = (SqlLiteral) node;
  final String value = ((NlsString)literal.getValue()).getValue();
  if(validStrings.contains(value.toLowerCase())) {
    return true;
  }

  if(throwOnFailure) {
    throw callBinding.newValidationSignatureError();
    //throw new SqlValidatorException(String.format("DATE_PART function only accepts the following values for a date type: %s.", Joiner.on(", ").join(validStrings)), null);
  }

  return false;
}
 
Example 14
Source File: TableEnv.java    From marble with Apache License 2.0 5 votes vote down vote up
protected SqlTypeName getSqlTypeNameForJdbcType(int jdbcType) {
  //FIX Calcite jdbc type converting bug
  SqlTypeName typeName = SqlTypeName.getNameForJdbcType(jdbcType);
  if (jdbcType == Types.LONGVARCHAR) {
    typeName = SqlTypeName.VARCHAR;
  }
  if (jdbcType == Types.SMALLINT || jdbcType == Types.TINYINT) {
    //the type of jdbc value is INTEGER when jdbcType is SMALLINT or TINYINT
    typeName = SqlTypeName.INTEGER;
  }
  return typeName;
}
 
Example 15
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 16
Source File: SqlVarCharStringLiteral.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private SqlVarCharStringLiteral(NlsString val, SqlParserPos pos) {
  super(val, SqlTypeName.VARCHAR, pos);
}
 
Example 17
Source File: DruidTable.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public boolean isRolledUp(String column) {
  // The only rolled up columns we care about are Complex Metrics (aka sketches).
  // But we also need to check if this column name is a dimension
  return complexMetrics.get(column) != null
          && allFields.get(column) != SqlTypeName.VARCHAR;
}
 
Example 18
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 2 votes vote down vote up
/**
 * Checks if given type is string scalar type.
 *
 * @param sqlTypeName Calcite's sql type name
 * @return true if given type is string scalar type
 */
public static boolean isScalarStringType(final SqlTypeName sqlTypeName) {
  return sqlTypeName == SqlTypeName.VARCHAR || sqlTypeName == SqlTypeName.CHAR;
}