use of net.sf.jsqlparser.expression.Expression in project herddb by diennea.
the class SQLExpressionCompiler method compileExpression.
// this method never returns NULL
public static CompiledSQLExpression compileExpression(String validatedTableAlias, Expression exp) {
if (exp instanceof BinaryExpression) {
CompiledSQLExpression compiled = compileSpecialBinaryExpression(validatedTableAlias, exp);
if (compiled != null) {
return compiled;
}
}
if (exp instanceof net.sf.jsqlparser.schema.Column) {
return compileColumnExpression(validatedTableAlias, exp);
} else if (exp instanceof StringValue) {
return new ConstantExpression(RawString.of(((StringValue) exp).getValue()));
} else if (exp instanceof LongValue) {
try {
return new ConstantExpression(((LongValue) exp).getValue());
} catch (NumberFormatException largeNumber) {
return new ConstantExpression(Double.valueOf(((LongValue) exp).getStringValue()));
}
} else if (exp instanceof DoubleValue) {
return new ConstantExpression(((DoubleValue) exp).getValue());
} else if (exp instanceof TimestampValue) {
return new ConstantExpression(((TimestampValue) exp).getValue());
} else if (exp instanceof NullValue) {
return new ConstantExpression(null);
} else if (exp instanceof TimeKeyExpression) {
TimeKeyExpression ext = (TimeKeyExpression) exp;
if (CURRENT_TIMESTAMP.equalsIgnoreCase(ext.getStringValue())) {
return new ConstantExpression(new java.sql.Timestamp(System.currentTimeMillis()));
} else {
throw new StatementExecutionException("unhandled expression " + exp);
}
} else if (exp instanceof JdbcParameter) {
int index = ((JdbcParameter) exp).getIndex() - 1;
return new JdbcParameterExpression(index);
} else if (exp instanceof AndExpression) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledAndExpression(a, b, c));
} else if (exp instanceof OrExpression) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledOrExpression(a, b, c));
} else if (exp instanceof Function) {
return CompiledFunction.create((Function) exp, validatedTableAlias);
} else if (exp instanceof Addition) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledAddExpression(a, b, c));
} else if (exp instanceof Subtraction) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledSubtractExpression(a, b, c));
} else if (exp instanceof Multiplication) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledMultiplyExpression(a, b, c));
} else if (exp instanceof Division) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledDivideExpression(a, b, c));
} else if (exp instanceof Parenthesis) {
Parenthesis p = (Parenthesis) exp;
CompiledSQLExpression inner = compileExpression(validatedTableAlias, p.getExpression());
return new CompiledParenthesisExpression(p.isNot(), inner);
} else if (exp instanceof EqualsTo) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledEqualsExpression(a, b, c));
} else if (exp instanceof NotEqualsTo) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledNotEqualsExpression(a, b, c));
} else if (exp instanceof MinorThan) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledMinorThenExpression(a, b, c));
} else if (exp instanceof MinorThanEquals) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledMinorThenEqualsExpression(a, b, c));
} else if (exp instanceof GreaterThan) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledGreaterThenExpression(a, b, c));
} else if (exp instanceof GreaterThanEquals) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledGreaterThenEqualsExpression(a, b, c));
} else if (exp instanceof LikeExpression) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledLikeExpression(a, b, c));
} else if (exp instanceof Between) {
return CompiledBetweenExpression.create(validatedTableAlias, (Between) exp);
} else if (exp instanceof SignedExpression) {
SignedExpression s = (SignedExpression) exp;
CompiledSQLExpression inner = compileExpression(validatedTableAlias, s.getExpression());
return new CompiledSignedExpression(s.getSign(), inner);
} else if (exp instanceof InExpression) {
InExpression in = (InExpression) exp;
return CompiledInExpression.create(in, validatedTableAlias);
} else if (exp instanceof IsNullExpression) {
IsNullExpression i = (IsNullExpression) exp;
CompiledSQLExpression left = compileExpression(validatedTableAlias, i.getLeftExpression());
return new CompiledIsNullExpression(i.isNot(), left);
} else if (exp instanceof CaseExpression) {
return CompiledCaseExpression.create(validatedTableAlias, (CaseExpression) exp);
}
throw new StatementExecutionException("unsupported operand " + exp.getClass() + ", expression is " + exp);
}
use of net.sf.jsqlparser.expression.Expression in project herddb by diennea.
the class CompiledFunction method create.
public static CompiledFunction create(Function f, String validatedTableAlias) {
String name = f.getName();
List<Expression> params = null;
if (f.getParameters() != null) {
params = f.getParameters().getExpressions();
}
switch(name) {
case BuiltinFunctions.COUNT:
case BuiltinFunctions.SUM:
case BuiltinFunctions.MIN:
case BuiltinFunctions.MAX:
// AGGREGATED FUNCTION
return new CompiledFunction(name, null);
case BuiltinFunctions.LOWER:
{
if (params == null || params.size() != 1) {
throw new StatementExecutionException("function " + name + " must have one parameter");
}
break;
}
case BuiltinFunctions.UPPER:
{
if (params == null || params.size() != 1) {
throw new StatementExecutionException("function " + name + " must have one parameter");
}
break;
}
case BuiltinFunctions.ABS:
{
if (params == null || params.size() != 1) {
throw new StatementExecutionException("function " + name + " must have one parameter");
}
break;
}
case BuiltinFunctions.ROUND:
{
if (params == null || (params.size() != 1 && params.size() != 2)) {
throw new StatementExecutionException("function " + name + " must have one or two parameters");
}
break;
}
default:
throw new StatementExecutionException("unhandled function " + name);
}
List<CompiledSQLExpression> compiledParams = new ArrayList<>();
for (Expression exp : f.getParameters().getExpressions()) {
compiledParams.add(compileExpression(validatedTableAlias, exp));
}
return new CompiledFunction(name, compiledParams);
}
use of net.sf.jsqlparser.expression.Expression in project herddb by diennea.
the class CompiledCaseExpression method create.
public static CompiledCaseExpression create(String validatedTableAlias, CaseExpression caseExpression) {
Expression switchExpression = caseExpression.getSwitchExpression();
if (switchExpression != null) {
throw new StatementExecutionException("unhandled expression CASE SWITCH, type " + caseExpression.getClass() + ": " + caseExpression);
}
List<Entry<CompiledSQLExpression, CompiledSQLExpression>> whens = null;
if (caseExpression.getWhenClauses() != null) {
whens = new ArrayList<>();
for (Expression when : caseExpression.getWhenClauses()) {
WhenClause whenClause = (WhenClause) when;
CompiledSQLExpression whenCondition = compileExpression(validatedTableAlias, whenClause.getWhenExpression());
if (whenCondition == null) {
return null;
}
CompiledSQLExpression thenExpr = compileExpression(validatedTableAlias, whenClause.getThenExpression());
whens.add(new AbstractMap.SimpleImmutableEntry<>(whenCondition, thenExpr));
}
}
Expression elseExp = caseExpression.getElseExpression();
if (elseExp != null) {
CompiledSQLExpression elseExpression = compileExpression(validatedTableAlias, elseExp);
if (elseExpression == null) {
return null;
}
return new CompiledCaseExpression(whens, elseExpression);
} else {
return new CompiledCaseExpression(whens, null);
}
}
use of net.sf.jsqlparser.expression.Expression in project herddb by diennea.
the class JSQLParserPlanner method buildUpdateStatement.
private ExecutionPlan buildUpdateStatement(String defaultTableSpace, Update update, boolean returnValues) throws StatementExecutionException {
net.sf.jsqlparser.schema.Table table = update.getTable();
// no alias for UPDATE!
checkSupported(table.getAlias() == null);
OpSchema tableSchema = getTableSchema(defaultTableSpace, table);
TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSchema.tableSpace);
AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableSchema.name);
Table tableImpl = tableManager.getTable();
checkSupported(update.getSelect() == null);
checkSupported(update.getJoins() == null);
checkSupported(update.getOrderByElements() == null);
checkSupported(update.getReturningExpressionList() == null);
checkSupported(update.getStartJoins() == null || update.getStartJoins().isEmpty());
List<Expression> projects = update.getExpressions();
List<CompiledSQLExpression> expressions = new ArrayList<>(projects.size());
int index = 0;
List<String> updateColumnList = new ArrayList<>(projects.size());
for (net.sf.jsqlparser.schema.Column column : update.getColumns()) {
checkSupported(column.getTable() == null);
String columnName = fixMySqlBackTicks(column.getColumnName().toLowerCase());
checkSupported(!tableImpl.isPrimaryKeyColumn(columnName));
updateColumnList.add(columnName);
CompiledSQLExpression exp = SQLParserExpressionCompiler.compileExpression(projects.get(index), tableSchema);
expressions.add(exp);
index++;
}
RecordFunction function = new SQLRecordFunction(updateColumnList, tableImpl, expressions);
Predicate where = null;
if (update.getWhere() != null) {
CompiledSQLExpression whereExpression = SQLParserExpressionCompiler.compileExpression(update.getWhere(), tableSchema);
if (whereExpression != null) {
SQLRecordPredicate sqlWhere = new SQLRecordPredicate(tableImpl, null, whereExpression);
IndexUtils.discoverIndexOperations(tableSchema.tableSpace, whereExpression, tableImpl, sqlWhere, update, tableSpaceManager);
where = sqlWhere;
}
}
PlannerOp op = new SimpleUpdateOp(new UpdateStatement(tableSchema.tableSpace, tableSchema.name, null, function, where).setReturnValues(returnValues));
return optimizePlan(op);
}
use of net.sf.jsqlparser.expression.Expression in project herddb by diennea.
the class JSQLParserPlanner method planAggregate.
private PlannerOp planAggregate(List<SelectExpressionItem> fieldList, OpSchema inputSchema, PlannerOp input, OpSchema originalTableSchema, GroupByElement groupBy) {
List<Column> outputSchemaKeysInGroupBy = new ArrayList<>();
List<String> fieldnamesKeysInGroupBy = new ArrayList<>();
List<Column> outputSchemaAggregationResults = new ArrayList<>();
List<String> fieldnamesAggregationResults = new ArrayList<>();
List<Integer> projectionAggregationResults = new ArrayList<>();
List<List<Integer>> argLists = new ArrayList<>();
List<String> aggtypes = new ArrayList<>();
List<Column> originalOutputSchema = new ArrayList<>();
List<String> originalFieldNames = new ArrayList<>();
Set<String> nonAggregateColumns = new HashSet<>();
int k = 0;
for (SelectExpressionItem sel : fieldList) {
Alias alias = sel.getAlias();
String fieldName = null;
if (alias != null) {
checkSupported(alias.getAliasColumns() == null);
fieldName = fixMySqlBackTicks(alias.getName().toLowerCase());
}
Expression exp = sel.getExpression();
Function fn = SQLParserExpressionCompiler.detectAggregatedFunction(exp);
if (fn != null) {
int type = SQLParserExpressionCompiler.getAggregateFunctionType(exp, inputSchema);
ColumnRef additionalColumn = SQLParserExpressionCompiler.getAggregateFunctionArgument(fn, inputSchema);
aggtypes.add(fixMySqlBackTicks(fn.getName().toLowerCase()));
if (additionalColumn != null) {
// SELECT min(col) -> we have to add "col" to the scan
IntHolder pos = new IntHolder();
findColumnInSchema(additionalColumn.tableName, fixMySqlBackTicks(additionalColumn.name), originalTableSchema, pos);
checkSupported(pos.value >= 0);
argLists.add(Collections.singletonList(pos.value));
} else {
argLists.add(Collections.emptyList());
}
if (fieldName == null) {
fieldName = "expr$" + k;
}
Column col = Column.column(fieldName, type);
outputSchemaAggregationResults.add(col);
originalFieldNames.add(fieldName);
originalOutputSchema.add(col);
fieldnamesAggregationResults.add(fieldName);
projectionAggregationResults.add(k);
} else if (exp instanceof net.sf.jsqlparser.schema.Column) {
net.sf.jsqlparser.schema.Column colRef = (net.sf.jsqlparser.schema.Column) exp;
String tableAlias = extractTableName(colRef);
ColumnRef colInSchema = findColumnInSchema(tableAlias, fixMySqlBackTicks(colRef.getColumnName()), inputSchema, new IntHolder());
checkSupported(colInSchema != null);
if (fieldName == null) {
fieldName = colInSchema.name;
}
Column col = Column.column(fieldName, colInSchema.type);
originalFieldNames.add(fieldName);
originalOutputSchema.add(col);
nonAggregateColumns.add(fieldName);
} else {
checkSupported(false);
}
k++;
}
// GROUP BY
List<Integer> groupedFieldsIndexes = new ArrayList<>();
List<Integer> projectionForGroupByFields = new ArrayList<>();
if (groupBy != null) {
checkSupported(groupBy.getGroupingSets() == null || groupBy.getGroupingSets().isEmpty());
int posInGroupBy = 0;
for (Expression exp : groupBy.getGroupByExpressions()) {
if (exp instanceof net.sf.jsqlparser.schema.Column) {
net.sf.jsqlparser.schema.Column colRef = (net.sf.jsqlparser.schema.Column) exp;
String tableName = extractTableName(colRef);
IntHolder pos = new IntHolder();
ColumnRef colInSchema = findColumnInSchema(tableName, fixMySqlBackTicks(colRef.getColumnName()), inputSchema, pos);
checkSupported(colInSchema != null);
groupedFieldsIndexes.add(pos.value);
fieldnamesKeysInGroupBy.add(fixMySqlBackTicks(colRef.getColumnName()));
outputSchemaKeysInGroupBy.add(colInSchema.toColumn());
projectionForGroupByFields.add(posInGroupBy);
} else {
checkSupported(false);
}
posInGroupBy++;
}
} else {
// -> column MUST be in GROUP BY !
for (String s : nonAggregateColumns) {
throw new StatementExecutionException("field " + s + " MUST appear in GROUP BY clause");
}
}
PlannerOp op;
if (!groupedFieldsIndexes.isEmpty()) {
// this is tricky,
// the AggregateOp always create a result in the form of
// FIELD1,FIELD2......AGG1,AGG2
// Basically it puts all of the results of the aggregation to the right
// So we have to add a projection that makes the resultset appear
// as it is defined in the SELECT clause
List<Column> outputSchema = new ArrayList<>();
outputSchema.addAll(outputSchemaKeysInGroupBy);
outputSchema.addAll(outputSchemaAggregationResults);
Column[] outputSchemaArray = outputSchema.toArray(new Column[0]);
List<String> aggreateFieldNames = new ArrayList<>();
aggreateFieldNames.addAll(fieldnamesKeysInGroupBy);
aggreateFieldNames.addAll(fieldnamesAggregationResults);
op = new AggregateOp(input, aggreateFieldNames.toArray(new String[0]), outputSchemaArray, aggtypes.toArray(new String[0]), argLists, groupedFieldsIndexes);
String[] reodereded = originalFieldNames.toArray(new String[0]);
int[] projections = new int[originalFieldNames.size()];
int i = 0;
for (int pos : projectionForGroupByFields) {
projections[pos] = i++;
}
for (int pos : projectionAggregationResults) {
projections[pos] = i++;
}
ProjectOp.ZeroCopyProjection projection = new ProjectOp.ZeroCopyProjection(reodereded, originalOutputSchema.toArray(new Column[0]), projections);
op = new ProjectOp(projection, op);
} else {
// no "GROUP BY", so no need for an additional projection
// this is the SELECT COUNT(*) FROM TABLE case
op = new AggregateOp(input, originalFieldNames.toArray(new String[0]), originalOutputSchema.toArray(new Column[0]), aggtypes.toArray(new String[0]), argLists, groupedFieldsIndexes);
}
return op;
}
Aggregations