@@ -9,10 +9,15 @@ import (
99 "github.com/QuesmaOrg/quesma/platform/schema"
1010 "github.com/QuesmaOrg/quesma/platform/types"
1111 "strings"
12+ "sync"
13+ "sync/atomic"
1214)
1315
1416type HydrolixLowerer struct {
15- virtualTableStorage persistence.JSONDatabase
17+ virtualTableStorage persistence.JSONDatabase
18+ ingestCounter int64
19+ ingestFieldStatistics IngestFieldStatistics
20+ ingestFieldStatisticsLock sync.Mutex
1621}
1722
1823func NewHydrolixLowerer (virtualTableStorage persistence.JSONDatabase ) * HydrolixLowerer {
@@ -21,6 +26,54 @@ func NewHydrolixLowerer(virtualTableStorage persistence.JSONDatabase) *HydrolixL
2126 }
2227}
2328
29+ func (ip * HydrolixLowerer ) GenerateIngestContent (table * chLib.Table ,
30+ data types.JSON ,
31+ inValidJson types.JSON ,
32+ encodings map [schema.FieldEncodingKey ]schema.EncodedFieldName ) ([]AlterStatement , types.JSON , []NonSchemaField , error ) {
33+
34+ if len (table .Config .Attributes ) == 0 {
35+ return nil , data , nil , nil
36+ }
37+
38+ mDiff := DifferenceMap (data , table ) // TODO change to DifferenceMap(m, t)
39+
40+ if len (mDiff ) == 0 && len (inValidJson ) == 0 { // no need to modify, just insert 'js'
41+ return nil , data , nil , nil
42+ }
43+
44+ // check attributes precondition
45+ if len (table .Config .Attributes ) <= 0 {
46+ return nil , nil , nil , fmt .Errorf ("no attributes config, but received non-schema fields: %s" , mDiff )
47+ }
48+ attrsMap , _ := BuildAttrsMap (mDiff , table .Config )
49+
50+ // generateNewColumns is called on original attributes map
51+ // before adding invalid fields to it
52+ // otherwise it would contain invalid fields e.g. with wrong types
53+ // we only want to add fields that are not part of the schema e.g we don't
54+ // have columns for them
55+ var alterStatements []AlterStatement
56+ atomic .AddInt64 (& ip .ingestCounter , 1 )
57+ //if ok, alteredAttributesIndexes := ip.shouldAlterColumns(table, attrsMap); ok {
58+ // alterStatements = ip.generateNewColumns(attrsMap, table, alteredAttributesIndexes, encodings)
59+ //}
60+ // If there are some invalid fields, we need to add them to the attributes map
61+ // to not lose them and be able to store them later by
62+ // generating correct update query
63+ // addInvalidJsonFieldsToAttributes returns a new map with invalid fields added
64+ // this map is then used to generate non-schema fields string
65+ attrsMapWithInvalidFields := addInvalidJsonFieldsToAttributes (attrsMap , inValidJson )
66+ nonSchemaFields , err := generateNonSchemaFields (attrsMapWithInvalidFields )
67+
68+ if err != nil {
69+ return nil , nil , nil , err
70+ }
71+
72+ onlySchemaFields := RemoveNonSchemaFields (data , table )
73+
74+ return alterStatements , onlySchemaFields , nonSchemaFields , nil
75+ }
76+
2477func (l * HydrolixLowerer ) LowerToDDL (
2578 validatedJsons []types.JSON ,
2679 table * chLib.Table ,
@@ -68,6 +121,17 @@ func (l *HydrolixLowerer) LowerToDDL(
68121 partitioningField , defaultField ,
69122 partitioningGranularity , defaultGranularity )
70123
124+ for i , preprocessedJson := range validatedJsons {
125+ alter , onlySchemaFields , nonSchemaFields , err := l .GenerateIngestContent (table , preprocessedJson ,
126+ invalidJsons [i ], encodings )
127+ _ = alter
128+ _ = onlySchemaFields
129+ _ = nonSchemaFields
130+ if err != nil {
131+ return nil , fmt .Errorf ("error BuildInsertJson, tablename: '%s' : %v" , table .Name , err )
132+ }
133+ }
134+
71135 result := fmt .Sprintf (`{
72136 "schema": {
73137 "project": "%s",
0 commit comments