Java Code Examples for org.apache.calcite.sql.SqlKind#SELECT

The following examples show how to use org.apache.calcite.sql.SqlKind#SELECT . 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
private boolean shouldCheckForRollUp(SqlNode from) {
	if (from != null) {
		SqlKind kind = stripAs(from).getKind();
		return kind != SqlKind.VALUES && kind != SqlKind.SELECT;
	}
	return false;
}
 
Example 2
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
private boolean shouldCheckForRollUp(SqlNode from) {
	if (from != null) {
		SqlKind kind = stripAs(from).getKind();
		return kind != SqlKind.VALUES && kind != SqlKind.SELECT;
	}
	return false;
}
 
Example 3
Source File: MysqlSideFunction.java    From alchemy with Apache License 2.0 5 votes vote down vote up
private String modifySql(SideTable sideTable) throws SqlParseException {
    SqlParser.Config config = SqlParser.configBuilder().setLex(Lex.MYSQL).build();
    SqlParser sqlParser = SqlParser.create(sideTable.getSql(), config);
    SqlNode sqlNode = sqlParser.parseStmt();
    if (SqlKind.SELECT != sqlNode.getKind()) {
        throw new UnsupportedOperationException(
            "MysqlAsyncReqRow only support query sql, sql:" + sideTable.getSql());
    }
    SqlSelect sqlSelect = (SqlSelect)sqlNode;
    SqlNode whereNode = sqlSelect.getWhere();
    SqlBinaryOperator and = new SqlBinaryOperator("AND", SqlKind.AND, 24, true,
        ReturnTypes.BOOLEAN_NULLABLE_OPTIMIZED, InferTypes.BOOLEAN, OperandTypes.BOOLEAN_BOOLEAN);
    List<SqlBasicCall> conditionNodes = createConditionNodes(sideTable.getConditions(), sideTable.getSideAlias());
    List<SqlNode> nodes = new ArrayList<>();
    nodes.addAll(conditionNodes);
    if (whereNode != null) {
        nodes.add(whereNode);
    } else {
        SqlBinaryOperator equal = new SqlBinaryOperator("=", SqlKind.EQUALS, 30, true, ReturnTypes.BOOLEAN_NULLABLE,
            InferTypes.FIRST_KNOWN, OperandTypes.COMPARABLE_UNORDERED_COMPARABLE_UNORDERED);
        SqlBasicCall andEqual
            = new SqlBasicCall(equal, SideParser.createEqualNodes(SqlKind.AND), new SqlParserPos(0, 0));
        nodes.add(andEqual);
    }
    SqlBasicCall sqlBasicCall
        = new SqlBasicCall(and, nodes.toArray(new SqlNode[nodes.size()]), new SqlParserPos(0, 0));
    sqlSelect.setWhere(sqlBasicCall);
    return sqlSelect.toString();
}
 
Example 4
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 5
Source File: DremioRelToSqlConverter.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private List<SqlNode> addJoinChildSelectNodes(SqlNode node, Set<String> usedNames) {
  final SqlNode childNode = (node.getKind() == SqlKind.AS) ? ((SqlBasicCall) node).getOperands()[0] : node;

  if (childNode.getKind() == SqlKind.JOIN) {
    final SqlJoin join = (SqlJoin)childNode;
    // Delegate to the children of the join to get the node list.
    final List<SqlNode> leftList = addJoinChildSelectNodes(join.getLeft(), usedNames);
    final List<SqlNode> rightList = addJoinChildSelectNodes(join.getRight(), usedNames);
    if (leftList == null || rightList == null) {
      // Not possible to get the nodes of one or the other child, abandon the effort.
      return null;
    }

    return ImmutableList.<SqlNode>builder().addAll(leftList).addAll(rightList).build();
  }

  if (childNode.getKind() != SqlKind.SELECT || ((SqlSelect)childNode).getSelectList() == null) {
    return null;
  }

  // Although we are expanding the * into a list of column references, don't do anything but the expansion as
  // the underlying references will have any necessary modifications (ie addition of explicit casts) done to them
  // already.
  final String tableAlias = SqlValidatorUtil.getAlias(node, -1);
  final List<SqlNode> selectList = new ArrayList<>();
  ((SqlSelect) childNode).getSelectList().getList().stream().forEach(n -> {
    String colAlias = SqlValidatorUtil.getAlias(n, -1);
    if (null == colAlias) {
      // Guard against possible null aliases being returned by generating a unique value.
      colAlias = SqlValidatorUtil.uniquify(colAlias, usedNames, SqlValidatorUtil.EXPR_SUGGESTER);
    } else if (colAlias.equals("\"*\"")) {
      // If * is used as an alias, it ends up getting double quoted when it should not be.
      colAlias = "*";
    }
    final List<String> names = (tableAlias != null) ? ImmutableList.of(tableAlias, colAlias) : ImmutableList.of(colAlias);

    if (n.getKind() == SqlKind.IDENTIFIER) {
      // Ensure we have unique names being used.
      final String alias = SqlValidatorUtil.uniquify(colAlias, usedNames, SqlValidatorUtil.EXPR_SUGGESTER);

      selectList.add(SqlStdOperatorTable.AS.createCall(
        POS,
        new SqlIdentifier(names, n.getParserPosition()),
        new SqlIdentifier(alias, POS)));
    } else if (n.getKind() == SqlKind.AS) {
      selectList.add(new SqlIdentifier(names, POS));
    } else {
      selectList.add(n);
    }

    usedNames.add(colAlias);
  });

  return selectList;
}
 
Example 6
Source File: DruidTable.java    From calcite with Apache License 2.0 4 votes vote down vote up
private boolean isValidParentKind(SqlNode node) {
  return node.getKind() == SqlKind.SELECT
          || node.getKind() == SqlKind.FILTER
          || isSupportedPostAggOperation(node.getKind());
}
 
Example 7
Source File: CalcitePrepareImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
private PreparedResult prepare_(Supplier<RelNode> fn,
    RelDataType resultType) {
  Class runtimeContextClass = Object.class;
  init(runtimeContextClass);

  final RelNode rel = fn.get();
  final RelDataType rowType = rel.getRowType();
  final List<Pair<Integer, String>> fields =
      Pair.zip(ImmutableIntList.identity(rowType.getFieldCount()),
          rowType.getFieldNames());
  final RelCollation collation =
      rel instanceof Sort
          ? ((Sort) rel).collation
          : RelCollations.EMPTY;
  RelRoot root = new RelRoot(rel, resultType, SqlKind.SELECT, fields,
      collation, new ArrayList<>());

  if (timingTracer != null) {
    timingTracer.traceTime("end sql2rel");
  }

  final RelDataType jdbcType =
      makeStruct(rexBuilder.getTypeFactory(), resultType);
  fieldOrigins = Collections.nCopies(jdbcType.getFieldCount(), null);
  parameterRowType = rexBuilder.getTypeFactory().builder().build();

  // Structured type flattening, view expansion, and plugging in
  // physical storage.
  root = root.withRel(flattenTypes(root.rel, true));

  // Trim unused fields.
  root = trimUnusedFields(root);

  final List<Materialization> materializations = ImmutableList.of();
  final List<CalciteSchema.LatticeEntry> lattices = ImmutableList.of();
  root = optimize(root, materializations, lattices);

  if (timingTracer != null) {
    timingTracer.traceTime("end optimization");
  }

  return implement(root);
}
 
Example 8
Source File: MockCatalogReader.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public boolean rolledUpColumnValidInsideAgg(String column,
    SqlCall call, SqlNode parent, CalciteConnectionConfig config) {
  // For testing
  return call.getKind() != SqlKind.MAX
      && (parent.getKind() == SqlKind.SELECT || parent.getKind() == SqlKind.FILTER);
}
 
Example 9
Source File: MockCatalogReader.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public boolean rolledUpColumnValidInsideAgg(String column,
    SqlCall call, SqlNode parent, CalciteConnectionConfig config) {
  // For testing
  return call.getKind() != SqlKind.MAX
          && (parent.getKind() == SqlKind.SELECT || parent.getKind() == SqlKind.FILTER);
}