Esempio n. 1
0
 @Override
 public void open() {
   TAP_OPEN.in();
   try {
     if (isSkipBinding()) {
       ValueSource value = bindings.getValue(skip());
       if (!value.isNull()) this.skipLeft = value.getInt32();
     } else {
       this.skipLeft = skip();
     }
     if (skipLeft < 0) throw new NegativeLimitException("OFFSET", skipLeft);
     if (isLimitBinding()) {
       ValueSource value = bindings.getValue(limit());
       if (value.isNull()) this.limitLeft = Integer.MAX_VALUE;
       else {
         TInstance type = MNumeric.INT.instance(true);
         TExecutionContext executionContext = new TExecutionContext(null, type, context);
         Value ivalue = new Value(MNumeric.INT.instance(true));
         MNumeric.INT.fromObject(executionContext, value, ivalue);
         this.limitLeft = ivalue.getInt32();
       }
     } else {
       this.limitLeft = limit();
     }
     if (limitLeft < 0) throw new NegativeLimitException("LIMIT", limitLeft);
     super.open();
   } finally {
     TAP_OPEN.out();
   }
 }
  private void runPlan(String expectedSQL) throws Exception {
    DumpGroupLoadablePlan loadablePlan = new DumpGroupLoadablePlan();
    DirectObjectPlan plan = loadablePlan.plan();

    Schema schema = new Schema(ais());
    StoreAdapter adapter = newStoreAdapter(schema);
    QueryContext queryContext =
        new SimpleQueryContext(adapter) {
          @Override
          public String getCurrentSchema() {
            return SCHEMA_NAME;
          }
        };
    QueryBindings queryBindings = queryContext.createBindings();
    queryBindings.setValue(0, new Value(MString.varcharFor(SCHEMA_NAME), SCHEMA_NAME));
    queryBindings.setValue(1, new Value(MString.varcharFor(groupName), groupName));
    if (multiple) queryBindings.setValue(2, new Value(MNumeric.INT.instance(false), 10));
    if (commitFreq > 0)
      queryBindings.setValue(3, new Value(MNumeric.INT.instance(false), commitFreq));

    DirectObjectCursor cursor = plan.cursor(queryContext, queryBindings);

    StringBuilder actual = new StringBuilder();

    cursor.open();
    while (true) {
      List<?> columns = cursor.next();
      if (columns == null) {
        break;
      } else if (!columns.isEmpty()) {
        assertTrue(columns.size() == 1);
        if (actual.length() > 0) actual.append("\n");
        actual.append(columns.get(0));
      }
    }
    cursor.close();

    assertEquals(expectedSQL, actual.toString());
  }
Esempio n. 3
0
 @Override
 public Operator plan() {
   // select id, value, $1 from test
   Group group = ais().getGroup("test");
   Table testTable = ais().getTable("test", "test");
   RowType testRowType = schema().tableRowType(testTable);
   return project_Default(
       groupScan_Default(group),
       Arrays.asList(
           ExpressionGenerators.field(testRowType, 0),
           ExpressionGenerators.field(testRowType, 1),
           ExpressionGenerators.variable(MNumeric.INT.instance(true), 0)),
       testRowType);
 }
Esempio n. 4
0
  /**
   * Creates a Value from the given object, deriving the type from the object.
   *
   * @throws java.lang.UnsupportedOperationException if the type cannot be inferred
   */
  public static Value fromObject(Object object) {
    final TInstance type;
    Value value = null;
    if (object == null) {
      value = new Value(null);
      value.putNull();
    } else if (object instanceof String) {
      String s = (String) object;
      type =
          MString.VARCHAR.instance(
              s.length(),
              StringFactory.DEFAULT_CHARSET.ordinal(),
              StringFactory.NULL_COLLATION_ID,
              false);
      value = new Value(type, s);
    } else if (object instanceof Long) {
      type = MNumeric.BIGINT.instance(false);
      value = new Value(type, (Long) object);
    } else if (object instanceof Integer) {
      type = MNumeric.INT.instance(false);
      value = new Value(type, (Integer) object);
    } else if (object instanceof Double) {
      type = MApproximateNumber.DOUBLE.instance(false);
      value = new Value(type, (Double) object);
    } else if (object instanceof Float) {
      type = MApproximateNumber.FLOAT.instance(false);
      value = new Value(type, (Float) object);
    } else if (object instanceof BigDecimalWrapper) {
      BigDecimalWrapper bdw = (BigDecimalWrapper) object;
      type = MNumeric.DECIMAL.instance(bdw.getPrecision(), bdw.getScale(), false);
      value = new Value(type);
      value.putObject(bdw);
    } else if (object instanceof BigDecimal) {
      BigDecimal bd = (BigDecimal) object;
      type =
          MNumeric.DECIMAL.instance(
              BigDecimalWrapperImpl.sqlPrecision(bd), BigDecimalWrapperImpl.sqlScale(bd), false);
      value = new Value(type);
      value.putObject(new BigDecimalWrapperImpl(bd));
    } else if (object instanceof ByteSource || object instanceof byte[]) {
      byte[] bytes;
      if (object instanceof byte[]) {
        bytes = (byte[]) object;
      } else {
        ByteSource source = (ByteSource) object;
        byte[] srcArray = source.byteArray();
        int offset = source.byteArrayOffset();
        int end = offset + source.byteArrayLength();
        bytes = Arrays.copyOfRange(srcArray, offset, end);
      }
      type = MBinary.VARBINARY.instance(bytes.length, false);
      value = new Value(type, bytes);
    } else if (object instanceof BigInteger) {
      type = MNumeric.BIGINT_UNSIGNED.instance(false);
      BigInteger bi = (BigInteger) object;
      value = new Value(type, bi.longValue());
    } else if (object instanceof Boolean) {
      type = AkBool.INSTANCE.instance(false);
      value = new Value(type, (Boolean) object);
    } else if (object instanceof Character) {
      type = MString.VARCHAR.instance(1, false);
      value = new Value(type, object.toString());
    } else if (object instanceof Short) {
      type = MNumeric.SMALLINT.instance(false);
      value = new Value(type, (Short) object);
    } else if (object instanceof Byte) {
      type = MNumeric.TINYINT.instance(false);
      value = new Value(type, (Byte) object);
    } else if (object instanceof UUID) {
      type = AkGUID.INSTANCE.instance(false);
      value = new Value(type);
      value.putObject(object);
    } else if (object instanceof BlobRef) {
      type = AkBlob.INSTANCE.instance(false);
      value = new Value(type);
      value.putObject(type);
    } else {
      throw new UnsupportedOperationException(
          "can't convert " + object + " of type " + object.getClass());
    }

    return value;
  }