Java Code Examples for org.apache.calcite.sql.SqlSelect#getHaving()

The following examples show how to use org.apache.calcite.sql.SqlSelect#getHaving() . 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 5 votes vote down vote up
/** Returns the parse tree node (GROUP BY, HAVING, or an aggregate function
 * call) that causes {@code select} to be an aggregate query, or null if it
 * is not an aggregate query.
 *
 * <p>The node is useful context for error messages,
 * but you cannot assume that the node is the only aggregate function. */
protected SqlNode getAggregate(SqlSelect select) {
	SqlNode node = select.getGroup();
	if (node != null) {
		return node;
	}
	node = select.getHaving();
	if (node != null) {
		return node;
	}
	return getAgg(select);
}
 
Example 2
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
protected void validateHavingClause(SqlSelect select) {
	// HAVING is validated in the scope after groups have been created.
	// For example, in "SELECT empno FROM emp WHERE empno = 10 GROUP BY
	// deptno HAVING empno = 10", the reference to 'empno' in the HAVING
	// clause is illegal.
	SqlNode having = select.getHaving();
	if (having == null) {
		return;
	}
	final AggregatingScope havingScope =
		(AggregatingScope) getSelectScope(select);
	if (getConformance().isHavingAlias()) {
		SqlNode newExpr = expandGroupByOrHavingExpr(having, havingScope, select, true);
		if (having != newExpr) {
			having = newExpr;
			select.setHaving(newExpr);
		}
	}
	havingScope.checkAggregateExpr(having, true);
	inferUnknownTypes(
		booleanType,
		havingScope,
		having);
	having.validate(this, havingScope);
	final RelDataType type = deriveType(havingScope, having);
	if (!SqlTypeUtil.inBooleanFamily(type)) {
		throw newValidationError(having, RESOURCE.havingMustBeBoolean());
	}
}
 
Example 3
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
/** Returns the parse tree node (GROUP BY, HAVING, or an aggregate function
 * call) that causes {@code select} to be an aggregate query, or null if it
 * is not an aggregate query.
 *
 * <p>The node is useful context for error messages,
 * but you cannot assume that the node is the only aggregate function. */
protected SqlNode getAggregate(SqlSelect select) {
	SqlNode node = select.getGroup();
	if (node != null) {
		return node;
	}
	node = select.getHaving();
	if (node != null) {
		return node;
	}
	return getAgg(select);
}
 
Example 4
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
protected void validateHavingClause(SqlSelect select) {
	// HAVING is validated in the scope after groups have been created.
	// For example, in "SELECT empno FROM emp WHERE empno = 10 GROUP BY
	// deptno HAVING empno = 10", the reference to 'empno' in the HAVING
	// clause is illegal.
	SqlNode having = select.getHaving();
	if (having == null) {
		return;
	}
	final AggregatingScope havingScope =
		(AggregatingScope) getSelectScope(select);
	if (getConformance().isHavingAlias()) {
		SqlNode newExpr = expandGroupByOrHavingExpr(having, havingScope, select, true);
		if (having != newExpr) {
			having = newExpr;
			select.setHaving(newExpr);
		}
	}
	havingScope.checkAggregateExpr(having, true);
	inferUnknownTypes(
		booleanType,
		havingScope,
		having);
	having.validate(this, havingScope);
	final RelDataType type = deriveType(havingScope, having);
	if (!SqlTypeUtil.inBooleanFamily(type)) {
		throw newValidationError(having, RESOURCE.havingMustBeBoolean());
	}
}
 
Example 5
Source File: SqlParseUtil.java    From alchemy with Apache License 2.0 5 votes vote down vote up
private static void parseSource(SqlSelect sqlSelect, List<String> sources, List<String> udfs)
    throws SqlParseException {
    SqlNodeList selectList = sqlSelect.getSelectList();
    SqlNode from = sqlSelect.getFrom();
    SqlNode where = sqlSelect.getWhere();
    SqlNode having = sqlSelect.getHaving();
    parseSelectList(selectList, sources, udfs);
    parseFrom(from, sources, udfs);
    parseFunction(where, udfs);
    parseFunction(having, udfs);
}
 
Example 6
Source File: RuleParser.java    From streamline with Apache License 2.0 5 votes vote down vote up
private Having parseHaving(SqlSelect sqlSelect) {
    Having having = null;
    SqlNode sqlHaving = sqlSelect.getHaving();
    if (sqlHaving != null) {
        ExpressionGenerator exprGenerator = new ExpressionGenerator(streams, catalogUdfs);
        having = new Having(sqlHaving.accept(exprGenerator));
        referredUdfs.addAll(exprGenerator.getReferredUdfs());
    }
    LOG.debug("Having {}", having);
    return having;
}
 
Example 7
Source File: QuerySemantics.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private static VirtualDatasetState.Builder extractSelect(String sql, SqlNode node, final RelDataType relDataType) {
  SqlSelect select = (SqlSelect)node;

  // From table
  final SqlNode fromNode = select.getFrom();
  if (fromNode == null) {
    return fallback("without FROM clause", node, sql);
  }
  final FromNode from = extractFrom(fromNode);

  // Selected columns
  final List<Column> columns = extractColumns(relDataType, select, from);

  final SqlNode where = select.getWhere();
  if (where != null) {
    return fallback("where is not supported yet", where, sql);
  }
  final SqlNodeList groupBy = select.getGroup();
  if (groupBy != null) {
    return fallback("group by is not supported yet", groupBy, sql);
  }
  final SqlNode having = select.getHaving();
  if (having != null) {
    return fallback("having is not supported yet", having, sql);
  }
  final SqlNodeList windowDecls = select.getWindowList();
  if (windowDecls != null && !windowDecls.getList().isEmpty()) {
    return fallback("window is not supported yet", windowDecls, sql);
  }
  final List<Order> orders = extractOrders(select.getOrderList(), from);

  final SqlNode offset = select.getOffset();
  if (offset != null) {
    return fallback("offset is not supported yet", offset, sql);
  }
  final SqlNode fetch = select.getFetch();
  if (fetch != null) {
    return fallback("fetch is not supported yet", fetch, sql);
  }
  From.Builder fromTable = From.newBuilder()
    .setType(FromType.TABLE)
    .setValue(from.getTableToString());
  if (from.alias != null) {
    fromTable.setAlias(from.getAliasToString());
  }

  final VirtualDatasetState.Builder state = VirtualDatasetState.newBuilder().setFrom(fromTable);
  if (columns != null) {
    state.addAllColumns(columns);
  }
  if (orders != null) {
    state.addAllOrders(orders);
  }
  return state;
}