org.apache.calcite.sql.SqlDynamicParam Java Examples

The following examples show how to use org.apache.calcite.sql.SqlDynamicParam. 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: MysqlSideFunction.java    From alchemy with Apache License 2.0 6 votes vote down vote up
private List<SqlBasicCall> createConditionNodes(List<String> conditions, Alias alias) {
    SqlBinaryOperator equal = new SqlBinaryOperator("=", SqlKind.EQUALS, 30, true, ReturnTypes.BOOLEAN_NULLABLE,
        InferTypes.FIRST_KNOWN, OperandTypes.COMPARABLE_UNORDERED_COMPARABLE_UNORDERED);
    List<SqlBasicCall> nodes = new ArrayList<>(conditions.size());
    int num = 0;
    for (String condition : conditions) {
        List<String> fields = new ArrayList<>(2);
        fields.add(alias.getAlias());
        fields.add(condition);
        SqlIdentifier leftIdentifier = new SqlIdentifier(fields, new SqlParserPos(0, 0));
        SqlDynamicParam sqlDynamicParam = new SqlDynamicParam(num++, new SqlParserPos(0, 0));
        SqlNode[] sqlNodes = new SqlNode[2];
        sqlNodes[0] = leftIdentifier;
        sqlNodes[1] = sqlDynamicParam;
        SqlBasicCall andEqual = new SqlBasicCall(equal, sqlNodes, new SqlParserPos(0, 0));
        nodes.add(andEqual);
    }
    return nodes;
}
 
Example #2
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public RelDataType getParameterRowType(SqlNode sqlQuery) {
	// NOTE: We assume that bind variables occur in depth-first tree
	// traversal in the same order that they occurred in the SQL text.
	final List<RelDataType> types = new ArrayList<>();
	// NOTE: but parameters on fetch/offset would be counted twice
	// as they are counted in the SqlOrderBy call and the inner SqlSelect call
	final Set<SqlNode> alreadyVisited = new HashSet<>();
	sqlQuery.accept(
		new SqlShuttle() {

			@Override public SqlNode visit(SqlDynamicParam param) {
				if (alreadyVisited.add(param)) {
					RelDataType type = getValidatedNodeType(param);
					types.add(type);
				}
				return param;
			}
		});
	return typeFactory.createStructType(
		types,
		new AbstractList<String>() {
			@Override public String get(int index) {
				return "?" + index;
			}

			@Override public int size() {
				return types.size();
			}
		});
}
 
Example #3
Source File: MycatCalciteSQLPrepareObject.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public PlanRunner plan(List<Object> params) {
    SqlNode accept = params.isEmpty() ? sqlNode : SqlNode.clone(sqlNode).accept(
            new SqlShuttle() {
                int index = 0;

                @Override
                public SqlNode visit(SqlDynamicParam param) {
                    Object o = params.get(index);
                    index++;
                    return literal(o);
                }
            });
    return new MycatSqlPlanner(this,getSql(), accept,dataContext);
}
 
Example #4
Source File: EmptyScope.java    From Bats with Apache License 2.0 5 votes vote down vote up
public SqlMonotonicity getMonotonicity(SqlNode expr) {
  return
      ((expr instanceof SqlLiteral)
          || (expr instanceof SqlDynamicParam)
          || (expr instanceof SqlDataTypeSpec)) ? SqlMonotonicity.CONSTANT
          : SqlMonotonicity.NOT_MONOTONIC;
}
 
Example #5
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
public RelDataType getParameterRowType(SqlNode sqlQuery) {
	// NOTE: We assume that bind variables occur in depth-first tree
	// traversal in the same order that they occurred in the SQL text.
	final List<RelDataType> types = new ArrayList<>();
	// NOTE: but parameters on fetch/offset would be counted twice
	// as they are counted in the SqlOrderBy call and the inner SqlSelect call
	final Set<SqlNode> alreadyVisited = new HashSet<>();
	sqlQuery.accept(
		new SqlShuttle() {

			@Override public SqlNode visit(SqlDynamicParam param) {
				if (alreadyVisited.add(param)) {
					RelDataType type = getValidatedNodeType(param);
					types.add(type);
				}
				return param;
			}
		});
	return typeFactory.createStructType(
		types,
		new AbstractList<String>() {
			@Override public String get(int index) {
				return "?" + index;
			}

			@Override public int size() {
				return types.size();
			}
		});
}
 
Example #6
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
private void handleOffsetFetch(SqlNode offset, SqlNode fetch) {
	if (offset instanceof SqlDynamicParam) {
		setValidatedNodeType(offset,
			typeFactory.createSqlType(SqlTypeName.INTEGER));
	}
	if (fetch instanceof SqlDynamicParam) {
		setValidatedNodeType(fetch,
			typeFactory.createSqlType(SqlTypeName.INTEGER));
	}
}
 
Example #7
Source File: AbstractTypeCoercion.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Cast operand at index {@code index} to target type.
 * we do this base on the fact that validate happens before type coercion.
 */
protected boolean coerceOperandType(
    SqlValidatorScope scope,
    SqlCall call,
    int index,
    RelDataType targetType) {
  // Transform the JavaType to SQL type because the SqlDataTypeSpec
  // does not support deriving JavaType yet.
  if (RelDataTypeFactoryImpl.isJavaType(targetType)) {
    targetType = ((JavaTypeFactory) factory).toSql(targetType);
  }

  SqlNode operand = call.getOperandList().get(index);
  if (operand instanceof SqlDynamicParam) {
    // Do not support implicit type coercion for dynamic param.
    return false;
  }
  // Check it early.
  if (!needToCast(scope, operand, targetType)) {
    return false;
  }
  // Fix up nullable attr.
  RelDataType targetType1 = syncAttributes(validator.deriveType(scope, operand), targetType);
  SqlNode desired = castTo(operand, targetType1);
  call.setOperand(index, desired);
  updateInferredType(desired, targetType1);
  return true;
}
 
Example #8
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void handleOffsetFetch(SqlNode offset, SqlNode fetch) {
	if (offset instanceof SqlDynamicParam) {
		setValidatedNodeType(offset,
			typeFactory.createSqlType(SqlTypeName.INTEGER));
	}
	if (fetch instanceof SqlDynamicParam) {
		setValidatedNodeType(fetch,
			typeFactory.createSqlType(SqlTypeName.INTEGER));
	}
}
 
Example #9
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  final boolean isNullable = opBinding
      .getOperandType(0)
      .isNullable();

  RelDataType ret = factory.createTypeWithNullability(
      opBinding.getOperandType(1),
      isNullable);
  if (opBinding instanceof SqlCallBinding) {
    SqlCallBinding callBinding = (SqlCallBinding) opBinding;
    SqlNode operand0 = callBinding.operand(0);

    // dynamic parameters and null constants need their types assigned
    // to them using the type they are casted to.
    if(((operand0 instanceof SqlLiteral)
        && (((SqlLiteral) operand0).getValue() == null))
            || (operand0 instanceof SqlDynamicParam)) {
      callBinding.getValidator().setValidatedNodeType(
          operand0,
          ret);
    }
  }

  return ret;
}
 
Example #10
Source File: EmptyScope.java    From calcite with Apache License 2.0 5 votes vote down vote up
public SqlMonotonicity getMonotonicity(SqlNode expr) {
  return
      ((expr instanceof SqlLiteral)
          || (expr instanceof SqlDynamicParam)
          || (expr instanceof SqlDataTypeSpec)) ? SqlMonotonicity.CONSTANT
          : SqlMonotonicity.NOT_MONOTONIC;
}
 
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: 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 #13
Source File: AbstractTypeCoercion.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Cast column at index {@code index} to target type.
 *
 * @param scope      Validator scope for the node list
 * @param nodeList   Column node list
 * @param index      Index of column
 * @param targetType Target type to cast to
 */
protected boolean coerceColumnType(
    SqlValidatorScope scope,
    SqlNodeList nodeList,
    int index,
    RelDataType targetType) {
  // Transform the JavaType to SQL type because the SqlDataTypeSpec
  // does not support deriving JavaType yet.
  if (RelDataTypeFactoryImpl.isJavaType(targetType)) {
    targetType = ((JavaTypeFactory) factory).toSql(targetType);
  }

  // This will happen when there is a star/dynamic-star column in the select list,
  // and the source is values expression, i.e. `select * from (values(1, 2, 3))`.
  // There is no need to coerce the column type, only remark
  // the inferred row type has changed, we will then add in type coercion
  // when expanding star/dynamic-star.

  // See SqlToRelConverter#convertSelectList for details.
  if (index >= nodeList.getList().size()) {
    // Can only happen when there is a star(*) in the column,
    // just return true.
    return true;
  }

  final SqlNode node = nodeList.get(index);
  if (node instanceof SqlDynamicParam) {
    // Do not support implicit type coercion for dynamic param.
    return false;
  }
  if (node instanceof SqlIdentifier) {
    // Do not expand a star/dynamic table col.
    SqlIdentifier node1 = (SqlIdentifier) node;
    if (node1.isStar()) {
      return true;
    } else if (DynamicRecordType.isDynamicStarColName(Util.last(node1.names))) {
      // Should support implicit cast for dynamic table.
      return false;
    }
  }

  if (node instanceof SqlCall) {
    SqlCall node2 = (SqlCall) node;
    if (node2.getOperator().kind == SqlKind.AS) {
      final SqlNode operand = node2.operand(0);
      if (!needToCast(scope, operand, targetType)) {
        return false;
      }
      RelDataType targetType2 = syncAttributes(validator.deriveType(scope, operand), targetType);
      final SqlNode casted = castTo(operand, targetType2);
      node2.setOperand(0, casted);
      updateInferredType(casted, targetType2);
      return true;
    }
  }
  if (!needToCast(scope, node, targetType)) {
    return false;
  }
  RelDataType targetType3 = syncAttributes(validator.deriveType(scope, node), targetType);
  final SqlNode node3 = castTo(node, targetType3);
  nodeList.set(index, node3);
  updateInferredType(node3, targetType3);
  return true;
}
 
Example #14
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 4 votes vote down vote up
public RelDataType visit(SqlDynamicParam param) {
	return unknownType;
}
 
Example #15
Source File: PushDownUtil.java    From kylin with Apache License 2.0 4 votes vote down vote up
@Override
public SqlNode visit(SqlDynamicParam param) {
    return null;
}
 
Example #16
Source File: AncestorsVisitor.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public List<SqlIdentifier> visit(SqlDynamicParam param) {
  return Collections.emptyList();
}
 
Example #17
Source File: SqlValidatorUtil.java    From calcite with Apache License 2.0 4 votes vote down vote up
public SqlNode visit(SqlDynamicParam param) {
  return SqlNode.clone(param);
}
 
Example #18
Source File: BaseSqlVisitor.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public T visit(SqlDynamicParam param) {
  throw new UnsupportedOperationException("SqlDynamicParam " + param);
}
 
Example #19
Source File: SqlNodes.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override public Void visit(SqlDynamicParam param) {
  return format(param);
}
 
Example #20
Source File: SqlShuttle.java    From calcite with Apache License 2.0 4 votes vote down vote up
public SqlNode visit(SqlDynamicParam param) {
  return param;
}
 
Example #21
Source File: SqlBasicVisitor.java    From calcite with Apache License 2.0 4 votes vote down vote up
public R visit(SqlDynamicParam param) {
  return null;
}
 
Example #22
Source File: PushDownUtil.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public SqlNode visit(SqlDynamicParam param) {
    return null;
}
 
Example #23
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override public Set<String> visit(SqlDynamicParam param) {
	return ImmutableSet.of();
}
 
Example #24
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override public Void visit(SqlDynamicParam param) {
	throw Util.needToImplement(param);
}
 
Example #25
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 4 votes vote down vote up
public void validateDynamicParam(SqlDynamicParam dynamicParam) {
}
 
Example #26
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override public Set<String> visit(SqlDynamicParam param) {
	return ImmutableSet.of();
}
 
Example #27
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
public RelDataType visit(SqlDynamicParam param) {
	return unknownType;
}
 
Example #28
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override public Void visit(SqlDynamicParam param) {
	throw Util.needToImplement(param);
}
 
Example #29
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
public void validateDynamicParam(SqlDynamicParam dynamicParam) {
}
 
Example #30
Source File: SqlBasicVisitor.java    From Bats with Apache License 2.0 4 votes vote down vote up
public R visit(SqlDynamicParam param) {
  return null;
}