Skip to content

Commit

Permalink
Use key instead of string to avoid duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
bdw429s committed Jul 11, 2023
1 parent a5caadc commit 59322fb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 34 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/lucee/runtime/db/HSQLDBHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public static QueryImpl __execute(PageContext pc, SQL sql, int maxrows, int fetc
nqr = new QueryImpl(pc, dc, sql, maxrows, fetchsize, timeout, "query", null, false, false, null);
} catch (PageException pe) {
throw pe;
}
}
finally {
DBUtil.setReadOnlyEL(conn, false);
DBUtil.commitEL(conn);
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/lucee/runtime/db/QoQ.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ private Query doUnionDistinct(PageContext pc, Query previous, Query target, SQL
}

// Initialize our object to track the partitions
QueryPartitions queryPartitions = new QueryPartitions(sql, selectExpressions, new Expression[0], newTarget, new HashSet<String>(), this);
QueryPartitions queryPartitions = new QueryPartitions(sql, selectExpressions, new Expression[0], newTarget, new HashSet<Key>(), this);

// Add in all the rows from our previous work
for (int row = 1; row <= previous.getRecordcount(); row++) {
Expand Down
38 changes: 19 additions & 19 deletions core/src/main/java/lucee/runtime/sql/QueryPartitions.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*
**/
package lucee.runtime.sql;

Expand Down Expand Up @@ -64,7 +64,7 @@ public class QueryPartitions {

/**
* Constructor
*
*
* @param sql
* @param columns
* @param groupbys
Expand All @@ -73,7 +73,7 @@ public class QueryPartitions {
* @param qoQ
* @throws PageException
*/
public QueryPartitions(SQL sql, Expression[] columns, Expression[] groupbys, Query target, Set<String> additionalColumns, QoQ qoQ) throws PageException {
public QueryPartitions(SQL sql, Expression[] columns, Expression[] groupbys, Query target, Set<Key> additionalColumns, QoQ qoQ) throws PageException {
this.sql = sql;
this.qoQ = qoQ;
this.columns = columns;
Expand All @@ -93,8 +93,8 @@ public QueryPartitions(SQL sql, Expression[] columns, Expression[] groupbys, Que

// Convert these strings to Keys now so we don't do it over and over later
this.additionalColumns = new HashSet<Collection.Key>();
for (String col: additionalColumns) {
this.additionalColumns.add(Caster.toKey(col));
for (Key col: additionalColumns) {
this.additionalColumns.add(col);
}
// Convert these Expression aliases to Keys now so we don't do it over and over later
this.columnKeys = new Collection.Key[columns.length];
Expand All @@ -105,9 +105,9 @@ public QueryPartitions(SQL sql, Expression[] columns, Expression[] groupbys, Que

/**
* Adds empty partition for aggregating empty results
*
*
* @param source Source query to get data from
* @param target target query (for column reference)
* @param target target query (for column reference)
* @throws PageException
*/
public void addEmptyPartition( Query source, Query target ) throws PageException {
Expand All @@ -117,7 +117,7 @@ public void addEmptyPartition( Query source, Query target ) throws PageException
/**
* Call this to add a single row to the proper partition finaizedColumnVals is true when all data in
* the source Query is fully realized and there are no expressions left to evaluate
*
*
* @param pc PageContext
* @param source Source query to get data from
* @param row Row to get data from
Expand Down Expand Up @@ -150,7 +150,7 @@ public void addRow(PageContext pc, Query source, int row, boolean finalizedColum
// be added later, but there's no use filling up the partition with place holders
else {
for (int cell = 0; cell < columns.length; cell++) {

// Literal values
if (columns[cell] instanceof Value) {
Value v = (Value) columns[cell];
Expand All @@ -176,7 +176,7 @@ else if (columns[cell] instanceof ColumnExpression) {

/**
* Generate a unique string that represents the column data being grouped on
*
*
* @param pc PageContext
* @param source QueryImpl to get data from. Note, operations have not yet been processed
* @param row Row to get data from
Expand Down Expand Up @@ -208,7 +208,7 @@ public String buildPartitionKey(PageContext pc, Query source, int row, boolean f

/**
* Helper function to turn column data into string
*
*
* @param value
* @param col
* @return
Expand All @@ -234,7 +234,7 @@ private String createUniqueValue(String value, String col) throws PageException

/**
* Get number of partitions
*
*
* @return
*/
public int getPartitionCount() {
Expand All @@ -243,7 +243,7 @@ public int getPartitionCount() {

/**
* Get partition Map
*
*
* @return
*/
public HashMap<String, Query> getPartitions() {
Expand All @@ -252,7 +252,7 @@ public HashMap<String, Query> getPartitions() {

/**
* Get array of grouped Query objects
*
*
* @return
*/
public Query[] getPartitionArray() {
Expand All @@ -262,7 +262,7 @@ public Query[] getPartitionArray() {
/**
* Create new Query for a partition. Needs to have all ColumnExpressions in the final select as well
* as any additional columns required for operation expressions
*
*
* @param target Query for target data (for column refernces)
* @param source source query we're getting data from
* @param finalizedColumnVals If we're adding finalized data, just copy it
Expand Down
18 changes: 9 additions & 9 deletions core/src/main/java/lucee/runtime/sql/Select.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*
**/
package lucee.runtime.sql;

Expand All @@ -35,7 +35,7 @@

public class Select {
private List selects = new ArrayList();
private Set<String> additionalColumns = new HashSet();
private Set<Key> additionalColumns = new HashSet();
private List froms = new ArrayList();
private Operation where;
private List groupbys = new ArrayList();
Expand Down Expand Up @@ -94,20 +94,20 @@ public void setTop(ValueNumber top) {
this.top = top;
}

public void calcAdditionalColumns(Set<String> allColumns) {
public void calcAdditionalColumns(Set<Key> allColumns) {
// Remove any columns we are explicitly selecting
for (Expression expSelect: getSelects()) {
if (expSelect instanceof ColumnExpression) {
ColumnExpression ce = (ColumnExpression) expSelect;
allColumns.remove(ce.getColumnName());
allColumns.remove(ce.getColumn());
}
}
// What's left are columns used by functions and aggregates,
// but not directly part of the final result
this.additionalColumns = allColumns;
}

public Set<String> getAdditionalColumns() {
public Set<Key> getAdditionalColumns() {
return this.additionalColumns;
}

Expand Down
11 changes: 7 additions & 4 deletions core/src/main/java/lucee/runtime/sql/SelectParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import lucee.runtime.sql.exp.value.ValueNumber;
import lucee.runtime.sql.exp.value.ValueString;
import lucee.runtime.db.SQL;
import lucee.runtime.type.Collection.Key;

public class SelectParser {

Expand All @@ -56,7 +57,7 @@ public class SelectParser {
*/

private int columnIndex = 0;
private Set<String> allColumns = new HashSet<String>();
private Set<Key> allColumns = new HashSet<Key>();

// select <select-statement> from <tables> where <where-statement>
public Selects parse(String sql) throws SQLParserException {
Expand Down Expand Up @@ -122,7 +123,7 @@ public Selects parse(String sql) throws SQLParserException {
}
}
select.calcAdditionalColumns(allColumns);
allColumns = new HashSet<String>();
allColumns = new HashSet<Key>();
selects.addSelect(select);

runAgain = false;
Expand Down Expand Up @@ -583,16 +584,18 @@ private Expression column(ParserString raw) throws SQLParserException {
if ("false".equalsIgnoreCase(name)) return new ValueBoolean(false);
if ("null".equalsIgnoreCase(name)) return new ValueNull();
}

ColumnExpression column = new ColumnExpression(name, name.equals("?") ? columnIndex++ : 0);
allColumns.add(column.getColumnName());

raw.removeSpace();
while (raw.forwardIfCurrent(".")) {
raw.removeSpace();
String sub = identifier(raw, hb);
if (sub == null) throw new SQLParserException("invalid column definition");
column.setSub(sub);
}

allColumns.add(column.getColumn());

raw.removeSpace();
if (raw.forwardIfCurrent('(')) {
String thisName = column.getFullName().toLowerCase();
Expand Down

0 comments on commit 59322fb

Please sign in to comment.