
prerna.query.interpreters.sql.ImpalaSqlInterpreter Maven / Gradle / Ivy
The newest version!
package prerna.query.interpreters.sql;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import org.openrdf.query.QueryEvaluationException;
import org.openrdf.query.TupleQueryResult;
import prerna.algorithm.api.ITableDataFrame;
import prerna.algorithm.api.SemossDataType;
import prerna.engine.api.IDatabaseEngine;
import prerna.query.interpreters.AbstractQueryInterpreter;
import prerna.query.querystruct.HardSelectQueryStruct;
import prerna.query.querystruct.SelectQueryStruct;
import prerna.query.querystruct.filters.AndQueryFilter;
import prerna.query.querystruct.filters.IQueryFilter;
import prerna.query.querystruct.filters.OrQueryFilter;
import prerna.query.querystruct.filters.SimpleQueryFilter;
import prerna.query.querystruct.filters.SimpleQueryFilter.FILTER_TYPE;
import prerna.query.querystruct.joins.BasicRelationship;
import prerna.query.querystruct.joins.IRelation;
import prerna.query.querystruct.selectors.IQuerySelector;
import prerna.query.querystruct.selectors.IQuerySelector.SELECTOR_TYPE;
import prerna.query.querystruct.selectors.IQuerySort;
import prerna.query.querystruct.selectors.QueryArithmeticSelector;
import prerna.query.querystruct.selectors.QueryColumnOrderBySelector;
import prerna.query.querystruct.selectors.QueryColumnOrderBySelector.ORDER_BY_DIRECTION;
import prerna.query.querystruct.selectors.QueryColumnSelector;
import prerna.query.querystruct.selectors.QueryConstantSelector;
import prerna.query.querystruct.selectors.QueryFunctionSelector;
import prerna.query.querystruct.selectors.QueryOpaqueSelector;
import prerna.sablecc2.om.PixelDataType;
import prerna.sablecc2.om.nounmeta.NounMetadata;
import prerna.util.Constants;
import prerna.util.Utility;
import prerna.util.sql.AbstractSqlQueryUtil;
import prerna.util.sql.RdbmsTypeEnum;
import prerna.util.sql.SqlQueryUtilFactory;
public class ImpalaSqlInterpreter extends AbstractQueryInterpreter {
// this keeps the table aliases
private Hashtable aliases = new Hashtable();
// keep track of processed tables used to ensure we don't re-add tables into the
// from string
private Hashtable tableProcessed = new Hashtable();
// we will keep track of the conceptual names to physical names so we don't
// re-query the owl multiple times
private transient Hashtable conceptualConceptToPhysicalMap = new Hashtable();
// need to also keep track of the properties
private transient Hashtable conceptualPropertyToPhysicalMap = new Hashtable();
// need to keep track of the primary key for tables
private transient Map primaryKeyCache = new HashMap();
// we can create a statement without an engine...
// but everything needs to be the physical schema
private IDatabaseEngine engine;
private ITableDataFrame frame;
// where the wheres are all kept
// key is always a combination of concept and comparator
// and the values are values
private List filterStatements = new Vector();
private transient Map relationshipConceptPropertiesMap = new HashMap();
private String selectors = "";
private Set selectorList = new HashSet();
// keep selector alias
private List selectorAliases = new Vector();
// keep list of columns for tables
private Map> retTableToCols = new HashMap>();
private int uniqueSelectorCount;
private List froms = new Vector();
// store the joins in the object for easy use
private SqlJoinStructList joinStructList = new SqlJoinStructList();
private AbstractSqlQueryUtil queryUtil = SqlQueryUtilFactory.initialize(RdbmsTypeEnum.IMPALA);
public ImpalaSqlInterpreter() {
}
public ImpalaSqlInterpreter(IDatabaseEngine engine) {
this.engine = engine;
this.queryUtil = SqlQueryUtilFactory.initialize(RdbmsTypeEnum.IMPALA);
}
public ImpalaSqlInterpreter(ITableDataFrame frame) {
this.frame = frame;
}
/**
* Main method to invoke to take the QueryStruct to compose the appropriate SQL
* query
*/
@Override
public String composeQuery() {
if (this.qs instanceof HardSelectQueryStruct) {
return ((HardSelectQueryStruct) this.qs).getQuery();
}
/*
* Need to create the query... This to consider: 1) the user is going to be
* using the conceptual names as defined by the OWL (if present and OWL is the
* improved version). This has a few consequences: 1.a) when a user enters a
* table name, we need to determine what the primary key is for that table 1.b)
* need to consider what tables are used within joins and which are not. this
* will determine when we add it to the from clause or if the table will be
* defined via the join
*/
// we do the joins since when we get to adding the from portion of the query
// we want to make sure that table is not used within the joins
addJoins();
addSelectors();
addFilters();
// If there is more than 1 unique count needed, a seperate query is needed to be
// generated
if (uniqueSelectorCount > 1) {
String query = createUniqueCountQuery();
return query;
}
StringBuilder query = new StringBuilder("SELECT ");
String distinct = "";
if (((SelectQueryStruct) this.qs).isDistinct()) {
// distinct = "DISTINCT ";
}
if (this.engine != null && !engine.isBasic() && joinStructList.isEmpty()) {
// if there are no joins, we know we are querying from a single table
// the vast majority of the time, there shouldn't be any duplicates if
// we are selecting all the columns
String table = froms.get(0)[0];
if (engine != null && !engine.isBasic()) {
String physicalUri = engine.getPhysicalUriFromPixelSelector(table);
if ((engine.getPhysicalConcepts().size() == 1)
&& (engine.getPropertyUris4PhysicalUri(physicalUri).size() + 1) == selectorList.size()) {
// plus one is for the concept itself
// no distinct needed
query.append(selectors);
} else {
query.append(distinct).append(selectors);
}
} else {
// need a distinct
query.append(distinct).append(selectors).append(" FROM ");
}
} else {
// default is to use a distinct
query.append(distinct).append(selectors);
}
// if there is a join
// can only have one table in from in general sql case
// thus, the order matters
// so get a good starting from table
// we can use any of the froms that is not part of the join
boolean appendStartingFrom = true;
if (this.joinStructList.isEmpty()) {
appendStartingFrom = false;
query.append(" FROM ");
if (this.froms.isEmpty() && this.frame != null) {
query.append(frame.getName());
} else {
String[] startPoint = this.froms.get(0);
query.append(startPoint[0]).append(" ").append(startPoint[1]).append(" ");
}
} else {
query.append(" ").append(joinStructList.getJoinSyntax(appendStartingFrom));
}
int numFilters = this.filterStatements.size();
for (int i = 0; i < numFilters; i++) {
if (i == 0) {
query.append(" WHERE ");
} else {
query.append(" AND ");
}
query.append(this.filterStatements.get(i).toString());
}
// boolean firstTime = true;
// for(String key : this.andWhereFilters.keySet()) {
// String whereStatement = this.andWhereFilters.get(key);
// if(firstTime) {
// firstTime = false;
// query.append(" WHERE ").append(whereStatement);
// } else {
// query.append(" AND ").append(whereStatement);
// }
// }
// for(String key : this.orWhereFilters.keySet()) {
// String whereStatement = this.orWhereFilters.get(key);
// if(firstTime) {
// firstTime = false;
// query.append(" WHERE ").append(whereStatement);
// } else {
// query.append(" OR ").append(whereStatement);
// }
// }
// grab the order by and get the corresponding display name for that order by
// column
query = appendGroupBy(query);
query = appendOrderBy(query);
long limit = ((SelectQueryStruct) this.qs).getLimit();
long offset = ((SelectQueryStruct) this.qs).getOffset();
query = this.queryUtil.addLimitOffsetToQuery(query, limit, offset);
if (query.length() > 500) {
logger.info("SQL QUERY.... " + Utility.cleanLogString(query.substring(0, 500)) + "...");
} else {
logger.info("SQL QUERY.... " + Utility.cleanLogString(query.toString()));
}
return query.toString();
}
////////////////////// MULTIPLE UNIQUE COUNTS////////////////////
public String createUniqueCountQuery() {
// clear all lists and alisas as they will be repopulated
selectors = "";
selectorList.clear();
selectorAliases.clear();
StringBuilder query1 = new StringBuilder("SELECT ");
List selectorData = qs.getSelectors();
// column selectors stored here
List columnSelectors = new ArrayList();
// unique selectors stored here
List unSelectors = new ArrayList();
// other math/other selectors stored here
List mathSelectors = new ArrayList();
// for each selector, add it to the proper list and start appending column and
// other selectors to the query
for (int i = 0; i < selectorData.size(); i++) {
IQuerySelector selector = selectorData.get(i);
if ((selector.getSelectorType() == IQuerySelector.SELECTOR_TYPE.COLUMN)) {
columnSelectors.add(selector);
query1.append("t0." + selector.getAlias() + ", ");
}
if ((selector.getSelectorType() == IQuerySelector.SELECTOR_TYPE.FUNCTION)) {
if (((QueryFunctionSelector) selector).getFunction().equalsIgnoreCase("uniquecount")) {
unSelectors.add(selector);
} else {
mathSelectors.add(selector);
query1.append("t0." + selector.getAlias() + ", ");
}
}
}
// add unique selectors to the query
for (int j = 0; j < unSelectors.size(); j++) {
IQuerySelector selector = unSelectors.get(j);
if (j < unSelectors.size() - 1) {
query1.append("t" + (j + 1) + "." + selector.getAlias() + ", ");
} else {
query1.append("t" + (j + 1) + "." + selector.getAlias() + " ");
}
}
query1.append("FROM ");
for (IQuerySelector selector : columnSelectors) {
addSelector(selector);
}
for (IQuerySelector selector : mathSelectors) {
addSelector(selector);
}
// get the database/table name and add to string
StringBuilder tableName = new StringBuilder();
String[] startPoint = froms.get(0);
tableName.append(startPoint[0]).append(" ").append(startPoint[1]).append(" ");
query1.append("(SELECT ").append(selectors).append(" FROM ").append(tableName);
query1 = appendGroupBy(query1);
query1.append(") t0 inner join ");
System.out.println(query1.toString());
StringBuilder queryWhere = new StringBuilder();
// for each unique selector, create a select statement that will inner join'ed
// for each selector add to the Where query
for (int k = 0; k < unSelectors.size(); k++) {
selectors = "";
for (IQuerySelector selector : columnSelectors) {
addSelector(selector);
}
// query 2 adds to select statement, query3 adds to where statement
StringBuilder query2 = new StringBuilder();
StringBuilder query3 = new StringBuilder();
IQuerySelector selector = unSelectors.get(k);
addSelector(selector);
query2.append("(SELECT ").append(selectors).append(" FROM ").append(tableName);
query2 = appendGroupBy(query2);
query2.append(") t").append(k + 1);
query3.append("t0.").append(columnSelectors.get(0).getAlias()).append(" = t").append(k + 1).append(".")
.append(columnSelectors.get(0).getAlias());
if (k < unSelectors.size() - 1) {
query2.append(" inner join ");
query3.append(" AND ");
}
queryWhere.append(query3);
query1.append(query2);
}
query1.append(" WHERE ").append(queryWhere);
long limit = ((SelectQueryStruct) this.qs).getLimit();
long offset = ((SelectQueryStruct) this.qs).getOffset();
query1 = this.queryUtil.addLimitOffsetToQuery(query1, limit, offset);
if (query1.length() > 500) {
logger.info("SQL QUERY.... " + Utility.cleanLogString(query1.substring(0, 500)) + "...");
} else {
logger.info("SQL QUERY.... " + Utility.cleanLogString(query1.toString()));
}
System.out.println(query1);
return query1.toString();
}
//////////////////////////// adding selectors
//////////////////////////// //////////////////////////////////////////
/**
* Loops through the selectors defined in the QS to add them to the selector
* string and considers if the table should be added to the from string
*/
public void addSelectors() {
uniqueSelectorCount = 0;
List selectorData = qs.getSelectors();
for (IQuerySelector selector : selectorData) {
if (selector.getSelectorType() == IQuerySelector.SELECTOR_TYPE.FUNCTION) {
// count the number of unique selectors. if over 2 a different query will have
// to be built
if (((QueryFunctionSelector) selector).getFunction().equalsIgnoreCase("uniquecount")) {
uniqueSelectorCount++;
}
}
addSelector(selector);
}
}
private void addSelector(IQuerySelector selector) {
String alias = selector.getAlias();
alias = alias.toLowerCase();
selector.setAlias(alias);
String newSelector = processSelector(selector, true) + " AS " + "\"" + alias + "\"";
if (selectors.length() == 0) {
selectors = newSelector;
} else {
selectors += " , " + newSelector;
}
selectorList.add(newSelector);
selectorAliases.add(alias);
}
/**
* Method is used to generate the appropriate syntax for each type of selector
* Note, this returns everything without the alias since this is called again
* from the base methods it calls to allow for complex math expressions
*
* @param selector
* @return
*/
/*
* private String processSelector(IQuerySelector selector, boolean
* addProcessedColumn) { IQuerySelector.SELECTOR_TYPE selectorType =
* selector.getSelectorType(); if(selectorType ==
* IQuerySelector.SELECTOR_TYPE.CONSTANT) {
* System.out.println("Selector Type = Constant"); return
* processConstantSelector((QueryConstantSelector) selector); } else
* if(selectorType == IQuerySelector.SELECTOR_TYPE.COLUMN) {
* System.out.println("Selector Type = Column");
*
* return processColumnSelector((QueryColumnSelector) selector,
* addProcessedColumn); } else if(selectorType ==
* IQuerySelector.SELECTOR_TYPE.FUNCTION) {
* System.out.println("Selector Type = MultiMath");
*
* return processFunctionSelector((QueryFunctionSelector) selector); } else
* if(selectorType == IQuerySelector.SELECTOR_TYPE.ARITHMETIC) {
* System.out.println("Selector Type = Arithmetic");
*
* return processArithmeticSelector((QueryArithmeticSelector) selector); } else
* if(selectorType == IQuerySelector.SELECTOR_TYPE.OPAQUE) { return
* processOpaqueSelector((QueryOpaqueSelector) selector); }
*
* return null; }
*/
private String processSelector(IQuerySelector selector, boolean addProcessedColumn) {
IQuerySelector.SELECTOR_TYPE selectorType = selector.getSelectorType();
if (selectorType == IQuerySelector.SELECTOR_TYPE.CONSTANT) {
return processConstantSelector((QueryConstantSelector) selector);
} else if (selectorType == IQuerySelector.SELECTOR_TYPE.COLUMN) {
return processColumnSelector((QueryColumnSelector) selector, addProcessedColumn);
} else if (selectorType == IQuerySelector.SELECTOR_TYPE.FUNCTION) {
return processFunctionSelector((QueryFunctionSelector) selector);
} else if (selectorType == IQuerySelector.SELECTOR_TYPE.ARITHMETIC) {
return processArithmeticSelector((QueryArithmeticSelector) selector);
} else if (selectorType == IQuerySelector.SELECTOR_TYPE.OPAQUE) {
return processOpaqueSelector((QueryOpaqueSelector) selector);
}
return null;
}
private String processConstantSelector(QueryConstantSelector selector) {
Object constant = selector.getConstant();
if (constant instanceof Number) {
return constant.toString();
} else {
return "\"" + constant + "\"";
}
}
/**
* The second
*
* @param selector
* @param isTrueColumn
* @return
*/
private String processColumnSelector(QueryColumnSelector selector, boolean notEmbeddedColumn) {
String table = selector.getTable();
String colName = selector.getColumn();
String tableAlias = getAlias(getPhysicalTableNameFromConceptualName(table));
// will be getting the physical column name
String physicalColName = colName;
// if engine is not null, get the info from the engine
if (engine != null && !engine.isBasic()) {
// if the colName is the primary key placeholder
// we will go ahead and grab the primary key from the table
if (colName.equals(SelectQueryStruct.PRIM_KEY_PLACEHOLDER)) {
physicalColName = getPrimKey4Table(table);
// the display name is defaulted to the table name
} else {
// default assumption is the info being passed is the conceptual name
// get the physical from the conceptual
physicalColName = getPhysicalPropertyNameFromConceptualName(table, colName);
}
}
// need to perform this check
// if there are no joins
// we need to have a from table
if (this.joinStructList.isEmpty()) {
addFrom(table, tableAlias);
}
// keep track of all the processed columns
if (notEmbeddedColumn) {
this.retTableToCols.putIfAbsent(table, new Vector());
this.retTableToCols.get(table).add(colName);
}
return tableAlias + "." + physicalColName;
}
private String processFunctionSelector(QueryFunctionSelector selector) {
List innerSelectors = selector.getInnerSelector();
String function = selector.getFunction();
StringBuilder expression = new StringBuilder();
expression.append(this.queryUtil.getSqlFunctionSyntax(function)).append("(");
if (selector.isDistinct()) {
expression.append("DISTINCT ");
}
int size = innerSelectors.size();
for (int i = 0; i < size; i++) {
if (i == 0) {
expression.append(processSelector(innerSelectors.get(i), false));
} else {
expression.append(",").append(processSelector(innerSelectors.get(i), false));
}
}
expression.append(")");
return expression.toString();
}
private String processArithmeticSelector(QueryArithmeticSelector selector) {
IQuerySelector leftSelector = selector.getLeftSelector();
IQuerySelector rightSelector = selector.getRightSelector();
String mathExpr = selector.getMathExpr();
if (mathExpr.equals("/")) {
return "(" + processSelector(leftSelector, false) + " " + mathExpr + " NULLIF("
+ processSelector(rightSelector, false) + ",0))";
} else {
return "(" + processSelector(leftSelector, false) + " " + mathExpr + " "
+ processSelector(rightSelector, false) + ")";
}
}
private String processOpaqueSelector(QueryOpaqueSelector selector) {
if (this.joinStructList.isEmpty() && selector.getTable() != null) {
addFrom(selector.getTable(), selector.getTable());
}
return selector.getQuerySelectorSyntax();
}
//////////////////////////////////// end adding selectors
//////////////////////////////////// /////////////////////////////////////
/////////////////////////////////// adding from
/////////////////////////////////// ////////////////////////////////////////////////
/**
* Adds the form statement for each table
*
* @param conceptualTableName The name of the table
*/
private void addFrom(String conceptualTableName, String alias) {
// need to determine if we can have multiple froms or not
// we don't want to add the from table multiple times as this is invalid in sql
if (!tableProcessed.containsKey(conceptualTableName)) {
tableProcessed.put(conceptualTableName, "true");
// we want to use the physical table name
String physicalTableName = getPhysicalTableNameFromConceptualName(conceptualTableName);
froms.add(new String[] { physicalTableName, alias });
}
}
////////////////////////////////////// end adding from
////////////////////////////////////// ///////////////////////////////////////
////////////////////////////////////// adding joins
////////////////////////////////////// /////////////////////////////////////////////
/**
* Adds the joins for the query
*/
public void addJoins() {
for (IRelation relationship : qs.getRelations()) {
if (relationship.getRelationType() == IRelation.RELATION_TYPE.BASIC) {
BasicRelationship rel = (BasicRelationship) relationship;
String from = rel.getFromConcept();
String joinType = rel.getJoinType();
String to = rel.getToConcept();
// String comparator = rel.getComparator();
// if(comparator == null) {
// comparator = "=";
// }
addJoin(from, joinType, to);
} else {
logger.info("Cannot process relationship of type: " + relationship.getRelationType());
}
}
// Map> relationsData = qs.getRelations();
// // loop through all the relationships
// // realize we can be joining on properties within a table
// for(String startConceptProperty : relationsData.keySet() ) {
// // the key for this object is the specific type of join to be used
// // between this instance and all the other ones
// Map joinMap = relationsData.get(startConceptProperty);
// for(String comparator : joinMap.keySet()) {
// List joinColumns = joinMap.get(comparator);
// for(String endConceptProperty : joinColumns) {
// // go through and perform the actual join
// addJoin(startConceptProperty, comparator, endConceptProperty);
// }
// }
// }
}
/**
* Adds the join to the relationHash which gets added to the query in
* composeQuery
*
* @param fromCol The starting column, this can be just a table or
* table__column
* @param joinType The type of join
* @param toCol The ending column, this can be just a table or table__column
*/
private void addJoin(String fromCol, String joinType, String toCol) {
// get the parts of the join
String[] relConProp = getRelationshipConceptProperties(fromCol, toCol);
String targetTable = relConProp[0];
String targetColumn = relConProp[1];
String sourceTable = relConProp[2];
String sourceColumn = relConProp[3];
String compName = joinType.replace(".", " ");
SqlJoinStruct jStruct = new SqlJoinStruct();
jStruct.setJoinType(compName);
// add source
jStruct.setSourceTable(sourceTable);
jStruct.setSourceTableAlias(getAlias(sourceTable));
jStruct.setSourceCol(sourceColumn);
// add target
jStruct.setTargetTable(targetTable);
jStruct.setTargetTableAlias(getAlias(targetTable));
jStruct.setTargetCol(targetColumn);
joinStructList.addJoin(jStruct);
}
////////////////////////////////////////// end adding joins
////////////////////////////////////////// ///////////////////////////////////////
////////////////////////////////////////// adding filters
////////////////////////////////////////// ////////////////////////////////////////////
public void addFilters() {
List filters = qs.getCombinedFilters().getFilters();
for (IQueryFilter filter : filters) {
StringBuilder filterSyntax = processFilter(filter);
if (filterSyntax != null) {
this.filterStatements.add(filterSyntax.toString());
}
}
}
private StringBuilder processFilter(IQueryFilter filter) {
IQueryFilter.QUERY_FILTER_TYPE filterType = filter.getQueryFilterType();
if (filterType == IQueryFilter.QUERY_FILTER_TYPE.SIMPLE) {
return processSimpleQueryFilter((SimpleQueryFilter) filter);
} else if (filterType == IQueryFilter.QUERY_FILTER_TYPE.AND) {
return processAndQueryFilter((AndQueryFilter) filter);
} else if (filterType == IQueryFilter.QUERY_FILTER_TYPE.OR) {
return processOrQueryFilter((OrQueryFilter) filter);
}
return null;
}
private StringBuilder processOrQueryFilter(OrQueryFilter filter) {
StringBuilder filterBuilder = new StringBuilder();
List filterList = filter.getFilterList();
int numAnds = filterList.size();
for (int i = 0; i < numAnds; i++) {
if (i == 0) {
filterBuilder.append("(");
} else {
filterBuilder.append(" OR ");
}
filterBuilder.append(processFilter(filterList.get(i)));
}
filterBuilder.append(")");
return filterBuilder;
}
private StringBuilder processAndQueryFilter(AndQueryFilter filter) {
StringBuilder filterBuilder = new StringBuilder();
List filterList = filter.getFilterList();
int numAnds = filterList.size();
for (int i = 0; i < numAnds; i++) {
if (i == 0) {
filterBuilder.append("(");
} else {
filterBuilder.append(" AND ");
}
filterBuilder.append(processFilter(filterList.get(i)));
}
filterBuilder.append(")");
return filterBuilder;
}
private StringBuilder processSimpleQueryFilter(SimpleQueryFilter filter) {
NounMetadata leftComp = filter.getLComparison();
NounMetadata rightComp = filter.getRComparison();
String thisComparator = filter.getComparator();
FILTER_TYPE fType = filter.getSimpleFilterType();
if (fType == FILTER_TYPE.COL_TO_COL) {
return addSelectorToSelectorFilter(leftComp, rightComp, thisComparator);
} else if (fType == FILTER_TYPE.COL_TO_VALUES) {
return addSelectorToValuesFilter(leftComp, rightComp, thisComparator);
} else if (fType == FILTER_TYPE.VALUES_TO_COL) {
// same logic as above, just switch the order and reverse the comparator if it
// is numeric
return addSelectorToValuesFilter(rightComp, leftComp,
IQueryFilter.getReverseNumericalComparator(thisComparator));
} else if (fType == FILTER_TYPE.COL_TO_QUERY) {
return addSelectorToQueryFilter(leftComp, rightComp, thisComparator);
} else if (fType == FILTER_TYPE.QUERY_TO_COL) {
return addSelectorToQueryFilter(leftComp, rightComp,
IQueryFilter.getReverseNumericalComparator(thisComparator));
} else if (fType == FILTER_TYPE.VALUE_TO_VALUE) {
// WHY WOULD YOU DO THIS!!!
return addValueToValueFilter(rightComp, leftComp, thisComparator);
}
return null;
}
private StringBuilder addSelectorToQueryFilter(NounMetadata leftComp, NounMetadata rightComp,
String thisComparator) {
// get the left side
IQuerySelector leftSelector = (IQuerySelector) leftComp.getValue();
String leftSelectorExpression = processSelector(leftSelector, false);
SelectQueryStruct subQs = (SelectQueryStruct) rightComp.getValue();
ImpalaSqlInterpreter innerInterpreter;
try {
innerInterpreter = this.getClass().newInstance();
if (this.frame != null) {
// subQs = QueryStructConverter.getPhysicalQs(subQs, this.frame.getMetaData());
}
innerInterpreter.setQueryStruct(subQs);
innerInterpreter.setLogger(this.logger);
String innerQuery = innerInterpreter.composeQuery();
StringBuilder filterBuilder = new StringBuilder(leftSelectorExpression);
if (thisComparator.trim().equals("==")) {
filterBuilder.append(" IN ( ").append(innerQuery).append(" ) ");
} else if (thisComparator.trim().equals("!=") || thisComparator.equals("<>")) {
filterBuilder.append(" NOT IN ( ").append(innerQuery).append(" ) ");
} else {
filterBuilder.append(" ").append(thisComparator).append(" (").append(innerQuery).append(")");
}
return filterBuilder;
} catch (InstantiationException | IllegalAccessException e) {
logger.error(Constants.STACKTRACE, e);
}
return null;
}
/**
* Add filter for a column to values
*
* @param filter
* @param leftComp
* @param rightComp
* @param thisComparator
*/
private StringBuilder addSelectorToValuesFilter(NounMetadata leftComp, NounMetadata rightComp,
String thisComparator) {
// get the left side
IQuerySelector leftSelector = (IQuerySelector) leftComp.getValue();
String leftSelectorExpression = processSelector(leftSelector, false);
String leftDataType = leftSelector.getDataType();
// if it is null, then we know we have a column
// need to grab from metadata
if (leftDataType == null) {
String left_concept_property = leftSelector.getQueryStructName();
String[] leftConProp = getConceptProperty(left_concept_property);
String leftConcept = leftConProp[0];
String leftProperty = leftConProp[1];
if (engine != null && !engine.isBasic()) {
leftDataType = this.engine
.getDataTypes("http://semoss.org/ontologies/Concept/" + leftProperty + "/" + leftConcept);
// ugh, need to try if it is a property
if (leftDataType == null) {
leftDataType = this.engine.getDataTypes(
"http://semoss.org/ontologies/Relation/Contains/" + leftProperty + "/" + leftConcept);
}
if (leftDataType != null) {
leftDataType = leftDataType.replace("TYPE:", "");
}
} else if (frame != null) {
leftDataType = this.frame.getMetaData().getHeaderTypeAsString(left_concept_property);
}
}
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy