Java Code Examples for org.apache.calcite.sql.SqlUtil#isNullLiteral()

The following examples show how to use org.apache.calcite.sql.SqlUtil#isNullLiteral() . 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: RelToSqlConverter.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** @see #dispatch */
public Result visit(Project e) {
  e.getVariablesSet();
  Result x = visitChild(0, e.getInput());
  parseCorrelTable(e, x);
  if (isStar(e.getProjects(), e.getInput().getRowType(), e.getRowType())) {
    return x;
  }
  final Builder builder =
      x.builder(e, Clause.SELECT);
  final List<SqlNode> selectList = new ArrayList<>();
  for (RexNode ref : e.getProjects()) {
    SqlNode sqlExpr = builder.context.toSql(null, ref);
    if (SqlUtil.isNullLiteral(sqlExpr, false)) {
      sqlExpr = castNullType(sqlExpr, e.getRowType().getFieldList().get(selectList.size()));
    }
    addSelect(selectList, sqlExpr, e.getRowType());
  }

  builder.setSelect(new SqlNodeList(selectList, POS));
  return builder.result();
}
 
Example 2
Source File: SqlUserDefinedTableMacro.java    From Bats with Apache License 2.0 5 votes vote down vote up
private static Object getValue(SqlNode right) throws NonLiteralException {
  switch (right.getKind()) {
  case ARRAY_VALUE_CONSTRUCTOR:
    final List<Object> list = new ArrayList<>();
    for (SqlNode o : ((SqlCall) right).getOperandList()) {
      list.add(getValue(o));
    }
    return ImmutableNullableList.copyOf(list);
  case MAP_VALUE_CONSTRUCTOR:
    final ImmutableMap.Builder<Object, Object> builder2 =
        ImmutableMap.builder();
    final List<SqlNode> operands = ((SqlCall) right).getOperandList();
    for (int i = 0; i < operands.size(); i += 2) {
      final SqlNode key = operands.get(i);
      final SqlNode value = operands.get(i + 1);
      builder2.put(getValue(key), getValue(value));
    }
    return builder2.build();
  default:
    if (SqlUtil.isNullLiteral(right, true)) {
      return null;
    }
    if (SqlUtil.isLiteral(right)) {
      return ((SqlLiteral) right).getValue();
    }
    if (right.getKind() == SqlKind.DEFAULT) {
      return null; // currently NULL is the only default value
    }
    throw new NonLiteralException();
  }
}
 
Example 3
Source File: SqlCastFunction.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Makes sure that the number and types of arguments are allowable.
 * Operators (such as "ROW" and "AS") which do not check their arguments can
 * override this method.
 */
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  final SqlNode left = callBinding.operand(0);
  final SqlNode right = callBinding.operand(1);
  if (SqlUtil.isNullLiteral(left, false)
      || left instanceof SqlDynamicParam) {
    return true;
  }
  RelDataType validatedNodeType =
      callBinding.getValidator().getValidatedNodeType(left);
  RelDataType returnType =
      callBinding.getValidator().deriveType(callBinding.getScope(), right);
  if (!SqlTypeUtil.canCastFrom(returnType, validatedNodeType, true)) {
    if (throwOnFailure) {
      throw callBinding.newError(
          RESOURCE.cannotCastValue(validatedNodeType.toString(),
              returnType.toString()));
    }
    return false;
  }
  if (SqlTypeUtil.areCharacterSetsMismatched(
      validatedNodeType,
      returnType)) {
    if (throwOnFailure) {
      // Include full type string to indicate character
      // set mismatch.
      throw callBinding.newError(
          RESOURCE.cannotCastValue(validatedNodeType.getFullTypeString(),
              returnType.getFullTypeString()));
    }
    return false;
  }
  return true;
}
 
Example 4
Source File: LiteralOperandTypeChecker.java    From Bats with Apache License 2.0 5 votes vote down vote up
public boolean checkSingleOperandType(
    SqlCallBinding callBinding,
    SqlNode node,
    int iFormalOperand,
    boolean throwOnFailure) {
  Util.discard(iFormalOperand);

  if (SqlUtil.isNullLiteral(node, true)) {
    if (allowNull) {
      return true;
    }
    if (throwOnFailure) {
      throw callBinding.newError(
          RESOURCE.argumentMustNotBeNull(
              callBinding.getOperator().getName()));
    }
    return false;
  }
  if (!SqlUtil.isLiteral(node) && !SqlUtil.isLiteralChain(node)) {
    if (throwOnFailure) {
      throw callBinding.newError(
          RESOURCE.argumentMustBeLiteral(
              callBinding.getOperator().getName()));
    }
    return false;
  }

  return true;
}
 
Example 5
Source File: FamilyOperandTypeChecker.java    From Bats with Apache License 2.0 5 votes vote down vote up
public boolean checkSingleOperandType(
    SqlCallBinding callBinding,
    SqlNode node,
    int iFormalOperand,
    boolean throwOnFailure) {
  SqlTypeFamily family = families.get(iFormalOperand);
  if (family == SqlTypeFamily.ANY) {
    // no need to check
    return true;
  }
  if (SqlUtil.isNullLiteral(node, false)) {
    if (throwOnFailure) {
      throw callBinding.getValidator().newValidationError(node,
          RESOURCE.nullIllegal());
    } else {
      return false;
    }
  }
  RelDataType type =
      callBinding.getValidator().deriveType(
          callBinding.getScope(),
          node);
  SqlTypeName typeName = type.getSqlTypeName();

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

  if (!family.getTypeNames().contains(typeName)) {
    if (throwOnFailure) {
      throw callBinding.newValidationSignatureError();
    }
    return false;
  }
  return true;
}
 
Example 6
Source File: HiveTableSqlFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
private static Object getValue(SqlNode right) throws NonLiteralException {
	switch (right.getKind()) {
		case ARRAY_VALUE_CONSTRUCTOR:
			final List<Object> list = new ArrayList<>();
			for (SqlNode o : ((SqlCall) right).getOperandList()) {
				list.add(getValue(o));
			}
			return ImmutableNullableList.copyOf(list).toArray();
		case MAP_VALUE_CONSTRUCTOR:
			final Map<Object, Object> map = new HashMap<>();
			final List<SqlNode> operands = ((SqlCall) right).getOperandList();
			for (int i = 0; i < operands.size(); i += 2) {
				final SqlNode key = operands.get(i);
				final SqlNode value = operands.get(i + 1);
				map.put(getValue(key), getValue(value));
			}
			return map;
		default:
			if (SqlUtil.isNullLiteral(right, true)) {
				return null;
			}
			if (SqlUtil.isLiteral(right)) {
				return ((SqlLiteral) right).getValue();
			}
			if (right.getKind() == SqlKind.DEFAULT) {
				return null; // currently NULL is the only default value
			}
			throw new NonLiteralException();
	}
}
 
Example 7
Source File: RepeatFamilyOperandTypeChecker.java    From flink with Apache License 2.0 5 votes vote down vote up
public boolean checkSingleOperandType(
	SqlCallBinding callBinding,
	SqlNode node,
	boolean throwOnFailure) {

	if (SqlUtil.isNullLiteral(node, false)) {
		if (throwOnFailure) {
			throw callBinding.getValidator().newValidationError(node,
				RESOURCE.nullIllegal());
		} else {
			return false;
		}
	}

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

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

	if (!family.getTypeNames().contains(typeName)) {
		if (throwOnFailure) {
			throw callBinding.newValidationSignatureError();
		}
		return false;
	}
	return true;
}
 
Example 8
Source File: DremioArgChecker.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private boolean checkOp(Checker checker, SqlCallBinding callBinding, SqlNode node, int iFormalOperand,
    boolean throwOnFailure) {

  if (SqlUtil.isNullLiteral(node, false)) {
    if (throwOnFailure) {
      throw callBinding.getValidator().newValidationError(node,
          RESOURCE.nullIllegal());
    } else {
      return false;
    }
  }
  RelDataType type = callBinding.getValidator().deriveType(callBinding.getScope(), node);
  SqlTypeName typeName = type.getSqlTypeName();

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

  if (!checker.check(type)) {
    if (throwOnFailure) {
      throw callBinding.newValidationSignatureError();
    }
    return false;
  }
  return true;
}
 
Example 9
Source File: HiveTableSqlFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
private static Object getValue(SqlNode right) throws NonLiteralException {
	switch (right.getKind()) {
		case ARRAY_VALUE_CONSTRUCTOR:
			final List<Object> list = new ArrayList<>();
			for (SqlNode o : ((SqlCall) right).getOperandList()) {
				list.add(getValue(o));
			}
			return ImmutableNullableList.copyOf(list).toArray();
		case MAP_VALUE_CONSTRUCTOR:
			final Map<Object, Object> map = new HashMap<>();
			final List<SqlNode> operands = ((SqlCall) right).getOperandList();
			for (int i = 0; i < operands.size(); i += 2) {
				final SqlNode key = operands.get(i);
				final SqlNode value = operands.get(i + 1);
				map.put(getValue(key), getValue(value));
			}
			return map;
		default:
			if (SqlUtil.isNullLiteral(right, true)) {
				return null;
			}
			if (SqlUtil.isLiteral(right)) {
				return ((SqlLiteral) right).getValue();
			}
			if (right.getKind() == SqlKind.DEFAULT) {
				return null; // currently NULL is the only default value
			}
			throw new NonLiteralException();
	}
}
 
Example 10
Source File: SqlUserDefinedTableMacro.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static Object getValue(SqlNode right) throws NonLiteralException {
  switch (right.getKind()) {
  case ARRAY_VALUE_CONSTRUCTOR:
    final List<Object> list = new ArrayList<>();
    for (SqlNode o : ((SqlCall) right).getOperandList()) {
      list.add(getValue(o));
    }
    return ImmutableNullableList.copyOf(list);
  case MAP_VALUE_CONSTRUCTOR:
    final ImmutableMap.Builder<Object, Object> builder2 =
        ImmutableMap.builder();
    final List<SqlNode> operands = ((SqlCall) right).getOperandList();
    for (int i = 0; i < operands.size(); i += 2) {
      final SqlNode key = operands.get(i);
      final SqlNode value = operands.get(i + 1);
      builder2.put(getValue(key), getValue(value));
    }
    return builder2.build();
  case CAST:
    return getValue(((SqlCall) right).operand(0));
  default:
    if (SqlUtil.isNullLiteral(right, true)) {
      return null;
    }
    if (SqlUtil.isLiteral(right)) {
      return ((SqlLiteral) right).getValue();
    }
    if (right.getKind() == SqlKind.DEFAULT) {
      return null; // currently NULL is the only default value
    }
    throw new NonLiteralException();
  }
}
 
Example 11
Source File: SqlCastFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Makes sure that the number and types of arguments are allowable.
 * Operators (such as "ROW" and "AS") which do not check their arguments can
 * override this method.
 */
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  final SqlNode left = callBinding.operand(0);
  final SqlNode right = callBinding.operand(1);
  if (SqlUtil.isNullLiteral(left, false)
      || left instanceof SqlDynamicParam) {
    return true;
  }
  RelDataType validatedNodeType =
      callBinding.getValidator().getValidatedNodeType(left);
  RelDataType returnType =
      callBinding.getValidator().deriveType(callBinding.getScope(), right);
  if (!SqlTypeUtil.canCastFrom(returnType, validatedNodeType, true)) {
    if (throwOnFailure) {
      throw callBinding.newError(
          RESOURCE.cannotCastValue(validatedNodeType.toString(),
              returnType.toString()));
    }
    return false;
  }
  if (SqlTypeUtil.areCharacterSetsMismatched(
      validatedNodeType,
      returnType)) {
    if (throwOnFailure) {
      // Include full type string to indicate character
      // set mismatch.
      throw callBinding.newError(
          RESOURCE.cannotCastValue(validatedNodeType.getFullTypeString(),
              returnType.getFullTypeString()));
    }
    return false;
  }
  return true;
}
 
Example 12
Source File: LiteralOperandTypeChecker.java    From calcite with Apache License 2.0 5 votes vote down vote up
public boolean checkSingleOperandType(
    SqlCallBinding callBinding,
    SqlNode node,
    int iFormalOperand,
    boolean throwOnFailure) {
  Util.discard(iFormalOperand);

  if (SqlUtil.isNullLiteral(node, true)) {
    if (allowNull) {
      return true;
    }
    if (throwOnFailure) {
      throw callBinding.newError(
          RESOURCE.argumentMustNotBeNull(
              callBinding.getOperator().getName()));
    }
    return false;
  }
  if (!SqlUtil.isLiteral(node) && !SqlUtil.isLiteralChain(node)) {
    if (throwOnFailure) {
      throw callBinding.newError(
          RESOURCE.argumentMustBeLiteral(
              callBinding.getOperator().getName()));
    }
    return false;
  }

  return true;
}
 
Example 13
Source File: FamilyOperandTypeChecker.java    From calcite with Apache License 2.0 5 votes vote down vote up
public boolean checkSingleOperandType(
    SqlCallBinding callBinding,
    SqlNode node,
    int iFormalOperand,
    boolean throwOnFailure) {
  SqlTypeFamily family = families.get(iFormalOperand);
  if (family == SqlTypeFamily.ANY) {
    // no need to check
    return true;
  }
  if (SqlUtil.isNullLiteral(node, false)) {
    if (callBinding.isTypeCoercionEnabled()) {
      return true;
    } else if (throwOnFailure) {
      throw callBinding.getValidator().newValidationError(node,
          RESOURCE.nullIllegal());
    } else {
      return false;
    }
  }
  RelDataType type =
      callBinding.getValidator().deriveType(
          callBinding.getScope(),
          node);
  SqlTypeName typeName = type.getSqlTypeName();

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

  if (!family.getTypeNames().contains(typeName)) {
    if (throwOnFailure) {
      throw callBinding.newValidationSignatureError();
    }
    return false;
  }
  return true;
}
 
Example 14
Source File: CallBindingCallContext.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isArgumentNull(int pos) {
	return SqlUtil.isNullLiteral(adaptedArguments.get(pos), false);
}