Optimize string processing in select (#6593)

Reduce allocations during string concatenation and simplify some
processing code.
This commit is contained in:
Aditya Manthramurthy
2018-10-09 14:02:19 -07:00
committed by kannappanr
parent 54ae364def
commit e3eec89d24
2 changed files with 45 additions and 83 deletions

View File

@@ -55,8 +55,8 @@ func stringInSlice(x string, list []string) bool {
// This function returns the index of a string in a list
func stringIndex(a string, list []string) int {
for i := range list {
if list[i] == a {
for i, v := range list {
if v == a {
return i
}
}
@@ -65,10 +65,8 @@ func stringIndex(a string, list []string) int {
// Returns a true or false, whether a string can be represented as an int.
func representsInt(s string) bool {
if _, err := strconv.Atoi(s); err == nil {
return true
}
return false
_, err := strconv.Atoi(s)
return err == nil
}
// The function below processes the where clause into an acutal boolean given a
@@ -236,16 +234,16 @@ func checkValidOperator(operator string) error {
}
// checkStringType converts the value from the csv to the appropriate one.
func checkStringType(myTblVal string) interface{} {
myInt, isInt := strconv.Atoi(myTblVal)
myFloat, isFloat := strconv.ParseFloat(myTblVal, 64)
if isInt == nil {
return myInt
} else if isFloat == nil {
return myFloat
} else {
return myTblVal
func checkStringType(tblVal string) interface{} {
intVal, err := strconv.Atoi(tblVal)
if err == nil {
return intVal
}
floatVal, err := strconv.ParseFloat(tblVal, 64)
if err == nil {
return floatVal
}
return tblVal
}
// stringEval is for evaluating the state of string comparison.
@@ -627,31 +625,6 @@ func (reader *Input) whereClauseNameErrs(whereClause interface{}, alias string)
return nil
}
// qualityCheck ensures the row has enough separators.
func qualityCheck(row string, amountOfSep int, sep string) string {
for i := 0; i < amountOfSep; i++ {
row = row + sep
}
return row
}
// writeRow helps to write the row regardless of how many entries.
func writeRow(myRow string, myEntry string, delimiter string, numOfReqCols int) string {
if myEntry == "" && len(myRow) == 0 && numOfReqCols == 1 {
return myEntry
}
if myEntry == "" && len(myRow) == 0 {
return myEntry + delimiter
}
if len(myRow) == 1 && myRow[0] == ',' {
return myRow + myEntry
}
if len(myRow) == 0 {
return myEntry
}
return myRow + delimiter + myEntry
}
// colNameErrs is a function which makes sure that the headers are requested are
// present in the file otherwise it throws an error.
func (reader *Input) colNameErrs(columnNames []string) error {
@@ -677,24 +650,23 @@ func (reader *Input) colNameErrs(columnNames []string) error {
}
// aggFuncToStr converts an array of floats into a properly formatted string.
func (reader *Input) aggFuncToStr(myAggVals []float64) string {
var myRow string
var aggregateval string
if myAggVals[0] == math.Trunc(myAggVals[0]) {
myRow = strconv.FormatInt(int64(myAggVals[0]), 10)
} else {
myRow = strconv.FormatFloat(myAggVals[0], 'f', 6, 64)
func (reader *Input) aggFuncToStr(aggVals []float64) string {
// Define a number formatting function
numToStr := func(f float64) string {
if f == math.Trunc(f) {
return strconv.FormatInt(int64(f), 10)
}
return strconv.FormatFloat(f, 'f', 6, 64)
}
for i := 1; i < len(myAggVals); i++ {
if myAggVals[i] == math.Trunc(myAggVals[i]) {
aggregateval = strconv.FormatInt(int64(myAggVals[i]), 10)
} else {
aggregateval = strconv.FormatFloat(myAggVals[i], 'f', 6, 64)
}
myRow = myRow + reader.options.OutputFieldDelimiter + aggregateval
// Display all whole numbers in aggVals as integers
vals := make([]string, len(aggVals))
for i, v := range aggVals {
vals[i] = numToStr(v)
}
return myRow
// Intersperse field delimiter
return strings.Join(vals, reader.options.OutputFieldDelimiter)
}
// checkForDuplicates ensures we do not have an ambigious column name.