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

The following examples show how to use org.apache.calcite.sql.SqlSelect#getFrom() . 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: AncestorsVisitor.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public List<SqlIdentifier> visit(SqlCall call) {
  List<SqlIdentifier> result = new ArrayList<>();
  switch (call.getKind()) {
  case SELECT:
    SqlSelect select = (SqlSelect)call;
    final SqlNode from = select.getFrom();
    if (from != null) {
      result.addAll(extractAncestorsFromFrom(from));
    }
    break;
  default:
    result.addAll(visitAll(call.getOperandList()));
    break;
  }
  return result;
}
 
Example 2
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Looks up completion hints for a syntactically correct select SQL that has
 * been parsed into an expression tree.
 *
 * @param select   the Select node of the parsed expression tree
 * @param pos      indicates the position in the sql statement we want to get
 *                 completion hints for
 * @param hintList list of {@link SqlMoniker} (sql identifiers) that can
 *                 fill in at the indicated position
 */
void lookupSelectHints(
	SqlSelect select,
	SqlParserPos pos,
	Collection<SqlMoniker> hintList) {
	IdInfo info = idPositions.get(pos.toString());
	if ((info == null) || (info.scope == null)) {
		SqlNode fromNode = select.getFrom();
		final SqlValidatorScope fromScope = getFromScope(select);
		lookupFromHints(fromNode, fromScope, pos, hintList);
	} else {
		lookupNameCompletionHints(info.scope, info.id.names,
			info.id.getParserPosition(), hintList);
	}
}
 
Example 3
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Looks up completion hints for a syntactically correct select SQL that has
 * been parsed into an expression tree.
 *
 * @param select   the Select node of the parsed expression tree
 * @param pos      indicates the position in the sql statement we want to get
 *                 completion hints for
 * @param hintList list of {@link SqlMoniker} (sql identifiers) that can
 *                 fill in at the indicated position
 */
void lookupSelectHints(
	SqlSelect select,
	SqlParserPos pos,
	Collection<SqlMoniker> hintList) {
	IdInfo info = idPositions.get(pos.toString());
	if ((info == null) || (info.scope == null)) {
		SqlNode fromNode = select.getFrom();
		final SqlValidatorScope fromScope = getFromScope(select);
		lookupFromHints(fromNode, fromScope, pos, hintList);
	} else {
		lookupNameCompletionHints(info.scope, info.id.names,
			info.id.getParserPosition(), hintList);
	}
}
 
Example 4
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 5
Source File: SideStream.java    From alchemy with Apache License 2.0 5 votes vote down vote up
public static DataStream<Row> buildStream(StreamTableEnvironment env, SqlSelect sqlSelect, Alias leftAlias,
    Alias sideAlias, SourceDescriptor sideSource) throws Exception {
    SqlSelect leftSelect = SideParser.newSelect(sqlSelect, leftAlias.getTable(), leftAlias.getAlias(), true, false);
    // register leftTable
    Table leftTable = env.sqlQuery(leftSelect.toString());
    DataStream<Row> leftStream = env.toAppendStream(leftTable, Row.class);
    SqlSelect rightSelect
        = SideParser.newSelect(sqlSelect, sideAlias.getTable(), sideAlias.getAlias(), false, false);
    SqlJoin sqlJoin = (SqlJoin)sqlSelect.getFrom();
    List<String> equalFields = SideParser.findConditionFields(sqlJoin.getCondition(), leftAlias.getAlias());
    if (sideSource.getSide().isPartition()) {
        leftStream = leftStream.keyBy(equalFields.toArray(new String[equalFields.size()]));
    }
    RowTypeInfo sideType = createSideType(rightSelect.getSelectList(), sideSource.getSchema());
    RowTypeInfo returnType = createReturnType(leftTable.getSchema(), sideType);
    SideTable sideTable = createSideTable(leftTable.getSchema(), sideType, sqlJoin.getJoinType(), rightSelect,
        equalFields, sideAlias, sideSource.getSide());
    DataStream<Row> returnStream;
    if (sideSource.getSide().isAsync()) {
        AbstractAsyncSideFunction reqRow = sideSource.transform(sideTable);
        returnStream = AsyncDataStream.orderedWait(leftStream, reqRow, sideSource.getSide().getTimeout(),
            TimeUnit.MILLISECONDS, sideSource.getSide().getCapacity());
    } else {
        AbstractSyncSideFunction syncReqRow = sideSource.transform(sideTable);
        returnStream = leftStream.flatMap(syncReqRow);
    }
    returnStream.getTransformation().setOutputType(returnType);
    return returnStream;
}
 
Example 6
Source File: RuleParser.java    From streamline with Apache License 2.0 5 votes vote down vote up
private List<Stream> parseStreams(SqlSelect sqlSelect) throws Exception {
    List<Stream> streams = new ArrayList<>();
    SqlNode sqlFrom = sqlSelect.getFrom();
    LOG.debug("from = {}", sqlFrom);
    if (sqlFrom instanceof SqlJoin) {
        throw new IllegalArgumentException("Sql join is not yet supported");
    } else if (sqlFrom instanceof SqlIdentifier) {
        streams.add(getStream(((SqlIdentifier) sqlFrom).getSimple()));
    }
    LOG.debug("Streams {}", streams);
    return streams;
}
 
Example 7
Source File: AbstractFlinkClient.java    From alchemy with Apache License 2.0 4 votes vote down vote up
private Table registerSql(StreamTableEnvironment env, String sql, Map<String, TableSource> tableSources,
                          Map<String, SourceDescriptor> sideSources) throws Exception {
    if (sideSources.isEmpty()) {
        return env.sqlQuery(sql);
    }
    Deque<SqlNode> deque = SideParser.parse(sql);
    SqlNode last;
    SqlSelect modifyNode = null;
    SqlNode fullNode = deque.peekFirst();
    while ((last = deque.pollLast()) != null) {
        if (modifyNode != null) {
            SideParser.rewrite(last, modifyNode);
            modifyNode = null;
        }
        if (last.getKind() == SqlKind.SELECT) {
            SqlSelect sqlSelect = (SqlSelect) last;
            SqlNode selectFrom = sqlSelect.getFrom();
            if (SqlKind.JOIN != selectFrom.getKind()) {
                continue;
            }
            SqlJoin sqlJoin = (SqlJoin) selectFrom;
            Alias sideAlias = SideParser.getTableName(sqlJoin.getRight());
            Alias leftAlias = SideParser.getTableName(sqlJoin.getLeft());
            if (isSide(sideSources.keySet(), leftAlias.getTable())) {
                throw new UnsupportedOperationException("side table must be right table");
            }
            if (!isSide(sideSources.keySet(), sideAlias.getTable())) {
                continue;
            }
            DataStream<Row> dataStream = SideStream.buildStream(env, sqlSelect, leftAlias, sideAlias,
                sideSources.get(sideAlias.getTable()));
            Alias newTable = new Alias(leftAlias.getTable() + "_" + sideAlias.getTable(),
                leftAlias.getAlias() + "_" + sideAlias.getAlias());
            if (!env.isRegistered(newTable.getTable())) {
                env.registerDataStream(newTable.getTable(), dataStream);
            }
            SqlSelect newSelect
                = SideParser.newSelect(sqlSelect, newTable.getTable(), newTable.getAlias(), false, true);
            modifyNode = newSelect;
        }
    }
    if (modifyNode != null) {
        return env.sqlQuery(modifyNode.toString());
    } else {
        return env.sqlQuery(fullNode.toString());
    }

}
 
Example 8
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;
}