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

The following examples show how to use org.apache.calcite.sql.type.SqlTypeName#VARBINARY . 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 5 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  final SqlTypeName type = SqlTypeName.VARBINARY;

  return createCalciteTypeWithNullability(
      factory, type, opBinding.getOperandType(0).isNullable());
}
 
Example 2
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 type = SqlTypeName.VARBINARY;

  return createCalciteTypeWithNullability(factory, type, opBinding.getOperandType(0).isNullable(), null);
}
 
Example 3
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 4
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  if (functions.isEmpty()) {
    return factory.createTypeWithNullability(
        factory.createSqlType(SqlTypeName.ANY),
        true);
  }

  // The following logic is just a safe play:
  // Even if any of the input arguments has ANY type,
  // it "might" still be possible to determine the return type based on other non-ANY types
  for (RelDataType type : opBinding.collectOperandTypes()) {
    if (getDrillTypeFromCalciteType(type) == TypeProtos.MinorType.LATE) {
      // This code for boolean output type is added for addressing DRILL-1729
      // In summary, if we have a boolean output function in the WHERE-CLAUSE,
      // this logic can validate and execute user queries seamlessly
      boolean allBooleanOutput = true;
      boolean isNullable = false;
      for (DrillFuncHolder function : functions) {
        if (function.getReturnType().getMinorType() != TypeProtos.MinorType.BIT) {
          allBooleanOutput = false;
          break;
        }
        if (function.getReturnType().getMode() == TypeProtos.DataMode.OPTIONAL
            || function.getNullHandling() == FunctionTemplate.NullHandling.NULL_IF_NULL) {
          isNullable = true;
        }
      }

      if (allBooleanOutput) {
        return factory.createTypeWithNullability(
            factory.createSqlType(SqlTypeName.BOOLEAN), isNullable);
      } else {
        return factory.createTypeWithNullability(
            factory.createSqlType(SqlTypeName.ANY),
            true);
      }
    } else if (SET_SCALE_DECIMAL_FUNCTIONS.contains(opBinding.getOperator().getName())
        && getDrillTypeFromCalciteType(type) == TypeProtos.MinorType.VARDECIMAL) {
      return factory.createTypeWithNullability(
          factory.createSqlType(SqlTypeName.ANY),
          true);
    }
  }

  final FunctionCall functionCall = convertSqlOperatorBindingToFunctionCall(opBinding);
  final DrillFuncHolder func = resolveDrillFuncHolder(opBinding, functions, functionCall);

  final RelDataType returnType = getReturnType(opBinding,
      func.getReturnType(functionCall.args), func.getNullHandling());

  return returnType.getSqlTypeName() == SqlTypeName.VARBINARY
      ? createCalciteTypeWithNullability(factory, SqlTypeName.ANY, returnType.isNullable())
          : returnType;
}
 
Example 5
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();
  if (functions.isEmpty()) {
    return factory.createTypeWithNullability(
        factory.createSqlType(SqlTypeName.ANY),
        true);
  }

  // The following logic is just a safe play:
  // Even if any of the input arguments has ANY type,
  // it "might" still be possible to determine the return type based on other non-ANY types
  for (RelDataType type : opBinding.collectOperandTypes()) {
    if (getMinorTypeFromCalciteType(type) == TypeProtos.MinorType.LATE) {
      // This code for boolean output type is added for addressing DRILL-1729
      // In summary, if we have a boolean output function in the WHERE-CLAUSE,
      // this logic can validate and execute user queries seamlessly
      boolean allBooleanOutput = true;
      for (AbstractFunctionHolder function : functions) {
        if (!function.isReturnTypeIndependent() || function.getReturnType(null).toMinorType() != TypeProtos.MinorType.BIT) {
          allBooleanOutput = false;
          break;
        }
      }

      if(allBooleanOutput) {
        return factory.createTypeWithNullability(
            factory.createSqlType(SqlTypeName.BOOLEAN), true);
      } else {
        return factory.createTypeWithNullability(
            factory.createSqlType(SqlTypeName.ANY),
            true);
      }
    }
  }

  final AbstractFunctionHolder func = resolveFunctionHolder(opBinding, functions, isDecimalV2Enabled);
  final RelDataType returnType = getReturnType(opBinding, func);
  return returnType.getSqlTypeName() == SqlTypeName.VARBINARY
      ? createCalciteTypeWithNullability(factory, SqlTypeName.ANY, returnType.isNullable(), null)
          : returnType;
}
 
Example 6
Source File: HiveDDLUtils.java    From flink with Apache License 2.0 4 votes vote down vote up
private static SqlTypeNameSpec convertDataTypes(SqlTypeNameSpec nameSpec) throws ParseException {
	if (nameSpec instanceof SqlBasicTypeNameSpec) {
		SqlBasicTypeNameSpec basicNameSpec = (SqlBasicTypeNameSpec) nameSpec;
		if (basicNameSpec.getTypeName().getSimple().equalsIgnoreCase(SqlTypeName.TIMESTAMP.name())) {
			if (basicNameSpec.getPrecision() < 0) {
				nameSpec = new SqlBasicTypeNameSpec(SqlTypeName.TIMESTAMP, 9, basicNameSpec.getScale(),
						basicNameSpec.getCharSetName(), basicNameSpec.getParserPos());
			}
		} else if (basicNameSpec.getTypeName().getSimple().equalsIgnoreCase(SqlTypeName.BINARY.name())) {
			if (basicNameSpec.getPrecision() < 0) {
				nameSpec = new SqlBasicTypeNameSpec(SqlTypeName.VARBINARY, Integer.MAX_VALUE, basicNameSpec.getScale(),
						basicNameSpec.getCharSetName(), basicNameSpec.getParserPos());
			}
		} else if (basicNameSpec.getTypeName().getSimple().equalsIgnoreCase(SqlTypeName.VARCHAR.name())) {
			if (basicNameSpec.getPrecision() < 0) {
				throw new ParseException("VARCHAR precision is mandatory");
			}
		}
	} else if (nameSpec instanceof ExtendedSqlCollectionTypeNameSpec) {
		ExtendedSqlCollectionTypeNameSpec collectionNameSpec = (ExtendedSqlCollectionTypeNameSpec) nameSpec;
		SqlTypeNameSpec elementNameSpec = collectionNameSpec.getElementTypeName();
		SqlTypeNameSpec convertedElementNameSpec = convertDataTypes(elementNameSpec);
		if (convertedElementNameSpec != elementNameSpec) {
			nameSpec = new ExtendedSqlCollectionTypeNameSpec(convertedElementNameSpec,
					collectionNameSpec.elementNullable(), collectionNameSpec.getCollectionTypeName(),
					collectionNameSpec.unparseAsStandard(), collectionNameSpec.getParserPos());
		}
	} else if (nameSpec instanceof SqlMapTypeNameSpec) {
		SqlMapTypeNameSpec mapNameSpec = (SqlMapTypeNameSpec) nameSpec;
		SqlDataTypeSpec keyTypeSpec = mapNameSpec.getKeyType();
		SqlDataTypeSpec valTypeSpec = mapNameSpec.getValType();
		SqlDataTypeSpec convertedKeyTypeSpec = convertDataTypes(keyTypeSpec);
		SqlDataTypeSpec convertedValTypeSpec = convertDataTypes(valTypeSpec);
		if (keyTypeSpec != convertedKeyTypeSpec || valTypeSpec != convertedValTypeSpec) {
			nameSpec = new SqlMapTypeNameSpec(convertedKeyTypeSpec, convertedValTypeSpec, nameSpec.getParserPos());
		}
	} else if (nameSpec instanceof ExtendedSqlRowTypeNameSpec) {
		ExtendedSqlRowTypeNameSpec rowNameSpec = (ExtendedSqlRowTypeNameSpec) nameSpec;
		List<SqlDataTypeSpec> fieldTypeSpecs = rowNameSpec.getFieldTypes();
		List<SqlDataTypeSpec> convertedFieldTypeSpecs = new ArrayList<>(fieldTypeSpecs.size());
		boolean updated = false;
		for (SqlDataTypeSpec fieldTypeSpec : fieldTypeSpecs) {
			SqlDataTypeSpec convertedFieldTypeSpec = convertDataTypes(fieldTypeSpec);
			if (fieldTypeSpec != convertedFieldTypeSpec) {
				updated = true;
			}
			convertedFieldTypeSpecs.add(convertedFieldTypeSpec);
		}
		if (updated) {
			nameSpec = new ExtendedSqlRowTypeNameSpec(nameSpec.getParserPos(), rowNameSpec.getFieldNames(),
					convertedFieldTypeSpecs, rowNameSpec.getComments(), rowNameSpec.unparseAsStandard());
		}
	}
	return nameSpec;
}