org.apache.calcite.sql.SqlSelect Java Examples

The following examples show how to use org.apache.calcite.sql.SqlSelect. 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: SideParser.java    From alchemy with Apache License 2.0 6 votes vote down vote up
public static void rewrite(SqlNode sqlNode, SqlSelect sqlSelect) {
    SqlKind sqlKind = sqlNode.getKind();
    switch (sqlKind) {
        case INSERT:
            SqlInsert sqlInsert = ((SqlInsert)sqlNode);
            sqlInsert.setSource(sqlSelect);
            break;
        case SELECT:
            SqlSelect select = (SqlSelect)sqlNode;
            select.setFrom(sqlSelect);
            break;
        case AS:
            SqlBasicCall basicCall = (SqlBasicCall)sqlNode;
            basicCall.setOperand(0, sqlSelect);
            break;
        default:
            throw new UnsupportedOperationException(sqlKind + "目前不支持维表操作");
    }
}
 
Example #2
Source File: SqlParseUtil.java    From alchemy with Apache License 2.0 6 votes vote down vote up
public static void parse(List<String> sqls, List<String> sources, List<String> udfs, List<String> sinks)
    throws SqlParseException {
    for (String sql : sqls) {
        SqlParser sqlParser = SqlParser.create(sql, CONFIG);
        SqlNode sqlNode = sqlParser.parseStmt();
        SqlKind kind = sqlNode.getKind();
        switch (kind){
            case INSERT:
                SqlInsert sqlInsert = (SqlInsert)sqlNode;
                addSink(sinks, findSinkName(sqlInsert));
                SqlSelect source = (SqlSelect) sqlInsert.getSource();
                parseSource(source, sources, udfs);
                break;
            case SELECT:
                parseSource((SqlSelect) sqlNode, sources, udfs);
                break;
            default:
                throw new IllegalArgumentException("It must be an insert SQL, sql:" + sql);
        }
    }
}
 
Example #3
Source File: SqlImplementor.java    From Bats with Apache License 2.0 6 votes vote down vote up
private boolean hasNestedAggregations(LogicalAggregate rel) {
    if (node instanceof SqlSelect) {
        final SqlNodeList selectList = ((SqlSelect) node).getSelectList();
        if (selectList != null) {
            final Set<Integer> aggregatesArgs = new HashSet<>();
            for (AggregateCall aggregateCall : rel.getAggCallList()) {
                aggregatesArgs.addAll(aggregateCall.getArgList());
            }
            for (int aggregatesArg : aggregatesArgs) {
                if (selectList.get(aggregatesArg) instanceof SqlBasicCall) {
                    final SqlBasicCall call = (SqlBasicCall) selectList.get(aggregatesArg);
                    for (SqlNode operand : call.getOperands()) {
                        if (operand instanceof SqlCall
                                && ((SqlCall) operand).getOperator() instanceof SqlAggFunction) {
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}
 
Example #4
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
public void validateUpdate(SqlUpdate call) {
	final SqlValidatorNamespace targetNamespace = getNamespace(call);
	validateNamespace(targetNamespace, unknownType);
	final RelOptTable relOptTable = SqlValidatorUtil.getRelOptTable(
		targetNamespace, catalogReader.unwrap(Prepare.CatalogReader.class), null, null);
	final SqlValidatorTable table = relOptTable == null
		? targetNamespace.getTable()
		: relOptTable.unwrap(SqlValidatorTable.class);

	final RelDataType targetRowType =
		createTargetRowType(
			table,
			call.getTargetColumnList(),
			true);

	final SqlSelect select = call.getSourceSelect();
	validateSelect(select, targetRowType);

	final RelDataType sourceRowType = getNamespace(call).getRowType();
	checkTypeAssignment(sourceRowType, targetRowType, call);

	checkConstraint(table, call, targetRowType);

	validateAccess(call.getTargetTable(), table, SqlAccessEnum.UPDATE);
}
 
Example #5
Source File: SideParser.java    From alchemy with Apache License 2.0 6 votes vote down vote up
public static void parse(SqlNode sqlNode, Deque<SqlNode> deque) {
    deque.offer(sqlNode);
    SqlKind sqlKind = sqlNode.getKind();
    switch (sqlKind) {
        case INSERT:
            SqlNode sqlSource = ((SqlInsert)sqlNode).getSource();
            parse(sqlSource, deque);
            break;
        case SELECT:
            SqlNode sqlFrom = ((SqlSelect)sqlNode).getFrom();
            parse(sqlFrom, deque);
            break;
        case JOIN:
            SqlNode sqlLeft = ((SqlJoin)sqlNode).getLeft();
            SqlNode sqlRight = ((SqlJoin)sqlNode).getRight();
            parse(sqlLeft, deque);
            parse(sqlRight, deque);
            break;
        case AS:
            SqlNode sqlAs = ((SqlBasicCall)sqlNode).getOperands()[0];
            parse(sqlAs, deque);
            break;
        default:
            return;
    }
}
 
Example #6
Source File: RuleParser.java    From streamline with Apache License 2.0 6 votes vote down vote up
public void parse() {
    try {
        SchemaPlus schema = Frameworks.createRootSchema(true);
        FrameworkConfig config = Frameworks.newConfigBuilder().defaultSchema(schema).build();
        Planner planner = Frameworks.getPlanner(config);
        SqlSelect sqlSelect = (SqlSelect) planner.parse(sql);
        // FROM
        streams = parseStreams(sqlSelect);
        // SELECT
        projection = parseProjection(sqlSelect);
        // WHERE
        condition = parseCondition(sqlSelect);
        // GROUP BY
        groupBy = parseGroupBy(sqlSelect);
        // HAVING
        having = parseHaving(sqlSelect);
    } catch (Exception ex) {
        LOG.error("Got Exception while parsing rule {}", sql);
        throw new RuntimeException(ex);
    }
}
 
Example #7
Source File: SideParser.java    From alchemy with Apache License 2.0 6 votes vote down vote up
public static SqlSelect newSelect(SqlSelect selectSelf, String table, String alias, boolean left, boolean newTable) {
    List<SqlNode> operand = selectSelf.getOperandList();
    SqlNodeList keywordList = (SqlNodeList)operand.get(0);
    SqlNodeList selectList = (SqlNodeList)operand.get(1);
    SqlNode from = operand.get(2);
    SqlNode where = operand.get(3);
    SqlNodeList groupBy = (SqlNodeList)operand.get(4);
    SqlNode having = operand.get(5);
    SqlNodeList windowDecls = (SqlNodeList)operand.get(6);
    SqlNodeList orderBy = (SqlNodeList)operand.get(7);
    SqlNode offset = operand.get(8);
    SqlNode fetch = operand.get(9);
    if (left) {
        return newSelect(selectSelf.getParserPosition(), keywordList, selectList, ((SqlJoin)from).getLeft(), where,
            groupBy, having, windowDecls, orderBy, offset, fetch, alias, newTable);
    }
    if (newTable) {
        return newSelect(selectSelf.getParserPosition(), null,  creatFullNewSelectList(alias, selectList), createNewFrom(table, alias, from),
            where, groupBy, having, windowDecls, orderBy, offset, fetch, alias, newTable);
    } else {
        return newSelect(selectSelf.getParserPosition(), null, selectList, ((SqlJoin)from).getRight(), where,
            groupBy, having, windowDecls, orderBy, offset, fetch, alias, newTable);
    }

}
 
Example #8
Source File: SqlImplementor.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Wraps a node in a SELECT statement that has no clauses:
 *  "SELECT ... FROM (node)". */
SqlSelect wrapSelect(SqlNode node) {
  assert node instanceof SqlJoin
      || node instanceof SqlIdentifier
      || node instanceof SqlMatchRecognize
      || node instanceof SqlCall
          && (((SqlCall) node).getOperator() instanceof SqlSetOperator
              || ((SqlCall) node).getOperator() == SqlStdOperatorTable.AS
              || ((SqlCall) node).getOperator() == SqlStdOperatorTable.VALUES)
      : node;
  if (requiresAlias(node)) {
    node = as(node, "t");
  }
  return new SqlSelect(POS, SqlNodeList.EMPTY, null, node, null, null, null,
      SqlNodeList.EMPTY, null, null, null, null);
}
 
Example #9
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
public boolean isAggregate(SqlSelect select) {
	if (getAggregate(select) != null) {
		return true;
	}
	// Also when nested window aggregates are present
	for (SqlCall call : overFinder.findAll(select.getSelectList())) {
		assert call.getKind() == SqlKind.OVER;
		if (isNestedAggregateWindow(call.operand(0))) {
			return true;
		}
		if (isOverAggregateWindow(call.operand(1))) {
			return true;
		}
	}
	return false;
}
 
Example #10
Source File: SqlParseUtil.java    From alchemy with Apache License 2.0 6 votes vote down vote up
private static void parseSelect(SqlNode sqlNode, List<String> sources, List<String> udfs) throws SqlParseException {
    SqlKind sqlKind = sqlNode.getKind();
    switch (sqlKind) {
        case IDENTIFIER:
            break;
        case AS:
            SqlNode firstNode = ((SqlBasicCall)sqlNode).operand(0);
            parseSelect(firstNode, sources, udfs);
            break;
        case SELECT:
            parseSource((SqlSelect)sqlNode, sources, udfs);
            break;
        default:
            parseFunction(sqlNode, udfs);

    }
}
 
Example #11
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
public void validateUpdate(SqlUpdate call) {
	final SqlValidatorNamespace targetNamespace = getNamespace(call);
	validateNamespace(targetNamespace, unknownType);
	final RelOptTable relOptTable = SqlValidatorUtil.getRelOptTable(
		targetNamespace, catalogReader.unwrap(Prepare.CatalogReader.class), null, null);
	final SqlValidatorTable table = relOptTable == null
		? targetNamespace.getTable()
		: relOptTable.unwrap(SqlValidatorTable.class);

	final RelDataType targetRowType =
		createTargetRowType(
			table,
			call.getTargetColumnList(),
			true);

	final SqlSelect select = call.getSourceSelect();
	validateSelect(select, targetRowType);

	final RelDataType sourceRowType = getNamespace(call).getRowType();
	checkTypeAssignment(sourceRowType, targetRowType, call);

	checkConstraint(table, call, targetRowType);

	validateAccess(call.getTargetTable(), table, SqlAccessEnum.UPDATE);
}
 
Example #12
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Locates the n'th expression in an INSERT or UPDATE query.
 *
 * @param query       Query
 * @param ordinal     Ordinal of expression
 * @param sourceCount Number of expressions
 * @return Ordinal'th expression, never null
 */
private SqlNode getNthExpr(SqlNode query, int ordinal, int sourceCount) {
	if (query instanceof SqlInsert) {
		SqlInsert insert = (SqlInsert) query;
		if (insert.getTargetColumnList() != null) {
			return insert.getTargetColumnList().get(ordinal);
		} else {
			return getNthExpr(
				insert.getSource(),
				ordinal,
				sourceCount);
		}
	} else if (query instanceof SqlUpdate) {
		SqlUpdate update = (SqlUpdate) query;
		if (update.getTargetColumnList() != null) {
			return update.getTargetColumnList().get(ordinal);
		} else if (update.getSourceExpressionList() != null) {
			return update.getSourceExpressionList().get(ordinal);
		} else {
			return getNthExpr(
				update.getSourceSelect(),
				ordinal,
				sourceCount);
		}
	} else if (query instanceof SqlSelect) {
		SqlSelect select = (SqlSelect) query;
		if (select.getSelectList().size() == sourceCount) {
			return select.getSelectList().get(ordinal);
		} else {
			return query; // give up
		}
	} else {
		return query; // give up
	}
}
 
Example #13
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void checkRollUpInGroupBy(SqlSelect select) {
	SqlNodeList group = select.getGroup();
	if (group != null) {
		for (SqlNode node : group) {
			checkRollUp(null, select, node, getGroupScope(select), "GROUP BY");
		}
	}
}
 
Example #14
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
ExtendedExpander(SqlValidatorImpl validator, SqlValidatorScope scope,
	SqlSelect select, SqlNode root, boolean havingExpr) {
	super(validator, scope);
	this.select = select;
	this.root = root;
	this.havingExpr = havingExpr;
}
 
Example #15
Source File: SqlImplementor.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Builder(RelNode rel, List<Clause> clauses, SqlSelect select,
    Context context, boolean anon,
    @Nullable Map<String, RelDataType> aliases) {
  this.rel = Objects.requireNonNull(rel);
  this.clauses = Objects.requireNonNull(clauses);
  this.select = Objects.requireNonNull(select);
  this.context = Objects.requireNonNull(context);
  this.anon = anon;
  this.aliases = aliases;
}
 
Example #16
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
protected void validateWhereClause(SqlSelect select) {
	// validate WHERE clause
	final SqlNode where = select.getWhere();
	if (where == null) {
		return;
	}
	final SqlValidatorScope whereScope = getWhereScope(select);
	final SqlNode expandedWhere = expand(where, whereScope);
	select.setWhere(expandedWhere);
	validateWhereOrOn(whereScope, expandedWhere, "WHERE");
}
 
Example #17
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Processes SubQuery found in Select list. Checks that is actually Scalar
 * sub-query and makes proper entries in each of the 3 lists used to create
 * the final rowType entry.
 *
 * @param parentSelect        base SqlSelect item
 * @param selectItem          child SqlSelect from select list
 * @param expandedSelectItems Select items after processing
 * @param aliasList           built from user or system values
 * @param fieldList           Built up entries for each select list entry
 */
private void handleScalarSubQuery(
	SqlSelect parentSelect,
	SqlSelect selectItem,
	List<SqlNode> expandedSelectItems,
	Set<String> aliasList,
	List<Map.Entry<String, RelDataType>> fieldList) {
	// A scalar sub-query only has one output column.
	if (1 != selectItem.getSelectList().size()) {
		throw newValidationError(selectItem,
			RESOURCE.onlyScalarSubQueryAllowed());
	}

	// No expansion in this routine just append to list.
	expandedSelectItems.add(selectItem);

	// Get or generate alias and add to list.
	final String alias =
		deriveAlias(
			selectItem,
			aliasList.size());
	aliasList.add(alias);

	final SelectScope scope = (SelectScope) getWhereScope(parentSelect);
	final RelDataType type = deriveType(scope, selectItem);
	setValidatedNodeType(selectItem, type);

	// we do not want to pass on the RelRecordType returned
	// by the sub query.  Just the type of the single expression
	// in the sub-query select list.
	assert type instanceof RelRecordType;
	RelRecordType rec = (RelRecordType) type;

	RelDataType nodeType = rec.getFieldList().get(0).getType();
	nodeType = typeFactory.createTypeWithNullability(nodeType, true);
	fieldList.add(Pair.of(alias, nodeType));
}
 
Example #18
Source File: SqlImplementor.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Wraps a node in a SELECT statement that has no clauses:
 * "SELECT ... FROM (node)".
 */
SqlSelect wrapSelect(SqlNode node) {
  assert node instanceof SqlJoin
    || node instanceof SqlIdentifier
    || node instanceof SqlMatchRecognize
    || node instanceof SqlCall
    && (((SqlCall) node).getOperator() instanceof SqlSetOperator
    || ((SqlCall) node).getOperator() == SqlStdOperatorTable.AS
    || ((SqlCall) node).getOperator() == SqlStdOperatorTable.VALUES)
    : node;
  return new SqlSelect(POS, SqlNodeList.EMPTY, null, node, null, null, null,
    SqlNodeList.EMPTY, null, null, null);
}
 
Example #19
Source File: RelToSqlConverter.java    From quark with Apache License 2.0 5 votes vote down vote up
public Builder(RelNode rel, List<Clause> clauses, SqlSelect select,
               Context context) {
  this.rel = rel;
  this.clauses = clauses;
  this.select = select;
  this.context = context;
}
 
Example #20
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/** If there is at least one call to an aggregate function, returns the
 * first. */
private SqlNode getAgg(SqlSelect select) {
	final SelectScope selectScope = getRawSelectScope(select);
	if (selectScope != null) {
		final List<SqlNode> selectList = selectScope.getExpandedSelectList();
		if (selectList != null) {
			return aggFinder.findAgg(selectList);
		}
	}
	return aggFinder.findAgg(select.getSelectList());
}
 
Example #21
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
public SelectScope getRawSelectScope(SqlSelect select) {
	SqlValidatorScope scope = getSelectScope(select);
	if (scope instanceof AggregatingSelectScope) {
		scope = ((AggregatingSelectScope) scope).getParent();
	}
	return (SelectScope) scope;
}
 
Example #22
Source File: AggregatingSelectScope.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an AggregatingSelectScope
 *
 * @param selectScope Parent scope
 * @param select      Enclosing SELECT node
 * @param distinct    Whether SELECT is DISTINCT
 */
AggregatingSelectScope(
    SqlValidatorScope selectScope,
    SqlSelect select,
    boolean distinct) {
  // The select scope is the parent in the sense that all columns which
  // are available in the select scope are available. Whether they are
  // valid as aggregation expressions... now that's a different matter.
  super(selectScope);
  this.select = select;
  this.distinct = distinct;
}
 
Example #23
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
private void lookupSelectHints(
	SqlValidatorNamespace ns,
	SqlParserPos pos,
	Collection<SqlMoniker> hintList) {
	final SqlNode node = ns.getNode();
	if (node instanceof SqlSelect) {
		lookupSelectHints((SqlSelect) node, pos, hintList);
	}
}
 
Example #24
Source File: SideStream.java    From alchemy with Apache License 2.0 5 votes vote down vote up
private static SideTable createSideTable(TableSchema leftSchema, RowTypeInfo sideType, JoinType joinType,
    SqlSelect rightSelect, List<String> equalFields, Alias sideAlias, Side side) {
    List<Integer> indexFields = createFieldIndex(leftSchema, equalFields);
    SideTable sideTable = new SideTable();
    sideTable.setConditionIndexs(indexFields);
    sideTable.setConditions(equalFields);
    sideTable.setSide(side);
    sideTable.setJoinType(joinType);
    sideTable.setRowSize(leftSchema.getFieldCount() + sideType.getArity());
    sideTable.setSideAlias(sideAlias);
    sideTable.setSideType(sideType);
    sideTable.setSql(rightSelect.toString());
    return sideTable;
}
 
Example #25
Source File: CalciteParser.java    From kylin with Apache License 2.0 5 votes vote down vote up
public static SqlNode getOnlySelectNode(String sql) {
    SqlNodeList selectList = null;
    try {
        selectList = ((SqlSelect) CalciteParser.parse(sql)).getSelectList();
    } catch (SqlParseException e) {
        throw new RuntimeException(
                "Failed to parse expression \'" + sql + "\', please make sure the expression is valid", e);
    }

    Preconditions.checkArgument(selectList.size() == 1,
            "Expression is invalid because size of select list exceeds one");

    return selectList.get(0);
}
 
Example #26
Source File: RuleParser.java    From streamline with Apache License 2.0 5 votes vote down vote up
private Projection parseProjection(SqlSelect sqlSelect) {
    Projection projection;
    ExpressionGenerator exprGenerator = new ExpressionGenerator(streams, catalogUdfs);
    ExpressionList exprList = (ExpressionList) sqlSelect.getSelectList().accept(exprGenerator);
    if (exprList.getExpressions().size() == 1 && exprList.getExpressions().get(0) == STAR) {
        projection = null;
    } else {
        projection = new Projection(exprList.getExpressions());
    }
    referredUdfs.addAll(exprGenerator.getReferredUdfs());
    LOG.debug("Projection {}", projection);
    return projection;
}
 
Example #27
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 #28
Source File: SqlAdvisorValidator.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Calls the parent class method and masks Farrago exception thrown.
 */
protected void validateHavingClause(SqlSelect select) {
  try {
    super.validateHavingClause(select);
  } catch (CalciteException e) {
    Util.swallow(e, TRACER);
  }
}
 
Example #29
Source File: SqlAdvisorValidator.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Calls the parent class method and masks Farrago exception thrown.
 */
protected void validateWhereClause(SqlSelect select) {
  try {
    super.validateWhereClause(select);
  } catch (CalciteException e) {
    Util.swallow(e, TRACER);
  }
}
 
Example #30
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
/** If there is at least one call to an aggregate function, returns the
 * first. */
private SqlNode getAgg(SqlSelect select) {
	final SelectScope selectScope = getRawSelectScope(select);
	if (selectScope != null) {
		final List<SqlNode> selectList = selectScope.getExpandedSelectList();
		if (selectList != null) {
			return aggFinder.findAgg(selectList);
		}
	}
	return aggFinder.findAgg(select.getSelectList());
}