Skip to content

Auto resolve expressions #8

@EpicStep

Description

@EpicStep

As part of this task, it is necessary to implement the auto_resolve_expression setting. If it is enabled, then you need to generate an expression from system.columns.

Example:

package database

import (
	"context"
	"fmt"
	"strings"
)

type Column struct {
	Name              string
	Type              ColumnType
	AggregateFunction string
	IsSortingKey      bool
	IsPrimaryKey      bool
}

type ColumnType int

const (
	ColumnDefaultType ColumnType = iota + 1
	AggregateFunctionType
)

func (db *Database) Columns(ctx context.Context, database, table string) ([]Column, error) {
	var columns []Column

	rows, err := db.cluster.Query(ctx,
		`SELECT 
    				name, type, is_in_sorting_key, is_in_primary_key 
				FROM 
				    system.columns 
				WHERE 
				    database = $1 AND table = $2`, database, table)
	if err != nil {
		return nil, err
	}

	defer rows.Close()

	for rows.Next() {
		var column Column

		var typeString string

		if err = rows.Scan(&column.Name, &typeString, &column.IsSortingKey, &column.IsPrimaryKey); err != nil {
			return nil, err
		}

		column.Type = parseType(typeString)
		if column.Type == AggregateFunctionType {
			column.AggregateFunction = parseAggregateFunction(typeString)
		}

		columns = append(columns, column)
	}

	if len(columns) == 0 {
		return nil, fmt.Errorf("columns not found")
	}

	return columns, nil
}

func parseType(s string) ColumnType {
	if strings.HasPrefix(s, "AggregateFunction") {
		return AggregateFunctionType
	}

	return ColumnDefaultType
}

func parseAggregateFunction(s string) string {
	args := strings.TrimSuffix(strings.TrimPrefix(s, "AggregateFunction("), ")")
	splittedArgs := strings.Split(args, ",")

	return splittedArgs[0]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions