Package adams.parser
Class SpreadSheetQuery
-
- All Implemented Interfaces:
Destroyable,GlobalInfoSupporter,LoggingLevelHandler,LoggingSupporter,OptionHandler,SizeOfHandler,GrammarSupplier,Serializable
public class SpreadSheetQuery extends AbstractSymbolEvaluator<SpreadSheet>
Evaluates spreadsheet subset queries.
The following grammar is used:
expr_list ::= expr_list expr_part | expr_part;
expr_part ::= select | update | delete;
select ::= SELECT col_list [limit]
| SELECT col_list WHERE cond_list [limit]
| SELECT col_list ORDER BY order_list [limit]
| SELECT col_list WHERE cond_list ORDER BY order_list [limit]
| SELECT agg_list
| SELECT agg_list GROUP BY col_list
| SELECT agg_list HAVING cond_list
| SELECT agg_list GROUP BY col_list HAVING cond_list
;
update ::= UPDATE SET upd_list
| UPDATE SET upd_list WHERE cond_list
;
delete ::= DELETE WHERE cond_list
;
col_list ::= col_list COMMA col
| col
| SELECT NUMBER [subsample: <1 = percent; >= 1 number of rows]
;
col ::= *
| COLUMN
| COLUMN AS COLUMN
;
upd_list ::= upd_list COMMA upd | upd;
upd ::= COLUMN = value
;
order_list::= order_list COMMA order | order;
order ::= COLUMN
| COLUMN ASC
| COLUMN DESC
;
cond_list ::= cond_list cond
| cond
;
cond ::= COLUMN < value
| COLUMN <= value
| COLUMN = value
| COLUMN <> value
| COLUMN >= value
| COLUMN > value
| COLUMN REGEXP STRING
| COLUMN IS NULL
| CELLTYPE ( COLUMN ) = "numeric|long|double|boolean|string|time|date|datetime|timestamp|object|missing"
| ( cond )
| cond:c1 AND cond:c2
| cond:c1 OR cond:c2
| NOT cond
;
value ::= NUMBER
| STRING
| PARSE ( "number" , STRING )
| PARSE ( "date" , STRING )
| PARSE ( "time" , STRING )
| PARSE ( "timestamp" , STRING )
;
limit ::= LIMIT NUMBER:max
| LIMIT NUMBER:offset , NUMBER:max
;
agg_list ::= agg_list COMMA agg
| agg
;
agg ::= COUNT [(*)|(COLUMN)] [AS COLUMN]
| MIN ( COLUMN ) [AS COLUMN]
| MAX ( COLUMN ) [AS COLUMN]
| RANGE ( COLUMN ) [AS COLUMN] (= MIN - MAX)
| MEAN ( COLUMN ) [AS COLUMN]
| AVERAGE ( COLUMN ) [AS COLUMN]
| STDEV ( COLUMN ) [AS COLUMN]
| STDEVP ( COLUMN ) [AS COLUMN]
| SUM ( COLUMN ) [AS COLUMN]
| IQR ( COLUMN ) [AS COLUMN]
| INTERQUARTILE ( COLUMN ) [AS COLUMN]
Notes:
- time format: 'HH:mm'
- date format: 'yyyy-MM-dd'
- timestamp format: 'yyyy-MM-dd HH:mm'
- STRING is referring to characters enclosed by double quotes
- COLUMN is either a string with no blanks (consisting of letters, numbers, hyphen or underscore; eg 'MyCol-1') or a bracket enclosed string when containing blanks (eg '[Some other col]')
- columns used in the ORDER BY clause must be present in the SELECT part; also, any alias given to them in SELECT must be used instead of original column name
-logging-level <OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST> (property: loggingLevel) The logging level for outputting errors and debugging output. default: WARNING
-env <java.lang.String> (property: environment) The class to use for determining the environment. default: adams.env.Environment
-expression <java.lang.String> (property: expression) The spreadsheet query to evaluate. default: SELECT *
-symbol <adams.core.base.BaseString> [-symbol ...] (property: symbols) The symbols to initialize the parser with, key-value pairs: name=value. default:
-reader <adams.data.io.input.SpreadSheetReader> (property: reader) The spreadsheet reader for loading the spreadsheet to work on. default: adams.data.io.input.CsvSpreadSheetReader -data-row-type adams.data.spreadsheet.DenseDataRow -spreadsheet-type adams.data.spreadsheet.SpreadSheet
-input <adams.core.io.PlaceholderFile> (property: input) The input file to load with the specified reader; ignored if pointing to directory. default: ${CWD}- Author:
- FracPete (fracpete at waikato dot ac dot nz)
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected PlaceholderFilem_Inputthe spreadsheet file to read.protected SpreadSheetReaderm_Readerthe spreadsheet reader for loading the spreadsheet.protected SpreadSheetm_Sheetthe spreadsheet to use.-
Fields inherited from class adams.parser.AbstractSymbolEvaluator
m_Symbols
-
Fields inherited from class adams.parser.AbstractExpressionEvaluator
m_Environment, m_Expression
-
Fields inherited from class adams.core.option.AbstractOptionHandler
m_OptionManager
-
Fields inherited from class adams.core.logging.LoggingObject
m_Logger, m_LoggingIsEnabled, m_LoggingLevel
-
-
Constructor Summary
Constructors Constructor Description SpreadSheetQuery()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddefineOptions()Adds options to the internal list of options.protected SpreadSheetdoEvaluate(HashMap symbols)Performs the actual evaluation.SpreadSheetevaluate()Performs the evaluation.static SpreadSheetevaluate(String expr, HashMap symbols, SpreadSheet sheet)Parses and evaluates the given expression.StringexpressionTipText()Returns the tip text for this property.protected StringgetDefaultExpression()Returns the default expression to use.StringgetGrammar()Returns a string representation of the grammar.PlaceholderFilegetInput()Returns the spreadsheet file to load, ignored if pointing to directory.SpreadSheetReadergetReader()Returns the spreadsheet reader that loads the sheet.SpreadSheetgetSheet()Returns the underlying spreadsheet.StringglobalInfo()Returns a string describing the object.protected ObjectinitializeSymbol(String name, String value)Initializes the symbol.StringinputTipText()Returns the tip text for this property.protected voidloadSheet()Loads the spreadsheet from disk, if possible.static voidmain(String[] args)Runs the evaluator from command-line.StringreaderTipText()Returns the tip text for this property.voidsetInput(PlaceholderFile value)Sets the spreadsheet file to load, ignored if pointing to directory.voidsetReader(SpreadSheetReader value)Sets the spreadsheet reader that loads the sheet.voidsetSheet(SpreadSheet value)Sets the underlying spreadsheet.-
Methods inherited from class adams.parser.AbstractSymbolEvaluator
getSymbols, initialize, initializeSymbols, setSymbols, symbolsTipText
-
Methods inherited from class adams.parser.AbstractExpressionEvaluator
environmentTipText, forCommandLine, forName, getEnvironment, getExpression, runEvaluator, setEnvironment, setExpression
-
Methods inherited from class adams.core.option.AbstractOptionHandler
cleanUpOptions, destroy, finishInit, getDefaultLoggingLevel, getOptionManager, loggingLevelTipText, newOptionManager, reset, setLoggingLevel, toCommandLine, toString
-
Methods inherited from class adams.core.logging.LoggingObject
configureLogger, getLogger, getLoggingLevel, initializeLogging, isLoggingEnabled, sizeOf
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface adams.core.logging.LoggingLevelHandler
getLoggingLevel
-
-
-
-
Field Detail
-
m_Sheet
protected SpreadSheet m_Sheet
the spreadsheet to use.
-
m_Reader
protected SpreadSheetReader m_Reader
the spreadsheet reader for loading the spreadsheet.
-
m_Input
protected PlaceholderFile m_Input
the spreadsheet file to read.
-
-
Method Detail
-
globalInfo
public String globalInfo()
Returns a string describing the object.- Specified by:
globalInfoin interfaceGlobalInfoSupporter- Specified by:
globalInfoin classAbstractOptionHandler- Returns:
- a description suitable for displaying in the gui
-
defineOptions
public void defineOptions()
Adds options to the internal list of options.- Specified by:
defineOptionsin interfaceOptionHandler- Overrides:
defineOptionsin classAbstractSymbolEvaluator<SpreadSheet>
-
getGrammar
public String getGrammar()
Returns a string representation of the grammar.- Returns:
- the grammar, null if not available
-
getDefaultExpression
protected String getDefaultExpression()
Returns the default expression to use.- Specified by:
getDefaultExpressionin classAbstractExpressionEvaluator<SpreadSheet>- Returns:
- the default expression
-
expressionTipText
public String expressionTipText()
Returns the tip text for this property.- Specified by:
expressionTipTextin classAbstractExpressionEvaluator<SpreadSheet>- Returns:
- tip text for this property suitable for displaying in the GUI or for listing the options.
-
setReader
public void setReader(SpreadSheetReader value)
Sets the spreadsheet reader that loads the sheet.- Parameters:
value- the reader
-
getReader
public SpreadSheetReader getReader()
Returns the spreadsheet reader that loads the sheet.- Returns:
- the reader
-
readerTipText
public String readerTipText()
Returns the tip text for this property.- Returns:
- tip text for this property suitable for displaying in the GUI or for listing the options.
-
setInput
public void setInput(PlaceholderFile value)
Sets the spreadsheet file to load, ignored if pointing to directory.- Parameters:
value- the input file
-
getInput
public PlaceholderFile getInput()
Returns the spreadsheet file to load, ignored if pointing to directory.- Returns:
- the input file
-
inputTipText
public String inputTipText()
Returns the tip text for this property.- Returns:
- tip text for this property suitable for displaying in the GUI or for listing the options.
-
setSheet
public void setSheet(SpreadSheet value)
Sets the underlying spreadsheet.- Parameters:
value- the spreadsheet
-
getSheet
public SpreadSheet getSheet()
Returns the underlying spreadsheet.- Returns:
- the spreadsheet
-
initializeSymbol
protected Object initializeSymbol(String name, String value)
Initializes the symbol.- Specified by:
initializeSymbolin classAbstractSymbolEvaluator<SpreadSheet>- Parameters:
name- the name of the symbolvalue- the string representation of the symbol- Returns:
- the object representation of the symbol
-
doEvaluate
protected SpreadSheet doEvaluate(HashMap symbols) throws Exception
Performs the actual evaluation.- Specified by:
doEvaluatein classAbstractSymbolEvaluator<SpreadSheet>- Parameters:
symbols- the symbols to use- Returns:
- the evaluation, or null in case of error
- Throws:
Exception- if evaluation fails
-
loadSheet
protected void loadSheet()
Loads the spreadsheet from disk, if possible.
-
evaluate
public SpreadSheet evaluate() throws Exception
Performs the evaluation.- Overrides:
evaluatein classAbstractSymbolEvaluator<SpreadSheet>- Returns:
- the evaluation, or null in case of error
- Throws:
Exception- if evaluation fails
-
evaluate
public static SpreadSheet evaluate(String expr, HashMap symbols, SpreadSheet sheet) throws Exception
Parses and evaluates the given expression. Returns the result of the mathematical expression, based on the given values of the symbols.- Parameters:
expr- the expression to evaluatesymbols- the symbol/value mapping- Returns:
- the evaluated result
- Throws:
Exception- if something goes wrong
-
main
public static void main(String[] args)
Runs the evaluator from command-line.- Parameters:
args- the command-line options, use "-help" to list them
-
-