Skip to content

Commit

Permalink
Use constants for limit/offset
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Henneberger <git@danielhenneberger.com>
  • Loading branch information
henneberger committed Oct 30, 2023
1 parent 3ecddff commit 21f2a63
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.datasqrl.graphql.jdbc;

import static com.datasqrl.graphql.jdbc.SchemaConstants.LIMIT;
import static com.datasqrl.graphql.jdbc.SchemaConstants.OFFSET;

import com.datasqrl.graphql.server.Model.Argument;
import com.datasqrl.graphql.server.Model.ArgumentParameter;
import com.datasqrl.graphql.server.Model.FixedArgument;
Expand Down Expand Up @@ -71,8 +74,8 @@ public static Object unboxList(List<Map<String, Object>> o, boolean isList) {
@Override
public CompletableFuture runPagedJdbcQuery(ResolvedPagedJdbcQuery pgQuery,
boolean isList, QueryExecutionContext context) {
Optional<Integer> limit = Optional.ofNullable(getEnvironment().getArgument("limit"));
Optional<Integer> offset = Optional.ofNullable(getEnvironment().getArgument("offset"));
Optional<Integer> limit = Optional.ofNullable(getEnvironment().getArgument(LIMIT));
Optional<Integer> offset = Optional.ofNullable(getEnvironment().getArgument(OFFSET));
Object[] paramObj = new Object[pgQuery.getQuery().getParameters().size()];
for (int i = 0; i < pgQuery.getQuery().getParameters().size(); i++) {
JdbcParameterHandler param = pgQuery.getQuery().getParameters().get(i);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.datasqrl.graphql.jdbc;

public class SchemaConstants {

public static final String LIMIT = "limit";
public static final String OFFSET = "offset";
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.datasqrl.graphql;

import static com.datasqrl.graphql.jdbc.SchemaConstants.LIMIT;
import static com.datasqrl.graphql.jdbc.SchemaConstants.OFFSET;

import com.datasqrl.graphql.VertxJdbcClient.PreparedSqrlQueryImpl;
import com.datasqrl.graphql.server.Model.Argument;
import com.datasqrl.graphql.server.Model.ArgumentParameter;
Expand Down Expand Up @@ -60,8 +63,8 @@ public CompletableFuture runQuery(BuildGraphQLEngine server, ResolvedJdbcQuery p
@Override
public CompletableFuture runPagedJdbcQuery(ResolvedPagedJdbcQuery pgQuery,
boolean isList, QueryExecutionContext context) {
Optional<Integer> limit = Optional.ofNullable(getEnvironment().getArgument("limit"));
Optional<Integer> offset = Optional.ofNullable(getEnvironment().getArgument("offset"));
Optional<Integer> limit = Optional.ofNullable(getEnvironment().getArgument(LIMIT));
Optional<Integer> offset = Optional.ofNullable(getEnvironment().getArgument(OFFSET));
Object[] paramObj = new Object[pgQuery.getQuery().getParameters().size()];
for (int i = 0; i < pgQuery.getQuery().getParameters().size(); i++) {
JdbcParameterHandler param = pgQuery.getQuery().getParameters().get(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import static com.datasqrl.graphql.generate.ObjectTypeGenerator.logIfInvalid;
import static com.datasqrl.graphql.generate.SchemaGenerator.isValidGraphQLName;
import static com.datasqrl.graphql.generate.SchemaGeneratorUtil.getInputType;
import static com.datasqrl.graphql.jdbc.SchemaConstants.LIMIT;
import static com.datasqrl.graphql.jdbc.SchemaConstants.OFFSET;
import static graphql.schema.GraphQLNonNull.nonNull;

import com.datasqrl.function.SqrlFunctionParameter;
Expand Down Expand Up @@ -93,13 +95,13 @@ private List<GraphQLArgument> generateLimitOffset() {

//add limit / offset
GraphQLArgument limit = GraphQLArgument.newArgument()
.name("limit")
.name(LIMIT)
.type(Scalars.GraphQLInt)
.defaultValueLiteral(IntValue.of(10))
.build();

GraphQLArgument offset = GraphQLArgument.newArgument()
.name("offset")
.name(OFFSET)
.type(Scalars.GraphQLInt)
.defaultValueLiteral(IntValue.of(0))
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.datasqrl.graphql.inference;

import static com.datasqrl.canonicalizer.ReservedName.VARIABLE_PREFIX;
import static com.datasqrl.graphql.jdbc.SchemaConstants.LIMIT;
import static com.datasqrl.graphql.jdbc.SchemaConstants.OFFSET;

import com.datasqrl.calcite.SqrlFramework;
import com.datasqrl.calcite.function.SqrlTableMacro;
Expand Down Expand Up @@ -142,8 +144,8 @@ public Model.ArgumentSet create(List<ArgCombination> arg, SqrlTableMacro macro,
}

private boolean isLimitOrOffset(ArgCombination c) {
return c.getDefinition().getName().equalsIgnoreCase("limit") ||
c.getDefinition().getName().equalsIgnoreCase("offset");
return c.getDefinition().getName().equalsIgnoreCase(LIMIT) ||
c.getDefinition().getName().equalsIgnoreCase(OFFSET);
}

private Pair<SqlUserDefinedTableFunction, Boolean> resolveOperator(SqrlTableMacro macro, List<ArgCombination> arg) {
Expand Down Expand Up @@ -197,20 +199,20 @@ private List<SqrlFunctionParameter> getInternalParams(List<FunctionParameter> pa

private Optional<ArgCombination> onlyLimit(List<ArgCombination> arg) {
return arg.stream()
.filter(f -> f.getDefinition().getName().equalsIgnoreCase("limit"))
.filter(f -> f.getDefinition().getName().equalsIgnoreCase(LIMIT))
.findFirst();
}

private Optional<ArgCombination> onlyOffset(List<ArgCombination> arg) {
return arg.stream()
.filter(f -> f.getDefinition().getName().equalsIgnoreCase("offset"))
.filter(f -> f.getDefinition().getName().equalsIgnoreCase(OFFSET))
.findFirst();
}

private List<ArgCombination> removePaging(List<ArgCombination> arg) {
return arg.stream()
.filter(f -> !f.getDefinition().getName().equalsIgnoreCase("limit")
&& !f.getDefinition().getName().equalsIgnoreCase("offset"))
.filter(f -> !f.getDefinition().getName().equalsIgnoreCase(LIMIT)
&& !f.getDefinition().getName().equalsIgnoreCase(OFFSET))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.datasqrl.graphql.inference;

import static com.datasqrl.graphql.jdbc.SchemaConstants.LIMIT;
import static com.datasqrl.graphql.jdbc.SchemaConstants.OFFSET;

import com.datasqrl.calcite.QueryPlanner;
import com.datasqrl.graphql.APIConnectorManager;
import com.datasqrl.graphql.inference.SchemaBuilder.ArgCombination;
Expand Down Expand Up @@ -145,17 +148,17 @@ public void limitOffset(Optional<ArgCombination> limit, Optional<ArgCombination>
this.limitOffsetFlag = true;
if (limit.isPresent()) {
if (limit.get().getDefaultValue().isEmpty()) {
createVariableForLimitOffset("limit");
createVariableForLimitOffset(LIMIT);
} else {
createLiteralForLimitOffset("limit");
createLiteralForLimitOffset(LIMIT);
}
}

if (offset.isPresent()) {
if (offset.get().getDefaultValue().isEmpty()) {
createVariableForLimitOffset("offset");
createVariableForLimitOffset(OFFSET);
} else {
createLiteralForLimitOffset("offset");
createLiteralForLimitOffset(OFFSET);
}
}
}
Expand Down Expand Up @@ -199,21 +202,21 @@ public void limitOffsetRowNum(Optional<ArgCombination> limit, Optional<ArgCombin

if (offset.isPresent() && limit.isPresent()) {
if (limitValue.isPresent() && offsetValue.isPresent()) { //both are scalars
addArgumentLiteralFilter("limit", limitValue.get());
addArgumentLiteralFilter("offset", offsetValue.get());
addArgumentLiteralFilter(LIMIT, limitValue.get());
addArgumentLiteralFilter(OFFSET, offsetValue.get());
relBuilder
.limit(offsetValue.get(), limitValue.get());
} else if (limitValue.isPresent()) { //offset is a variable
addArgumentLiteralFilter("limit", limitValue.get());
RexDynamicParam param = addVariableOperand("offset", type);
addArgumentLiteralFilter(LIMIT, limitValue.get());
RexDynamicParam param = addVariableOperand(OFFSET, type);

RexInputRef rowNum = createRowNum(partitionKeysFields, relBuilder);
relBuilder
.filter(relBuilder.call(SqlStdOperatorTable.GREATER_THAN, rowNum, param))
.limit(0, limitValue.get());
} else if (offsetValue.isPresent()) { //limit is a variable
RexDynamicParam param = addVariableOperand("limit", type);
addArgumentLiteralFilter("offset", offset.get());
RexDynamicParam param = addVariableOperand(LIMIT, type);
addArgumentLiteralFilter(OFFSET, offset.get());

RexInputRef rowNum = createRowNum(partitionKeysFields, relBuilder);
// between offset + 1 and limit + offset
Expand All @@ -223,8 +226,8 @@ public void limitOffsetRowNum(Optional<ArgCombination> limit, Optional<ArgCombin
relBuilder.call(SqlStdOperatorTable.PLUS, relBuilder.literal(offsetValue.get()),
param)));
} else { //both are variables
RexDynamicParam limitParam = addVariableOperand("limit", type);
RexDynamicParam offsetParam = addVariableOperand("offset", type);
RexDynamicParam limitParam = addVariableOperand(LIMIT, type);
RexDynamicParam offsetParam = addVariableOperand(OFFSET, type);

RexInputRef rowNum = createRowNum(partitionKeysFields, relBuilder);
//
Expand All @@ -241,7 +244,7 @@ public void limitOffsetRowNum(Optional<ArgCombination> limit, Optional<ArgCombin
relBuilder
.limit(offsetValue.get(), 0);
} else {
RexDynamicParam offsetParam = addVariableOperand("offset", type);
RexDynamicParam offsetParam = addVariableOperand(OFFSET, type);

RexInputRef rowNum = createRowNum(partitionKeysFields, relBuilder);

Expand All @@ -253,7 +256,7 @@ public void limitOffsetRowNum(Optional<ArgCombination> limit, Optional<ArgCombin
relBuilder
.limit(0, limitValue.get());
} else {
RexDynamicParam limitParam = addVariableOperand("limit", type);
RexDynamicParam limitParam = addVariableOperand(LIMIT, type);

RexInputRef rowNum = createRowNum(partitionKeysFields, relBuilder);

Expand Down

0 comments on commit 21f2a63

Please sign in to comment.