org.apache.kudu.client.KuduPredicate Java Examples

The following examples show how to use org.apache.kudu.client.KuduPredicate. 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: KuduIOIT.java    From beam with Apache License 2.0 6 votes vote down vote up
private void runReadWithPredicates() {
  PCollection<String> output =
      readPipeline.apply(
          "Read with predicates",
          KuduIO.<String>read()
              .withMasterAddresses(options.getKuduMasterAddresses())
              .withTable(options.getKuduTable())
              .withParseFn(
                  (SerializableFunction<RowResult, String>) input -> input.getString(COL_NAME))
              .withPredicates(
                  Arrays.asList(
                      KuduPredicate.newComparisonPredicate(
                          SCHEMA.getColumn(COL_ID), KuduPredicate.ComparisonOp.GREATER_EQUAL, 2),
                      KuduPredicate.newComparisonPredicate(
                          SCHEMA.getColumn(COL_ID), KuduPredicate.ComparisonOp.LESS, 7)))
              .withCoder(StringUtf8Coder.of()));

  output.apply(Count.globally());

  PAssert.thatSingleton(output.apply("Count", Count.globally())).isEqualTo((long) 5);

  readPipeline.run().waitUntilFinish();
}
 
Example #2
Source File: KuduOperations.java    From geowave with Apache License 2.0 6 votes vote down vote up
public List<Delete> getDeletions(
    final KuduTable table,
    final List<KuduPredicate> predicates,
    final Function<RowResult, PersistentKuduRow> adapter) throws KuduException {
  // TODO: Kudu Java API does not support deleting with predicates, so we first perform a scan and
  // then perform individual row deletions with the full primary key. This is inefficient, because
  // we need to read in entire rows in order to perform deletions.
  final KuduScannerBuilder scannerBuilder = getScannerBuilder(table);
  for (final KuduPredicate pred : predicates) {
    scannerBuilder.addPredicate(pred);
  }
  final KuduScanner scanner = scannerBuilder.build();
  final List<RowResultIterator> allResults = new ArrayList<>();
  while (scanner.hasMoreRows()) {
    allResults.add(scanner.nextRows());
  }
  final Iterator<Delete> deletions =
      Streams.stream(Iterators.concat(allResults.iterator())).map(result -> {
        final PersistentKuduRow row = adapter.apply(result);
        final Delete delete = table.newDelete();
        row.populatePartialRowPrimaryKey(delete.getRow());
        return delete;
      }).iterator();
  return Lists.newArrayList(deletions);
}
 
Example #3
Source File: KuduFilterInfo.java    From flink-learning with Apache License 2.0 6 votes vote down vote up
public KuduPredicate toPredicate(ColumnSchema column) {
    KuduPredicate predicate;
    switch (this.type) {
        case IS_IN:
            predicate = KuduPredicate.newInListPredicate(column, (List<?>) this.value);
            break;
        case IS_NULL:
            predicate = KuduPredicate.newIsNullPredicate(column);
            break;
        case IS_NOT_NULL:
            predicate = KuduPredicate.newIsNotNullPredicate(column);
            break;
        default:
            predicate = predicateComparator(column);
            break;
    }
    return predicate;
}
 
Example #4
Source File: KuduClientTestCommons.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
protected void lookUpAndPopulateRecord(UnitTestTablePojo keyInfo) throws Exception
{
  KuduScanner scanner = kuduClient.newScannerBuilder(kuduTable)
      .addPredicate(KuduPredicate.newComparisonPredicate(columnDefs.get("introwkey"),
      KuduPredicate.ComparisonOp.EQUAL,keyInfo.getIntrowkey()))
      .addPredicate(KuduPredicate.newComparisonPredicate(columnDefs.get("stringrowkey"),
      KuduPredicate.ComparisonOp.EQUAL,keyInfo.getStringrowkey()))
      .addPredicate(KuduPredicate.newComparisonPredicate(columnDefs.get("timestamprowkey"),
      KuduPredicate.ComparisonOp.EQUAL,keyInfo.getTimestamprowkey()))
      .build();
  RowResultIterator rowResultItr = scanner.nextRows();
  while (rowResultItr.hasNext()) {
    RowResult thisRow = rowResultItr.next();
    keyInfo.setFloatdata(thisRow.getFloat("floatdata"));
    keyInfo.setBooldata(thisRow.getBoolean("booldata"));
    keyInfo.setBinarydata(thisRow.getBinary("binarydata"));
    keyInfo.setLongdata(thisRow.getLong("longdata"));
    keyInfo.setTimestampdata(thisRow.getLong("timestampdata"));
    keyInfo.setStringdata("stringdata");
    break;
  }
}
 
Example #5
Source File: KuduServiceImpl.java    From beam with Apache License 2.0 6 votes vote down vote up
/** Configures the scanner builder to conform to the spec. */
private static <T2> void configureBuilder(
    KuduIO.Read<T2> spec, Schema schema, AbstractKuduScannerBuilder builder) {
  builder.cacheBlocks(true); // as per kudu-spark
  if (spec.getBatchSize() != null) {
    builder.batchSizeBytes(spec.getBatchSize());
  }
  if (spec.getProjectedColumns() != null) {
    builder.setProjectedColumnNames(spec.getProjectedColumns());
  }
  if (spec.getFaultTolerent() != null) {
    builder.setFaultTolerant(spec.getFaultTolerent());
  }
  if (spec.getSerializablePredicates() != null) {
    for (Common.ColumnPredicatePB predicate : spec.getSerializablePredicates()) {
      builder.addPredicate(KuduPredicate.fromPB(schema, predicate));
    }
  }
}
 
Example #6
Source File: KuduFilterInfo.java    From flink-learning with Apache License 2.0 6 votes vote down vote up
public KuduPredicate toPredicate(ColumnSchema column) {
    KuduPredicate predicate;
    switch (this.type) {
        case IS_IN:
            predicate = KuduPredicate.newInListPredicate(column, (List<?>) this.value);
            break;
        case IS_NULL:
            predicate = KuduPredicate.newIsNullPredicate(column);
            break;
        case IS_NOT_NULL:
            predicate = KuduPredicate.newIsNotNullPredicate(column);
            break;
        default:
            predicate = predicateComparator(column);
            break;
    }
    return predicate;
}
 
Example #7
Source File: KuduFilterInfo.java    From bahir-flink with Apache License 2.0 6 votes vote down vote up
public KuduPredicate toPredicate(ColumnSchema column) {
    KuduPredicate predicate;
    switch (this.type) {
        case IS_IN:
            predicate = KuduPredicate.newInListPredicate(column, (List<?>) this.value);
            break;
        case IS_NULL:
            predicate = KuduPredicate.newIsNullPredicate(column);
            break;
        case IS_NOT_NULL:
            predicate = KuduPredicate.newIsNotNullPredicate(column);
            break;
        default:
            predicate = predicateComparator(column);
            break;
    }
    return predicate;
}
 
Example #8
Source File: KuduSQLParseTreeListener.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
@Override
public void exitFILTER_COMPARISION_EXP(KuduSQLExpressionParser.FILTER_COMPARISION_EXPContext ctx)
{
  super.exitFILTER_COMPARISION_EXP(ctx);
  String columnName = extractColumnNameFromContext(ctx.idorcolumnname());
  if (columnSchemaLookups.containsKey(columnName)) {
    if (ctx.comparisionoperator().EQUAL_TO() != null) {
      kuduPredicateList.add(buildKuduPredicate(columnName,KuduPredicate.ComparisonOp.EQUAL,ctx.anyvalue()));
    }
    if (ctx.comparisionoperator().GREATER_THAN() != null) {
      kuduPredicateList.add(buildKuduPredicate(columnName,KuduPredicate.ComparisonOp.GREATER,ctx.anyvalue()));
    }
    if (ctx.comparisionoperator().LESSER_THAN() != null) {
      kuduPredicateList.add(buildKuduPredicate(columnName,KuduPredicate.ComparisonOp.LESS,ctx.anyvalue()));
    }
    if (ctx.comparisionoperator().GREATER_THAN_OR_EQUAL() != null) {
      kuduPredicateList.add(buildKuduPredicate(columnName,KuduPredicate.ComparisonOp.GREATER_EQUAL,ctx.anyvalue()));
    }
    if (ctx.comparisionoperator().LESSER_THAN_OR_EQUAL() != null) {
      kuduPredicateList.add(buildKuduPredicate(columnName,KuduPredicate.ComparisonOp.LESS_EQUAL,ctx.anyvalue()));
    }
  } else {
    LOG.error(columnName + " is not a valid column name for this kudu table");
    isSuccessfullyParsed = false;
  }
}
 
Example #9
Source File: KuduLookupService.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<Record> lookup(Map<String, Object> coordinates) {

    //Scanner
    KuduScanner.KuduScannerBuilder builder = kuduClient.newScannerBuilder(table);

    builder.setProjectedColumnNames(columnNames);
    builder.replicaSelection(replicaSelection);

    //Only expecting one match
    builder.limit(1);

    coordinates.forEach((key,value)->
            builder.addPredicate(KuduPredicate.newComparisonPredicate(tableSchema.getColumn(key), KuduPredicate.ComparisonOp.EQUAL, value))
    );

    KuduScanner kuduScanner = builder.build();

    //Run lookup
    for ( RowResult row : kuduScanner){
        final Map<String, Object> values = new HashMap<>();
        for(String columnName : columnNames){
            Object object;
            if(row.getColumnType(columnName) == Type.BINARY){
                object = Base64.getEncoder().encodeToString(row.getBinaryCopy(columnName));
            } else {
                object = row.getObject(columnName);
            }
            values.put(columnName, object);
        }
        return Optional.of(new MapRecord(resultSchema, values));
    }

    //No match
    return Optional.empty();
}
 
Example #10
Source File: Tags.java    From kudu-ts with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves the tagset IDs of all tagsets which contain the specified tag.
 * The tagset IDs are returned in sorted order.
 *
 * @param key the tag key
 * @param value the tag value
 * @return the sorted tagset IDs
 */
public Deferred<IntVec> getTagsetIDsForTag(final String key, final String value) {
  AsyncKuduScanner.AsyncKuduScannerBuilder scan = client.newScannerBuilder(table);
  scan.addPredicate(KuduPredicate.newComparisonPredicate(Tables.TAGS_KEY_COLUMN,
                                                         ComparisonOp.EQUAL, key));
  scan.addPredicate(KuduPredicate.newComparisonPredicate(Tables.TAGS_VALUE_COLUMN,
                                                         ComparisonOp.EQUAL, value));
  scan.setProjectedColumnIndexes(TAGSET_ID_PROJECTION);
  final AsyncKuduScanner scanner = scan.build();

  class GetTagCB implements Callback<Deferred<IntVec>, RowResultIterator> {
    private final IntVec tagsetIDs = IntVec.create();
    @Override
    public Deferred<IntVec> call(RowResultIterator results) {
      for (RowResult result : results) {
        tagsetIDs.push(result.getInt(0));
      }
      if (scanner.hasMoreRows()) {
        return scanner.nextRows().addCallbackDeferring(this);
      }
      // The Kudu java client doesn't yet allow us to specify a sorted
      // (fault-tolerant) scan, so have to sort manually.
      tagsetIDs.sort();
      return Deferred.fromResult(tagsetIDs);
    }
    @Override
    public String toString() {
      return MoreObjects.toStringHelper(this).add("key", key).add("value", value).toString();
    }
  }

  return scanner.nextRows().addCallbackDeferring(new GetTagCB());
}
 
Example #11
Source File: KuduIO.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Filters the rows read from Kudu using the given predicates. */
public Read<T> withPredicates(List<KuduPredicate> predicates) {
  checkArgument(predicates != null, "predicates cannot be null");
  // reuse the kudu protobuf serialization mechanism
  List<Common.ColumnPredicatePB> serializablePredicates =
      predicates.stream().map(KuduPredicate::toPB).collect(Collectors.toList());
  return builder().setSerializablePredicates(serializablePredicates).build();
}
 
Example #12
Source File: KuduOutput.java    From envelope with Apache License 2.0 5 votes vote down vote up
private KuduScanner scannerForFilters(Iterable<Row> filters, KuduTable table) throws KuduException {
  List<Row> filtersList = Lists.newArrayList(filters);

  if (filtersList.size() == 0) {
    throw new RuntimeException("Kudu existing filter was not provided.");
  }
  
  if (filtersList.get(0).schema() == null) {
    throw new RuntimeException("Kudu existing filter did not contain a schema.");
  }
  
  if (hasAccumulators()) {
    accumulators.getLongAccumulators().get(ACCUMULATOR_NUMBER_OF_SCANNERS).add(1);
    accumulators.getLongAccumulators().get(ACCUMULATOR_NUMBER_OF_FILTERS_SCANNED).add(filtersList.size());
  }
  
  KuduScannerBuilder builder = getConnection().getClient().newScannerBuilder(table);

  for (String fieldName : filtersList.get(0).schema().fieldNames()) {
    ColumnSchema columnSchema = table.getSchema().getColumn(fieldName);

    List<Object> columnValues = Lists.newArrayList();
    for (Row filter : filtersList) {
      Object columnValue = filter.getAs(fieldName);
      columnValues.add(columnValue);
    }

    KuduPredicate predicate = KuduPredicate.newInListPredicate(columnSchema, columnValues);

    builder = builder.addPredicate(predicate);
  }

  KuduScanner scanner = builder.build();

  return scanner;
}
 
Example #13
Source File: Tagsets.java    From kudu-ts with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an {@link AsyncKuduScanner} over the tagset table beginning with
 * the specified ID.
 *
 * @param id the ID to begin scanning from
 * @return the scanner
 */
private AsyncKuduScanner tagsetScanner(int id) {
  AsyncKuduScanner.AsyncKuduScannerBuilder scanBuilder = client.newScannerBuilder(tagsetsTable);
  scanBuilder.addPredicate(KuduPredicate.newComparisonPredicate(Tables.TAGSETS_ID_COLUMN,
                                                                ComparisonOp.GREATER_EQUAL,
                                                                id));
  if (id < Integer.MAX_VALUE - TAGSETS_PER_SCAN) {
    scanBuilder.addPredicate(KuduPredicate.newComparisonPredicate(Tables.TAGSETS_ID_COLUMN,
                                                                  ComparisonOp.LESS,
                                                                  id + TAGSETS_PER_SCAN));
  }
  scanBuilder.setProjectedColumnIndexes(columnIndexes);
  AbstractionBulldozer.sortResultsByPrimaryKey(scanBuilder);
  return scanBuilder.build();
}
 
Example #14
Source File: NativeKuduClientSession.java    From presto-kudu with Apache License 2.0 5 votes vote down vote up
private KuduPredicate createComparisonPredicate(ColumnSchema columnSchema,
                                                KuduPredicate.ComparisonOp op,
                                                Object value) {
    com.facebook.presto.spi.type.Type type = TypeHelper.fromKuduColumn(columnSchema);
    Object javaValue = TypeHelper.getJavaValue(type, value);
    if (javaValue instanceof Long) {
        return KuduPredicate.newComparisonPredicate(columnSchema, op, (Long) javaValue);
    } else if (javaValue instanceof Integer) {
        return KuduPredicate.newComparisonPredicate(columnSchema, op, (Integer) javaValue);
    } else if (javaValue instanceof Short) {
        return KuduPredicate.newComparisonPredicate(columnSchema, op, (Short) javaValue);
    } else if (javaValue instanceof Byte) {
        return KuduPredicate.newComparisonPredicate(columnSchema, op, (Byte) javaValue);
    } else if (javaValue instanceof String) {
        return KuduPredicate.newComparisonPredicate(columnSchema, op, (String) javaValue);
    } else if (javaValue instanceof Double) {
        return KuduPredicate.newComparisonPredicate(columnSchema, op, (Double) javaValue);
    } else if (javaValue instanceof Float) {
        return KuduPredicate.newComparisonPredicate(columnSchema, op, (Float) javaValue);
    } else if (javaValue instanceof Boolean) {
        return KuduPredicate.newComparisonPredicate(columnSchema, op, (Boolean) javaValue);
    } else if (javaValue instanceof byte[]) {
        return KuduPredicate.newComparisonPredicate(columnSchema, op, (byte[]) javaValue);
    } else if (javaValue == null) {
        throw new IllegalStateException("Unexpected null java value for column " + columnSchema.getName());
    } else {
        throw new IllegalStateException("Unexpected java value for column "
                + columnSchema.getName() + ": " + javaValue + "(" + javaValue.getClass() + ")");
    }
}
 
Example #15
Source File: KuduSQLParseTreeListener.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@Override
public void exitIS_NULL_FILTER_EXP(KuduSQLExpressionParser.IS_NULL_FILTER_EXPContext ctx)
{
  super.exitIS_NULL_FILTER_EXP(ctx);
  String columnName = extractColumnNameFromContext(ctx.idorcolumnname());
  if (columnSchemaLookups.containsKey(columnName)) {
    kuduPredicateList.add(KuduPredicate.newIsNullPredicate(columnSchemaLookups.get(columnName)));
  } else {
    LOG.error(columnName + " is not a valid column name for this kudu table");
    isSuccessfullyParsed = false;
  }

}
 
Example #16
Source File: NativeKuduClientSession.java    From presto-kudu with Apache License 2.0 5 votes vote down vote up
@Override
public List<String> listSchemaNames() {
    try {
        if (rawSchemasTable == null) {
            if (!client.tableExists(rawSchemasTableName)) {
                createAndFillSchemasTable();
            }
            rawSchemasTable = getSchemasTable();
        }

        ColumnSchema tenantColumn = rawSchemasTable.getSchema().getColumnByIndex(0);
        KuduScanner scanner = client.newScannerBuilder(rawSchemasTable)
                .addPredicate(KuduPredicate.newComparisonPredicate(tenantColumn, KuduPredicate.ComparisonOp.EQUAL, tenantPrefix))
                .setProjectedColumnIndexes(ImmutableList.of(1))
                .build();
        RowResultIterator iterator = scanner.nextRows();
        ArrayList<String> result = new ArrayList<>();
        while (iterator != null) {
            for (RowResult row : iterator) {
                result.add(row.getString(0));
            }
            iterator = scanner.nextRows();
        }
        return result;
    } catch (KuduException e) {
        throw new PrestoException(GENERIC_INTERNAL_ERROR, e);
    }
}
 
Example #17
Source File: KuduSQLParseTreeListener.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@Override
public void exitIS_NOT_NULL_FILTER_EXP(KuduSQLExpressionParser.IS_NOT_NULL_FILTER_EXPContext ctx)
{
  super.exitIS_NOT_NULL_FILTER_EXP(ctx);
  String columnName = extractColumnNameFromContext(ctx.idorcolumnname());
  if (columnSchemaLookups.containsKey(columnName)) {
    kuduPredicateList.add(KuduPredicate.newIsNotNullPredicate(columnSchemaLookups.get(columnName)));
  } else {
    LOG.error(columnName + " is not a valid column name for this kudu table");
    isSuccessfullyParsed = false;
  }
}
 
Example #18
Source File: KuduClientSession.java    From presto with Apache License 2.0 5 votes vote down vote up
private KuduPredicate createComparisonPredicate(ColumnSchema columnSchema, KuduPredicate.ComparisonOp op, Object value)
{
    io.prestosql.spi.type.Type type = TypeHelper.fromKuduColumn(columnSchema);
    Object javaValue = TypeHelper.getJavaValue(type, value);
    if (javaValue instanceof Long) {
        return KuduPredicate.newComparisonPredicate(columnSchema, op, (Long) javaValue);
    }
    if (javaValue instanceof BigDecimal) {
        return KuduPredicate.newComparisonPredicate(columnSchema, op, (BigDecimal) javaValue);
    }
    if (javaValue instanceof Integer) {
        return KuduPredicate.newComparisonPredicate(columnSchema, op, (Integer) javaValue);
    }
    if (javaValue instanceof Short) {
        return KuduPredicate.newComparisonPredicate(columnSchema, op, (Short) javaValue);
    }
    if (javaValue instanceof Byte) {
        return KuduPredicate.newComparisonPredicate(columnSchema, op, (Byte) javaValue);
    }
    if (javaValue instanceof String) {
        return KuduPredicate.newComparisonPredicate(columnSchema, op, (String) javaValue);
    }
    if (javaValue instanceof Double) {
        return KuduPredicate.newComparisonPredicate(columnSchema, op, (Double) javaValue);
    }
    if (javaValue instanceof Float) {
        return KuduPredicate.newComparisonPredicate(columnSchema, op, (Float) javaValue);
    }
    if (javaValue instanceof Boolean) {
        return KuduPredicate.newComparisonPredicate(columnSchema, op, (Boolean) javaValue);
    }
    if (javaValue instanceof byte[]) {
        return KuduPredicate.newComparisonPredicate(columnSchema, op, (byte[]) javaValue);
    }
    if (javaValue == null) {
        throw new IllegalStateException("Unexpected null java value for column " + columnSchema.getName());
    }
    throw new IllegalStateException("Unexpected java value for column "
            + columnSchema.getName() + ": " + javaValue + "(" + javaValue.getClass() + ")");
}
 
Example #19
Source File: KuduSQLParseTreeListener.java    From attic-apex-malhar with Apache License 2.0 4 votes vote down vote up
@Override
public void exitIN_FILTER_EXP(KuduSQLExpressionParser.IN_FILTER_EXPContext ctx)
{
  super.exitIN_FILTER_EXP(ctx);
  String columnName = extractColumnNameFromContext(ctx.idorcolumnname());
  if (columnSchemaLookups.containsKey(columnName)) {
    KuduSQLParser.ListofanyvalueContext  listofanyvalueContext = ctx.listofanyvalue();
    // sort out an IN predicate for a list of bools
    KuduSQLExpressionParser.ListofboolsContext listofboolsContext = listofanyvalueContext.listofbools();
    if (listofboolsContext != null) {
      kuduPredicateList.add(KuduPredicate.newInListPredicate(columnSchemaLookups.get(columnName),
          extractListOfBools(listofboolsContext,columnName)));
    }

    // sort out an IN predicate for a list of doubles
    KuduSQLExpressionParser.ListofdoublesContext listofdoubles = listofanyvalueContext.listofdoubles();
    if (listofdoubles != null) {
      kuduPredicateList.add(KuduPredicate.newInListPredicate(columnSchemaLookups.get(columnName),
          extractListOfDoubles(listofdoubles,columnName)));
    }

    // sort out an IN predicate for a list of floats
    KuduSQLExpressionParser.ListoffloatsContext listoffloatsContext = listofanyvalueContext.listoffloats();
    if (listoffloatsContext != null) {
      kuduPredicateList.add(KuduPredicate.newInListPredicate(columnSchemaLookups.get(columnName),
          extractListOfFloats(listoffloatsContext,columnName)));
    }

    // sort out an IN predicate for a list of longs
    KuduSQLExpressionParser.ListofnumsContext listofnumsContext = listofanyvalueContext.listofnums();
    if (listofnumsContext != null) {
      kuduPredicateList.add(KuduPredicate.newInListPredicate(columnSchemaLookups.get(columnName),
          extractListOfLongs(listofnumsContext,columnName)));
    }

    // sort out an IN predicate for a list of strings or binary
    KuduSQLExpressionParser.ListofstringsContext listofstringsContext = listofanyvalueContext.listofstrings();
    if (listofstringsContext != null) {
      if (columnSchemaLookups.get(columnName).getType().getDataType().getNumber() == Common.DataType.STRING_VALUE) {
        kuduPredicateList.add(KuduPredicate.newInListPredicate(columnSchemaLookups.get(columnName),
            extractListOfStrings(listofstringsContext,columnName)));
      }
      if (columnSchemaLookups.get(columnName).getType().getDataType().getNumber() == Common.DataType.STRING_VALUE) {
        kuduPredicateList.add(KuduPredicate.newInListPredicate(columnSchemaLookups.get(columnName),
            extractListOfBinary(listofstringsContext,columnName)));
      }
    }

  } else {
    LOG.error(columnName + " is not a valid column name to be used in a where clause for this kudu table");
    isSuccessfullyParsed = false;
  }
}
 
Example #20
Source File: KuduSQLParseTreeListener.java    From attic-apex-malhar with Apache License 2.0 4 votes vote down vote up
private KuduPredicate buildKuduPredicate(String columnName,KuduPredicate.ComparisonOp comparisonOp,
    KuduSQLExpressionParser.AnyvalueContext anyvalueContext)
{
  ColumnSchema thisColumnSchema = columnSchemaLookups.get(columnName);
  if (anyvalueContext.bool() != null) {
    if (thisColumnSchema.getType().getDataType().getNumber() != (Common.DataType.BOOL_VALUE)) {
      LOG.error(" Mismatched data type for column " + columnName);
      isSuccessfullyParsed  = false;
      return null;
    }
    return KuduPredicate.newComparisonPredicate(thisColumnSchema,comparisonOp,
      Boolean.valueOf(anyvalueContext.bool().getText().toLowerCase()));
  }
  if (anyvalueContext.doubleval() != null) {
    if (thisColumnSchema.getType().getDataType().getNumber() != (Common.DataType.DOUBLE_VALUE)) {
      LOG.error(" Mismatched data type for column (Ensure doubles are appended with letter d)" + columnName);
      isSuccessfullyParsed  = false;
      return null;
    }
    return KuduPredicate.newComparisonPredicate(thisColumnSchema,comparisonOp,
      Double.valueOf(anyvalueContext.doubleval().getText()));
  }
  if (anyvalueContext.floatval() != null) {
    if (thisColumnSchema.getType().getDataType().getNumber() != (Common.DataType.FLOAT_VALUE)) {
      LOG.error(" Mismatched data type for column (Ensure doubles are appended with letter f)" + columnName);
      isSuccessfullyParsed  = false;
      return null;
    }
    return KuduPredicate.newComparisonPredicate(thisColumnSchema,comparisonOp,
      Float.valueOf(anyvalueContext.floatval().getText()));
  }
  if (anyvalueContext.stringval() != null) {
    if ( (thisColumnSchema.getType().getDataType().getNumber() != (Common.DataType.STRING_VALUE)) &&
        (thisColumnSchema.getType().getDataType().getNumber() != (Common.DataType.BINARY_VALUE))) {
      LOG.error(" Mismatched data type for column ( Has to be a string or a binary value enclosed in" +
          " double quotes" + columnName);
      isSuccessfullyParsed  = false;
      return null;
    }
    if (thisColumnSchema.getType().getDataType().getNumber() == (Common.DataType.STRING_VALUE)) {
      return KuduPredicate.newComparisonPredicate(thisColumnSchema, comparisonOp,
        anyvalueContext.stringval().getText());
    }
    if (thisColumnSchema.getType().getDataType().getNumber() == (Common.DataType.BINARY_VALUE)) {
      return KuduPredicate.newComparisonPredicate(thisColumnSchema, comparisonOp,
        anyvalueContext.stringval().getText().getBytes());
    }
  }
  if (anyvalueContext.numval() != null) {
    int dataTypeNumberForKuduCol = thisColumnSchema.getType().getDataType().getNumber();
    if (
        (dataTypeNumberForKuduCol != (Common.DataType.UNIXTIME_MICROS_VALUE)) &&
        (dataTypeNumberForKuduCol != (Common.DataType.INT8_VALUE)) &&
        (dataTypeNumberForKuduCol != (Common.DataType.INT16_VALUE)) &&
        (dataTypeNumberForKuduCol != (Common.DataType.INT32_VALUE)) &&
        (dataTypeNumberForKuduCol != (Common.DataType.INT64_VALUE)) &&
        (dataTypeNumberForKuduCol != (Common.DataType.UINT8_VALUE)) &&
        (dataTypeNumberForKuduCol != (Common.DataType.UINT16_VALUE)) &&
        (dataTypeNumberForKuduCol != (Common.DataType.UINT32_VALUE)) &&
        (dataTypeNumberForKuduCol != (Common.DataType.UINT64_VALUE))
        ) {
      LOG.error(" Mismatched data type for column " + columnName);
      isSuccessfullyParsed  = false;
      return null;
    }
    return KuduPredicate.newComparisonPredicate(thisColumnSchema,comparisonOp,
      Long.valueOf(anyvalueContext.numval().getText()));
  }
  return null;
}
 
Example #21
Source File: KuduSQLParseTreeListener.java    From attic-apex-malhar with Apache License 2.0 4 votes vote down vote up
public List<KuduPredicate> getKuduPredicateList()
{
  return kuduPredicateList;
}
 
Example #22
Source File: KuduSQLParseTreeListener.java    From attic-apex-malhar with Apache License 2.0 4 votes vote down vote up
public void setKuduPredicateList(List<KuduPredicate> kuduPredicateList)
{
  this.kuduPredicateList = kuduPredicateList;
}
 
Example #23
Source File: AbstractKuduPartitionScanner.java    From attic-apex-malhar with Apache License 2.0 4 votes vote down vote up
/***
 * The main logic which takes the parsed in query and builds the Kudud scan tokens specific to this query.
 * It makes sure that these scan tokens are sorted before the actual scan tokens that are to be executed in the
 * current physical instance of the operator are shortlisted. Since the kudu scan taken builder gives the scan
 * tokens for the query and does not differentiate between a distributed system and a single instance system, this
 * method takes the plan as generated by the Kudu scan token builder and then chooses only those segments that were
 * decided to be the responsibility of this operator at partitioning time.
 * @param parsedQuery The parsed query instance
 * @return A list of partition scan metadata objects that are applicable for this instance of the physical operator
 * i.e. the operator owning this instance of the scanner.
 * @throws IOException If the scan assignment cannot be serialized
 */
public List<KuduPartitionScanAssignmentMeta> preparePlanForScanners(SQLToKuduPredicatesTranslator parsedQuery)
  throws IOException
{
  List<KuduPredicate> predicateList = parsedQuery.getKuduSQLParseTreeListener().getKuduPredicateList();
  ApexKuduConnection apexKuduConnection = verifyConnectionStaleness(0);// we will have atleast one connection
  KuduScanToken.KuduScanTokenBuilder builder = apexKuduConnection.getKuduClient().newScanTokenBuilder(
      apexKuduConnection.getKuduTable());
  builder = builder.setProjectedColumnNames(new ArrayList<>(
      parsedQuery.getKuduSQLParseTreeListener().getListOfColumnsUsed()));
  for (KuduPredicate aPredicate : predicateList) {
    builder = builder.addPredicate(aPredicate);
  }
  builder.setFaultTolerant(parentOperator.isFaultTolerantScanner());
  Map<String,String> optionsUsedForThisQuery = parentOperator.getOptionsEnabledForCurrentQuery();
  if ( optionsUsedForThisQuery.containsKey(KuduSQLParseTreeListener.READ_SNAPSHOT_TIME)) {
    try {
      long readSnapShotTime = Long.valueOf(optionsUsedForThisQuery.get(KuduSQLParseTreeListener.READ_SNAPSHOT_TIME));
      builder = builder.readMode(AsyncKuduScanner.ReadMode.READ_AT_SNAPSHOT);
      builder = builder.snapshotTimestampMicros(readSnapShotTime);
      LOG.info("Using read snapshot for this query as " + readSnapShotTime);
    } catch ( Exception ex) {
      LOG.error("Cannot parse the Read snaptshot time " + ex.getMessage(), ex);
    }
  }
  List<KuduScanToken> allPossibleScanTokens = builder.build();
  Collections.sort(allPossibleScanTokens, // Make sure we deal with a sorted list of scan tokens
      new Comparator<KuduScanToken>()
    {
      @Override
      public int compare(KuduScanToken left, KuduScanToken right)
      {
        return left.compareTo(right);
      }
    });
  LOG.info(" Query will scan " + allPossibleScanTokens.size() + " tablets");
  if ( LOG.isDebugEnabled()) {
    LOG.debug(" Predicates scheduled for this query are " + predicateList.size());
    for ( int i = 0; i < allPossibleScanTokens.size(); i++) {
      LOG.debug("A tablet scheduled for all operators scanning is " + allPossibleScanTokens.get(i).getTablet());
    }
  }
  List<KuduPartitionScanAssignmentMeta> partitionPieForThisOperator = parentOperator.getPartitionPieAssignment();
  List<KuduPartitionScanAssignmentMeta> returnOfAssignments = new ArrayList<>();
  int totalScansForThisQuery = allPossibleScanTokens.size();
  int counterForPartAssignments = 0;
  for (KuduPartitionScanAssignmentMeta aPartofThePie : partitionPieForThisOperator) {
    if ( aPartofThePie.getOrdinal() < totalScansForThisQuery) { // a given query plan might have less scantokens
      KuduPartitionScanAssignmentMeta aMetaForThisQuery = new KuduPartitionScanAssignmentMeta();
      aMetaForThisQuery.setTotalSize(totalScansForThisQuery);
      aMetaForThisQuery.setOrdinal(counterForPartAssignments);
      counterForPartAssignments += 1;
      aMetaForThisQuery.setCurrentQuery(parsedQuery.getSqlExpresssion());
      // we pick up only those ordinals that are part of the original partition pie assignment
      KuduScanToken aTokenForThisOperator = allPossibleScanTokens.get(aPartofThePie.getOrdinal());
      aMetaForThisQuery.setSerializedKuduScanToken(aTokenForThisOperator.serialize());
      returnOfAssignments.add(aMetaForThisQuery);
      LOG.debug("Added query scan for this operator " + aMetaForThisQuery + " with scan tablet as " +
          allPossibleScanTokens.get(aPartofThePie.getOrdinal()).getTablet());
    }
  }
  LOG.info(" A total of " + returnOfAssignments.size() + " have been scheduled for this operator");
  return returnOfAssignments;
}
 
Example #24
Source File: KuduFilterInfo.java    From bahir-flink with Apache License 2.0 4 votes vote down vote up
FilterType(KuduPredicate.ComparisonOp comparator) {
    this.comparator = comparator;
}
 
Example #25
Source File: KuduClientSession.java    From presto with Apache License 2.0 4 votes vote down vote up
private KuduPredicate createInListPredicate(ColumnSchema columnSchema, DiscreteValues discreteValues)
{
    io.prestosql.spi.type.Type type = TypeHelper.fromKuduColumn(columnSchema);
    List<Object> javaValues = discreteValues.getValues().stream().map(value -> TypeHelper.getJavaValue(type, value)).collect(toImmutableList());
    return KuduPredicate.newInListPredicate(columnSchema, javaValues);
}
 
Example #26
Source File: KuduFilterInfo.java    From bahir-flink with Apache License 2.0 4 votes vote down vote up
private KuduPredicate predicateComparator(ColumnSchema column) {

        KuduPredicate.ComparisonOp comparison = this.type.comparator;

        KuduPredicate predicate;

        switch (column.getType()) {
            case STRING:
                predicate = KuduPredicate.newComparisonPredicate(column, comparison, (String) this.value);
                break;
            case FLOAT:
                predicate = KuduPredicate.newComparisonPredicate(column, comparison, this.value);
                break;
            case INT8:
                predicate = KuduPredicate.newComparisonPredicate(column, comparison, this.value);
                break;
            case INT16:
                predicate = KuduPredicate.newComparisonPredicate(column, comparison, this.value);
                break;
            case INT32:
                predicate = KuduPredicate.newComparisonPredicate(column, comparison, this.value);
                break;
            case INT64:
                predicate = KuduPredicate.newComparisonPredicate(column, comparison, this.value);
                break;
            case DOUBLE:
                predicate = KuduPredicate.newComparisonPredicate(column, comparison, this.value);
                break;
            case BOOL:
                predicate = KuduPredicate.newComparisonPredicate(column, comparison, this.value);
                break;
            case UNIXTIME_MICROS:
                Long time = (Long) this.value;
                predicate = KuduPredicate.newComparisonPredicate(column, comparison, time * 1000);
                break;
            case BINARY:
                predicate = KuduPredicate.newComparisonPredicate(column, comparison, (byte[]) this.value);
                break;
            default:
                throw new IllegalArgumentException("Illegal var type: " + column.getType());
        }
        return predicate;
    }
 
Example #27
Source File: KuduFilterInfo.java    From bahir-flink with Apache License 2.0 4 votes vote down vote up
public KuduPredicate toPredicate(Schema schema) {
    return toPredicate(schema.getColumn(this.column));
}
 
Example #28
Source File: NativeKuduClientSession.java    From presto-kudu with Apache License 2.0 4 votes vote down vote up
private KuduPredicate createEqualsPredicate(ColumnSchema columnSchema, Object value) {
    return createComparisonPredicate(columnSchema, KuduPredicate.ComparisonOp.EQUAL, value);
}
 
Example #29
Source File: NativeKuduClientSession.java    From presto-kudu with Apache License 2.0 4 votes vote down vote up
private KuduPredicate createInListPredicate(ColumnSchema columnSchema, DiscreteValues discreteValues) {
    com.facebook.presto.spi.type.Type type = TypeHelper.fromKuduColumn(columnSchema);
    List<Object> javaValues = discreteValues.getValues().stream().map(value -> TypeHelper.getJavaValue(type, value)).collect(toImmutableList());
    return KuduPredicate.newInListPredicate(columnSchema, javaValues);
}
 
Example #30
Source File: KuduFilterInfo.java    From flink-learning with Apache License 2.0 4 votes vote down vote up
FilterType(KuduPredicate.ComparisonOp comparator) {
    this.comparator = comparator;
}