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

The following examples show how to use org.apache.calcite.sql.SqlKind#LITERAL . 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: SqlImplementor.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public SqlNode toSql(RexProgram program, RexNode rex) {
    if (rex.getKind() == SqlKind.LITERAL) {
        final RexLiteral literal = (RexLiteral) rex;
        if (literal.getTypeName().getFamily() == SqlTypeFamily.CHARACTER) {
            return new SqlIdentifier(RexLiteral.stringValue(literal), POS);
        }
    }
    return super.toSql(program, rex);
}
 
Example 2
Source File: ElasticsearchFilter.java    From dk-fitting with Apache License 2.0 5 votes vote down vote up
private boolean translateBinary(SqlKind operator, RexNode left, RexNode right) {
    if (right.getKind() != SqlKind.LITERAL) return false;
    final RexLiteral rightLiteral = (RexLiteral) right;
    switch (left.getKind())
    {
        case INPUT_REF:
            String name = getRowType().getFieldNames().get(((RexInputRef) left).getIndex());
            translateOp(operator, name, rightLiteral);
            return true;
        case CAST:
            return translateBinary(operator, ((RexCall) left).operands.get(0), right);
        default: return false;
    }
}
 
Example 3
Source File: ElasticsearchFilter.java    From dk-fitting with Apache License 2.0 5 votes vote down vote up
private boolean translateBinary(SqlKind operator, RexNode left, RexNode right) {
    if (right.getKind() != SqlKind.LITERAL) return false;
    final RexLiteral rightLiteral = (RexLiteral) right;
    switch (left.getKind())
    {
        case INPUT_REF:
            String name = getRowType().getFieldNames().get(((RexInputRef) left).getIndex());
            translateOp(operator, name, rightLiteral);
            return true;
        case CAST:
            return translateBinary(operator, ((RexCall) left).operands.get(0), right);
        default: return false;
    }
}
 
Example 4
Source File: SqlImplementor.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public SqlNode toSql(RexProgram program, RexNode rex) {
  if (rex.getKind() == SqlKind.LITERAL) {
    final RexLiteral literal = (RexLiteral) rex;
    if (literal.getTypeName().getFamily() == SqlTypeFamily.CHARACTER) {
      return new SqlIdentifier(RexLiteral.stringValue(literal), POS);
    }
  }
  return super.toSql(program, rex);
}
 
Example 5
Source File: DremioRelToSqlConverter.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public SqlNode toSql(RexProgram program, RexNode rex) {
  if (rex.getKind() == SqlKind.LITERAL) {
    final RexLiteral literal = (RexLiteral) rex;
    if (literal.getTypeName().getFamily() == SqlTypeFamily.CHARACTER) {
      return new SqlIdentifier(RexLiteral.stringValue(literal), POS);
    }
  }
  return super.toSql(program, rex);
}
 
Example 6
Source File: DruidJsonFilter.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Nullable
private static DruidJsonFilter toInKindDruidFilter(RexNode e, RelDataType rowType,
    DruidQuery druidQuery) {
  if (e.getKind() != SqlKind.IN && e.getKind() != SqlKind.NOT_IN) {
    throw new AssertionError(
        DruidQuery.format("Expecting IN or NOT IN but got [%s]", e.getKind()));
  }
  ImmutableList.Builder<String> listBuilder = ImmutableList.builder();
  for (RexNode rexNode : ((RexCall) e).getOperands()) {
    if (rexNode.getKind() == SqlKind.LITERAL) {
      String value = toDruidLiteral(rexNode, rowType, druidQuery);
      if (value == null) {
        return null;
      }
      listBuilder.add(value);
    }
  }
  Pair<String, ExtractionFunction> druidColumn = DruidQuery
      .toDruidColumn(((RexCall) e).getOperands().get(0),
      rowType, druidQuery);
  final String columnName = druidColumn.left;
  final ExtractionFunction extractionFunction = druidColumn.right;
  if (columnName == null) {
    return null;
  }
  if (e.getKind() != SqlKind.NOT_IN) {
    return new DruidJsonFilter.JsonInFilter(columnName, listBuilder.build(), extractionFunction);
  } else {
    return toNotDruidFilter(
        new DruidJsonFilter.JsonInFilter(columnName, listBuilder.build(), extractionFunction));
  }
}
 
Example 7
Source File: SqlImplementor.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public SqlNode toSql(RexProgram program, RexNode rex) {
  if (rex.getKind() == SqlKind.LITERAL) {
    final RexLiteral literal = (RexLiteral) rex;
    if (literal.getTypeName().getFamily() == SqlTypeFamily.CHARACTER) {
      return new SqlIdentifier(RexLiteral.stringValue(literal), POS);
    }
  }
  return super.toSql(program, rex);
}
 
Example 8
Source File: RexLiteralImpl.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public SqlKind getKind() {
    return SqlKind.LITERAL;
}
 
Example 9
Source File: TypeInferenceUtils.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  SqlTypeName typeToCastTo = null;
  if (opBinding instanceof SqlCallBinding) {
    SqlCallBinding sqlCallBinding = (SqlCallBinding) opBinding;
    if (sqlCallBinding.operand(1).getKind() == SqlKind.LITERAL) {
      String type = null;
      try {
        SqlLiteral sqlLiteral = (SqlLiteral) sqlCallBinding.operand(1);
        type = ((NlsString) sqlLiteral.getValue()).getValue();
        switch(type) {
          case "JSON":
            typeToCastTo = SqlTypeName.ANY;
            break;
          case "UTF8":
          case "UTF16":
            typeToCastTo = SqlTypeName.VARCHAR;
            break;
          case "BOOLEAN_BYTE":
            typeToCastTo = SqlTypeName.BOOLEAN;
            break;
          case "TINYINT_BE":
          case "TINYINT":
            typeToCastTo = SqlTypeName.TINYINT;
            break;
          case "SMALLINT_BE":
          case "SMALLINT":
            typeToCastTo = SqlTypeName.SMALLINT;
            break;
          case "INT_BE":
          case "INT":
          case "INT_HADOOPV":
            typeToCastTo = SqlTypeName.INTEGER;
            break;
          case "BIGINT_BE":
          case "BIGINT":
          case "BIGINT_HADOOPV":
            typeToCastTo = SqlTypeName.BIGINT;
            break;
          case "FLOAT":
            typeToCastTo = SqlTypeName.FLOAT;
            break;
          case "DOUBLE":
            typeToCastTo = SqlTypeName.DOUBLE;
            break;
          case "DATE_EPOCH_BE":
          case "DATE_EPOCH":
            typeToCastTo = SqlTypeName.DATE;
            break;
          case "TIME_EPOCH_BE":
          case "TIME_EPOCH":
            typeToCastTo = SqlTypeName.TIME;
            break;
          case "TIMESTAMP_EPOCH":
          case "TIMESTAMP_IMPALA":
            typeToCastTo = SqlTypeName.TIMESTAMP;
            break;
          default:
            typeToCastTo = SqlTypeName.ANY;
            break;
        }
      } catch (final ClassCastException e) {
        logger.debug("Failed to parse string for convert_from()");
      }
    }
  }

  if (typeToCastTo == null) {
    typeToCastTo = SqlTypeName.ANY;
  }
  return factory.createTypeWithNullability(
      factory.createSqlType(typeToCastTo),
      true);
}
 
Example 10
Source File: DruidExpressions.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Translates Calcite rexNode to Druid Expression when possible
 * @param rexNode rexNode to convert to a Druid Expression
 * @param inputRowType input row type of the rexNode to translate
 * @param druidRel Druid query
 *
 * @return Druid Expression or null when can not convert the RexNode
 */
@Nullable
public static String toDruidExpression(
    final RexNode rexNode,
    final RelDataType inputRowType,
    final DruidQuery druidRel) {
  SqlKind kind = rexNode.getKind();
  SqlTypeName sqlTypeName = rexNode.getType().getSqlTypeName();

  if (kind == SqlKind.INPUT_REF) {
    final RexInputRef ref = (RexInputRef) rexNode;
    final String columnName = inputRowType.getFieldNames().get(ref.getIndex());
    if (columnName == null) {
      return null;
    }
    if (druidRel.getDruidTable().timestampFieldName.equals(columnName)) {
      return DruidExpressions.fromColumn(DruidTable.DEFAULT_TIMESTAMP_COLUMN);
    }
    return DruidExpressions.fromColumn(columnName);
  }

  if (rexNode instanceof RexCall) {
    final SqlOperator operator = ((RexCall) rexNode).getOperator();
    final DruidSqlOperatorConverter conversion = druidRel.getOperatorConversionMap()
        .get(operator);
    if (conversion == null) {
      //unknown operator can not translate
      return null;
    } else {
      return conversion.toDruidExpression(rexNode, inputRowType, druidRel);
    }
  }
  if (kind == SqlKind.LITERAL) {
    // Translate literal.
    if (RexLiteral.isNullLiteral(rexNode)) {
      //case the filter/project might yield to unknown let Calcite deal with this for now
      return null;
    } else if (SqlTypeName.NUMERIC_TYPES.contains(sqlTypeName)) {
      return DruidExpressions.numberLiteral((Number) RexLiteral
          .value(rexNode));
    } else if (SqlTypeFamily.INTERVAL_DAY_TIME == sqlTypeName.getFamily()) {
      // Calcite represents DAY-TIME intervals in milliseconds.
      final long milliseconds = ((Number) RexLiteral.value(rexNode)).longValue();
      return DruidExpressions.numberLiteral(milliseconds);
    } else if (SqlTypeFamily.INTERVAL_YEAR_MONTH == sqlTypeName.getFamily()) {
      // Calcite represents YEAR-MONTH intervals in months.
      final long months = ((Number) RexLiteral.value(rexNode)).longValue();
      return DruidExpressions.numberLiteral(months);
    } else if (SqlTypeName.STRING_TYPES.contains(sqlTypeName)) {
      return
          DruidExpressions.stringLiteral(RexLiteral.stringValue(rexNode));
    } else if (SqlTypeName.DATE == sqlTypeName
        || SqlTypeName.TIMESTAMP == sqlTypeName
        || SqlTypeName.TIME_WITH_LOCAL_TIME_ZONE == sqlTypeName) {
      return DruidExpressions.numberLiteral(
          DruidDateTimeUtils.literalValue(rexNode));
    } else if (SqlTypeName.BOOLEAN == sqlTypeName) {
      return DruidExpressions.numberLiteral(RexLiteral.booleanValue(rexNode) ? 1 : 0);
    }
  }
  // Not Literal/InputRef/RexCall or unknown type?
  return null;
}
 
Example 11
Source File: RexLiteral.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public SqlKind getKind() {
  return SqlKind.LITERAL;
}