/////////////////////// // Overridden functions /////////////////////// // Public public override object GetValue(HqlField field) { switch (field.FieldType) { case HqlFieldType.FIELDNUM: return(GetValue(field.Fieldnum)); case HqlFieldType.FIXEDWIDTH: return(GetValue(field.Start, field.Length)); case HqlFieldType.STAR: return(GetEntireLine()); case HqlFieldType.FUNCTION: case HqlFieldType.SCALAR: return(field.GetValue(this)); case HqlFieldType.LITERAL_INT: case HqlFieldType.LITERAL_FLOAT: case HqlFieldType.LITERAL_STRING: return(field.GetValue(this)); case HqlFieldType.ROWNUM: return(_rownum); case HqlFieldType.FILENAME: return(_sourceName); default: throw new Exception("Unknown HqlField type"); } }
public override object GetValue(HqlField field) { // layout is // key = [order, group] // row = [order, select, with] for (int i = 0; i < _select.Count; ++i) { if (_select[i].Equals(field)) { return(_row.GetValue(_orderby.Count + i)); } } for (int i = 0; i < _groupby.Count; ++i) { if (_groupby[i].Equals(field)) { return(_key.GetValue(_orderby.Count + i)); } } //for (int i = 0; i < _orderby.Count; ++i) //{ // if (_orderby[i].Equals(field)) // return _key.GetValue(_orderby.Count + i); //} throw new Exception("Unknown field in Having"); }
public bool ContainsField(HqlField field) { for (int i = 0; i < _q.Count; ++i) { Object o = _q[i]; if (o is HqlToken) { HqlToken token = (HqlToken)o; if (token.Field != null && token.Field.ContainsField(field)) { return(true); } } else if (o is HqlCompareToken) { HqlCompareToken ct = (HqlCompareToken)o; if (ct.ContainsField(field)) { return(true); } } else { throw new Exception("Unknown object in list"); } } return(false); }
/////////////////////// // Private private void VerifyFieldsPresent(HqlFunction func) { if (func.HasScalar) { HqlField field = func.Field; VerifyFieldsPresent(field); } }
public bool Equals(HqlField field) { if (this.FieldType != field.FieldType) { // hack this is an optimization if (FieldType == HqlFieldType.LITERAL_STRING && field.FieldType == HqlFieldType.STAR && Name.Equals("*")) { return(true); } if (field.FieldType == HqlFieldType.LITERAL_STRING && this.FieldType == HqlFieldType.STAR && field.Name.Equals("*")) { return(true); } // end hack this is an optimization return(false); } // this is safe because if neither have them, they will be both string.empty if (this.FieldType != HqlFieldType.SCALAR && this.FieldType != HqlFieldType.FUNCTION) { if (!field.TableReference.Equals(TableReference)) { return(false); } } switch (_type) { case HqlFieldType.ROWNUM: case HqlFieldType.FILENAME: case HqlFieldType.STAR: return(true); case HqlFieldType.FIELDNUM: return(Fieldnum == field.Fieldnum); case HqlFieldType.FIXEDWIDTH: return(Name.Equals(field.Name) && Start == field.Start && Length == field.Length); case HqlFieldType.LITERAL_STRING: return(Name.Equals(field.Name)); case HqlFieldType.FUNCTION: return(Func.Equals(field.Func)); case HqlFieldType.SCALAR: return(Scalar.Equals(field.Scalar)); case HqlFieldType.LITERAL_INT: return(this.IntValue == field.IntValue); default: throw new Exception("Unknown type of Field"); } return(false); }
static private bool NotNullAndContainsField(HqlToken t, HqlField field) { if (t == null || t.Field == null) { return(false); } return(t.Field.ContainsField(field)); }
public override object GetValue(HqlField field) { object o; if (HasValue(field, out o)) { return(o); } throw new Exception(String.Format("Attempted to retrieve |{0}| in Values", field.ToString())); }
public bool ContainsField(HqlField field) { for (int i = 0; i < Count; ++i) { if (field.Equals(this[i])) { return(true); } } return(false); }
/////////////////////// // Public public bool ContainsField(HqlField field) { if (NotNullAndContainsField(Token1, field)) { return(true); } if (NotNullAndContainsField(Token2, field)) { return(true); } return(false); }
private void SanityCheckSelectGroupbyFields() { // if groupby is used, then for every non-function field in select, it must be in the group by if (_groupby.Count > 0 || _select.HasFunctions) { for (int i = 0; i < _select.Count; ++i) { HqlField f = _select.FieldGroup[i]; CheckFieldIntoGroupBy(_groupby, f, "SELECT", f); } } }
private void SanityCheckOrderbyGroupbyFields() { // if groupby is used, then for every non-function field in order, it must be in the group by if (_groupby.Count > 0 || _orderby.HasFunctions) { for (int i = 0; i < _orderby.Count; ++i) { HqlField f = _orderby.FieldGroup[i]; CheckFieldIntoGroupBy(_groupby, f, "ORDER-BY", f); } } }
private void SanityCheckOutputGroupbyFields() { // if groupby is used, then for every non-function field in select, it must be in the group by if (_groupby.Count > 0 || (_settings.Output != null && _settings.Output.HasFunctions)) { for (int i = 0; _settings.Output != null && i < _settings.Output.Count; ++i) { HqlField f = _settings.Output.FieldGroup[i]; CheckFieldIntoGroupBy(_groupby, f, "OUTPUT", f); } } }
private void Optimize1() { // check for any count(*) and save as count('*') with a literal because it doesn't really matter what the value is for (int i = 0; i < _select.Count; ++i) { HqlField f = _select.FieldGroup[i]; for (; f.HasFunction; f = f.Func.Field) { if (f.HasFunction && f.Func.FuncType == HqlFunctionType.COUNT && f.Func.Field.FieldType == HqlFieldType.STAR) { f.Func.Field = new HqlField(HqlFieldType.LITERAL_STRING, "*"); } } } for (int i = 0; i < _orderby.Count; ++i) { HqlField f = _orderby.FieldGroup[i]; for (; f.HasFunction; f = f.Func.Field) { if (f.HasFunction && f.Func.FuncType == HqlFunctionType.COUNT && f.Func.Field.FieldType == HqlFieldType.STAR) { f.Func.Field = new HqlField(HqlFieldType.LITERAL_STRING, "*"); } } } for (int i = 0; i < _groupby.Count; ++i) { HqlField f = _groupby.FieldGroup[i]; for (; f.HasFunction; f = f.Func.Field) { if (f.HasFunction && f.Func.FuncType == HqlFunctionType.COUNT && f.Func.Field.FieldType == HqlFieldType.STAR) { f.Func.Field = new HqlField(HqlFieldType.LITERAL_STRING, "*"); } } } for (int i = 0; _settings.Output != null && i < _settings.Output.Count; ++i) { HqlField f = _settings.Output.FieldGroup[i]; for (; f.HasFunction; f = f.Func.Field) { if (f.HasFunction && f.Func.FuncType == HqlFunctionType.COUNT && f.Func.Field.FieldType == HqlFieldType.STAR) { f.Func.Field = new HqlField(HqlFieldType.LITERAL_STRING, "*"); } } } }
private void ProcessHavingAndOrder() { if (_having.Count > 0) { HqlHavingRow havingRow = new HqlHavingRow(_orderby.FieldGroup, _groupby.FieldGroup, _select.FieldGroup); for (IDictionaryEnumerator ptr = _groupby.Table.GetEnumeratorForTable(); ptr.MoveNext();) { HqlKey key2 = (HqlKey)ptr.Key; HqlResultRow value2 = (HqlResultRow)ptr.Value; havingRow.Key = key2; havingRow.Row = value2; if (!_having.Evaluate(havingRow)) { value2.ValuesSet = false; } } } if (_orderby.Count > 0) { // I need to replace the keys with the actual values so that I sort them correctly for (IDictionaryEnumerator ptr = _groupby.Table.GetEnumeratorForTable(); ptr.MoveNext();) { HqlKey key2 = (HqlKey)ptr.Key; HqlResultRow value2 = (HqlResultRow)ptr.Value; if (!value2.ValuesSet) { continue; } for (int i = 0; i < _orderby.Count; ++i) { HqlField f = _orderby.FieldGroup[i]; if (f.HasFunction) { object o = ((HqlCalc)value2[i]).GetValue(); if (f.Scalar != null) { key2[i] = f.Scalar.Evaluate(o); } else { key2[i] = o; } } } } } }
public bool HasValue(HqlField field, out object o) { for (int i = 0; i < _fieldsImpacted.Count; ++i) { if (_fieldsImpacted[i].Equals(field)) { o = GetValue(i); return(true); } } o = null; return(false); }
public void VerifyFieldsPresent(HqlField field) { //switch (field.FieldType) //{ // case HqlFieldType.LITERAL_FLOAT: // case HqlFieldType.LITERAL_INT: // case HqlFieldType.LITERAL_STRING: // case HqlFieldType.NULL: // break; // case HqlFieldType.FUNCTION: // VerifyFieldsPresent(field.Func); // break; // case HqlFieldType.SCALAR: // VerifyFieldsPresent(field.Scalar); // break; // default: // if (!ContainsField(field)) // { // field.PrintResult = false; // AddField(field); // } // break; //} switch (field.FieldType) { case HqlFieldType.LITERAL_FLOAT: case HqlFieldType.LITERAL_INT: case HqlFieldType.LITERAL_STRING: case HqlFieldType.NULL: return; } if (!ContainsField(field)) { field.PrintResult = false; AddField(field); } if (field.FieldType == HqlFieldType.FUNCTION) { VerifyFieldsPresent(field.Func); } if (field.FieldType == HqlFieldType.SCALAR) { VerifyFieldsPresent(field.Scalar); } }
/////////////////////// // Private private void Init(HqlFieldGroup group1, HqlFieldGroup group2, HqlFieldGroup group3) { _valuesSet = false; int countGroup1 = (group1 == null) ? 0 : group1.Count; int countGroup2 = (group2 == null) ? 0 : group2.Count; int countGroup3 = (group3 == null) ? 0 : group3.Count; HqlField[] fields1 = (group1 == null) ? null : group1.Fields; HqlField[] fields2 = (group2 == null) ? null : group2.Fields; HqlField[] fields3 = (group3 == null) ? null : group3.Fields; _calc = new HqlCalc[countGroup1 + countGroup2 + countGroup3]; _grouped = new object[countGroup1 + countGroup2 + countGroup3]; int start = 0; for (int i = 0; i < countGroup1; ++i) { HqlField f = fields1[i]; if (f.HasFunction) { _calc[i + start] = f.Func.CreateCalcObject(); } } start += countGroup1; for (int i = 0; i < countGroup2; ++i) { HqlField f = fields2[i]; if (f.HasFunction) { _calc[i + start] = f.Func.CreateCalcObject(); } } start += countGroup2; for (int i = 0; i < countGroup3; ++i) { HqlField f = fields3[i]; if (f.HasFunction) { _calc[i + start] = f.Func.CreateCalcObject(); } } //start += countGroup3; }
/////////////////////// // Public public bool ContainsField(HqlField field) { if (this.Equals(field)) { return(true); } switch (this.FieldType) { case HqlFieldType.SCALAR: return(Scalar.ContainsField(field)); case HqlFieldType.FUNCTION: return(Func.ContainsField(field)); } return(false); }
/////////////////////// // Overridden functions /////////////////////// // Public public void AddField(HqlField field) { if (_fields == null) { _fields = new HqlField[1]; _fields[0] = field; } else { HqlField[] newfields = new HqlField[_fields.Length + 1]; for (int i = 0; i < _fields.Length; ++i) { newfields[i] = _fields[i]; } newfields[_fields.Length] = field; _fields = newfields; } }
/////////////////////// // Static Functions static public object EvaluateGroupByField(HqlField f, int num, HqlResultRow row) { if (f.HasFunction) { if (f.Scalar != null) { object s = ((HqlCalc)row[num]).GetValue(); return(f.Scalar.Evaluate(s)); } else { object s = ((HqlCalc)row[num]).GetPrintValue(); return(s); } } else { string s = row[num].ToString(); return(s); } }
private void LoadField(HqlField field) { if (field == null) { return; } if (!field.ContainsTableReference(TableReference)) { return; } if (!FieldsImpacted.ContainsField(field)) { FieldsImpacted.AddField(field); } if (field.FieldType == HqlFieldType.FUNCTION) { LoadField(field.Func.Field); } else if (field.FieldType == HqlFieldType.SCALAR) { HqlScalar scalar = field.Scalar; if (scalar.HasMultipleFields) { for (int i = 0; i < scalar.Fields.Length; ++i) { LoadField(scalar.Fields[i]); } } else { LoadField(scalar.Field); } } }
abstract public object GetValue(HqlField field);
/////////////////////// // Overridden functions /////////////////////// // Public public override void Parse(HqlTokenProcessor processor) { HqlToken token; bool FieldExpected = true; // first thing should be select token = processor.GetToken(); if (token.WordType != HqlWordType.KEYWORD || token.Keyword != HqlKeyword.SELECT) { throw new Exception("Expected SELECT"); } // now pull back things until you get to FROM for (; ;) { processor.MoveNextToken(); token = processor.GetToken(); if (processor.MatchesEndOfProcessing()) { throw new Exception("Reached EOL in SELECT clause"); } if (token.WordType == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.FROM) { break; } if (token.WordType == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.COMMA) { FieldExpected = true; continue; } if (_fieldgroup != null && _fieldgroup.Count > 0 && token.WordType == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.AS) { processor.MoveNextToken(); token = processor.GetToken(); if (token.WordType != HqlWordType.TEXT && token.WordType != HqlWordType.UNKNOWN) { throw new Exception("Expected a table reference name following AS"); } _fieldgroup.SetPreviousFieldRename(token.Data); continue; } // else time to process it! if (!FieldExpected) { throw new Exception("Found additional SELECT fields not expected"); } FieldExpected = false; if (token.WordType == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.STAR) { AddField(new HqlField(HqlFieldType.STAR)); } else if (token.WordType == HqlWordType.FIELD) { AddField(token.Field); } else if (token.WordType == HqlWordType.SCALAR) { AddField(token.Field); } else if (token.WordType == HqlWordType.LITERAL_STRING) { HqlField f = new HqlField(HqlFieldType.LITERAL_STRING, token.Data); AddField(f); } else if (token.WordType == HqlWordType.UNKNOWN && processor.CheckForTokenReference(ref token)) { AddField(token.Field); } else if (token.WordType == HqlWordType.ROWNUM) { AddField(token.Field); } else { throw new Exception("Cannot determine data in SELECT clause"); } } if (FieldExpected) { throw new Exception("Expected additional SELECT fields"); } }
/////////////////////// // Overridden functions /////////////////////// // Public public override void Parse(HqlTokenProcessor processor) { HqlToken token; bool FieldExpected = true; // first thing should be groupby token = processor.GetToken(); if (token.WordType != HqlWordType.KEYWORD || token.Keyword != HqlKeyword.GROUPBY) { return; } // now pull back things until you get to HAVING or ORDER BY or EOL for (; ;) { processor.MoveNextToken(); token = processor.GetToken(); if (processor.MatchesEndOfProcessing()) { return; } if (token.WordType == HqlWordType.KEYWORD && ( token.Keyword == HqlKeyword.HAVING || token.Keyword == HqlKeyword.ORDERBY || token.Keyword == HqlKeyword.WITH ) ) { break; } if (token.WordType == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.COMMA) { FieldExpected = true; continue; } // else time to process it! if (!FieldExpected) { throw new Exception("Found additional GROUPBY fields not expected"); } FieldExpected = false; // * = everything // FIELD = field<NUM> such as field1, field2, field10 // FIXEDWIDTH = NAME(S, L) | NAME(S,L) such as bob(1, 5) or susan(10,5) if (token.WordType == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.STAR) { throw new Exception("Cannot GROUPBY STAR"); } else if (token.WordType == HqlWordType.FIELD) { AddField(token.Field); } else if (token.WordType == HqlWordType.SCALAR) { AddField(token.Field); } else if (token.WordType == HqlWordType.LITERAL_STRING) { HqlField f = new HqlField(HqlFieldType.LITERAL_STRING, token.Data); AddField(f); } else if (token.WordType == HqlWordType.UNKNOWN && processor.CheckForTokenReference(ref token)) { AddField(token.Field); } else { throw new Exception("Cannot determine data in GROUPBY clause"); } } if (FieldExpected) { throw new Exception("Expected additional GROUPBY fields"); } }
private bool MoveNextTokenCheckFixedWidth() { // if this is just text followed by a ( it must be a field or function // if (_token.WordType == HqlWordType.UNKNOWN && MatchNext("(")) { HqlToken fieldName = _token; MoveNextToken(); HqlToken paren = _token; if (paren.WordType != HqlWordType.KEYWORD || paren.Keyword != HqlKeyword.OPENPAREN) { throw new Exception("Expected an open-paren in fixed-width declaration."); } MoveNextToken(); HqlToken first = _token; if (first.WordType == HqlWordType.INT) { // means it is a field. Next two tokens should be a comma and int MoveNextToken(); HqlToken comma = _token; if (!comma.Data.Equals(",")) { throw new Exception("Expected a comma in fixed-width declaration."); } MoveNextToken(); HqlToken second = _token; if (second.WordType != HqlWordType.INT) { throw new Exception("Expected an int in fixed-width declaration."); } MoveNextToken(); HqlToken closeparen = _token; if (closeparen.WordType != HqlWordType.KEYWORD || closeparen.Keyword != HqlKeyword.CLOSEDPAREN) { throw new Exception("Expected a close-paren in fixed-width declaration."); } if ((Int64)first.Parsed < 1) { throw new Exception("Expected an integer greater than 0 for start position."); } if ((Int64)second.Parsed < 1) { throw new Exception("Expected an integer greater than 0 for length."); } first.Parsed = (Int64)first.Parsed - 1; // it is one-based so move back to zero-based HqlField f = new HqlField(HqlFieldType.FIXEDWIDTH, (int)(Int64)first.Parsed, (int)(Int64)second.Parsed, fieldName.Data); _token = new HqlToken(HqlWordType.FIELD, f); AddTokenToInternalList(_token); return(true); } else { throw new Exception("Expected a well-formed fixed-width statement."); } } return(false); }
static private void CheckFieldIntoGroupBy(HqlGroupBy groupby, HqlField f, string type, HqlField entireField) { if (f.HasFunction) { return; } if (f.IsLiteralType) { return; } if (!f.PrintResult) { return; } if (groupby.ContainsField(f)) { return; } if (!f.HasScalar) { throw new Exception(String.Format("Expected the {0} value of {1} in the GROUP-BY", type, f.GetFullName())); } else if (f.HasScalar) { if (f.Scalar.HasMultipleFields) { HqlField[] fields = f.Scalar.Fields; if (fields == null) { throw new Exception(String.Format("Expected the {0} value of {1} in the GROUP-BY", type, f.GetFullName())); } for (int j = 0; j < fields.Length; j++) { HqlField ff = fields[j]; CheckFieldIntoGroupBy(groupby, ff, type, entireField); } } else { f = f.FinalField; if (f.IsLiteralType) { return; } if (!groupby.ContainsField(f)) { throw new Exception(String.Format("Expected the {0} value of {1} in the GROUP-BY", type, f.GetFullName())); } } } else { throw new Exception("Unknown case of FIELD"); } }
public void AddField(HqlField field) { _fieldgroup.AddField(field); }
private bool MoveNextTokenCheckFunction() { if (_token.WordType == HqlWordType.KEYWORD && _token.Keyword == HqlKeyword.FUNCTION && MatchNext("(")) { HqlFunction func = null; HqlToken funcName = _token; MoveNextToken(); HqlToken paren = _token; if (paren.WordType != HqlWordType.KEYWORD || paren.Keyword != HqlKeyword.OPENPAREN) { throw new Exception("Expected an open-paren in function declaration."); } MoveNextToken(); HqlToken next = _token; if (next.WordType == HqlWordType.FIELD) { if (next.Field.HasFunction) { throw new Exception("Cannot have nested functions"); } func = new HqlFunction(HqlFunction.ResolveFunctionType(funcName.Data), next.Field); next = new HqlToken(HqlWordType.FIELD, new HqlField(HqlFieldType.FUNCTION, func)); } else if (next.WordType == HqlWordType.KEYWORD && next.Keyword == HqlKeyword.STAR) { HqlField star = new HqlField(HqlFieldType.STAR); func = new HqlFunction(HqlFunction.ResolveFunctionType(funcName.Data), star); next = new HqlToken(HqlWordType.FIELD, new HqlField(HqlFieldType.FUNCTION, func)); } else if (next.WordType == HqlWordType.SCALAR) { func = new HqlFunction(HqlFunction.ResolveFunctionType(funcName.Data), next.Field); next = new HqlToken(HqlWordType.FIELD, new HqlField(HqlFieldType.FUNCTION, func)); } else if (next.WordType == HqlWordType.INT || next.WordType == HqlWordType.FLOAT) { func = new HqlFunction(HqlFunction.ResolveFunctionType(funcName.Data), HqlField.CreateObject(next)); next = new HqlToken(HqlWordType.FIELD, new HqlField(HqlFieldType.FUNCTION, func)); } else { throw new Exception("Expected a valid field instead of this unknown object"); } MoveNextToken(); HqlToken lookForComma = _token; while (lookForComma.WordType == HqlWordType.KEYWORD && lookForComma.Keyword == HqlKeyword.COMMA) { MoveNextToken(); HqlToken option = _token; func.SetOption(option); MoveNextToken(); lookForComma = _token; } HqlToken closedparen = _token; if (closedparen.WordType != HqlWordType.KEYWORD || closedparen.Keyword != HqlKeyword.CLOSEDPAREN) { throw new Exception("Expected an close-paren in function declaration."); } _token = next; return(true); } return(false); }
/////////////////////// // Constructors public HqlFunction(HqlFunctionType type, HqlField field) { _type = type; _field = field; //_options = null; // unnecessary }
public bool ContainsField(HqlField field) { return(_fieldgroup.ContainsField(field)); }