Java Code Examples for org.apache.calcite.sql.fun.SqlStdOperatorTable#ITEM

The following examples show how to use org.apache.calcite.sql.fun.SqlStdOperatorTable#ITEM . 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: DrillProjectRelBase.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public Boolean visitCall(RexCall call) {
  if (call.getOperator() == SqlStdOperatorTable.ITEM) {
    final RexNode op0 = call.getOperands().get(0);
    final RexNode op1 = call.getOperands().get(1);

    if (op0 instanceof RexInputRef &&
        op1 instanceof RexLiteral && ((RexLiteral) op1).getTypeName() == SqlTypeName.CHAR) {
      return true;
    } else if (op0 instanceof RexCall &&
        op1 instanceof RexLiteral && ((RexLiteral) op1).getTypeName() == SqlTypeName.CHAR) {
      return op0.accept(this);
    }
  }

  return false;
}
 
Example 2
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
protected SqlNode expandDynamicStar(SqlIdentifier id, SqlIdentifier fqId) {
	if (DynamicRecordType.isDynamicStarColName(Util.last(fqId.names))
		&& !DynamicRecordType.isDynamicStarColName(Util.last(id.names))) {
		// Convert a column ref into ITEM(*, 'col_name')
		// for a dynamic star field in dynTable's rowType.
		SqlNode[] inputs = new SqlNode[2];
		inputs[0] = fqId;
		inputs[1] = SqlLiteral.createCharString(
			Util.last(id.names),
			id.getParserPosition());
		return new SqlBasicCall(
			SqlStdOperatorTable.ITEM,
			inputs,
			id.getParserPosition());
	}
	return fqId;
}
 
Example 3
Source File: ElasticsearchRules.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public String visitCall(RexCall call) {
  final String name = isItemCall(call);
  if (name != null) {
    return name;
  }

  final List<String> strings = visitList(call.operands);

  if (call.getKind() == SqlKind.CAST) {
    return call.getOperands().get(0).accept(this);
  }

  if (call.getOperator() == SqlStdOperatorTable.ITEM) {
    final RexNode op1 = call.getOperands().get(1);
    if (op1 instanceof RexLiteral && op1.getType().getSqlTypeName() == SqlTypeName.INTEGER) {
      return stripQuotes(strings.get(0)) + "[" + ((RexLiteral) op1).getValue2() + "]";
    }
  }
  throw new IllegalArgumentException("Translation of " + call
      + " is not supported by ElasticsearchProject");
}
 
Example 4
Source File: ElasticsearchRules.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Returns 'string' if it is a call to item['string'], null otherwise.
 * @param call current relational expression
 * @return literal value
 */
private static String isItemCall(RexCall call) {
  if (call.getOperator() != SqlStdOperatorTable.ITEM) {
    return null;
  }
  final RexNode op0 = call.getOperands().get(0);
  final RexNode op1 = call.getOperands().get(1);

  if (op0 instanceof RexInputRef
      && ((RexInputRef) op0).getIndex() == 0
      && op1 instanceof RexLiteral
      && ((RexLiteral) op1).getValue2() instanceof String) {
    return (String) ((RexLiteral) op1).getValue2();
  }
  return null;
}
 
Example 5
Source File: GeodeRules.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public String visitCall(RexCall call) {
  final List<String> strings = new ArrayList<>();
  visitList(call.operands, strings);
  if (call.getOperator() == SqlStdOperatorTable.ITEM) {
    final RexNode op1 = call.getOperands().get(1);
    if (op1 instanceof RexLiteral) {
      if (op1.getType().getSqlTypeName() == SqlTypeName.INTEGER) {
        return stripQuotes(strings.get(0)) + "[" + ((RexLiteral) op1).getValue2() + "]";
      } else if (op1.getType().getSqlTypeName() == SqlTypeName.CHAR) {
        return stripQuotes(strings.get(0)) + "." + ((RexLiteral) op1).getValue2();
      }
    }
  }

  return super.visitCall(call);
}
 
Example 6
Source File: ProjectRelBase.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public Boolean visitCall(RexCall call) {
  if (call.getOperator() == SqlStdOperatorTable.ITEM) {
    final RexNode op0 = call.getOperands().get(0);
    final RexNode op1 = call.getOperands().get(1);

    if (op0 instanceof RexInputRef &&
        op1 instanceof RexLiteral && ((RexLiteral) op1).getTypeName().getFamily() == SqlTypeFamily.CHARACTER) {
      return true;
    } else if (op0 instanceof RexCall &&
        op1 instanceof RexLiteral && ((RexLiteral) op1).getTypeName().getFamily() == SqlTypeFamily.CHARACTER) {
      return op0.accept(this);
    }
  }

  return false;
}
 
Example 7
Source File: GeodeRules.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Returns 'string' if it is a call to item['string'], null otherwise.
 */
static String isItem(RexCall call) {
  if (call.getOperator() != SqlStdOperatorTable.ITEM) {
    return null;
  }
  final RexNode op0 = call.getOperands().get(0);
  final RexNode op1 = call.getOperands().get(1);

  if (op0 instanceof RexInputRef
      && ((RexInputRef) op0).getIndex() == 0
      && op1 instanceof RexLiteral
      && ((RexLiteral) op1).getValue2() instanceof String) {
    return (String) ((RexLiteral) op1).getValue2();
  }
  return null;
}
 
Example 8
Source File: SchemaField.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RexNode visitCall(RexCall call) {
  final boolean isItem = call.getOperator().getSyntax() == SqlSyntax.SPECIAL && call.getOperator() == SqlStdOperatorTable.ITEM;
  if(isItem){
    return visitCandidate(call);
  }
  return super.visitCall(call);
}
 
Example 9
Source File: MongoRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns 'string' if it is a call to item['string'], null otherwise. */
static String isItem(RexCall call) {
  if (call.getOperator() != SqlStdOperatorTable.ITEM) {
    return null;
  }
  final RexNode op0 = call.operands.get(0);
  final RexNode op1 = call.operands.get(1);
  if (op0 instanceof RexInputRef
      && ((RexInputRef) op0).getIndex() == 0
      && op1 instanceof RexLiteral
      && ((RexLiteral) op1).getValue2() instanceof String) {
    return (String) ((RexLiteral) op1).getValue2();
  }
  return null;
}
 
Example 10
Source File: SchemaField.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public SchemaPath visitCall(RexCall call) {
  if(call.getOperator().getSyntax() != SqlSyntax.SPECIAL || call.getOperator() != SqlStdOperatorTable.ITEM){
    return null;
  }
  LogicalExpression logExpr = call.getOperands().get(0).accept(this);

  if (!(logExpr instanceof SchemaPath)) {
    return (SchemaPath) logExpr;
  }

  SchemaPath left = (SchemaPath) logExpr;
  final RexLiteral literal = (RexLiteral) call.getOperands().get(1);
  switch(literal.getTypeName()){
  case DECIMAL:
  case INTEGER:
    switch(indexMode){
    case ALLOW:
      return left.getChild(((BigDecimal)literal.getValue()).intValue());
    case SKIP:
      return left;
    case DISALLOW:
    default:
      return null;
    }

  case CHAR:
  case VARCHAR:
    return left.getChild(literal.getValue2().toString());
  default:
    // fall through
  }

  return null;
}
 
Example 11
Source File: DrillCompoundIdentifier.java    From Bats with Apache License 2.0 5 votes vote down vote up
public SqlNode getNode(SqlNode node) {
  SqlLiteral literal;
  if (isArray) {
    literal = SqlLiteral.createExactNumeric(value, parserPos);
  } else {
    literal = SqlLiteral.createCharString(value, parserPos);
  }
  return new SqlBasicCall(SqlStdOperatorTable.ITEM, new SqlNode[]{node, literal}, parserPos);
}
 
Example 12
Source File: CompoundIdentifier.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public SqlNode getNode(SqlNode node){
  SqlLiteral literal;
  if(isArray){
    literal = SqlLiteral.createExactNumeric(value, parserPos);
  }else{
    literal = SqlLiteral.createCharString(value, parserPos);
  }
  return new SqlBasicCall(SqlStdOperatorTable.ITEM, new SqlNode[]{ node, literal }, parserPos);
}
 
Example 13
Source File: ElasticsearchFilter.java    From dk-fitting with Apache License 2.0 5 votes vote down vote up
private String isItem(RexCall call) {
    if (call.getOperator() != SqlStdOperatorTable.ITEM) {
        return null;
    }
    final RexNode op0 = call.getOperands().get(0);
    final RexNode op1 = call.getOperands().get(1);

    if (op0 instanceof RexInputRef
            && ((RexInputRef) op0).getIndex() == 0
            && op1 instanceof RexLiteral
            && ((RexLiteral) op1).getValue2() instanceof String) {
        return (String) ((RexLiteral) op1).getValue2();
    }
    return null;
}
 
Example 14
Source File: ElasticsearchFilter.java    From dk-fitting with Apache License 2.0 5 votes vote down vote up
private String isItem(RexCall call) {
    if (call.getOperator() != SqlStdOperatorTable.ITEM) {
        return null;
    }
    final RexNode op0 = call.getOperands().get(0);
    final RexNode op1 = call.getOperands().get(1);

    if (op0 instanceof RexInputRef
            && ((RexInputRef) op0).getIndex() == 0
            && op1 instanceof RexLiteral
            && ((RexLiteral) op1).getValue2() instanceof String) {
        return (String) ((RexLiteral) op1).getValue2();
    }
    return null;
}
 
Example 15
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override public SqlNode visit(SqlIdentifier id) {
	// First check for builtin functions which don't have
	// parentheses, like "LOCALTIME".
	SqlCall call =
		SqlUtil.makeCall(
			validator.getOperatorTable(),
			id);
	if (call != null) {
		return call.accept(this);
	}
	final SqlIdentifier fqId = getScope().fullyQualify(id).identifier;
	SqlNode expandedExpr = fqId;
	// Convert a column ref into ITEM(*, 'col_name').
	// select col_name from (select * from dynTable)
	// SqlIdentifier "col_name" would be resolved to a dynamic star field in dynTable's rowType.
	// Expand such SqlIdentifier to ITEM operator.
	if (DynamicRecordType.isDynamicStarColName(Util.last(fqId.names))
		&& !DynamicRecordType.isDynamicStarColName(Util.last(id.names))) {
		SqlNode[] inputs = new SqlNode[2];
		inputs[0] = fqId;
		inputs[1] = SqlLiteral.createCharString(
			Util.last(id.names),
			id.getParserPosition());
		SqlBasicCall item_call = new SqlBasicCall(
			SqlStdOperatorTable.ITEM,
			inputs,
			id.getParserPosition());
		expandedExpr = item_call;
	}
	validator.setOriginal(expandedExpr, id);
	return expandedExpr;
}
 
Example 16
Source File: MapProjectionFieldVisitor.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public String visitCall(RexCall call) {
  if (call.op == SqlStdOperatorTable.ITEM) {
    return ((RexLiteral) call.getOperands().get(1)).getValueAs(String.class);
  }
  return super.visitCall(call);
}
 
Example 17
Source File: MongoRules.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public String visitCall(RexCall call) {
  String name = isItem(call);
  if (name != null) {
    return "'$" + name + "'";
  }
  final List<String> strings = visitList(call.operands);
  if (call.getKind() == SqlKind.CAST) {
    return strings.get(0);
  }
  String stdOperator = MONGO_OPERATORS.get(call.getOperator());
  if (stdOperator != null) {
    return "{" + stdOperator + ": [" + Util.commaList(strings) + "]}";
  }
  if (call.getOperator() == SqlStdOperatorTable.ITEM) {
    final RexNode op1 = call.operands.get(1);
    if (op1 instanceof RexLiteral
        && op1.getType().getSqlTypeName() == SqlTypeName.INTEGER) {
      if (!Bug.CALCITE_194_FIXED) {
        return "'" + stripQuotes(strings.get(0)) + "["
            + ((RexLiteral) op1).getValue2() + "]'";
      }
      return strings.get(0) + "[" + strings.get(1) + "]";
    }
  }
  if (call.getOperator() == SqlStdOperatorTable.CASE) {
    StringBuilder sb = new StringBuilder();
    StringBuilder finish = new StringBuilder();
    // case(a, b, c)  -> $cond:[a, b, c]
    // case(a, b, c, d) -> $cond:[a, b, $cond:[c, d, null]]
    // case(a, b, c, d, e) -> $cond:[a, b, $cond:[c, d, e]]
    for (int i = 0; i < strings.size(); i += 2) {
      sb.append("{$cond:[");
      finish.append("]}");

      sb.append(strings.get(i));
      sb.append(',');
      sb.append(strings.get(i + 1));
      sb.append(',');
      if (i == strings.size() - 3) {
        sb.append(strings.get(i + 2));
        break;
      }
      if (i == strings.size() - 2) {
        sb.append("null");
        break;
      }
    }
    sb.append(finish);
    return sb.toString();
  }
  throw new IllegalArgumentException("Translation of " + call.toString()
      + " is not supported by MongoProject");
}