Java Code Examples for org.apache.calcite.sql.SqlIdentifier#isStar()

The following examples show how to use org.apache.calcite.sql.SqlIdentifier#isStar() . 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: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private void checkRollUp(SqlNode grandParent, SqlNode parent,
	SqlNode current, SqlValidatorScope scope, String optionalClause) {
	current = stripAs(current);
	if (current instanceof SqlCall && !(current instanceof SqlSelect)) {
		// Validate OVER separately
		checkRollUpInWindow(getWindowInOver(current), scope);
		current = stripOver(current);

		List<SqlNode> children = ((SqlCall) stripDot(current)).getOperandList();
		for (SqlNode child : children) {
			checkRollUp(parent, current, child, scope, optionalClause);
		}
	} else if (current instanceof SqlIdentifier) {
		SqlIdentifier id = (SqlIdentifier) current;
		if (!id.isStar() && isRolledUpColumn(id, scope)) {
			if (!isAggregation(parent.getKind())
				|| !isRolledUpColumnAllowedInAgg(id, scope, (SqlCall) parent, grandParent)) {
				String context = optionalClause != null ? optionalClause : parent.getKind().toString();
				throw newValidationError(id,
					RESOURCE.rolledUpNotAllowed(deriveAlias(id, 0), context));
			}
		}
	}
}
 
Example 2
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
private void checkRollUp(SqlNode grandParent, SqlNode parent,
	SqlNode current, SqlValidatorScope scope, String optionalClause) {
	current = stripAs(current);
	if (current instanceof SqlCall && !(current instanceof SqlSelect)) {
		// Validate OVER separately
		checkRollUpInWindow(getWindowInOver(current), scope);
		current = stripOver(current);

		List<SqlNode> children = ((SqlCall) stripDot(current)).getOperandList();
		for (SqlNode child : children) {
			checkRollUp(parent, current, child, scope, optionalClause);
		}
	} else if (current instanceof SqlIdentifier) {
		SqlIdentifier id = (SqlIdentifier) current;
		if (!id.isStar() && isRolledUpColumn(id, scope)) {
			if (!isAggregation(parent.getKind())
				|| !isRolledUpColumnAllowedInAgg(id, scope, (SqlCall) parent, grandParent)) {
				String context = optionalClause != null ? optionalClause : parent.getKind().toString();
				throw newValidationError(id,
					RESOURCE.rolledUpNotAllowed(deriveAlias(id, 0), context));
			}
		}
	}
}
 
Example 3
Source File: FunctionCatalogOperatorTable.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void lookupOperatorOverloads(
		SqlIdentifier opName,
		SqlFunctionCategory category,
		SqlSyntax syntax,
		List<SqlOperator> operatorList,
		SqlNameMatcher nameMatcher) {
	if (opName.isStar()) {
		return;
	}

	final UnresolvedIdentifier identifier = UnresolvedIdentifier.of(opName.names);

	functionCatalog.lookupFunction(identifier)
		.flatMap(lookupResult ->
			convertToSqlFunction(
				category,
				lookupResult.getFunctionIdentifier(),
				lookupResult.getFunctionDefinition()))
		.ifPresent(operatorList::add);
}
 
Example 4
Source File: AggChecker.java    From Bats with Apache License 2.0 5 votes vote down vote up
public Void visit(SqlIdentifier id) {
  if (isGroupExpr(id) || id.isStar()) {
    // Star may validly occur in "SELECT COUNT(*) OVER w"
    return null;
  }

  // Is it a call to a parentheses-free function?
  SqlCall call =
      SqlUtil.makeCall(
          validator.getOperatorTable(),
          id);
  if (call != null) {
    return call.accept(this);
  }

  // Didn't find the identifier in the group-by list as is, now find
  // it fully-qualified.
  // TODO: It would be better if we always compared fully-qualified
  // to fully-qualified.
  final SqlQualified fqId = scopes.peek().fullyQualify(id);
  if (isGroupExpr(fqId.identifier)) {
    return null;
  }
  SqlNode originalExpr = validator.getOriginal(id);
  final String exprString = originalExpr.toString();
  throw validator.newValidationError(originalExpr,
      distinct
          ? RESOURCE.notSelectDistinctExpr(exprString)
          : RESOURCE.notGroupExpr(exprString));
}
 
Example 5
Source File: ExpressionGenerator.java    From streamline with Apache License 2.0 5 votes vote down vote up
@Override
public Expression visit(SqlIdentifier id) {
    if (id.isStar()) {
        return STAR;
    } else if (id.isSimple()) {
        return new FieldExpression(getField(getSchema(), id.getSimple()));
    } else if (id.names.size() == 2) {
        return new FieldExpression(getField(getSchema(id.names.get(0)), id.names.get(1)));
    } else {
        throw new UnsupportedOperationException("Compound identifier with more than two levels");
    }
}
 
Example 6
Source File: AggChecker.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Void visit(SqlIdentifier id) {
  if (isGroupExpr(id) || id.isStar()) {
    // Star may validly occur in "SELECT COUNT(*) OVER w"
    return null;
  }

  // Is it a call to a parentheses-free function?
  final SqlCall call = validator.makeNullaryCall(id);
  if (call != null) {
    return call.accept(this);
  }

  // Didn't find the identifier in the group-by list as is, now find
  // it fully-qualified.
  // TODO: It would be better if we always compared fully-qualified
  // to fully-qualified.
  final SqlQualified fqId = scopes.peek().fullyQualify(id);
  if (isGroupExpr(fqId.identifier)) {
    return null;
  }
  SqlNode originalExpr = validator.getOriginal(id);
  final String exprString = originalExpr.toString();
  throw validator.newValidationError(originalExpr,
      distinct
          ? RESOURCE.notSelectDistinctExpr(exprString)
          : RESOURCE.notGroupExpr(exprString));
}
 
Example 7
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;
}