Class FindInStatement<IN,T extends tech.ydb.yoj.repository.db.Entity<T>,RESULT>

java.lang.Object
tech.ydb.yoj.repository.ydb.statement.YqlStatement<PARAMS,ENTITY,RESULT>
tech.ydb.yoj.repository.ydb.statement.MultipleVarsYqlStatement<IN,T,RESULT>
tech.ydb.yoj.repository.ydb.statement.FindInStatement<IN,T,RESULT>
All Implemented Interfaces:
Statement<IN,RESULT>

public final class FindInStatement<IN,T extends tech.ydb.yoj.repository.db.Entity<T>,RESULT> extends MultipleVarsYqlStatement<IN,T,RESULT>

Creates statement for SELECT ... WHERE PK IN (PK1, PK2, ...). PK can be both a primary key and a key of secondary indexes. In the case of a secondary index, you must specify the name of the index used.

For the entity:

  @GlobalIndex(name = "index_by_keys", fields = {"k1", "k2"})
  public class Sample implements Entity<Sample> {
    Id id;
    String value;
    int k1;
    String k2;

    public static class Id implements Entity.Id<Sample> {
      int id1;
      int id2;
    }
  }
 

The statement will be :

 DECLARE $Input AS List<Struct<id1:Int32?,id2:Int32?>>;
 SELECT t.`id1` AS `id1`, t.`id2` AS `id2`, t.`value` AS `value`
 FROM AS_TABLE($Input) AS k
 JOIN `sample` AS t
 ON t.`id1` = k.`id1` AND t.`id2` = k.`id2`
 ORDER BY id1 ASC, id2 ASC
 

If the entity ID is complex type, a part of the ID fields can be null. They will be omitted in the query. For the given example, if all ids have id2 == null, the statement will be:

 DECLARE $Input AS List<Struct<id1:Int32?>>;
 SELECT t.`id1` AS `id1`, t.`id2` AS `id2`, t.`value` AS `value`
 FROM AS_TABLE($Input) AS k
 JOIN `sample` AS t
 ON t.`id1` = k.`id1`
 ORDER BY id1 ASC, id2 ASC
 

If orderBy specified as order by value descending, than statement will be:

 DECLARE $Input AS List<Struct<id1:Int32?,id2:Int32?>>;
 SELECT t.`id1` AS `id1`, t.`id2` AS `id2`, t.`value` AS `value`
 FROM AS_TABLE($Input) AS k
 JOIN `sample` AS t
 ON t.`id1` = k.`id1` AND t.`id2` = k.`id2`
 ORDER BY value DESC
 

YDB restriction of the ORDER BY clause: ordering fields must be present in the resultSchema.

Statement also support filter expression. With the filter statement will will be as follows:

 DECLARE $Input AS List<Struct<id1:Int32?,id2:Int32?>>;
 DECLARE $pred_0_value AS 'Utf8?';
 SELECT `id1`, `id2`, `value`
 FROM (
 SELECT t.`id1` AS `id1`, t.`id2` AS `id2`, t.`value` AS `value`
 FROM AS_TABLE($Input) AS k
 JOIN `sample` AS t
 ON t.`id1` = k.`id1` AND t.`id2` = k.`id2`
 )
 WHERE value = $pred_0_value
 ORDER BY id1 ASC, id2 ASC
 

When using a secondary index, the statement will look like this:

 DECLARE $Input AS List<Struct<`k1`:Int32,`k2`:Utf8>>;
 SELECT t.`value` AS `value`
 FROM AS_TABLE($Input) AS k
 JOIN `sample` VIEW `index_by_keys` AS t
 ON t.`k1` = k.`k1` AND t.`k2` = k.`k2`
 
  • Constructor Details

    • FindInStatement

      protected FindInStatement(tech.ydb.yoj.repository.db.EntitySchema<T> schema, tech.ydb.yoj.databind.schema.Schema<RESULT> resultSchema, Iterable<? extends tech.ydb.yoj.repository.db.Entity.Id<T>> ids, @Nullable tech.ydb.yoj.databind.expression.FilterExpression<T> filter, @Nullable tech.ydb.yoj.databind.expression.OrderExpression<T> orderBy, @Nullable Integer limit)
      Creates new FindInStatement instance with pagination.
      Parameters:
      schema - entity schema
      resultSchema - result schema
      ids - the ids of entities to load
      filter - optional additional filter expression, the filter is expected to filter by non-PK fields
      orderBy - order by expression for result sorting, order fields must be present in the resultSchema
    • FindInStatement

      protected FindInStatement(tech.ydb.yoj.repository.db.EntitySchema<T> schema, tech.ydb.yoj.databind.schema.Schema<RESULT> resultSchema, String indexName, Iterable<V> keys, @Nullable tech.ydb.yoj.databind.expression.FilterExpression<T> filter, @Nullable tech.ydb.yoj.databind.expression.OrderExpression<T> orderBy, @Nullable Integer limit)
      Creates new FindInStatement instance with index usage and pagination.
  • Method Details

    • getQueryType

      public Statement.QueryType getQueryType()
      Description copied from interface: Statement
      Returns query type (for query merging purposes).
      Returns:
      query type
    • getQuery

      public String getQuery(String tablespace)
      Description copied from interface: Statement
      Returns parameterized YQL for this query.
      Parameters:
      tablespace - base path for all tables referenced in the query
      Returns:
      YQL
    • getParams

      public List<YqlStatementParam> getParams()
      Overrides:
      getParams in class MultipleVarsYqlStatement<IN,T extends tech.ydb.yoj.repository.db.Entity<T>,RESULT>
    • toQueryParameters

      public Map<String,com.yandex.ydb.ValueProtos.TypedValue> toQueryParameters(IN in)
      Description copied from interface: Statement
      Returns the query's parameter values as YDB protobuf structures.
      Specified by:
      toQueryParameters in interface Statement<IN,T extends tech.ydb.yoj.repository.db.Entity<T>>
      Overrides:
      toQueryParameters in class MultipleVarsYqlStatement<IN,T extends tech.ydb.yoj.repository.db.Entity<T>,RESULT>
      Parameters:
      in - parameter values Might be null depending on the statement type, e.g. for DELETE statements.
      Returns:
      map: parameter name -> value as protobuf
    • declarations

      protected String declarations()
      Overrides:
      declarations in class MultipleVarsYqlStatement<IN,T extends tech.ydb.yoj.repository.db.Entity<T>,RESULT>
    • outNames

      protected String outNames()
      Overrides:
      outNames in class YqlStatement<IN,T extends tech.ydb.yoj.repository.db.Entity<T>,RESULT>
    • flattenInputVariables

      protected Function<IN,Map<String,Object>> flattenInputVariables()
      Specified by:
      flattenInputVariables in class MultipleVarsYqlStatement<IN,T extends tech.ydb.yoj.repository.db.Entity<T>,RESULT>
    • toDebugString

      public String toDebugString(IN in)
      Description copied from interface: Statement
      Returns debug representation of this query with the specified parameter values.
      Parameters:
      in - parameter values. Might be null depending on the statement type, e.g. for DELETE statements.
      Returns:
      debug representation of the query parameterized with params