func RewriteSqlSource(m *SqlSource, parentStmt *SqlSelect) *SqlSelect {

	if m.Source != nil {
		return m.Source
	}
	// Rewrite this SqlSource for the given parent, ie
	//   1)  find the column names we need to request from source including those used in join/where
	//   2)  rewrite the where for this partial query
	//   3)  any columns in join expression that are not equal between
	//          sides should be aliased towards the left-hand join portion
	//   4)  if we need different sort for our join algo?

	newCols := make(Columns, 0)
	if !parentStmt.Star {
		for idx, col := range parentStmt.Columns {
			left, _, hasLeft := col.LeftRight()
			if !hasLeft {
				// Was not left/right qualified, so use as is?  or is this an error?
				//  what is official sql grammar on this?
				newCol := col.Copy()
				newCol.ParentIndex = idx
				newCol.Index = len(newCols)
				newCols = append(newCols, newCol)

			} else if hasLeft && left == m.Alias {
				newCol := col.CopyRewrite(m.Alias)
				newCol.ParentIndex = idx
				newCol.SourceIndex = len(newCols)
				newCol.Index = len(newCols)
				newCols = append(newCols, newCol)
			}
		}
	}

	// TODO:
	//  - rewrite the Sort
	//  - rewrite the group-by
	sql2 := &SqlSelect{Columns: newCols, Star: parentStmt.Star}
	m.joinNodes = make([]expr.Node, 0)
	if m.SubQuery != nil {
		if len(m.SubQuery.From) != 1 {
			u.Errorf("Not supported, nested subQuery %v", m.SubQuery.String())
		} else {
			sql2.From = append(sql2.From, &SqlSource{Name: m.SubQuery.From[0].Name})
		}
	} else {
		sql2.From = append(sql2.From, &SqlSource{Name: m.Name})
	}

	for _, from := range parentStmt.From {
		// We need to check each participant in the Join for possible
		// columns which need to be re-written
		sql2.Columns = columnsFromJoin(m, from.JoinExpr, sql2.Columns)

		// We also need to create an expression used for evaluating
		// the values of Join "Keys"
		if from.JoinExpr != nil {
			joinNodesForFrom(parentStmt, m, from.JoinExpr, 0)
		}
	}

	if parentStmt.Where != nil {
		node, cols := rewriteWhere(parentStmt, m, parentStmt.Where.Expr, make(Columns, 0))
		if node != nil {
			sql2.Where = &SqlWhere{Expr: node}
		}
		if len(cols) > 0 {
			parentIdx := len(parentStmt.Columns)
			for _, col := range cols {
				col.Index = len(sql2.Columns)
				col.ParentIndex = parentIdx
				parentIdx++
				sql2.Columns = append(sql2.Columns, col)
			}
		}
	}
	m.Source = sql2
	m.cols = sql2.UnAliasedColumns()
	return sql2
}
// RewriteSqlSource this Source to act as a stand-alone query to backend
// @parentStmt = the parent statement that this a partial source to
RewriteSqlSource this Source to act as a stand-alone query to backend
@parentStmt = the parent statement that this a partial source to
// RewriteSqlSource this Source to act as a stand - alone query to backend // @parentStmt = the parent statement that this a partial source to
func RewriteSqlSource(m *SqlSource, parentStmt *SqlSelect) *SqlSelect {
	if m.Source != nil {
		return m.Source
	}
	newCols := make(Columns, 0)
	if !parentStmt.Star {
		for idx, col := range parentStmt.Columns {
			left, _, hasLeft := col.LeftRight()
			if !hasLeft {
				newCol := col.Copy()
				newCol.ParentIndex = idx
				newCol.Index = len(newCols)
				newCols = append(newCols, newCol)
			} else if hasLeft && left == m.Alias {
				newCol := col.CopyRewrite(m.Alias)
				newCol.ParentIndex = idx
				newCol.SourceIndex = len(newCols)
				newCol.Index = len(newCols)
				newCols = append(newCols, newCol)
			}
		}
	}
	sql2 := &SqlSelect{Columns: newCols, Star: parentStmt.Star}
	m.joinNodes = make([]expr.Node, 0)
	if m.SubQuery != nil {
		if len(m.SubQuery.From) != 1 {
			u.Errorf("Not supported, nested subQuery %v", m.SubQuery.String())
		} else {
			sql2.From = append(sql2.From, &SqlSource{Name: m.SubQuery.From[0].Name})
		}
	} else {
		sql2.From = append(sql2.From, &SqlSource{Name: m.Name})
	}
	for _, from := range parentStmt.From {
		sql2.Columns = columnsFromJoin(m, from.JoinExpr, sql2.Columns)
		if from.JoinExpr != nil {
			joinNodesForFrom(parentStmt, m, from.JoinExpr, 0)
		}
	}
	if parentStmt.Where != nil {
		node, cols := rewriteWhere(parentStmt, m, parentStmt.Where.Expr, make(Columns, 0))
		if node != nil {
			sql2.Where = &SqlWhere{Expr: node}
		}
		if len(cols) > 0 {
			parentIdx := len(parentStmt.Columns)
			for _, col := range cols {
				col.Index = len(sql2.Columns)
				col.ParentIndex = parentIdx
				parentIdx++
				sql2.Columns = append(sql2.Columns, col)
			}
		}
	}
	m.Source = sql2
	m.cols = sql2.UnAliasedColumns()
	return sql2
}
func RewriteSqlSource ( m * SqlSource , parentStmt * SqlSelect ) * SqlSelect { if m . Source != nil { return m . Source } newCols := make ( Columns , 0 ) if ! parentStmt . Star { for idx , col := range parentStmt . Columns { left , _ , hasLeft := col . LeftRight ( ) if ! hasLeft { newCol := col . Copy ( ) newCol . ParentIndex = idx newCol . Index = len ( newCols ) newCols = append ( newCols , newCol ) } else if hasLeft && left == m . Alias { newCol := col . CopyRewrite ( m . Alias ) newCol . ParentIndex = idx newCol . SourceIndex = len ( newCols ) newCol . Index = len ( newCols ) newCols = append ( newCols , newCol ) } } } sql2 := & SqlSelect { Columns : newCols , Star : parentStmt . Star } m . joinNodes = make ( [ ] expr . Node , 0 ) if m . SubQuery != nil { if len ( m . SubQuery . From ) != 1 { u . Errorf ( " " , m . SubQuery . String ( ) ) } else { sql2 . From = append ( sql2 . From , & SqlSource { Name : m . SubQuery . From [ 0 ] . Name } ) } } else { sql2 . From = append ( sql2 . From , & SqlSource { Name : m . Name } ) } for _ , from := range parentStmt . From { sql2 . Columns = columnsFromJoin ( m , from . JoinExpr , sql2 . Columns ) if from . JoinExpr != nil { joinNodesForFrom ( parentStmt , m , from . JoinExpr , 0 ) } } if parentStmt . Where != nil { node , cols := rewriteWhere ( parentStmt , m , parentStmt . Where . Expr , make ( Columns , 0 ) ) if node != nil { sql2 . Where = & SqlWhere { Expr : node } } if len ( cols ) > 0 { parentIdx := len ( parentStmt . Columns ) for _ , col := range cols { col . Index = len ( sql2 . Columns ) col . ParentIndex = parentIdx parentIdx ++ sql2 . Columns = append ( sql2 . Columns , col ) } } } m . Source = sql2 m . cols = sql2 . UnAliasedColumns ( ) return sql2 }
RewriteSqlSource this Source to act as a stand-alone query to backend
@parentStmt = the parent statement that this a partial source to
RewriteSqlSource this Source to act as a stand - alone query to backend @parentStmt = the parent statement that this a partial source to
// Rewrite this SqlSource for the given parent, ie //   1)  find the column names we need to request from source including those used in join/where //   2)  rewrite the where for this partial query //   3)  any columns in join expression that are not equal between //          sides should be aliased towards the left-hand join portion //   4)  if we need different sort for our join algo? // Was not left/right qualified, so use as is?  or is this an error? //  what is official sql grammar on this? // TODO: //  - rewrite the Sort //  - rewrite the group-by // We need to check each participant in the Join for possible // columns which need to be re-written // We also need to create an expression used for evaluating // the values of Join "Keys"