Querying SingleStore

SingleStore is supported by the querydsl-sql module through SingleStoreTemplates and SingleStoreQueryFactory. Everything described in Querying SQL applies; this page covers what is specific to SingleStore.

Maven Integration

<dependency>
  <groupId>io.github.openfeign.querydsl</groupId>
  <artifactId>querydsl-sql</artifactId>
  <version>7.1</version>
</dependency>

<dependency>
  <groupId>com.singlestore</groupId>
  <artifactId>singlestore-jdbc-client</artifactId>
  <version>1.2.12</version>
</dependency>

Code Generation

Generate query types from the database schema with the querydsl-maven-plugin export goal, using the SingleStore driver:

<plugin>
  <groupId>io.github.openfeign.querydsl</groupId>
  <artifactId>querydsl-maven-plugin</artifactId>
  <version>7.1</version>
  <executions>
    <execution>
      <goals>
        <goal>export</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <jdbcDriver>com.singlestore.jdbc.Driver</jdbcDriver>
    <jdbcUrl>jdbc:singlestore://localhost:3306/mydb</jdbcUrl>
    <jdbcUser>root</jdbcUser>
    <jdbcPassword>secret</jdbcPassword>
    <schemaPattern>mydb</schemaPattern>
    <packageName>com.myproject.domain</packageName>
    <targetFolder>${project.basedir}/target/generated-sources/java</targetFolder>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>com.singlestore</groupId>
      <artifactId>singlestore-jdbc-client</artifactId>
      <version>1.2.12</version>
    </dependency>
  </dependencies>
</plugin>

SingleStore has no foreign keys, and its driver fails on foreign key metadata lookups. The exporter therefore skips those lookups. The generated types contain primary keys but no ForeignKey members, so write joins with explicit on(...) conditions.

Templates Detection

SQLTemplatesRegistry returns SingleStoreTemplates for connections made with the SingleStore driver, which reports the product name SingleStore. If you connect through MySQL Connector/J, or set useMysqlMetadata=true on the SingleStore driver, the driver reports the server as MySQL and the registry returns MySQLTemplates. Those templates do not fit SingleStore; for example, their forShare() emits LOCK IN SHARE MODE, which SingleStore rejects. In that case, configure SingleStoreTemplates explicitly:

SQLTemplates templates = SingleStoreTemplates.builder().build();
Configuration configuration = new Configuration(templates);
SingleStoreQueryFactory queryFactory =
    new SingleStoreQueryFactory(configuration, dataSource::getConnection);

Queries and DML

SingleStoreQueryFactory is a SQLQueryFactory, so it creates SQLQuery instances and the usual insert, update, delete and merge clauses. It also offers the MySQL insert variants:

// insert ignore into SURVEY ...
queryFactory.insertIgnore(survey)
    .columns(survey.id, survey.name).values(1, "Hello").execute();

// insert ... on duplicate key update NAME2 = ?
queryFactory.insertOnDuplicateKeyUpdate(survey, SQLExpressions.set(survey.name2, "Updated"))
    .columns(survey.id, survey.name).values(1, "Hello").execute();

// replace into SURVEY ...
queryFactory.replace(survey)
    .columns(survey.id, survey.name).values(1, "Hello").execute();

merge(...) is emulated with a select followed by an update or an insert, because SingleStore has no MERGE statement.

The MySQL-only query hints of MySQLQuery (straightJoin, calcFoundRows, lockInShareMode, buffer and priority hints) are not available. forUpdate() is supported. forShare() throws, and forShare(true) falls back to forUpdate().

insertOnDuplicateKeyUpdate(entity, String) inlines the clause without escaping. Use the Expression overloads for anything that is not a constant.

SingleStore Constraints

  • Columns of the shard key cannot be updated. Such an update fails with the server error Shard-key-affecting columns cannot be updated.
  • Unique and primary keys must contain the shard key.
  • LIKE ... ESCAPE is not supported. SingleStoreTemplates accepts only the default backslash escape, so builder().escape(ch) with any other character throws IllegalArgumentException, and like(pattern, escapeChar) fails on the server.
  • Quantified subqueries (> any (...), > all (...)) and row-value in subqueries are not supported.
  • corr, covar_pop, covar_samp, cume_dist and the regr_* window functions are not supported.
  • update ... limit must be constrained to a single partition.
  • SingleStore applies a trailing order by on a union to the last branch only. SingleStoreTemplates therefore wraps unions in a derived table, so every branch of a union must have unique column labels.
  • The auto increment counter does not move past IDs you insert explicitly, so generated keys can collide with them. Avoid mixing explicit and generated IDs, or run aggregator sync auto_increment on db.table all after explicit inserts. That command must run on the master aggregator.