diff --git a/.gitignore b/.gitignore index c55af97cdace..22c928900a53 100644 --- a/.gitignore +++ b/.gitignore @@ -16,7 +16,7 @@ local/ *.dylib bin/ dist/ - +test/ # Emacs *~ \#*\# @@ -34,3 +34,4 @@ integration-coverage.html # Wix *.wixobj *.wixpdb +/config.yaml diff --git a/go.mod b/go.mod index 69e187008683..f84f8e9b6784 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/open-telemetry/opentelemetry-collector-contrib go 1.17 require ( + github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20220816024939-bc8df83d7b9d github.com/open-telemetry/opentelemetry-collector-contrib/exporter/alibabacloudlogserviceexporter v0.55.0 github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awscloudwatchlogsexporter v0.55.0 github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsemfexporter v0.55.0 @@ -153,7 +154,9 @@ require ( github.com/prometheus/prometheus v0.36.2 github.com/stretchr/testify v1.8.0 go.opentelemetry.io/collector v0.56.0 + go.opentelemetry.io/collector/pdata v0.56.0 go.uber.org/multierr v1.8.0 + go.uber.org/zap v1.21.0 golang.org/x/sys v0.0.0-20220624220833-87e55d714810 ) @@ -531,7 +534,6 @@ require ( go.mongodb.org/atlas v0.16.0 // indirect go.mongodb.org/mongo-driver v1.9.1 // indirect go.opencensus.io v0.23.0 // indirect - go.opentelemetry.io/collector/pdata v0.56.0 // indirect go.opentelemetry.io/collector/semconv v0.56.0 // indirect go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.33.0 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.33.0 // indirect @@ -544,7 +546,6 @@ require ( go.opentelemetry.io/otel/trace v1.8.0 // indirect go.uber.org/atomic v1.9.0 // indirect go.uber.org/goleak v1.1.12 // indirect - go.uber.org/zap v1.21.0 // indirect golang.org/x/crypto v0.0.0-20220507011949-2cf3adece122 // indirect golang.org/x/exp v0.0.0-20200908183739-ae8ad444f925 // indirect golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect diff --git a/go.sum b/go.sum index 7726c9db02c8..c37e2664b471 100644 --- a/go.sum +++ b/go.sum @@ -293,6 +293,8 @@ github.com/aliyun/aliyun-log-go-sdk v0.1.37 h1:GvswbgLqVOHNeMWssQ9zA+R7YVDP6arLU github.com/aliyun/aliyun-log-go-sdk v0.1.37/go.mod h1:1QQ59pEJiVVXqKgbHcU6FWIgxT5RKBt+CT8AiQ2bEts= github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= +github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20220816024939-bc8df83d7b9d h1:0xIrH2lJbraclvJT3pvTf3u2oCAL60cAqiv4qRpz4EI= +github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20220816024939-bc8df83d7b9d/go.mod h1:F7bn7fEU90QkQ3tnmaTx3LTKLEDqnwWODIYppRQ5hnY= github.com/antonmedv/expr v1.9.0 h1:j4HI3NHEdgDnN9p6oI6Ndr0G5QryMY0FNxT4ONrFDGU= github.com/antonmedv/expr v1.9.0/go.mod h1:5qsM3oLGDND7sDmQGDXHkYfkjYMUX14qsgqmHhwGEk8= github.com/apache/arrow/go/arrow v0.0.0-20211112161151-bc219186db40 h1:q4dksr6ICHXqG5hm0ZW5IHyeEJXoIJSOZeBLmWPNeIQ= diff --git a/internal/components/components.go b/internal/components/components.go index e51f31190be2..37d465ea11d4 100644 --- a/internal/components/components.go +++ b/internal/components/components.go @@ -18,6 +18,7 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/opsrampotlpexporter" "github.com/open-telemetry/opentelemetry-collector-contrib/processor/batchmemlimitprocessor" "github.com/open-telemetry/opentelemetry-collector-contrib/processor/scrubbingprocessor" + "github.com/open-telemetry/opentelemetry-collector-contrib/processor/stream_processor" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/exporter/loggingexporter" "go.opentelemetry.io/collector/exporter/otlpexporter" @@ -351,6 +352,7 @@ func Components() (component.Factories, error) { transformprocessor.NewFactory(), scrubbingprocessor.NewFactory(), batchmemlimitprocessor.NewFactory(), + stream_processor.NewFactory(), } factories.Processors, err = component.MakeProcessorFactoryMap(processors...) if err != nil { diff --git a/pkg/stanza/operator/helper/input.go b/pkg/stanza/operator/helper/input.go index d965b802112b..1dd5b6a5b10a 100644 --- a/pkg/stanza/operator/helper/input.go +++ b/pkg/stanza/operator/helper/input.go @@ -16,6 +16,7 @@ package helper // import "github.com/open-telemetry/opentelemetry-collector-cont import ( "context" + "fmt" "go.uber.org/zap" @@ -88,6 +89,26 @@ func (i *InputOperator) NewEntry(value interface{}) (*entry.Entry, error) { return entry, nil } +func (i *InputOperator) NewEntryWithAttr(value interface{}) (*entry.Entry, error) { + entry := entry.New() + + converted, ok := value.(map[string]interface{}) + if !ok { + return nil, fmt.Errorf("value type is not map") + } + entry.Attributes = converted + + if err := i.Attribute(entry); err != nil { + return nil, errors.Wrap(err, "add attributes to entry") + } + + if err := i.Identify(entry); err != nil { + return nil, errors.Wrap(err, "add resource keys to entry") + } + + return entry, nil +} + // CanProcess will always return false for an input operator. func (i *InputOperator) CanProcess() bool { return false diff --git a/pkg/stanza/operator/input/windows/operator.go b/pkg/stanza/operator/input/windows/operator.go index 1ae50471367c..2d5532ae2ed3 100644 --- a/pkg/stanza/operator/input/windows/operator.go +++ b/pkg/stanza/operator/input/windows/operator.go @@ -20,6 +20,7 @@ package windows // import "github.com/open-telemetry/opentelemetry-collector-con import ( "context" "fmt" + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry" "sync" "time" @@ -39,6 +40,7 @@ type Config struct { Channel string `mapstructure:"channel" json:"channel" yaml:"channel"` MaxReads int `mapstructure:"max_reads,omitempty" json:"max_reads,omitempty" yaml:"max_reads,omitempty"` StartAt string `mapstructure:"start_at,omitempty" json:"start_at,omitempty" yaml:"start_at,omitempty"` + BodyToAttr bool `mapstructure:"body_to_attr" json:"body_to_attr" yaml:"body_to_attr"` PollInterval helper.Duration `mapstructure:"poll_interval,omitempty" json:"poll_interval,omitempty" yaml:"poll_interval,omitempty"` } @@ -68,6 +70,7 @@ func (c *Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) { maxReads: c.MaxReads, startAt: c.StartAt, pollInterval: c.PollInterval, + bodyToAttr: c.BodyToAttr, }, nil } @@ -92,6 +95,7 @@ type Input struct { channel string maxReads int startAt string + bodyToAttr bool pollInterval helper.Duration persister operator.Persister cancel context.CancelFunc @@ -223,7 +227,15 @@ func (e *Input) processEvent(ctx context.Context, event Event) { // sendEvent will send EventXML as an entry to the operator's output. func (e *Input) sendEvent(ctx context.Context, eventXML EventXML) { body := eventXML.parseBody() - entry, err := e.NewEntry(body) + var entry *entry.Entry + var err error + + if e.bodyToAttr { + entry, err = e.NewEntryWithAttr(body) + } else { + entry, err = e.NewEntry(body) + } + if err != nil { e.Errorf("Failed to create entry: %s", err) return diff --git a/processor/batchmemlimitprocessor/config.go b/processor/batchmemlimitprocessor/config.go index fbab4de421a1..6c523a5da0f5 100644 --- a/processor/batchmemlimitprocessor/config.go +++ b/processor/batchmemlimitprocessor/config.go @@ -5,7 +5,7 @@ import ( "go.opentelemetry.io/collector/config" ) -// Config defines configuration for Resource processor. +// Config defines configuration for batch memory limit processor. type Config struct { config.ProcessorSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct adapter.BaseConfig `mapstructure:",squash"` diff --git a/processor/scrubbingprocessor/config.go b/processor/scrubbingprocessor/config.go index 1219225da7d0..ea1a7427f137 100644 --- a/processor/scrubbingprocessor/config.go +++ b/processor/scrubbingprocessor/config.go @@ -20,7 +20,7 @@ type MaskingSettings struct { Placeholder string `mapstructure:"placeholder"` } -// Config defines configuration for Resource processor. +// Config defines configuration for scrubbing processor. type Config struct { config.ProcessorSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct adapter.BaseConfig `mapstructure:",squash"` diff --git a/processor/stream_processor/Sql.g4 b/processor/stream_processor/Sql.g4 new file mode 100644 index 000000000000..34e07eeca9be --- /dev/null +++ b/processor/stream_processor/Sql.g4 @@ -0,0 +1,194 @@ +grammar Sql; + + +//java -jar antlr-4.10.1-complete.jar -Dlanguage=Go -o parser Sql.g4 -no-listener -visitor + +sqlQuery + : selectQuery EOF; + +selectQuery + : K_SELECT resultColumns (whereStatement)? EOQ #selectSimple + | K_SELECT aggregationColumns windowTumbling (whereStatement)? EOQ #selectTumbling + | K_SELECT groupByAggregationColumns windowTumbling (whereStatement)? groupBy EOQ #selectTumblingGroupBy + ; + +windowTumbling + : K_WINDOW_TUMBLING NUMERIC_LITERAL + ; + + +resultColumns + : column (COMMA column)* # selectColumns + | STAR # selectStar + ; + + +aggregationColumns + : aggregationColumn (COMMA aggregationColumn)* #selectAggregations + ; + +groupByAggregationColumns + : (column | aggregationColumn) (COMMA (aggregationColumn | column))* #selectGroupByAggregations + ; + + +aggregationColumn + : (K_MIN | K_MAX | K_COUNT | K_AVG | K_SUM) L_BRACKET ( IDENTIFIER | IDENTIFIER DOT IDENTIFIER) R_BRACKET alias? + ; + +column + : (IDENTIFIER | IDENTIFIER DOT IDENTIFIER) alias? #identifierColumn + | function alias? #functionColumn + ; + + + +alias + : K_AS IDENTIFIER + ; + +function + : functionName L_BRACKET ( IDENTIFIER | IDENTIFIER DOT IDENTIFIER) (COMMA literalValue)* R_BRACKET #simpleFunction + | functionName L_BRACKET ( IDENTIFIER | IDENTIFIER DOT IDENTIFIER) (COMMA literalValue)* R_BRACKET #simpleFunction + | functionName L_BRACKET function (COMMA literalValue)* R_BRACKET #recursiveFunction + ; + +functionName + : IDENTIFIER + ; + + +whereStatement + : K_WHERE expr #whereStmt + ; + +expr + : simpleExpr #simpleCondition + | (compoundExpr | simpleExpr) ( K_AND | K_OR ) (compoundExpr | simpleExpr) #compoundRecursiveCondition + // IDENTIFIER (K_IS_NULL | K_IS_NOT_NULL) #nullCondition + | expr (K_AND| K_OR) expr #simpleRecursiveCondition + | compoundExpr #simpleCompoundCondition + ; + +simpleExpr + : IDENTIFIER comparisonOperator literalValue #simpleExpression + | IDENTIFIER DOT IDENTIFIER comparisonOperator literalValue #nestedExpression + ; + +compoundExpr + : L_BRACKET expr R_BRACKET #compoundExpression + ; + +comparisonOperator + : K_EQUAL | K_GREATER | K_LESS | K_LESS_EQUAL | K_GREATER_EQUAL | K_NOT_EQUAL | K_LIKE |K_NOT_LIKE | K_IN | K_IS | K_NOT_IN + ; + + +literalValue + : NUMERIC_LITERAL + | BOOLEAN_LITERAL + | STRING_LITERAL + ; + + +groupBy + : K_GROUP_BY column + ; + + +SPACE + : [ \u000B\t\r\n] -> channel(HIDDEN) + ; + +WS : [ \t]+ -> skip ; + +COMMA : ',' ; +L_BRACKET : '(' ; +R_BRACKET : ')' ; +DOT : '.'; + +EOQ: ';'; + +K_SELECT : S E L E C T; +K_WHERE : W H E R E; +K_WINDOW_TUMBLING : W I N D O W SPACE T U M B L I N G; +K_GROUP_BY : G R O U P SPACE B Y; +K_AND : A N D; +K_AS : A S; +K_OR : O R; +K_IS : I S; +K_LIKE : L I K E; +K_NOT_LIKE : N O T SPACE L I K E; +K_EQUAL : '='; +K_GREATER : '>'; +K_LESS : '<'; +K_LESS_EQUAL : (K_LESS K_EQUAL); +K_GREATER_EQUAL : (K_GREATER K_EQUAL); +K_NOT_EQUAL : ('!' K_EQUAL); +K_NULL : N U L L; +K_IS_NULL : (K_IS SPACE K_NULL); +K_IS_NOT_NULL : (K_IS SPACE K_NOT SPACE K_NULL); +K_NOT : N O T; +K_NOT_IN : (K_NOT SPACE I N); +K_IN : I N; +K_COUNT : C O U N T; +K_SUM : S U M; +K_MIN : M I N; +K_MAX : M A X; +K_AVG : A V G; +K_TRUE : T R U E; +K_FALSE : F A L S E; + + + +IDENTIFIER + : '"' (~'"' | '""')* '"' + | '`' (~'`' | '``')* '`' + | '[' ~']'* ']' + | [a-zA-Z_] [a-zA-Z_0-9]* + ; + + +NUMERIC_LITERAL + : DIGIT+ ( '.' DIGIT* )? ( E [-+]? DIGIT+ )? + | '.' DIGIT+ ( E [-+]? DIGIT+ )? + ; + +STRING_LITERAL + : '\'' ( ~'\'' | '\'\'' )* '\'' + ; + + +BOOLEAN_LITERAL + : (K_TRUE | K_FALSE) + ; + +STAR : '*'; + +fragment DIGIT : [0-9]; +fragment A : [aA]; +fragment B : [bB]; +fragment C : [cC]; +fragment D : [dD]; +fragment E : [eE]; +fragment F : [fF]; +fragment G : [gG]; +fragment H : [hH]; +fragment I : [iI]; +fragment J : [jJ]; +fragment K : [kK]; +fragment L : [lL]; +fragment M : [mM]; +fragment N : [nN]; +fragment O : [oO]; +fragment P : [pP]; +fragment Q : [qQ]; +fragment R : [rR]; +fragment S : [sS]; +fragment T : [tT]; +fragment U : [uU]; +fragment V : [vV]; +fragment W : [wW]; +fragment X : [xX]; +fragment Y : [yY]; +fragment Z : [zZ]; diff --git a/processor/stream_processor/cmd/main.go b/processor/stream_processor/cmd/main.go new file mode 100644 index 000000000000..c33fc151535f --- /dev/null +++ b/processor/stream_processor/cmd/main.go @@ -0,0 +1,50 @@ +package main + +import ( + "bufio" + "fmt" + "github.com/open-telemetry/opentelemetry-collector-contrib/processor/stream_processor/parser" + "go.opentelemetry.io/collector/pdata/pcommon" + "go.opentelemetry.io/collector/pdata/plog" + "go.uber.org/zap" + "os" + "strings" +) + +func main() { + reader := bufio.NewReader(os.Stdin) + + for { + fmt.Print("-> ") + query, _ := reader.ReadString('\n') + if strings.TrimRight(strings.ToLower(query), "\r\n\\") == "quit" { + break + } + + in := make(chan plog.LogRecordSlice) + out := make(chan plog.LogRecordSlice) + outErr := make(chan error) + visitor := parser.NewSQLStreamVisitor(query, in, out, outErr, zap.NewNop()) + + in <- parser.GenerateTestLogs() + + select { + case ls := <-out: + for i := 0; i < ls.Len(); i++ { + ls.At(i).Attributes().Range(func(k string, v pcommon.Value) bool { + fmt.Printf("%q: %q ", k, v.AsString()) + return true + }) + + fmt.Print("\n") + } + + case err := <-outErr: + fmt.Println(err) + continue + } + visitor.Stop() + + } + +} diff --git a/processor/stream_processor/config.go b/processor/stream_processor/config.go new file mode 100644 index 000000000000..70cb4876f633 --- /dev/null +++ b/processor/stream_processor/config.go @@ -0,0 +1,25 @@ +package stream_processor + +import ( + "fmt" + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/adapter" + "go.opentelemetry.io/collector/config" +) + +// Config defines configuration for stream processor. +type Config struct { + config.ProcessorSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct + adapter.BaseConfig `mapstructure:",squash"` + Query string `mapstructure:"query"` +} + +var _ config.Processor = (*Config)(nil) + +// Validate checks if the processor configuration is valid +func (cfg *Config) Validate() error { + if len(cfg.Query) == 0 { + return fmt.Errorf("correct sql query missed") + } + + return nil +} diff --git a/processor/stream_processor/config_test.go b/processor/stream_processor/config_test.go new file mode 100644 index 000000000000..ae10af7c87b8 --- /dev/null +++ b/processor/stream_processor/config_test.go @@ -0,0 +1,27 @@ +package stream_processor + +import ( + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/component/componenttest" + "go.opentelemetry.io/collector/config" + "go.opentelemetry.io/collector/service/servicetest" +) + +func TestConfigLoad(t *testing.T) { + factories, err := componenttest.NopFactories() + assert.Nil(t, err) + + factory := NewFactory() + factories.Processors[typeStr] = factory + cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config.yaml"), factories) + assert.Nil(t, err) + require.NotNil(t, cfg) + id := config.NewComponentIDWithName(typeStr, "") + component := cfg.Processors[id] + assert.Equal(t, component.(*Config).Query, "select * where price = 5;") + +} diff --git a/processor/stream_processor/doc.go b/processor/stream_processor/doc.go new file mode 100644 index 000000000000..84d3606cf41d --- /dev/null +++ b/processor/stream_processor/doc.go @@ -0,0 +1 @@ +package stream_processor diff --git a/processor/stream_processor/factory.go b/processor/stream_processor/factory.go new file mode 100644 index 000000000000..2c47ad19ef2b --- /dev/null +++ b/processor/stream_processor/factory.go @@ -0,0 +1,38 @@ +package stream_processor + +import ( + "context" + "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/config" + "go.opentelemetry.io/collector/consumer" +) + +const ( + // The value of "type" key in configuration. + typeStr = "sql_stream" + // The stability level of the processor. + stability = component.StabilityLevelInDevelopment +) + +var processorCapabilities = consumer.Capabilities{MutatesData: true} + +func NewFactory() component.ProcessorFactory { + return component.NewProcessorFactory(typeStr, createDefaultConfig, + component.WithLogsProcessorAndStabilityLevel(createSqlStreamProcessor, + stability)) +} + +func createDefaultConfig() config.Processor { + return &Config{ + ProcessorSettings: config.NewProcessorSettings(config.NewComponentID(typeStr)), + } +} + +func createSqlStreamProcessor( + _ context.Context, + set component.ProcessorCreateSettings, + cfg config.Processor, + nextConsumer consumer.Logs, +) (component.LogsProcessor, error) { + return newSqlStreamProcessor(nextConsumer, set.Logger, cfg.(*Config)), nil +} diff --git a/processor/stream_processor/parser/Sql.interp b/processor/stream_processor/parser/Sql.interp new file mode 100644 index 000000000000..bcb64eee286b --- /dev/null +++ b/processor/stream_processor/parser/Sql.interp @@ -0,0 +1,111 @@ +token literal names: +null +null +null +',' +'(' +')' +'.' +';' +null +null +null +null +null +null +null +null +null +null +'=' +'>' +'<' +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +'*' + +token symbolic names: +null +SPACE +WS +COMMA +L_BRACKET +R_BRACKET +DOT +EOQ +K_SELECT +K_WHERE +K_WINDOW_TUMBLING +K_GROUP_BY +K_AND +K_AS +K_OR +K_IS +K_LIKE +K_NOT_LIKE +K_EQUAL +K_GREATER +K_LESS +K_LESS_EQUAL +K_GREATER_EQUAL +K_NOT_EQUAL +K_NULL +K_IS_NULL +K_IS_NOT_NULL +K_NOT +K_NOT_IN +K_IN +K_COUNT +K_SUM +K_MIN +K_MAX +K_AVG +K_TRUE +K_FALSE +IDENTIFIER +NUMERIC_LITERAL +STRING_LITERAL +BOOLEAN_LITERAL +STAR + +rule names: +sqlQuery +selectQuery +windowTumbling +resultColumns +aggregationColumns +groupByAggregationColumns +aggregationColumn +column +alias +function +functionName +whereStatement +expr +simpleExpr +compoundExpr +comparisonOperator +literalValue +groupBy + + +atn: +[4, 1, 41, 230, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2, 10, 7, 10, 2, 11, 7, 11, 2, 12, 7, 12, 2, 13, 7, 13, 2, 14, 7, 14, 2, 15, 7, 15, 2, 16, 7, 16, 2, 17, 7, 17, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 3, 1, 43, 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 51, 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 59, 8, 1, 1, 1, 1, 1, 1, 1, 3, 1, 64, 8, 1, 1, 2, 1, 2, 1, 2, 1, 3, 1, 3, 1, 3, 5, 3, 72, 8, 3, 10, 3, 12, 3, 75, 9, 3, 1, 3, 3, 3, 78, 8, 3, 1, 4, 1, 4, 1, 4, 5, 4, 83, 8, 4, 10, 4, 12, 4, 86, 9, 4, 1, 5, 1, 5, 3, 5, 90, 8, 5, 1, 5, 1, 5, 1, 5, 3, 5, 95, 8, 5, 5, 5, 97, 8, 5, 10, 5, 12, 5, 100, 9, 5, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 3, 6, 108, 8, 6, 1, 6, 1, 6, 3, 6, 112, 8, 6, 1, 7, 1, 7, 1, 7, 1, 7, 3, 7, 118, 8, 7, 1, 7, 3, 7, 121, 8, 7, 1, 7, 1, 7, 3, 7, 125, 8, 7, 3, 7, 127, 8, 7, 1, 8, 1, 8, 1, 8, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 3, 9, 138, 8, 9, 1, 9, 1, 9, 5, 9, 142, 8, 9, 10, 9, 12, 9, 145, 9, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 3, 9, 155, 8, 9, 1, 9, 1, 9, 5, 9, 159, 8, 9, 10, 9, 12, 9, 162, 9, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 5, 9, 171, 8, 9, 10, 9, 12, 9, 174, 9, 9, 1, 9, 1, 9, 3, 9, 178, 8, 9, 1, 10, 1, 10, 1, 11, 1, 11, 1, 11, 1, 12, 1, 12, 1, 12, 1, 12, 3, 12, 189, 8, 12, 1, 12, 1, 12, 1, 12, 3, 12, 194, 8, 12, 1, 12, 3, 12, 197, 8, 12, 1, 12, 1, 12, 1, 12, 5, 12, 202, 8, 12, 10, 12, 12, 12, 205, 9, 12, 1, 13, 1, 13, 1, 13, 1, 13, 1, 13, 1, 13, 1, 13, 1, 13, 1, 13, 1, 13, 3, 13, 217, 8, 13, 1, 14, 1, 14, 1, 14, 1, 14, 1, 15, 1, 15, 1, 16, 1, 16, 1, 17, 1, 17, 1, 17, 1, 17, 0, 1, 24, 18, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 0, 4, 1, 0, 30, 34, 2, 0, 12, 12, 14, 14, 2, 0, 15, 23, 28, 29, 1, 0, 38, 40, 241, 0, 36, 1, 0, 0, 0, 2, 63, 1, 0, 0, 0, 4, 65, 1, 0, 0, 0, 6, 77, 1, 0, 0, 0, 8, 79, 1, 0, 0, 0, 10, 89, 1, 0, 0, 0, 12, 101, 1, 0, 0, 0, 14, 126, 1, 0, 0, 0, 16, 128, 1, 0, 0, 0, 18, 177, 1, 0, 0, 0, 20, 179, 1, 0, 0, 0, 22, 181, 1, 0, 0, 0, 24, 196, 1, 0, 0, 0, 26, 216, 1, 0, 0, 0, 28, 218, 1, 0, 0, 0, 30, 222, 1, 0, 0, 0, 32, 224, 1, 0, 0, 0, 34, 226, 1, 0, 0, 0, 36, 37, 3, 2, 1, 0, 37, 38, 5, 0, 0, 1, 38, 1, 1, 0, 0, 0, 39, 40, 5, 8, 0, 0, 40, 42, 3, 6, 3, 0, 41, 43, 3, 22, 11, 0, 42, 41, 1, 0, 0, 0, 42, 43, 1, 0, 0, 0, 43, 44, 1, 0, 0, 0, 44, 45, 5, 7, 0, 0, 45, 64, 1, 0, 0, 0, 46, 47, 5, 8, 0, 0, 47, 48, 3, 8, 4, 0, 48, 50, 3, 4, 2, 0, 49, 51, 3, 22, 11, 0, 50, 49, 1, 0, 0, 0, 50, 51, 1, 0, 0, 0, 51, 52, 1, 0, 0, 0, 52, 53, 5, 7, 0, 0, 53, 64, 1, 0, 0, 0, 54, 55, 5, 8, 0, 0, 55, 56, 3, 10, 5, 0, 56, 58, 3, 4, 2, 0, 57, 59, 3, 22, 11, 0, 58, 57, 1, 0, 0, 0, 58, 59, 1, 0, 0, 0, 59, 60, 1, 0, 0, 0, 60, 61, 3, 34, 17, 0, 61, 62, 5, 7, 0, 0, 62, 64, 1, 0, 0, 0, 63, 39, 1, 0, 0, 0, 63, 46, 1, 0, 0, 0, 63, 54, 1, 0, 0, 0, 64, 3, 1, 0, 0, 0, 65, 66, 5, 10, 0, 0, 66, 67, 5, 38, 0, 0, 67, 5, 1, 0, 0, 0, 68, 73, 3, 14, 7, 0, 69, 70, 5, 3, 0, 0, 70, 72, 3, 14, 7, 0, 71, 69, 1, 0, 0, 0, 72, 75, 1, 0, 0, 0, 73, 71, 1, 0, 0, 0, 73, 74, 1, 0, 0, 0, 74, 78, 1, 0, 0, 0, 75, 73, 1, 0, 0, 0, 76, 78, 5, 41, 0, 0, 77, 68, 1, 0, 0, 0, 77, 76, 1, 0, 0, 0, 78, 7, 1, 0, 0, 0, 79, 84, 3, 12, 6, 0, 80, 81, 5, 3, 0, 0, 81, 83, 3, 12, 6, 0, 82, 80, 1, 0, 0, 0, 83, 86, 1, 0, 0, 0, 84, 82, 1, 0, 0, 0, 84, 85, 1, 0, 0, 0, 85, 9, 1, 0, 0, 0, 86, 84, 1, 0, 0, 0, 87, 90, 3, 14, 7, 0, 88, 90, 3, 12, 6, 0, 89, 87, 1, 0, 0, 0, 89, 88, 1, 0, 0, 0, 90, 98, 1, 0, 0, 0, 91, 94, 5, 3, 0, 0, 92, 95, 3, 12, 6, 0, 93, 95, 3, 14, 7, 0, 94, 92, 1, 0, 0, 0, 94, 93, 1, 0, 0, 0, 95, 97, 1, 0, 0, 0, 96, 91, 1, 0, 0, 0, 97, 100, 1, 0, 0, 0, 98, 96, 1, 0, 0, 0, 98, 99, 1, 0, 0, 0, 99, 11, 1, 0, 0, 0, 100, 98, 1, 0, 0, 0, 101, 102, 7, 0, 0, 0, 102, 107, 5, 4, 0, 0, 103, 108, 5, 37, 0, 0, 104, 105, 5, 37, 0, 0, 105, 106, 5, 6, 0, 0, 106, 108, 5, 37, 0, 0, 107, 103, 1, 0, 0, 0, 107, 104, 1, 0, 0, 0, 108, 109, 1, 0, 0, 0, 109, 111, 5, 5, 0, 0, 110, 112, 3, 16, 8, 0, 111, 110, 1, 0, 0, 0, 111, 112, 1, 0, 0, 0, 112, 13, 1, 0, 0, 0, 113, 118, 5, 37, 0, 0, 114, 115, 5, 37, 0, 0, 115, 116, 5, 6, 0, 0, 116, 118, 5, 37, 0, 0, 117, 113, 1, 0, 0, 0, 117, 114, 1, 0, 0, 0, 118, 120, 1, 0, 0, 0, 119, 121, 3, 16, 8, 0, 120, 119, 1, 0, 0, 0, 120, 121, 1, 0, 0, 0, 121, 127, 1, 0, 0, 0, 122, 124, 3, 18, 9, 0, 123, 125, 3, 16, 8, 0, 124, 123, 1, 0, 0, 0, 124, 125, 1, 0, 0, 0, 125, 127, 1, 0, 0, 0, 126, 117, 1, 0, 0, 0, 126, 122, 1, 0, 0, 0, 127, 15, 1, 0, 0, 0, 128, 129, 5, 13, 0, 0, 129, 130, 5, 37, 0, 0, 130, 17, 1, 0, 0, 0, 131, 132, 3, 20, 10, 0, 132, 137, 5, 4, 0, 0, 133, 138, 5, 37, 0, 0, 134, 135, 5, 37, 0, 0, 135, 136, 5, 6, 0, 0, 136, 138, 5, 37, 0, 0, 137, 133, 1, 0, 0, 0, 137, 134, 1, 0, 0, 0, 138, 143, 1, 0, 0, 0, 139, 140, 5, 3, 0, 0, 140, 142, 3, 32, 16, 0, 141, 139, 1, 0, 0, 0, 142, 145, 1, 0, 0, 0, 143, 141, 1, 0, 0, 0, 143, 144, 1, 0, 0, 0, 144, 146, 1, 0, 0, 0, 145, 143, 1, 0, 0, 0, 146, 147, 5, 5, 0, 0, 147, 178, 1, 0, 0, 0, 148, 149, 3, 20, 10, 0, 149, 154, 5, 4, 0, 0, 150, 155, 5, 37, 0, 0, 151, 152, 5, 37, 0, 0, 152, 153, 5, 6, 0, 0, 153, 155, 5, 37, 0, 0, 154, 150, 1, 0, 0, 0, 154, 151, 1, 0, 0, 0, 155, 160, 1, 0, 0, 0, 156, 157, 5, 3, 0, 0, 157, 159, 3, 32, 16, 0, 158, 156, 1, 0, 0, 0, 159, 162, 1, 0, 0, 0, 160, 158, 1, 0, 0, 0, 160, 161, 1, 0, 0, 0, 161, 163, 1, 0, 0, 0, 162, 160, 1, 0, 0, 0, 163, 164, 5, 5, 0, 0, 164, 178, 1, 0, 0, 0, 165, 166, 3, 20, 10, 0, 166, 167, 5, 4, 0, 0, 167, 172, 3, 18, 9, 0, 168, 169, 5, 3, 0, 0, 169, 171, 3, 32, 16, 0, 170, 168, 1, 0, 0, 0, 171, 174, 1, 0, 0, 0, 172, 170, 1, 0, 0, 0, 172, 173, 1, 0, 0, 0, 173, 175, 1, 0, 0, 0, 174, 172, 1, 0, 0, 0, 175, 176, 5, 5, 0, 0, 176, 178, 1, 0, 0, 0, 177, 131, 1, 0, 0, 0, 177, 148, 1, 0, 0, 0, 177, 165, 1, 0, 0, 0, 178, 19, 1, 0, 0, 0, 179, 180, 5, 37, 0, 0, 180, 21, 1, 0, 0, 0, 181, 182, 5, 9, 0, 0, 182, 183, 3, 24, 12, 0, 183, 23, 1, 0, 0, 0, 184, 185, 6, 12, -1, 0, 185, 197, 3, 26, 13, 0, 186, 189, 3, 28, 14, 0, 187, 189, 3, 26, 13, 0, 188, 186, 1, 0, 0, 0, 188, 187, 1, 0, 0, 0, 189, 190, 1, 0, 0, 0, 190, 193, 7, 1, 0, 0, 191, 194, 3, 28, 14, 0, 192, 194, 3, 26, 13, 0, 193, 191, 1, 0, 0, 0, 193, 192, 1, 0, 0, 0, 194, 197, 1, 0, 0, 0, 195, 197, 3, 28, 14, 0, 196, 184, 1, 0, 0, 0, 196, 188, 1, 0, 0, 0, 196, 195, 1, 0, 0, 0, 197, 203, 1, 0, 0, 0, 198, 199, 10, 2, 0, 0, 199, 200, 7, 1, 0, 0, 200, 202, 3, 24, 12, 3, 201, 198, 1, 0, 0, 0, 202, 205, 1, 0, 0, 0, 203, 201, 1, 0, 0, 0, 203, 204, 1, 0, 0, 0, 204, 25, 1, 0, 0, 0, 205, 203, 1, 0, 0, 0, 206, 207, 5, 37, 0, 0, 207, 208, 3, 30, 15, 0, 208, 209, 3, 32, 16, 0, 209, 217, 1, 0, 0, 0, 210, 211, 5, 37, 0, 0, 211, 212, 5, 6, 0, 0, 212, 213, 5, 37, 0, 0, 213, 214, 3, 30, 15, 0, 214, 215, 3, 32, 16, 0, 215, 217, 1, 0, 0, 0, 216, 206, 1, 0, 0, 0, 216, 210, 1, 0, 0, 0, 217, 27, 1, 0, 0, 0, 218, 219, 5, 4, 0, 0, 219, 220, 3, 24, 12, 0, 220, 221, 5, 5, 0, 0, 221, 29, 1, 0, 0, 0, 222, 223, 7, 2, 0, 0, 223, 31, 1, 0, 0, 0, 224, 225, 7, 3, 0, 0, 225, 33, 1, 0, 0, 0, 226, 227, 5, 11, 0, 0, 227, 228, 3, 14, 7, 0, 228, 35, 1, 0, 0, 0, 27, 42, 50, 58, 63, 73, 77, 84, 89, 94, 98, 107, 111, 117, 120, 124, 126, 137, 143, 154, 160, 172, 177, 188, 193, 196, 203, 216] \ No newline at end of file diff --git a/processor/stream_processor/parser/Sql.tokens b/processor/stream_processor/parser/Sql.tokens new file mode 100644 index 000000000000..93bad54ad372 --- /dev/null +++ b/processor/stream_processor/parser/Sql.tokens @@ -0,0 +1,50 @@ +SPACE=1 +WS=2 +COMMA=3 +L_BRACKET=4 +R_BRACKET=5 +DOT=6 +EOQ=7 +K_SELECT=8 +K_WHERE=9 +K_WINDOW_TUMBLING=10 +K_GROUP_BY=11 +K_AND=12 +K_AS=13 +K_OR=14 +K_IS=15 +K_LIKE=16 +K_NOT_LIKE=17 +K_EQUAL=18 +K_GREATER=19 +K_LESS=20 +K_LESS_EQUAL=21 +K_GREATER_EQUAL=22 +K_NOT_EQUAL=23 +K_NULL=24 +K_IS_NULL=25 +K_IS_NOT_NULL=26 +K_NOT=27 +K_NOT_IN=28 +K_IN=29 +K_COUNT=30 +K_SUM=31 +K_MIN=32 +K_MAX=33 +K_AVG=34 +K_TRUE=35 +K_FALSE=36 +IDENTIFIER=37 +NUMERIC_LITERAL=38 +STRING_LITERAL=39 +BOOLEAN_LITERAL=40 +STAR=41 +','=3 +'('=4 +')'=5 +'.'=6 +';'=7 +'='=18 +'>'=19 +'<'=20 +'*'=41 diff --git a/processor/stream_processor/parser/SqlLexer.interp b/processor/stream_processor/parser/SqlLexer.interp new file mode 100644 index 000000000000..378322250ec2 --- /dev/null +++ b/processor/stream_processor/parser/SqlLexer.interp @@ -0,0 +1,167 @@ +token literal names: +null +null +null +',' +'(' +')' +'.' +';' +null +null +null +null +null +null +null +null +null +null +'=' +'>' +'<' +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +'*' + +token symbolic names: +null +SPACE +WS +COMMA +L_BRACKET +R_BRACKET +DOT +EOQ +K_SELECT +K_WHERE +K_WINDOW_TUMBLING +K_GROUP_BY +K_AND +K_AS +K_OR +K_IS +K_LIKE +K_NOT_LIKE +K_EQUAL +K_GREATER +K_LESS +K_LESS_EQUAL +K_GREATER_EQUAL +K_NOT_EQUAL +K_NULL +K_IS_NULL +K_IS_NOT_NULL +K_NOT +K_NOT_IN +K_IN +K_COUNT +K_SUM +K_MIN +K_MAX +K_AVG +K_TRUE +K_FALSE +IDENTIFIER +NUMERIC_LITERAL +STRING_LITERAL +BOOLEAN_LITERAL +STAR + +rule names: +SPACE +WS +COMMA +L_BRACKET +R_BRACKET +DOT +EOQ +K_SELECT +K_WHERE +K_WINDOW_TUMBLING +K_GROUP_BY +K_AND +K_AS +K_OR +K_IS +K_LIKE +K_NOT_LIKE +K_EQUAL +K_GREATER +K_LESS +K_LESS_EQUAL +K_GREATER_EQUAL +K_NOT_EQUAL +K_NULL +K_IS_NULL +K_IS_NOT_NULL +K_NOT +K_NOT_IN +K_IN +K_COUNT +K_SUM +K_MIN +K_MAX +K_AVG +K_TRUE +K_FALSE +IDENTIFIER +NUMERIC_LITERAL +STRING_LITERAL +BOOLEAN_LITERAL +STAR +DIGIT +A +B +C +D +E +F +G +H +I +J +K +L +M +N +O +P +Q +R +S +T +U +V +W +X +Y +Z + +channel names: +DEFAULT_TOKEN_CHANNEL +HIDDEN + +mode names: +DEFAULT_MODE + +atn: +[4, 0, 41, 450, 6, -1, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2, 10, 7, 10, 2, 11, 7, 11, 2, 12, 7, 12, 2, 13, 7, 13, 2, 14, 7, 14, 2, 15, 7, 15, 2, 16, 7, 16, 2, 17, 7, 17, 2, 18, 7, 18, 2, 19, 7, 19, 2, 20, 7, 20, 2, 21, 7, 21, 2, 22, 7, 22, 2, 23, 7, 23, 2, 24, 7, 24, 2, 25, 7, 25, 2, 26, 7, 26, 2, 27, 7, 27, 2, 28, 7, 28, 2, 29, 7, 29, 2, 30, 7, 30, 2, 31, 7, 31, 2, 32, 7, 32, 2, 33, 7, 33, 2, 34, 7, 34, 2, 35, 7, 35, 2, 36, 7, 36, 2, 37, 7, 37, 2, 38, 7, 38, 2, 39, 7, 39, 2, 40, 7, 40, 2, 41, 7, 41, 2, 42, 7, 42, 2, 43, 7, 43, 2, 44, 7, 44, 2, 45, 7, 45, 2, 46, 7, 46, 2, 47, 7, 47, 2, 48, 7, 48, 2, 49, 7, 49, 2, 50, 7, 50, 2, 51, 7, 51, 2, 52, 7, 52, 2, 53, 7, 53, 2, 54, 7, 54, 2, 55, 7, 55, 2, 56, 7, 56, 2, 57, 7, 57, 2, 58, 7, 58, 2, 59, 7, 59, 2, 60, 7, 60, 2, 61, 7, 61, 2, 62, 7, 62, 2, 63, 7, 63, 2, 64, 7, 64, 2, 65, 7, 65, 2, 66, 7, 66, 2, 67, 7, 67, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 4, 1, 143, 8, 1, 11, 1, 12, 1, 144, 1, 1, 1, 1, 1, 2, 1, 2, 1, 3, 1, 3, 1, 4, 1, 4, 1, 5, 1, 5, 1, 6, 1, 6, 1, 7, 1, 7, 1, 7, 1, 7, 1, 7, 1, 7, 1, 7, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 10, 1, 10, 1, 10, 1, 10, 1, 10, 1, 10, 1, 10, 1, 10, 1, 10, 1, 11, 1, 11, 1, 11, 1, 11, 1, 12, 1, 12, 1, 12, 1, 13, 1, 13, 1, 13, 1, 14, 1, 14, 1, 14, 1, 15, 1, 15, 1, 15, 1, 15, 1, 15, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 17, 1, 17, 1, 18, 1, 18, 1, 19, 1, 19, 1, 20, 1, 20, 1, 20, 1, 21, 1, 21, 1, 21, 1, 22, 1, 22, 1, 22, 1, 23, 1, 23, 1, 23, 1, 23, 1, 23, 1, 24, 1, 24, 1, 24, 1, 24, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 26, 1, 26, 1, 26, 1, 26, 1, 27, 1, 27, 1, 27, 1, 27, 1, 27, 1, 28, 1, 28, 1, 28, 1, 29, 1, 29, 1, 29, 1, 29, 1, 29, 1, 29, 1, 30, 1, 30, 1, 30, 1, 30, 1, 31, 1, 31, 1, 31, 1, 31, 1, 32, 1, 32, 1, 32, 1, 32, 1, 33, 1, 33, 1, 33, 1, 33, 1, 34, 1, 34, 1, 34, 1, 34, 1, 34, 1, 35, 1, 35, 1, 35, 1, 35, 1, 35, 1, 35, 1, 36, 1, 36, 1, 36, 1, 36, 5, 36, 303, 8, 36, 10, 36, 12, 36, 306, 9, 36, 1, 36, 1, 36, 1, 36, 1, 36, 1, 36, 5, 36, 313, 8, 36, 10, 36, 12, 36, 316, 9, 36, 1, 36, 1, 36, 1, 36, 5, 36, 321, 8, 36, 10, 36, 12, 36, 324, 9, 36, 1, 36, 1, 36, 1, 36, 5, 36, 329, 8, 36, 10, 36, 12, 36, 332, 9, 36, 3, 36, 334, 8, 36, 1, 37, 4, 37, 337, 8, 37, 11, 37, 12, 37, 338, 1, 37, 1, 37, 5, 37, 343, 8, 37, 10, 37, 12, 37, 346, 9, 37, 3, 37, 348, 8, 37, 1, 37, 1, 37, 3, 37, 352, 8, 37, 1, 37, 4, 37, 355, 8, 37, 11, 37, 12, 37, 356, 3, 37, 359, 8, 37, 1, 37, 1, 37, 4, 37, 363, 8, 37, 11, 37, 12, 37, 364, 1, 37, 1, 37, 3, 37, 369, 8, 37, 1, 37, 4, 37, 372, 8, 37, 11, 37, 12, 37, 373, 3, 37, 376, 8, 37, 3, 37, 378, 8, 37, 1, 38, 1, 38, 1, 38, 1, 38, 5, 38, 384, 8, 38, 10, 38, 12, 38, 387, 9, 38, 1, 38, 1, 38, 1, 39, 1, 39, 3, 39, 393, 8, 39, 1, 40, 1, 40, 1, 41, 1, 41, 1, 42, 1, 42, 1, 43, 1, 43, 1, 44, 1, 44, 1, 45, 1, 45, 1, 46, 1, 46, 1, 47, 1, 47, 1, 48, 1, 48, 1, 49, 1, 49, 1, 50, 1, 50, 1, 51, 1, 51, 1, 52, 1, 52, 1, 53, 1, 53, 1, 54, 1, 54, 1, 55, 1, 55, 1, 56, 1, 56, 1, 57, 1, 57, 1, 58, 1, 58, 1, 59, 1, 59, 1, 60, 1, 60, 1, 61, 1, 61, 1, 62, 1, 62, 1, 63, 1, 63, 1, 64, 1, 64, 1, 65, 1, 65, 1, 66, 1, 66, 1, 67, 1, 67, 0, 0, 68, 1, 1, 3, 2, 5, 3, 7, 4, 9, 5, 11, 6, 13, 7, 15, 8, 17, 9, 19, 10, 21, 11, 23, 12, 25, 13, 27, 14, 29, 15, 31, 16, 33, 17, 35, 18, 37, 19, 39, 20, 41, 21, 43, 22, 45, 23, 47, 24, 49, 25, 51, 26, 53, 27, 55, 28, 57, 29, 59, 30, 61, 31, 63, 32, 65, 33, 67, 34, 69, 35, 71, 36, 73, 37, 75, 38, 77, 39, 79, 40, 81, 41, 83, 0, 85, 0, 87, 0, 89, 0, 91, 0, 93, 0, 95, 0, 97, 0, 99, 0, 101, 0, 103, 0, 105, 0, 107, 0, 109, 0, 111, 0, 113, 0, 115, 0, 117, 0, 119, 0, 121, 0, 123, 0, 125, 0, 127, 0, 129, 0, 131, 0, 133, 0, 135, 0, 1, 0, 36, 3, 0, 9, 11, 13, 13, 32, 32, 2, 0, 9, 9, 32, 32, 1, 0, 34, 34, 1, 0, 96, 96, 1, 0, 93, 93, 3, 0, 65, 90, 95, 95, 97, 122, 4, 0, 48, 57, 65, 90, 95, 95, 97, 122, 2, 0, 43, 43, 45, 45, 1, 0, 39, 39, 1, 0, 48, 57, 2, 0, 65, 65, 97, 97, 2, 0, 66, 66, 98, 98, 2, 0, 67, 67, 99, 99, 2, 0, 68, 68, 100, 100, 2, 0, 69, 69, 101, 101, 2, 0, 70, 70, 102, 102, 2, 0, 71, 71, 103, 103, 2, 0, 72, 72, 104, 104, 2, 0, 73, 73, 105, 105, 2, 0, 74, 74, 106, 106, 2, 0, 75, 75, 107, 107, 2, 0, 76, 76, 108, 108, 2, 0, 77, 77, 109, 109, 2, 0, 78, 78, 110, 110, 2, 0, 79, 79, 111, 111, 2, 0, 80, 80, 112, 112, 2, 0, 81, 81, 113, 113, 2, 0, 82, 82, 114, 114, 2, 0, 83, 83, 115, 115, 2, 0, 84, 84, 116, 116, 2, 0, 85, 85, 117, 117, 2, 0, 86, 86, 118, 118, 2, 0, 87, 87, 119, 119, 2, 0, 88, 88, 120, 120, 2, 0, 89, 89, 121, 121, 2, 0, 90, 90, 122, 122, 446, 0, 1, 1, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 5, 1, 0, 0, 0, 0, 7, 1, 0, 0, 0, 0, 9, 1, 0, 0, 0, 0, 11, 1, 0, 0, 0, 0, 13, 1, 0, 0, 0, 0, 15, 1, 0, 0, 0, 0, 17, 1, 0, 0, 0, 0, 19, 1, 0, 0, 0, 0, 21, 1, 0, 0, 0, 0, 23, 1, 0, 0, 0, 0, 25, 1, 0, 0, 0, 0, 27, 1, 0, 0, 0, 0, 29, 1, 0, 0, 0, 0, 31, 1, 0, 0, 0, 0, 33, 1, 0, 0, 0, 0, 35, 1, 0, 0, 0, 0, 37, 1, 0, 0, 0, 0, 39, 1, 0, 0, 0, 0, 41, 1, 0, 0, 0, 0, 43, 1, 0, 0, 0, 0, 45, 1, 0, 0, 0, 0, 47, 1, 0, 0, 0, 0, 49, 1, 0, 0, 0, 0, 51, 1, 0, 0, 0, 0, 53, 1, 0, 0, 0, 0, 55, 1, 0, 0, 0, 0, 57, 1, 0, 0, 0, 0, 59, 1, 0, 0, 0, 0, 61, 1, 0, 0, 0, 0, 63, 1, 0, 0, 0, 0, 65, 1, 0, 0, 0, 0, 67, 1, 0, 0, 0, 0, 69, 1, 0, 0, 0, 0, 71, 1, 0, 0, 0, 0, 73, 1, 0, 0, 0, 0, 75, 1, 0, 0, 0, 0, 77, 1, 0, 0, 0, 0, 79, 1, 0, 0, 0, 0, 81, 1, 0, 0, 0, 1, 137, 1, 0, 0, 0, 3, 142, 1, 0, 0, 0, 5, 148, 1, 0, 0, 0, 7, 150, 1, 0, 0, 0, 9, 152, 1, 0, 0, 0, 11, 154, 1, 0, 0, 0, 13, 156, 1, 0, 0, 0, 15, 158, 1, 0, 0, 0, 17, 165, 1, 0, 0, 0, 19, 171, 1, 0, 0, 0, 21, 187, 1, 0, 0, 0, 23, 196, 1, 0, 0, 0, 25, 200, 1, 0, 0, 0, 27, 203, 1, 0, 0, 0, 29, 206, 1, 0, 0, 0, 31, 209, 1, 0, 0, 0, 33, 214, 1, 0, 0, 0, 35, 223, 1, 0, 0, 0, 37, 225, 1, 0, 0, 0, 39, 227, 1, 0, 0, 0, 41, 229, 1, 0, 0, 0, 43, 232, 1, 0, 0, 0, 45, 235, 1, 0, 0, 0, 47, 238, 1, 0, 0, 0, 49, 243, 1, 0, 0, 0, 51, 247, 1, 0, 0, 0, 53, 253, 1, 0, 0, 0, 55, 257, 1, 0, 0, 0, 57, 262, 1, 0, 0, 0, 59, 265, 1, 0, 0, 0, 61, 271, 1, 0, 0, 0, 63, 275, 1, 0, 0, 0, 65, 279, 1, 0, 0, 0, 67, 283, 1, 0, 0, 0, 69, 287, 1, 0, 0, 0, 71, 292, 1, 0, 0, 0, 73, 333, 1, 0, 0, 0, 75, 377, 1, 0, 0, 0, 77, 379, 1, 0, 0, 0, 79, 392, 1, 0, 0, 0, 81, 394, 1, 0, 0, 0, 83, 396, 1, 0, 0, 0, 85, 398, 1, 0, 0, 0, 87, 400, 1, 0, 0, 0, 89, 402, 1, 0, 0, 0, 91, 404, 1, 0, 0, 0, 93, 406, 1, 0, 0, 0, 95, 408, 1, 0, 0, 0, 97, 410, 1, 0, 0, 0, 99, 412, 1, 0, 0, 0, 101, 414, 1, 0, 0, 0, 103, 416, 1, 0, 0, 0, 105, 418, 1, 0, 0, 0, 107, 420, 1, 0, 0, 0, 109, 422, 1, 0, 0, 0, 111, 424, 1, 0, 0, 0, 113, 426, 1, 0, 0, 0, 115, 428, 1, 0, 0, 0, 117, 430, 1, 0, 0, 0, 119, 432, 1, 0, 0, 0, 121, 434, 1, 0, 0, 0, 123, 436, 1, 0, 0, 0, 125, 438, 1, 0, 0, 0, 127, 440, 1, 0, 0, 0, 129, 442, 1, 0, 0, 0, 131, 444, 1, 0, 0, 0, 133, 446, 1, 0, 0, 0, 135, 448, 1, 0, 0, 0, 137, 138, 7, 0, 0, 0, 138, 139, 1, 0, 0, 0, 139, 140, 6, 0, 0, 0, 140, 2, 1, 0, 0, 0, 141, 143, 7, 1, 0, 0, 142, 141, 1, 0, 0, 0, 143, 144, 1, 0, 0, 0, 144, 142, 1, 0, 0, 0, 144, 145, 1, 0, 0, 0, 145, 146, 1, 0, 0, 0, 146, 147, 6, 1, 1, 0, 147, 4, 1, 0, 0, 0, 148, 149, 5, 44, 0, 0, 149, 6, 1, 0, 0, 0, 150, 151, 5, 40, 0, 0, 151, 8, 1, 0, 0, 0, 152, 153, 5, 41, 0, 0, 153, 10, 1, 0, 0, 0, 154, 155, 5, 46, 0, 0, 155, 12, 1, 0, 0, 0, 156, 157, 5, 59, 0, 0, 157, 14, 1, 0, 0, 0, 158, 159, 3, 121, 60, 0, 159, 160, 3, 93, 46, 0, 160, 161, 3, 107, 53, 0, 161, 162, 3, 93, 46, 0, 162, 163, 3, 89, 44, 0, 163, 164, 3, 123, 61, 0, 164, 16, 1, 0, 0, 0, 165, 166, 3, 129, 64, 0, 166, 167, 3, 99, 49, 0, 167, 168, 3, 93, 46, 0, 168, 169, 3, 119, 59, 0, 169, 170, 3, 93, 46, 0, 170, 18, 1, 0, 0, 0, 171, 172, 3, 129, 64, 0, 172, 173, 3, 101, 50, 0, 173, 174, 3, 111, 55, 0, 174, 175, 3, 91, 45, 0, 175, 176, 3, 113, 56, 0, 176, 177, 3, 129, 64, 0, 177, 178, 3, 1, 0, 0, 178, 179, 3, 123, 61, 0, 179, 180, 3, 125, 62, 0, 180, 181, 3, 109, 54, 0, 181, 182, 3, 87, 43, 0, 182, 183, 3, 107, 53, 0, 183, 184, 3, 101, 50, 0, 184, 185, 3, 111, 55, 0, 185, 186, 3, 97, 48, 0, 186, 20, 1, 0, 0, 0, 187, 188, 3, 97, 48, 0, 188, 189, 3, 119, 59, 0, 189, 190, 3, 113, 56, 0, 190, 191, 3, 125, 62, 0, 191, 192, 3, 115, 57, 0, 192, 193, 3, 1, 0, 0, 193, 194, 3, 87, 43, 0, 194, 195, 3, 133, 66, 0, 195, 22, 1, 0, 0, 0, 196, 197, 3, 85, 42, 0, 197, 198, 3, 111, 55, 0, 198, 199, 3, 91, 45, 0, 199, 24, 1, 0, 0, 0, 200, 201, 3, 85, 42, 0, 201, 202, 3, 121, 60, 0, 202, 26, 1, 0, 0, 0, 203, 204, 3, 113, 56, 0, 204, 205, 3, 119, 59, 0, 205, 28, 1, 0, 0, 0, 206, 207, 3, 101, 50, 0, 207, 208, 3, 121, 60, 0, 208, 30, 1, 0, 0, 0, 209, 210, 3, 107, 53, 0, 210, 211, 3, 101, 50, 0, 211, 212, 3, 105, 52, 0, 212, 213, 3, 93, 46, 0, 213, 32, 1, 0, 0, 0, 214, 215, 3, 111, 55, 0, 215, 216, 3, 113, 56, 0, 216, 217, 3, 123, 61, 0, 217, 218, 3, 1, 0, 0, 218, 219, 3, 107, 53, 0, 219, 220, 3, 101, 50, 0, 220, 221, 3, 105, 52, 0, 221, 222, 3, 93, 46, 0, 222, 34, 1, 0, 0, 0, 223, 224, 5, 61, 0, 0, 224, 36, 1, 0, 0, 0, 225, 226, 5, 62, 0, 0, 226, 38, 1, 0, 0, 0, 227, 228, 5, 60, 0, 0, 228, 40, 1, 0, 0, 0, 229, 230, 3, 39, 19, 0, 230, 231, 3, 35, 17, 0, 231, 42, 1, 0, 0, 0, 232, 233, 3, 37, 18, 0, 233, 234, 3, 35, 17, 0, 234, 44, 1, 0, 0, 0, 235, 236, 5, 33, 0, 0, 236, 237, 3, 35, 17, 0, 237, 46, 1, 0, 0, 0, 238, 239, 3, 111, 55, 0, 239, 240, 3, 125, 62, 0, 240, 241, 3, 107, 53, 0, 241, 242, 3, 107, 53, 0, 242, 48, 1, 0, 0, 0, 243, 244, 3, 29, 14, 0, 244, 245, 3, 1, 0, 0, 245, 246, 3, 47, 23, 0, 246, 50, 1, 0, 0, 0, 247, 248, 3, 29, 14, 0, 248, 249, 3, 1, 0, 0, 249, 250, 3, 53, 26, 0, 250, 251, 3, 1, 0, 0, 251, 252, 3, 47, 23, 0, 252, 52, 1, 0, 0, 0, 253, 254, 3, 111, 55, 0, 254, 255, 3, 113, 56, 0, 255, 256, 3, 123, 61, 0, 256, 54, 1, 0, 0, 0, 257, 258, 3, 53, 26, 0, 258, 259, 3, 1, 0, 0, 259, 260, 3, 101, 50, 0, 260, 261, 3, 111, 55, 0, 261, 56, 1, 0, 0, 0, 262, 263, 3, 101, 50, 0, 263, 264, 3, 111, 55, 0, 264, 58, 1, 0, 0, 0, 265, 266, 3, 89, 44, 0, 266, 267, 3, 113, 56, 0, 267, 268, 3, 125, 62, 0, 268, 269, 3, 111, 55, 0, 269, 270, 3, 123, 61, 0, 270, 60, 1, 0, 0, 0, 271, 272, 3, 121, 60, 0, 272, 273, 3, 125, 62, 0, 273, 274, 3, 109, 54, 0, 274, 62, 1, 0, 0, 0, 275, 276, 3, 109, 54, 0, 276, 277, 3, 101, 50, 0, 277, 278, 3, 111, 55, 0, 278, 64, 1, 0, 0, 0, 279, 280, 3, 109, 54, 0, 280, 281, 3, 85, 42, 0, 281, 282, 3, 131, 65, 0, 282, 66, 1, 0, 0, 0, 283, 284, 3, 85, 42, 0, 284, 285, 3, 127, 63, 0, 285, 286, 3, 97, 48, 0, 286, 68, 1, 0, 0, 0, 287, 288, 3, 123, 61, 0, 288, 289, 3, 119, 59, 0, 289, 290, 3, 125, 62, 0, 290, 291, 3, 93, 46, 0, 291, 70, 1, 0, 0, 0, 292, 293, 3, 95, 47, 0, 293, 294, 3, 85, 42, 0, 294, 295, 3, 107, 53, 0, 295, 296, 3, 121, 60, 0, 296, 297, 3, 93, 46, 0, 297, 72, 1, 0, 0, 0, 298, 304, 5, 34, 0, 0, 299, 303, 8, 2, 0, 0, 300, 301, 5, 34, 0, 0, 301, 303, 5, 34, 0, 0, 302, 299, 1, 0, 0, 0, 302, 300, 1, 0, 0, 0, 303, 306, 1, 0, 0, 0, 304, 302, 1, 0, 0, 0, 304, 305, 1, 0, 0, 0, 305, 307, 1, 0, 0, 0, 306, 304, 1, 0, 0, 0, 307, 334, 5, 34, 0, 0, 308, 314, 5, 96, 0, 0, 309, 313, 8, 3, 0, 0, 310, 311, 5, 96, 0, 0, 311, 313, 5, 96, 0, 0, 312, 309, 1, 0, 0, 0, 312, 310, 1, 0, 0, 0, 313, 316, 1, 0, 0, 0, 314, 312, 1, 0, 0, 0, 314, 315, 1, 0, 0, 0, 315, 317, 1, 0, 0, 0, 316, 314, 1, 0, 0, 0, 317, 334, 5, 96, 0, 0, 318, 322, 5, 91, 0, 0, 319, 321, 8, 4, 0, 0, 320, 319, 1, 0, 0, 0, 321, 324, 1, 0, 0, 0, 322, 320, 1, 0, 0, 0, 322, 323, 1, 0, 0, 0, 323, 325, 1, 0, 0, 0, 324, 322, 1, 0, 0, 0, 325, 334, 5, 93, 0, 0, 326, 330, 7, 5, 0, 0, 327, 329, 7, 6, 0, 0, 328, 327, 1, 0, 0, 0, 329, 332, 1, 0, 0, 0, 330, 328, 1, 0, 0, 0, 330, 331, 1, 0, 0, 0, 331, 334, 1, 0, 0, 0, 332, 330, 1, 0, 0, 0, 333, 298, 1, 0, 0, 0, 333, 308, 1, 0, 0, 0, 333, 318, 1, 0, 0, 0, 333, 326, 1, 0, 0, 0, 334, 74, 1, 0, 0, 0, 335, 337, 3, 83, 41, 0, 336, 335, 1, 0, 0, 0, 337, 338, 1, 0, 0, 0, 338, 336, 1, 0, 0, 0, 338, 339, 1, 0, 0, 0, 339, 347, 1, 0, 0, 0, 340, 344, 5, 46, 0, 0, 341, 343, 3, 83, 41, 0, 342, 341, 1, 0, 0, 0, 343, 346, 1, 0, 0, 0, 344, 342, 1, 0, 0, 0, 344, 345, 1, 0, 0, 0, 345, 348, 1, 0, 0, 0, 346, 344, 1, 0, 0, 0, 347, 340, 1, 0, 0, 0, 347, 348, 1, 0, 0, 0, 348, 358, 1, 0, 0, 0, 349, 351, 3, 93, 46, 0, 350, 352, 7, 7, 0, 0, 351, 350, 1, 0, 0, 0, 351, 352, 1, 0, 0, 0, 352, 354, 1, 0, 0, 0, 353, 355, 3, 83, 41, 0, 354, 353, 1, 0, 0, 0, 355, 356, 1, 0, 0, 0, 356, 354, 1, 0, 0, 0, 356, 357, 1, 0, 0, 0, 357, 359, 1, 0, 0, 0, 358, 349, 1, 0, 0, 0, 358, 359, 1, 0, 0, 0, 359, 378, 1, 0, 0, 0, 360, 362, 5, 46, 0, 0, 361, 363, 3, 83, 41, 0, 362, 361, 1, 0, 0, 0, 363, 364, 1, 0, 0, 0, 364, 362, 1, 0, 0, 0, 364, 365, 1, 0, 0, 0, 365, 375, 1, 0, 0, 0, 366, 368, 3, 93, 46, 0, 367, 369, 7, 7, 0, 0, 368, 367, 1, 0, 0, 0, 368, 369, 1, 0, 0, 0, 369, 371, 1, 0, 0, 0, 370, 372, 3, 83, 41, 0, 371, 370, 1, 0, 0, 0, 372, 373, 1, 0, 0, 0, 373, 371, 1, 0, 0, 0, 373, 374, 1, 0, 0, 0, 374, 376, 1, 0, 0, 0, 375, 366, 1, 0, 0, 0, 375, 376, 1, 0, 0, 0, 376, 378, 1, 0, 0, 0, 377, 336, 1, 0, 0, 0, 377, 360, 1, 0, 0, 0, 378, 76, 1, 0, 0, 0, 379, 385, 5, 39, 0, 0, 380, 384, 8, 8, 0, 0, 381, 382, 5, 39, 0, 0, 382, 384, 5, 39, 0, 0, 383, 380, 1, 0, 0, 0, 383, 381, 1, 0, 0, 0, 384, 387, 1, 0, 0, 0, 385, 383, 1, 0, 0, 0, 385, 386, 1, 0, 0, 0, 386, 388, 1, 0, 0, 0, 387, 385, 1, 0, 0, 0, 388, 389, 5, 39, 0, 0, 389, 78, 1, 0, 0, 0, 390, 393, 3, 69, 34, 0, 391, 393, 3, 71, 35, 0, 392, 390, 1, 0, 0, 0, 392, 391, 1, 0, 0, 0, 393, 80, 1, 0, 0, 0, 394, 395, 5, 42, 0, 0, 395, 82, 1, 0, 0, 0, 396, 397, 7, 9, 0, 0, 397, 84, 1, 0, 0, 0, 398, 399, 7, 10, 0, 0, 399, 86, 1, 0, 0, 0, 400, 401, 7, 11, 0, 0, 401, 88, 1, 0, 0, 0, 402, 403, 7, 12, 0, 0, 403, 90, 1, 0, 0, 0, 404, 405, 7, 13, 0, 0, 405, 92, 1, 0, 0, 0, 406, 407, 7, 14, 0, 0, 407, 94, 1, 0, 0, 0, 408, 409, 7, 15, 0, 0, 409, 96, 1, 0, 0, 0, 410, 411, 7, 16, 0, 0, 411, 98, 1, 0, 0, 0, 412, 413, 7, 17, 0, 0, 413, 100, 1, 0, 0, 0, 414, 415, 7, 18, 0, 0, 415, 102, 1, 0, 0, 0, 416, 417, 7, 19, 0, 0, 417, 104, 1, 0, 0, 0, 418, 419, 7, 20, 0, 0, 419, 106, 1, 0, 0, 0, 420, 421, 7, 21, 0, 0, 421, 108, 1, 0, 0, 0, 422, 423, 7, 22, 0, 0, 423, 110, 1, 0, 0, 0, 424, 425, 7, 23, 0, 0, 425, 112, 1, 0, 0, 0, 426, 427, 7, 24, 0, 0, 427, 114, 1, 0, 0, 0, 428, 429, 7, 25, 0, 0, 429, 116, 1, 0, 0, 0, 430, 431, 7, 26, 0, 0, 431, 118, 1, 0, 0, 0, 432, 433, 7, 27, 0, 0, 433, 120, 1, 0, 0, 0, 434, 435, 7, 28, 0, 0, 435, 122, 1, 0, 0, 0, 436, 437, 7, 29, 0, 0, 437, 124, 1, 0, 0, 0, 438, 439, 7, 30, 0, 0, 439, 126, 1, 0, 0, 0, 440, 441, 7, 31, 0, 0, 441, 128, 1, 0, 0, 0, 442, 443, 7, 32, 0, 0, 443, 130, 1, 0, 0, 0, 444, 445, 7, 33, 0, 0, 445, 132, 1, 0, 0, 0, 446, 447, 7, 34, 0, 0, 447, 134, 1, 0, 0, 0, 448, 449, 7, 35, 0, 0, 449, 136, 1, 0, 0, 0, 23, 0, 144, 302, 304, 312, 314, 322, 330, 333, 338, 344, 347, 351, 356, 358, 364, 368, 373, 375, 377, 383, 385, 392, 2, 0, 1, 0, 6, 0, 0] \ No newline at end of file diff --git a/processor/stream_processor/parser/SqlLexer.tokens b/processor/stream_processor/parser/SqlLexer.tokens new file mode 100644 index 000000000000..93bad54ad372 --- /dev/null +++ b/processor/stream_processor/parser/SqlLexer.tokens @@ -0,0 +1,50 @@ +SPACE=1 +WS=2 +COMMA=3 +L_BRACKET=4 +R_BRACKET=5 +DOT=6 +EOQ=7 +K_SELECT=8 +K_WHERE=9 +K_WINDOW_TUMBLING=10 +K_GROUP_BY=11 +K_AND=12 +K_AS=13 +K_OR=14 +K_IS=15 +K_LIKE=16 +K_NOT_LIKE=17 +K_EQUAL=18 +K_GREATER=19 +K_LESS=20 +K_LESS_EQUAL=21 +K_GREATER_EQUAL=22 +K_NOT_EQUAL=23 +K_NULL=24 +K_IS_NULL=25 +K_IS_NOT_NULL=26 +K_NOT=27 +K_NOT_IN=28 +K_IN=29 +K_COUNT=30 +K_SUM=31 +K_MIN=32 +K_MAX=33 +K_AVG=34 +K_TRUE=35 +K_FALSE=36 +IDENTIFIER=37 +NUMERIC_LITERAL=38 +STRING_LITERAL=39 +BOOLEAN_LITERAL=40 +STAR=41 +','=3 +'('=4 +')'=5 +'.'=6 +';'=7 +'='=18 +'>'=19 +'<'=20 +'*'=41 diff --git a/processor/stream_processor/parser/sql_base_visitor.go b/processor/stream_processor/parser/sql_base_visitor.go new file mode 100644 index 000000000000..cc519023b2c3 --- /dev/null +++ b/processor/stream_processor/parser/sql_base_visitor.go @@ -0,0 +1,117 @@ +// Code generated from Sql.g4 by ANTLR 4.10.1. DO NOT EDIT. + +package parser // Sql + +import "github.com/antlr/antlr4/runtime/Go/antlr" + +type BaseSqlVisitor struct { + *antlr.BaseParseTreeVisitor +} + +func (v *BaseSqlVisitor) VisitSqlQuery(ctx *SqlQueryContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitSelectSimple(ctx *SelectSimpleContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitSelectTumbling(ctx *SelectTumblingContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitSelectTumblingGroupBy(ctx *SelectTumblingGroupByContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitWindowTumbling(ctx *WindowTumblingContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitSelectColumns(ctx *SelectColumnsContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitSelectStar(ctx *SelectStarContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitSelectAggregations(ctx *SelectAggregationsContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitSelectGroupByAggregations(ctx *SelectGroupByAggregationsContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitAggregationColumn(ctx *AggregationColumnContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitIdentifierColumn(ctx *IdentifierColumnContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitFunctionColumn(ctx *FunctionColumnContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitAlias(ctx *AliasContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitSimpleFunction(ctx *SimpleFunctionContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitRecursiveFunction(ctx *RecursiveFunctionContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitFunctionName(ctx *FunctionNameContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitWhereStmt(ctx *WhereStmtContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitSimpleCondition(ctx *SimpleConditionContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitCompoundRecursiveCondition(ctx *CompoundRecursiveConditionContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitSimpleCompoundCondition(ctx *SimpleCompoundConditionContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitSimpleRecursiveCondition(ctx *SimpleRecursiveConditionContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitSimpleExpression(ctx *SimpleExpressionContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitNestedExpression(ctx *NestedExpressionContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitCompoundExpression(ctx *CompoundExpressionContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitComparisonOperator(ctx *ComparisonOperatorContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitLiteralValue(ctx *LiteralValueContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *BaseSqlVisitor) VisitGroupBy(ctx *GroupByContext) interface{} { + return v.VisitChildren(ctx) +} diff --git a/processor/stream_processor/parser/sql_lexer.go b/processor/stream_processor/parser/sql_lexer.go new file mode 100644 index 000000000000..608b376fac3f --- /dev/null +++ b/processor/stream_processor/parser/sql_lexer.go @@ -0,0 +1,357 @@ +// Code generated from Sql.g4 by ANTLR 4.10.1. DO NOT EDIT. + +package parser + +import ( + "fmt" + "sync" + "unicode" + + "github.com/antlr/antlr4/runtime/Go/antlr" +) + +// Suppress unused import error +var _ = fmt.Printf +var _ = sync.Once{} +var _ = unicode.IsLetter + +type SqlLexer struct { + *antlr.BaseLexer + channelNames []string + modeNames []string + // TODO: EOF string +} + +var sqllexerLexerStaticData struct { + once sync.Once + serializedATN []int32 + channelNames []string + modeNames []string + literalNames []string + symbolicNames []string + ruleNames []string + predictionContextCache *antlr.PredictionContextCache + atn *antlr.ATN + decisionToDFA []*antlr.DFA +} + +func sqllexerLexerInit() { + staticData := &sqllexerLexerStaticData + staticData.channelNames = []string{ + "DEFAULT_TOKEN_CHANNEL", "HIDDEN", + } + staticData.modeNames = []string{ + "DEFAULT_MODE", + } + staticData.literalNames = []string{ + "", "", "", "','", "'('", "')'", "'.'", "';'", "", "", "", "", "", "", + "", "", "", "", "'='", "'>'", "'<'", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", "", "", "'*'", + } + staticData.symbolicNames = []string{ + "", "SPACE", "WS", "COMMA", "L_BRACKET", "R_BRACKET", "DOT", "EOQ", + "K_SELECT", "K_WHERE", "K_WINDOW_TUMBLING", "K_GROUP_BY", "K_AND", "K_AS", + "K_OR", "K_IS", "K_LIKE", "K_NOT_LIKE", "K_EQUAL", "K_GREATER", "K_LESS", + "K_LESS_EQUAL", "K_GREATER_EQUAL", "K_NOT_EQUAL", "K_NULL", "K_IS_NULL", + "K_IS_NOT_NULL", "K_NOT", "K_NOT_IN", "K_IN", "K_COUNT", "K_SUM", "K_MIN", + "K_MAX", "K_AVG", "K_TRUE", "K_FALSE", "IDENTIFIER", "NUMERIC_LITERAL", + "STRING_LITERAL", "BOOLEAN_LITERAL", "STAR", + } + staticData.ruleNames = []string{ + "SPACE", "WS", "COMMA", "L_BRACKET", "R_BRACKET", "DOT", "EOQ", "K_SELECT", + "K_WHERE", "K_WINDOW_TUMBLING", "K_GROUP_BY", "K_AND", "K_AS", "K_OR", + "K_IS", "K_LIKE", "K_NOT_LIKE", "K_EQUAL", "K_GREATER", "K_LESS", "K_LESS_EQUAL", + "K_GREATER_EQUAL", "K_NOT_EQUAL", "K_NULL", "K_IS_NULL", "K_IS_NOT_NULL", + "K_NOT", "K_NOT_IN", "K_IN", "K_COUNT", "K_SUM", "K_MIN", "K_MAX", "K_AVG", + "K_TRUE", "K_FALSE", "IDENTIFIER", "NUMERIC_LITERAL", "STRING_LITERAL", + "BOOLEAN_LITERAL", "STAR", "DIGIT", "A", "B", "C", "D", "E", "F", "G", + "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", + "V", "W", "X", "Y", "Z", + } + staticData.predictionContextCache = antlr.NewPredictionContextCache() + staticData.serializedATN = []int32{ + 4, 0, 41, 450, 6, -1, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, + 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2, + 10, 7, 10, 2, 11, 7, 11, 2, 12, 7, 12, 2, 13, 7, 13, 2, 14, 7, 14, 2, 15, + 7, 15, 2, 16, 7, 16, 2, 17, 7, 17, 2, 18, 7, 18, 2, 19, 7, 19, 2, 20, 7, + 20, 2, 21, 7, 21, 2, 22, 7, 22, 2, 23, 7, 23, 2, 24, 7, 24, 2, 25, 7, 25, + 2, 26, 7, 26, 2, 27, 7, 27, 2, 28, 7, 28, 2, 29, 7, 29, 2, 30, 7, 30, 2, + 31, 7, 31, 2, 32, 7, 32, 2, 33, 7, 33, 2, 34, 7, 34, 2, 35, 7, 35, 2, 36, + 7, 36, 2, 37, 7, 37, 2, 38, 7, 38, 2, 39, 7, 39, 2, 40, 7, 40, 2, 41, 7, + 41, 2, 42, 7, 42, 2, 43, 7, 43, 2, 44, 7, 44, 2, 45, 7, 45, 2, 46, 7, 46, + 2, 47, 7, 47, 2, 48, 7, 48, 2, 49, 7, 49, 2, 50, 7, 50, 2, 51, 7, 51, 2, + 52, 7, 52, 2, 53, 7, 53, 2, 54, 7, 54, 2, 55, 7, 55, 2, 56, 7, 56, 2, 57, + 7, 57, 2, 58, 7, 58, 2, 59, 7, 59, 2, 60, 7, 60, 2, 61, 7, 61, 2, 62, 7, + 62, 2, 63, 7, 63, 2, 64, 7, 64, 2, 65, 7, 65, 2, 66, 7, 66, 2, 67, 7, 67, + 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 4, 1, 143, 8, 1, 11, 1, 12, 1, 144, 1, 1, + 1, 1, 1, 2, 1, 2, 1, 3, 1, 3, 1, 4, 1, 4, 1, 5, 1, 5, 1, 6, 1, 6, 1, 7, + 1, 7, 1, 7, 1, 7, 1, 7, 1, 7, 1, 7, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, + 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, + 1, 9, 1, 9, 1, 9, 1, 9, 1, 10, 1, 10, 1, 10, 1, 10, 1, 10, 1, 10, 1, 10, + 1, 10, 1, 10, 1, 11, 1, 11, 1, 11, 1, 11, 1, 12, 1, 12, 1, 12, 1, 13, 1, + 13, 1, 13, 1, 14, 1, 14, 1, 14, 1, 15, 1, 15, 1, 15, 1, 15, 1, 15, 1, 16, + 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 17, 1, 17, 1, + 18, 1, 18, 1, 19, 1, 19, 1, 20, 1, 20, 1, 20, 1, 21, 1, 21, 1, 21, 1, 22, + 1, 22, 1, 22, 1, 23, 1, 23, 1, 23, 1, 23, 1, 23, 1, 24, 1, 24, 1, 24, 1, + 24, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 26, 1, 26, 1, 26, 1, 26, + 1, 27, 1, 27, 1, 27, 1, 27, 1, 27, 1, 28, 1, 28, 1, 28, 1, 29, 1, 29, 1, + 29, 1, 29, 1, 29, 1, 29, 1, 30, 1, 30, 1, 30, 1, 30, 1, 31, 1, 31, 1, 31, + 1, 31, 1, 32, 1, 32, 1, 32, 1, 32, 1, 33, 1, 33, 1, 33, 1, 33, 1, 34, 1, + 34, 1, 34, 1, 34, 1, 34, 1, 35, 1, 35, 1, 35, 1, 35, 1, 35, 1, 35, 1, 36, + 1, 36, 1, 36, 1, 36, 5, 36, 303, 8, 36, 10, 36, 12, 36, 306, 9, 36, 1, + 36, 1, 36, 1, 36, 1, 36, 1, 36, 5, 36, 313, 8, 36, 10, 36, 12, 36, 316, + 9, 36, 1, 36, 1, 36, 1, 36, 5, 36, 321, 8, 36, 10, 36, 12, 36, 324, 9, + 36, 1, 36, 1, 36, 1, 36, 5, 36, 329, 8, 36, 10, 36, 12, 36, 332, 9, 36, + 3, 36, 334, 8, 36, 1, 37, 4, 37, 337, 8, 37, 11, 37, 12, 37, 338, 1, 37, + 1, 37, 5, 37, 343, 8, 37, 10, 37, 12, 37, 346, 9, 37, 3, 37, 348, 8, 37, + 1, 37, 1, 37, 3, 37, 352, 8, 37, 1, 37, 4, 37, 355, 8, 37, 11, 37, 12, + 37, 356, 3, 37, 359, 8, 37, 1, 37, 1, 37, 4, 37, 363, 8, 37, 11, 37, 12, + 37, 364, 1, 37, 1, 37, 3, 37, 369, 8, 37, 1, 37, 4, 37, 372, 8, 37, 11, + 37, 12, 37, 373, 3, 37, 376, 8, 37, 3, 37, 378, 8, 37, 1, 38, 1, 38, 1, + 38, 1, 38, 5, 38, 384, 8, 38, 10, 38, 12, 38, 387, 9, 38, 1, 38, 1, 38, + 1, 39, 1, 39, 3, 39, 393, 8, 39, 1, 40, 1, 40, 1, 41, 1, 41, 1, 42, 1, + 42, 1, 43, 1, 43, 1, 44, 1, 44, 1, 45, 1, 45, 1, 46, 1, 46, 1, 47, 1, 47, + 1, 48, 1, 48, 1, 49, 1, 49, 1, 50, 1, 50, 1, 51, 1, 51, 1, 52, 1, 52, 1, + 53, 1, 53, 1, 54, 1, 54, 1, 55, 1, 55, 1, 56, 1, 56, 1, 57, 1, 57, 1, 58, + 1, 58, 1, 59, 1, 59, 1, 60, 1, 60, 1, 61, 1, 61, 1, 62, 1, 62, 1, 63, 1, + 63, 1, 64, 1, 64, 1, 65, 1, 65, 1, 66, 1, 66, 1, 67, 1, 67, 0, 0, 68, 1, + 1, 3, 2, 5, 3, 7, 4, 9, 5, 11, 6, 13, 7, 15, 8, 17, 9, 19, 10, 21, 11, + 23, 12, 25, 13, 27, 14, 29, 15, 31, 16, 33, 17, 35, 18, 37, 19, 39, 20, + 41, 21, 43, 22, 45, 23, 47, 24, 49, 25, 51, 26, 53, 27, 55, 28, 57, 29, + 59, 30, 61, 31, 63, 32, 65, 33, 67, 34, 69, 35, 71, 36, 73, 37, 75, 38, + 77, 39, 79, 40, 81, 41, 83, 0, 85, 0, 87, 0, 89, 0, 91, 0, 93, 0, 95, 0, + 97, 0, 99, 0, 101, 0, 103, 0, 105, 0, 107, 0, 109, 0, 111, 0, 113, 0, 115, + 0, 117, 0, 119, 0, 121, 0, 123, 0, 125, 0, 127, 0, 129, 0, 131, 0, 133, + 0, 135, 0, 1, 0, 36, 3, 0, 9, 11, 13, 13, 32, 32, 2, 0, 9, 9, 32, 32, 1, + 0, 34, 34, 1, 0, 96, 96, 1, 0, 93, 93, 3, 0, 65, 90, 95, 95, 97, 122, 4, + 0, 48, 57, 65, 90, 95, 95, 97, 122, 2, 0, 43, 43, 45, 45, 1, 0, 39, 39, + 1, 0, 48, 57, 2, 0, 65, 65, 97, 97, 2, 0, 66, 66, 98, 98, 2, 0, 67, 67, + 99, 99, 2, 0, 68, 68, 100, 100, 2, 0, 69, 69, 101, 101, 2, 0, 70, 70, 102, + 102, 2, 0, 71, 71, 103, 103, 2, 0, 72, 72, 104, 104, 2, 0, 73, 73, 105, + 105, 2, 0, 74, 74, 106, 106, 2, 0, 75, 75, 107, 107, 2, 0, 76, 76, 108, + 108, 2, 0, 77, 77, 109, 109, 2, 0, 78, 78, 110, 110, 2, 0, 79, 79, 111, + 111, 2, 0, 80, 80, 112, 112, 2, 0, 81, 81, 113, 113, 2, 0, 82, 82, 114, + 114, 2, 0, 83, 83, 115, 115, 2, 0, 84, 84, 116, 116, 2, 0, 85, 85, 117, + 117, 2, 0, 86, 86, 118, 118, 2, 0, 87, 87, 119, 119, 2, 0, 88, 88, 120, + 120, 2, 0, 89, 89, 121, 121, 2, 0, 90, 90, 122, 122, 446, 0, 1, 1, 0, 0, + 0, 0, 3, 1, 0, 0, 0, 0, 5, 1, 0, 0, 0, 0, 7, 1, 0, 0, 0, 0, 9, 1, 0, 0, + 0, 0, 11, 1, 0, 0, 0, 0, 13, 1, 0, 0, 0, 0, 15, 1, 0, 0, 0, 0, 17, 1, 0, + 0, 0, 0, 19, 1, 0, 0, 0, 0, 21, 1, 0, 0, 0, 0, 23, 1, 0, 0, 0, 0, 25, 1, + 0, 0, 0, 0, 27, 1, 0, 0, 0, 0, 29, 1, 0, 0, 0, 0, 31, 1, 0, 0, 0, 0, 33, + 1, 0, 0, 0, 0, 35, 1, 0, 0, 0, 0, 37, 1, 0, 0, 0, 0, 39, 1, 0, 0, 0, 0, + 41, 1, 0, 0, 0, 0, 43, 1, 0, 0, 0, 0, 45, 1, 0, 0, 0, 0, 47, 1, 0, 0, 0, + 0, 49, 1, 0, 0, 0, 0, 51, 1, 0, 0, 0, 0, 53, 1, 0, 0, 0, 0, 55, 1, 0, 0, + 0, 0, 57, 1, 0, 0, 0, 0, 59, 1, 0, 0, 0, 0, 61, 1, 0, 0, 0, 0, 63, 1, 0, + 0, 0, 0, 65, 1, 0, 0, 0, 0, 67, 1, 0, 0, 0, 0, 69, 1, 0, 0, 0, 0, 71, 1, + 0, 0, 0, 0, 73, 1, 0, 0, 0, 0, 75, 1, 0, 0, 0, 0, 77, 1, 0, 0, 0, 0, 79, + 1, 0, 0, 0, 0, 81, 1, 0, 0, 0, 1, 137, 1, 0, 0, 0, 3, 142, 1, 0, 0, 0, + 5, 148, 1, 0, 0, 0, 7, 150, 1, 0, 0, 0, 9, 152, 1, 0, 0, 0, 11, 154, 1, + 0, 0, 0, 13, 156, 1, 0, 0, 0, 15, 158, 1, 0, 0, 0, 17, 165, 1, 0, 0, 0, + 19, 171, 1, 0, 0, 0, 21, 187, 1, 0, 0, 0, 23, 196, 1, 0, 0, 0, 25, 200, + 1, 0, 0, 0, 27, 203, 1, 0, 0, 0, 29, 206, 1, 0, 0, 0, 31, 209, 1, 0, 0, + 0, 33, 214, 1, 0, 0, 0, 35, 223, 1, 0, 0, 0, 37, 225, 1, 0, 0, 0, 39, 227, + 1, 0, 0, 0, 41, 229, 1, 0, 0, 0, 43, 232, 1, 0, 0, 0, 45, 235, 1, 0, 0, + 0, 47, 238, 1, 0, 0, 0, 49, 243, 1, 0, 0, 0, 51, 247, 1, 0, 0, 0, 53, 253, + 1, 0, 0, 0, 55, 257, 1, 0, 0, 0, 57, 262, 1, 0, 0, 0, 59, 265, 1, 0, 0, + 0, 61, 271, 1, 0, 0, 0, 63, 275, 1, 0, 0, 0, 65, 279, 1, 0, 0, 0, 67, 283, + 1, 0, 0, 0, 69, 287, 1, 0, 0, 0, 71, 292, 1, 0, 0, 0, 73, 333, 1, 0, 0, + 0, 75, 377, 1, 0, 0, 0, 77, 379, 1, 0, 0, 0, 79, 392, 1, 0, 0, 0, 81, 394, + 1, 0, 0, 0, 83, 396, 1, 0, 0, 0, 85, 398, 1, 0, 0, 0, 87, 400, 1, 0, 0, + 0, 89, 402, 1, 0, 0, 0, 91, 404, 1, 0, 0, 0, 93, 406, 1, 0, 0, 0, 95, 408, + 1, 0, 0, 0, 97, 410, 1, 0, 0, 0, 99, 412, 1, 0, 0, 0, 101, 414, 1, 0, 0, + 0, 103, 416, 1, 0, 0, 0, 105, 418, 1, 0, 0, 0, 107, 420, 1, 0, 0, 0, 109, + 422, 1, 0, 0, 0, 111, 424, 1, 0, 0, 0, 113, 426, 1, 0, 0, 0, 115, 428, + 1, 0, 0, 0, 117, 430, 1, 0, 0, 0, 119, 432, 1, 0, 0, 0, 121, 434, 1, 0, + 0, 0, 123, 436, 1, 0, 0, 0, 125, 438, 1, 0, 0, 0, 127, 440, 1, 0, 0, 0, + 129, 442, 1, 0, 0, 0, 131, 444, 1, 0, 0, 0, 133, 446, 1, 0, 0, 0, 135, + 448, 1, 0, 0, 0, 137, 138, 7, 0, 0, 0, 138, 139, 1, 0, 0, 0, 139, 140, + 6, 0, 0, 0, 140, 2, 1, 0, 0, 0, 141, 143, 7, 1, 0, 0, 142, 141, 1, 0, 0, + 0, 143, 144, 1, 0, 0, 0, 144, 142, 1, 0, 0, 0, 144, 145, 1, 0, 0, 0, 145, + 146, 1, 0, 0, 0, 146, 147, 6, 1, 1, 0, 147, 4, 1, 0, 0, 0, 148, 149, 5, + 44, 0, 0, 149, 6, 1, 0, 0, 0, 150, 151, 5, 40, 0, 0, 151, 8, 1, 0, 0, 0, + 152, 153, 5, 41, 0, 0, 153, 10, 1, 0, 0, 0, 154, 155, 5, 46, 0, 0, 155, + 12, 1, 0, 0, 0, 156, 157, 5, 59, 0, 0, 157, 14, 1, 0, 0, 0, 158, 159, 3, + 121, 60, 0, 159, 160, 3, 93, 46, 0, 160, 161, 3, 107, 53, 0, 161, 162, + 3, 93, 46, 0, 162, 163, 3, 89, 44, 0, 163, 164, 3, 123, 61, 0, 164, 16, + 1, 0, 0, 0, 165, 166, 3, 129, 64, 0, 166, 167, 3, 99, 49, 0, 167, 168, + 3, 93, 46, 0, 168, 169, 3, 119, 59, 0, 169, 170, 3, 93, 46, 0, 170, 18, + 1, 0, 0, 0, 171, 172, 3, 129, 64, 0, 172, 173, 3, 101, 50, 0, 173, 174, + 3, 111, 55, 0, 174, 175, 3, 91, 45, 0, 175, 176, 3, 113, 56, 0, 176, 177, + 3, 129, 64, 0, 177, 178, 3, 1, 0, 0, 178, 179, 3, 123, 61, 0, 179, 180, + 3, 125, 62, 0, 180, 181, 3, 109, 54, 0, 181, 182, 3, 87, 43, 0, 182, 183, + 3, 107, 53, 0, 183, 184, 3, 101, 50, 0, 184, 185, 3, 111, 55, 0, 185, 186, + 3, 97, 48, 0, 186, 20, 1, 0, 0, 0, 187, 188, 3, 97, 48, 0, 188, 189, 3, + 119, 59, 0, 189, 190, 3, 113, 56, 0, 190, 191, 3, 125, 62, 0, 191, 192, + 3, 115, 57, 0, 192, 193, 3, 1, 0, 0, 193, 194, 3, 87, 43, 0, 194, 195, + 3, 133, 66, 0, 195, 22, 1, 0, 0, 0, 196, 197, 3, 85, 42, 0, 197, 198, 3, + 111, 55, 0, 198, 199, 3, 91, 45, 0, 199, 24, 1, 0, 0, 0, 200, 201, 3, 85, + 42, 0, 201, 202, 3, 121, 60, 0, 202, 26, 1, 0, 0, 0, 203, 204, 3, 113, + 56, 0, 204, 205, 3, 119, 59, 0, 205, 28, 1, 0, 0, 0, 206, 207, 3, 101, + 50, 0, 207, 208, 3, 121, 60, 0, 208, 30, 1, 0, 0, 0, 209, 210, 3, 107, + 53, 0, 210, 211, 3, 101, 50, 0, 211, 212, 3, 105, 52, 0, 212, 213, 3, 93, + 46, 0, 213, 32, 1, 0, 0, 0, 214, 215, 3, 111, 55, 0, 215, 216, 3, 113, + 56, 0, 216, 217, 3, 123, 61, 0, 217, 218, 3, 1, 0, 0, 218, 219, 3, 107, + 53, 0, 219, 220, 3, 101, 50, 0, 220, 221, 3, 105, 52, 0, 221, 222, 3, 93, + 46, 0, 222, 34, 1, 0, 0, 0, 223, 224, 5, 61, 0, 0, 224, 36, 1, 0, 0, 0, + 225, 226, 5, 62, 0, 0, 226, 38, 1, 0, 0, 0, 227, 228, 5, 60, 0, 0, 228, + 40, 1, 0, 0, 0, 229, 230, 3, 39, 19, 0, 230, 231, 3, 35, 17, 0, 231, 42, + 1, 0, 0, 0, 232, 233, 3, 37, 18, 0, 233, 234, 3, 35, 17, 0, 234, 44, 1, + 0, 0, 0, 235, 236, 5, 33, 0, 0, 236, 237, 3, 35, 17, 0, 237, 46, 1, 0, + 0, 0, 238, 239, 3, 111, 55, 0, 239, 240, 3, 125, 62, 0, 240, 241, 3, 107, + 53, 0, 241, 242, 3, 107, 53, 0, 242, 48, 1, 0, 0, 0, 243, 244, 3, 29, 14, + 0, 244, 245, 3, 1, 0, 0, 245, 246, 3, 47, 23, 0, 246, 50, 1, 0, 0, 0, 247, + 248, 3, 29, 14, 0, 248, 249, 3, 1, 0, 0, 249, 250, 3, 53, 26, 0, 250, 251, + 3, 1, 0, 0, 251, 252, 3, 47, 23, 0, 252, 52, 1, 0, 0, 0, 253, 254, 3, 111, + 55, 0, 254, 255, 3, 113, 56, 0, 255, 256, 3, 123, 61, 0, 256, 54, 1, 0, + 0, 0, 257, 258, 3, 53, 26, 0, 258, 259, 3, 1, 0, 0, 259, 260, 3, 101, 50, + 0, 260, 261, 3, 111, 55, 0, 261, 56, 1, 0, 0, 0, 262, 263, 3, 101, 50, + 0, 263, 264, 3, 111, 55, 0, 264, 58, 1, 0, 0, 0, 265, 266, 3, 89, 44, 0, + 266, 267, 3, 113, 56, 0, 267, 268, 3, 125, 62, 0, 268, 269, 3, 111, 55, + 0, 269, 270, 3, 123, 61, 0, 270, 60, 1, 0, 0, 0, 271, 272, 3, 121, 60, + 0, 272, 273, 3, 125, 62, 0, 273, 274, 3, 109, 54, 0, 274, 62, 1, 0, 0, + 0, 275, 276, 3, 109, 54, 0, 276, 277, 3, 101, 50, 0, 277, 278, 3, 111, + 55, 0, 278, 64, 1, 0, 0, 0, 279, 280, 3, 109, 54, 0, 280, 281, 3, 85, 42, + 0, 281, 282, 3, 131, 65, 0, 282, 66, 1, 0, 0, 0, 283, 284, 3, 85, 42, 0, + 284, 285, 3, 127, 63, 0, 285, 286, 3, 97, 48, 0, 286, 68, 1, 0, 0, 0, 287, + 288, 3, 123, 61, 0, 288, 289, 3, 119, 59, 0, 289, 290, 3, 125, 62, 0, 290, + 291, 3, 93, 46, 0, 291, 70, 1, 0, 0, 0, 292, 293, 3, 95, 47, 0, 293, 294, + 3, 85, 42, 0, 294, 295, 3, 107, 53, 0, 295, 296, 3, 121, 60, 0, 296, 297, + 3, 93, 46, 0, 297, 72, 1, 0, 0, 0, 298, 304, 5, 34, 0, 0, 299, 303, 8, + 2, 0, 0, 300, 301, 5, 34, 0, 0, 301, 303, 5, 34, 0, 0, 302, 299, 1, 0, + 0, 0, 302, 300, 1, 0, 0, 0, 303, 306, 1, 0, 0, 0, 304, 302, 1, 0, 0, 0, + 304, 305, 1, 0, 0, 0, 305, 307, 1, 0, 0, 0, 306, 304, 1, 0, 0, 0, 307, + 334, 5, 34, 0, 0, 308, 314, 5, 96, 0, 0, 309, 313, 8, 3, 0, 0, 310, 311, + 5, 96, 0, 0, 311, 313, 5, 96, 0, 0, 312, 309, 1, 0, 0, 0, 312, 310, 1, + 0, 0, 0, 313, 316, 1, 0, 0, 0, 314, 312, 1, 0, 0, 0, 314, 315, 1, 0, 0, + 0, 315, 317, 1, 0, 0, 0, 316, 314, 1, 0, 0, 0, 317, 334, 5, 96, 0, 0, 318, + 322, 5, 91, 0, 0, 319, 321, 8, 4, 0, 0, 320, 319, 1, 0, 0, 0, 321, 324, + 1, 0, 0, 0, 322, 320, 1, 0, 0, 0, 322, 323, 1, 0, 0, 0, 323, 325, 1, 0, + 0, 0, 324, 322, 1, 0, 0, 0, 325, 334, 5, 93, 0, 0, 326, 330, 7, 5, 0, 0, + 327, 329, 7, 6, 0, 0, 328, 327, 1, 0, 0, 0, 329, 332, 1, 0, 0, 0, 330, + 328, 1, 0, 0, 0, 330, 331, 1, 0, 0, 0, 331, 334, 1, 0, 0, 0, 332, 330, + 1, 0, 0, 0, 333, 298, 1, 0, 0, 0, 333, 308, 1, 0, 0, 0, 333, 318, 1, 0, + 0, 0, 333, 326, 1, 0, 0, 0, 334, 74, 1, 0, 0, 0, 335, 337, 3, 83, 41, 0, + 336, 335, 1, 0, 0, 0, 337, 338, 1, 0, 0, 0, 338, 336, 1, 0, 0, 0, 338, + 339, 1, 0, 0, 0, 339, 347, 1, 0, 0, 0, 340, 344, 5, 46, 0, 0, 341, 343, + 3, 83, 41, 0, 342, 341, 1, 0, 0, 0, 343, 346, 1, 0, 0, 0, 344, 342, 1, + 0, 0, 0, 344, 345, 1, 0, 0, 0, 345, 348, 1, 0, 0, 0, 346, 344, 1, 0, 0, + 0, 347, 340, 1, 0, 0, 0, 347, 348, 1, 0, 0, 0, 348, 358, 1, 0, 0, 0, 349, + 351, 3, 93, 46, 0, 350, 352, 7, 7, 0, 0, 351, 350, 1, 0, 0, 0, 351, 352, + 1, 0, 0, 0, 352, 354, 1, 0, 0, 0, 353, 355, 3, 83, 41, 0, 354, 353, 1, + 0, 0, 0, 355, 356, 1, 0, 0, 0, 356, 354, 1, 0, 0, 0, 356, 357, 1, 0, 0, + 0, 357, 359, 1, 0, 0, 0, 358, 349, 1, 0, 0, 0, 358, 359, 1, 0, 0, 0, 359, + 378, 1, 0, 0, 0, 360, 362, 5, 46, 0, 0, 361, 363, 3, 83, 41, 0, 362, 361, + 1, 0, 0, 0, 363, 364, 1, 0, 0, 0, 364, 362, 1, 0, 0, 0, 364, 365, 1, 0, + 0, 0, 365, 375, 1, 0, 0, 0, 366, 368, 3, 93, 46, 0, 367, 369, 7, 7, 0, + 0, 368, 367, 1, 0, 0, 0, 368, 369, 1, 0, 0, 0, 369, 371, 1, 0, 0, 0, 370, + 372, 3, 83, 41, 0, 371, 370, 1, 0, 0, 0, 372, 373, 1, 0, 0, 0, 373, 371, + 1, 0, 0, 0, 373, 374, 1, 0, 0, 0, 374, 376, 1, 0, 0, 0, 375, 366, 1, 0, + 0, 0, 375, 376, 1, 0, 0, 0, 376, 378, 1, 0, 0, 0, 377, 336, 1, 0, 0, 0, + 377, 360, 1, 0, 0, 0, 378, 76, 1, 0, 0, 0, 379, 385, 5, 39, 0, 0, 380, + 384, 8, 8, 0, 0, 381, 382, 5, 39, 0, 0, 382, 384, 5, 39, 0, 0, 383, 380, + 1, 0, 0, 0, 383, 381, 1, 0, 0, 0, 384, 387, 1, 0, 0, 0, 385, 383, 1, 0, + 0, 0, 385, 386, 1, 0, 0, 0, 386, 388, 1, 0, 0, 0, 387, 385, 1, 0, 0, 0, + 388, 389, 5, 39, 0, 0, 389, 78, 1, 0, 0, 0, 390, 393, 3, 69, 34, 0, 391, + 393, 3, 71, 35, 0, 392, 390, 1, 0, 0, 0, 392, 391, 1, 0, 0, 0, 393, 80, + 1, 0, 0, 0, 394, 395, 5, 42, 0, 0, 395, 82, 1, 0, 0, 0, 396, 397, 7, 9, + 0, 0, 397, 84, 1, 0, 0, 0, 398, 399, 7, 10, 0, 0, 399, 86, 1, 0, 0, 0, + 400, 401, 7, 11, 0, 0, 401, 88, 1, 0, 0, 0, 402, 403, 7, 12, 0, 0, 403, + 90, 1, 0, 0, 0, 404, 405, 7, 13, 0, 0, 405, 92, 1, 0, 0, 0, 406, 407, 7, + 14, 0, 0, 407, 94, 1, 0, 0, 0, 408, 409, 7, 15, 0, 0, 409, 96, 1, 0, 0, + 0, 410, 411, 7, 16, 0, 0, 411, 98, 1, 0, 0, 0, 412, 413, 7, 17, 0, 0, 413, + 100, 1, 0, 0, 0, 414, 415, 7, 18, 0, 0, 415, 102, 1, 0, 0, 0, 416, 417, + 7, 19, 0, 0, 417, 104, 1, 0, 0, 0, 418, 419, 7, 20, 0, 0, 419, 106, 1, + 0, 0, 0, 420, 421, 7, 21, 0, 0, 421, 108, 1, 0, 0, 0, 422, 423, 7, 22, + 0, 0, 423, 110, 1, 0, 0, 0, 424, 425, 7, 23, 0, 0, 425, 112, 1, 0, 0, 0, + 426, 427, 7, 24, 0, 0, 427, 114, 1, 0, 0, 0, 428, 429, 7, 25, 0, 0, 429, + 116, 1, 0, 0, 0, 430, 431, 7, 26, 0, 0, 431, 118, 1, 0, 0, 0, 432, 433, + 7, 27, 0, 0, 433, 120, 1, 0, 0, 0, 434, 435, 7, 28, 0, 0, 435, 122, 1, + 0, 0, 0, 436, 437, 7, 29, 0, 0, 437, 124, 1, 0, 0, 0, 438, 439, 7, 30, + 0, 0, 439, 126, 1, 0, 0, 0, 440, 441, 7, 31, 0, 0, 441, 128, 1, 0, 0, 0, + 442, 443, 7, 32, 0, 0, 443, 130, 1, 0, 0, 0, 444, 445, 7, 33, 0, 0, 445, + 132, 1, 0, 0, 0, 446, 447, 7, 34, 0, 0, 447, 134, 1, 0, 0, 0, 448, 449, + 7, 35, 0, 0, 449, 136, 1, 0, 0, 0, 23, 0, 144, 302, 304, 312, 314, 322, + 330, 333, 338, 344, 347, 351, 356, 358, 364, 368, 373, 375, 377, 383, 385, + 392, 2, 0, 1, 0, 6, 0, 0, + } + deserializer := antlr.NewATNDeserializer(nil) + staticData.atn = deserializer.Deserialize(staticData.serializedATN) + atn := staticData.atn + staticData.decisionToDFA = make([]*antlr.DFA, len(atn.DecisionToState)) + decisionToDFA := staticData.decisionToDFA + for index, state := range atn.DecisionToState { + decisionToDFA[index] = antlr.NewDFA(state, index) + } +} + +// SqlLexerInit initializes any static state used to implement SqlLexer. By default the +// static state used to implement the lexer is lazily initialized during the first call to +// NewSqlLexer(). You can call this function if you wish to initialize the static state ahead +// of time. +func SqlLexerInit() { + staticData := &sqllexerLexerStaticData + staticData.once.Do(sqllexerLexerInit) +} + +// NewSqlLexer produces a new lexer instance for the optional input antlr.CharStream. +func NewSqlLexer(input antlr.CharStream) *SqlLexer { + SqlLexerInit() + l := new(SqlLexer) + l.BaseLexer = antlr.NewBaseLexer(input) + staticData := &sqllexerLexerStaticData + l.Interpreter = antlr.NewLexerATNSimulator(l, staticData.atn, staticData.decisionToDFA, staticData.predictionContextCache) + l.channelNames = staticData.channelNames + l.modeNames = staticData.modeNames + l.RuleNames = staticData.ruleNames + l.LiteralNames = staticData.literalNames + l.SymbolicNames = staticData.symbolicNames + l.GrammarFileName = "Sql.g4" + // TODO: l.EOF = antlr.TokenEOF + + return l +} + +// SqlLexer tokens. +const ( + SqlLexerSPACE = 1 + SqlLexerWS = 2 + SqlLexerCOMMA = 3 + SqlLexerL_BRACKET = 4 + SqlLexerR_BRACKET = 5 + SqlLexerDOT = 6 + SqlLexerEOQ = 7 + SqlLexerK_SELECT = 8 + SqlLexerK_WHERE = 9 + SqlLexerK_WINDOW_TUMBLING = 10 + SqlLexerK_GROUP_BY = 11 + SqlLexerK_AND = 12 + SqlLexerK_AS = 13 + SqlLexerK_OR = 14 + SqlLexerK_IS = 15 + SqlLexerK_LIKE = 16 + SqlLexerK_NOT_LIKE = 17 + SqlLexerK_EQUAL = 18 + SqlLexerK_GREATER = 19 + SqlLexerK_LESS = 20 + SqlLexerK_LESS_EQUAL = 21 + SqlLexerK_GREATER_EQUAL = 22 + SqlLexerK_NOT_EQUAL = 23 + SqlLexerK_NULL = 24 + SqlLexerK_IS_NULL = 25 + SqlLexerK_IS_NOT_NULL = 26 + SqlLexerK_NOT = 27 + SqlLexerK_NOT_IN = 28 + SqlLexerK_IN = 29 + SqlLexerK_COUNT = 30 + SqlLexerK_SUM = 31 + SqlLexerK_MIN = 32 + SqlLexerK_MAX = 33 + SqlLexerK_AVG = 34 + SqlLexerK_TRUE = 35 + SqlLexerK_FALSE = 36 + SqlLexerIDENTIFIER = 37 + SqlLexerNUMERIC_LITERAL = 38 + SqlLexerSTRING_LITERAL = 39 + SqlLexerBOOLEAN_LITERAL = 40 + SqlLexerSTAR = 41 +) diff --git a/processor/stream_processor/parser/sql_lexer_test.go b/processor/stream_processor/parser/sql_lexer_test.go new file mode 100644 index 000000000000..ee78b3b57842 --- /dev/null +++ b/processor/stream_processor/parser/sql_lexer_test.go @@ -0,0 +1,53 @@ +package parser + +import ( + "fmt" + "github.com/antlr/antlr4/runtime/Go/antlr" + "go.opentelemetry.io/collector/pdata/pcommon" + "go.opentelemetry.io/collector/pdata/plog" + "testing" +) + +func TestSelectExpr(t *testing.T) { + is := antlr.NewInputStream(`SELECT field1, min(field2) WHERE field3 <= 3 and field4 is not null `) + lexer := NewSqlLexer(is) + + for { + t := lexer.NextToken() + if t.GetTokenType() == antlr.TokenEOF { + break + } + fmt.Printf("%s (%q)\n", + lexer.SymbolicNames[t.GetTokenType()], t.GetText()) + } + +} + +/* +func TestParser(t *testing.T) { + is := antlr.NewInputStream("SELEct field1, field2 WHERE field3 > 3 and field4 IS 'test';") + + lexer := NewSqlLexer(is) + stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel) + + // Create the Parser + p := NewSqlParser(stream) + in := make(chan plog.LogRecord) + out := make(chan plog.LogRecord) + ctx := context.TODO() + + listener := NewStreamListener(in, out, ctx) + + antlr.ParseTreeWalkerDefault.Walk(&listener, p.SelectQuery()) + +} +*/ + +func createSampleLog() plog.LogRecord { + record := plog.NewLogRecord() + record.Attributes().Insert("name", pcommon.NewValueString("kwak")) + record.Attributes().Insert("source", pcommon.NewValueString("some")) + record.Attributes().InsertBool("alive", true) + return record + +} diff --git a/processor/stream_processor/parser/sql_parser.go b/processor/stream_processor/parser/sql_parser.go new file mode 100644 index 000000000000..df4d209cb070 --- /dev/null +++ b/processor/stream_processor/parser/sql_parser.go @@ -0,0 +1,4073 @@ +// Code generated from Sql.g4 by ANTLR 4.10.1. DO NOT EDIT. + +package parser // Sql + +import ( + "fmt" + "strconv" + "sync" + + "github.com/antlr/antlr4/runtime/Go/antlr" +) + +// Suppress unused import errors +var _ = fmt.Printf +var _ = strconv.Itoa +var _ = sync.Once{} + +type SqlParser struct { + *antlr.BaseParser +} + +var sqlParserStaticData struct { + once sync.Once + serializedATN []int32 + literalNames []string + symbolicNames []string + ruleNames []string + predictionContextCache *antlr.PredictionContextCache + atn *antlr.ATN + decisionToDFA []*antlr.DFA +} + +func sqlParserInit() { + staticData := &sqlParserStaticData + staticData.literalNames = []string{ + "", "", "", "','", "'('", "')'", "'.'", "';'", "", "", "", "", "", "", + "", "", "", "", "'='", "'>'", "'<'", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", "", "", "'*'", + } + staticData.symbolicNames = []string{ + "", "SPACE", "WS", "COMMA", "L_BRACKET", "R_BRACKET", "DOT", "EOQ", + "K_SELECT", "K_WHERE", "K_WINDOW_TUMBLING", "K_GROUP_BY", "K_AND", "K_AS", + "K_OR", "K_IS", "K_LIKE", "K_NOT_LIKE", "K_EQUAL", "K_GREATER", "K_LESS", + "K_LESS_EQUAL", "K_GREATER_EQUAL", "K_NOT_EQUAL", "K_NULL", "K_IS_NULL", + "K_IS_NOT_NULL", "K_NOT", "K_NOT_IN", "K_IN", "K_COUNT", "K_SUM", "K_MIN", + "K_MAX", "K_AVG", "K_TRUE", "K_FALSE", "IDENTIFIER", "NUMERIC_LITERAL", + "STRING_LITERAL", "BOOLEAN_LITERAL", "STAR", + } + staticData.ruleNames = []string{ + "sqlQuery", "selectQuery", "windowTumbling", "resultColumns", "aggregationColumns", + "groupByAggregationColumns", "aggregationColumn", "column", "alias", + "function", "functionName", "whereStatement", "expr", "simpleExpr", + "compoundExpr", "comparisonOperator", "literalValue", "groupBy", + } + staticData.predictionContextCache = antlr.NewPredictionContextCache() + staticData.serializedATN = []int32{ + 4, 1, 41, 230, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, + 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2, 10, 7, + 10, 2, 11, 7, 11, 2, 12, 7, 12, 2, 13, 7, 13, 2, 14, 7, 14, 2, 15, 7, 15, + 2, 16, 7, 16, 2, 17, 7, 17, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 3, 1, 43, + 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 51, 8, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 3, 1, 59, 8, 1, 1, 1, 1, 1, 1, 1, 3, 1, 64, 8, 1, + 1, 2, 1, 2, 1, 2, 1, 3, 1, 3, 1, 3, 5, 3, 72, 8, 3, 10, 3, 12, 3, 75, 9, + 3, 1, 3, 3, 3, 78, 8, 3, 1, 4, 1, 4, 1, 4, 5, 4, 83, 8, 4, 10, 4, 12, 4, + 86, 9, 4, 1, 5, 1, 5, 3, 5, 90, 8, 5, 1, 5, 1, 5, 1, 5, 3, 5, 95, 8, 5, + 5, 5, 97, 8, 5, 10, 5, 12, 5, 100, 9, 5, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, + 1, 6, 3, 6, 108, 8, 6, 1, 6, 1, 6, 3, 6, 112, 8, 6, 1, 7, 1, 7, 1, 7, 1, + 7, 3, 7, 118, 8, 7, 1, 7, 3, 7, 121, 8, 7, 1, 7, 1, 7, 3, 7, 125, 8, 7, + 3, 7, 127, 8, 7, 1, 8, 1, 8, 1, 8, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, + 3, 9, 138, 8, 9, 1, 9, 1, 9, 5, 9, 142, 8, 9, 10, 9, 12, 9, 145, 9, 9, + 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 3, 9, 155, 8, 9, 1, 9, + 1, 9, 5, 9, 159, 8, 9, 10, 9, 12, 9, 162, 9, 9, 1, 9, 1, 9, 1, 9, 1, 9, + 1, 9, 1, 9, 1, 9, 5, 9, 171, 8, 9, 10, 9, 12, 9, 174, 9, 9, 1, 9, 1, 9, + 3, 9, 178, 8, 9, 1, 10, 1, 10, 1, 11, 1, 11, 1, 11, 1, 12, 1, 12, 1, 12, + 1, 12, 3, 12, 189, 8, 12, 1, 12, 1, 12, 1, 12, 3, 12, 194, 8, 12, 1, 12, + 3, 12, 197, 8, 12, 1, 12, 1, 12, 1, 12, 5, 12, 202, 8, 12, 10, 12, 12, + 12, 205, 9, 12, 1, 13, 1, 13, 1, 13, 1, 13, 1, 13, 1, 13, 1, 13, 1, 13, + 1, 13, 1, 13, 3, 13, 217, 8, 13, 1, 14, 1, 14, 1, 14, 1, 14, 1, 15, 1, + 15, 1, 16, 1, 16, 1, 17, 1, 17, 1, 17, 1, 17, 0, 1, 24, 18, 0, 2, 4, 6, + 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 0, 4, 1, 0, 30, + 34, 2, 0, 12, 12, 14, 14, 2, 0, 15, 23, 28, 29, 1, 0, 38, 40, 241, 0, 36, + 1, 0, 0, 0, 2, 63, 1, 0, 0, 0, 4, 65, 1, 0, 0, 0, 6, 77, 1, 0, 0, 0, 8, + 79, 1, 0, 0, 0, 10, 89, 1, 0, 0, 0, 12, 101, 1, 0, 0, 0, 14, 126, 1, 0, + 0, 0, 16, 128, 1, 0, 0, 0, 18, 177, 1, 0, 0, 0, 20, 179, 1, 0, 0, 0, 22, + 181, 1, 0, 0, 0, 24, 196, 1, 0, 0, 0, 26, 216, 1, 0, 0, 0, 28, 218, 1, + 0, 0, 0, 30, 222, 1, 0, 0, 0, 32, 224, 1, 0, 0, 0, 34, 226, 1, 0, 0, 0, + 36, 37, 3, 2, 1, 0, 37, 38, 5, 0, 0, 1, 38, 1, 1, 0, 0, 0, 39, 40, 5, 8, + 0, 0, 40, 42, 3, 6, 3, 0, 41, 43, 3, 22, 11, 0, 42, 41, 1, 0, 0, 0, 42, + 43, 1, 0, 0, 0, 43, 44, 1, 0, 0, 0, 44, 45, 5, 7, 0, 0, 45, 64, 1, 0, 0, + 0, 46, 47, 5, 8, 0, 0, 47, 48, 3, 8, 4, 0, 48, 50, 3, 4, 2, 0, 49, 51, + 3, 22, 11, 0, 50, 49, 1, 0, 0, 0, 50, 51, 1, 0, 0, 0, 51, 52, 1, 0, 0, + 0, 52, 53, 5, 7, 0, 0, 53, 64, 1, 0, 0, 0, 54, 55, 5, 8, 0, 0, 55, 56, + 3, 10, 5, 0, 56, 58, 3, 4, 2, 0, 57, 59, 3, 22, 11, 0, 58, 57, 1, 0, 0, + 0, 58, 59, 1, 0, 0, 0, 59, 60, 1, 0, 0, 0, 60, 61, 3, 34, 17, 0, 61, 62, + 5, 7, 0, 0, 62, 64, 1, 0, 0, 0, 63, 39, 1, 0, 0, 0, 63, 46, 1, 0, 0, 0, + 63, 54, 1, 0, 0, 0, 64, 3, 1, 0, 0, 0, 65, 66, 5, 10, 0, 0, 66, 67, 5, + 38, 0, 0, 67, 5, 1, 0, 0, 0, 68, 73, 3, 14, 7, 0, 69, 70, 5, 3, 0, 0, 70, + 72, 3, 14, 7, 0, 71, 69, 1, 0, 0, 0, 72, 75, 1, 0, 0, 0, 73, 71, 1, 0, + 0, 0, 73, 74, 1, 0, 0, 0, 74, 78, 1, 0, 0, 0, 75, 73, 1, 0, 0, 0, 76, 78, + 5, 41, 0, 0, 77, 68, 1, 0, 0, 0, 77, 76, 1, 0, 0, 0, 78, 7, 1, 0, 0, 0, + 79, 84, 3, 12, 6, 0, 80, 81, 5, 3, 0, 0, 81, 83, 3, 12, 6, 0, 82, 80, 1, + 0, 0, 0, 83, 86, 1, 0, 0, 0, 84, 82, 1, 0, 0, 0, 84, 85, 1, 0, 0, 0, 85, + 9, 1, 0, 0, 0, 86, 84, 1, 0, 0, 0, 87, 90, 3, 14, 7, 0, 88, 90, 3, 12, + 6, 0, 89, 87, 1, 0, 0, 0, 89, 88, 1, 0, 0, 0, 90, 98, 1, 0, 0, 0, 91, 94, + 5, 3, 0, 0, 92, 95, 3, 12, 6, 0, 93, 95, 3, 14, 7, 0, 94, 92, 1, 0, 0, + 0, 94, 93, 1, 0, 0, 0, 95, 97, 1, 0, 0, 0, 96, 91, 1, 0, 0, 0, 97, 100, + 1, 0, 0, 0, 98, 96, 1, 0, 0, 0, 98, 99, 1, 0, 0, 0, 99, 11, 1, 0, 0, 0, + 100, 98, 1, 0, 0, 0, 101, 102, 7, 0, 0, 0, 102, 107, 5, 4, 0, 0, 103, 108, + 5, 37, 0, 0, 104, 105, 5, 37, 0, 0, 105, 106, 5, 6, 0, 0, 106, 108, 5, + 37, 0, 0, 107, 103, 1, 0, 0, 0, 107, 104, 1, 0, 0, 0, 108, 109, 1, 0, 0, + 0, 109, 111, 5, 5, 0, 0, 110, 112, 3, 16, 8, 0, 111, 110, 1, 0, 0, 0, 111, + 112, 1, 0, 0, 0, 112, 13, 1, 0, 0, 0, 113, 118, 5, 37, 0, 0, 114, 115, + 5, 37, 0, 0, 115, 116, 5, 6, 0, 0, 116, 118, 5, 37, 0, 0, 117, 113, 1, + 0, 0, 0, 117, 114, 1, 0, 0, 0, 118, 120, 1, 0, 0, 0, 119, 121, 3, 16, 8, + 0, 120, 119, 1, 0, 0, 0, 120, 121, 1, 0, 0, 0, 121, 127, 1, 0, 0, 0, 122, + 124, 3, 18, 9, 0, 123, 125, 3, 16, 8, 0, 124, 123, 1, 0, 0, 0, 124, 125, + 1, 0, 0, 0, 125, 127, 1, 0, 0, 0, 126, 117, 1, 0, 0, 0, 126, 122, 1, 0, + 0, 0, 127, 15, 1, 0, 0, 0, 128, 129, 5, 13, 0, 0, 129, 130, 5, 37, 0, 0, + 130, 17, 1, 0, 0, 0, 131, 132, 3, 20, 10, 0, 132, 137, 5, 4, 0, 0, 133, + 138, 5, 37, 0, 0, 134, 135, 5, 37, 0, 0, 135, 136, 5, 6, 0, 0, 136, 138, + 5, 37, 0, 0, 137, 133, 1, 0, 0, 0, 137, 134, 1, 0, 0, 0, 138, 143, 1, 0, + 0, 0, 139, 140, 5, 3, 0, 0, 140, 142, 3, 32, 16, 0, 141, 139, 1, 0, 0, + 0, 142, 145, 1, 0, 0, 0, 143, 141, 1, 0, 0, 0, 143, 144, 1, 0, 0, 0, 144, + 146, 1, 0, 0, 0, 145, 143, 1, 0, 0, 0, 146, 147, 5, 5, 0, 0, 147, 178, + 1, 0, 0, 0, 148, 149, 3, 20, 10, 0, 149, 154, 5, 4, 0, 0, 150, 155, 5, + 37, 0, 0, 151, 152, 5, 37, 0, 0, 152, 153, 5, 6, 0, 0, 153, 155, 5, 37, + 0, 0, 154, 150, 1, 0, 0, 0, 154, 151, 1, 0, 0, 0, 155, 160, 1, 0, 0, 0, + 156, 157, 5, 3, 0, 0, 157, 159, 3, 32, 16, 0, 158, 156, 1, 0, 0, 0, 159, + 162, 1, 0, 0, 0, 160, 158, 1, 0, 0, 0, 160, 161, 1, 0, 0, 0, 161, 163, + 1, 0, 0, 0, 162, 160, 1, 0, 0, 0, 163, 164, 5, 5, 0, 0, 164, 178, 1, 0, + 0, 0, 165, 166, 3, 20, 10, 0, 166, 167, 5, 4, 0, 0, 167, 172, 3, 18, 9, + 0, 168, 169, 5, 3, 0, 0, 169, 171, 3, 32, 16, 0, 170, 168, 1, 0, 0, 0, + 171, 174, 1, 0, 0, 0, 172, 170, 1, 0, 0, 0, 172, 173, 1, 0, 0, 0, 173, + 175, 1, 0, 0, 0, 174, 172, 1, 0, 0, 0, 175, 176, 5, 5, 0, 0, 176, 178, + 1, 0, 0, 0, 177, 131, 1, 0, 0, 0, 177, 148, 1, 0, 0, 0, 177, 165, 1, 0, + 0, 0, 178, 19, 1, 0, 0, 0, 179, 180, 5, 37, 0, 0, 180, 21, 1, 0, 0, 0, + 181, 182, 5, 9, 0, 0, 182, 183, 3, 24, 12, 0, 183, 23, 1, 0, 0, 0, 184, + 185, 6, 12, -1, 0, 185, 197, 3, 26, 13, 0, 186, 189, 3, 28, 14, 0, 187, + 189, 3, 26, 13, 0, 188, 186, 1, 0, 0, 0, 188, 187, 1, 0, 0, 0, 189, 190, + 1, 0, 0, 0, 190, 193, 7, 1, 0, 0, 191, 194, 3, 28, 14, 0, 192, 194, 3, + 26, 13, 0, 193, 191, 1, 0, 0, 0, 193, 192, 1, 0, 0, 0, 194, 197, 1, 0, + 0, 0, 195, 197, 3, 28, 14, 0, 196, 184, 1, 0, 0, 0, 196, 188, 1, 0, 0, + 0, 196, 195, 1, 0, 0, 0, 197, 203, 1, 0, 0, 0, 198, 199, 10, 2, 0, 0, 199, + 200, 7, 1, 0, 0, 200, 202, 3, 24, 12, 3, 201, 198, 1, 0, 0, 0, 202, 205, + 1, 0, 0, 0, 203, 201, 1, 0, 0, 0, 203, 204, 1, 0, 0, 0, 204, 25, 1, 0, + 0, 0, 205, 203, 1, 0, 0, 0, 206, 207, 5, 37, 0, 0, 207, 208, 3, 30, 15, + 0, 208, 209, 3, 32, 16, 0, 209, 217, 1, 0, 0, 0, 210, 211, 5, 37, 0, 0, + 211, 212, 5, 6, 0, 0, 212, 213, 5, 37, 0, 0, 213, 214, 3, 30, 15, 0, 214, + 215, 3, 32, 16, 0, 215, 217, 1, 0, 0, 0, 216, 206, 1, 0, 0, 0, 216, 210, + 1, 0, 0, 0, 217, 27, 1, 0, 0, 0, 218, 219, 5, 4, 0, 0, 219, 220, 3, 24, + 12, 0, 220, 221, 5, 5, 0, 0, 221, 29, 1, 0, 0, 0, 222, 223, 7, 2, 0, 0, + 223, 31, 1, 0, 0, 0, 224, 225, 7, 3, 0, 0, 225, 33, 1, 0, 0, 0, 226, 227, + 5, 11, 0, 0, 227, 228, 3, 14, 7, 0, 228, 35, 1, 0, 0, 0, 27, 42, 50, 58, + 63, 73, 77, 84, 89, 94, 98, 107, 111, 117, 120, 124, 126, 137, 143, 154, + 160, 172, 177, 188, 193, 196, 203, 216, + } + deserializer := antlr.NewATNDeserializer(nil) + staticData.atn = deserializer.Deserialize(staticData.serializedATN) + atn := staticData.atn + staticData.decisionToDFA = make([]*antlr.DFA, len(atn.DecisionToState)) + decisionToDFA := staticData.decisionToDFA + for index, state := range atn.DecisionToState { + decisionToDFA[index] = antlr.NewDFA(state, index) + } +} + +// SqlParserInit initializes any static state used to implement SqlParser. By default the +// static state used to implement the parser is lazily initialized during the first call to +// NewSqlParser(). You can call this function if you wish to initialize the static state ahead +// of time. +func SqlParserInit() { + staticData := &sqlParserStaticData + staticData.once.Do(sqlParserInit) +} + +// NewSqlParser produces a new parser instance for the optional input antlr.TokenStream. +func NewSqlParser(input antlr.TokenStream) *SqlParser { + SqlParserInit() + this := new(SqlParser) + this.BaseParser = antlr.NewBaseParser(input) + staticData := &sqlParserStaticData + this.Interpreter = antlr.NewParserATNSimulator(this, staticData.atn, staticData.decisionToDFA, staticData.predictionContextCache) + this.RuleNames = staticData.ruleNames + this.LiteralNames = staticData.literalNames + this.SymbolicNames = staticData.symbolicNames + this.GrammarFileName = "Sql.g4" + + return this +} + +// SqlParser tokens. +const ( + SqlParserEOF = antlr.TokenEOF + SqlParserSPACE = 1 + SqlParserWS = 2 + SqlParserCOMMA = 3 + SqlParserL_BRACKET = 4 + SqlParserR_BRACKET = 5 + SqlParserDOT = 6 + SqlParserEOQ = 7 + SqlParserK_SELECT = 8 + SqlParserK_WHERE = 9 + SqlParserK_WINDOW_TUMBLING = 10 + SqlParserK_GROUP_BY = 11 + SqlParserK_AND = 12 + SqlParserK_AS = 13 + SqlParserK_OR = 14 + SqlParserK_IS = 15 + SqlParserK_LIKE = 16 + SqlParserK_NOT_LIKE = 17 + SqlParserK_EQUAL = 18 + SqlParserK_GREATER = 19 + SqlParserK_LESS = 20 + SqlParserK_LESS_EQUAL = 21 + SqlParserK_GREATER_EQUAL = 22 + SqlParserK_NOT_EQUAL = 23 + SqlParserK_NULL = 24 + SqlParserK_IS_NULL = 25 + SqlParserK_IS_NOT_NULL = 26 + SqlParserK_NOT = 27 + SqlParserK_NOT_IN = 28 + SqlParserK_IN = 29 + SqlParserK_COUNT = 30 + SqlParserK_SUM = 31 + SqlParserK_MIN = 32 + SqlParserK_MAX = 33 + SqlParserK_AVG = 34 + SqlParserK_TRUE = 35 + SqlParserK_FALSE = 36 + SqlParserIDENTIFIER = 37 + SqlParserNUMERIC_LITERAL = 38 + SqlParserSTRING_LITERAL = 39 + SqlParserBOOLEAN_LITERAL = 40 + SqlParserSTAR = 41 +) + +// SqlParser rules. +const ( + SqlParserRULE_sqlQuery = 0 + SqlParserRULE_selectQuery = 1 + SqlParserRULE_windowTumbling = 2 + SqlParserRULE_resultColumns = 3 + SqlParserRULE_aggregationColumns = 4 + SqlParserRULE_groupByAggregationColumns = 5 + SqlParserRULE_aggregationColumn = 6 + SqlParserRULE_column = 7 + SqlParserRULE_alias = 8 + SqlParserRULE_function = 9 + SqlParserRULE_functionName = 10 + SqlParserRULE_whereStatement = 11 + SqlParserRULE_expr = 12 + SqlParserRULE_simpleExpr = 13 + SqlParserRULE_compoundExpr = 14 + SqlParserRULE_comparisonOperator = 15 + SqlParserRULE_literalValue = 16 + SqlParserRULE_groupBy = 17 +) + +// ISqlQueryContext is an interface to support dynamic dispatch. +type ISqlQueryContext interface { + antlr.ParserRuleContext + + // GetParser returns the parser. + GetParser() antlr.Parser + + // IsSqlQueryContext differentiates from other interfaces. + IsSqlQueryContext() +} + +type SqlQueryContext struct { + *antlr.BaseParserRuleContext + parser antlr.Parser +} + +func NewEmptySqlQueryContext() *SqlQueryContext { + var p = new(SqlQueryContext) + p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(nil, -1) + p.RuleIndex = SqlParserRULE_sqlQuery + return p +} + +func (*SqlQueryContext) IsSqlQueryContext() {} + +func NewSqlQueryContext(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int) *SqlQueryContext { + var p = new(SqlQueryContext) + + p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(parent, invokingState) + + p.parser = parser + p.RuleIndex = SqlParserRULE_sqlQuery + + return p +} + +func (s *SqlQueryContext) GetParser() antlr.Parser { return s.parser } + +func (s *SqlQueryContext) SelectQuery() ISelectQueryContext { + var t antlr.RuleContext + for _, ctx := range s.GetChildren() { + if _, ok := ctx.(ISelectQueryContext); ok { + t = ctx.(antlr.RuleContext) + break + } + } + + if t == nil { + return nil + } + + return t.(ISelectQueryContext) +} + +func (s *SqlQueryContext) EOF() antlr.TerminalNode { + return s.GetToken(SqlParserEOF, 0) +} + +func (s *SqlQueryContext) GetRuleContext() antlr.RuleContext { + return s +} + +func (s *SqlQueryContext) ToStringTree(ruleNames []string, recog antlr.Recognizer) string { + return antlr.TreesStringTree(s, ruleNames, recog) +} + +func (s *SqlQueryContext) Accept(visitor antlr.ParseTreeVisitor) interface{} { + switch t := visitor.(type) { + case SqlVisitor: + return t.VisitSqlQuery(s) + + default: + return t.VisitChildren(s) + } +} + +func (p *SqlParser) SqlQuery() (localctx ISqlQueryContext) { + this := p + _ = this + + localctx = NewSqlQueryContext(p, p.GetParserRuleContext(), p.GetState()) + p.EnterRule(localctx, 0, SqlParserRULE_sqlQuery) + + defer func() { + p.ExitRule() + }() + + defer func() { + if err := recover(); err != nil { + if v, ok := err.(antlr.RecognitionException); ok { + localctx.SetException(v) + p.GetErrorHandler().ReportError(p, v) + p.GetErrorHandler().Recover(p, v) + } else { + panic(err) + } + } + }() + + p.EnterOuterAlt(localctx, 1) + { + p.SetState(36) + p.SelectQuery() + } + { + p.SetState(37) + p.Match(SqlParserEOF) + } + + return localctx +} + +// ISelectQueryContext is an interface to support dynamic dispatch. +type ISelectQueryContext interface { + antlr.ParserRuleContext + + // GetParser returns the parser. + GetParser() antlr.Parser + + // IsSelectQueryContext differentiates from other interfaces. + IsSelectQueryContext() +} + +type SelectQueryContext struct { + *antlr.BaseParserRuleContext + parser antlr.Parser +} + +func NewEmptySelectQueryContext() *SelectQueryContext { + var p = new(SelectQueryContext) + p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(nil, -1) + p.RuleIndex = SqlParserRULE_selectQuery + return p +} + +func (*SelectQueryContext) IsSelectQueryContext() {} + +func NewSelectQueryContext(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int) *SelectQueryContext { + var p = new(SelectQueryContext) + + p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(parent, invokingState) + + p.parser = parser + p.RuleIndex = SqlParserRULE_selectQuery + + return p +} + +func (s *SelectQueryContext) GetParser() antlr.Parser { return s.parser } + +func (s *SelectQueryContext) CopyFrom(ctx *SelectQueryContext) { + s.BaseParserRuleContext.CopyFrom(ctx.BaseParserRuleContext) +} + +func (s *SelectQueryContext) GetRuleContext() antlr.RuleContext { + return s +} + +func (s *SelectQueryContext) ToStringTree(ruleNames []string, recog antlr.Recognizer) string { + return antlr.TreesStringTree(s, ruleNames, recog) +} + +type SelectTumblingContext struct { + *SelectQueryContext +} + +func NewSelectTumblingContext(parser antlr.Parser, ctx antlr.ParserRuleContext) *SelectTumblingContext { + var p = new(SelectTumblingContext) + + p.SelectQueryContext = NewEmptySelectQueryContext() + p.parser = parser + p.CopyFrom(ctx.(*SelectQueryContext)) + + return p +} + +func (s *SelectTumblingContext) GetRuleContext() antlr.RuleContext { + return s +} + +func (s *SelectTumblingContext) K_SELECT() antlr.TerminalNode { + return s.GetToken(SqlParserK_SELECT, 0) +} + +func (s *SelectTumblingContext) AggregationColumns() IAggregationColumnsContext { + var t antlr.RuleContext + for _, ctx := range s.GetChildren() { + if _, ok := ctx.(IAggregationColumnsContext); ok { + t = ctx.(antlr.RuleContext) + break + } + } + + if t == nil { + return nil + } + + return t.(IAggregationColumnsContext) +} + +func (s *SelectTumblingContext) WindowTumbling() IWindowTumblingContext { + var t antlr.RuleContext + for _, ctx := range s.GetChildren() { + if _, ok := ctx.(IWindowTumblingContext); ok { + t = ctx.(antlr.RuleContext) + break + } + } + + if t == nil { + return nil + } + + return t.(IWindowTumblingContext) +} + +func (s *SelectTumblingContext) EOQ() antlr.TerminalNode { + return s.GetToken(SqlParserEOQ, 0) +} + +func (s *SelectTumblingContext) WhereStatement() IWhereStatementContext { + var t antlr.RuleContext + for _, ctx := range s.GetChildren() { + if _, ok := ctx.(IWhereStatementContext); ok { + t = ctx.(antlr.RuleContext) + break + } + } + + if t == nil { + return nil + } + + return t.(IWhereStatementContext) +} + +func (s *SelectTumblingContext) Accept(visitor antlr.ParseTreeVisitor) interface{} { + switch t := visitor.(type) { + case SqlVisitor: + return t.VisitSelectTumbling(s) + + default: + return t.VisitChildren(s) + } +} + +type SelectSimpleContext struct { + *SelectQueryContext +} + +func NewSelectSimpleContext(parser antlr.Parser, ctx antlr.ParserRuleContext) *SelectSimpleContext { + var p = new(SelectSimpleContext) + + p.SelectQueryContext = NewEmptySelectQueryContext() + p.parser = parser + p.CopyFrom(ctx.(*SelectQueryContext)) + + return p +} + +func (s *SelectSimpleContext) GetRuleContext() antlr.RuleContext { + return s +} + +func (s *SelectSimpleContext) K_SELECT() antlr.TerminalNode { + return s.GetToken(SqlParserK_SELECT, 0) +} + +func (s *SelectSimpleContext) ResultColumns() IResultColumnsContext { + var t antlr.RuleContext + for _, ctx := range s.GetChildren() { + if _, ok := ctx.(IResultColumnsContext); ok { + t = ctx.(antlr.RuleContext) + break + } + } + + if t == nil { + return nil + } + + return t.(IResultColumnsContext) +} + +func (s *SelectSimpleContext) EOQ() antlr.TerminalNode { + return s.GetToken(SqlParserEOQ, 0) +} + +func (s *SelectSimpleContext) WhereStatement() IWhereStatementContext { + var t antlr.RuleContext + for _, ctx := range s.GetChildren() { + if _, ok := ctx.(IWhereStatementContext); ok { + t = ctx.(antlr.RuleContext) + break + } + } + + if t == nil { + return nil + } + + return t.(IWhereStatementContext) +} + +func (s *SelectSimpleContext) Accept(visitor antlr.ParseTreeVisitor) interface{} { + switch t := visitor.(type) { + case SqlVisitor: + return t.VisitSelectSimple(s) + + default: + return t.VisitChildren(s) + } +} + +type SelectTumblingGroupByContext struct { + *SelectQueryContext +} + +func NewSelectTumblingGroupByContext(parser antlr.Parser, ctx antlr.ParserRuleContext) *SelectTumblingGroupByContext { + var p = new(SelectTumblingGroupByContext) + + p.SelectQueryContext = NewEmptySelectQueryContext() + p.parser = parser + p.CopyFrom(ctx.(*SelectQueryContext)) + + return p +} + +func (s *SelectTumblingGroupByContext) GetRuleContext() antlr.RuleContext { + return s +} + +func (s *SelectTumblingGroupByContext) K_SELECT() antlr.TerminalNode { + return s.GetToken(SqlParserK_SELECT, 0) +} + +func (s *SelectTumblingGroupByContext) GroupByAggregationColumns() IGroupByAggregationColumnsContext { + var t antlr.RuleContext + for _, ctx := range s.GetChildren() { + if _, ok := ctx.(IGroupByAggregationColumnsContext); ok { + t = ctx.(antlr.RuleContext) + break + } + } + + if t == nil { + return nil + } + + return t.(IGroupByAggregationColumnsContext) +} + +func (s *SelectTumblingGroupByContext) WindowTumbling() IWindowTumblingContext { + var t antlr.RuleContext + for _, ctx := range s.GetChildren() { + if _, ok := ctx.(IWindowTumblingContext); ok { + t = ctx.(antlr.RuleContext) + break + } + } + + if t == nil { + return nil + } + + return t.(IWindowTumblingContext) +} + +func (s *SelectTumblingGroupByContext) GroupBy() IGroupByContext { + var t antlr.RuleContext + for _, ctx := range s.GetChildren() { + if _, ok := ctx.(IGroupByContext); ok { + t = ctx.(antlr.RuleContext) + break + } + } + + if t == nil { + return nil + } + + return t.(IGroupByContext) +} + +func (s *SelectTumblingGroupByContext) EOQ() antlr.TerminalNode { + return s.GetToken(SqlParserEOQ, 0) +} + +func (s *SelectTumblingGroupByContext) WhereStatement() IWhereStatementContext { + var t antlr.RuleContext + for _, ctx := range s.GetChildren() { + if _, ok := ctx.(IWhereStatementContext); ok { + t = ctx.(antlr.RuleContext) + break + } + } + + if t == nil { + return nil + } + + return t.(IWhereStatementContext) +} + +func (s *SelectTumblingGroupByContext) Accept(visitor antlr.ParseTreeVisitor) interface{} { + switch t := visitor.(type) { + case SqlVisitor: + return t.VisitSelectTumblingGroupBy(s) + + default: + return t.VisitChildren(s) + } +} + +func (p *SqlParser) SelectQuery() (localctx ISelectQueryContext) { + this := p + _ = this + + localctx = NewSelectQueryContext(p, p.GetParserRuleContext(), p.GetState()) + p.EnterRule(localctx, 2, SqlParserRULE_selectQuery) + var _la int + + defer func() { + p.ExitRule() + }() + + defer func() { + if err := recover(); err != nil { + if v, ok := err.(antlr.RecognitionException); ok { + localctx.SetException(v) + p.GetErrorHandler().ReportError(p, v) + p.GetErrorHandler().Recover(p, v) + } else { + panic(err) + } + } + }() + + p.SetState(63) + p.GetErrorHandler().Sync(p) + switch p.GetInterpreter().AdaptivePredict(p.GetTokenStream(), 3, p.GetParserRuleContext()) { + case 1: + localctx = NewSelectSimpleContext(p, localctx) + p.EnterOuterAlt(localctx, 1) + { + p.SetState(39) + p.Match(SqlParserK_SELECT) + } + { + p.SetState(40) + p.ResultColumns() + } + p.SetState(42) + p.GetErrorHandler().Sync(p) + _la = p.GetTokenStream().LA(1) + + if _la == SqlParserK_WHERE { + { + p.SetState(41) + p.WhereStatement() + } + + } + { + p.SetState(44) + p.Match(SqlParserEOQ) + } + + case 2: + localctx = NewSelectTumblingContext(p, localctx) + p.EnterOuterAlt(localctx, 2) + { + p.SetState(46) + p.Match(SqlParserK_SELECT) + } + { + p.SetState(47) + p.AggregationColumns() + } + { + p.SetState(48) + p.WindowTumbling() + } + p.SetState(50) + p.GetErrorHandler().Sync(p) + _la = p.GetTokenStream().LA(1) + + if _la == SqlParserK_WHERE { + { + p.SetState(49) + p.WhereStatement() + } + + } + { + p.SetState(52) + p.Match(SqlParserEOQ) + } + + case 3: + localctx = NewSelectTumblingGroupByContext(p, localctx) + p.EnterOuterAlt(localctx, 3) + { + p.SetState(54) + p.Match(SqlParserK_SELECT) + } + { + p.SetState(55) + p.GroupByAggregationColumns() + } + { + p.SetState(56) + p.WindowTumbling() + } + p.SetState(58) + p.GetErrorHandler().Sync(p) + _la = p.GetTokenStream().LA(1) + + if _la == SqlParserK_WHERE { + { + p.SetState(57) + p.WhereStatement() + } + + } + { + p.SetState(60) + p.GroupBy() + } + { + p.SetState(61) + p.Match(SqlParserEOQ) + } + + } + + return localctx +} + +// IWindowTumblingContext is an interface to support dynamic dispatch. +type IWindowTumblingContext interface { + antlr.ParserRuleContext + + // GetParser returns the parser. + GetParser() antlr.Parser + + // IsWindowTumblingContext differentiates from other interfaces. + IsWindowTumblingContext() +} + +type WindowTumblingContext struct { + *antlr.BaseParserRuleContext + parser antlr.Parser +} + +func NewEmptyWindowTumblingContext() *WindowTumblingContext { + var p = new(WindowTumblingContext) + p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(nil, -1) + p.RuleIndex = SqlParserRULE_windowTumbling + return p +} + +func (*WindowTumblingContext) IsWindowTumblingContext() {} + +func NewWindowTumblingContext(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int) *WindowTumblingContext { + var p = new(WindowTumblingContext) + + p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(parent, invokingState) + + p.parser = parser + p.RuleIndex = SqlParserRULE_windowTumbling + + return p +} + +func (s *WindowTumblingContext) GetParser() antlr.Parser { return s.parser } + +func (s *WindowTumblingContext) K_WINDOW_TUMBLING() antlr.TerminalNode { + return s.GetToken(SqlParserK_WINDOW_TUMBLING, 0) +} + +func (s *WindowTumblingContext) NUMERIC_LITERAL() antlr.TerminalNode { + return s.GetToken(SqlParserNUMERIC_LITERAL, 0) +} + +func (s *WindowTumblingContext) GetRuleContext() antlr.RuleContext { + return s +} + +func (s *WindowTumblingContext) ToStringTree(ruleNames []string, recog antlr.Recognizer) string { + return antlr.TreesStringTree(s, ruleNames, recog) +} + +func (s *WindowTumblingContext) Accept(visitor antlr.ParseTreeVisitor) interface{} { + switch t := visitor.(type) { + case SqlVisitor: + return t.VisitWindowTumbling(s) + + default: + return t.VisitChildren(s) + } +} + +func (p *SqlParser) WindowTumbling() (localctx IWindowTumblingContext) { + this := p + _ = this + + localctx = NewWindowTumblingContext(p, p.GetParserRuleContext(), p.GetState()) + p.EnterRule(localctx, 4, SqlParserRULE_windowTumbling) + + defer func() { + p.ExitRule() + }() + + defer func() { + if err := recover(); err != nil { + if v, ok := err.(antlr.RecognitionException); ok { + localctx.SetException(v) + p.GetErrorHandler().ReportError(p, v) + p.GetErrorHandler().Recover(p, v) + } else { + panic(err) + } + } + }() + + p.EnterOuterAlt(localctx, 1) + { + p.SetState(65) + p.Match(SqlParserK_WINDOW_TUMBLING) + } + { + p.SetState(66) + p.Match(SqlParserNUMERIC_LITERAL) + } + + return localctx +} + +// IResultColumnsContext is an interface to support dynamic dispatch. +type IResultColumnsContext interface { + antlr.ParserRuleContext + + // GetParser returns the parser. + GetParser() antlr.Parser + + // IsResultColumnsContext differentiates from other interfaces. + IsResultColumnsContext() +} + +type ResultColumnsContext struct { + *antlr.BaseParserRuleContext + parser antlr.Parser +} + +func NewEmptyResultColumnsContext() *ResultColumnsContext { + var p = new(ResultColumnsContext) + p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(nil, -1) + p.RuleIndex = SqlParserRULE_resultColumns + return p +} + +func (*ResultColumnsContext) IsResultColumnsContext() {} + +func NewResultColumnsContext(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int) *ResultColumnsContext { + var p = new(ResultColumnsContext) + + p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(parent, invokingState) + + p.parser = parser + p.RuleIndex = SqlParserRULE_resultColumns + + return p +} + +func (s *ResultColumnsContext) GetParser() antlr.Parser { return s.parser } + +func (s *ResultColumnsContext) CopyFrom(ctx *ResultColumnsContext) { + s.BaseParserRuleContext.CopyFrom(ctx.BaseParserRuleContext) +} + +func (s *ResultColumnsContext) GetRuleContext() antlr.RuleContext { + return s +} + +func (s *ResultColumnsContext) ToStringTree(ruleNames []string, recog antlr.Recognizer) string { + return antlr.TreesStringTree(s, ruleNames, recog) +} + +type SelectStarContext struct { + *ResultColumnsContext +} + +func NewSelectStarContext(parser antlr.Parser, ctx antlr.ParserRuleContext) *SelectStarContext { + var p = new(SelectStarContext) + + p.ResultColumnsContext = NewEmptyResultColumnsContext() + p.parser = parser + p.CopyFrom(ctx.(*ResultColumnsContext)) + + return p +} + +func (s *SelectStarContext) GetRuleContext() antlr.RuleContext { + return s +} + +func (s *SelectStarContext) STAR() antlr.TerminalNode { + return s.GetToken(SqlParserSTAR, 0) +} + +func (s *SelectStarContext) Accept(visitor antlr.ParseTreeVisitor) interface{} { + switch t := visitor.(type) { + case SqlVisitor: + return t.VisitSelectStar(s) + + default: + return t.VisitChildren(s) + } +} + +type SelectColumnsContext struct { + *ResultColumnsContext +} + +func NewSelectColumnsContext(parser antlr.Parser, ctx antlr.ParserRuleContext) *SelectColumnsContext { + var p = new(SelectColumnsContext) + + p.ResultColumnsContext = NewEmptyResultColumnsContext() + p.parser = parser + p.CopyFrom(ctx.(*ResultColumnsContext)) + + return p +} + +func (s *SelectColumnsContext) GetRuleContext() antlr.RuleContext { + return s +} + +func (s *SelectColumnsContext) AllColumn() []IColumnContext { + children := s.GetChildren() + len := 0 + for _, ctx := range children { + if _, ok := ctx.(IColumnContext); ok { + len++ + } + } + + tst := make([]IColumnContext, len) + i := 0 + for _, ctx := range children { + if t, ok := ctx.(IColumnContext); ok { + tst[i] = t.(IColumnContext) + i++ + } + } + + return tst +} + +func (s *SelectColumnsContext) Column(i int) IColumnContext { + var t antlr.RuleContext + j := 0 + for _, ctx := range s.GetChildren() { + if _, ok := ctx.(IColumnContext); ok { + if j == i { + t = ctx.(antlr.RuleContext) + break + } + j++ + } + } + + if t == nil { + return nil + } + + return t.(IColumnContext) +} + +func (s *SelectColumnsContext) AllCOMMA() []antlr.TerminalNode { + return s.GetTokens(SqlParserCOMMA) +} + +func (s *SelectColumnsContext) COMMA(i int) antlr.TerminalNode { + return s.GetToken(SqlParserCOMMA, i) +} + +func (s *SelectColumnsContext) Accept(visitor antlr.ParseTreeVisitor) interface{} { + switch t := visitor.(type) { + case SqlVisitor: + return t.VisitSelectColumns(s) + + default: + return t.VisitChildren(s) + } +} + +func (p *SqlParser) ResultColumns() (localctx IResultColumnsContext) { + this := p + _ = this + + localctx = NewResultColumnsContext(p, p.GetParserRuleContext(), p.GetState()) + p.EnterRule(localctx, 6, SqlParserRULE_resultColumns) + var _la int + + defer func() { + p.ExitRule() + }() + + defer func() { + if err := recover(); err != nil { + if v, ok := err.(antlr.RecognitionException); ok { + localctx.SetException(v) + p.GetErrorHandler().ReportError(p, v) + p.GetErrorHandler().Recover(p, v) + } else { + panic(err) + } + } + }() + + p.SetState(77) + p.GetErrorHandler().Sync(p) + + switch p.GetTokenStream().LA(1) { + case SqlParserIDENTIFIER: + localctx = NewSelectColumnsContext(p, localctx) + p.EnterOuterAlt(localctx, 1) + { + p.SetState(68) + p.Column() + } + p.SetState(73) + p.GetErrorHandler().Sync(p) + _la = p.GetTokenStream().LA(1) + + for _la == SqlParserCOMMA { + { + p.SetState(69) + p.Match(SqlParserCOMMA) + } + { + p.SetState(70) + p.Column() + } + + p.SetState(75) + p.GetErrorHandler().Sync(p) + _la = p.GetTokenStream().LA(1) + } + + case SqlParserSTAR: + localctx = NewSelectStarContext(p, localctx) + p.EnterOuterAlt(localctx, 2) + { + p.SetState(76) + p.Match(SqlParserSTAR) + } + + default: + panic(antlr.NewNoViableAltException(p, nil, nil, nil, nil, nil)) + } + + return localctx +} + +// IAggregationColumnsContext is an interface to support dynamic dispatch. +type IAggregationColumnsContext interface { + antlr.ParserRuleContext + + // GetParser returns the parser. + GetParser() antlr.Parser + + // IsAggregationColumnsContext differentiates from other interfaces. + IsAggregationColumnsContext() +} + +type AggregationColumnsContext struct { + *antlr.BaseParserRuleContext + parser antlr.Parser +} + +func NewEmptyAggregationColumnsContext() *AggregationColumnsContext { + var p = new(AggregationColumnsContext) + p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(nil, -1) + p.RuleIndex = SqlParserRULE_aggregationColumns + return p +} + +func (*AggregationColumnsContext) IsAggregationColumnsContext() {} + +func NewAggregationColumnsContext(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int) *AggregationColumnsContext { + var p = new(AggregationColumnsContext) + + p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(parent, invokingState) + + p.parser = parser + p.RuleIndex = SqlParserRULE_aggregationColumns + + return p +} + +func (s *AggregationColumnsContext) GetParser() antlr.Parser { return s.parser } + +func (s *AggregationColumnsContext) CopyFrom(ctx *AggregationColumnsContext) { + s.BaseParserRuleContext.CopyFrom(ctx.BaseParserRuleContext) +} + +func (s *AggregationColumnsContext) GetRuleContext() antlr.RuleContext { + return s +} + +func (s *AggregationColumnsContext) ToStringTree(ruleNames []string, recog antlr.Recognizer) string { + return antlr.TreesStringTree(s, ruleNames, recog) +} + +type SelectAggregationsContext struct { + *AggregationColumnsContext +} + +func NewSelectAggregationsContext(parser antlr.Parser, ctx antlr.ParserRuleContext) *SelectAggregationsContext { + var p = new(SelectAggregationsContext) + + p.AggregationColumnsContext = NewEmptyAggregationColumnsContext() + p.parser = parser + p.CopyFrom(ctx.(*AggregationColumnsContext)) + + return p +} + +func (s *SelectAggregationsContext) GetRuleContext() antlr.RuleContext { + return s +} + +func (s *SelectAggregationsContext) AllAggregationColumn() []IAggregationColumnContext { + children := s.GetChildren() + len := 0 + for _, ctx := range children { + if _, ok := ctx.(IAggregationColumnContext); ok { + len++ + } + } + + tst := make([]IAggregationColumnContext, len) + i := 0 + for _, ctx := range children { + if t, ok := ctx.(IAggregationColumnContext); ok { + tst[i] = t.(IAggregationColumnContext) + i++ + } + } + + return tst +} + +func (s *SelectAggregationsContext) AggregationColumn(i int) IAggregationColumnContext { + var t antlr.RuleContext + j := 0 + for _, ctx := range s.GetChildren() { + if _, ok := ctx.(IAggregationColumnContext); ok { + if j == i { + t = ctx.(antlr.RuleContext) + break + } + j++ + } + } + + if t == nil { + return nil + } + + return t.(IAggregationColumnContext) +} + +func (s *SelectAggregationsContext) AllCOMMA() []antlr.TerminalNode { + return s.GetTokens(SqlParserCOMMA) +} + +func (s *SelectAggregationsContext) COMMA(i int) antlr.TerminalNode { + return s.GetToken(SqlParserCOMMA, i) +} + +func (s *SelectAggregationsContext) Accept(visitor antlr.ParseTreeVisitor) interface{} { + switch t := visitor.(type) { + case SqlVisitor: + return t.VisitSelectAggregations(s) + + default: + return t.VisitChildren(s) + } +} + +func (p *SqlParser) AggregationColumns() (localctx IAggregationColumnsContext) { + this := p + _ = this + + localctx = NewAggregationColumnsContext(p, p.GetParserRuleContext(), p.GetState()) + p.EnterRule(localctx, 8, SqlParserRULE_aggregationColumns) + var _la int + + defer func() { + p.ExitRule() + }() + + defer func() { + if err := recover(); err != nil { + if v, ok := err.(antlr.RecognitionException); ok { + localctx.SetException(v) + p.GetErrorHandler().ReportError(p, v) + p.GetErrorHandler().Recover(p, v) + } else { + panic(err) + } + } + }() + + localctx = NewSelectAggregationsContext(p, localctx) + p.EnterOuterAlt(localctx, 1) + { + p.SetState(79) + p.AggregationColumn() + } + p.SetState(84) + p.GetErrorHandler().Sync(p) + _la = p.GetTokenStream().LA(1) + + for _la == SqlParserCOMMA { + { + p.SetState(80) + p.Match(SqlParserCOMMA) + } + { + p.SetState(81) + p.AggregationColumn() + } + + p.SetState(86) + p.GetErrorHandler().Sync(p) + _la = p.GetTokenStream().LA(1) + } + + return localctx +} + +// IGroupByAggregationColumnsContext is an interface to support dynamic dispatch. +type IGroupByAggregationColumnsContext interface { + antlr.ParserRuleContext + + // GetParser returns the parser. + GetParser() antlr.Parser + + // IsGroupByAggregationColumnsContext differentiates from other interfaces. + IsGroupByAggregationColumnsContext() +} + +type GroupByAggregationColumnsContext struct { + *antlr.BaseParserRuleContext + parser antlr.Parser +} + +func NewEmptyGroupByAggregationColumnsContext() *GroupByAggregationColumnsContext { + var p = new(GroupByAggregationColumnsContext) + p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(nil, -1) + p.RuleIndex = SqlParserRULE_groupByAggregationColumns + return p +} + +func (*GroupByAggregationColumnsContext) IsGroupByAggregationColumnsContext() {} + +func NewGroupByAggregationColumnsContext(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int) *GroupByAggregationColumnsContext { + var p = new(GroupByAggregationColumnsContext) + + p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(parent, invokingState) + + p.parser = parser + p.RuleIndex = SqlParserRULE_groupByAggregationColumns + + return p +} + +func (s *GroupByAggregationColumnsContext) GetParser() antlr.Parser { return s.parser } + +func (s *GroupByAggregationColumnsContext) CopyFrom(ctx *GroupByAggregationColumnsContext) { + s.BaseParserRuleContext.CopyFrom(ctx.BaseParserRuleContext) +} + +func (s *GroupByAggregationColumnsContext) GetRuleContext() antlr.RuleContext { + return s +} + +func (s *GroupByAggregationColumnsContext) ToStringTree(ruleNames []string, recog antlr.Recognizer) string { + return antlr.TreesStringTree(s, ruleNames, recog) +} + +type SelectGroupByAggregationsContext struct { + *GroupByAggregationColumnsContext +} + +func NewSelectGroupByAggregationsContext(parser antlr.Parser, ctx antlr.ParserRuleContext) *SelectGroupByAggregationsContext { + var p = new(SelectGroupByAggregationsContext) + + p.GroupByAggregationColumnsContext = NewEmptyGroupByAggregationColumnsContext() + p.parser = parser + p.CopyFrom(ctx.(*GroupByAggregationColumnsContext)) + + return p +} + +func (s *SelectGroupByAggregationsContext) GetRuleContext() antlr.RuleContext { + return s +} + +func (s *SelectGroupByAggregationsContext) AllColumn() []IColumnContext { + children := s.GetChildren() + len := 0 + for _, ctx := range children { + if _, ok := ctx.(IColumnContext); ok { + len++ + } + } + + tst := make([]IColumnContext, len) + i := 0 + for _, ctx := range children { + if t, ok := ctx.(IColumnContext); ok { + tst[i] = t.(IColumnContext) + i++ + } + } + + return tst +} + +func (s *SelectGroupByAggregationsContext) Column(i int) IColumnContext { + var t antlr.RuleContext + j := 0 + for _, ctx := range s.GetChildren() { + if _, ok := ctx.(IColumnContext); ok { + if j == i { + t = ctx.(antlr.RuleContext) + break + } + j++ + } + } + + if t == nil { + return nil + } + + return t.(IColumnContext) +} + +func (s *SelectGroupByAggregationsContext) AllAggregationColumn() []IAggregationColumnContext { + children := s.GetChildren() + len := 0 + for _, ctx := range children { + if _, ok := ctx.(IAggregationColumnContext); ok { + len++ + } + } + + tst := make([]IAggregationColumnContext, len) + i := 0 + for _, ctx := range children { + if t, ok := ctx.(IAggregationColumnContext); ok { + tst[i] = t.(IAggregationColumnContext) + i++ + } + } + + return tst +} + +func (s *SelectGroupByAggregationsContext) AggregationColumn(i int) IAggregationColumnContext { + var t antlr.RuleContext + j := 0 + for _, ctx := range s.GetChildren() { + if _, ok := ctx.(IAggregationColumnContext); ok { + if j == i { + t = ctx.(antlr.RuleContext) + break + } + j++ + } + } + + if t == nil { + return nil + } + + return t.(IAggregationColumnContext) +} + +func (s *SelectGroupByAggregationsContext) AllCOMMA() []antlr.TerminalNode { + return s.GetTokens(SqlParserCOMMA) +} + +func (s *SelectGroupByAggregationsContext) COMMA(i int) antlr.TerminalNode { + return s.GetToken(SqlParserCOMMA, i) +} + +func (s *SelectGroupByAggregationsContext) Accept(visitor antlr.ParseTreeVisitor) interface{} { + switch t := visitor.(type) { + case SqlVisitor: + return t.VisitSelectGroupByAggregations(s) + + default: + return t.VisitChildren(s) + } +} + +func (p *SqlParser) GroupByAggregationColumns() (localctx IGroupByAggregationColumnsContext) { + this := p + _ = this + + localctx = NewGroupByAggregationColumnsContext(p, p.GetParserRuleContext(), p.GetState()) + p.EnterRule(localctx, 10, SqlParserRULE_groupByAggregationColumns) + var _la int + + defer func() { + p.ExitRule() + }() + + defer func() { + if err := recover(); err != nil { + if v, ok := err.(antlr.RecognitionException); ok { + localctx.SetException(v) + p.GetErrorHandler().ReportError(p, v) + p.GetErrorHandler().Recover(p, v) + } else { + panic(err) + } + } + }() + + localctx = NewSelectGroupByAggregationsContext(p, localctx) + p.EnterOuterAlt(localctx, 1) + p.SetState(89) + p.GetErrorHandler().Sync(p) + + switch p.GetTokenStream().LA(1) { + case SqlParserIDENTIFIER: + { + p.SetState(87) + p.Column() + } + + case SqlParserK_COUNT, SqlParserK_SUM, SqlParserK_MIN, SqlParserK_MAX, SqlParserK_AVG: + { + p.SetState(88) + p.AggregationColumn() + } + + default: + panic(antlr.NewNoViableAltException(p, nil, nil, nil, nil, nil)) + } + p.SetState(98) + p.GetErrorHandler().Sync(p) + _la = p.GetTokenStream().LA(1) + + for _la == SqlParserCOMMA { + { + p.SetState(91) + p.Match(SqlParserCOMMA) + } + p.SetState(94) + p.GetErrorHandler().Sync(p) + + switch p.GetTokenStream().LA(1) { + case SqlParserK_COUNT, SqlParserK_SUM, SqlParserK_MIN, SqlParserK_MAX, SqlParserK_AVG: + { + p.SetState(92) + p.AggregationColumn() + } + + case SqlParserIDENTIFIER: + { + p.SetState(93) + p.Column() + } + + default: + panic(antlr.NewNoViableAltException(p, nil, nil, nil, nil, nil)) + } + + p.SetState(100) + p.GetErrorHandler().Sync(p) + _la = p.GetTokenStream().LA(1) + } + + return localctx +} + +// IAggregationColumnContext is an interface to support dynamic dispatch. +type IAggregationColumnContext interface { + antlr.ParserRuleContext + + // GetParser returns the parser. + GetParser() antlr.Parser + + // IsAggregationColumnContext differentiates from other interfaces. + IsAggregationColumnContext() +} + +type AggregationColumnContext struct { + *antlr.BaseParserRuleContext + parser antlr.Parser +} + +func NewEmptyAggregationColumnContext() *AggregationColumnContext { + var p = new(AggregationColumnContext) + p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(nil, -1) + p.RuleIndex = SqlParserRULE_aggregationColumn + return p +} + +func (*AggregationColumnContext) IsAggregationColumnContext() {} + +func NewAggregationColumnContext(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int) *AggregationColumnContext { + var p = new(AggregationColumnContext) + + p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(parent, invokingState) + + p.parser = parser + p.RuleIndex = SqlParserRULE_aggregationColumn + + return p +} + +func (s *AggregationColumnContext) GetParser() antlr.Parser { return s.parser } + +func (s *AggregationColumnContext) L_BRACKET() antlr.TerminalNode { + return s.GetToken(SqlParserL_BRACKET, 0) +} + +func (s *AggregationColumnContext) R_BRACKET() antlr.TerminalNode { + return s.GetToken(SqlParserR_BRACKET, 0) +} + +func (s *AggregationColumnContext) K_MIN() antlr.TerminalNode { + return s.GetToken(SqlParserK_MIN, 0) +} + +func (s *AggregationColumnContext) K_MAX() antlr.TerminalNode { + return s.GetToken(SqlParserK_MAX, 0) +} + +func (s *AggregationColumnContext) K_COUNT() antlr.TerminalNode { + return s.GetToken(SqlParserK_COUNT, 0) +} + +func (s *AggregationColumnContext) K_AVG() antlr.TerminalNode { + return s.GetToken(SqlParserK_AVG, 0) +} + +func (s *AggregationColumnContext) K_SUM() antlr.TerminalNode { + return s.GetToken(SqlParserK_SUM, 0) +} + +func (s *AggregationColumnContext) AllIDENTIFIER() []antlr.TerminalNode { + return s.GetTokens(SqlParserIDENTIFIER) +} + +func (s *AggregationColumnContext) IDENTIFIER(i int) antlr.TerminalNode { + return s.GetToken(SqlParserIDENTIFIER, i) +} + +func (s *AggregationColumnContext) DOT() antlr.TerminalNode { + return s.GetToken(SqlParserDOT, 0) +} + +func (s *AggregationColumnContext) Alias() IAliasContext { + var t antlr.RuleContext + for _, ctx := range s.GetChildren() { + if _, ok := ctx.(IAliasContext); ok { + t = ctx.(antlr.RuleContext) + break + } + } + + if t == nil { + return nil + } + + return t.(IAliasContext) +} + +func (s *AggregationColumnContext) GetRuleContext() antlr.RuleContext { + return s +} + +func (s *AggregationColumnContext) ToStringTree(ruleNames []string, recog antlr.Recognizer) string { + return antlr.TreesStringTree(s, ruleNames, recog) +} + +func (s *AggregationColumnContext) Accept(visitor antlr.ParseTreeVisitor) interface{} { + switch t := visitor.(type) { + case SqlVisitor: + return t.VisitAggregationColumn(s) + + default: + return t.VisitChildren(s) + } +} + +func (p *SqlParser) AggregationColumn() (localctx IAggregationColumnContext) { + this := p + _ = this + + localctx = NewAggregationColumnContext(p, p.GetParserRuleContext(), p.GetState()) + p.EnterRule(localctx, 12, SqlParserRULE_aggregationColumn) + var _la int + + defer func() { + p.ExitRule() + }() + + defer func() { + if err := recover(); err != nil { + if v, ok := err.(antlr.RecognitionException); ok { + localctx.SetException(v) + p.GetErrorHandler().ReportError(p, v) + p.GetErrorHandler().Recover(p, v) + } else { + panic(err) + } + } + }() + + p.EnterOuterAlt(localctx, 1) + { + p.SetState(101) + _la = p.GetTokenStream().LA(1) + + if !(((_la-30)&-(0x1f+1)) == 0 && ((1< 90);`, + expected: 13, + }, + { + name: "nested 11", + query: `SELECT * WHERE (provider.type = 'middle' and price < 30) and (provider.type = 'middle' and price > 90);`, + expected: 0, + }, + { + name: "nested 12", + query: `SELECT * WHERE (provider.type = 'middle' and price < 10) or (provider.type = 'middle' and price > 90) or (provider.type = 'big' and price > 90);`, + expected: 10, + }, + { + name: "nested 13", + query: `SELECT * WHERE (provider.type = 'middle' and price < 10 and is_alive='true');`, + expected: 2, + }, + { + name: "nested 14", + query: `SELECT * WHERE (provider.type = 'middle' and price < 10 and is_alive='true') or (provider.type = 'small' and price < 10 and is_alive='false');`, + expected: 4, + }, + { + name: "nested 15", + query: `SELECT * WHERE provider.type = 'middle' and provider.number > 50 and price > 50;`, + expected: 17, + }, + { + name: "nested 16", + query: `SELECT * WHERE provider.type = 'big' and provider.number > 50 and price > 50;`, + expected: 16, + }, + { + name: "nested 17", + query: `SELECT * WHERE provider.type = 'big' and provider.number > 50 or price > 80 ;`, + expected: 29, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + in := make(chan plog.LogRecordSlice) + out := make(chan plog.LogRecordSlice) + outErr := make(chan error) + visitor := NewSQLStreamVisitor(tt.query, in, out, outErr, zap.NewNop()) + defer visitor.Stop() + in <- GenerateTestLogs() + + ls := <-out + + assert.Equal(t, tt.expected, ls.Len()) + + }) + } + +} + +func TestSelectNested(t *testing.T) { + tests := []struct { + name, query string + + expected int + expectedNested func() pcommon.Value + }{ + { + name: "nested 2 fields", + query: `SELECT provider.source, provider.type where provider.number = 2;`, + expected: 1, + expectedNested: func() pcommon.Value { + val := pcommon.NewValueMap() + val.MapVal().Insert("source", pcommon.NewValueString("Source 2")) + val.MapVal().Insert("type", pcommon.NewValueString("big")) + return val + }, + }, + { + name: "nested 3 fields", + query: `SELECT provider.source, provider.type, provider.number where provider.number = 2;`, + expected: 1, + expectedNested: func() pcommon.Value { + val := pcommon.NewValueMap() + val.MapVal().Insert("source", pcommon.NewValueString("Source 2")) + val.MapVal().Insert("type", pcommon.NewValueString("big")) + val.MapVal().Insert("number", pcommon.NewValueDouble(2)) + return val + }, + }, + { + name: "nested all fields", + query: `SELECT provider where provider.number = 2;`, + expected: 1, + expectedNested: func() pcommon.Value { + val := pcommon.NewValueMap() + val.MapVal().Insert("source", pcommon.NewValueString("Source 2")) + val.MapVal().Insert("type", pcommon.NewValueString("big")) + val.MapVal().Insert("number", pcommon.NewValueDouble(2)) + return val + }, + }, + { + name: "nested all unnecessary fields", + query: `SELECT provider.source, provider.type, provider.number, provider.source, name where provider.number = 2;`, + expected: 1, + expectedNested: func() pcommon.Value { + val := pcommon.NewValueMap() + val.MapVal().Insert("source", pcommon.NewValueString("Source 2")) + val.MapVal().Insert("type", pcommon.NewValueString("big")) + val.MapVal().Insert("number", pcommon.NewValueDouble(2)) + return val + }, + }, + { + name: "nested without where", + query: `SELECT provider.source;`, + expected: 100, + expectedNested: func() pcommon.Value { + val := pcommon.NewValueMap() + val.MapVal().Insert("source", pcommon.NewValueString("Source 0")) + return val + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + in := make(chan plog.LogRecordSlice) + out := make(chan plog.LogRecordSlice) + outErr := make(chan error) + visitor := NewSQLStreamVisitor(tt.query, in, out, outErr, zap.NewNop()) + defer visitor.Stop() + in <- GenerateTestLogs() + + ls := <-out + res, ok := ls.At(0).Attributes().Get("provider") + assert.True(t, ok) + assert.True(t, reflect.DeepEqual(tt.expectedNested(), res)) + + }) + } + +} + +func TestSelectNestedWIthAliases(t *testing.T) { + tests := []struct { + name, query, key string + expected int + expectedNested func() pcommon.Map + }{ + { + name: "nested 2 with alias", + query: `SELECT provider.source as NewSource, provider.type as NewType where provider.number = 2;`, + expected: 1, + expectedNested: func() pcommon.Map { + val := pcommon.NewMap() + val.Insert("NewSource", pcommon.NewValueString("Source 2")) + val.Insert("NewType", pcommon.NewValueString("big")) + return val + }, + }, + { + name: "nested 2 with alias and nested", + query: `SELECT provider.source as NewSource, provider.type as NewType, provider.number where provider.number = 2;`, + expected: 1, + expectedNested: func() pcommon.Map { + val := pcommon.NewMap() + val.Insert("NewSource", pcommon.NewValueString("Source 2")) + val.Insert("NewType", pcommon.NewValueString("big")) + mapValue := pcommon.NewValueMap() + mapValue.MapVal().Insert("number", pcommon.NewValueDouble(2)) + val.Insert("provider", mapValue) + return val + }, + }, + { + name: "nested 2 with alias and nested ans simple", + query: `SELECT provider.source as NewSource, provider.type as NewType, price, provider.number where provider.number = 2;`, + expected: 1, + expectedNested: func() pcommon.Map { + val := pcommon.NewMap() + val.Insert("NewSource", pcommon.NewValueString("Source 2")) + val.Insert("NewType", pcommon.NewValueString("big")) + val.Insert("price", pcommon.NewValueInt(2)) + mapValue := pcommon.NewValueMap() + mapValue.MapVal().Insert("number", pcommon.NewValueDouble(2)) + val.Insert("provider", mapValue) + return val + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + in := make(chan plog.LogRecordSlice) + out := make(chan plog.LogRecordSlice) + outErr := make(chan error) + visitor := NewSQLStreamVisitor(tt.query, in, out, outErr, zap.NewNop()) + defer visitor.Stop() + in <- GenerateTestLogs() + + ls := <-out + res := ls.At(0).Attributes() + assert.True(t, reflect.DeepEqual(tt.expectedNested(), res)) + + }) + } + +} diff --git a/processor/stream_processor/parser/sql_stream_tumbling_test.go b/processor/stream_processor/parser/sql_stream_tumbling_test.go new file mode 100644 index 000000000000..7d5454237743 --- /dev/null +++ b/processor/stream_processor/parser/sql_stream_tumbling_test.go @@ -0,0 +1,435 @@ +package parser + +import ( + "fmt" + "math/rand" + "strconv" + "sync" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "go.opentelemetry.io/collector/pdata/pcommon" + "go.opentelemetry.io/collector/pdata/plog" + "go.uber.org/zap" +) + +func TestIsWindowTumbling(t *testing.T) { + tests := []struct { + name string + query string + value int + res bool + }{ + { + name: "not tumbling query", + query: `SELECT * WHERE price > 80 or name like '3';`, + value: 0, + res: false, + }, + { + name: "tumbling query", + query: `select max(price) window tumbling 3 where price > 4;`, + value: 3, + res: true, + }, + { + name: "incorrect tumbling query", + query: `select max(price) window tumbling kwa where price > 4;`, + value: 0, + res: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + res, val := IsTumblingQuery(tt.query) + assert.Equal(t, tt.value, val) + assert.Equal(t, tt.res, res) + }) + } +} + +func TestWindowTumblingLoopOneField(t *testing.T) { + tests := []struct { + name, query string + expected int + expectedRes float64 + }{ + { + name: "max", + query: "select max(price) window tumbling 50 where price > 4;", + expected: 1, + expectedRes: 99.0, + }, + { + name: "min", + query: "select min(price) window tumbling 50 where price > 4;", + expected: 1, + expectedRes: 5.0, + }, + { + name: "min 2", + query: "select min(price) window tumbling 50 where price > 95;", + expected: 1, + expectedRes: 96.0, + }, + { + name: "min 2", + query: "select count(price) window tumbling 50 where price > 95;", + expected: 1, + expectedRes: 4.0, + }, + { + name: "sum", + query: "select sum(price) window tumbling 50 where price > 95;", + expected: 1, + expectedRes: 390.0, + }, + { + name: "sum", + query: "select avg(price) window tumbling 50 where price < 10;", + expected: 1, + expectedRes: 4.5, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + in := make(chan plog.LogRecordSlice) + out := make(chan plog.LogRecordSlice) + outErr := make(chan error) + var ls plog.LogRecordSlice + var wg sync.WaitGroup + wg.Add(1) + visitor := NewSQLStreamVisitor(tt.query, in, out, outErr, zap.NewNop()) + defer visitor.Stop() + go func() { + defer wg.Done() + for { + ls = <-out + break + } + }() + + in <- GenerateTestLogs() + <-time.After(1 * time.Millisecond) + wg.Wait() + + assert.Equal(t, tt.expected, ls.Len()) + val, ok := ls.At(0).Attributes().Get("price") + assert.True(t, ok) + assert.Equal(t, tt.expectedRes, val.DoubleVal()) + }) + } +} + +func TestWindowTumblingAllAggregations(t *testing.T) { + tests := []struct { + name, query string + expected int + expectedSum, expectedAvg, expectedCount, expectedMax, expectedMin float64 + }{ + { + name: "1", + query: "select max(price), min(price) as min_price, avg(price) as avg_price, sum(price) as sum_price, count(price) as count_price window tumbling 50 where price > 95;", + expected: 1, + expectedSum: 390.0, + expectedMin: 96.0, + expectedMax: 99.0, + expectedCount: 4.0, + expectedAvg: 97.5, + }, + { + name: "2", + query: "select max(price), min(price) as min_price, avg(price) as avg_price, sum(price) as sum_price, count(price) as count_price window tumbling 50 where price < 6;", + expected: 1, + expectedSum: 15.0, + expectedMin: 0.0, + expectedMax: 5.0, + expectedCount: 6.0, + expectedAvg: 2.5, + }, + { + name: "3", + query: "select max(price), min(price) as min_price, avg(price) as avg_price, sum(price) as sum_price, count(price) as count_price window tumbling 50 where price > 6 and price < 11;", + expected: 1, + expectedSum: 34.0, + expectedMin: 7.0, + expectedMax: 10.0, + expectedCount: 4.0, + expectedAvg: 8.5, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + in := make(chan plog.LogRecordSlice) + out := make(chan plog.LogRecordSlice) + outErr := make(chan error) + var wg sync.WaitGroup + wg.Add(1) + visitor := NewSQLStreamVisitor(tt.query, in, out, outErr, zap.NewNop()) + defer visitor.Stop() + + in <- GenerateTestLogs() + ls := <-out + + assert.Equal(t, tt.expected, ls.Len()) + assert.Equal(t, ls.At(0).Attributes().Len(), 5) + + val, ok := ls.At(0).Attributes().Get("price") + assert.True(t, ok) + assert.Equal(t, tt.expectedMax, val.DoubleVal()) + + val, ok = ls.At(0).Attributes().Get("min_price") + assert.True(t, ok) + assert.Equal(t, tt.expectedMin, val.DoubleVal()) + + val, ok = ls.At(0).Attributes().Get("count_price") + assert.True(t, ok) + assert.Equal(t, tt.expectedCount, val.DoubleVal()) + + val, ok = ls.At(0).Attributes().Get("sum_price") + assert.True(t, ok) + assert.Equal(t, tt.expectedSum, val.DoubleVal()) + + val, ok = ls.At(0).Attributes().Get("avg_price") + assert.True(t, ok) + assert.Equal(t, tt.expectedAvg, val.DoubleVal()) + }) + } +} + +func TestWindowTumblingNestedAggregations(t *testing.T) { + tests := []struct { + name, query string + expected int + expectedSum, expectedAvg, expectedCount, expectedMax, expectedMin float64 + }{ + + { + name: "1", + query: "select max(provider.number), min(provider.number) as min_provider_number, avg(provider.number) as avg_provider_number, sum(provider.number) as sum_provider_number, count(provider.number) as count_provider_number window tumbling 50 where price > 6 and price < 11;", + expected: 1, + expectedSum: 34.0, + expectedMin: 7.0, + expectedMax: 10.0, + expectedCount: 4.0, + expectedAvg: 8.5, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + in := make(chan plog.LogRecordSlice) + out := make(chan plog.LogRecordSlice) + outErr := make(chan error) + var wg sync.WaitGroup + wg.Add(1) + visitor := NewSQLStreamVisitor(tt.query, in, out, outErr, zap.NewNop()) + defer visitor.Stop() + + in <- GenerateTestLogs() + ls := <-out + + assert.Equal(t, tt.expected, ls.Len()) + + val, ok := ls.At(0).Attributes().Get("provider") + assert.True(t, ok) + val, ok = val.MapVal().Get("number") + assert.True(t, ok) + assert.Equal(t, tt.expectedMax, val.DoubleVal()) + + val, ok = ls.At(0).Attributes().Get("min_provider_number") + assert.True(t, ok) + assert.Equal(t, tt.expectedMin, val.DoubleVal()) + + val, ok = ls.At(0).Attributes().Get("count_provider_number") + assert.True(t, ok) + assert.Equal(t, tt.expectedCount, val.DoubleVal()) + + val, ok = ls.At(0).Attributes().Get("sum_provider_number") + assert.True(t, ok) + assert.Equal(t, tt.expectedSum, val.DoubleVal()) + + val, ok = ls.At(0).Attributes().Get("avg_provider_number") + assert.True(t, ok) + assert.Equal(t, tt.expectedAvg, val.DoubleVal()) + }) + } +} + +func TestWindowTumblingAvg(t *testing.T) { + in := make(chan plog.LogRecordSlice) + out := make(chan plog.LogRecordSlice) + outErr := make(chan error) + query := "select avg(price), sum(price) as SumPrice window tumbling 3 ;" + var ls plog.LogRecordSlice + + visitor := NewSQLStreamVisitor(query, in, out, outErr, zap.NewNop()) + defer visitor.Stop() + in <- GenerateTestLogs() + ls = <-out + res, ok := ls.At(0).Attributes().Get("price") + assert.True(t, ok) + assert.Equal(t, pcommon.NewValueDouble(49.5), res) +} + +func TestWindowTumblingCount(t *testing.T) { + in := make(chan plog.LogRecordSlice) + out := make(chan plog.LogRecordSlice) + outErr := make(chan error) + query := "select count(price) window tumbling 30 ;" + var ls plog.LogRecordSlice + + visitor := NewSQLStreamVisitor(query, in, out, outErr, zap.NewNop()) + defer visitor.Stop() + in <- GenerateTestLogs() + ls = <-out + res, ok := ls.At(0).Attributes().Get("price") + assert.True(t, ok) + assert.Equal(t, pcommon.NewValueDouble(100.0), res) +} + +func TestWindowTumblingSum(t *testing.T) { + in := make(chan plog.LogRecordSlice) + out := make(chan plog.LogRecordSlice) + outErr := make(chan error) + query := "select sum(price) window tumbling 30 ;" + var ls plog.LogRecordSlice + + visitor := NewSQLStreamVisitor(query, in, out, outErr, zap.NewNop()) + defer visitor.Stop() + in <- GenerateTestLogs() + ls = <-out + res, ok := ls.At(0).Attributes().Get("price") + assert.True(t, ok) + assert.Equal(t, pcommon.NewValueDouble(4950.0), res) +} + +func TestWindowTumblingMin(t *testing.T) { + in := make(chan plog.LogRecordSlice) + out := make(chan plog.LogRecordSlice) + outErr := make(chan error) + query := "select min(price) window tumbling 30 ;" + var ls plog.LogRecordSlice + + visitor := NewSQLStreamVisitor(query, in, out, outErr, zap.NewNop()) + defer visitor.Stop() + in <- GenerateTestLogs() + ls = <-out + res, ok := ls.At(0).Attributes().Get("price") + assert.True(t, ok) + assert.Equal(t, pcommon.NewValueDouble(0.0), res) +} + +func TestWindowTumblingMax(t *testing.T) { + in := make(chan plog.LogRecordSlice) + out := make(chan plog.LogRecordSlice) + outErr := make(chan error) + query := "select max(price) window tumbling 30 ;" + var ls plog.LogRecordSlice + + visitor := NewSQLStreamVisitor(query, in, out, outErr, zap.NewNop()) + defer visitor.Stop() + in <- GenerateTestLogs() + ls = <-out + res, ok := ls.At(0).Attributes().Get("price") + assert.True(t, ok) + assert.Equal(t, pcommon.NewValueDouble(99.0), res) +} + +/* +func TestAvgContext_K_MIN(t *testing.T) { + ls := GenerateTestLogs() + expectedRes, err := min(ls, "price") + assert.Nil(t, err) + assert.Equal(t, 0.0, expectedRes) + +} + +func TestAvgContext_K_MAX(t *testing.T) { + ls := GenerateTestLogs() + expectedRes, err := max(ls, "price") + assert.Nil(t, err) + assert.Equal(t, 99.0, expectedRes) +} + +func TestAvgContext_K_SUM(t *testing.T) { + ls := GenerateTestLogs() + expectedRes, err := sum(ls, "price") + assert.Nil(t, err) + assert.Equal(t, 4950.0, expectedRes) +} + +*/ + +func TestAvgContext_K_COUNT(t *testing.T) { + //ls := GenerateTestLogs() + count := 100 //count(ls) + assert.Equal(t, 100, count) +} + +func TestWindowTumblingFlow(t *testing.T) { + t.Skip() + in := make(chan plog.LogRecordSlice) + out := make(chan plog.LogRecordSlice) + outErr := make(chan error) + query := "select avg(price) window tumbling 3000 ;" + + visitor := NewSQLStreamVisitor(query, in, out, outErr, zap.NewNop()) + defer visitor.Stop() + var wg sync.WaitGroup + + wg.Add(1) + go func() { + for { + in <- generateRandomSplittedTestLogs() + <-time.After(500 * time.Millisecond) + } + }() + + go func() { + for { + ls := <-out + ls.At(0).Attributes().Range(func(k string, v pcommon.Value) bool { + fmt.Printf("%q: %q ", k, v.AsString()) + return true + }) + fmt.Print("\n") + } + }() + + wg.Wait() +} + +func generateRandomSplittedTestLogs() plog.LogRecordSlice { + + ld := plog.NewLogs() + sc := ld.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty() + + r := rand.Intn(100) + + for i := 0; i < r; i++ { + record := sc.LogRecords().AppendEmpty() + record.Attributes().InsertString("name", "Test name "+strconv.Itoa(i)) + record.Attributes().InsertBool("is_alive", i%2 == 0) + record.Attributes().InsertInt("price", int64(i)) + } + + return ld.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords() +} + +func generateGroupByTestLogs() plog.LogRecordSlice { + ld := plog.NewLogs() + sc := ld.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty() + + for i := 0; i < 100; i++ { + record := sc.LogRecords().AppendEmpty() + name := strconv.Itoa(i) + record.Attributes().InsertString("name", fmt.Sprint("Test name ", string(name[0]))) + record.Attributes().InsertString("lname", fmt.Sprint("Last name ", string(name[0]))) + record.Attributes().InsertBool("is_alive", i%2 == 0) + record.Attributes().InsertInt("price", int64(i)) + } + + return ld.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords() +} diff --git a/processor/stream_processor/parser/sql_stream_visitor.go b/processor/stream_processor/parser/sql_stream_visitor.go new file mode 100644 index 000000000000..3661e8522020 --- /dev/null +++ b/processor/stream_processor/parser/sql_stream_visitor.go @@ -0,0 +1,695 @@ +package parser + +import ( + "fmt" + "strings" + "time" + + "github.com/antlr/antlr4/runtime/Go/antlr" + "go.opentelemetry.io/collector/pdata/pcommon" + "go.opentelemetry.io/collector/pdata/plog" + "go.uber.org/zap" +) + +// check that type implements SqlVisitor +var _ SqlVisitor = (*SQLStreamVisitor)(nil) + +type SQLStreamVisitor struct { + antlr.ParseTreeVisitor + logger *zap.Logger + + query string + + tumblingPeriod time.Duration + + // this is what we've got + originalRecords plog.LogRecordSlice + currentOriginalRecord plog.LogRecord + + // this is logs to emit + resultRecords plog.LogRecordSlice + currentResultRecord plog.LogRecord + groupByTempLogRecords map[string]plog.LogRecordSlice + + in <-chan plog.LogRecordSlice + out chan<- plog.LogRecordSlice + outErr chan<- error + shutdownC chan struct{} +} + +func NewSQLStreamVisitor(query string, in <-chan plog.LogRecordSlice, out chan<- plog.LogRecordSlice, outErr chan<- error, logger *zap.Logger) *SQLStreamVisitor { + + visitor := &SQLStreamVisitor{ + ParseTreeVisitor: &SQLStreamVisitor{}, + query: query, + logger: logger, + in: in, + out: out, + outErr: outErr, + shutdownC: make(chan struct{}, 1), + } + + isTumbling, val := IsTumblingQuery(query) + if isTumbling { + visitor.tumblingPeriod = time.Millisecond * time.Duration(val) + visitor.startWindowTumblingLoop() + return visitor + } + + go visitor.startSimpleWorkerLoop() + return visitor +} + +func (v *SQLStreamVisitor) Stop() { + close(v.shutdownC) +} + +func (v *SQLStreamVisitor) startSimpleWorkerLoop() { + + for { + select { + case v.originalRecords = <-v.in: + if err := v.runQuery(); err != nil { + v.outErr <- err + break + } + v.out <- v.resultRecords + v.originalRecords = plog.NewLogRecordSlice() + + case <-v.shutdownC: + if v.resultRecords.Len() > 0 { + v.out <- v.resultRecords + } + v.logger.Debug("sql engine shutting down") + return + + } + + } +} + +func (v *SQLStreamVisitor) startWindowTumblingLoop() { + v.originalRecords = plog.NewLogRecordSlice() + ticker := time.NewTicker(v.tumblingPeriod) + + go func() { + for { + select { + case newRec := <-v.in: + newRec.CopyTo(v.originalRecords) + case <-v.shutdownC: + v.logger.Debug("sql engine shutting down") + return + } + } + }() + + go func() { + for { + select { + case <-ticker.C: + if v.originalRecords.Len() > 0 { + if err := v.runQuery(); err != nil { + v.outErr <- err + break + } + v.out <- v.resultRecords + v.originalRecords = plog.NewLogRecordSlice() + } + case <-v.shutdownC: + v.logger.Debug("sql engine shutting down") + return + + } + } + }() +} + +func (v *SQLStreamVisitor) runQuery() error { + v.resultRecords = plog.NewLogRecordSlice() + is := antlr.NewInputStream(v.query) + lexer := NewSqlLexer(is) + stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel) + parser := NewSqlParser(stream) + res := v.Visit(parser.SqlQuery()) + switch res := res.(type) { + case error: + return res + } + + return nil +} + +func (v *SQLStreamVisitor) GetResult() (plog.LogRecordSlice, error) { + return v.originalRecords, nil +} + +func (v *SQLStreamVisitor) Visit(tree antlr.ParseTree) interface{} { + + switch t := tree.(type) { + case *antlr.ErrorNodeImpl: + return nil + case *SqlQueryContext: + return t.Accept(v) + } + + return nil +} + +func (v *SQLStreamVisitor) VisitSqlQuery(ctx *SqlQueryContext) interface{} { + return ctx.SelectQuery().Accept(v) +} + +func (v *SQLStreamVisitor) VisitSelectSimple(ctx *SelectSimpleContext) interface{} { + if ctx.WhereStatement() == nil { + return ctx.ResultColumns().Accept(v) + } + switch res := ctx.WhereStatement().Accept(v).(type) { + case error: + return res + case nil: + + } + + return nil +} + +func (v *SQLStreamVisitor) VisitSelectTumbling(ctx *SelectTumblingContext) interface{} { + if ctx.WhereStatement() != nil { + switch res := ctx.WhereStatement().Accept(v).(type) { + case error: + return res + case nil: + + } + } + + return ctx.AggregationColumns().Accept(v) +} + +func (v *SQLStreamVisitor) VisitSelectTumblingGroupBy(ctx *SelectTumblingGroupByContext) interface{} { + if ctx.WhereStatement() != nil { + switch res := ctx.WhereStatement().Accept(v).(type) { + case error: + return res + case nil: + } + } + + switch res := ctx.GroupBy().Accept(v).(type) { + case error: + return res + case nil: + } + + return ctx.GroupByAggregationColumns().Accept(v) +} + +func (v *SQLStreamVisitor) VisitGroupBy(ctx *GroupByContext) interface{} { + + v.groupByTempLogRecords = make(map[string]plog.LogRecordSlice) + + for i := 0; i < v.originalRecords.Len(); i++ { + record := v.originalRecords.At(i) + _, value, err := getAttributeValue(ctx.Column(), record.Attributes()) + if err != nil { + return err + } + + _, ok := v.groupByTempLogRecords[value.AsString()] + if !ok { + v.groupByTempLogRecords[value.AsString()] = plog.NewLogRecordSlice() + } + appendedRec := v.groupByTempLogRecords[value.AsString()].AppendEmpty() + record.CopyTo(appendedRec) + } + return nil +} + +func (v *SQLStreamVisitor) VisitSelectGroupByAggregations(ctx *SelectGroupByAggregationsContext) interface{} { + resLS := plog.NewLogRecordSlice() + + for k, ls := range v.groupByTempLogRecords { + newRec := resLS.AppendEmpty() + if err := calculateGroupByAggregations(ctx, k, newRec, ls); err != nil { + return err + } + + } + v.resultRecords = resLS + return nil +} + +func (v *SQLStreamVisitor) VisitSelectAggregations(ctx *SelectAggregationsContext) interface{} { + + resLS := plog.NewLogRecordSlice() + newRec := resLS.AppendEmpty() + if err := calculateAggregations(ctx, newRec, v.originalRecords); err != nil { + return err + } + v.resultRecords = resLS + return nil +} + +func (v *SQLStreamVisitor) VisitAggregationColumn(ctx *AggregationColumnContext) interface{} { + // first we check that fields listed in select (including nested) exist in log record + fieldName := ctx.IDENTIFIER(0).GetText() + value, ok := v.currentOriginalRecord.Attributes().Get(fieldName) + if !ok { + return fmt.Errorf("field %q missed", fieldName) + } + + // this is simple field + if len(ctx.AllIDENTIFIER()) == 1 { + if ctx.Alias() != nil { + fieldName = ctx.Alias().GetStop().GetText() + } + v.currentResultRecord.Attributes().Insert(fieldName, value) + + return nil + } + + // here we process nested field + nestedFieldName := ctx.IDENTIFIER(1).GetText() + nestedVal, err := nestedFieldExistsInAttr(fieldName, nestedFieldName, v.currentOriginalRecord.Attributes()) + if err != nil { + return err + } + if ctx.Alias() != nil { + fieldName = ctx.Alias().GetStop().GetText() + v.currentResultRecord.Attributes().Insert(fieldName, nestedVal) + return nil + } + + var newMapVal pcommon.Value + newMapVal, ok = v.currentResultRecord.Attributes().Get(fieldName) + if !ok { + newMapVal = pcommon.NewValueMap() + } + + newMapVal.MapVal().Insert(nestedFieldName, nestedVal) + v.currentResultRecord.Attributes().Insert(fieldName, newMapVal) + + return nil +} + +// VisitSelectColumns is called in case of missed where statement and removes missed attributes +func (v *SQLStreamVisitor) VisitSelectColumns(ctx *SelectColumnsContext) interface{} { + var err error + for i := 0; i < v.originalRecords.Len(); i++ { + v.currentOriginalRecord = v.originalRecords.At(i) + v.currentResultRecord = plog.NewLogRecord() + + for _, col := range ctx.AllColumn() { + switch res := col.Accept(v).(type) { + case error: + err = res + } + } + + v.currentResultRecord.MoveTo(v.resultRecords.AppendEmpty()) + + } + + return err + +} + +func (v *SQLStreamVisitor) VisitWhereStmt(ctx *WhereStmtContext) interface{} { + var err error + // if WHERE statement missed + if ctx.Expr() == nil { + return nil + } + selectColCtx := getSelectColumnsFromWhereCtx(ctx) + selectAggrColCtx := getSelectAggregationsFromWhereCtx(ctx) + selectStarCtx := getSelectStarCtx(ctx) + + v.originalRecords.RemoveIf(func(record plog.LogRecord) bool { + v.currentOriginalRecord = record + v.currentResultRecord = plog.NewLogRecord() + + switch res := ctx.Expr().Accept(v).(type) { + case error: + err = res + return false + + case bool: + if !res { + return true + } + + // select * case + if selectStarCtx != nil { + v.currentOriginalRecord.MoveTo(v.resultRecords.AppendEmpty()) + return false + } + + // align result records according listed in select fields + if selectColCtx != nil { + err = v.applySelectColumns(selectColCtx) + } + + // the same for aggregation case ##selectTumbling #selectTumblingGroupBy + if selectAggrColCtx != nil { + err = v.applySelectAggregationColumns(selectAggrColCtx) + } + + v.currentResultRecord.MoveTo(v.resultRecords.AppendEmpty()) + return false + } + + return false + }) + + return err +} + +func (v *SQLStreamVisitor) applySelectColumns(ctx *SelectColumnsContext) error { + for _, col := range ctx.AllColumn() { + switch res := col.Accept(v).(type) { + case error: + return res + } + } + + return nil +} + +func (v *SQLStreamVisitor) applySelectAggregationColumns(ctx *SelectAggregationsContext) error { + for _, col := range ctx.AllAggregationColumn() { + switch res := col.Accept(v).(type) { + case error: + return res + } + } + + return nil +} + +func (v *SQLStreamVisitor) VisitSelectStar(ctx *SelectStarContext) interface{} { + v.originalRecords.CopyTo(v.resultRecords) + return nil +} + +func (v *SQLStreamVisitor) VisitIdentifierColumn(ctx *IdentifierColumnContext) interface{} { + // first we check that fields listed in select (including nested) exist in log record + fieldName := ctx.IDENTIFIER(0).GetText() + value, ok := v.currentOriginalRecord.Attributes().Get(fieldName) + if !ok { + return fmt.Errorf("field %q missed", fieldName) + } + + // this is simple field + if len(ctx.AllIDENTIFIER()) == 1 { + if ctx.Alias() != nil { + fieldName = ctx.Alias().GetStop().GetText() + } + v.currentResultRecord.Attributes().Insert(fieldName, value) + + return nil + } + + // here we process nested field + nestedFieldName := ctx.IDENTIFIER(1).GetText() + nestedVal, err := nestedFieldExistsInAttr(fieldName, nestedFieldName, v.currentOriginalRecord.Attributes()) + if err != nil { + return err + } + if ctx.Alias() != nil { + fieldName = ctx.Alias().GetStop().GetText() + v.currentResultRecord.Attributes().Insert(fieldName, nestedVal) + return nil + } + + var newMapVal pcommon.Value + newMapVal, ok = v.currentResultRecord.Attributes().Get(fieldName) + if !ok { + newMapVal = pcommon.NewValueMap() + } + + newMapVal.MapVal().Insert(nestedFieldName, nestedVal) + v.currentResultRecord.Attributes().Insert(fieldName, newMapVal) + + return nil +} + +func (v *SQLStreamVisitor) VisitFunctionColumn(ctx *FunctionColumnContext) interface{} { + + var simpleFuncCtx *SimpleFunctionContext + res := ctx.Function().Accept(v) + + switch res.(type) { + case error: + return res + case pcommon.Value: + default: + return fmt.Errorf("undefined result type") + } + + simpleFuncCtx, ok := ctx.GetChild(0).(*SimpleFunctionContext) + if !ok { + simpleFuncCtx = getSimpleFunctionContext(ctx) + } + + fieldName := simpleFuncCtx.IDENTIFIER(0).GetText() + if ctx.Alias() != nil { + fieldName = ctx.Alias().GetStop().GetText() + } + + // if simple field + if len(simpleFuncCtx.AllIDENTIFIER()) == 1 { + v.currentResultRecord.Attributes().Insert(fieldName, res.(pcommon.Value)) + return nil + } + + //this is nested field + nestedFieldName := simpleFuncCtx.IDENTIFIER(1).GetText() + newMapVal := pcommon.NewValueMap() + newMapVal.MapVal().Insert(nestedFieldName, res.(pcommon.Value)) + v.currentResultRecord.Attributes().Insert(nestedFieldName, newMapVal) + + return res +} + +func (v *SQLStreamVisitor) VisitSimpleFunction(ctx *SimpleFunctionContext) interface{} { + + fieldName := ctx.IDENTIFIER(0).GetText() + functionMame := ctx.FunctionName().GetText() + args := ctx.AllLiteralValue() + + value, ok := v.currentOriginalRecord.Attributes().Get(fieldName) + if !ok { + return fmt.Errorf("field %q missed", fieldName) + } + + // this is simple field + if len(ctx.AllIDENTIFIER()) == 1 { + + newValue, err := v.applyFunction(value, functionMame, args...) + if err != nil { + return err + } + + // v.currentResultRecord.Attributes().Insert(fieldName, newValue) + return newValue + } + + // here we process nested field + nestedFieldName := ctx.IDENTIFIER(1).GetText() + nestedVal, err := nestedFieldExistsInAttr(fieldName, nestedFieldName, v.currentOriginalRecord.Attributes()) + if err != nil { + return err + } + newValue, err := v.applyFunction(value, functionMame, args...) + + newMapVal := pcommon.NewValueMap() + newMapVal.MapVal().Insert(nestedFieldName, nestedVal) + //v.currentResultRecord.Attributes().Insert(fieldName, newValue) + return newValue +} + +func (v *SQLStreamVisitor) VisitRecursiveFunction(ctx *RecursiveFunctionContext) interface{} { + + res := ctx.Function().Accept(v) + + switch res.(type) { + case error: + return res + case pcommon.Value: + default: + return fmt.Errorf("undefined return type") + + } + tmpValue, err := v.applyFunction(res.(pcommon.Value), ctx.FunctionName().GetText(), ctx.AllLiteralValue()...) + if err != nil { + return nil + } + + return tmpValue +} + +func (v *SQLStreamVisitor) applyFunction(value pcommon.Value, functionName string, args ...ILiteralValueContext) (pcommon.Value, error) { + switch functionName { + case upper: + return pcommon.NewValueString(strings.ToUpper(value.AsString())), nil + case lower: + return pcommon.NewValueString(strings.ToLower(value.AsString())), nil + case substr: + if len(args) != 2 { + return pcommon.NewValueEmpty(), fmt.Errorf("substr accept two arguments") + } + res, err := substring(value, args[0].GetText(), args[1].GetText()) + return res, err + + default: + return pcommon.NewValueEmpty(), fmt.Errorf("function %q isn't available", functionName) + + } +} + +func (v *SQLStreamVisitor) VisitSimpleCondition(ctx *SimpleConditionContext) interface{} { + return ctx.SimpleExpr().Accept(v) +} + +func (v *SQLStreamVisitor) VisitSimpleExpression(ctx *SimpleExpressionContext) interface{} { + fieldValue, ok := v.currentOriginalRecord.Attributes().Get(ctx.IDENTIFIER().GetText()) + if !ok { + return fmt.Errorf("field %q missed in log record", ctx.IDENTIFIER().GetText()) + } + + res, err := compareExpression(ctx.ComparisonOperator(), ctx.LiteralValue(), fieldValue) + if err != nil { + return err + } + return res + +} + +func (v *SQLStreamVisitor) VisitNestedExpression(ctx *NestedExpressionContext) interface{} { + fieldName := ctx.IDENTIFIER(0).GetText() + fieldValue, ok := v.currentOriginalRecord.Attributes().Get(fieldName) + if !ok { + return fmt.Errorf("field %q missed in log record", fieldValue.AsString()) + } + if fieldValue.Type() != pcommon.ValueTypeMap { + return fmt.Errorf("field %q is not nested map", fieldName) + } + + nestedFieldName := ctx.IDENTIFIER(1).GetText() + nestedFieldValue, ok := fieldValue.MapVal().Get(nestedFieldName) + if !ok { + v.logger.Error("nested key missed", zap.String("name: ", nestedFieldName)) + return false + } + + res, err := compareExpression(ctx.ComparisonOperator(), ctx.LiteralValue(), nestedFieldValue) + if err != nil { + return err + } + return res + +} + +func (v *SQLStreamVisitor) VisitSimpleRecursiveCondition(ctx *SimpleRecursiveConditionContext) interface{} { + + left := ctx.Expr(0).Accept(v) + switch left := ctx.Expr(0).Accept(v).(type) { + case error: + return left + case bool: + } + + right := ctx.Expr(1).Accept(v) + switch right.(type) { + case error: + return right + case bool: + } + + if ctx.K_OR() != nil { + if left == true || right == true { + return true + } + return false + } + if left == true && right == true { + return true + } + return false +} + +func (v *SQLStreamVisitor) VisitWindowTumbling(ctx *WindowTumblingContext) interface{} { + return nil +} +func (v *SQLStreamVisitor) VisitCompoundRecursiveCondition(ctx *CompoundRecursiveConditionContext) interface{} { + var left, right interface{} + + if ctx.CompoundExpr(0) != nil { + left = ctx.CompoundExpr(0).Accept(v) + if ctx.CompoundExpr(1) != nil { + right = ctx.CompoundExpr(1).Accept(v) + } else { + right = ctx.SimpleExpr(0).Accept(v) + } + } else { + left = ctx.SimpleExpr(0).Accept(v) + if ctx.SimpleExpr(1) != nil { + right = ctx.SimpleExpr(1).Accept(v) + } else { + right = ctx.CompoundExpr(1).Accept(v) + } + + } + + switch left.(type) { + case error: + return left + case bool: + } + + switch right.(type) { + case error: + return left + case bool: + } + + if ctx.K_OR() != nil { + if left == true || right == true { + return true + } + return false + } + if left == true && right == true { + return true + } + return false +} + +func (v *SQLStreamVisitor) VisitSimpleCompoundCondition(ctx *SimpleCompoundConditionContext) interface{} { + return ctx.CompoundExpr().Accept(v) +} + +func (v *SQLStreamVisitor) VisitCompoundExpression(ctx *CompoundExpressionContext) interface{} { + return ctx.Expr().Accept(v) +} + +func (v *SQLStreamVisitor) VisitComparisonOperator(ctx *ComparisonOperatorContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *SQLStreamVisitor) VisitLiteralValue(ctx *LiteralValueContext) interface{} { + return v.VisitChildren(ctx) +} + +func (v *SQLStreamVisitor) VisitAlias(ctx *AliasContext) interface{} { + return nil +} + +func (v *SQLStreamVisitor) VisitFunction(ctx *FunctionContext) interface{} { + return nil +} + +func (v *SQLStreamVisitor) VisitFunctionName(ctx *FunctionNameContext) interface{} { + return nil +} diff --git a/processor/stream_processor/parser/sql_stream_visitor_test.go b/processor/stream_processor/parser/sql_stream_visitor_test.go new file mode 100644 index 000000000000..b49626c70987 --- /dev/null +++ b/processor/stream_processor/parser/sql_stream_visitor_test.go @@ -0,0 +1,474 @@ +package parser + +import ( + "encoding/csv" + "errors" + "fmt" + "os" + "strconv" + "testing" + + "go.uber.org/zap" + + "github.com/stretchr/testify/assert" + "go.opentelemetry.io/collector/pdata/plog" +) + +func TestResultColumnsSelectColumns(t *testing.T) { + + tests := []struct { + name string + query string + expectedCount int + err error + }{ + { + name: "correct value columns", + query: `SELECT name, price, is_alive;`, + expectedCount: 100, + err: nil, + }, + { + name: "incorrect value columns", + query: `SELECT field, is_alive;`, + err: errors.New(`field "field" missed`), + expectedCount: 0, + }, + { + name: "one value columns", + query: `SELECT name ;`, + err: nil, + expectedCount: 100, + }, + { + name: "one incorrect value columns", + query: `SELECT field ;`, + err: errors.New(`field "field" missed`), + expectedCount: 0, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + in := make(chan plog.LogRecordSlice) + out := make(chan plog.LogRecordSlice) + outErr := make(chan error) + visitor := NewSQLStreamVisitor(tt.query, in, out, outErr, zap.NewNop()) + defer visitor.Stop() + in <- GenerateTestLogs() + var ls plog.LogRecordSlice + var err error + select { + case ls = <-out: + case err = <-outErr: + } + + assert.Equal(t, tt.err, err) + if tt.err == nil { + assert.Equal(t, tt.expectedCount, ls.Len()) + } + }) + } +} + +func TestResultColumnsSelectColumnsAttributes(t *testing.T) { + tests := []struct { + name string + query string + expectedAttr []string + expectedCount int + }{ + { + name: "one attributes must be left", + query: `SELECT name;`, + expectedAttr: []string{"name"}, + expectedCount: 100, + }, + { + name: "two attributes must be left", + query: `SELECT price, is_alive ;`, + expectedAttr: []string{"price", "is_alive"}, + expectedCount: 100, + }, + { + name: "as clause", + query: `SELECT price as NewPrice, is_alive as DEAD ;`, + expectedAttr: []string{"NewPrice", "DEAD"}, + expectedCount: 100, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + in := make(chan plog.LogRecordSlice) + out := make(chan plog.LogRecordSlice) + outErr := make(chan error) + visitor := NewSQLStreamVisitor(tt.query, in, out, outErr, zap.NewNop()) + defer visitor.Stop() + in <- GenerateTestLogs() + + ls := <-out + assert.Equal(t, tt.expectedCount, ls.Len()) + + for i := 0; i < ls.Len(); i++ { + rec := ls.At(i) + assert.Equal(t, len(tt.expectedAttr), rec.Attributes().Len()) + for _, expectedAttr := range tt.expectedAttr { + _, ok := rec.Attributes().Get(expectedAttr) + assert.True(t, ok) + } + } + }) + } +} + +func TestWhereCondition(t *testing.T) { + tests := []struct { + name string + query string + expected error + }{ + + { + name: "where fields exists ", + query: `SELECT name, is_alive WHERE name = 'test' and is_alive = 'true';`, + expected: nil, + }, + + { + name: "where fields missed ", + query: `SELECT name, is_alive WHERE name = 'test' and non_exists = 'true';`, + expected: errors.New(`field "non_exists" missed in log record`), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + in := make(chan plog.LogRecordSlice) + out := make(chan plog.LogRecordSlice) + outErr := make(chan error) + visitor := NewSQLStreamVisitor(tt.query, in, out, outErr, zap.NewNop()) + defer visitor.Stop() + in <- GenerateTestLogs() + var err error + + select { + case <-out: + return + case err = <-outErr: + } + + assert.Equal(t, tt.expected, err) + }) + } + +} + +func TestSimpleCondition(t *testing.T) { + tests := []struct { + name string + query string + expectedAttr []string + expectedCount int + }{ + { + name: "equal condition ", + query: `SELECT name WHERE name = 'Test name 50';`, + expectedAttr: []string{"name"}, + expectedCount: 1, + }, + { + name: "greater condition ", + query: `SELECT name WHERE name > 'Test name 50';`, + expectedAttr: []string{"name"}, + expectedCount: 53, + }, + { + name: "not equal condition ", + query: `SELECT name WHERE name != 'Test name 50';`, + expectedAttr: []string{"name"}, + expectedCount: 99, + }, + { + name: "greater numeric ", + query: `SELECT name, price WHERE price > 20;`, + expectedAttr: []string{"name"}, + expectedCount: 79, + }, + { + name: "greater equal numeric ", + query: `SELECT name, price WHERE price >= 20;`, + expectedAttr: []string{"name"}, + expectedCount: 80, + }, + { + name: "less numeric ", + query: `SELECT name, price WHERE price < 20;`, + expectedAttr: []string{"name", "price"}, + expectedCount: 20, + }, + { + name: "less equal numeric ", + query: `SELECT name, price WHERE price <= 20;`, + expectedAttr: []string{"name"}, + expectedCount: 21, + }, + { + name: "equal true ", + query: `SELECT name, is_alive WHERE is_alive = 'true';`, + expectedAttr: []string{"name, price"}, + expectedCount: 50, + }, + { + name: "not equal true ", + query: `SELECT name, is_alive WHERE is_alive != 'true';`, + expectedAttr: []string{"name, is_alive"}, + expectedCount: 50, + }, + { + name: "equal false ", + query: `SELECT name, is_alive WHERE is_alive = 'false';`, + expectedAttr: []string{"name, is_alive"}, + expectedCount: 50, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + in := make(chan plog.LogRecordSlice) + out := make(chan plog.LogRecordSlice) + outErr := make(chan error) + visitor := NewSQLStreamVisitor(tt.query, in, out, outErr, zap.NewNop()) + defer visitor.Stop() + in <- GenerateTestLogs() + ls := <-out + assert.Equal(t, tt.expectedCount, ls.Len()) + + }) + } + +} + +func TestRecursiveCondition(t *testing.T) { + tests := []struct { + name string + query string + expectedCount int + }{ + { + name: "where and condition ", + query: `SELECT name, is_alive WHERE name != 'test' and is_alive = 'true';`, + expectedCount: 50, + }, + { + name: "where or 1 ", + query: `SELECT name, is_alive WHERE name != 'test' or is_alive = 'true';`, + expectedCount: 100, + }, + { + name: "test or 2", + query: `SELECT name WHERE name = 'Test name 10' or is_alive = 'false';`, + expectedCount: 51, + }, + { + name: "test and 1", + query: `SELECT name WHERE name = 'Test name 10' and is_alive = 'false';`, + expectedCount: 0, + }, + { + name: "test and 2", + query: `SELECT name WHERE name = 'Test name 10' and is_alive = 'true';`, + expectedCount: 1, + }, + { + name: "test and 3", + query: `SELECT name WHERE name = 'Test name 10' and price = 10;`, + expectedCount: 1, + }, + { + name: "test and 4", + query: `SELECT name WHERE name = 'Test name 10' and price = 9;`, + expectedCount: 0, + }, + { + name: "like 1", + query: `SELECT name WHERE name like 'Test name 1' and is_alive = 'false';`, + expectedCount: 6, + }, + { + name: "like 2", + query: `SELECT name WHERE name like 'Test name' and price > 70';`, + expectedCount: 29, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + in := make(chan plog.LogRecordSlice) + out := make(chan plog.LogRecordSlice) + outErr := make(chan error) + visitor := NewSQLStreamVisitor(tt.query, in, out, outErr, zap.NewNop()) + defer visitor.Stop() + in <- GenerateTestLogs() + ls := <-out + assert.Equal(t, tt.expectedCount, ls.Len()) + }) + } + +} + +func TestCompoundCondition(t *testing.T) { + tests := []struct { + name string + query string + expectedCount int + }{ + { + name: "and 1 ", + query: `SELECT name, is_alive WHERE (name != 'test' and is_alive = 'true') and (is_alive = 'true' and price > 50);`, + expectedCount: 24, + }, + { + name: "and 2 ", + query: `SELECT name, is_alive WHERE (name = 'Test name 10' and price > 5) and (is_alive = 'true' and price > 5);`, + expectedCount: 1, + }, + { + name: "and 3 ", + query: `SELECT name, is_alive WHERE (name = 'Test name 10' and price > 5) and (is_alive = 'false' and price > 5);`, + expectedCount: 0, + }, + { + name: "or 1 ", + query: `SELECT name, is_alive WHERE (name != 'test' and is_alive = 'true') or (is_alive = 'true' and price > 50);`, + expectedCount: 50, + }, + { + name: "or 2 ", + query: `SELECT name, is_alive WHERE (name = 'Test name 10' and price > 5 ) or (is_alive = 'true');`, + expectedCount: 50, + }, + { + name: "or 2 ", + query: `SELECT name, is_alive WHERE (name = 'Test name 10' and price > 5 ) or (is_alive = 'false');`, + expectedCount: 51, + }, + { + name: "like 1 ", + query: `SELECT name, is_alive WHERE (name like '2' and price > 5 ) or (is_alive = 'false');`, + expectedCount: 63, + }, + { + name: "like 2 ", + query: `SELECT name, is_alive WHERE (name like '2' and price > 5 ) or (price < 30 or is_alive = 'false');`, + expectedCount: 72, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + in := make(chan plog.LogRecordSlice) + out := make(chan plog.LogRecordSlice) + outErr := make(chan error) + visitor := NewSQLStreamVisitor(tt.query, in, out, outErr, zap.NewNop()) + defer visitor.Stop() + in <- GenerateTestLogs() + ls := <-out + assert.Equal(t, tt.expectedCount, ls.Len()) + }) + } + +} + +func TestComplexCompoundCondition(t *testing.T) { + tests := []struct { + name string + query string + expectedCount int + }{ + { + name: "3 compounds ", + query: `SELECT * WHERE (name like '2' and price < 3) or (name like '3' and price = 3) or (name = 'Test name 4' );`, + expectedCount: 3, + }, + { + name: "3 compounds without brackets", + query: `SELECT * WHERE (name like '2' and price < 3) or (name like '3' and price = 3) or name = 'Test name 4' ;`, + expectedCount: 3, + }, + { + name: "3 and compounds", + query: `SELECT * WHERE (name like '2' and price < 3) or (name like '3' and price = 3) or (name = 'Test name 4' and price = 4) ;`, + expectedCount: 3, + }, + { + name: "4 compounds", + query: `SELECT * WHERE (name like '2' and price < 3) or (name like '3' and price = 3) or (name = 'Test name 4' and price = 4) or (name = 'Test name 5' and price = 5) ;`, + expectedCount: 4, + }, + { + name: "5 compounds", + query: `SELECT * WHERE (name like '2' and price < 3) or (name like '3' and price = 3) or (name = 'Test name 4' and price = 4) or (name = 'Test name 5' and price = 5) or (name = 'Test name 6' and price = 6) ;`, + expectedCount: 5, + }, + { + name: "5 compounds", + query: `SELECT * WHERE (name like '2' and price < 3) or (name like '3' and price = 3) or (name = 'Test name 4' and price = 4) or (name = 'Test name 5' and price = 5) and (name = 'Test name 6' and price = 6) ;`, + expectedCount: 0, + }, + { + name: "3 and", + query: `SELECT * WHERE name like '2' and price < 3 and is_alive = 'true' ;`, + expectedCount: 1, + }, + { + name: "3 and", + query: `SELECT * WHERE name like '2' and price < 3 and is_alive != 'true' ;`, + expectedCount: 0, + }, + { + name: "3 and", + query: `SELECT * WHERE (name like '2' and price < 3 and is_alive = 'true') ;`, + expectedCount: 1, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + in := make(chan plog.LogRecordSlice) + out := make(chan plog.LogRecordSlice) + outErr := make(chan error) + visitor := NewSQLStreamVisitor(tt.query, in, out, outErr, zap.NewNop()) + defer visitor.Stop() + in <- GenerateTestLogs() + ls := <-out + assert.Equal(t, tt.expectedCount, ls.Len()) + }) + } + +} + +func TestWriteTestLogsToCSV(t *testing.T) { + t.Skip() + f, _ := os.Create("test.csv") + w := csv.NewWriter(f) + defer w.Flush() + for i := 0; i < 100; i++ { + isAlive := strconv.FormatBool(i%2 == 0) + err := w.Write([]string{"Test name " + strconv.Itoa(i), isAlive, strconv.Itoa(i)}) + assert.Nil(t, err) + + } + +} + +func TestWriteTestLogsGroupByToCSV(t *testing.T) { + t.Skip() + f, _ := os.Create("testGroupBy.csv") + w := csv.NewWriter(f) + defer w.Flush() + for i := 0; i < 100; i++ { + isAlive := strconv.FormatBool(i%2 == 0) + name := strconv.Itoa(i) + err := w.Write([]string{fmt.Sprint("Test name ", string(name[0])), isAlive, strconv.Itoa(i)}) + assert.Nil(t, err) + + } + +} diff --git a/processor/stream_processor/parser/sql_visitor.go b/processor/stream_processor/parser/sql_visitor.go new file mode 100644 index 000000000000..99f50df34842 --- /dev/null +++ b/processor/stream_processor/parser/sql_visitor.go @@ -0,0 +1,91 @@ +// Code generated from Sql.g4 by ANTLR 4.10.1. DO NOT EDIT. + +package parser // Sql + +import "github.com/antlr/antlr4/runtime/Go/antlr" + +// A complete Visitor for a parse tree produced by SqlParser. +type SqlVisitor interface { + antlr.ParseTreeVisitor + + // Visit a parse tree produced by SqlParser#sqlQuery. + VisitSqlQuery(ctx *SqlQueryContext) interface{} + + // Visit a parse tree produced by SqlParser#selectSimple. + VisitSelectSimple(ctx *SelectSimpleContext) interface{} + + // Visit a parse tree produced by SqlParser#selectTumbling. + VisitSelectTumbling(ctx *SelectTumblingContext) interface{} + + // Visit a parse tree produced by SqlParser#selectTumblingGroupBy. + VisitSelectTumblingGroupBy(ctx *SelectTumblingGroupByContext) interface{} + + // Visit a parse tree produced by SqlParser#windowTumbling. + VisitWindowTumbling(ctx *WindowTumblingContext) interface{} + + // Visit a parse tree produced by SqlParser#selectColumns. + VisitSelectColumns(ctx *SelectColumnsContext) interface{} + + // Visit a parse tree produced by SqlParser#selectStar. + VisitSelectStar(ctx *SelectStarContext) interface{} + + // Visit a parse tree produced by SqlParser#selectAggregations. + VisitSelectAggregations(ctx *SelectAggregationsContext) interface{} + + // Visit a parse tree produced by SqlParser#selectGroupByAggregations. + VisitSelectGroupByAggregations(ctx *SelectGroupByAggregationsContext) interface{} + + // Visit a parse tree produced by SqlParser#aggregationColumn. + VisitAggregationColumn(ctx *AggregationColumnContext) interface{} + + // Visit a parse tree produced by SqlParser#identifierColumn. + VisitIdentifierColumn(ctx *IdentifierColumnContext) interface{} + + // Visit a parse tree produced by SqlParser#functionColumn. + VisitFunctionColumn(ctx *FunctionColumnContext) interface{} + + // Visit a parse tree produced by SqlParser#alias. + VisitAlias(ctx *AliasContext) interface{} + + // Visit a parse tree produced by SqlParser#simpleFunction. + VisitSimpleFunction(ctx *SimpleFunctionContext) interface{} + + // Visit a parse tree produced by SqlParser#recursiveFunction. + VisitRecursiveFunction(ctx *RecursiveFunctionContext) interface{} + + // Visit a parse tree produced by SqlParser#functionName. + VisitFunctionName(ctx *FunctionNameContext) interface{} + + // Visit a parse tree produced by SqlParser#whereStmt. + VisitWhereStmt(ctx *WhereStmtContext) interface{} + + // Visit a parse tree produced by SqlParser#simpleCondition. + VisitSimpleCondition(ctx *SimpleConditionContext) interface{} + + // Visit a parse tree produced by SqlParser#compoundRecursiveCondition. + VisitCompoundRecursiveCondition(ctx *CompoundRecursiveConditionContext) interface{} + + // Visit a parse tree produced by SqlParser#simpleCompoundCondition. + VisitSimpleCompoundCondition(ctx *SimpleCompoundConditionContext) interface{} + + // Visit a parse tree produced by SqlParser#simpleRecursiveCondition. + VisitSimpleRecursiveCondition(ctx *SimpleRecursiveConditionContext) interface{} + + // Visit a parse tree produced by SqlParser#simpleExpression. + VisitSimpleExpression(ctx *SimpleExpressionContext) interface{} + + // Visit a parse tree produced by SqlParser#nestedExpression. + VisitNestedExpression(ctx *NestedExpressionContext) interface{} + + // Visit a parse tree produced by SqlParser#compoundExpression. + VisitCompoundExpression(ctx *CompoundExpressionContext) interface{} + + // Visit a parse tree produced by SqlParser#comparisonOperator. + VisitComparisonOperator(ctx *ComparisonOperatorContext) interface{} + + // Visit a parse tree produced by SqlParser#literalValue. + VisitLiteralValue(ctx *LiteralValueContext) interface{} + + // Visit a parse tree produced by SqlParser#groupBy. + VisitGroupBy(ctx *GroupByContext) interface{} +} diff --git a/processor/stream_processor/parser/test.csv b/processor/stream_processor/parser/test.csv new file mode 100644 index 000000000000..0605e44aa48f --- /dev/null +++ b/processor/stream_processor/parser/test.csv @@ -0,0 +1,100 @@ +Test name 0,true,0 +Test name 1,false,1 +Test name 2,true,2 +Test name 3,false,3 +Test name 4,true,4 +Test name 5,false,5 +Test name 6,true,6 +Test name 7,false,7 +Test name 8,true,8 +Test name 9,false,9 +Test name 10,true,10 +Test name 11,false,11 +Test name 12,true,12 +Test name 13,false,13 +Test name 14,true,14 +Test name 15,false,15 +Test name 16,true,16 +Test name 17,false,17 +Test name 18,true,18 +Test name 19,false,19 +Test name 20,true,20 +Test name 21,false,21 +Test name 22,true,22 +Test name 23,false,23 +Test name 24,true,24 +Test name 25,false,25 +Test name 26,true,26 +Test name 27,false,27 +Test name 28,true,28 +Test name 29,false,29 +Test name 30,true,30 +Test name 31,false,31 +Test name 32,true,32 +Test name 33,false,33 +Test name 34,true,34 +Test name 35,false,35 +Test name 36,true,36 +Test name 37,false,37 +Test name 38,true,38 +Test name 39,false,39 +Test name 40,true,40 +Test name 41,false,41 +Test name 42,true,42 +Test name 43,false,43 +Test name 44,true,44 +Test name 45,false,45 +Test name 46,true,46 +Test name 47,false,47 +Test name 48,true,48 +Test name 49,false,49 +Test name 50,true,50 +Test name 51,false,51 +Test name 52,true,52 +Test name 53,false,53 +Test name 54,true,54 +Test name 55,false,55 +Test name 56,true,56 +Test name 57,false,57 +Test name 58,true,58 +Test name 59,false,59 +Test name 60,true,60 +Test name 61,false,61 +Test name 62,true,62 +Test name 63,false,63 +Test name 64,true,64 +Test name 65,false,65 +Test name 66,true,66 +Test name 67,false,67 +Test name 68,true,68 +Test name 69,false,69 +Test name 70,true,70 +Test name 71,false,71 +Test name 72,true,72 +Test name 73,false,73 +Test name 74,true,74 +Test name 75,false,75 +Test name 76,true,76 +Test name 77,false,77 +Test name 78,true,78 +Test name 79,false,79 +Test name 80,true,80 +Test name 81,false,81 +Test name 82,true,82 +Test name 83,false,83 +Test name 84,true,84 +Test name 85,false,85 +Test name 86,true,86 +Test name 87,false,87 +Test name 88,true,88 +Test name 89,false,89 +Test name 90,true,90 +Test name 91,false,91 +Test name 92,true,92 +Test name 93,false,93 +Test name 94,true,94 +Test name 95,false,95 +Test name 96,true,96 +Test name 97,false,97 +Test name 98,true,98 +Test name 99,false,99 diff --git a/processor/stream_processor/parser/test_utils.go b/processor/stream_processor/parser/test_utils.go new file mode 100644 index 000000000000..922286d940a5 --- /dev/null +++ b/processor/stream_processor/parser/test_utils.go @@ -0,0 +1,29 @@ +package parser + +import ( + "go.opentelemetry.io/collector/pdata/pcommon" + "go.opentelemetry.io/collector/pdata/plog" + "strconv" +) + +func GenerateTestLogs() plog.LogRecordSlice { + + ld := plog.NewLogs() + sc := ld.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty() + types := []string{"middle", "small", "big"} + + for i := 0; i < 100; i++ { + record := sc.LogRecords().AppendEmpty() + record.Attributes().InsertString("name", "Test name "+strconv.Itoa(i)) + record.Attributes().InsertBool("is_alive", i%2 == 0) + record.Attributes().InsertInt("price", int64(i)) + nested := pcommon.NewValueMap() + typeIndex := i % 3 + nested.MapVal().InsertString("source", "Source "+strconv.Itoa(i)) + nested.MapVal().InsertString("type", types[typeIndex]) + nested.MapVal().InsertDouble("number", float64(i)) + record.Attributes().Insert("provider", nested) + } + + return ld.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords() +} diff --git a/processor/stream_processor/parser/visitor_utils.go b/processor/stream_processor/parser/visitor_utils.go new file mode 100644 index 000000000000..971260faefb7 --- /dev/null +++ b/processor/stream_processor/parser/visitor_utils.go @@ -0,0 +1,598 @@ +package parser + +import ( + "fmt" + "strconv" + "strings" + + "go.opentelemetry.io/collector/pdata/pcommon" + "go.opentelemetry.io/collector/pdata/plog" +) + +const ( + lower = "lower" + upper = "upper" + substr = "substr" +) + +func compareExpression(operator IComparisonOperatorContext, literalValue ILiteralValueContext, fieldValue pcommon.Value) (bool, error) { + switch literalValue.GetStart().GetTokenType() { + case SqlParserNUMERIC_LITERAL: + parsedValue, err := strconv.ParseFloat(fieldValue.AsString(), 64) + if err != nil { + return false, fmt.Errorf("can't convert record field value %q to numeric; %w", fieldValue.AsString(), err) + } + comparisonValue, err := strconv.ParseFloat(literalValue.GetText(), 64) + if err != nil { + return false, fmt.Errorf("can't convert comparison value %q to numeric; %w", literalValue.GetText(), err) + } + return compareNumeric(operator.GetStart().GetTokenType(), parsedValue, comparisonValue), nil + case SqlParserSTRING_LITERAL: + // we need to remove quotes + value := strings.TrimSuffix(strings.TrimPrefix(literalValue.GetText(), `'`), `'`) + return compareString(operator.GetStart().GetTokenType(), fieldValue.AsString(), value), nil + case SqlParserBOOLEAN_LITERAL: + parsedValue, err := strconv.ParseBool(fieldValue.AsString()) + if err != nil { + return false, fmt.Errorf("can't convert field value %q to boolean; %w", fieldValue.AsString(), err) + } + comparisonValue, err := strconv.ParseBool(literalValue.GetText()) + if err != nil { + return false, fmt.Errorf("can't convert comparison value %q to boolean; %w", literalValue.GetText(), err) + } + + return compareBool(operator.GetStart().GetTokenType(), parsedValue, comparisonValue), nil + + default: + return false, fmt.Errorf("missed literal value type %q", literalValue.GetText()) + } +} + +func compareString(comparisonToken int, fieldVal, comparisonVal string) bool { + switch comparisonToken { + case SqlParserK_EQUAL: + return fieldVal == comparisonVal + case SqlParserK_NOT_EQUAL: + return fieldVal != comparisonVal + case SqlParserK_GREATER: + return fieldVal > comparisonVal + case SqlParserK_GREATER_EQUAL: + return fieldVal >= comparisonVal + case SqlParserK_LESS: + return fieldVal < comparisonVal + case SqlParserK_LESS_EQUAL: + return fieldVal <= comparisonVal + case SqlParserK_LIKE: + return strings.Contains(fmt.Sprint(fieldVal), fmt.Sprint(comparisonVal)) + case SqlParserK_NOT_LIKE: + return !strings.Contains(fmt.Sprint(fieldVal), fmt.Sprint(comparisonVal)) + case SqlParserK_IS_NULL: + return len(fmt.Sprint(fieldVal)) == 0 + case SqlParserK_IS_NOT_NULL: + return len(fmt.Sprint(fieldVal)) != 0 + default: + return false + } +} + +func compareNumeric(comparisonToken int, fieldVal, comparisonVal float64) bool { + switch comparisonToken { + case SqlParserK_EQUAL: + return fieldVal == comparisonVal + case SqlParserK_NOT_EQUAL: + return fieldVal != comparisonVal + case SqlParserK_GREATER: + return fieldVal > comparisonVal + case SqlParserK_GREATER_EQUAL: + return fieldVal >= comparisonVal + case SqlParserK_LESS: + return fieldVal < comparisonVal + case SqlParserK_LESS_EQUAL: + return fieldVal <= comparisonVal + case SqlParserK_IS_NULL: + return fieldVal == 0 + case SqlParserK_IS_NOT_NULL: + return fieldVal != 0 + default: + return false + } +} + +func compareBool(comparisonToken int, fieldVal, comparisonVal bool) bool { + switch comparisonToken { + case SqlParserK_EQUAL: + return fieldVal == comparisonVal + case SqlParserK_NOT_EQUAL: + return fieldVal != comparisonVal + } + return false +} + +func getFieldAggregatedValue(ctx *AggregationColumnContext, ls plog.LogRecordSlice) (float64, error) { + + var res float64 + var err error + + if ctx.K_AVG() != nil { + res, err = avg(ls, ctx) + } + if ctx.K_SUM() != nil { + res, err = sum(ls, ctx) + } + if ctx.K_COUNT() != nil { + res = float64(count(ls)) + } + if ctx.K_MAX() != nil { + res, err = max(ls, ctx) + } + if ctx.K_MIN() != nil { + res, err = min(ls, ctx) + } + + return res, err +} + +func insertIdentifierColumnToRecord(ctx *IdentifierColumnContext, attr pcommon.Map, value string) error { + if ctx.Alias() != nil { + attr.Insert(ctx.Alias().GetStop().GetText(), pcommon.NewValueString(value)) + return nil + } + + fieldName := ctx.IDENTIFIER(0).GetText() + if len(ctx.AllIDENTIFIER()) > 1 { + nested, ok := attr.Get(fieldName) + + if ok && nested.Type() != pcommon.ValueTypeMap { + return fmt.Errorf("field %q already exists and isn't nested", fieldName) + } + + if !ok { + nested = pcommon.NewValueMap() + nested.MapVal().Insert(ctx.IDENTIFIER(1).GetText(), pcommon.NewValueString(value)) + } + + attr.Insert(fieldName, nested) + return nil + } + + _, ok := attr.Get(fieldName) + if ok { + return fmt.Errorf("field %q is duplicated. Use an alias", fieldName) + } + attr.Insert(fieldName, pcommon.NewValueString(value)) + + return nil +} +func insertAggregationColumnToRecord(ctx *AggregationColumnContext, attr pcommon.Map, value float64) error { + if ctx.Alias() != nil { + attr.Insert(ctx.Alias().GetStop().GetText(), pcommon.NewValueDouble(value)) + return nil + } + + fieldName := ctx.IDENTIFIER(0).GetText() + if len(ctx.AllIDENTIFIER()) > 1 { + nested, ok := attr.Get(fieldName) + + if ok && nested.Type() != pcommon.ValueTypeMap { + return fmt.Errorf("field %q already exists and isn't nested", fieldName) + } + + if !ok { + nested = pcommon.NewValueMap() + nested.MapVal().Insert(ctx.IDENTIFIER(1).GetText(), pcommon.NewValueDouble(value)) + } + + attr.Insert(fieldName, nested) + return nil + } + + _, ok := attr.Get(fieldName) + if ok { + return fmt.Errorf("field %q is duplicated. Use an alias", fieldName) + } + attr.Insert(fieldName, pcommon.NewValueDouble(value)) + + return nil +} + +func sum(ls plog.LogRecordSlice, ctx *AggregationColumnContext) (float64, error) { + var sum float64 + for i := 0; i < ls.Len(); i++ { + curRec := ls.At(i) + _, val, err := getAttributeValueForAggregation(ctx, curRec.Attributes()) + if err != nil { + return 0, err + } + convertedVal, err := strconv.ParseFloat(val.AsString(), 64) + if err != nil { + return 0.0, err + } + sum += convertedVal + } + + return sum, nil +} + +func min(ls plog.LogRecordSlice, ctx *AggregationColumnContext) (float64, error) { + var conErr error + ls.Sort(func(a, b plog.LogRecord) bool { + _, leftVal, err := getAttributeValueForAggregation(ctx, a.Attributes()) + if err != nil { + conErr = err + } + + _, rightVal, err := getAttributeValueForAggregation(ctx, b.Attributes()) + + if err != nil { + conErr = err + } + + switch leftVal.Type() { + case pcommon.ValueTypeInt: + return leftVal.IntVal() < rightVal.IntVal() + case pcommon.ValueTypeDouble: + return leftVal.DoubleVal() < rightVal.DoubleVal() + + } + + return false + }) + + if ls.Len() > 0 { + _, res, err := getAttributeValueForAggregation(ctx, ls.At(0).Attributes()) + convertedRes, _ := strconv.ParseFloat(res.AsString(), 64) + return convertedRes, err + } + + return 0, conErr +} + +func max(ls plog.LogRecordSlice, ctx *AggregationColumnContext) (float64, error) { + var conErr error + sorted := ls.Sort(func(a, b plog.LogRecord) bool { + _, leftVal, err := getAttributeValueForAggregation(ctx, a.Attributes()) + if err != nil { + conErr = err + } + + _, rightVal, err := getAttributeValueForAggregation(ctx, b.Attributes()) + if err != nil { + conErr = err + } + + switch leftVal.Type() { + case pcommon.ValueTypeInt: + return leftVal.IntVal() > rightVal.IntVal() + case pcommon.ValueTypeDouble: + return leftVal.DoubleVal() > rightVal.DoubleVal() + + } + + return false + }) + + if sorted.Len() > 0 { + _, res, err := getAttributeValueForAggregation(ctx, sorted.At(0).Attributes()) + convertedRes, _ := strconv.ParseFloat(res.AsString(), 64) + return convertedRes, err + } + + return 0.0, conErr +} + +func avg(ls plog.LogRecordSlice, ctx *AggregationColumnContext) (float64, error) { + var sum float64 + for i := 0; i < ls.Len(); i++ { + curRec := ls.At(i) + _, val, err := getAttributeValueForAggregation(ctx, curRec.Attributes()) + if err != nil { + return 0, err + } + convertedVal, err := strconv.ParseFloat(val.AsString(), 64) + if err != nil { + return 0.0, err + } + sum += convertedVal + } + return sum / float64(ls.Len()), nil +} + +func count(ls plog.LogRecordSlice) int { + return ls.Len() +} + +// check if we need remove attribute missed in select list +func fieldExists(key string, value pcommon.Value, allColumns []interface{}) bool { + //simple attribute + if value.Type() != pcommon.ValueTypeMap { + for _, col := range allColumns { + var id string + switch typedCol := col.(type) { + case *IdentifierColumnContext: + id = typedCol.IDENTIFIER(0).GetText() + case *SimpleFunctionContext: + id = typedCol.IDENTIFIER(0).GetText() + } + if key == id { + return false + } + } + return true + } + + //find if nested attr existed in select list + for _, col := range allColumns { + switch typedCol := col.(type) { + case *IdentifierColumnContext: + if len(typedCol.AllIDENTIFIER()) == 1 { + if key == typedCol.IDENTIFIER(0).GetText() { + return false + } + } + case *SimpleFunctionContext: + if len(typedCol.AllIDENTIFIER()) == 1 { + if key == typedCol.IDENTIFIER(0).GetText() { + return false + } + } + } + } + + nestedFound := false + //now check is we have only nested field in select list e.g. field.nestedField + value.MapVal().RemoveIf(func(nestedKey string, value pcommon.Value) bool { + for _, col := range allColumns { + switch typedCol := col.(type) { + case *IdentifierColumnContext: + if len(typedCol.AllIDENTIFIER()) > 1 { + if key == typedCol.IDENTIFIER(0).GetText() && nestedKey == typedCol.IDENTIFIER(1).GetText() { + nestedFound = true + return false + } + } + case *SimpleFunctionContext: + if len(typedCol.AllIDENTIFIER()) > 1 { + if key == typedCol.IDENTIFIER(0).GetText() && nestedKey == typedCol.IDENTIFIER(1).GetText() { + nestedFound = true + return false + } + } + } + } + return true + }) + + return !nestedFound +} + +func getAttributeValueForAggregation(ctx *AggregationColumnContext, attr pcommon.Map) (string, pcommon.Value, error) { + if len(ctx.AllIDENTIFIER()) > 1 { + value, err := nestedFieldExistsInAttr(ctx.IDENTIFIER(0).GetText(), ctx.IDENTIFIER(1).GetText(), attr) + return ctx.IDENTIFIER(0).GetText() + "," + ctx.IDENTIFIER(1).GetText(), value, err + } + value, err := fieldExistsInAttr(ctx.IDENTIFIER(0).GetText(), attr) + return ctx.IDENTIFIER(0).GetText(), value, err + +} + +func getAttributeValue(column IColumnContext, attr pcommon.Map) (string, pcommon.Value, error) { + switch col := column.(type) { + case *IdentifierColumnContext: + if len(col.AllIDENTIFIER()) > 1 { + value, err := nestedFieldExistsInAttr(col.IDENTIFIER(0).GetText(), col.IDENTIFIER(1).GetText(), attr) + return col.IDENTIFIER(0).GetText() + "," + col.IDENTIFIER(1).GetText(), value, err + } + value, err := fieldExistsInAttr(col.IDENTIFIER(0).GetText(), attr) + return col.IDENTIFIER(0).GetText(), value, err + default: + return "", pcommon.NewValueEmpty(), fmt.Errorf("unknown column type") + } + +} + +// check if field exists in attr +func fieldExistsInAttr(fieldName string, attr pcommon.Map) (pcommon.Value, error) { + value, ok := attr.Get(fieldName) + if !ok { + return pcommon.NewValueEmpty(), fmt.Errorf("field %q missed", fieldName) + } + return value, nil +} + +// check is nested field exists +func nestedFieldExistsInAttr(fieldName, nestedFieldName string, attr pcommon.Map) (pcommon.Value, error) { + value, ok := attr.Get(fieldName) + if !ok { + return pcommon.NewValueEmpty(), fmt.Errorf("field %q missed", fieldName) + } + + // if this nested field + if value.Type() != pcommon.ValueTypeMap { + return pcommon.NewValueEmpty(), fmt.Errorf("field %q isn't nested", fieldName) + } + + nestedValue, ok := value.MapVal().Get(nestedFieldName) + if !ok { + return pcommon.NewValueEmpty(), fmt.Errorf("field %q missed nested field %q", fieldName, nestedFieldName) + } + + return nestedValue, nil +} + +func getSelectColumnsFromWhereCtx(ctx *WhereStmtContext) *SelectColumnsContext { + for _, resCtx := range ctx.GetParent().GetChildren() { + resColumnCtx, ok := resCtx.(*SelectColumnsContext) + if !ok { + continue + } + return resColumnCtx + } + + return nil +} + +func getSelectAggregationsFromWhereCtx(ctx *WhereStmtContext) *SelectAggregationsContext { + for _, resCtx := range ctx.GetParent().GetChildren() { + resColumnCtx, ok := resCtx.(*SelectAggregationsContext) + if !ok { + continue + } + return resColumnCtx + } + + return nil +} + +func getSelectStarCtx(ctx *WhereStmtContext) *SelectStarContext { + for _, resCtx := range ctx.GetParent().GetChildren() { + resColumnCtx, ok := resCtx.(*SelectStarContext) + if !ok { + continue + } + return resColumnCtx + } + + return nil +} + +func substring(value pcommon.Value, start, length string) (pcommon.Value, error) { + + startN, err := strconv.Atoi(start) + if err != nil { + return pcommon.NewValueEmpty(), fmt.Errorf("first arg must be numeric") + } + + lengthN, err := strconv.Atoi(length) + if err != nil { + return pcommon.NewValueEmpty(), fmt.Errorf("second arg must be numeric") + } + + runes := []rune(value.AsString()) + if startN > len(runes) { + return pcommon.NewValueString(""), nil + } + + if startN+lengthN > len(runes) { + lengthN = len(runes) - startN + } + + return pcommon.NewValueString(string(runes[startN : startN+lengthN])), nil + +} + +func getSimpleFunctionContext(ctx *FunctionColumnContext) *SimpleFunctionContext { + var simpleFuncCtx *SimpleFunctionContext + for _, childRecCtx := range ctx.Function().GetChildren() { + switch childRecCtx.(type) { + case *SimpleFunctionContext: + simpleFuncCtx = childRecCtx.(*SimpleFunctionContext) + case *RecursiveFunctionContext: + simpleFuncCtx = findSimpleFuncCtx(childRecCtx.(*RecursiveFunctionContext)) + default: + continue + } + } + + return simpleFuncCtx +} + +func findSimpleFuncCtx(ctx *RecursiveFunctionContext) *SimpleFunctionContext { + for _, childCtx := range ctx.GetChildren() { + switch childCtx.(type) { + case *RecursiveFunctionContext: + return findSimpleFuncCtx(childCtx.(*RecursiveFunctionContext)) + case *SimpleFunctionContext: + return childCtx.(*SimpleFunctionContext) + default: + continue + } + } + + return nil +} + +func getGroupByFieldFromAggregationCtx(ctx *SelectGroupByAggregationsContext) (string, error) { + for _, node := range ctx.GetParent().GetChildren() { + groupByCtx, ok := node.(*GroupByContext) + if !ok { + continue + } + idCol, ok := groupByCtx.Column().(*IdentifierColumnContext) + if !ok { + return ",", fmt.Errorf("unknown group by clause field type") + } + return getFieldNameFromIDColumnCtx(idCol), nil + } + + return "", fmt.Errorf("group by field missed") + +} + +func getFieldNameFromIDColumnCtx(col *IdentifierColumnContext) string { + fieldName := col.IDENTIFIER(0).GetText() + if len(col.AllIDENTIFIER()) > 1 { + return fieldName + "." + col.IDENTIFIER(1).GetText() + } + return fieldName +} + +func calculateGroupByAggregations(ctx *SelectGroupByAggregationsContext, key string, rec plog.LogRecord, ls plog.LogRecordSlice) error { + for _, aggregation := range ctx.AllAggregationColumn() { + + col, ok := aggregation.(*AggregationColumnContext) + if !ok { + return fmt.Errorf("unknown aggragation column type") + } + res, err := getFieldAggregatedValue(col, ls) + if err != nil { + return err + } + if err = insertAggregationColumnToRecord(col, rec.Attributes(), res); err != nil { + return err + } + } + + if len(ctx.AllColumn()) == 0 { + return nil + } + + for _, nonAgrCol := range ctx.AllColumn() { + groupByFieldName, err := getGroupByFieldFromAggregationCtx(ctx) + if err != nil { + return err + } + + selectField, ok := nonAgrCol.(*IdentifierColumnContext) + if !ok { + return fmt.Errorf("field %q in select list is inknown type", nonAgrCol.GetText()) + } + + selectFieldName := getFieldNameFromIDColumnCtx(selectField) + if strings.Compare(groupByFieldName, selectFieldName) != 0 { + return fmt.Errorf("you must use the same field for group by %q clause and select list", groupByFieldName) + } + + if err := insertIdentifierColumnToRecord(selectField, rec.Attributes(), key); err != nil { + return err + } + } + + return nil +} + +func calculateAggregations(ctx *SelectAggregationsContext, rec plog.LogRecord, ls plog.LogRecordSlice) error { + for _, aggregation := range ctx.AllAggregationColumn() { + col, ok := aggregation.(*AggregationColumnContext) + if !ok { + return fmt.Errorf("unknown aggragation column type") + } + res, err := getFieldAggregatedValue(col, ls) + if err != nil { + return err + } + if err = insertAggregationColumnToRecord(col, rec.Attributes(), res); err != nil { + return err + } + } + return nil +} diff --git a/processor/stream_processor/parser/windowTumblingVisitor.go b/processor/stream_processor/parser/windowTumblingVisitor.go new file mode 100644 index 000000000000..5cdacee491fe --- /dev/null +++ b/processor/stream_processor/parser/windowTumblingVisitor.go @@ -0,0 +1,64 @@ +package parser + +import ( + "errors" + "strconv" + + "github.com/antlr/antlr4/runtime/Go/antlr" +) + +//This is just to determine if it is window tumbling query and get time period value +//TODO find better way +var _ SqlVisitor = (*TumblingVisitor)(nil) + +type TumblingVisitor struct { + *BaseSqlVisitor +} + +func IsTumblingQuery(query string) (bool, int) { + is := antlr.NewInputStream(query) + lexer := NewSqlLexer(is) + stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel) + stream.Size() + parser := NewSqlParser(stream) + + t := &TumblingVisitor{ + BaseSqlVisitor: &BaseSqlVisitor{}, + } + switch res := t.Visit(parser.SqlQuery()).(type) { + case int: + return true, res + case error: + return false, 0 + } + + return false, 0 +} + +func (v *TumblingVisitor) Visit(tree antlr.ParseTree) interface{} { + return tree.(*SqlQueryContext).Accept(v) +} + +func (v *TumblingVisitor) VisitSqlQuery(ctx *SqlQueryContext) interface{} { + return ctx.SelectQuery().Accept(v) +} + +func (v *TumblingVisitor) VisitSelectTumbling(ctx *SelectTumblingContext) interface{} { + return ctx.WindowTumbling().Accept(v) +} + +func (v *TumblingVisitor) VisitWindowTumbling(ctx *WindowTumblingContext) interface{} { + if ctx.NUMERIC_LITERAL() != nil { + res, _ := strconv.Atoi(ctx.NUMERIC_LITERAL().GetText()) + return res + } + return errors.New("this is not window tumbling query") +} + +func (v *TumblingVisitor) VisitGroupBy(ctx *GroupByContext) interface{} { + return nil +} + +func (v *TumblingVisitor) VisitSelectTumblingGroupBy(ctx *SelectTumblingGroupByContext) interface{} { + return ctx.WindowTumbling().Accept(v) +} diff --git a/processor/stream_processor/processor.go b/processor/stream_processor/processor.go new file mode 100644 index 000000000000..7b70bdb87a2e --- /dev/null +++ b/processor/stream_processor/processor.go @@ -0,0 +1,95 @@ +package stream_processor + +import ( + "context" + "fmt" + "github.com/open-telemetry/opentelemetry-collector-contrib/processor/stream_processor/parser" + "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/consumer" + "go.opentelemetry.io/collector/pdata/pcommon" + "go.opentelemetry.io/collector/pdata/plog" + "go.uber.org/zap" +) + +type sqlStreamProcessor struct { + logger *zap.Logger + config *Config + nextConsumer consumer.Logs + in, out chan plog.LogRecordSlice + outErr chan error + processor *parser.SQLStreamVisitor +} + +func newSqlStreamProcessor(next consumer.Logs, logger *zap.Logger, cfg *Config) *sqlStreamProcessor { + in := make(chan plog.LogRecordSlice) + out := make(chan plog.LogRecordSlice) + outErr := make(chan error) + processor := parser.NewSQLStreamVisitor(cfg.Query, in, out, outErr, logger) + + return &sqlStreamProcessor{ + logger: logger, + config: cfg, + nextConsumer: next, + in: in, + out: out, + outErr: outErr, + processor: processor, + } +} + +func (sp *sqlStreamProcessor) processorLoop() { + for { + select { + case ls := <-sp.out: + printLogs(ls) + case err := <-sp.outErr: + sp.logger.Error("error", zap.Error(err)) + sp.processor.Stop() + } + } +} + +func printLogs(ls plog.LogRecordSlice) { + if ls.Len() == 0 { + return + } + for i := 0; i < ls.Len(); i++ { + fmt.Println("EVENT: ") + ls.At(i).Attributes().Range(func(k string, v pcommon.Value) bool { + if v.Type() == pcommon.ValueTypeMap { + fmt.Printf(" Nested key %q \n", k) + v.MapVal().Range(func(key string, val pcommon.Value) bool { + fmt.Printf("Key: %q: Value: %q \n", key, val.AsString()) + return true + }) + fmt.Println() + return true + } + fmt.Printf("%q: %q \n", k, v.AsString()) + return true + }) + fmt.Print("\n") + } + //fmt.Println("END OF THIS BATCH") +} + +func (sp *sqlStreamProcessor) ConsumeLogs(ctx context.Context, ld plog.Logs) error { + //<-time.After(1 * time.Second) + sp.in <- ld.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords() + return nil +} + +func (sp *sqlStreamProcessor) Capabilities() consumer.Capabilities { + return consumer.Capabilities{MutatesData: true} +} + +// Start is invoked during service startup. +func (sp *sqlStreamProcessor) Start(context.Context, component.Host) error { + go sp.processorLoop() + return nil +} + +// Shutdown is invoked during service shutdown. +func (sp *sqlStreamProcessor) Shutdown(context.Context) error { + return nil +} diff --git a/processor/stream_processor/testdata/config.yaml b/processor/stream_processor/testdata/config.yaml new file mode 100644 index 000000000000..1d3b231ca67d --- /dev/null +++ b/processor/stream_processor/testdata/config.yaml @@ -0,0 +1,18 @@ +receivers: + nop: + + +processors: + sqlStream: + query: "select * where price = 5;" + +exporters: + nop: + + +service: + pipelines: + logs: + receivers: [nop] + processors: [sqlStream] + exporters: [nop] \ No newline at end of file