Java Code Examples for org.apache.calcite.sql.SqlNodeList#getList()

The following examples show how to use org.apache.calcite.sql.SqlNodeList#getList() . 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: SqlCase.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a call to the switched form of the case operator, viz:
 *
 * <blockquote><code>CASE value<br>
 * WHEN whenList[0] THEN thenList[0]<br>
 * WHEN whenList[1] THEN thenList[1]<br>
 * ...<br>
 * ELSE elseClause<br>
 * END</code></blockquote>
 */
public static SqlCase createSwitched(SqlParserPos pos, SqlNode value,
    SqlNodeList whenList, SqlNodeList thenList, SqlNode elseClause) {
  if (null != value) {
    List<SqlNode> list = whenList.getList();
    for (int i = 0; i < list.size(); i++) {
      SqlNode e = list.get(i);
      final SqlCall call;
      if (e instanceof SqlNodeList) {
        call = SqlStdOperatorTable.IN.createCall(pos, value, e);
      } else {
        call = SqlStdOperatorTable.EQUALS.createCall(pos, value, e);
      }
      list.set(i, call);
    }
  }

  if (null == elseClause) {
    elseClause = SqlLiteral.createNull(pos);
  }

  return new SqlCase(pos, null, whenList, thenList, elseClause);
}
 
Example 2
Source File: SqlShuttle.java    From Bats with Apache License 2.0 6 votes vote down vote up
public SqlNode visit(SqlNodeList nodeList) {
  boolean update = false;
  List<SqlNode> exprs = nodeList.getList();
  int exprCount = exprs.size();
  List<SqlNode> newList = new ArrayList<>(exprCount);
  for (SqlNode operand : exprs) {
    SqlNode clonedOperand;
    if (operand == null) {
      clonedOperand = null;
    } else {
      clonedOperand = operand.accept(this);
      if (clonedOperand != operand) {
        update = true;
      }
    }
    newList.add(clonedOperand);
  }
  if (update) {
    return new SqlNodeList(newList, nodeList.getParserPosition());
  } else {
    return nodeList;
  }
}
 
Example 3
Source File: SqlCreateHiveView.java    From flink with Apache License 2.0 6 votes vote down vote up
public SqlCreateHiveView(SqlParserPos pos, SqlIdentifier viewName, SqlNodeList fieldList, SqlNode query,
		boolean ifNotExists, SqlCharStringLiteral comment, SqlNodeList properties) {
	super(
			pos,
			viewName,
			fieldList,
			query,
			false,
			false,
			ifNotExists,
			HiveDDLUtils.unescapeStringLiteral(comment),
			properties
	);
	HiveDDLUtils.unescapeProperties(properties);
	originPropList = new SqlNodeList(properties.getList(), properties.getParserPosition());
	// mark it as a hive view
	properties.add(HiveDDLUtils.toTableOption(CatalogConfig.IS_GENERIC, "false", pos));
}
 
Example 4
Source File: SqlCreateHiveDatabase.java    From flink with Apache License 2.0 6 votes vote down vote up
public SqlCreateHiveDatabase(SqlParserPos pos, SqlIdentifier databaseName, SqlNodeList propertyList,
		SqlCharStringLiteral comment, SqlCharStringLiteral location, boolean ifNotExists) throws ParseException {
	super(
			pos,
			databaseName,
			HiveDDLUtils.checkReservedDBProperties(propertyList),
			HiveDDLUtils.unescapeStringLiteral(comment),
			ifNotExists
	);
	HiveDDLUtils.ensureNonGeneric(propertyList);
	originPropList = new SqlNodeList(propertyList.getList(), propertyList.getParserPosition());
	// mark it as a hive database
	propertyList.add(HiveDDLUtils.toTableOption(CatalogConfig.IS_GENERIC, "false", pos));
	if (location != null) {
		propertyList.add(new SqlTableOption(
				SqlLiteral.createCharString(DATABASE_LOCATION_URI, location.getParserPosition()),
				location,
				location.getParserPosition()));
	}
	this.location = location;
}
 
Example 5
Source File: SqlShuttle.java    From calcite with Apache License 2.0 6 votes vote down vote up
public SqlNode visit(SqlNodeList nodeList) {
  boolean update = false;
  List<SqlNode> exprs = nodeList.getList();
  int exprCount = exprs.size();
  List<SqlNode> newList = new ArrayList<>(exprCount);
  for (SqlNode operand : exprs) {
    SqlNode clonedOperand;
    if (operand == null) {
      clonedOperand = null;
    } else {
      clonedOperand = operand.accept(this);
      if (clonedOperand != operand) {
        update = true;
      }
    }
    newList.add(clonedOperand);
  }
  if (update) {
    return new SqlNodeList(newList, nodeList.getParserPosition());
  } else {
    return nodeList;
  }
}
 
Example 6
Source File: SqlCase.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a call to the switched form of the case operator, viz:
 *
 * <blockquote><code>CASE value<br>
 * WHEN whenList[0] THEN thenList[0]<br>
 * WHEN whenList[1] THEN thenList[1]<br>
 * ...<br>
 * ELSE elseClause<br>
 * END</code></blockquote>
 */
public static SqlCase createSwitched(SqlParserPos pos, SqlNode value,
    SqlNodeList whenList, SqlNodeList thenList, SqlNode elseClause) {
  if (null != value) {
    List<SqlNode> list = whenList.getList();
    for (int i = 0; i < list.size(); i++) {
      SqlNode e = list.get(i);
      final SqlCall call;
      if (e instanceof SqlNodeList) {
        call = SqlStdOperatorTable.IN.createCall(pos, value, e);
      } else {
        call = SqlStdOperatorTable.EQUALS.createCall(pos, value, e);
      }
      list.set(i, call);
    }
  }

  if (null == elseClause) {
    elseClause = SqlLiteral.createNull(pos);
  }

  return new SqlCase(pos, null, whenList, thenList, elseClause);
}
 
Example 7
Source File: RefreshMetadataHandler.java    From Bats with Apache License 2.0 5 votes vote down vote up
private Set<String> getColumnRootSegments(SqlNodeList columnList) {
  Set<String> columnSet = new HashSet<>();
  if (columnList != null) {
    for (SqlNode column : columnList.getList()) {
      // Add only the root segment. Collect metadata for all the columns under that root segment
      columnSet.add(SchemaPath.parseFromString(column.toString()).getRootSegmentPath());
    }
  }
  return columnSet;
}
 
Example 8
Source File: SideParser.java    From alchemy with Apache License 2.0 5 votes vote down vote up
/**
 *  select a.name , a.age , FUN(a.weight) as weight from test  -->  { name , age , weight}
 * @param selectList
 * @return
 */
public static List<String> findSelectField(SqlNodeList selectList){
    List<SqlNode> nodes = selectList.getList();
    List<String> fields = new ArrayList<>();
    for (SqlNode node : nodes){
        SqlKind kind = node.getKind();
        String field;
        switch (kind){
            case AS:
                SqlBasicCall call = (SqlBasicCall) node;
                field = findField(call.operand(0));
                break;
            case IDENTIFIER:
                field = findField(node);
                break;
            default:
                throw new UnsupportedOperationException("Don't supported findSelectField in" + node);
        }
        if (StringUtils.isEmpty(field)){
            // a.*
            return Collections.emptyList();
        }else{
            fields.add(field);
        }
    }
    return fields;
}
 
Example 9
Source File: SqlCreateReflection.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private List<NameAndMeasures> toNameAndMeasures(SqlNodeList list){
  if(list == null){
    return ImmutableList.of();
  }
  List<NameAndMeasures> columnNames = Lists.newArrayList();
  for(SqlNode node : list.getList()) {
    IdentifierWithMeasures ident = (IdentifierWithMeasures) node;
    NameAndMeasures value = new NameAndMeasures(ident.getSimple(), ident.getMeasureTypes());
    columnNames.add(value);
  }
  return columnNames;
}
 
Example 10
Source File: SqlCreateReflection.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private List<NameAndGranularity> toNameAndGranularity(SqlNodeList list){
  if(list == null){
    return ImmutableList.of();
  }
  List<NameAndGranularity> columnNames = Lists.newArrayList();
  for(SqlNode node : list.getList()) {
    IdentifierWithGranularity ident = (IdentifierWithGranularity) node;
    NameAndGranularity value = new NameAndGranularity(ident.getSimple(), ident.getByDay() ? Granularity.BY_DAY : Granularity.NORMAL);
    columnNames.add(value);

  }
  return columnNames;
}
 
Example 11
Source File: SqlCreateReflection.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private List<String> toStrings(SqlNodeList list){
  if(list == null){
    return ImmutableList.of();
  }
  List<String> columnNames = Lists.newArrayList();
  for(SqlNode node : list.getList()) {
    columnNames.add(node.toString());
  }
  return columnNames;
}
 
Example 12
Source File: SqlHandlerUtil.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * create sql column declarations from SqlNodeList
 */
public static List<SqlColumnDeclaration> columnDeclarationsFromSqlNodes(SqlNodeList columnList, String sql) {
  List<SqlColumnDeclaration> columnDeclarations = new ArrayList<>();
  for (SqlNode node : columnList.getList()) {
    if (node instanceof SqlColumnDeclaration) {
      columnDeclarations.add((SqlColumnDeclaration) node);
    } else {
      throw SqlExceptionHelper.parseError("Column type not specified", sql, node.getParserPosition()).buildSilently();
    }
  }
  return columnDeclarations;
}
 
Example 13
Source File: SqlValidatorUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Converts a list of extended columns
 * (of the form [name0, type0, name1, type1, ...])
 * into a list of (name, type) pairs. */
private static List<Pair<SqlIdentifier, SqlDataTypeSpec>> pairs(
    SqlNodeList extendedColumns) {
  final List list = extendedColumns.getList();
  //noinspection unchecked
  return Util.pairs(list);
}
 
Example 14
Source File: ExpressionGenerator.java    From streamline with Apache License 2.0 5 votes vote down vote up
@Override
public Expression visit(SqlNodeList nodeList) {
    List<Expression> expressions = new ArrayList<>();
    for (SqlNode node : nodeList.getList()) {
        expressions.add(node.accept(this));
    }
    return new ExpressionList(expressions);
}
 
Example 15
Source File: SqlValidatorUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Converts a list of extended columns
 * (of the form [name0, type0, name1, type1, ...])
 * into a list of (name, type) pairs. */
private static List<Pair<SqlIdentifier, SqlDataTypeSpec>> pairs(
    SqlNodeList extendedColumns) {
  final List list = extendedColumns.getList();
  //noinspection unchecked
  return Util.pairs(list);
}
 
Example 16
Source File: CalciteSqlParser.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
private static Expression toExpression(SqlNode node) {
  LOGGER.debug("Current processing SqlNode: {}, node.getKind(): {}", node, node.getKind());
  switch (node.getKind()) {
    case IDENTIFIER:
      if (((SqlIdentifier) node).isStar()) {
        return RequestUtils.getIdentifierExpression("*");
      }
      if (((SqlIdentifier) node).isSimple()) {
        return RequestUtils.getIdentifierExpression(((SqlIdentifier) node).getSimple());
      }
      return RequestUtils.getIdentifierExpression(node.toString());
    case LITERAL:
      return RequestUtils.getLiteralExpression((SqlLiteral) node);
    case AS:
      SqlBasicCall asFuncSqlNode = (SqlBasicCall) node;
      final Expression asFuncExpr = RequestUtils.getFunctionExpression(SqlKind.AS.toString());
      asFuncExpr.getFunctionCall().addToOperands(toExpression(asFuncSqlNode.getOperands()[0]));
      SqlNode aliasSqlNode = asFuncSqlNode.getOperands()[1];
      String aliasName;
      switch (aliasSqlNode.getKind()) {
        case IDENTIFIER:
          aliasName = ((SqlIdentifier) aliasSqlNode).getSimple();
          break;
        case LITERAL:
          aliasName = ((SqlLiteral) aliasSqlNode).toValue();
          break;
        default:
          throw new SqlCompilationException("Unsupported Alias sql node - " + aliasSqlNode);
      }
      asFuncExpr.getFunctionCall().addToOperands(RequestUtils.getIdentifierExpression(aliasName));
      return asFuncExpr;
    case CASE:
      // CASE WHEN Statement is model as a function with variable length parameters.
      // Assume N is number of WHEN Statements, total number of parameters is (2 * N + 1).
      // - N: Convert each WHEN Statement into a function Expression;
      // - N: Convert each THEN Statement into an Expression;
      // - 1: Convert ELSE Statement into an Expression.
      SqlCase caseSqlNode = (SqlCase) node;
      SqlNodeList whenOperands = caseSqlNode.getWhenOperands();
      SqlNodeList thenOperands = caseSqlNode.getThenOperands();
      SqlNode elseOperand = caseSqlNode.getElseOperand();
      Expression caseFuncExpr = RequestUtils.getFunctionExpression(SqlKind.CASE.name());
      for (SqlNode whenSqlNode : whenOperands.getList()) {
        Expression whenExpression = toExpression(whenSqlNode);
        if (isAggregateExpression(whenExpression)) {
          throw new SqlCompilationException(
              "Aggregation functions inside WHEN Clause is not supported - " + whenSqlNode);
        }
        caseFuncExpr.getFunctionCall().addToOperands(whenExpression);
      }
      for (SqlNode thenSqlNode : thenOperands.getList()) {
        Expression thenExpression = toExpression(thenSqlNode);
        if (isAggregateExpression(thenExpression)) {
          throw new SqlCompilationException(
              "Aggregation functions inside THEN Clause is not supported - " + thenSqlNode);
        }
        caseFuncExpr.getFunctionCall().addToOperands(thenExpression);
      }
      Expression elseExpression = toExpression(elseOperand);
      if (isAggregateExpression(elseExpression)) {
        throw new SqlCompilationException(
            "Aggregation functions inside ELSE Clause is not supported - " + elseExpression);
      }
      caseFuncExpr.getFunctionCall().addToOperands(elseExpression);
      return caseFuncExpr;
    case OTHER:
      if (node instanceof SqlDataTypeSpec) {
        // This is to handle expression like: CAST(col AS INT)
        return RequestUtils.getLiteralExpression(((SqlDataTypeSpec) node).getTypeName().getSimple());
      } else {
        // Move on to process default logic.
      }
    default:
      return compileFunctionExpression((SqlBasicCall) node);
  }
}
 
Example 17
Source File: SqlAlterHiveDatabase.java    From flink with Apache License 2.0 4 votes vote down vote up
public SqlAlterHiveDatabase(SqlParserPos pos, SqlIdentifier databaseName, SqlNodeList propertyList) {
	super(pos, databaseName, propertyList);
	originPropList = new SqlNodeList(propertyList.getList(), propertyList.getParserPosition());
	propertyList.add(HiveDDLUtils.toTableOption(ALTER_DATABASE_OP, getAlterOp().name(), pos));
}
 
Example 18
Source File: QuerySemantics.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private static List<Order> extractOrders(SqlNodeList orderBy, final FromNode from) {
  List<Order> orders = new ArrayList<>();
  if (orderBy != null) {
    for (SqlNode sqlNode : orderBy.getList()) {
      orders.add(sqlNode.accept(new BaseSqlVisitor<Order>() {
        @Override
        public Order visit(SqlIdentifier id) {
          return Order.newBuilder()
            .setName(idToRef(from, id))
            .setDirection(OrderDirection.ASC)
            .build();
        }
        @Override
        public Order visit(SqlCall call) {
          switch (call.getOperator().getKind()) {
            // there's no ASCENDING. It always fall in the id case above
            case DESCENDING:
              List<SqlNode> operandList = call.getOperandList();
              if (operandList.size() != 1) {
                throw new UnsupportedOperationException("Unexpected DESC operands in order clause:\n" + SqlNodes.toTreeString(call));
              }
              SqlNode operand = operandList.get(0);
              if (operand.getKind() == IDENTIFIER) {
                return Order.newBuilder()
                  .setName(idToRef(from, (SqlIdentifier)operand))
                  .setDirection(OrderDirection.DESC)
                  .build();
              } else {
                throw new UnsupportedOperationException("Unexpected DESC operand in order clause:\n" + SqlNodes.toTreeString(call));
              }
            default:
              throw new UnsupportedOperationException("Unexpected SqlOperatorImpl in order clause:\n" + SqlNodes.toTreeString(call));
          }
        }
      }));
    }
  }
  if (orders.size() == 0) {
    return null;
  }
  return orders;
}